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