blob: d719ad0cd2d224779bb79186b9a9cd5cc93342ac [file] [log] [blame]
Steve Baker450aa7f2014-08-25 10:37:27 +12001# 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 Baker450aa7f2014-08-25 10:37:27 +120013import os
14import random
15import re
Steve Baker450aa7f2014-08-25 10:37:27 +120016import subprocess
Steve Baker450aa7f2014-08-25 10:37:27 +120017import time
18
Pavlo Shchelokovskyy60e0ecd2014-12-14 22:17:21 +020019import fixtures
Steve Baker450aa7f2014-08-25 10:37:27 +120020from heatclient import exc as heat_exceptions
Thomas Hervedb36c092017-03-23 11:20:14 +010021from keystoneauth1 import exceptions as kc_exceptions
Mark Vanderwiel6d8e0862015-10-15 12:51:07 -050022from neutronclient.common import exceptions as network_exceptions
Steve Baker24641292015-03-13 10:47:50 +130023from oslo_log import log as logging
Jens Rosenboom4f069fb2015-02-18 14:19:07 +010024from oslo_utils import timeutils
Pavlo Shchelokovskyy60e0ecd2014-12-14 22:17:21 +020025import six
Sirushti Murugesan4920fda2015-04-22 00:35:26 +053026from six.moves import urllib
Pavlo Shchelokovskyy60e0ecd2014-12-14 22:17:21 +020027import testscenarios
28import testtools
Steve Baker450aa7f2014-08-25 10:37:27 +120029
rabid2916d02017-09-22 18:19:24 +053030from heat_tempest_plugin.common import exceptions
31from heat_tempest_plugin.common import remote_client
32from heat_tempest_plugin import config
33from heat_tempest_plugin.services import clients
Steve Baker450aa7f2014-08-25 10:37:27 +120034
35LOG = logging.getLogger(__name__)
Angus Salkeld24043702014-11-21 08:49:26 +100036_LOG_FORMAT = "%(levelname)8s [%(name)s] %(message)s"
Steve Baker450aa7f2014-08-25 10:37:27 +120037
38
Angus Salkeld08514ad2015-02-06 10:08:31 +100039def call_until_true(duration, sleep_for, func, *args, **kwargs):
Peter Razumovskyf0ac9582015-09-24 16:49:03 +030040 """Call the function until it returns True or the duration elapsed.
41
Steve Baker450aa7f2014-08-25 10:37:27 +120042 Call the given function until it returns True (and return True) or
43 until the specified duration (in seconds) elapses (and return
44 False).
45
46 :param func: A zero argument callable that returns True on success.
47 :param duration: The number of seconds for which to attempt a
48 successful call of the function.
49 :param sleep_for: The number of seconds to sleep after an unsuccessful
50 invocation of the function.
51 """
52 now = time.time()
53 timeout = now + duration
54 while now < timeout:
Angus Salkeld08514ad2015-02-06 10:08:31 +100055 if func(*args, **kwargs):
Steve Baker450aa7f2014-08-25 10:37:27 +120056 return True
57 LOG.debug("Sleeping for %d seconds", sleep_for)
58 time.sleep(sleep_for)
59 now = time.time()
60 return False
61
62
63def rand_name(name=''):
ricolina4eb53d2017-04-24 23:51:09 +080064 randbits = six.text_type(random.randint(1, 0x7fffffff))
Steve Baker450aa7f2014-08-25 10:37:27 +120065 if name:
66 return name + '-' + randbits
67 else:
68 return randbits
69
70
Zane Bitterf407e102017-10-05 14:19:32 -040071def requires_convergence(test_method):
72 '''Decorator for convergence-only tests.
73
74 The decorated test will be skipped when convergence is disabled.
75 '''
rabi94a520a2017-11-17 22:49:17 +053076 plugin = config.CONF.orchestration_plugin
77 convergence_enabled = plugin.convergence_engine_enabled
Zane Bitterf407e102017-10-05 14:19:32 -040078 skipper = testtools.skipUnless(convergence_enabled,
79 "Convergence-only tests are disabled")
80 return skipper(test_method)
81
82
Angus Salkeld95f65a22014-11-24 12:38:30 +100083class HeatIntegrationTest(testscenarios.WithScenarios,
84 testtools.TestCase):
Steve Baker450aa7f2014-08-25 10:37:27 +120085
86 def setUp(self):
87 super(HeatIntegrationTest, self).setUp()
88
rabid2916d02017-09-22 18:19:24 +053089 self.conf = config.CONF.orchestration_plugin
Steve Baker450aa7f2014-08-25 10:37:27 +120090
91 self.assertIsNotNone(self.conf.auth_url,
92 'No auth_url configured')
93 self.assertIsNotNone(self.conf.username,
94 'No username configured')
95 self.assertIsNotNone(self.conf.password,
96 'No password configured')
rabifd98a472016-05-24 10:18:33 +053097 self.setup_clients(self.conf)
98 self.useFixture(fixtures.FakeLogger(format=_LOG_FORMAT))
99 self.updated_time = {}
100 if self.conf.disable_ssl_certificate_validation:
101 self.verify_cert = False
102 else:
103 self.verify_cert = self.conf.ca_file or True
Steve Baker450aa7f2014-08-25 10:37:27 +1200104
Steve Bakerb752e912016-08-01 22:05:37 +0000105 def setup_clients(self, conf, admin_credentials=False):
106 self.manager = clients.ClientManager(conf, admin_credentials)
Steve Baker450aa7f2014-08-25 10:37:27 +1200107 self.identity_client = self.manager.identity_client
108 self.orchestration_client = self.manager.orchestration_client
109 self.compute_client = self.manager.compute_client
110 self.network_client = self.manager.network_client
111 self.volume_client = self.manager.volume_client
Angus Salkeld4408da32015-02-03 18:53:30 +1000112 self.object_client = self.manager.object_client
rabid69f0312017-10-26 14:52:52 +0530113 self.metric_client = self.manager.metric_client
rabifd98a472016-05-24 10:18:33 +0530114
115 self.client = self.orchestration_client
116
117 def setup_clients_for_admin(self):
Steve Bakerb752e912016-08-01 22:05:37 +0000118 self.setup_clients(self.conf, True)
Steve Baker450aa7f2014-08-25 10:37:27 +1200119
Steve Baker450aa7f2014-08-25 10:37:27 +1200120 def get_remote_client(self, server_or_ip, username, private_key=None):
121 if isinstance(server_or_ip, six.string_types):
122 ip = server_or_ip
123 else:
124 network_name_for_ssh = self.conf.network_for_ssh
125 ip = server_or_ip.networks[network_name_for_ssh][0]
126 if private_key is None:
127 private_key = self.keypair.private_key
128 linux_client = remote_client.RemoteClient(ip, username,
129 pkey=private_key,
130 conf=self.conf)
131 try:
132 linux_client.validate_authentication()
133 except exceptions.SSHTimeout:
liyi09461f72017-03-21 12:17:51 +0800134 LOG.exception('ssh connection to %s failed', ip)
Steve Baker450aa7f2014-08-25 10:37:27 +1200135 raise
136
137 return linux_client
138
Anastasia Kuznetsova3e0ab4d2015-03-06 18:10:13 +0400139 def check_connectivity(self, check_ip):
140 def try_connect(ip):
141 try:
Sirushti Murugesan4920fda2015-04-22 00:35:26 +0530142 urllib.request.urlopen('http://%s/' % ip)
Anastasia Kuznetsova3e0ab4d2015-03-06 18:10:13 +0400143 return True
144 except IOError:
145 return False
146
147 timeout = self.conf.connectivity_timeout
148 elapsed_time = 0
149 while not try_connect(check_ip):
150 time.sleep(10)
151 elapsed_time += 10
152 if elapsed_time > timeout:
153 raise exceptions.TimeoutException()
154
Steve Baker450aa7f2014-08-25 10:37:27 +1200155 def _log_console_output(self, servers=None):
156 if not servers:
157 servers = self.compute_client.servers.list()
158 for server in servers:
Steve Baker24641292015-03-13 10:47:50 +1300159 LOG.info('Console output for %s', server.id)
160 LOG.info(server.get_console_output())
Steve Baker450aa7f2014-08-25 10:37:27 +1200161
Sergey Kraynevd6fa5c02015-02-13 03:03:55 -0500162 def _load_template(self, base_file, file_name, sub_dir=None):
163 sub_dir = sub_dir or ''
Steve Baker450aa7f2014-08-25 10:37:27 +1200164 filepath = os.path.join(os.path.dirname(os.path.realpath(base_file)),
Sergey Kraynevd6fa5c02015-02-13 03:03:55 -0500165 sub_dir, file_name)
Steve Baker450aa7f2014-08-25 10:37:27 +1200166 with open(filepath) as f:
167 return f.read()
168
169 def create_keypair(self, client=None, name=None):
170 if client is None:
171 client = self.compute_client
172 if name is None:
173 name = rand_name('heat-keypair')
174 keypair = client.keypairs.create(name)
175 self.assertEqual(keypair.name, name)
176
177 def delete_keypair():
178 keypair.delete()
179
180 self.addCleanup(delete_keypair)
181 return keypair
182
Sergey Krayneva265c132015-02-13 03:51:03 -0500183 def assign_keypair(self):
184 if self.conf.keypair_name:
185 self.keypair = None
186 self.keypair_name = self.conf.keypair_name
187 else:
188 self.keypair = self.create_keypair()
189 self.keypair_name = self.keypair.id
190
Steve Baker450aa7f2014-08-25 10:37:27 +1200191 @classmethod
192 def _stack_rand_name(cls):
193 return rand_name(cls.__name__)
194
Anastasia Kuznetsova673fc432015-03-12 16:41:36 +0400195 def _get_network(self, net_name=None):
196 if net_name is None:
197 net_name = self.conf.fixed_network_name
Steve Baker450aa7f2014-08-25 10:37:27 +1200198 networks = self.network_client.list_networks()
199 for net in networks['networks']:
Anastasia Kuznetsova673fc432015-03-12 16:41:36 +0400200 if net['name'] == net_name:
Steve Baker450aa7f2014-08-25 10:37:27 +1200201 return net
202
Mark Vanderwiel6d8e0862015-10-15 12:51:07 -0500203 def is_network_extension_supported(self, extension_alias):
204 try:
205 self.network_client.show_extension(extension_alias)
206 except network_exceptions.NeutronClientException:
207 return False
208 return True
209
Thomas Hervedb36c092017-03-23 11:20:14 +0100210 def is_service_available(self, service_type):
211 try:
212 self.identity_client.get_endpoint_url(
213 service_type, self.conf.region)
214 except kc_exceptions.EndpointNotFound:
215 return False
216 else:
217 return True
218
Steve Baker450aa7f2014-08-25 10:37:27 +1200219 @staticmethod
Angus Salkelda89a0282015-07-24 15:47:38 +1000220 def _stack_output(stack, output_key, validate_errors=True):
Steve Baker450aa7f2014-08-25 10:37:27 +1200221 """Return a stack output value for a given key."""
Angus Salkelda89a0282015-07-24 15:47:38 +1000222 value = None
223 for o in stack.outputs:
224 if validate_errors and 'output_error' in o:
225 # scan for errors in the stack output.
226 raise ValueError(
227 'Unexpected output errors in %s : %s' % (
228 output_key, o['output_error']))
229 if o['output_key'] == output_key:
230 value = o['output_value']
231 return value
Steve Baker450aa7f2014-08-25 10:37:27 +1200232
233 def _ping_ip_address(self, ip_address, should_succeed=True):
234 cmd = ['ping', '-c1', '-w1', ip_address]
235
236 def ping():
237 proc = subprocess.Popen(cmd,
238 stdout=subprocess.PIPE,
239 stderr=subprocess.PIPE)
240 proc.wait()
241 return (proc.returncode == 0) == should_succeed
242
243 return call_until_true(
Angus Salkeld08514ad2015-02-06 10:08:31 +1000244 self.conf.build_timeout, 1, ping)
Steve Baker450aa7f2014-08-25 10:37:27 +1200245
Angus Salkelda7500d12015-04-10 15:44:07 +1000246 def _wait_for_all_resource_status(self, stack_identifier,
247 status, failure_pattern='^.*_FAILED$',
248 success_on_not_found=False):
249 for res in self.client.resources.list(stack_identifier):
250 self._wait_for_resource_status(
251 stack_identifier, res.resource_name,
252 status, failure_pattern=failure_pattern,
253 success_on_not_found=success_on_not_found)
254
Steve Baker450aa7f2014-08-25 10:37:27 +1200255 def _wait_for_resource_status(self, stack_identifier, resource_name,
256 status, failure_pattern='^.*_FAILED$',
257 success_on_not_found=False):
258 """Waits for a Resource to reach a given status."""
259 fail_regexp = re.compile(failure_pattern)
260 build_timeout = self.conf.build_timeout
261 build_interval = self.conf.build_interval
262
263 start = timeutils.utcnow()
264 while timeutils.delta_seconds(start,
265 timeutils.utcnow()) < build_timeout:
266 try:
267 res = self.client.resources.get(
268 stack_identifier, resource_name)
269 except heat_exceptions.HTTPNotFound:
270 if success_on_not_found:
271 return
272 # ignore this, as the resource may not have
273 # been created yet
274 else:
275 if res.resource_status == status:
276 return
Sirushti Murugesane5ad25b2015-04-18 23:30:59 +0530277 wait_for_action = status.split('_')[0]
278 resource_action = res.resource_status.split('_')[0]
279 if (resource_action == wait_for_action and
280 fail_regexp.search(res.resource_status)):
Steve Baker450aa7f2014-08-25 10:37:27 +1200281 raise exceptions.StackResourceBuildErrorException(
282 resource_name=res.resource_name,
283 stack_identifier=stack_identifier,
284 resource_status=res.resource_status,
285 resource_status_reason=res.resource_status_reason)
286 time.sleep(build_interval)
287
288 message = ('Resource %s failed to reach %s status within '
289 'the required time (%s s).' %
Anastasia Kuznetsova9a745572015-03-04 13:19:15 +0400290 (resource_name, status, build_timeout))
Steve Baker450aa7f2014-08-25 10:37:27 +1200291 raise exceptions.TimeoutException(message)
292
Rabi Mishra87be9b42016-02-15 14:15:50 +0530293 def verify_resource_status(self, stack_identifier, resource_name,
294 status='CREATE_COMPLETE'):
295 try:
296 res = self.client.resources.get(stack_identifier, resource_name)
297 except heat_exceptions.HTTPNotFound:
298 return False
299 return res.resource_status == status
300
rabi5eaa4962017-08-31 10:55:13 +0530301 def _verify_status(self, stack, stack_identifier, status,
302 fail_regexp, is_action_cancelled=False):
Sirushti Murugesan13a8a172015-04-14 00:30:05 +0530303 if stack.stack_status == status:
Sergey Kraynev89082a32015-09-04 04:42:33 -0400304 # Handle UPDATE_COMPLETE/FAILED case: Make sure we don't
305 # wait for a stale UPDATE_COMPLETE/FAILED status.
306 if status in ('UPDATE_FAILED', 'UPDATE_COMPLETE'):
rabi5eaa4962017-08-31 10:55:13 +0530307 if is_action_cancelled:
308 return True
309
Sirushti Murugesan13a8a172015-04-14 00:30:05 +0530310 if self.updated_time.get(
311 stack_identifier) != stack.updated_time:
312 self.updated_time[stack_identifier] = stack.updated_time
313 return True
Thomas Herve0e8567e2016-09-22 15:07:37 +0200314 elif status == 'DELETE_COMPLETE' and stack.deletion_time is None:
315 # Wait for deleted_time to be filled, so that we have more
316 # confidence the operation is finished.
317 return False
Sirushti Murugesan13a8a172015-04-14 00:30:05 +0530318 else:
319 return True
320
Sirushti Murugesane5ad25b2015-04-18 23:30:59 +0530321 wait_for_action = status.split('_')[0]
322 if (stack.action == wait_for_action and
323 fail_regexp.search(stack.stack_status)):
Sergey Kraynev89082a32015-09-04 04:42:33 -0400324 # Handle UPDATE_COMPLETE/UPDATE_FAILED case.
325 if status in ('UPDATE_FAILED', 'UPDATE_COMPLETE'):
Sirushti Murugesan13a8a172015-04-14 00:30:05 +0530326 if self.updated_time.get(
327 stack_identifier) != stack.updated_time:
328 self.updated_time[stack_identifier] = stack.updated_time
329 raise exceptions.StackBuildErrorException(
330 stack_identifier=stack_identifier,
331 stack_status=stack.stack_status,
332 stack_status_reason=stack.stack_status_reason)
333 else:
334 raise exceptions.StackBuildErrorException(
335 stack_identifier=stack_identifier,
336 stack_status=stack.stack_status,
337 stack_status_reason=stack.stack_status_reason)
338
Steve Baker450aa7f2014-08-25 10:37:27 +1200339 def _wait_for_stack_status(self, stack_identifier, status,
Sergey Kraynev89082a32015-09-04 04:42:33 -0400340 failure_pattern=None,
Rabi Mishra81ca6bc2016-06-16 15:09:20 +0530341 success_on_not_found=False,
342 signal_required=False,
rabi5eaa4962017-08-31 10:55:13 +0530343 resources_to_signal=None,
344 is_action_cancelled=False):
Peter Razumovskyf0ac9582015-09-24 16:49:03 +0300345 """Waits for a Stack to reach a given status.
Steve Baker450aa7f2014-08-25 10:37:27 +1200346
347 Note this compares the full $action_$status, e.g
348 CREATE_COMPLETE, not just COMPLETE which is exposed
349 via the status property of Stack in heatclient
350 """
Sergey Kraynev89082a32015-09-04 04:42:33 -0400351 if failure_pattern:
352 fail_regexp = re.compile(failure_pattern)
353 elif 'FAILED' in status:
354 # If we're looking for e.g CREATE_FAILED, COMPLETE is unexpected.
355 fail_regexp = re.compile('^.*_COMPLETE$')
356 else:
357 fail_regexp = re.compile('^.*_FAILED$')
Steve Baker450aa7f2014-08-25 10:37:27 +1200358 build_timeout = self.conf.build_timeout
359 build_interval = self.conf.build_interval
360
361 start = timeutils.utcnow()
362 while timeutils.delta_seconds(start,
363 timeutils.utcnow()) < build_timeout:
364 try:
Sergey Kraynevf07f4712016-02-15 05:24:17 -0500365 stack = self.client.stacks.get(stack_identifier,
366 resolve_outputs=False)
Steve Baker450aa7f2014-08-25 10:37:27 +1200367 except heat_exceptions.HTTPNotFound:
368 if success_on_not_found:
369 return
370 # ignore this, as the resource may not have
371 # been created yet
372 else:
Sirushti Murugesan13a8a172015-04-14 00:30:05 +0530373 if self._verify_status(stack, stack_identifier, status,
rabi5eaa4962017-08-31 10:55:13 +0530374 fail_regexp, is_action_cancelled):
Steve Baker450aa7f2014-08-25 10:37:27 +1200375 return
Rabi Mishra81ca6bc2016-06-16 15:09:20 +0530376 if signal_required:
377 self.signal_resources(resources_to_signal)
Steve Baker450aa7f2014-08-25 10:37:27 +1200378 time.sleep(build_interval)
379
380 message = ('Stack %s failed to reach %s status within '
381 'the required time (%s s).' %
Anastasia Kuznetsova9a745572015-03-04 13:19:15 +0400382 (stack_identifier, status, build_timeout))
Steve Baker450aa7f2014-08-25 10:37:27 +1200383 raise exceptions.TimeoutException(message)
384
385 def _stack_delete(self, stack_identifier):
386 try:
Thomas Herve3eab2942015-10-22 17:29:21 +0200387 self._handle_in_progress(self.client.stacks.delete,
388 stack_identifier)
Steve Baker450aa7f2014-08-25 10:37:27 +1200389 except heat_exceptions.HTTPNotFound:
390 pass
391 self._wait_for_stack_status(
392 stack_identifier, 'DELETE_COMPLETE',
393 success_on_not_found=True)
Steven Hardyc9efd972014-11-20 11:31:55 +0000394
Thomas Herve3eab2942015-10-22 17:29:21 +0200395 def _handle_in_progress(self, fn, *args, **kwargs):
396 build_timeout = self.conf.build_timeout
397 build_interval = self.conf.build_interval
398 start = timeutils.utcnow()
399 while timeutils.delta_seconds(start,
400 timeutils.utcnow()) < build_timeout:
401 try:
402 fn(*args, **kwargs)
403 except heat_exceptions.HTTPConflict as ex:
404 # FIXME(sirushtim): Wait a little for the stack lock to be
405 # released and hopefully, the stack should be usable again.
406 if ex.error['error']['type'] != 'ActionInProgress':
407 raise ex
408
409 time.sleep(build_interval)
410 else:
411 break
412
Steven Hardy23284b62015-10-01 19:03:42 +0100413 def update_stack(self, stack_identifier, template=None, environment=None,
Sabeen Syed277ea692015-02-04 23:30:02 +0000414 files=None, parameters=None, tags=None,
Rakesh H Sa3325d62015-04-04 19:42:29 +0530415 expected_status='UPDATE_COMPLETE',
Steven Hardy23284b62015-10-01 19:03:42 +0100416 disable_rollback=True,
417 existing=False):
Steven Hardyc9efd972014-11-20 11:31:55 +0000418 env = environment or {}
419 env_files = files or {}
Sergey Kraynevbcc78df2015-02-27 04:34:32 -0500420 parameters = parameters or {}
Sirushti Murugesan3a195a52015-05-01 09:47:09 +0530421
Sergey Kraynev89082a32015-09-04 04:42:33 -0400422 self.updated_time[stack_identifier] = self.client.stacks.get(
Sergey Kraynevf07f4712016-02-15 05:24:17 -0500423 stack_identifier, resolve_outputs=False).updated_time
Sirushti Murugesan3a195a52015-05-01 09:47:09 +0530424
Thomas Herve3eab2942015-10-22 17:29:21 +0200425 self._handle_in_progress(
426 self.client.stacks.update,
427 stack_id=stack_identifier,
Thomas Herve3eab2942015-10-22 17:29:21 +0200428 template=template,
429 files=env_files,
430 disable_rollback=disable_rollback,
431 parameters=parameters,
432 environment=env,
433 tags=tags,
434 existing=existing)
Sirushti Murugesan3a195a52015-05-01 09:47:09 +0530435
Rakesh H Sa3325d62015-04-04 19:42:29 +0530436 kwargs = {'stack_identifier': stack_identifier,
437 'status': expected_status}
438 if expected_status in ['ROLLBACK_COMPLETE']:
Rakesh H Sa3325d62015-04-04 19:42:29 +0530439 # To trigger rollback you would intentionally fail the stack
440 # Hence check for rollback failures
441 kwargs['failure_pattern'] = '^ROLLBACK_FAILED$'
442
443 self._wait_for_stack_status(**kwargs)
Steven Hardyc9efd972014-11-20 11:31:55 +0000444
rabi5eaa4962017-08-31 10:55:13 +0530445 def cancel_update_stack(self, stack_identifier, rollback=True,
Oleksii Chuprykovfc2c58f2016-04-29 17:03:17 +0300446 expected_status='ROLLBACK_COMPLETE'):
447
448 stack_name = stack_identifier.split('/')[0]
449
450 self.updated_time[stack_identifier] = self.client.stacks.get(
451 stack_identifier, resolve_outputs=False).updated_time
452
rabi5eaa4962017-08-31 10:55:13 +0530453 if rollback:
454 self.client.actions.cancel_update(stack_name)
455 else:
456 self.client.actions.cancel_without_rollback(stack_name)
Oleksii Chuprykovfc2c58f2016-04-29 17:03:17 +0300457
458 kwargs = {'stack_identifier': stack_identifier,
459 'status': expected_status}
rabi5eaa4962017-08-31 10:55:13 +0530460 if expected_status == 'UPDATE_FAILED':
461 kwargs['is_action_cancelled'] = True
462
Oleksii Chuprykovfc2c58f2016-04-29 17:03:17 +0300463 if expected_status in ['ROLLBACK_COMPLETE']:
464 # To trigger rollback you would intentionally fail the stack
465 # Hence check for rollback failures
466 kwargs['failure_pattern'] = '^ROLLBACK_FAILED$'
467
468 self._wait_for_stack_status(**kwargs)
469
Jason Dunsmoreb5aa9022015-09-09 16:57:04 -0500470 def preview_update_stack(self, stack_identifier, template,
471 environment=None, files=None, parameters=None,
Steven Hardye6de2d62015-12-07 15:59:09 +0000472 tags=None, disable_rollback=True,
473 show_nested=False):
Jason Dunsmoreb5aa9022015-09-09 16:57:04 -0500474 env = environment or {}
475 env_files = files or {}
476 parameters = parameters or {}
Jason Dunsmoreb5aa9022015-09-09 16:57:04 -0500477
478 return self.client.stacks.preview_update(
479 stack_id=stack_identifier,
Jason Dunsmoreb5aa9022015-09-09 16:57:04 -0500480 template=template,
481 files=env_files,
482 disable_rollback=disable_rollback,
483 parameters=parameters,
484 environment=env,
Steven Hardye6de2d62015-12-07 15:59:09 +0000485 tags=tags,
486 show_nested=show_nested
Jason Dunsmoreb5aa9022015-09-09 16:57:04 -0500487 )
488
Steven Hardy03da0742015-03-19 00:13:17 -0400489 def assert_resource_is_a_stack(self, stack_identifier, res_name,
490 wait=False):
491 build_timeout = self.conf.build_timeout
492 build_interval = self.conf.build_interval
493 start = timeutils.utcnow()
494 while timeutils.delta_seconds(start,
495 timeutils.utcnow()) < build_timeout:
496 time.sleep(build_interval)
497 try:
498 nested_identifier = self._get_nested_identifier(
499 stack_identifier, res_name)
500 except Exception:
501 # We may have to wait, if the create is in-progress
502 if wait:
503 time.sleep(build_interval)
504 else:
505 raise
506 else:
507 return nested_identifier
508
509 def _get_nested_identifier(self, stack_identifier, res_name):
Angus Salkeld2bd63a42015-01-07 11:11:29 +1000510 rsrc = self.client.resources.get(stack_identifier, res_name)
511 nested_link = [l for l in rsrc.links if l['rel'] == 'nested']
512 nested_href = nested_link[0]['href']
513 nested_id = nested_href.split('/')[-1]
514 nested_identifier = '/'.join(nested_href.split('/')[-2:])
515 self.assertEqual(rsrc.physical_resource_id, nested_id)
516
Sergey Kraynevf07f4712016-02-15 05:24:17 -0500517 nested_stack = self.client.stacks.get(nested_id, resolve_outputs=False)
Angus Salkeld2bd63a42015-01-07 11:11:29 +1000518 nested_identifier2 = '%s/%s' % (nested_stack.stack_name,
519 nested_stack.id)
520 self.assertEqual(nested_identifier, nested_identifier2)
521 parent_id = stack_identifier.split("/")[-1]
522 self.assertEqual(parent_id, nested_stack.parent)
523 return nested_identifier
524
Rabi Mishra8bcff8a2015-09-21 18:15:04 +0530525 def group_nested_identifier(self, stack_identifier,
526 group_name):
527 # Get the nested stack identifier from a group resource
528 rsrc = self.client.resources.get(stack_identifier, group_name)
529 physical_resource_id = rsrc.physical_resource_id
530
Sergey Kraynevf07f4712016-02-15 05:24:17 -0500531 nested_stack = self.client.stacks.get(physical_resource_id,
532 resolve_outputs=False)
Rabi Mishra8bcff8a2015-09-21 18:15:04 +0530533 nested_identifier = '%s/%s' % (nested_stack.stack_name,
534 nested_stack.id)
535 parent_id = stack_identifier.split("/")[-1]
536 self.assertEqual(parent_id, nested_stack.parent)
537 return nested_identifier
538
539 def list_group_resources(self, stack_identifier,
540 group_name, minimal=True):
541 nested_identifier = self.group_nested_identifier(stack_identifier,
542 group_name)
543 if minimal:
544 return self.list_resources(nested_identifier)
545 return self.client.resources.list(nested_identifier)
546
Steven Hardyc9efd972014-11-20 11:31:55 +0000547 def list_resources(self, stack_identifier):
548 resources = self.client.resources.list(stack_identifier)
549 return dict((r.resource_name, r.resource_type) for r in resources)
Steven Hardyf2c82c02014-11-20 14:02:17 +0000550
Steven Hardyd448dae2016-06-14 14:57:28 +0100551 def get_resource_stack_id(self, r):
552 stack_link = [l for l in r.links if l.get('rel') == 'stack'][0]
553 return stack_link['href'].split("/")[-1]
554
Botond Zoltáne0b7aa12017-03-28 08:42:16 +0200555 def get_physical_resource_id(self, stack_identifier, resource_name):
556 try:
557 resource = self.client.resources.get(
558 stack_identifier, resource_name)
559 return resource.physical_resource_id
560 except Exception:
561 raise Exception('Resource (%s) not found in stack (%s)!' %
562 (stack_identifier, resource_name))
563
564 def get_stack_output(self, stack_identifier, output_key,
565 validate_errors=True):
566 stack = self.client.stacks.get(stack_identifier)
567 return self._stack_output(stack, output_key, validate_errors)
568
Rabi Mishra81ca6bc2016-06-16 15:09:20 +0530569 def check_input_values(self, group_resources, key, value):
570 # Check inputs for deployment and derived config
571 for r in group_resources:
572 d = self.client.software_deployments.get(
573 r.physical_resource_id)
574 self.assertEqual({key: value}, d.input_values)
575 c = self.client.software_configs.get(
576 d.config_id)
577 foo_input_c = [i for i in c.inputs if i.get('name') == key][0]
578 self.assertEqual(value, foo_input_c.get('value'))
579
580 def signal_resources(self, resources):
581 # Signal all IN_PROGRESS resources
582 for r in resources:
583 if 'IN_PROGRESS' in r.resource_status:
584 stack_id = self.get_resource_stack_id(r)
585 self.client.resources.signal(stack_id, r.resource_name)
586
Steven Hardyf2c82c02014-11-20 14:02:17 +0000587 def stack_create(self, stack_name=None, template=None, files=None,
Sabeen Syed277ea692015-02-04 23:30:02 +0000588 parameters=None, environment=None, tags=None,
589 expected_status='CREATE_COMPLETE',
Jay Dobies39c4ce42015-11-04 10:49:08 -0500590 disable_rollback=True, enable_cleanup=True,
rabi6ce8d962017-07-10 16:40:12 +0530591 environment_files=None, timeout=None):
Steven Hardyf2c82c02014-11-20 14:02:17 +0000592 name = stack_name or self._stack_rand_name()
593 templ = template or self.template
594 templ_files = files or {}
595 params = parameters or {}
596 env = environment or {}
rabi6ce8d962017-07-10 16:40:12 +0530597 timeout_mins = timeout or self.conf.build_timeout
Steven Hardyf2c82c02014-11-20 14:02:17 +0000598 self.client.stacks.create(
599 stack_name=name,
600 template=templ,
601 files=templ_files,
Rakesh H Sa3325d62015-04-04 19:42:29 +0530602 disable_rollback=disable_rollback,
Steven Hardyf2c82c02014-11-20 14:02:17 +0000603 parameters=params,
Sabeen Syed277ea692015-02-04 23:30:02 +0000604 environment=env,
Jay Dobies39c4ce42015-11-04 10:49:08 -0500605 tags=tags,
rabi6ce8d962017-07-10 16:40:12 +0530606 environment_files=environment_files,
607 timeout_mins=timeout_mins
Steven Hardyf2c82c02014-11-20 14:02:17 +0000608 )
rabic570e0f2017-10-26 13:07:13 +0530609 if enable_cleanup:
Steve Bakerdbea6ab2015-08-19 13:37:08 +1200610 self.addCleanup(self._stack_delete, name)
Steven Hardyf2c82c02014-11-20 14:02:17 +0000611
Sergey Kraynevf07f4712016-02-15 05:24:17 -0500612 stack = self.client.stacks.get(name, resolve_outputs=False)
Steven Hardyf2c82c02014-11-20 14:02:17 +0000613 stack_identifier = '%s/%s' % (name, stack.id)
Rakesh H Sa3325d62015-04-04 19:42:29 +0530614 kwargs = {'stack_identifier': stack_identifier,
615 'status': expected_status}
Steve Bakerf6c8f122015-02-10 13:54:46 +1300616 if expected_status:
Rakesh H Sa3325d62015-04-04 19:42:29 +0530617 if expected_status in ['ROLLBACK_COMPLETE']:
618 # To trigger rollback you would intentionally fail the stack
619 # Hence check for rollback failures
620 kwargs['failure_pattern'] = '^ROLLBACK_FAILED$'
621 self._wait_for_stack_status(**kwargs)
Steven Hardyf2c82c02014-11-20 14:02:17 +0000622 return stack_identifier
Angus Salkeld2bd63a42015-01-07 11:11:29 +1000623
624 def stack_adopt(self, stack_name=None, files=None,
625 parameters=None, environment=None, adopt_data=None,
626 wait_for_status='ADOPT_COMPLETE'):
Rabi Mishra477efc92015-07-31 13:01:45 +0530627 if (self.conf.skip_test_stack_action_list and
628 'ADOPT' in self.conf.skip_test_stack_action_list):
Sirushti Murugesan04ee8022015-02-02 23:00:23 +0530629 self.skipTest('Testing Stack adopt disabled in conf, skipping')
Angus Salkeld2bd63a42015-01-07 11:11:29 +1000630 name = stack_name or self._stack_rand_name()
631 templ_files = files or {}
632 params = parameters or {}
633 env = environment or {}
634 self.client.stacks.create(
635 stack_name=name,
636 files=templ_files,
637 disable_rollback=True,
638 parameters=params,
639 environment=env,
640 adopt_stack_data=adopt_data,
641 )
Steve Bakerdbea6ab2015-08-19 13:37:08 +1200642 self.addCleanup(self._stack_delete, name)
Sergey Kraynevf07f4712016-02-15 05:24:17 -0500643 stack = self.client.stacks.get(name, resolve_outputs=False)
Angus Salkeld2bd63a42015-01-07 11:11:29 +1000644 stack_identifier = '%s/%s' % (name, stack.id)
645 self._wait_for_stack_status(stack_identifier, wait_for_status)
646 return stack_identifier
Sirushti Murugesan04ee8022015-02-02 23:00:23 +0530647
648 def stack_abandon(self, stack_id):
Rabi Mishra477efc92015-07-31 13:01:45 +0530649 if (self.conf.skip_test_stack_action_list and
650 'ABANDON' in self.conf.skip_test_stack_action_list):
Steve Bakerdbea6ab2015-08-19 13:37:08 +1200651 self.addCleanup(self._stack_delete, stack_id)
Sirushti Murugesan04ee8022015-02-02 23:00:23 +0530652 self.skipTest('Testing Stack abandon disabled in conf, skipping')
653 info = self.client.stacks.abandon(stack_id=stack_id)
654 return info
Oleksii Chuprykovd9cd9dc2015-02-03 10:34:55 -0500655
rabi90b3ab42017-05-04 13:02:28 +0530656 def stack_snapshot(self, stack_id,
657 wait_for_status='SNAPSHOT_COMPLETE'):
658 snapshot = self.client.stacks.snapshot(stack_id=stack_id)
659 self._wait_for_stack_status(stack_id, wait_for_status)
660 return snapshot['id']
661
662 def stack_restore(self, stack_id, snapshot_id,
663 wait_for_status='RESTORE_COMPLETE'):
664 self.client.stacks.restore(stack_id, snapshot_id)
665 self._wait_for_stack_status(stack_id, wait_for_status)
666
Oleksii Chuprykovd9cd9dc2015-02-03 10:34:55 -0500667 def stack_suspend(self, stack_identifier):
Rabi Mishra477efc92015-07-31 13:01:45 +0530668 if (self.conf.skip_test_stack_action_list and
669 'SUSPEND' in self.conf.skip_test_stack_action_list):
Steve Bakerdbea6ab2015-08-19 13:37:08 +1200670 self.addCleanup(self._stack_delete, stack_identifier)
Rabi Mishra477efc92015-07-31 13:01:45 +0530671 self.skipTest('Testing Stack suspend disabled in conf, skipping')
rabif7d67082016-05-17 18:51:22 +0530672 self._handle_in_progress(self.client.actions.suspend, stack_identifier)
Angus Salkelda7500d12015-04-10 15:44:07 +1000673 # improve debugging by first checking the resource's state.
674 self._wait_for_all_resource_status(stack_identifier,
675 'SUSPEND_COMPLETE')
Oleksii Chuprykovd9cd9dc2015-02-03 10:34:55 -0500676 self._wait_for_stack_status(stack_identifier, 'SUSPEND_COMPLETE')
677
678 def stack_resume(self, stack_identifier):
Rabi Mishra477efc92015-07-31 13:01:45 +0530679 if (self.conf.skip_test_stack_action_list and
680 'RESUME' in self.conf.skip_test_stack_action_list):
Steve Bakerdbea6ab2015-08-19 13:37:08 +1200681 self.addCleanup(self._stack_delete, stack_identifier)
Rabi Mishra477efc92015-07-31 13:01:45 +0530682 self.skipTest('Testing Stack resume disabled in conf, skipping')
rabif7d67082016-05-17 18:51:22 +0530683 self._handle_in_progress(self.client.actions.resume, stack_identifier)
Angus Salkelda7500d12015-04-10 15:44:07 +1000684 # improve debugging by first checking the resource's state.
685 self._wait_for_all_resource_status(stack_identifier,
686 'RESUME_COMPLETE')
Oleksii Chuprykovd9cd9dc2015-02-03 10:34:55 -0500687 self._wait_for_stack_status(stack_identifier, 'RESUME_COMPLETE')
Steven Hardy03da0742015-03-19 00:13:17 -0400688
689 def wait_for_event_with_reason(self, stack_identifier, reason,
690 rsrc_name=None, num_expected=1):
691 build_timeout = self.conf.build_timeout
692 build_interval = self.conf.build_interval
693 start = timeutils.utcnow()
694 while timeutils.delta_seconds(start,
695 timeutils.utcnow()) < build_timeout:
696 try:
697 rsrc_events = self.client.events.list(stack_identifier,
698 resource_name=rsrc_name)
699 except heat_exceptions.HTTPNotFound:
liyi09461f72017-03-21 12:17:51 +0800700 LOG.debug("No events yet found for %s", rsrc_name)
Steven Hardy03da0742015-03-19 00:13:17 -0400701 else:
702 matched = [e for e in rsrc_events
703 if e.resource_status_reason == reason]
704 if len(matched) == num_expected:
705 return matched
706 time.sleep(build_interval)
Rakesh H Sc5735a82016-04-28 15:38:09 +0530707
Thomas Hervea6afca82017-04-10 23:44:26 +0200708 def check_autoscale_complete(self, stack_id, expected_num, parent_stack,
709 policy):
Rakesh H Sc5735a82016-04-28 15:38:09 +0530710 res_list = self.client.resources.list(stack_id)
711 all_res_complete = all(res.resource_status in ('UPDATE_COMPLETE',
712 'CREATE_COMPLETE')
713 for res in res_list)
714 all_res = len(res_list) == expected_num
Thomas Hervea6afca82017-04-10 23:44:26 +0200715 if all_res and all_res_complete:
716 metadata = self.client.resources.metadata(parent_stack, policy)
717 return not metadata.get('scaling_in_progress')
718 return False