Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 1 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 2 | # not use this file except in compliance with the License. You may obtain |
| 3 | # a copy of the License at |
| 4 | # |
| 5 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | # |
| 7 | # Unless required by applicable law or agreed to in writing, software |
| 8 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 | # License for the specific language governing permissions and limitations |
| 11 | # under the License. |
| 12 | |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 13 | import os |
| 14 | import random |
| 15 | import re |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 16 | import subprocess |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 17 | import time |
| 18 | |
Pavlo Shchelokovskyy | 60e0ecd | 2014-12-14 22:17:21 +0200 | [diff] [blame] | 19 | import fixtures |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 20 | from heatclient import exc as heat_exceptions |
Thomas Herve | db36c09 | 2017-03-23 11:20:14 +0100 | [diff] [blame] | 21 | from keystoneauth1 import exceptions as kc_exceptions |
Mark Vanderwiel | 6d8e086 | 2015-10-15 12:51:07 -0500 | [diff] [blame] | 22 | from neutronclient.common import exceptions as network_exceptions |
Steve Baker | 2464129 | 2015-03-13 10:47:50 +1300 | [diff] [blame] | 23 | from oslo_log import log as logging |
Jens Rosenboom | 4f069fb | 2015-02-18 14:19:07 +0100 | [diff] [blame] | 24 | from oslo_utils import timeutils |
Pavlo Shchelokovskyy | 60e0ecd | 2014-12-14 22:17:21 +0200 | [diff] [blame] | 25 | import six |
Sirushti Murugesan | 4920fda | 2015-04-22 00:35:26 +0530 | [diff] [blame] | 26 | from six.moves import urllib |
Pavlo Shchelokovskyy | 60e0ecd | 2014-12-14 22:17:21 +0200 | [diff] [blame] | 27 | import testscenarios |
| 28 | import testtools |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 29 | |
rabi | d2916d0 | 2017-09-22 18:19:24 +0530 | [diff] [blame] | 30 | from heat_tempest_plugin.common import exceptions |
| 31 | from heat_tempest_plugin.common import remote_client |
rabi | d2916d0 | 2017-09-22 18:19:24 +0530 | [diff] [blame] | 32 | from heat_tempest_plugin.services import clients |
Zane Bitter | b4acd96 | 2018-01-18 12:08:23 -0500 | [diff] [blame^] | 33 | from tempest import config |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 34 | |
| 35 | LOG = logging.getLogger(__name__) |
Angus Salkeld | 2404370 | 2014-11-21 08:49:26 +1000 | [diff] [blame] | 36 | _LOG_FORMAT = "%(levelname)8s [%(name)s] %(message)s" |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 37 | |
| 38 | |
Angus Salkeld | 08514ad | 2015-02-06 10:08:31 +1000 | [diff] [blame] | 39 | def call_until_true(duration, sleep_for, func, *args, **kwargs): |
Peter Razumovsky | f0ac958 | 2015-09-24 16:49:03 +0300 | [diff] [blame] | 40 | """Call the function until it returns True or the duration elapsed. |
| 41 | |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 42 | Call the given function until it returns True (and return True) or |
| 43 | until the specified duration (in seconds) elapses (and return |
| 44 | False). |
| 45 | |
| 46 | :param func: A zero argument callable that returns True on success. |
| 47 | :param duration: The number of seconds for which to attempt a |
| 48 | successful call of the function. |
| 49 | :param sleep_for: The number of seconds to sleep after an unsuccessful |
| 50 | invocation of the function. |
| 51 | """ |
| 52 | now = time.time() |
| 53 | timeout = now + duration |
| 54 | while now < timeout: |
Angus Salkeld | 08514ad | 2015-02-06 10:08:31 +1000 | [diff] [blame] | 55 | if func(*args, **kwargs): |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 56 | return True |
| 57 | LOG.debug("Sleeping for %d seconds", sleep_for) |
| 58 | time.sleep(sleep_for) |
| 59 | now = time.time() |
| 60 | return False |
| 61 | |
| 62 | |
rabi | 3220134 | 2017-11-17 22:57:58 +0530 | [diff] [blame] | 63 | def isotime(at): |
| 64 | if at is None: |
| 65 | return None |
| 66 | return at.strftime('%Y-%m-%dT%H:%M:%SZ') |
| 67 | |
| 68 | |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 69 | def rand_name(name=''): |
ricolin | a4eb53d | 2017-04-24 23:51:09 +0800 | [diff] [blame] | 70 | randbits = six.text_type(random.randint(1, 0x7fffffff)) |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 71 | if name: |
| 72 | return name + '-' + randbits |
| 73 | else: |
| 74 | return randbits |
| 75 | |
| 76 | |
Zane Bitter | f407e10 | 2017-10-05 14:19:32 -0400 | [diff] [blame] | 77 | def requires_convergence(test_method): |
| 78 | '''Decorator for convergence-only tests. |
| 79 | |
| 80 | The decorated test will be skipped when convergence is disabled. |
| 81 | ''' |
rabi | f89752b | 2017-11-18 22:14:30 +0530 | [diff] [blame] | 82 | plugin = config.CONF.heat_plugin |
rabi | 94a520a | 2017-11-17 22:49:17 +0530 | [diff] [blame] | 83 | convergence_enabled = plugin.convergence_engine_enabled |
Zane Bitter | f407e10 | 2017-10-05 14:19:32 -0400 | [diff] [blame] | 84 | skipper = testtools.skipUnless(convergence_enabled, |
| 85 | "Convergence-only tests are disabled") |
| 86 | return skipper(test_method) |
| 87 | |
| 88 | |
Angus Salkeld | 95f65a2 | 2014-11-24 12:38:30 +1000 | [diff] [blame] | 89 | class HeatIntegrationTest(testscenarios.WithScenarios, |
| 90 | testtools.TestCase): |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 91 | |
| 92 | def setUp(self): |
| 93 | super(HeatIntegrationTest, self).setUp() |
| 94 | |
rabi | f89752b | 2017-11-18 22:14:30 +0530 | [diff] [blame] | 95 | self.conf = config.CONF.heat_plugin |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 96 | |
| 97 | self.assertIsNotNone(self.conf.auth_url, |
| 98 | 'No auth_url configured') |
| 99 | self.assertIsNotNone(self.conf.username, |
| 100 | 'No username configured') |
| 101 | self.assertIsNotNone(self.conf.password, |
| 102 | 'No password configured') |
rabi | fd98a47 | 2016-05-24 10:18:33 +0530 | [diff] [blame] | 103 | self.setup_clients(self.conf) |
| 104 | self.useFixture(fixtures.FakeLogger(format=_LOG_FORMAT)) |
| 105 | self.updated_time = {} |
| 106 | if self.conf.disable_ssl_certificate_validation: |
| 107 | self.verify_cert = False |
| 108 | else: |
| 109 | self.verify_cert = self.conf.ca_file or True |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 110 | |
Steve Baker | b752e91 | 2016-08-01 22:05:37 +0000 | [diff] [blame] | 111 | def setup_clients(self, conf, admin_credentials=False): |
| 112 | self.manager = clients.ClientManager(conf, admin_credentials) |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 113 | self.identity_client = self.manager.identity_client |
| 114 | self.orchestration_client = self.manager.orchestration_client |
| 115 | self.compute_client = self.manager.compute_client |
| 116 | self.network_client = self.manager.network_client |
| 117 | self.volume_client = self.manager.volume_client |
Angus Salkeld | 4408da3 | 2015-02-03 18:53:30 +1000 | [diff] [blame] | 118 | self.object_client = self.manager.object_client |
rabi | d69f031 | 2017-10-26 14:52:52 +0530 | [diff] [blame] | 119 | self.metric_client = self.manager.metric_client |
rabi | fd98a47 | 2016-05-24 10:18:33 +0530 | [diff] [blame] | 120 | |
| 121 | self.client = self.orchestration_client |
| 122 | |
| 123 | def setup_clients_for_admin(self): |
Steve Baker | b752e91 | 2016-08-01 22:05:37 +0000 | [diff] [blame] | 124 | self.setup_clients(self.conf, True) |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 125 | |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 126 | def get_remote_client(self, server_or_ip, username, private_key=None): |
| 127 | if isinstance(server_or_ip, six.string_types): |
| 128 | ip = server_or_ip |
| 129 | else: |
| 130 | network_name_for_ssh = self.conf.network_for_ssh |
| 131 | ip = server_or_ip.networks[network_name_for_ssh][0] |
| 132 | if private_key is None: |
| 133 | private_key = self.keypair.private_key |
| 134 | linux_client = remote_client.RemoteClient(ip, username, |
| 135 | pkey=private_key, |
| 136 | conf=self.conf) |
| 137 | try: |
| 138 | linux_client.validate_authentication() |
| 139 | except exceptions.SSHTimeout: |
liyi | 09461f7 | 2017-03-21 12:17:51 +0800 | [diff] [blame] | 140 | LOG.exception('ssh connection to %s failed', ip) |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 141 | raise |
| 142 | |
| 143 | return linux_client |
| 144 | |
Anastasia Kuznetsova | 3e0ab4d | 2015-03-06 18:10:13 +0400 | [diff] [blame] | 145 | def check_connectivity(self, check_ip): |
| 146 | def try_connect(ip): |
| 147 | try: |
Sirushti Murugesan | 4920fda | 2015-04-22 00:35:26 +0530 | [diff] [blame] | 148 | urllib.request.urlopen('http://%s/' % ip) |
Anastasia Kuznetsova | 3e0ab4d | 2015-03-06 18:10:13 +0400 | [diff] [blame] | 149 | return True |
| 150 | except IOError: |
| 151 | return False |
| 152 | |
| 153 | timeout = self.conf.connectivity_timeout |
| 154 | elapsed_time = 0 |
| 155 | while not try_connect(check_ip): |
| 156 | time.sleep(10) |
| 157 | elapsed_time += 10 |
| 158 | if elapsed_time > timeout: |
| 159 | raise exceptions.TimeoutException() |
| 160 | |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 161 | def _log_console_output(self, servers=None): |
| 162 | if not servers: |
| 163 | servers = self.compute_client.servers.list() |
| 164 | for server in servers: |
Steve Baker | 2464129 | 2015-03-13 10:47:50 +1300 | [diff] [blame] | 165 | LOG.info('Console output for %s', server.id) |
| 166 | LOG.info(server.get_console_output()) |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 167 | |
Sergey Kraynev | d6fa5c0 | 2015-02-13 03:03:55 -0500 | [diff] [blame] | 168 | def _load_template(self, base_file, file_name, sub_dir=None): |
| 169 | sub_dir = sub_dir or '' |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 170 | filepath = os.path.join(os.path.dirname(os.path.realpath(base_file)), |
Sergey Kraynev | d6fa5c0 | 2015-02-13 03:03:55 -0500 | [diff] [blame] | 171 | sub_dir, file_name) |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 172 | with open(filepath) as f: |
| 173 | return f.read() |
| 174 | |
| 175 | def create_keypair(self, client=None, name=None): |
| 176 | if client is None: |
| 177 | client = self.compute_client |
| 178 | if name is None: |
| 179 | name = rand_name('heat-keypair') |
| 180 | keypair = client.keypairs.create(name) |
| 181 | self.assertEqual(keypair.name, name) |
| 182 | |
| 183 | def delete_keypair(): |
| 184 | keypair.delete() |
| 185 | |
| 186 | self.addCleanup(delete_keypair) |
| 187 | return keypair |
| 188 | |
Sergey Kraynev | a265c13 | 2015-02-13 03:51:03 -0500 | [diff] [blame] | 189 | def assign_keypair(self): |
| 190 | if self.conf.keypair_name: |
| 191 | self.keypair = None |
| 192 | self.keypair_name = self.conf.keypair_name |
| 193 | else: |
| 194 | self.keypair = self.create_keypair() |
| 195 | self.keypair_name = self.keypair.id |
| 196 | |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 197 | @classmethod |
| 198 | def _stack_rand_name(cls): |
| 199 | return rand_name(cls.__name__) |
| 200 | |
Anastasia Kuznetsova | 673fc43 | 2015-03-12 16:41:36 +0400 | [diff] [blame] | 201 | def _get_network(self, net_name=None): |
| 202 | if net_name is None: |
| 203 | net_name = self.conf.fixed_network_name |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 204 | networks = self.network_client.list_networks() |
| 205 | for net in networks['networks']: |
Anastasia Kuznetsova | 673fc43 | 2015-03-12 16:41:36 +0400 | [diff] [blame] | 206 | if net['name'] == net_name: |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 207 | return net |
| 208 | |
Mark Vanderwiel | 6d8e086 | 2015-10-15 12:51:07 -0500 | [diff] [blame] | 209 | def is_network_extension_supported(self, extension_alias): |
| 210 | try: |
| 211 | self.network_client.show_extension(extension_alias) |
| 212 | except network_exceptions.NeutronClientException: |
| 213 | return False |
| 214 | return True |
| 215 | |
Thomas Herve | db36c09 | 2017-03-23 11:20:14 +0100 | [diff] [blame] | 216 | def is_service_available(self, service_type): |
| 217 | try: |
| 218 | self.identity_client.get_endpoint_url( |
| 219 | service_type, self.conf.region) |
| 220 | except kc_exceptions.EndpointNotFound: |
| 221 | return False |
| 222 | else: |
| 223 | return True |
| 224 | |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 225 | @staticmethod |
Angus Salkeld | a89a028 | 2015-07-24 15:47:38 +1000 | [diff] [blame] | 226 | def _stack_output(stack, output_key, validate_errors=True): |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 227 | """Return a stack output value for a given key.""" |
Angus Salkeld | a89a028 | 2015-07-24 15:47:38 +1000 | [diff] [blame] | 228 | value = None |
| 229 | for o in stack.outputs: |
| 230 | if validate_errors and 'output_error' in o: |
| 231 | # scan for errors in the stack output. |
| 232 | raise ValueError( |
| 233 | 'Unexpected output errors in %s : %s' % ( |
| 234 | output_key, o['output_error'])) |
| 235 | if o['output_key'] == output_key: |
| 236 | value = o['output_value'] |
| 237 | return value |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 238 | |
| 239 | def _ping_ip_address(self, ip_address, should_succeed=True): |
| 240 | cmd = ['ping', '-c1', '-w1', ip_address] |
| 241 | |
| 242 | def ping(): |
| 243 | proc = subprocess.Popen(cmd, |
| 244 | stdout=subprocess.PIPE, |
| 245 | stderr=subprocess.PIPE) |
| 246 | proc.wait() |
| 247 | return (proc.returncode == 0) == should_succeed |
| 248 | |
| 249 | return call_until_true( |
Angus Salkeld | 08514ad | 2015-02-06 10:08:31 +1000 | [diff] [blame] | 250 | self.conf.build_timeout, 1, ping) |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 251 | |
Angus Salkeld | a7500d1 | 2015-04-10 15:44:07 +1000 | [diff] [blame] | 252 | def _wait_for_all_resource_status(self, stack_identifier, |
| 253 | status, failure_pattern='^.*_FAILED$', |
| 254 | success_on_not_found=False): |
| 255 | for res in self.client.resources.list(stack_identifier): |
| 256 | self._wait_for_resource_status( |
| 257 | stack_identifier, res.resource_name, |
| 258 | status, failure_pattern=failure_pattern, |
| 259 | success_on_not_found=success_on_not_found) |
| 260 | |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 261 | def _wait_for_resource_status(self, stack_identifier, resource_name, |
| 262 | status, failure_pattern='^.*_FAILED$', |
| 263 | success_on_not_found=False): |
| 264 | """Waits for a Resource to reach a given status.""" |
| 265 | fail_regexp = re.compile(failure_pattern) |
| 266 | build_timeout = self.conf.build_timeout |
| 267 | build_interval = self.conf.build_interval |
| 268 | |
| 269 | start = timeutils.utcnow() |
| 270 | while timeutils.delta_seconds(start, |
| 271 | timeutils.utcnow()) < build_timeout: |
| 272 | try: |
| 273 | res = self.client.resources.get( |
| 274 | stack_identifier, resource_name) |
| 275 | except heat_exceptions.HTTPNotFound: |
| 276 | if success_on_not_found: |
| 277 | return |
| 278 | # ignore this, as the resource may not have |
| 279 | # been created yet |
| 280 | else: |
| 281 | if res.resource_status == status: |
| 282 | return |
Sirushti Murugesan | e5ad25b | 2015-04-18 23:30:59 +0530 | [diff] [blame] | 283 | wait_for_action = status.split('_')[0] |
| 284 | resource_action = res.resource_status.split('_')[0] |
| 285 | if (resource_action == wait_for_action and |
| 286 | fail_regexp.search(res.resource_status)): |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 287 | raise exceptions.StackResourceBuildErrorException( |
| 288 | resource_name=res.resource_name, |
| 289 | stack_identifier=stack_identifier, |
| 290 | resource_status=res.resource_status, |
| 291 | resource_status_reason=res.resource_status_reason) |
| 292 | time.sleep(build_interval) |
| 293 | |
| 294 | message = ('Resource %s failed to reach %s status within ' |
| 295 | 'the required time (%s s).' % |
Anastasia Kuznetsova | 9a74557 | 2015-03-04 13:19:15 +0400 | [diff] [blame] | 296 | (resource_name, status, build_timeout)) |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 297 | raise exceptions.TimeoutException(message) |
| 298 | |
Rabi Mishra | 87be9b4 | 2016-02-15 14:15:50 +0530 | [diff] [blame] | 299 | def verify_resource_status(self, stack_identifier, resource_name, |
| 300 | status='CREATE_COMPLETE'): |
| 301 | try: |
| 302 | res = self.client.resources.get(stack_identifier, resource_name) |
| 303 | except heat_exceptions.HTTPNotFound: |
| 304 | return False |
| 305 | return res.resource_status == status |
| 306 | |
rabi | 5eaa496 | 2017-08-31 10:55:13 +0530 | [diff] [blame] | 307 | def _verify_status(self, stack, stack_identifier, status, |
| 308 | fail_regexp, is_action_cancelled=False): |
Sirushti Murugesan | 13a8a17 | 2015-04-14 00:30:05 +0530 | [diff] [blame] | 309 | if stack.stack_status == status: |
Sergey Kraynev | 89082a3 | 2015-09-04 04:42:33 -0400 | [diff] [blame] | 310 | # Handle UPDATE_COMPLETE/FAILED case: Make sure we don't |
| 311 | # wait for a stale UPDATE_COMPLETE/FAILED status. |
| 312 | if status in ('UPDATE_FAILED', 'UPDATE_COMPLETE'): |
rabi | 5eaa496 | 2017-08-31 10:55:13 +0530 | [diff] [blame] | 313 | if is_action_cancelled: |
| 314 | return True |
| 315 | |
Sirushti Murugesan | 13a8a17 | 2015-04-14 00:30:05 +0530 | [diff] [blame] | 316 | if self.updated_time.get( |
| 317 | stack_identifier) != stack.updated_time: |
| 318 | self.updated_time[stack_identifier] = stack.updated_time |
| 319 | return True |
Thomas Herve | 0e8567e | 2016-09-22 15:07:37 +0200 | [diff] [blame] | 320 | elif status == 'DELETE_COMPLETE' and stack.deletion_time is None: |
| 321 | # Wait for deleted_time to be filled, so that we have more |
| 322 | # confidence the operation is finished. |
| 323 | return False |
Sirushti Murugesan | 13a8a17 | 2015-04-14 00:30:05 +0530 | [diff] [blame] | 324 | else: |
| 325 | return True |
| 326 | |
Sirushti Murugesan | e5ad25b | 2015-04-18 23:30:59 +0530 | [diff] [blame] | 327 | wait_for_action = status.split('_')[0] |
| 328 | if (stack.action == wait_for_action and |
| 329 | fail_regexp.search(stack.stack_status)): |
Sergey Kraynev | 89082a3 | 2015-09-04 04:42:33 -0400 | [diff] [blame] | 330 | # Handle UPDATE_COMPLETE/UPDATE_FAILED case. |
| 331 | if status in ('UPDATE_FAILED', 'UPDATE_COMPLETE'): |
Sirushti Murugesan | 13a8a17 | 2015-04-14 00:30:05 +0530 | [diff] [blame] | 332 | if self.updated_time.get( |
| 333 | stack_identifier) != stack.updated_time: |
| 334 | self.updated_time[stack_identifier] = stack.updated_time |
| 335 | raise exceptions.StackBuildErrorException( |
| 336 | stack_identifier=stack_identifier, |
| 337 | stack_status=stack.stack_status, |
| 338 | stack_status_reason=stack.stack_status_reason) |
| 339 | else: |
| 340 | raise exceptions.StackBuildErrorException( |
| 341 | stack_identifier=stack_identifier, |
| 342 | stack_status=stack.stack_status, |
| 343 | stack_status_reason=stack.stack_status_reason) |
| 344 | |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 345 | def _wait_for_stack_status(self, stack_identifier, status, |
Sergey Kraynev | 89082a3 | 2015-09-04 04:42:33 -0400 | [diff] [blame] | 346 | failure_pattern=None, |
Rabi Mishra | 81ca6bc | 2016-06-16 15:09:20 +0530 | [diff] [blame] | 347 | success_on_not_found=False, |
| 348 | signal_required=False, |
rabi | 5eaa496 | 2017-08-31 10:55:13 +0530 | [diff] [blame] | 349 | resources_to_signal=None, |
| 350 | is_action_cancelled=False): |
Peter Razumovsky | f0ac958 | 2015-09-24 16:49:03 +0300 | [diff] [blame] | 351 | """Waits for a Stack to reach a given status. |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 352 | |
| 353 | Note this compares the full $action_$status, e.g |
| 354 | CREATE_COMPLETE, not just COMPLETE which is exposed |
| 355 | via the status property of Stack in heatclient |
| 356 | """ |
Sergey Kraynev | 89082a3 | 2015-09-04 04:42:33 -0400 | [diff] [blame] | 357 | if failure_pattern: |
| 358 | fail_regexp = re.compile(failure_pattern) |
| 359 | elif 'FAILED' in status: |
| 360 | # If we're looking for e.g CREATE_FAILED, COMPLETE is unexpected. |
| 361 | fail_regexp = re.compile('^.*_COMPLETE$') |
| 362 | else: |
| 363 | fail_regexp = re.compile('^.*_FAILED$') |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 364 | build_timeout = self.conf.build_timeout |
| 365 | build_interval = self.conf.build_interval |
| 366 | |
| 367 | start = timeutils.utcnow() |
| 368 | while timeutils.delta_seconds(start, |
| 369 | timeutils.utcnow()) < build_timeout: |
| 370 | try: |
Sergey Kraynev | f07f471 | 2016-02-15 05:24:17 -0500 | [diff] [blame] | 371 | stack = self.client.stacks.get(stack_identifier, |
| 372 | resolve_outputs=False) |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 373 | except heat_exceptions.HTTPNotFound: |
| 374 | if success_on_not_found: |
| 375 | return |
| 376 | # ignore this, as the resource may not have |
| 377 | # been created yet |
| 378 | else: |
Sirushti Murugesan | 13a8a17 | 2015-04-14 00:30:05 +0530 | [diff] [blame] | 379 | if self._verify_status(stack, stack_identifier, status, |
rabi | 5eaa496 | 2017-08-31 10:55:13 +0530 | [diff] [blame] | 380 | fail_regexp, is_action_cancelled): |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 381 | return |
Rabi Mishra | 81ca6bc | 2016-06-16 15:09:20 +0530 | [diff] [blame] | 382 | if signal_required: |
| 383 | self.signal_resources(resources_to_signal) |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 384 | time.sleep(build_interval) |
| 385 | |
| 386 | message = ('Stack %s failed to reach %s status within ' |
| 387 | 'the required time (%s s).' % |
Anastasia Kuznetsova | 9a74557 | 2015-03-04 13:19:15 +0400 | [diff] [blame] | 388 | (stack_identifier, status, build_timeout)) |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 389 | raise exceptions.TimeoutException(message) |
| 390 | |
| 391 | def _stack_delete(self, stack_identifier): |
| 392 | try: |
Thomas Herve | 3eab294 | 2015-10-22 17:29:21 +0200 | [diff] [blame] | 393 | self._handle_in_progress(self.client.stacks.delete, |
| 394 | stack_identifier) |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 395 | except heat_exceptions.HTTPNotFound: |
| 396 | pass |
| 397 | self._wait_for_stack_status( |
| 398 | stack_identifier, 'DELETE_COMPLETE', |
| 399 | success_on_not_found=True) |
Steven Hardy | c9efd97 | 2014-11-20 11:31:55 +0000 | [diff] [blame] | 400 | |
Thomas Herve | 3eab294 | 2015-10-22 17:29:21 +0200 | [diff] [blame] | 401 | def _handle_in_progress(self, fn, *args, **kwargs): |
| 402 | build_timeout = self.conf.build_timeout |
| 403 | build_interval = self.conf.build_interval |
| 404 | start = timeutils.utcnow() |
| 405 | while timeutils.delta_seconds(start, |
| 406 | timeutils.utcnow()) < build_timeout: |
| 407 | try: |
| 408 | fn(*args, **kwargs) |
| 409 | except heat_exceptions.HTTPConflict as ex: |
| 410 | # FIXME(sirushtim): Wait a little for the stack lock to be |
| 411 | # released and hopefully, the stack should be usable again. |
| 412 | if ex.error['error']['type'] != 'ActionInProgress': |
| 413 | raise ex |
| 414 | |
| 415 | time.sleep(build_interval) |
| 416 | else: |
| 417 | break |
| 418 | |
Steven Hardy | 23284b6 | 2015-10-01 19:03:42 +0100 | [diff] [blame] | 419 | def update_stack(self, stack_identifier, template=None, environment=None, |
Sabeen Syed | 277ea69 | 2015-02-04 23:30:02 +0000 | [diff] [blame] | 420 | files=None, parameters=None, tags=None, |
Rakesh H S | a3325d6 | 2015-04-04 19:42:29 +0530 | [diff] [blame] | 421 | expected_status='UPDATE_COMPLETE', |
Steven Hardy | 23284b6 | 2015-10-01 19:03:42 +0100 | [diff] [blame] | 422 | disable_rollback=True, |
| 423 | existing=False): |
Steven Hardy | c9efd97 | 2014-11-20 11:31:55 +0000 | [diff] [blame] | 424 | env = environment or {} |
| 425 | env_files = files or {} |
Sergey Kraynev | bcc78df | 2015-02-27 04:34:32 -0500 | [diff] [blame] | 426 | parameters = parameters or {} |
Sirushti Murugesan | 3a195a5 | 2015-05-01 09:47:09 +0530 | [diff] [blame] | 427 | |
Sergey Kraynev | 89082a3 | 2015-09-04 04:42:33 -0400 | [diff] [blame] | 428 | self.updated_time[stack_identifier] = self.client.stacks.get( |
Sergey Kraynev | f07f471 | 2016-02-15 05:24:17 -0500 | [diff] [blame] | 429 | stack_identifier, resolve_outputs=False).updated_time |
Sirushti Murugesan | 3a195a5 | 2015-05-01 09:47:09 +0530 | [diff] [blame] | 430 | |
Thomas Herve | 3eab294 | 2015-10-22 17:29:21 +0200 | [diff] [blame] | 431 | self._handle_in_progress( |
| 432 | self.client.stacks.update, |
| 433 | stack_id=stack_identifier, |
Thomas Herve | 3eab294 | 2015-10-22 17:29:21 +0200 | [diff] [blame] | 434 | template=template, |
| 435 | files=env_files, |
| 436 | disable_rollback=disable_rollback, |
| 437 | parameters=parameters, |
| 438 | environment=env, |
| 439 | tags=tags, |
| 440 | existing=existing) |
Sirushti Murugesan | 3a195a5 | 2015-05-01 09:47:09 +0530 | [diff] [blame] | 441 | |
Rakesh H S | a3325d6 | 2015-04-04 19:42:29 +0530 | [diff] [blame] | 442 | kwargs = {'stack_identifier': stack_identifier, |
| 443 | 'status': expected_status} |
| 444 | if expected_status in ['ROLLBACK_COMPLETE']: |
Rakesh H S | a3325d6 | 2015-04-04 19:42:29 +0530 | [diff] [blame] | 445 | # To trigger rollback you would intentionally fail the stack |
| 446 | # Hence check for rollback failures |
| 447 | kwargs['failure_pattern'] = '^ROLLBACK_FAILED$' |
| 448 | |
| 449 | self._wait_for_stack_status(**kwargs) |
Steven Hardy | c9efd97 | 2014-11-20 11:31:55 +0000 | [diff] [blame] | 450 | |
rabi | 5eaa496 | 2017-08-31 10:55:13 +0530 | [diff] [blame] | 451 | def cancel_update_stack(self, stack_identifier, rollback=True, |
Oleksii Chuprykov | fc2c58f | 2016-04-29 17:03:17 +0300 | [diff] [blame] | 452 | expected_status='ROLLBACK_COMPLETE'): |
| 453 | |
| 454 | stack_name = stack_identifier.split('/')[0] |
| 455 | |
| 456 | self.updated_time[stack_identifier] = self.client.stacks.get( |
| 457 | stack_identifier, resolve_outputs=False).updated_time |
| 458 | |
rabi | 5eaa496 | 2017-08-31 10:55:13 +0530 | [diff] [blame] | 459 | if rollback: |
| 460 | self.client.actions.cancel_update(stack_name) |
| 461 | else: |
| 462 | self.client.actions.cancel_without_rollback(stack_name) |
Oleksii Chuprykov | fc2c58f | 2016-04-29 17:03:17 +0300 | [diff] [blame] | 463 | |
| 464 | kwargs = {'stack_identifier': stack_identifier, |
| 465 | 'status': expected_status} |
rabi | 5eaa496 | 2017-08-31 10:55:13 +0530 | [diff] [blame] | 466 | if expected_status == 'UPDATE_FAILED': |
| 467 | kwargs['is_action_cancelled'] = True |
| 468 | |
Oleksii Chuprykov | fc2c58f | 2016-04-29 17:03:17 +0300 | [diff] [blame] | 469 | if expected_status in ['ROLLBACK_COMPLETE']: |
| 470 | # To trigger rollback you would intentionally fail the stack |
| 471 | # Hence check for rollback failures |
| 472 | kwargs['failure_pattern'] = '^ROLLBACK_FAILED$' |
| 473 | |
| 474 | self._wait_for_stack_status(**kwargs) |
| 475 | |
Jason Dunsmore | b5aa902 | 2015-09-09 16:57:04 -0500 | [diff] [blame] | 476 | def preview_update_stack(self, stack_identifier, template, |
| 477 | environment=None, files=None, parameters=None, |
Steven Hardy | e6de2d6 | 2015-12-07 15:59:09 +0000 | [diff] [blame] | 478 | tags=None, disable_rollback=True, |
| 479 | show_nested=False): |
Jason Dunsmore | b5aa902 | 2015-09-09 16:57:04 -0500 | [diff] [blame] | 480 | env = environment or {} |
| 481 | env_files = files or {} |
| 482 | parameters = parameters or {} |
Jason Dunsmore | b5aa902 | 2015-09-09 16:57:04 -0500 | [diff] [blame] | 483 | |
| 484 | return self.client.stacks.preview_update( |
| 485 | stack_id=stack_identifier, |
Jason Dunsmore | b5aa902 | 2015-09-09 16:57:04 -0500 | [diff] [blame] | 486 | template=template, |
| 487 | files=env_files, |
| 488 | disable_rollback=disable_rollback, |
| 489 | parameters=parameters, |
| 490 | environment=env, |
Steven Hardy | e6de2d6 | 2015-12-07 15:59:09 +0000 | [diff] [blame] | 491 | tags=tags, |
| 492 | show_nested=show_nested |
Jason Dunsmore | b5aa902 | 2015-09-09 16:57:04 -0500 | [diff] [blame] | 493 | ) |
| 494 | |
Steven Hardy | 03da074 | 2015-03-19 00:13:17 -0400 | [diff] [blame] | 495 | def assert_resource_is_a_stack(self, stack_identifier, res_name, |
| 496 | wait=False): |
| 497 | build_timeout = self.conf.build_timeout |
| 498 | build_interval = self.conf.build_interval |
| 499 | start = timeutils.utcnow() |
| 500 | while timeutils.delta_seconds(start, |
| 501 | timeutils.utcnow()) < build_timeout: |
| 502 | time.sleep(build_interval) |
| 503 | try: |
| 504 | nested_identifier = self._get_nested_identifier( |
| 505 | stack_identifier, res_name) |
| 506 | except Exception: |
| 507 | # We may have to wait, if the create is in-progress |
| 508 | if wait: |
| 509 | time.sleep(build_interval) |
| 510 | else: |
| 511 | raise |
| 512 | else: |
| 513 | return nested_identifier |
| 514 | |
| 515 | def _get_nested_identifier(self, stack_identifier, res_name): |
Angus Salkeld | 2bd63a4 | 2015-01-07 11:11:29 +1000 | [diff] [blame] | 516 | rsrc = self.client.resources.get(stack_identifier, res_name) |
| 517 | nested_link = [l for l in rsrc.links if l['rel'] == 'nested'] |
| 518 | nested_href = nested_link[0]['href'] |
| 519 | nested_id = nested_href.split('/')[-1] |
| 520 | nested_identifier = '/'.join(nested_href.split('/')[-2:]) |
| 521 | self.assertEqual(rsrc.physical_resource_id, nested_id) |
| 522 | |
Sergey Kraynev | f07f471 | 2016-02-15 05:24:17 -0500 | [diff] [blame] | 523 | nested_stack = self.client.stacks.get(nested_id, resolve_outputs=False) |
Angus Salkeld | 2bd63a4 | 2015-01-07 11:11:29 +1000 | [diff] [blame] | 524 | nested_identifier2 = '%s/%s' % (nested_stack.stack_name, |
| 525 | nested_stack.id) |
| 526 | self.assertEqual(nested_identifier, nested_identifier2) |
| 527 | parent_id = stack_identifier.split("/")[-1] |
| 528 | self.assertEqual(parent_id, nested_stack.parent) |
| 529 | return nested_identifier |
| 530 | |
Rabi Mishra | 8bcff8a | 2015-09-21 18:15:04 +0530 | [diff] [blame] | 531 | def group_nested_identifier(self, stack_identifier, |
| 532 | group_name): |
| 533 | # Get the nested stack identifier from a group resource |
| 534 | rsrc = self.client.resources.get(stack_identifier, group_name) |
| 535 | physical_resource_id = rsrc.physical_resource_id |
| 536 | |
Sergey Kraynev | f07f471 | 2016-02-15 05:24:17 -0500 | [diff] [blame] | 537 | nested_stack = self.client.stacks.get(physical_resource_id, |
| 538 | resolve_outputs=False) |
Rabi Mishra | 8bcff8a | 2015-09-21 18:15:04 +0530 | [diff] [blame] | 539 | nested_identifier = '%s/%s' % (nested_stack.stack_name, |
| 540 | nested_stack.id) |
| 541 | parent_id = stack_identifier.split("/")[-1] |
| 542 | self.assertEqual(parent_id, nested_stack.parent) |
| 543 | return nested_identifier |
| 544 | |
| 545 | def list_group_resources(self, stack_identifier, |
| 546 | group_name, minimal=True): |
| 547 | nested_identifier = self.group_nested_identifier(stack_identifier, |
| 548 | group_name) |
| 549 | if minimal: |
| 550 | return self.list_resources(nested_identifier) |
| 551 | return self.client.resources.list(nested_identifier) |
| 552 | |
Steven Hardy | c9efd97 | 2014-11-20 11:31:55 +0000 | [diff] [blame] | 553 | def list_resources(self, stack_identifier): |
| 554 | resources = self.client.resources.list(stack_identifier) |
| 555 | return dict((r.resource_name, r.resource_type) for r in resources) |
Steven Hardy | f2c82c0 | 2014-11-20 14:02:17 +0000 | [diff] [blame] | 556 | |
Steven Hardy | d448dae | 2016-06-14 14:57:28 +0100 | [diff] [blame] | 557 | def get_resource_stack_id(self, r): |
| 558 | stack_link = [l for l in r.links if l.get('rel') == 'stack'][0] |
| 559 | return stack_link['href'].split("/")[-1] |
| 560 | |
Botond Zoltán | e0b7aa1 | 2017-03-28 08:42:16 +0200 | [diff] [blame] | 561 | def get_physical_resource_id(self, stack_identifier, resource_name): |
| 562 | try: |
| 563 | resource = self.client.resources.get( |
| 564 | stack_identifier, resource_name) |
| 565 | return resource.physical_resource_id |
| 566 | except Exception: |
| 567 | raise Exception('Resource (%s) not found in stack (%s)!' % |
| 568 | (stack_identifier, resource_name)) |
| 569 | |
| 570 | def get_stack_output(self, stack_identifier, output_key, |
| 571 | validate_errors=True): |
| 572 | stack = self.client.stacks.get(stack_identifier) |
| 573 | return self._stack_output(stack, output_key, validate_errors) |
| 574 | |
Rabi Mishra | 81ca6bc | 2016-06-16 15:09:20 +0530 | [diff] [blame] | 575 | def check_input_values(self, group_resources, key, value): |
| 576 | # Check inputs for deployment and derived config |
| 577 | for r in group_resources: |
| 578 | d = self.client.software_deployments.get( |
| 579 | r.physical_resource_id) |
| 580 | self.assertEqual({key: value}, d.input_values) |
| 581 | c = self.client.software_configs.get( |
| 582 | d.config_id) |
| 583 | foo_input_c = [i for i in c.inputs if i.get('name') == key][0] |
| 584 | self.assertEqual(value, foo_input_c.get('value')) |
| 585 | |
| 586 | def signal_resources(self, resources): |
| 587 | # Signal all IN_PROGRESS resources |
| 588 | for r in resources: |
| 589 | if 'IN_PROGRESS' in r.resource_status: |
| 590 | stack_id = self.get_resource_stack_id(r) |
| 591 | self.client.resources.signal(stack_id, r.resource_name) |
| 592 | |
Steven Hardy | f2c82c0 | 2014-11-20 14:02:17 +0000 | [diff] [blame] | 593 | def stack_create(self, stack_name=None, template=None, files=None, |
Sabeen Syed | 277ea69 | 2015-02-04 23:30:02 +0000 | [diff] [blame] | 594 | parameters=None, environment=None, tags=None, |
| 595 | expected_status='CREATE_COMPLETE', |
Jay Dobies | 39c4ce4 | 2015-11-04 10:49:08 -0500 | [diff] [blame] | 596 | disable_rollback=True, enable_cleanup=True, |
rabi | 6ce8d96 | 2017-07-10 16:40:12 +0530 | [diff] [blame] | 597 | environment_files=None, timeout=None): |
Steven Hardy | f2c82c0 | 2014-11-20 14:02:17 +0000 | [diff] [blame] | 598 | name = stack_name or self._stack_rand_name() |
| 599 | templ = template or self.template |
| 600 | templ_files = files or {} |
| 601 | params = parameters or {} |
| 602 | env = environment or {} |
rabi | 6ce8d96 | 2017-07-10 16:40:12 +0530 | [diff] [blame] | 603 | timeout_mins = timeout or self.conf.build_timeout |
Steven Hardy | f2c82c0 | 2014-11-20 14:02:17 +0000 | [diff] [blame] | 604 | self.client.stacks.create( |
| 605 | stack_name=name, |
| 606 | template=templ, |
| 607 | files=templ_files, |
Rakesh H S | a3325d6 | 2015-04-04 19:42:29 +0530 | [diff] [blame] | 608 | disable_rollback=disable_rollback, |
Steven Hardy | f2c82c0 | 2014-11-20 14:02:17 +0000 | [diff] [blame] | 609 | parameters=params, |
Sabeen Syed | 277ea69 | 2015-02-04 23:30:02 +0000 | [diff] [blame] | 610 | environment=env, |
Jay Dobies | 39c4ce4 | 2015-11-04 10:49:08 -0500 | [diff] [blame] | 611 | tags=tags, |
rabi | 6ce8d96 | 2017-07-10 16:40:12 +0530 | [diff] [blame] | 612 | environment_files=environment_files, |
| 613 | timeout_mins=timeout_mins |
Steven Hardy | f2c82c0 | 2014-11-20 14:02:17 +0000 | [diff] [blame] | 614 | ) |
rabi | c570e0f | 2017-10-26 13:07:13 +0530 | [diff] [blame] | 615 | if enable_cleanup: |
Steve Baker | dbea6ab | 2015-08-19 13:37:08 +1200 | [diff] [blame] | 616 | self.addCleanup(self._stack_delete, name) |
Steven Hardy | f2c82c0 | 2014-11-20 14:02:17 +0000 | [diff] [blame] | 617 | |
Sergey Kraynev | f07f471 | 2016-02-15 05:24:17 -0500 | [diff] [blame] | 618 | stack = self.client.stacks.get(name, resolve_outputs=False) |
Steven Hardy | f2c82c0 | 2014-11-20 14:02:17 +0000 | [diff] [blame] | 619 | stack_identifier = '%s/%s' % (name, stack.id) |
Rakesh H S | a3325d6 | 2015-04-04 19:42:29 +0530 | [diff] [blame] | 620 | kwargs = {'stack_identifier': stack_identifier, |
| 621 | 'status': expected_status} |
Steve Baker | f6c8f12 | 2015-02-10 13:54:46 +1300 | [diff] [blame] | 622 | if expected_status: |
Rakesh H S | a3325d6 | 2015-04-04 19:42:29 +0530 | [diff] [blame] | 623 | if expected_status in ['ROLLBACK_COMPLETE']: |
| 624 | # To trigger rollback you would intentionally fail the stack |
| 625 | # Hence check for rollback failures |
| 626 | kwargs['failure_pattern'] = '^ROLLBACK_FAILED$' |
| 627 | self._wait_for_stack_status(**kwargs) |
Steven Hardy | f2c82c0 | 2014-11-20 14:02:17 +0000 | [diff] [blame] | 628 | return stack_identifier |
Angus Salkeld | 2bd63a4 | 2015-01-07 11:11:29 +1000 | [diff] [blame] | 629 | |
| 630 | def stack_adopt(self, stack_name=None, files=None, |
| 631 | parameters=None, environment=None, adopt_data=None, |
| 632 | wait_for_status='ADOPT_COMPLETE'): |
Rabi Mishra | 477efc9 | 2015-07-31 13:01:45 +0530 | [diff] [blame] | 633 | if (self.conf.skip_test_stack_action_list and |
| 634 | 'ADOPT' in self.conf.skip_test_stack_action_list): |
Sirushti Murugesan | 04ee802 | 2015-02-02 23:00:23 +0530 | [diff] [blame] | 635 | self.skipTest('Testing Stack adopt disabled in conf, skipping') |
Angus Salkeld | 2bd63a4 | 2015-01-07 11:11:29 +1000 | [diff] [blame] | 636 | name = stack_name or self._stack_rand_name() |
| 637 | templ_files = files or {} |
| 638 | params = parameters or {} |
| 639 | env = environment or {} |
| 640 | self.client.stacks.create( |
| 641 | stack_name=name, |
| 642 | files=templ_files, |
| 643 | disable_rollback=True, |
| 644 | parameters=params, |
| 645 | environment=env, |
| 646 | adopt_stack_data=adopt_data, |
| 647 | ) |
Steve Baker | dbea6ab | 2015-08-19 13:37:08 +1200 | [diff] [blame] | 648 | self.addCleanup(self._stack_delete, name) |
Sergey Kraynev | f07f471 | 2016-02-15 05:24:17 -0500 | [diff] [blame] | 649 | stack = self.client.stacks.get(name, resolve_outputs=False) |
Angus Salkeld | 2bd63a4 | 2015-01-07 11:11:29 +1000 | [diff] [blame] | 650 | stack_identifier = '%s/%s' % (name, stack.id) |
| 651 | self._wait_for_stack_status(stack_identifier, wait_for_status) |
| 652 | return stack_identifier |
Sirushti Murugesan | 04ee802 | 2015-02-02 23:00:23 +0530 | [diff] [blame] | 653 | |
| 654 | def stack_abandon(self, stack_id): |
Rabi Mishra | 477efc9 | 2015-07-31 13:01:45 +0530 | [diff] [blame] | 655 | if (self.conf.skip_test_stack_action_list and |
| 656 | 'ABANDON' in self.conf.skip_test_stack_action_list): |
Steve Baker | dbea6ab | 2015-08-19 13:37:08 +1200 | [diff] [blame] | 657 | self.addCleanup(self._stack_delete, stack_id) |
Sirushti Murugesan | 04ee802 | 2015-02-02 23:00:23 +0530 | [diff] [blame] | 658 | self.skipTest('Testing Stack abandon disabled in conf, skipping') |
| 659 | info = self.client.stacks.abandon(stack_id=stack_id) |
| 660 | return info |
Oleksii Chuprykov | d9cd9dc | 2015-02-03 10:34:55 -0500 | [diff] [blame] | 661 | |
rabi | 90b3ab4 | 2017-05-04 13:02:28 +0530 | [diff] [blame] | 662 | def stack_snapshot(self, stack_id, |
| 663 | wait_for_status='SNAPSHOT_COMPLETE'): |
| 664 | snapshot = self.client.stacks.snapshot(stack_id=stack_id) |
| 665 | self._wait_for_stack_status(stack_id, wait_for_status) |
| 666 | return snapshot['id'] |
| 667 | |
| 668 | def stack_restore(self, stack_id, snapshot_id, |
| 669 | wait_for_status='RESTORE_COMPLETE'): |
| 670 | self.client.stacks.restore(stack_id, snapshot_id) |
| 671 | self._wait_for_stack_status(stack_id, wait_for_status) |
| 672 | |
Oleksii Chuprykov | d9cd9dc | 2015-02-03 10:34:55 -0500 | [diff] [blame] | 673 | def stack_suspend(self, stack_identifier): |
Rabi Mishra | 477efc9 | 2015-07-31 13:01:45 +0530 | [diff] [blame] | 674 | if (self.conf.skip_test_stack_action_list and |
| 675 | 'SUSPEND' in self.conf.skip_test_stack_action_list): |
Steve Baker | dbea6ab | 2015-08-19 13:37:08 +1200 | [diff] [blame] | 676 | self.addCleanup(self._stack_delete, stack_identifier) |
Rabi Mishra | 477efc9 | 2015-07-31 13:01:45 +0530 | [diff] [blame] | 677 | self.skipTest('Testing Stack suspend disabled in conf, skipping') |
rabi | f7d6708 | 2016-05-17 18:51:22 +0530 | [diff] [blame] | 678 | self._handle_in_progress(self.client.actions.suspend, stack_identifier) |
Angus Salkeld | a7500d1 | 2015-04-10 15:44:07 +1000 | [diff] [blame] | 679 | # improve debugging by first checking the resource's state. |
| 680 | self._wait_for_all_resource_status(stack_identifier, |
| 681 | 'SUSPEND_COMPLETE') |
Oleksii Chuprykov | d9cd9dc | 2015-02-03 10:34:55 -0500 | [diff] [blame] | 682 | self._wait_for_stack_status(stack_identifier, 'SUSPEND_COMPLETE') |
| 683 | |
| 684 | def stack_resume(self, stack_identifier): |
Rabi Mishra | 477efc9 | 2015-07-31 13:01:45 +0530 | [diff] [blame] | 685 | if (self.conf.skip_test_stack_action_list and |
| 686 | 'RESUME' in self.conf.skip_test_stack_action_list): |
Steve Baker | dbea6ab | 2015-08-19 13:37:08 +1200 | [diff] [blame] | 687 | self.addCleanup(self._stack_delete, stack_identifier) |
Rabi Mishra | 477efc9 | 2015-07-31 13:01:45 +0530 | [diff] [blame] | 688 | self.skipTest('Testing Stack resume disabled in conf, skipping') |
rabi | f7d6708 | 2016-05-17 18:51:22 +0530 | [diff] [blame] | 689 | self._handle_in_progress(self.client.actions.resume, stack_identifier) |
Angus Salkeld | a7500d1 | 2015-04-10 15:44:07 +1000 | [diff] [blame] | 690 | # improve debugging by first checking the resource's state. |
| 691 | self._wait_for_all_resource_status(stack_identifier, |
| 692 | 'RESUME_COMPLETE') |
Oleksii Chuprykov | d9cd9dc | 2015-02-03 10:34:55 -0500 | [diff] [blame] | 693 | self._wait_for_stack_status(stack_identifier, 'RESUME_COMPLETE') |
Steven Hardy | 03da074 | 2015-03-19 00:13:17 -0400 | [diff] [blame] | 694 | |
| 695 | def wait_for_event_with_reason(self, stack_identifier, reason, |
| 696 | rsrc_name=None, num_expected=1): |
| 697 | build_timeout = self.conf.build_timeout |
| 698 | build_interval = self.conf.build_interval |
| 699 | start = timeutils.utcnow() |
| 700 | while timeutils.delta_seconds(start, |
| 701 | timeutils.utcnow()) < build_timeout: |
| 702 | try: |
| 703 | rsrc_events = self.client.events.list(stack_identifier, |
| 704 | resource_name=rsrc_name) |
| 705 | except heat_exceptions.HTTPNotFound: |
liyi | 09461f7 | 2017-03-21 12:17:51 +0800 | [diff] [blame] | 706 | LOG.debug("No events yet found for %s", rsrc_name) |
Steven Hardy | 03da074 | 2015-03-19 00:13:17 -0400 | [diff] [blame] | 707 | else: |
| 708 | matched = [e for e in rsrc_events |
| 709 | if e.resource_status_reason == reason] |
| 710 | if len(matched) == num_expected: |
| 711 | return matched |
| 712 | time.sleep(build_interval) |
Rakesh H S | c5735a8 | 2016-04-28 15:38:09 +0530 | [diff] [blame] | 713 | |
Thomas Herve | a6afca8 | 2017-04-10 23:44:26 +0200 | [diff] [blame] | 714 | def check_autoscale_complete(self, stack_id, expected_num, parent_stack, |
| 715 | policy): |
Rakesh H S | c5735a8 | 2016-04-28 15:38:09 +0530 | [diff] [blame] | 716 | res_list = self.client.resources.list(stack_id) |
| 717 | all_res_complete = all(res.resource_status in ('UPDATE_COMPLETE', |
| 718 | 'CREATE_COMPLETE') |
| 719 | for res in res_list) |
| 720 | all_res = len(res_list) == expected_num |
Thomas Herve | a6afca8 | 2017-04-10 23:44:26 +0200 | [diff] [blame] | 721 | if all_res and all_res_complete: |
| 722 | metadata = self.client.resources.metadata(parent_stack, policy) |
| 723 | return not metadata.get('scaling_in_progress') |
| 724 | return False |