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 |
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 six |
Sirushti Murugesan | 4920fda | 2015-04-22 00:35:26 +0530 | [diff] [blame] | 25 | from six.moves import urllib |
Pavlo Shchelokovskyy | 60e0ecd | 2014-12-14 22:17:21 +0200 | [diff] [blame] | 26 | import testscenarios |
| 27 | import testtools |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 28 | |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 29 | from heat_integrationtests.common import clients |
| 30 | from heat_integrationtests.common import config |
| 31 | from heat_integrationtests.common import exceptions |
| 32 | from heat_integrationtests.common import remote_client |
| 33 | |
| 34 | LOG = logging.getLogger(__name__) |
Angus Salkeld | 2404370 | 2014-11-21 08:49:26 +1000 | [diff] [blame] | 35 | _LOG_FORMAT = "%(levelname)8s [%(name)s] %(message)s" |
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 | |
| 62 | def rand_name(name=''): |
| 63 | randbits = str(random.randint(1, 0x7fffffff)) |
| 64 | if name: |
| 65 | return name + '-' + randbits |
| 66 | else: |
| 67 | return randbits |
| 68 | |
| 69 | |
Angus Salkeld | 95f65a2 | 2014-11-24 12:38:30 +1000 | [diff] [blame] | 70 | class HeatIntegrationTest(testscenarios.WithScenarios, |
| 71 | testtools.TestCase): |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 72 | |
| 73 | def setUp(self): |
| 74 | super(HeatIntegrationTest, self).setUp() |
| 75 | |
Steve Baker | ae90e13 | 2016-08-13 09:53:07 +1200 | [diff] [blame] | 76 | self.conf = config.CONF.heat_plugin |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 77 | |
| 78 | self.assertIsNotNone(self.conf.auth_url, |
| 79 | 'No auth_url configured') |
| 80 | self.assertIsNotNone(self.conf.username, |
| 81 | 'No username configured') |
| 82 | self.assertIsNotNone(self.conf.password, |
| 83 | 'No password configured') |
rabi | fd98a47 | 2016-05-24 10:18:33 +0530 | [diff] [blame] | 84 | self.setup_clients(self.conf) |
| 85 | self.useFixture(fixtures.FakeLogger(format=_LOG_FORMAT)) |
| 86 | self.updated_time = {} |
| 87 | if self.conf.disable_ssl_certificate_validation: |
| 88 | self.verify_cert = False |
| 89 | else: |
| 90 | self.verify_cert = self.conf.ca_file or True |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 91 | |
Steve Baker | b752e91 | 2016-08-01 22:05:37 +0000 | [diff] [blame] | 92 | def setup_clients(self, conf, admin_credentials=False): |
| 93 | self.manager = clients.ClientManager(conf, admin_credentials) |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 94 | self.identity_client = self.manager.identity_client |
| 95 | self.orchestration_client = self.manager.orchestration_client |
| 96 | self.compute_client = self.manager.compute_client |
| 97 | self.network_client = self.manager.network_client |
| 98 | self.volume_client = self.manager.volume_client |
Angus Salkeld | 4408da3 | 2015-02-03 18:53:30 +1000 | [diff] [blame] | 99 | self.object_client = self.manager.object_client |
Angus Salkeld | 406bbd5 | 2015-05-13 14:24:04 +1000 | [diff] [blame] | 100 | self.metering_client = self.manager.metering_client |
rabi | fd98a47 | 2016-05-24 10:18:33 +0530 | [diff] [blame] | 101 | |
| 102 | self.client = self.orchestration_client |
| 103 | |
| 104 | def setup_clients_for_admin(self): |
Steve Baker | b752e91 | 2016-08-01 22:05:37 +0000 | [diff] [blame] | 105 | self.setup_clients(self.conf, True) |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 106 | |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 107 | def get_remote_client(self, server_or_ip, username, private_key=None): |
| 108 | if isinstance(server_or_ip, six.string_types): |
| 109 | ip = server_or_ip |
| 110 | else: |
| 111 | network_name_for_ssh = self.conf.network_for_ssh |
| 112 | ip = server_or_ip.networks[network_name_for_ssh][0] |
| 113 | if private_key is None: |
| 114 | private_key = self.keypair.private_key |
| 115 | linux_client = remote_client.RemoteClient(ip, username, |
| 116 | pkey=private_key, |
| 117 | conf=self.conf) |
| 118 | try: |
| 119 | linux_client.validate_authentication() |
| 120 | except exceptions.SSHTimeout: |
| 121 | LOG.exception('ssh connection to %s failed' % ip) |
| 122 | raise |
| 123 | |
| 124 | return linux_client |
| 125 | |
Anastasia Kuznetsova | 3e0ab4d | 2015-03-06 18:10:13 +0400 | [diff] [blame] | 126 | def check_connectivity(self, check_ip): |
| 127 | def try_connect(ip): |
| 128 | try: |
Sirushti Murugesan | 4920fda | 2015-04-22 00:35:26 +0530 | [diff] [blame] | 129 | urllib.request.urlopen('http://%s/' % ip) |
Anastasia Kuznetsova | 3e0ab4d | 2015-03-06 18:10:13 +0400 | [diff] [blame] | 130 | return True |
| 131 | except IOError: |
| 132 | return False |
| 133 | |
| 134 | timeout = self.conf.connectivity_timeout |
| 135 | elapsed_time = 0 |
| 136 | while not try_connect(check_ip): |
| 137 | time.sleep(10) |
| 138 | elapsed_time += 10 |
| 139 | if elapsed_time > timeout: |
| 140 | raise exceptions.TimeoutException() |
| 141 | |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 142 | def _log_console_output(self, servers=None): |
| 143 | if not servers: |
| 144 | servers = self.compute_client.servers.list() |
| 145 | for server in servers: |
Steve Baker | 2464129 | 2015-03-13 10:47:50 +1300 | [diff] [blame] | 146 | LOG.info('Console output for %s', server.id) |
| 147 | LOG.info(server.get_console_output()) |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 148 | |
Sergey Kraynev | d6fa5c0 | 2015-02-13 03:03:55 -0500 | [diff] [blame] | 149 | def _load_template(self, base_file, file_name, sub_dir=None): |
| 150 | sub_dir = sub_dir or '' |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 151 | filepath = os.path.join(os.path.dirname(os.path.realpath(base_file)), |
Sergey Kraynev | d6fa5c0 | 2015-02-13 03:03:55 -0500 | [diff] [blame] | 152 | sub_dir, file_name) |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 153 | with open(filepath) as f: |
| 154 | return f.read() |
| 155 | |
| 156 | def create_keypair(self, client=None, name=None): |
| 157 | if client is None: |
| 158 | client = self.compute_client |
| 159 | if name is None: |
| 160 | name = rand_name('heat-keypair') |
| 161 | keypair = client.keypairs.create(name) |
| 162 | self.assertEqual(keypair.name, name) |
| 163 | |
| 164 | def delete_keypair(): |
| 165 | keypair.delete() |
| 166 | |
| 167 | self.addCleanup(delete_keypair) |
| 168 | return keypair |
| 169 | |
Sergey Kraynev | a265c13 | 2015-02-13 03:51:03 -0500 | [diff] [blame] | 170 | def assign_keypair(self): |
| 171 | if self.conf.keypair_name: |
| 172 | self.keypair = None |
| 173 | self.keypair_name = self.conf.keypair_name |
| 174 | else: |
| 175 | self.keypair = self.create_keypair() |
| 176 | self.keypair_name = self.keypair.id |
| 177 | |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 178 | @classmethod |
| 179 | def _stack_rand_name(cls): |
| 180 | return rand_name(cls.__name__) |
| 181 | |
Anastasia Kuznetsova | 673fc43 | 2015-03-12 16:41:36 +0400 | [diff] [blame] | 182 | def _get_network(self, net_name=None): |
| 183 | if net_name is None: |
| 184 | net_name = self.conf.fixed_network_name |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 185 | networks = self.network_client.list_networks() |
| 186 | for net in networks['networks']: |
Anastasia Kuznetsova | 673fc43 | 2015-03-12 16:41:36 +0400 | [diff] [blame] | 187 | if net['name'] == net_name: |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 188 | return net |
| 189 | |
Mark Vanderwiel | 6d8e086 | 2015-10-15 12:51:07 -0500 | [diff] [blame] | 190 | def is_network_extension_supported(self, extension_alias): |
| 191 | try: |
| 192 | self.network_client.show_extension(extension_alias) |
| 193 | except network_exceptions.NeutronClientException: |
| 194 | return False |
| 195 | return True |
| 196 | |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 197 | @staticmethod |
Angus Salkeld | a89a028 | 2015-07-24 15:47:38 +1000 | [diff] [blame] | 198 | def _stack_output(stack, output_key, validate_errors=True): |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 199 | """Return a stack output value for a given key.""" |
Angus Salkeld | a89a028 | 2015-07-24 15:47:38 +1000 | [diff] [blame] | 200 | value = None |
| 201 | for o in stack.outputs: |
| 202 | if validate_errors and 'output_error' in o: |
| 203 | # scan for errors in the stack output. |
| 204 | raise ValueError( |
| 205 | 'Unexpected output errors in %s : %s' % ( |
| 206 | output_key, o['output_error'])) |
| 207 | if o['output_key'] == output_key: |
| 208 | value = o['output_value'] |
| 209 | return value |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 210 | |
| 211 | def _ping_ip_address(self, ip_address, should_succeed=True): |
| 212 | cmd = ['ping', '-c1', '-w1', ip_address] |
| 213 | |
| 214 | def ping(): |
| 215 | proc = subprocess.Popen(cmd, |
| 216 | stdout=subprocess.PIPE, |
| 217 | stderr=subprocess.PIPE) |
| 218 | proc.wait() |
| 219 | return (proc.returncode == 0) == should_succeed |
| 220 | |
| 221 | return call_until_true( |
Angus Salkeld | 08514ad | 2015-02-06 10:08:31 +1000 | [diff] [blame] | 222 | self.conf.build_timeout, 1, ping) |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 223 | |
Angus Salkeld | a7500d1 | 2015-04-10 15:44:07 +1000 | [diff] [blame] | 224 | def _wait_for_all_resource_status(self, stack_identifier, |
| 225 | status, failure_pattern='^.*_FAILED$', |
| 226 | success_on_not_found=False): |
| 227 | for res in self.client.resources.list(stack_identifier): |
| 228 | self._wait_for_resource_status( |
| 229 | stack_identifier, res.resource_name, |
| 230 | status, failure_pattern=failure_pattern, |
| 231 | success_on_not_found=success_on_not_found) |
| 232 | |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 233 | def _wait_for_resource_status(self, stack_identifier, resource_name, |
| 234 | status, failure_pattern='^.*_FAILED$', |
| 235 | success_on_not_found=False): |
| 236 | """Waits for a Resource to reach a given status.""" |
| 237 | fail_regexp = re.compile(failure_pattern) |
| 238 | build_timeout = self.conf.build_timeout |
| 239 | build_interval = self.conf.build_interval |
| 240 | |
| 241 | start = timeutils.utcnow() |
| 242 | while timeutils.delta_seconds(start, |
| 243 | timeutils.utcnow()) < build_timeout: |
| 244 | try: |
| 245 | res = self.client.resources.get( |
| 246 | stack_identifier, resource_name) |
| 247 | except heat_exceptions.HTTPNotFound: |
| 248 | if success_on_not_found: |
| 249 | return |
| 250 | # ignore this, as the resource may not have |
| 251 | # been created yet |
| 252 | else: |
| 253 | if res.resource_status == status: |
| 254 | return |
Sirushti Murugesan | e5ad25b | 2015-04-18 23:30:59 +0530 | [diff] [blame] | 255 | wait_for_action = status.split('_')[0] |
| 256 | resource_action = res.resource_status.split('_')[0] |
| 257 | if (resource_action == wait_for_action and |
| 258 | fail_regexp.search(res.resource_status)): |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 259 | raise exceptions.StackResourceBuildErrorException( |
| 260 | resource_name=res.resource_name, |
| 261 | stack_identifier=stack_identifier, |
| 262 | resource_status=res.resource_status, |
| 263 | resource_status_reason=res.resource_status_reason) |
| 264 | time.sleep(build_interval) |
| 265 | |
| 266 | message = ('Resource %s failed to reach %s status within ' |
| 267 | 'the required time (%s s).' % |
Anastasia Kuznetsova | 9a74557 | 2015-03-04 13:19:15 +0400 | [diff] [blame] | 268 | (resource_name, status, build_timeout)) |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 269 | raise exceptions.TimeoutException(message) |
| 270 | |
Rabi Mishra | 87be9b4 | 2016-02-15 14:15:50 +0530 | [diff] [blame] | 271 | def verify_resource_status(self, stack_identifier, resource_name, |
| 272 | status='CREATE_COMPLETE'): |
| 273 | try: |
| 274 | res = self.client.resources.get(stack_identifier, resource_name) |
| 275 | except heat_exceptions.HTTPNotFound: |
| 276 | return False |
| 277 | return res.resource_status == status |
| 278 | |
Sirushti Murugesan | 13a8a17 | 2015-04-14 00:30:05 +0530 | [diff] [blame] | 279 | def _verify_status(self, stack, stack_identifier, status, fail_regexp): |
| 280 | if stack.stack_status == status: |
Sergey Kraynev | 89082a3 | 2015-09-04 04:42:33 -0400 | [diff] [blame] | 281 | # Handle UPDATE_COMPLETE/FAILED case: Make sure we don't |
| 282 | # wait for a stale UPDATE_COMPLETE/FAILED status. |
| 283 | if status in ('UPDATE_FAILED', 'UPDATE_COMPLETE'): |
Sirushti Murugesan | 13a8a17 | 2015-04-14 00:30:05 +0530 | [diff] [blame] | 284 | if self.updated_time.get( |
| 285 | stack_identifier) != stack.updated_time: |
| 286 | self.updated_time[stack_identifier] = stack.updated_time |
| 287 | return True |
| 288 | else: |
| 289 | return True |
| 290 | |
Sirushti Murugesan | e5ad25b | 2015-04-18 23:30:59 +0530 | [diff] [blame] | 291 | wait_for_action = status.split('_')[0] |
| 292 | if (stack.action == wait_for_action and |
| 293 | fail_regexp.search(stack.stack_status)): |
Sergey Kraynev | 89082a3 | 2015-09-04 04:42:33 -0400 | [diff] [blame] | 294 | # Handle UPDATE_COMPLETE/UPDATE_FAILED case. |
| 295 | if status in ('UPDATE_FAILED', 'UPDATE_COMPLETE'): |
Sirushti Murugesan | 13a8a17 | 2015-04-14 00:30:05 +0530 | [diff] [blame] | 296 | if self.updated_time.get( |
| 297 | stack_identifier) != stack.updated_time: |
| 298 | self.updated_time[stack_identifier] = stack.updated_time |
| 299 | raise exceptions.StackBuildErrorException( |
| 300 | stack_identifier=stack_identifier, |
| 301 | stack_status=stack.stack_status, |
| 302 | stack_status_reason=stack.stack_status_reason) |
| 303 | else: |
| 304 | raise exceptions.StackBuildErrorException( |
| 305 | stack_identifier=stack_identifier, |
| 306 | stack_status=stack.stack_status, |
| 307 | stack_status_reason=stack.stack_status_reason) |
| 308 | |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 309 | def _wait_for_stack_status(self, stack_identifier, status, |
Sergey Kraynev | 89082a3 | 2015-09-04 04:42:33 -0400 | [diff] [blame] | 310 | failure_pattern=None, |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 311 | success_on_not_found=False): |
Peter Razumovsky | f0ac958 | 2015-09-24 16:49:03 +0300 | [diff] [blame] | 312 | """Waits for a Stack to reach a given status. |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 313 | |
| 314 | Note this compares the full $action_$status, e.g |
| 315 | CREATE_COMPLETE, not just COMPLETE which is exposed |
| 316 | via the status property of Stack in heatclient |
| 317 | """ |
Sergey Kraynev | 89082a3 | 2015-09-04 04:42:33 -0400 | [diff] [blame] | 318 | if failure_pattern: |
| 319 | fail_regexp = re.compile(failure_pattern) |
| 320 | elif 'FAILED' in status: |
| 321 | # If we're looking for e.g CREATE_FAILED, COMPLETE is unexpected. |
| 322 | fail_regexp = re.compile('^.*_COMPLETE$') |
| 323 | else: |
| 324 | fail_regexp = re.compile('^.*_FAILED$') |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 325 | build_timeout = self.conf.build_timeout |
| 326 | build_interval = self.conf.build_interval |
| 327 | |
| 328 | start = timeutils.utcnow() |
| 329 | while timeutils.delta_seconds(start, |
| 330 | timeutils.utcnow()) < build_timeout: |
| 331 | try: |
Sergey Kraynev | f07f471 | 2016-02-15 05:24:17 -0500 | [diff] [blame] | 332 | stack = self.client.stacks.get(stack_identifier, |
| 333 | resolve_outputs=False) |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 334 | except heat_exceptions.HTTPNotFound: |
| 335 | if success_on_not_found: |
| 336 | return |
| 337 | # ignore this, as the resource may not have |
| 338 | # been created yet |
| 339 | else: |
Sirushti Murugesan | 13a8a17 | 2015-04-14 00:30:05 +0530 | [diff] [blame] | 340 | if self._verify_status(stack, stack_identifier, status, |
| 341 | fail_regexp): |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 342 | return |
Sirushti Murugesan | 13a8a17 | 2015-04-14 00:30:05 +0530 | [diff] [blame] | 343 | |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 344 | time.sleep(build_interval) |
| 345 | |
| 346 | message = ('Stack %s failed to reach %s status within ' |
| 347 | 'the required time (%s s).' % |
Anastasia Kuznetsova | 9a74557 | 2015-03-04 13:19:15 +0400 | [diff] [blame] | 348 | (stack_identifier, status, build_timeout)) |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 349 | raise exceptions.TimeoutException(message) |
| 350 | |
| 351 | def _stack_delete(self, stack_identifier): |
| 352 | try: |
Thomas Herve | 3eab294 | 2015-10-22 17:29:21 +0200 | [diff] [blame] | 353 | self._handle_in_progress(self.client.stacks.delete, |
| 354 | stack_identifier) |
Steve Baker | 450aa7f | 2014-08-25 10:37:27 +1200 | [diff] [blame] | 355 | except heat_exceptions.HTTPNotFound: |
| 356 | pass |
| 357 | self._wait_for_stack_status( |
| 358 | stack_identifier, 'DELETE_COMPLETE', |
| 359 | success_on_not_found=True) |
Steven Hardy | c9efd97 | 2014-11-20 11:31:55 +0000 | [diff] [blame] | 360 | |
Thomas Herve | 3eab294 | 2015-10-22 17:29:21 +0200 | [diff] [blame] | 361 | def _handle_in_progress(self, fn, *args, **kwargs): |
| 362 | build_timeout = self.conf.build_timeout |
| 363 | build_interval = self.conf.build_interval |
| 364 | start = timeutils.utcnow() |
| 365 | while timeutils.delta_seconds(start, |
| 366 | timeutils.utcnow()) < build_timeout: |
| 367 | try: |
| 368 | fn(*args, **kwargs) |
| 369 | except heat_exceptions.HTTPConflict as ex: |
| 370 | # FIXME(sirushtim): Wait a little for the stack lock to be |
| 371 | # released and hopefully, the stack should be usable again. |
| 372 | if ex.error['error']['type'] != 'ActionInProgress': |
| 373 | raise ex |
| 374 | |
| 375 | time.sleep(build_interval) |
| 376 | else: |
| 377 | break |
| 378 | |
Steven Hardy | 23284b6 | 2015-10-01 19:03:42 +0100 | [diff] [blame] | 379 | def update_stack(self, stack_identifier, template=None, environment=None, |
Sabeen Syed | 277ea69 | 2015-02-04 23:30:02 +0000 | [diff] [blame] | 380 | files=None, parameters=None, tags=None, |
Rakesh H S | a3325d6 | 2015-04-04 19:42:29 +0530 | [diff] [blame] | 381 | expected_status='UPDATE_COMPLETE', |
Steven Hardy | 23284b6 | 2015-10-01 19:03:42 +0100 | [diff] [blame] | 382 | disable_rollback=True, |
| 383 | existing=False): |
Steven Hardy | c9efd97 | 2014-11-20 11:31:55 +0000 | [diff] [blame] | 384 | env = environment or {} |
| 385 | env_files = files or {} |
Sergey Kraynev | bcc78df | 2015-02-27 04:34:32 -0500 | [diff] [blame] | 386 | parameters = parameters or {} |
Sirushti Murugesan | 3a195a5 | 2015-05-01 09:47:09 +0530 | [diff] [blame] | 387 | |
Sergey Kraynev | 89082a3 | 2015-09-04 04:42:33 -0400 | [diff] [blame] | 388 | self.updated_time[stack_identifier] = self.client.stacks.get( |
Sergey Kraynev | f07f471 | 2016-02-15 05:24:17 -0500 | [diff] [blame] | 389 | stack_identifier, resolve_outputs=False).updated_time |
Sirushti Murugesan | 3a195a5 | 2015-05-01 09:47:09 +0530 | [diff] [blame] | 390 | |
Thomas Herve | 3eab294 | 2015-10-22 17:29:21 +0200 | [diff] [blame] | 391 | self._handle_in_progress( |
| 392 | self.client.stacks.update, |
| 393 | stack_id=stack_identifier, |
Thomas Herve | 3eab294 | 2015-10-22 17:29:21 +0200 | [diff] [blame] | 394 | template=template, |
| 395 | files=env_files, |
| 396 | disable_rollback=disable_rollback, |
| 397 | parameters=parameters, |
| 398 | environment=env, |
| 399 | tags=tags, |
| 400 | existing=existing) |
Sirushti Murugesan | 3a195a5 | 2015-05-01 09:47:09 +0530 | [diff] [blame] | 401 | |
Rakesh H S | a3325d6 | 2015-04-04 19:42:29 +0530 | [diff] [blame] | 402 | kwargs = {'stack_identifier': stack_identifier, |
| 403 | 'status': expected_status} |
| 404 | if expected_status in ['ROLLBACK_COMPLETE']: |
Rakesh H S | a3325d6 | 2015-04-04 19:42:29 +0530 | [diff] [blame] | 405 | # To trigger rollback you would intentionally fail the stack |
| 406 | # Hence check for rollback failures |
| 407 | kwargs['failure_pattern'] = '^ROLLBACK_FAILED$' |
| 408 | |
| 409 | self._wait_for_stack_status(**kwargs) |
Steven Hardy | c9efd97 | 2014-11-20 11:31:55 +0000 | [diff] [blame] | 410 | |
Jason Dunsmore | b5aa902 | 2015-09-09 16:57:04 -0500 | [diff] [blame] | 411 | def preview_update_stack(self, stack_identifier, template, |
| 412 | environment=None, files=None, parameters=None, |
Steven Hardy | e6de2d6 | 2015-12-07 15:59:09 +0000 | [diff] [blame] | 413 | tags=None, disable_rollback=True, |
| 414 | show_nested=False): |
Jason Dunsmore | b5aa902 | 2015-09-09 16:57:04 -0500 | [diff] [blame] | 415 | env = environment or {} |
| 416 | env_files = files or {} |
| 417 | parameters = parameters or {} |
Jason Dunsmore | b5aa902 | 2015-09-09 16:57:04 -0500 | [diff] [blame] | 418 | |
| 419 | return self.client.stacks.preview_update( |
| 420 | stack_id=stack_identifier, |
Jason Dunsmore | b5aa902 | 2015-09-09 16:57:04 -0500 | [diff] [blame] | 421 | template=template, |
| 422 | files=env_files, |
| 423 | disable_rollback=disable_rollback, |
| 424 | parameters=parameters, |
| 425 | environment=env, |
Steven Hardy | e6de2d6 | 2015-12-07 15:59:09 +0000 | [diff] [blame] | 426 | tags=tags, |
| 427 | show_nested=show_nested |
Jason Dunsmore | b5aa902 | 2015-09-09 16:57:04 -0500 | [diff] [blame] | 428 | ) |
| 429 | |
Steven Hardy | 03da074 | 2015-03-19 00:13:17 -0400 | [diff] [blame] | 430 | def assert_resource_is_a_stack(self, stack_identifier, res_name, |
| 431 | wait=False): |
| 432 | build_timeout = self.conf.build_timeout |
| 433 | build_interval = self.conf.build_interval |
| 434 | start = timeutils.utcnow() |
| 435 | while timeutils.delta_seconds(start, |
| 436 | timeutils.utcnow()) < build_timeout: |
| 437 | time.sleep(build_interval) |
| 438 | try: |
| 439 | nested_identifier = self._get_nested_identifier( |
| 440 | stack_identifier, res_name) |
| 441 | except Exception: |
| 442 | # We may have to wait, if the create is in-progress |
| 443 | if wait: |
| 444 | time.sleep(build_interval) |
| 445 | else: |
| 446 | raise |
| 447 | else: |
| 448 | return nested_identifier |
| 449 | |
| 450 | def _get_nested_identifier(self, stack_identifier, res_name): |
Angus Salkeld | 2bd63a4 | 2015-01-07 11:11:29 +1000 | [diff] [blame] | 451 | rsrc = self.client.resources.get(stack_identifier, res_name) |
| 452 | nested_link = [l for l in rsrc.links if l['rel'] == 'nested'] |
| 453 | nested_href = nested_link[0]['href'] |
| 454 | nested_id = nested_href.split('/')[-1] |
| 455 | nested_identifier = '/'.join(nested_href.split('/')[-2:]) |
| 456 | self.assertEqual(rsrc.physical_resource_id, nested_id) |
| 457 | |
Sergey Kraynev | f07f471 | 2016-02-15 05:24:17 -0500 | [diff] [blame] | 458 | nested_stack = self.client.stacks.get(nested_id, resolve_outputs=False) |
Angus Salkeld | 2bd63a4 | 2015-01-07 11:11:29 +1000 | [diff] [blame] | 459 | nested_identifier2 = '%s/%s' % (nested_stack.stack_name, |
| 460 | nested_stack.id) |
| 461 | self.assertEqual(nested_identifier, nested_identifier2) |
| 462 | parent_id = stack_identifier.split("/")[-1] |
| 463 | self.assertEqual(parent_id, nested_stack.parent) |
| 464 | return nested_identifier |
| 465 | |
Rabi Mishra | 8bcff8a | 2015-09-21 18:15:04 +0530 | [diff] [blame] | 466 | def group_nested_identifier(self, stack_identifier, |
| 467 | group_name): |
| 468 | # Get the nested stack identifier from a group resource |
| 469 | rsrc = self.client.resources.get(stack_identifier, group_name) |
| 470 | physical_resource_id = rsrc.physical_resource_id |
| 471 | |
Sergey Kraynev | f07f471 | 2016-02-15 05:24:17 -0500 | [diff] [blame] | 472 | nested_stack = self.client.stacks.get(physical_resource_id, |
| 473 | resolve_outputs=False) |
Rabi Mishra | 8bcff8a | 2015-09-21 18:15:04 +0530 | [diff] [blame] | 474 | nested_identifier = '%s/%s' % (nested_stack.stack_name, |
| 475 | nested_stack.id) |
| 476 | parent_id = stack_identifier.split("/")[-1] |
| 477 | self.assertEqual(parent_id, nested_stack.parent) |
| 478 | return nested_identifier |
| 479 | |
| 480 | def list_group_resources(self, stack_identifier, |
| 481 | group_name, minimal=True): |
| 482 | nested_identifier = self.group_nested_identifier(stack_identifier, |
| 483 | group_name) |
| 484 | if minimal: |
| 485 | return self.list_resources(nested_identifier) |
| 486 | return self.client.resources.list(nested_identifier) |
| 487 | |
Steven Hardy | c9efd97 | 2014-11-20 11:31:55 +0000 | [diff] [blame] | 488 | def list_resources(self, stack_identifier): |
| 489 | resources = self.client.resources.list(stack_identifier) |
| 490 | return dict((r.resource_name, r.resource_type) for r in resources) |
Steven Hardy | f2c82c0 | 2014-11-20 14:02:17 +0000 | [diff] [blame] | 491 | |
Steven Hardy | d448dae | 2016-06-14 14:57:28 +0100 | [diff] [blame^] | 492 | def get_resource_stack_id(self, r): |
| 493 | stack_link = [l for l in r.links if l.get('rel') == 'stack'][0] |
| 494 | return stack_link['href'].split("/")[-1] |
| 495 | |
Steven Hardy | f2c82c0 | 2014-11-20 14:02:17 +0000 | [diff] [blame] | 496 | def stack_create(self, stack_name=None, template=None, files=None, |
Sabeen Syed | 277ea69 | 2015-02-04 23:30:02 +0000 | [diff] [blame] | 497 | parameters=None, environment=None, tags=None, |
| 498 | expected_status='CREATE_COMPLETE', |
Jay Dobies | 39c4ce4 | 2015-11-04 10:49:08 -0500 | [diff] [blame] | 499 | disable_rollback=True, enable_cleanup=True, |
| 500 | environment_files=None): |
Steven Hardy | f2c82c0 | 2014-11-20 14:02:17 +0000 | [diff] [blame] | 501 | name = stack_name or self._stack_rand_name() |
| 502 | templ = template or self.template |
| 503 | templ_files = files or {} |
| 504 | params = parameters or {} |
| 505 | env = environment or {} |
| 506 | self.client.stacks.create( |
| 507 | stack_name=name, |
| 508 | template=templ, |
| 509 | files=templ_files, |
Rakesh H S | a3325d6 | 2015-04-04 19:42:29 +0530 | [diff] [blame] | 510 | disable_rollback=disable_rollback, |
Steven Hardy | f2c82c0 | 2014-11-20 14:02:17 +0000 | [diff] [blame] | 511 | parameters=params, |
Sabeen Syed | 277ea69 | 2015-02-04 23:30:02 +0000 | [diff] [blame] | 512 | environment=env, |
Jay Dobies | 39c4ce4 | 2015-11-04 10:49:08 -0500 | [diff] [blame] | 513 | tags=tags, |
| 514 | environment_files=environment_files |
Steven Hardy | f2c82c0 | 2014-11-20 14:02:17 +0000 | [diff] [blame] | 515 | ) |
Sergey Kraynev | bf67ce3 | 2015-04-17 10:54:20 -0400 | [diff] [blame] | 516 | if expected_status not in ['ROLLBACK_COMPLETE'] and enable_cleanup: |
Steve Baker | dbea6ab | 2015-08-19 13:37:08 +1200 | [diff] [blame] | 517 | self.addCleanup(self._stack_delete, name) |
Steven Hardy | f2c82c0 | 2014-11-20 14:02:17 +0000 | [diff] [blame] | 518 | |
Sergey Kraynev | f07f471 | 2016-02-15 05:24:17 -0500 | [diff] [blame] | 519 | stack = self.client.stacks.get(name, resolve_outputs=False) |
Steven Hardy | f2c82c0 | 2014-11-20 14:02:17 +0000 | [diff] [blame] | 520 | stack_identifier = '%s/%s' % (name, stack.id) |
Rakesh H S | a3325d6 | 2015-04-04 19:42:29 +0530 | [diff] [blame] | 521 | kwargs = {'stack_identifier': stack_identifier, |
| 522 | 'status': expected_status} |
Steve Baker | f6c8f12 | 2015-02-10 13:54:46 +1300 | [diff] [blame] | 523 | if expected_status: |
Rakesh H S | a3325d6 | 2015-04-04 19:42:29 +0530 | [diff] [blame] | 524 | if expected_status in ['ROLLBACK_COMPLETE']: |
| 525 | # To trigger rollback you would intentionally fail the stack |
| 526 | # Hence check for rollback failures |
| 527 | kwargs['failure_pattern'] = '^ROLLBACK_FAILED$' |
| 528 | self._wait_for_stack_status(**kwargs) |
Steven Hardy | f2c82c0 | 2014-11-20 14:02:17 +0000 | [diff] [blame] | 529 | return stack_identifier |
Angus Salkeld | 2bd63a4 | 2015-01-07 11:11:29 +1000 | [diff] [blame] | 530 | |
| 531 | def stack_adopt(self, stack_name=None, files=None, |
| 532 | parameters=None, environment=None, adopt_data=None, |
| 533 | wait_for_status='ADOPT_COMPLETE'): |
Rabi Mishra | 477efc9 | 2015-07-31 13:01:45 +0530 | [diff] [blame] | 534 | if (self.conf.skip_test_stack_action_list and |
| 535 | 'ADOPT' in self.conf.skip_test_stack_action_list): |
Sirushti Murugesan | 04ee802 | 2015-02-02 23:00:23 +0530 | [diff] [blame] | 536 | self.skipTest('Testing Stack adopt disabled in conf, skipping') |
Angus Salkeld | 2bd63a4 | 2015-01-07 11:11:29 +1000 | [diff] [blame] | 537 | name = stack_name or self._stack_rand_name() |
| 538 | templ_files = files or {} |
| 539 | params = parameters or {} |
| 540 | env = environment or {} |
| 541 | self.client.stacks.create( |
| 542 | stack_name=name, |
| 543 | files=templ_files, |
| 544 | disable_rollback=True, |
| 545 | parameters=params, |
| 546 | environment=env, |
| 547 | adopt_stack_data=adopt_data, |
| 548 | ) |
Steve Baker | dbea6ab | 2015-08-19 13:37:08 +1200 | [diff] [blame] | 549 | self.addCleanup(self._stack_delete, name) |
Sergey Kraynev | f07f471 | 2016-02-15 05:24:17 -0500 | [diff] [blame] | 550 | stack = self.client.stacks.get(name, resolve_outputs=False) |
Angus Salkeld | 2bd63a4 | 2015-01-07 11:11:29 +1000 | [diff] [blame] | 551 | stack_identifier = '%s/%s' % (name, stack.id) |
| 552 | self._wait_for_stack_status(stack_identifier, wait_for_status) |
| 553 | return stack_identifier |
Sirushti Murugesan | 04ee802 | 2015-02-02 23:00:23 +0530 | [diff] [blame] | 554 | |
| 555 | def stack_abandon(self, stack_id): |
Rabi Mishra | 477efc9 | 2015-07-31 13:01:45 +0530 | [diff] [blame] | 556 | if (self.conf.skip_test_stack_action_list and |
| 557 | 'ABANDON' in self.conf.skip_test_stack_action_list): |
Steve Baker | dbea6ab | 2015-08-19 13:37:08 +1200 | [diff] [blame] | 558 | self.addCleanup(self._stack_delete, stack_id) |
Sirushti Murugesan | 04ee802 | 2015-02-02 23:00:23 +0530 | [diff] [blame] | 559 | self.skipTest('Testing Stack abandon disabled in conf, skipping') |
| 560 | info = self.client.stacks.abandon(stack_id=stack_id) |
| 561 | return info |
Oleksii Chuprykov | d9cd9dc | 2015-02-03 10:34:55 -0500 | [diff] [blame] | 562 | |
| 563 | def stack_suspend(self, stack_identifier): |
Rabi Mishra | 477efc9 | 2015-07-31 13:01:45 +0530 | [diff] [blame] | 564 | if (self.conf.skip_test_stack_action_list and |
| 565 | 'SUSPEND' in self.conf.skip_test_stack_action_list): |
Steve Baker | dbea6ab | 2015-08-19 13:37:08 +1200 | [diff] [blame] | 566 | self.addCleanup(self._stack_delete, stack_identifier) |
Rabi Mishra | 477efc9 | 2015-07-31 13:01:45 +0530 | [diff] [blame] | 567 | self.skipTest('Testing Stack suspend disabled in conf, skipping') |
rabi | f7d6708 | 2016-05-17 18:51:22 +0530 | [diff] [blame] | 568 | self._handle_in_progress(self.client.actions.suspend, stack_identifier) |
Angus Salkeld | a7500d1 | 2015-04-10 15:44:07 +1000 | [diff] [blame] | 569 | # improve debugging by first checking the resource's state. |
| 570 | self._wait_for_all_resource_status(stack_identifier, |
| 571 | 'SUSPEND_COMPLETE') |
Oleksii Chuprykov | d9cd9dc | 2015-02-03 10:34:55 -0500 | [diff] [blame] | 572 | self._wait_for_stack_status(stack_identifier, 'SUSPEND_COMPLETE') |
| 573 | |
| 574 | def stack_resume(self, stack_identifier): |
Rabi Mishra | 477efc9 | 2015-07-31 13:01:45 +0530 | [diff] [blame] | 575 | if (self.conf.skip_test_stack_action_list and |
| 576 | 'RESUME' in self.conf.skip_test_stack_action_list): |
Steve Baker | dbea6ab | 2015-08-19 13:37:08 +1200 | [diff] [blame] | 577 | self.addCleanup(self._stack_delete, stack_identifier) |
Rabi Mishra | 477efc9 | 2015-07-31 13:01:45 +0530 | [diff] [blame] | 578 | self.skipTest('Testing Stack resume disabled in conf, skipping') |
rabi | f7d6708 | 2016-05-17 18:51:22 +0530 | [diff] [blame] | 579 | self._handle_in_progress(self.client.actions.resume, stack_identifier) |
Angus Salkeld | a7500d1 | 2015-04-10 15:44:07 +1000 | [diff] [blame] | 580 | # improve debugging by first checking the resource's state. |
| 581 | self._wait_for_all_resource_status(stack_identifier, |
| 582 | 'RESUME_COMPLETE') |
Oleksii Chuprykov | d9cd9dc | 2015-02-03 10:34:55 -0500 | [diff] [blame] | 583 | self._wait_for_stack_status(stack_identifier, 'RESUME_COMPLETE') |
Steven Hardy | 03da074 | 2015-03-19 00:13:17 -0400 | [diff] [blame] | 584 | |
| 585 | def wait_for_event_with_reason(self, stack_identifier, reason, |
| 586 | rsrc_name=None, num_expected=1): |
| 587 | build_timeout = self.conf.build_timeout |
| 588 | build_interval = self.conf.build_interval |
| 589 | start = timeutils.utcnow() |
| 590 | while timeutils.delta_seconds(start, |
| 591 | timeutils.utcnow()) < build_timeout: |
| 592 | try: |
| 593 | rsrc_events = self.client.events.list(stack_identifier, |
| 594 | resource_name=rsrc_name) |
| 595 | except heat_exceptions.HTTPNotFound: |
| 596 | LOG.debug("No events yet found for %s" % rsrc_name) |
| 597 | else: |
| 598 | matched = [e for e in rsrc_events |
| 599 | if e.resource_status_reason == reason] |
| 600 | if len(matched) == num_expected: |
| 601 | return matched |
| 602 | time.sleep(build_interval) |
Rakesh H S | c5735a8 | 2016-04-28 15:38:09 +0530 | [diff] [blame] | 603 | |
| 604 | def check_autoscale_complete(self, stack_id, expected_num): |
| 605 | res_list = self.client.resources.list(stack_id) |
| 606 | all_res_complete = all(res.resource_status in ('UPDATE_COMPLETE', |
| 607 | 'CREATE_COMPLETE') |
| 608 | for res in res_list) |
| 609 | all_res = len(res_list) == expected_num |
| 610 | return all_res and all_res_complete |