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