ZhiQiang Fan | 39f9722 | 2013-09-20 04:49:44 +0800 | [diff] [blame] | 1 | # Copyright 2012 OpenStack Foundation |
Jay Pipes | 051075a | 2012-04-28 17:39:37 -0400 | [diff] [blame] | 2 | # All Rights Reserved. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 | # not use this file except in compliance with the License. You may obtain |
| 6 | # a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | # License for the specific language governing permissions and limitations |
| 14 | # under the License. |
| 15 | |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 16 | import atexit |
Masayuki Igawa | 80c1b9f | 2013-10-07 17:19:11 +0900 | [diff] [blame] | 17 | import functools |
Ian Wienand | 98c35f3 | 2013-07-23 20:34:23 +1000 | [diff] [blame] | 18 | import os |
Attila Fazekas | 5394332 | 2014-02-10 16:07:34 +0100 | [diff] [blame] | 19 | import sys |
Jay Pipes | 051075a | 2012-04-28 17:39:37 -0400 | [diff] [blame] | 20 | |
Jordan Pittier | 35a6375 | 2016-08-30 13:09:12 +0200 | [diff] [blame] | 21 | import debtcollector.moves |
Matthew Treinish | 78561ad | 2013-07-26 11:41:56 -0400 | [diff] [blame] | 22 | import fixtures |
Doug Hellmann | 583ce2c | 2015-03-11 14:55:46 +0000 | [diff] [blame] | 23 | from oslo_log import log as logging |
Chris Hoge | 296558c | 2015-02-19 00:29:49 -0600 | [diff] [blame] | 24 | import six |
ivan-zhu | 1feeb38 | 2013-01-24 10:14:39 +0800 | [diff] [blame] | 25 | import testtools |
Jay Pipes | 051075a | 2012-04-28 17:39:37 -0400 | [diff] [blame] | 26 | |
Matthew Treinish | 3e04685 | 2013-07-23 16:00:24 -0400 | [diff] [blame] | 27 | from tempest import clients |
Andrea Frittoli (andreaf) | 290b3e1 | 2015-10-08 10:25:02 +0100 | [diff] [blame] | 28 | from tempest.common import credentials_factory as credentials |
Rohan Kanade | 9ce97df | 2013-12-10 18:59:35 +0530 | [diff] [blame] | 29 | from tempest.common import fixed_network |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 30 | import tempest.common.validation_resources as vresources |
Attila Fazekas | dc21642 | 2013-01-29 15:12:14 +0100 | [diff] [blame] | 31 | from tempest import config |
Matthew Treinish | 16c4379 | 2013-09-09 19:55:23 +0000 | [diff] [blame] | 32 | from tempest import exceptions |
Matthew Treinish | 3787e4c | 2016-10-07 21:25:33 -0400 | [diff] [blame] | 33 | from tempest.lib.common import cred_client |
Jordan Pittier | 35a6375 | 2016-08-30 13:09:12 +0200 | [diff] [blame] | 34 | from tempest.lib.common.utils import test_utils |
Andrea Frittoli (andreaf) | db9672e | 2016-02-23 14:07:24 -0500 | [diff] [blame] | 35 | from tempest.lib import decorators |
Andrea Frittoli (andreaf) | af4f7cf | 2016-06-13 15:12:26 +0100 | [diff] [blame] | 36 | from tempest.lib import exceptions as lib_exc |
Jay Pipes | 051075a | 2012-04-28 17:39:37 -0400 | [diff] [blame] | 37 | |
| 38 | LOG = logging.getLogger(__name__) |
| 39 | |
Sean Dague | 86bd842 | 2013-12-20 09:56:44 -0500 | [diff] [blame] | 40 | CONF = config.CONF |
| 41 | |
Matthew Treinish | c1802bc | 2015-12-03 18:48:11 -0500 | [diff] [blame] | 42 | idempotent_id = decorators.idempotent_id |
| 43 | |
Jay Pipes | 051075a | 2012-04-28 17:39:37 -0400 | [diff] [blame] | 44 | |
Yaroslav Lobankov | da999f7 | 2015-06-30 20:32:55 +0300 | [diff] [blame] | 45 | def attr(**kwargs): |
liuchenhong | 00caec5 | 2015-07-19 22:40:28 +0800 | [diff] [blame] | 46 | """A decorator which applies the testtools attr decorator |
Chris Yeoh | 55530bb | 2013-02-08 16:04:27 +1030 | [diff] [blame] | 47 | |
Matthew Treinish | a74f5d4 | 2014-02-07 20:25:44 -0500 | [diff] [blame] | 48 | This decorator applies the testtools.testcase.attr if it is in the list of |
| 49 | attributes to testtools we want to apply. |
Attila Fazekas | b2902af | 2013-02-16 16:22:44 +0100 | [diff] [blame] | 50 | """ |
Chris Yeoh | 55530bb | 2013-02-08 16:04:27 +1030 | [diff] [blame] | 51 | |
| 52 | def decorator(f): |
Giulio Fidente | 4946a05 | 2013-05-14 12:23:51 +0200 | [diff] [blame] | 53 | if 'type' in kwargs and isinstance(kwargs['type'], str): |
| 54 | f = testtools.testcase.attr(kwargs['type'])(f) |
| 55 | elif 'type' in kwargs and isinstance(kwargs['type'], list): |
| 56 | for attr in kwargs['type']: |
| 57 | f = testtools.testcase.attr(attr)(f) |
Matthew Treinish | a74f5d4 | 2014-02-07 20:25:44 -0500 | [diff] [blame] | 58 | return f |
Chris Yeoh | 55530bb | 2013-02-08 16:04:27 +1030 | [diff] [blame] | 59 | |
| 60 | return decorator |
| 61 | |
| 62 | |
Matthew Treinish | 3d8c732 | 2014-08-03 23:53:28 -0400 | [diff] [blame] | 63 | def get_service_list(): |
Matthew Treinish | 8afbffd | 2014-01-21 23:56:13 +0000 | [diff] [blame] | 64 | service_list = { |
| 65 | 'compute': CONF.service_available.nova, |
| 66 | 'image': CONF.service_available.glance, |
| 67 | 'volume': CONF.service_available.cinder, |
Matthew Treinish | 8afbffd | 2014-01-21 23:56:13 +0000 | [diff] [blame] | 68 | 'network': True, |
| 69 | 'identity': True, |
| 70 | 'object_storage': CONF.service_available.swift, |
Matthew Treinish | 8afbffd | 2014-01-21 23:56:13 +0000 | [diff] [blame] | 71 | } |
Matthew Treinish | 3d8c732 | 2014-08-03 23:53:28 -0400 | [diff] [blame] | 72 | return service_list |
Matthew Treinish | 16c4379 | 2013-09-09 19:55:23 +0000 | [diff] [blame] | 73 | |
Matthew Treinish | 3d8c732 | 2014-08-03 23:53:28 -0400 | [diff] [blame] | 74 | |
Yaroslav Lobankov | da999f7 | 2015-06-30 20:32:55 +0300 | [diff] [blame] | 75 | def services(*args): |
Matthew Treinish | 3d8c732 | 2014-08-03 23:53:28 -0400 | [diff] [blame] | 76 | """A decorator used to set an attr for each service used in a test case |
| 77 | |
| 78 | This decorator applies a testtools attr for each service that gets |
| 79 | exercised by a test case. |
| 80 | """ |
Matthew Treinish | 16c4379 | 2013-09-09 19:55:23 +0000 | [diff] [blame] | 81 | def decorator(f): |
Masayuki Igawa | 22b3008 | 2016-06-27 16:18:59 +0900 | [diff] [blame] | 82 | services = ['compute', 'image', 'baremetal', 'volume', |
| 83 | 'network', 'identity', 'object_storage'] |
Matthew Treinish | 16c4379 | 2013-09-09 19:55:23 +0000 | [diff] [blame] | 84 | for service in args: |
Matthew Treinish | 3d8c732 | 2014-08-03 23:53:28 -0400 | [diff] [blame] | 85 | if service not in services: |
| 86 | raise exceptions.InvalidServiceTag('%s is not a valid ' |
| 87 | 'service' % service) |
Matthew Treinish | 16c4379 | 2013-09-09 19:55:23 +0000 | [diff] [blame] | 88 | attr(type=list(args))(f) |
Matthew Treinish | 8afbffd | 2014-01-21 23:56:13 +0000 | [diff] [blame] | 89 | |
| 90 | @functools.wraps(f) |
| 91 | def wrapper(self, *func_args, **func_kwargs): |
Matthew Treinish | 3d8c732 | 2014-08-03 23:53:28 -0400 | [diff] [blame] | 92 | service_list = get_service_list() |
| 93 | |
Matthew Treinish | 8afbffd | 2014-01-21 23:56:13 +0000 | [diff] [blame] | 94 | for service in args: |
| 95 | if not service_list[service]: |
| 96 | msg = 'Skipped because the %s service is not available' % ( |
| 97 | service) |
| 98 | raise testtools.TestCase.skipException(msg) |
| 99 | return f(self, *func_args, **func_kwargs) |
| 100 | return wrapper |
Matthew Treinish | 16c4379 | 2013-09-09 19:55:23 +0000 | [diff] [blame] | 101 | return decorator |
| 102 | |
| 103 | |
Yaroslav Lobankov | da999f7 | 2015-06-30 20:32:55 +0300 | [diff] [blame] | 104 | def requires_ext(**kwargs): |
Matthew Treinish | e3d2614 | 2013-11-26 19:14:58 +0000 | [diff] [blame] | 105 | """A decorator to skip tests if an extension is not enabled |
| 106 | |
| 107 | @param extension |
| 108 | @param service |
| 109 | """ |
| 110 | def decorator(func): |
| 111 | @functools.wraps(func) |
| 112 | def wrapper(*func_args, **func_kwargs): |
| 113 | if not is_extension_enabled(kwargs['extension'], |
| 114 | kwargs['service']): |
| 115 | msg = "Skipped because %s extension: %s is not enabled" % ( |
| 116 | kwargs['service'], kwargs['extension']) |
| 117 | raise testtools.TestCase.skipException(msg) |
| 118 | return func(*func_args, **func_kwargs) |
| 119 | return wrapper |
| 120 | return decorator |
| 121 | |
| 122 | |
| 123 | def is_extension_enabled(extension_name, service): |
| 124 | """A function that will check the list of enabled extensions from config |
| 125 | |
| 126 | """ |
Matthew Treinish | e3d2614 | 2013-11-26 19:14:58 +0000 | [diff] [blame] | 127 | config_dict = { |
Matthew Treinish | bc0e03e | 2014-01-30 16:51:06 +0000 | [diff] [blame] | 128 | 'compute': CONF.compute_feature_enabled.api_extensions, |
Matthew Treinish | bc0e03e | 2014-01-30 16:51:06 +0000 | [diff] [blame] | 129 | 'volume': CONF.volume_feature_enabled.api_extensions, |
| 130 | 'network': CONF.network_feature_enabled.api_extensions, |
| 131 | 'object': CONF.object_storage_feature_enabled.discoverable_apis, |
Jane Zadorozhna | 121576d | 2015-06-23 12:57:13 +0300 | [diff] [blame] | 132 | 'identity': CONF.identity_feature_enabled.api_extensions |
Matthew Treinish | e3d2614 | 2013-11-26 19:14:58 +0000 | [diff] [blame] | 133 | } |
Simeon Monov | 5d7effe | 2014-07-16 07:32:38 +0300 | [diff] [blame] | 134 | if len(config_dict[service]) == 0: |
| 135 | return False |
Matthew Treinish | e3d2614 | 2013-11-26 19:14:58 +0000 | [diff] [blame] | 136 | if config_dict[service][0] == 'all': |
| 137 | return True |
| 138 | if extension_name in config_dict[service]: |
| 139 | return True |
| 140 | return False |
| 141 | |
Ian Wienand | 98c35f3 | 2013-07-23 20:34:23 +1000 | [diff] [blame] | 142 | |
Ken'ichi Ohmichi | a1111c5 | 2016-12-01 19:42:03 -0800 | [diff] [blame] | 143 | def related_bug(bug, status_code=None): |
| 144 | """A decorator useful to know solutions from launchpad bug reports |
| 145 | |
| 146 | @param bug: The launchpad bug number causing the test |
| 147 | @param status_code: The status code related to the bug report |
| 148 | """ |
| 149 | def decorator(f): |
| 150 | @functools.wraps(f) |
| 151 | def wrapper(self, *func_args, **func_kwargs): |
| 152 | try: |
| 153 | return f(self, *func_args, **func_kwargs) |
| 154 | except Exception as exc: |
| 155 | exc_status_code = getattr(exc, 'status_code', None) |
| 156 | if status_code is None or status_code == exc_status_code: |
| 157 | LOG.error('Hints: This test was made for the bug %s. ' |
| 158 | 'The failure could be related to ' |
Jordan Pittier | 525ec71 | 2016-12-07 17:51:26 +0100 | [diff] [blame] | 159 | 'https://launchpad.net/bugs/%s', bug, bug) |
Ken'ichi Ohmichi | a1111c5 | 2016-12-01 19:42:03 -0800 | [diff] [blame] | 160 | raise exc |
| 161 | return wrapper |
| 162 | return decorator |
| 163 | |
| 164 | |
Yair Fried | 9591412 | 2016-03-03 09:14:40 +0200 | [diff] [blame] | 165 | def is_scheduler_filter_enabled(filter_name): |
ghanshyam | 5817e14 | 2016-12-01 11:38:46 +0900 | [diff] [blame] | 166 | """Check the list of enabled compute scheduler filters from config. |
| 167 | |
| 168 | This function checks whether the given compute scheduler filter is |
| 169 | available and configured in the config file. If the |
| 170 | scheduler_available_filters option is set to 'all' (Default value. which |
| 171 | means default filters are configured in nova) in tempest.conf then, this |
| 172 | function returns True with assumption that requested filter 'filter_name' |
| 173 | is one of available filter in nova ("nova.scheduler.filters.all_filters"). |
| 174 | """ |
Yair Fried | ca5cfb5 | 2016-01-04 15:41:55 +0200 | [diff] [blame] | 175 | |
| 176 | filters = CONF.compute_feature_enabled.scheduler_available_filters |
| 177 | if len(filters) == 0: |
| 178 | return False |
| 179 | if 'all' in filters: |
| 180 | return True |
| 181 | if filter_name in filters: |
| 182 | return True |
| 183 | return False |
| 184 | |
| 185 | |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 186 | at_exit_set = set() |
| 187 | |
| 188 | |
| 189 | def validate_tearDownClass(): |
| 190 | if at_exit_set: |
Sean Dague | eb1523b | 2014-03-10 10:17:44 -0400 | [diff] [blame] | 191 | LOG.error( |
| 192 | "tearDownClass does not call the super's " |
| 193 | "tearDownClass in these classes: \n" |
| 194 | + str(at_exit_set)) |
| 195 | |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 196 | |
| 197 | atexit.register(validate_tearDownClass) |
| 198 | |
Attila Fazekas | 5394332 | 2014-02-10 16:07:34 +0100 | [diff] [blame] | 199 | |
Matthew Treinish | 2474f41 | 2014-11-17 18:11:56 -0500 | [diff] [blame] | 200 | class BaseTestCase(testtools.testcase.WithAttributes, |
| 201 | testtools.TestCase): |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 202 | """The test base class defines Tempest framework for class level fixtures. |
Ken'ichi Ohmichi | 2e2ee19 | 2015-11-19 09:48:27 +0000 | [diff] [blame] | 203 | |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 204 | `setUpClass` and `tearDownClass` are defined here and cannot be overwritten |
| 205 | by subclasses (enforced via hacking rule T105). |
| 206 | |
| 207 | Set-up is split in a series of steps (setup stages), which can be |
| 208 | overwritten by test classes. Set-up stages are: |
| 209 | - skip_checks |
| 210 | - setup_credentials |
| 211 | - setup_clients |
| 212 | - resource_setup |
| 213 | |
| 214 | Tear-down is also split in a series of steps (teardown stages), which are |
| 215 | stacked for execution only if the corresponding setup stage had been |
| 216 | reached during the setup phase. Tear-down stages are: |
Andrea Frittoli (andreaf) | 17209bb | 2015-05-22 10:16:57 -0700 | [diff] [blame] | 217 | - clear_credentials (defined in the base test class) |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 218 | - resource_cleanup |
| 219 | """ |
Attila Fazekas | c43fec8 | 2013-04-09 23:17:52 +0200 | [diff] [blame] | 220 | |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 221 | setUpClassCalled = False |
| 222 | |
Andrea Frittoli | b21de6c | 2015-02-06 20:12:38 +0000 | [diff] [blame] | 223 | # NOTE(andreaf) credentials holds a list of the credentials to be allocated |
Andrea Frittoli (andreaf) | 825b2d3 | 2015-04-08 20:58:01 +0100 | [diff] [blame] | 224 | # at class setup time. Credential types can be 'primary', 'alt', 'admin' or |
| 225 | # a list of roles - the first element of the list being a label, and the |
| 226 | # rest the actual roles |
Andrea Frittoli | b21de6c | 2015-02-06 20:12:38 +0000 | [diff] [blame] | 227 | credentials = [] |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 228 | # Resources required to validate a server using ssh |
| 229 | validation_resources = {} |
Matthew Treinish | 9f756a0 | 2014-01-15 10:26:07 -0500 | [diff] [blame] | 230 | network_resources = {} |
| 231 | |
Sean Dague | 2ef32ac | 2014-06-09 11:32:23 -0400 | [diff] [blame] | 232 | # NOTE(sdague): log_format is defined inline here instead of using the oslo |
| 233 | # default because going through the config path recouples config to the |
| 234 | # stress tests too early, and depending on testr order will fail unit tests |
| 235 | log_format = ('%(asctime)s %(process)d %(levelname)-8s ' |
| 236 | '[%(name)s] %(message)s') |
| 237 | |
Ryota MIBU | 60687e5 | 2015-12-09 18:37:39 +0900 | [diff] [blame] | 238 | # Client manager class to use in this test case. |
| 239 | client_manager = clients.Manager |
| 240 | |
Sean Dague | 02620fd | 2016-03-02 15:52:51 -0500 | [diff] [blame] | 241 | # A way to adjust slow test classes |
| 242 | TIMEOUT_SCALING_FACTOR = 1 |
| 243 | |
Pavel Sedlák | 1053bd3 | 2013-04-16 16:47:40 +0200 | [diff] [blame] | 244 | @classmethod |
| 245 | def setUpClass(cls): |
Andrea Frittoli | 73ee247 | 2014-09-15 12:31:53 +0100 | [diff] [blame] | 246 | # It should never be overridden by descendants |
Pavel Sedlák | 1053bd3 | 2013-04-16 16:47:40 +0200 | [diff] [blame] | 247 | if hasattr(super(BaseTestCase, cls), 'setUpClass'): |
| 248 | super(BaseTestCase, cls).setUpClass() |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 249 | cls.setUpClassCalled = True |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 250 | # Stack of (name, callable) to be invoked in reverse order at teardown |
| 251 | cls.teardowns = [] |
| 252 | # All the configuration checks that may generate a skip |
| 253 | cls.skip_checks() |
Andrea Frittoli | 73ee247 | 2014-09-15 12:31:53 +0100 | [diff] [blame] | 254 | try: |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 255 | # Allocation of all required credentials and client managers |
Andrea Frittoli (andreaf) | 17209bb | 2015-05-22 10:16:57 -0700 | [diff] [blame] | 256 | cls.teardowns.append(('credentials', cls.clear_credentials)) |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 257 | cls.setup_credentials() |
| 258 | # Shortcuts to clients |
| 259 | cls.setup_clients() |
| 260 | # Additional class-wide test resources |
| 261 | cls.teardowns.append(('resources', cls.resource_cleanup)) |
Andrea Frittoli | 73ee247 | 2014-09-15 12:31:53 +0100 | [diff] [blame] | 262 | cls.resource_setup() |
| 263 | except Exception: |
| 264 | etype, value, trace = sys.exc_info() |
Jordan Pittier | 525ec71 | 2016-12-07 17:51:26 +0100 | [diff] [blame] | 265 | LOG.info("%s raised in %s.setUpClass. Invoking tearDownClass.", |
| 266 | etype, cls.__name__) |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 267 | cls.tearDownClass() |
Andrea Frittoli | 73ee247 | 2014-09-15 12:31:53 +0100 | [diff] [blame] | 268 | try: |
Matthew Treinish | 843227d | 2015-04-23 10:17:17 -0400 | [diff] [blame] | 269 | six.reraise(etype, value, trace) |
Andrea Frittoli | 73ee247 | 2014-09-15 12:31:53 +0100 | [diff] [blame] | 270 | finally: |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 271 | del trace # to avoid circular refs |
Pavel Sedlák | 1053bd3 | 2013-04-16 16:47:40 +0200 | [diff] [blame] | 272 | |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 273 | @classmethod |
| 274 | def tearDownClass(cls): |
Attila Fazekas | 5d27530 | 2013-08-29 12:35:12 +0200 | [diff] [blame] | 275 | at_exit_set.discard(cls) |
Andrea Frittoli | 73ee247 | 2014-09-15 12:31:53 +0100 | [diff] [blame] | 276 | # It should never be overridden by descendants |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 277 | if hasattr(super(BaseTestCase, cls), 'tearDownClass'): |
| 278 | super(BaseTestCase, cls).tearDownClass() |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 279 | # Save any existing exception, we always want to re-raise the original |
| 280 | # exception only |
| 281 | etype, value, trace = sys.exc_info() |
| 282 | # If there was no exception during setup we shall re-raise the first |
| 283 | # exception in teardown |
| 284 | re_raise = (etype is None) |
| 285 | while cls.teardowns: |
| 286 | name, teardown = cls.teardowns.pop() |
| 287 | # Catch any exception in tearDown so we can re-raise the original |
| 288 | # exception at the end |
| 289 | try: |
| 290 | teardown() |
| 291 | except Exception as te: |
| 292 | sys_exec_info = sys.exc_info() |
| 293 | tetype = sys_exec_info[0] |
| 294 | # TODO(andreaf): Till we have the ability to cleanup only |
| 295 | # resources that were successfully setup in resource_cleanup, |
| 296 | # log AttributeError as info instead of exception. |
| 297 | if tetype is AttributeError and name == 'resources': |
Jordan Pittier | 525ec71 | 2016-12-07 17:51:26 +0100 | [diff] [blame] | 298 | LOG.info("tearDownClass of %s failed: %s", name, te) |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 299 | else: |
Jordan Pittier | 525ec71 | 2016-12-07 17:51:26 +0100 | [diff] [blame] | 300 | LOG.exception("teardown of %s failed: %s", name, te) |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 301 | if not etype: |
| 302 | etype, value, trace = sys_exec_info |
Joshua White | bd76960 | 2016-02-02 09:30:11 -0800 | [diff] [blame] | 303 | # If exceptions were raised during teardown, and not before, re-raise |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 304 | # the first one |
| 305 | if re_raise and etype is not None: |
| 306 | try: |
Matthew Treinish | 843227d | 2015-04-23 10:17:17 -0400 | [diff] [blame] | 307 | six.reraise(etype, value, trace) |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 308 | finally: |
| 309 | del trace # to avoid circular refs |
Andrea Frittoli | 73ee247 | 2014-09-15 12:31:53 +0100 | [diff] [blame] | 310 | |
| 311 | @classmethod |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 312 | def skip_checks(cls): |
Ken'ichi Ohmichi | 2e2ee19 | 2015-11-19 09:48:27 +0000 | [diff] [blame] | 313 | """Class level skip checks. |
| 314 | |
| 315 | Subclasses verify in here all conditions that might prevent the |
| 316 | execution of the entire test class. |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 317 | Checks implemented here may not make use API calls, and should rely on |
| 318 | configuration alone. |
| 319 | In general skip checks that require an API call are discouraged. |
| 320 | If one is really needed it may be implemented either in the |
| 321 | resource_setup or at test level. |
| 322 | """ |
Andrea Frittoli (andreaf) | 32d0de1 | 2015-10-09 14:43:53 +0100 | [diff] [blame] | 323 | identity_version = cls.get_identity_version() |
| 324 | if 'admin' in cls.credentials and not credentials.is_admin_available( |
| 325 | identity_version=identity_version): |
Andrea Frittoli | b21de6c | 2015-02-06 20:12:38 +0000 | [diff] [blame] | 326 | msg = "Missing Identity Admin API credentials in configuration." |
| 327 | raise cls.skipException(msg) |
Andrea Frittoli (andreaf) | 32d0de1 | 2015-10-09 14:43:53 +0100 | [diff] [blame] | 328 | if 'alt' in cls.credentials and not credentials.is_alt_available( |
| 329 | identity_version=identity_version): |
Andrea Frittoli | b21de6c | 2015-02-06 20:12:38 +0000 | [diff] [blame] | 330 | msg = "Missing a 2nd set of API credentials in configuration." |
| 331 | raise cls.skipException(msg) |
Andrea Frittoli (andreaf) | 4160141 | 2015-05-12 16:39:03 +0100 | [diff] [blame] | 332 | if hasattr(cls, 'identity_version'): |
| 333 | if cls.identity_version == 'v2': |
| 334 | if not CONF.identity_feature_enabled.api_v2: |
| 335 | raise cls.skipException("Identity api v2 is not enabled") |
| 336 | elif cls.identity_version == 'v3': |
| 337 | if not CONF.identity_feature_enabled.api_v3: |
| 338 | raise cls.skipException("Identity api v3 is not enabled") |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 339 | |
| 340 | @classmethod |
| 341 | def setup_credentials(cls): |
edannon | 6cc6fbc | 2016-05-03 11:56:12 +0300 | [diff] [blame] | 342 | """Allocate credentials and create the client managers from them. |
Ken'ichi Ohmichi | 2e2ee19 | 2015-11-19 09:48:27 +0000 | [diff] [blame] | 343 | |
edannon | 6cc6fbc | 2016-05-03 11:56:12 +0300 | [diff] [blame] | 344 | For every element of credentials param function creates tenant/user, |
| 345 | Then it creates client manager for that credential. |
| 346 | |
| 347 | Network related tests must override this function with |
| 348 | set_network_resources() method, otherwise it will create |
| 349 | network resources(network resources are created in a later step). |
Andrea Frittoli | b21de6c | 2015-02-06 20:12:38 +0000 | [diff] [blame] | 350 | """ |
| 351 | for credentials_type in cls.credentials: |
| 352 | # This may raise an exception in case credentials are not available |
| 353 | # In that case we want to let the exception through and the test |
| 354 | # fail accordingly |
Andrea Frittoli (andreaf) | 825b2d3 | 2015-04-08 20:58:01 +0100 | [diff] [blame] | 355 | if isinstance(credentials_type, six.string_types): |
| 356 | manager = cls.get_client_manager( |
| 357 | credential_type=credentials_type) |
| 358 | setattr(cls, 'os_%s' % credentials_type, manager) |
| 359 | # Setup some common aliases |
| 360 | # TODO(andreaf) The aliases below are a temporary hack |
| 361 | # to avoid changing too much code in one patch. They should |
| 362 | # be removed eventually |
| 363 | if credentials_type == 'primary': |
| 364 | cls.os = cls.manager = cls.os_primary |
| 365 | if credentials_type == 'admin': |
| 366 | cls.os_adm = cls.admin_manager = cls.os_admin |
| 367 | if credentials_type == 'alt': |
| 368 | cls.alt_manager = cls.os_alt |
| 369 | elif isinstance(credentials_type, list): |
| 370 | manager = cls.get_client_manager(roles=credentials_type[1:], |
| 371 | force_new=True) |
| 372 | setattr(cls, 'os_roles_%s' % credentials_type[0], manager) |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 373 | |
| 374 | @classmethod |
| 375 | def setup_clients(cls): |
| 376 | """Create links to the clients into the test object.""" |
| 377 | # TODO(andreaf) There is a fair amount of code that could me moved from |
| 378 | # base / test classes in here. Ideally tests should be able to only |
| 379 | # specify which client is `client` and nothing else. |
Andrea Frittoli | 73ee247 | 2014-09-15 12:31:53 +0100 | [diff] [blame] | 380 | pass |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 381 | |
Emily Hugenbruch | 5bd4cbf | 2014-12-17 21:38:38 +0000 | [diff] [blame] | 382 | @classmethod |
| 383 | def resource_setup(cls): |
Ken'ichi Ohmichi | 2e2ee19 | 2015-11-19 09:48:27 +0000 | [diff] [blame] | 384 | """Class level resource setup for test cases.""" |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 385 | if hasattr(cls, "os"): |
| 386 | cls.validation_resources = vresources.create_validation_resources( |
| 387 | cls.os, cls.validation_resources) |
| 388 | else: |
zhangguoqing | 6c09664 | 2016-01-04 06:17:21 +0000 | [diff] [blame] | 389 | LOG.warning("Client manager not found, validation resources not" |
| 390 | " created") |
Emily Hugenbruch | 5bd4cbf | 2014-12-17 21:38:38 +0000 | [diff] [blame] | 391 | |
| 392 | @classmethod |
| 393 | def resource_cleanup(cls): |
| 394 | """Class level resource cleanup for test cases. |
Ken'ichi Ohmichi | 2e2ee19 | 2015-11-19 09:48:27 +0000 | [diff] [blame] | 395 | |
Emily Hugenbruch | 5bd4cbf | 2014-12-17 21:38:38 +0000 | [diff] [blame] | 396 | Resource cleanup must be able to handle the case of partially setup |
| 397 | resources, in case a failure during `resource_setup` should happen. |
| 398 | """ |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 399 | if cls.validation_resources: |
| 400 | if hasattr(cls, "os"): |
| 401 | vresources.clear_validation_resources(cls.os, |
| 402 | cls.validation_resources) |
| 403 | cls.validation_resources = {} |
| 404 | else: |
zhangguoqing | 6c09664 | 2016-01-04 06:17:21 +0000 | [diff] [blame] | 405 | LOG.warning("Client manager not found, validation resources " |
| 406 | "not deleted") |
Emily Hugenbruch | 5bd4cbf | 2014-12-17 21:38:38 +0000 | [diff] [blame] | 407 | |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 408 | def setUp(self): |
| 409 | super(BaseTestCase, self).setUp() |
| 410 | if not self.setUpClassCalled: |
| 411 | raise RuntimeError("setUpClass does not calls the super's" |
| 412 | "setUpClass in the " |
| 413 | + self.__class__.__name__) |
| 414 | at_exit_set.add(self.__class__) |
Matthew Treinish | 78561ad | 2013-07-26 11:41:56 -0400 | [diff] [blame] | 415 | test_timeout = os.environ.get('OS_TEST_TIMEOUT', 0) |
| 416 | try: |
Sean Dague | 02620fd | 2016-03-02 15:52:51 -0500 | [diff] [blame] | 417 | test_timeout = int(test_timeout) * self.TIMEOUT_SCALING_FACTOR |
Matthew Treinish | 78561ad | 2013-07-26 11:41:56 -0400 | [diff] [blame] | 418 | except ValueError: |
| 419 | test_timeout = 0 |
| 420 | if test_timeout > 0: |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 421 | self.useFixture(fixtures.Timeout(test_timeout, gentle=True)) |
Matthew Treinish | 78561ad | 2013-07-26 11:41:56 -0400 | [diff] [blame] | 422 | |
| 423 | if (os.environ.get('OS_STDOUT_CAPTURE') == 'True' or |
| 424 | os.environ.get('OS_STDOUT_CAPTURE') == '1'): |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 425 | stdout = self.useFixture(fixtures.StringStream('stdout')).stream |
| 426 | self.useFixture(fixtures.MonkeyPatch('sys.stdout', stdout)) |
Matthew Treinish | 78561ad | 2013-07-26 11:41:56 -0400 | [diff] [blame] | 427 | if (os.environ.get('OS_STDERR_CAPTURE') == 'True' or |
| 428 | os.environ.get('OS_STDERR_CAPTURE') == '1'): |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 429 | stderr = self.useFixture(fixtures.StringStream('stderr')).stream |
| 430 | self.useFixture(fixtures.MonkeyPatch('sys.stderr', stderr)) |
Attila Fazekas | 3138807 | 2013-08-15 08:58:07 +0200 | [diff] [blame] | 431 | if (os.environ.get('OS_LOG_CAPTURE') != 'False' and |
| 432 | os.environ.get('OS_LOG_CAPTURE') != '0'): |
Attila Fazekas | 3138807 | 2013-08-15 08:58:07 +0200 | [diff] [blame] | 433 | self.useFixture(fixtures.LoggerFixture(nuke_handlers=False, |
Sean Dague | 2ef32ac | 2014-06-09 11:32:23 -0400 | [diff] [blame] | 434 | format=self.log_format, |
Attila Fazekas | 90445be | 2013-10-24 16:46:03 +0200 | [diff] [blame] | 435 | level=None)) |
Matthew Treinish | 78561ad | 2013-07-26 11:41:56 -0400 | [diff] [blame] | 436 | |
Andrea Frittoli (andreaf) | 1f34241 | 2015-05-12 16:37:19 +0100 | [diff] [blame] | 437 | @property |
| 438 | def credentials_provider(self): |
| 439 | return self._get_credentials_provider() |
| 440 | |
Jamie Lennox | 1535017 | 2015-08-17 10:54:25 +1000 | [diff] [blame] | 441 | @property |
| 442 | def identity_utils(self): |
| 443 | """A client that abstracts v2 and v3 identity operations. |
| 444 | |
| 445 | This can be used for creating and tearing down projects in tests. It |
| 446 | should not be used for testing identity features. |
| 447 | """ |
| 448 | if CONF.identity.auth_version == 'v2': |
| 449 | client = self.os_admin.identity_client |
Daniel Mellado | 7aea534 | 2016-02-09 09:10:12 +0000 | [diff] [blame] | 450 | users_client = self.os_admin.users_client |
Daniel Mellado | b04da90 | 2015-11-20 17:43:12 +0100 | [diff] [blame] | 451 | project_client = self.os_admin.tenants_client |
Daniel Mellado | 6b16b92 | 2015-12-07 12:43:08 +0000 | [diff] [blame] | 452 | roles_client = self.os_admin.roles_client |
Daniel Mellado | 91a26b6 | 2016-02-11 11:13:04 +0000 | [diff] [blame] | 453 | domains_client = None |
Jamie Lennox | 1535017 | 2015-08-17 10:54:25 +1000 | [diff] [blame] | 454 | else: |
| 455 | client = self.os_admin.identity_v3_client |
Daniel Mellado | 7aea534 | 2016-02-09 09:10:12 +0000 | [diff] [blame] | 456 | users_client = self.os_admin.users_v3_client |
Arx Cruz | 24bcb88 | 2016-02-10 15:20:16 +0100 | [diff] [blame] | 457 | project_client = self.os_admin.projects_client |
| 458 | roles_client = self.os_admin.roles_v3_client |
Daniel Mellado | 91a26b6 | 2016-02-11 11:13:04 +0000 | [diff] [blame] | 459 | domains_client = self.os_admin.domains_client |
Jamie Lennox | 1535017 | 2015-08-17 10:54:25 +1000 | [diff] [blame] | 460 | |
| 461 | try: |
| 462 | domain = client.auth_provider.credentials.project_domain_name |
| 463 | except AttributeError: |
| 464 | domain = 'Default' |
| 465 | |
Daniel Mellado | b04da90 | 2015-11-20 17:43:12 +0100 | [diff] [blame] | 466 | return cred_client.get_creds_client(client, project_client, |
Daniel Mellado | 82c83a5 | 2015-12-09 15:16:49 +0000 | [diff] [blame] | 467 | users_client, |
Daniel Mellado | 7aea534 | 2016-02-09 09:10:12 +0000 | [diff] [blame] | 468 | roles_client, |
Daniel Mellado | 91a26b6 | 2016-02-11 11:13:04 +0000 | [diff] [blame] | 469 | domains_client, |
Daniel Mellado | b04da90 | 2015-11-20 17:43:12 +0100 | [diff] [blame] | 470 | project_domain_name=domain) |
Jamie Lennox | 1535017 | 2015-08-17 10:54:25 +1000 | [diff] [blame] | 471 | |
Andrea Frittoli (andreaf) | 1f34241 | 2015-05-12 16:37:19 +0100 | [diff] [blame] | 472 | @classmethod |
Andrea Frittoli (andreaf) | 32d0de1 | 2015-10-09 14:43:53 +0100 | [diff] [blame] | 473 | def get_identity_version(cls): |
| 474 | """Returns the identity version used by the test class""" |
| 475 | identity_version = getattr(cls, 'identity_version', None) |
| 476 | return identity_version or CONF.identity.auth_version |
| 477 | |
| 478 | @classmethod |
Andrea Frittoli (andreaf) | 1f34241 | 2015-05-12 16:37:19 +0100 | [diff] [blame] | 479 | def _get_credentials_provider(cls): |
| 480 | """Returns a credentials provider |
| 481 | |
| 482 | If no credential provider exists yet creates one. |
| 483 | It uses self.identity_version if defined, or the configuration value |
| 484 | """ |
| 485 | if (not hasattr(cls, '_creds_provider') or not cls._creds_provider or |
| 486 | not cls._creds_provider.name == cls.__name__): |
| 487 | force_tenant_isolation = getattr(cls, 'force_tenant_isolation', |
| 488 | False) |
Andrea Frittoli (andreaf) | 1f34241 | 2015-05-12 16:37:19 +0100 | [diff] [blame] | 489 | |
Andrea Frittoli (andreaf) | 17209bb | 2015-05-22 10:16:57 -0700 | [diff] [blame] | 490 | cls._creds_provider = credentials.get_credentials_provider( |
Andrea Frittoli (andreaf) | 1f34241 | 2015-05-12 16:37:19 +0100 | [diff] [blame] | 491 | name=cls.__name__, network_resources=cls.network_resources, |
| 492 | force_tenant_isolation=force_tenant_isolation, |
Andrea Frittoli (andreaf) | 32d0de1 | 2015-10-09 14:43:53 +0100 | [diff] [blame] | 493 | identity_version=cls.get_identity_version()) |
Andrea Frittoli (andreaf) | 1f34241 | 2015-05-12 16:37:19 +0100 | [diff] [blame] | 494 | return cls._creds_provider |
| 495 | |
Matthew Treinish | 3e04685 | 2013-07-23 16:00:24 -0400 | [diff] [blame] | 496 | @classmethod |
Andrea Frittoli (andreaf) | 4160141 | 2015-05-12 16:39:03 +0100 | [diff] [blame] | 497 | def get_client_manager(cls, credential_type=None, roles=None, |
| 498 | force_new=None): |
Andrea Frittoli (andreaf) | 737fac9 | 2015-05-12 16:14:35 +0100 | [diff] [blame] | 499 | """Returns an OpenStack client manager |
| 500 | |
| 501 | Returns an OpenStack client manager based on either credential_type |
| 502 | or a list of roles. If neither is specified, it defaults to |
| 503 | credential_type 'primary' |
Andrea Frittoli (andreaf) | 737fac9 | 2015-05-12 16:14:35 +0100 | [diff] [blame] | 504 | :param credential_type: string - primary, alt or admin |
| 505 | :param roles: list of roles |
| 506 | |
lei zhang | dd552b2 | 2015-11-25 20:41:48 +0800 | [diff] [blame] | 507 | :returns: the created client manager |
Andrea Frittoli (andreaf) | 737fac9 | 2015-05-12 16:14:35 +0100 | [diff] [blame] | 508 | :raises skipException: if the requested credentials are not available |
Ryan Hsu | 6c4bb3d | 2013-10-21 21:22:50 -0700 | [diff] [blame] | 509 | """ |
Andrea Frittoli (andreaf) | 737fac9 | 2015-05-12 16:14:35 +0100 | [diff] [blame] | 510 | if all([roles, credential_type]): |
| 511 | msg = "Cannot get credentials by type and roles at the same time" |
| 512 | raise ValueError(msg) |
| 513 | if not any([roles, credential_type]): |
| 514 | credential_type = 'primary' |
Andrea Frittoli (andreaf) | 1f34241 | 2015-05-12 16:37:19 +0100 | [diff] [blame] | 515 | cred_provider = cls._get_credentials_provider() |
Andrea Frittoli (andreaf) | 737fac9 | 2015-05-12 16:14:35 +0100 | [diff] [blame] | 516 | if roles: |
| 517 | for role in roles: |
Andrea Frittoli (andreaf) | 1f34241 | 2015-05-12 16:37:19 +0100 | [diff] [blame] | 518 | if not cred_provider.is_role_available(role): |
Andrea Frittoli (andreaf) | 737fac9 | 2015-05-12 16:14:35 +0100 | [diff] [blame] | 519 | skip_msg = ( |
| 520 | "%s skipped because the configured credential provider" |
| 521 | " is not able to provide credentials with the %s role " |
| 522 | "assigned." % (cls.__name__, role)) |
| 523 | raise cls.skipException(skip_msg) |
| 524 | params = dict(roles=roles) |
| 525 | if force_new is not None: |
| 526 | params.update(force_new=force_new) |
Andrea Frittoli (andreaf) | 1f34241 | 2015-05-12 16:37:19 +0100 | [diff] [blame] | 527 | creds = cred_provider.get_creds_by_roles(**params) |
Andrea Frittoli | b21de6c | 2015-02-06 20:12:38 +0000 | [diff] [blame] | 528 | else: |
Andrea Frittoli (andreaf) | 737fac9 | 2015-05-12 16:14:35 +0100 | [diff] [blame] | 529 | credentials_method = 'get_%s_creds' % credential_type |
Andrea Frittoli (andreaf) | 1f34241 | 2015-05-12 16:37:19 +0100 | [diff] [blame] | 530 | if hasattr(cred_provider, credentials_method): |
| 531 | creds = getattr(cred_provider, credentials_method)() |
Andrea Frittoli (andreaf) | 737fac9 | 2015-05-12 16:14:35 +0100 | [diff] [blame] | 532 | else: |
Andrea Frittoli (andreaf) | af4f7cf | 2016-06-13 15:12:26 +0100 | [diff] [blame] | 533 | raise lib_exc.InvalidCredentials( |
Andrea Frittoli (andreaf) | 737fac9 | 2015-05-12 16:14:35 +0100 | [diff] [blame] | 534 | "Invalid credentials type %s" % credential_type) |
Jordan Pittier | e4be907 | 2017-01-04 19:17:35 +0100 | [diff] [blame^] | 535 | manager = cls.client_manager(credentials=creds.credentials) |
Andrea Frittoli | 7322467 | 2016-12-09 21:08:19 +0000 | [diff] [blame] | 536 | # NOTE(andreaf) Ensure credentials have user and project id fields. |
| 537 | # It may not be the case when using pre-provisioned credentials. |
| 538 | manager.auth_provider.set_auth() |
| 539 | return manager |
Ryan Hsu | 6c4bb3d | 2013-10-21 21:22:50 -0700 | [diff] [blame] | 540 | |
| 541 | @classmethod |
Andrea Frittoli (andreaf) | 17209bb | 2015-05-22 10:16:57 -0700 | [diff] [blame] | 542 | def clear_credentials(cls): |
Ken'ichi Ohmichi | 2e2ee19 | 2015-11-19 09:48:27 +0000 | [diff] [blame] | 543 | """Clears creds if set""" |
Attila Fazekas | 5b0d926 | 2015-05-20 10:17:39 +0200 | [diff] [blame] | 544 | if hasattr(cls, '_creds_provider'): |
Andrea Frittoli (andreaf) | 17209bb | 2015-05-22 10:16:57 -0700 | [diff] [blame] | 545 | cls._creds_provider.clear_creds() |
Ryan Hsu | 6c4bb3d | 2013-10-21 21:22:50 -0700 | [diff] [blame] | 546 | |
| 547 | @classmethod |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 548 | def set_validation_resources(cls, keypair=None, floating_ip=None, |
| 549 | security_group=None, |
| 550 | security_group_rules=None): |
| 551 | """Specify which ssh server validation resources should be created. |
Ken'ichi Ohmichi | 2e2ee19 | 2015-11-19 09:48:27 +0000 | [diff] [blame] | 552 | |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 553 | Each of the argument must be set to either None, True or False, with |
| 554 | None - use default from config (security groups and security group |
| 555 | rules get created when set to None) |
| 556 | False - Do not create the validation resource |
| 557 | True - create the validation resource |
| 558 | |
| 559 | @param keypair |
| 560 | @param security_group |
| 561 | @param security_group_rules |
| 562 | @param floating_ip |
| 563 | """ |
Matthew Treinish | e5cca00 | 2015-05-11 15:36:50 -0400 | [diff] [blame] | 564 | if not CONF.validation.run_validation: |
| 565 | return |
Jordan Pittier | 79cd182 | 2016-12-08 17:20:35 +0100 | [diff] [blame] | 566 | |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 567 | if keypair is None: |
Jordan Pittier | 79cd182 | 2016-12-08 17:20:35 +0100 | [diff] [blame] | 568 | keypair = (CONF.validation.auth_method.lower() == "keypair") |
| 569 | |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 570 | if floating_ip is None: |
Jordan Pittier | 79cd182 | 2016-12-08 17:20:35 +0100 | [diff] [blame] | 571 | floating_ip = (CONF.validation.connect_method.lower() == |
| 572 | "floating") |
| 573 | |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 574 | if security_group is None: |
Brandon Palm | c6cc91d | 2015-08-19 13:20:21 -0500 | [diff] [blame] | 575 | security_group = CONF.validation.security_group |
Jordan Pittier | 79cd182 | 2016-12-08 17:20:35 +0100 | [diff] [blame] | 576 | |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 577 | if security_group_rules is None: |
Brandon Palm | c6cc91d | 2015-08-19 13:20:21 -0500 | [diff] [blame] | 578 | security_group_rules = CONF.validation.security_group_rules |
| 579 | |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 580 | if not cls.validation_resources: |
| 581 | cls.validation_resources = { |
| 582 | 'keypair': keypair, |
| 583 | 'security_group': security_group, |
| 584 | 'security_group_rules': security_group_rules, |
| 585 | 'floating_ip': floating_ip} |
| 586 | |
| 587 | @classmethod |
Andrea Frittoli | 7d5ed59 | 2015-02-10 01:10:23 +0000 | [diff] [blame] | 588 | def set_network_resources(cls, network=False, router=False, subnet=False, |
Matthew Treinish | 9f756a0 | 2014-01-15 10:26:07 -0500 | [diff] [blame] | 589 | dhcp=False): |
| 590 | """Specify which network resources should be created |
| 591 | |
| 592 | @param network |
| 593 | @param router |
| 594 | @param subnet |
| 595 | @param dhcp |
| 596 | """ |
Salvatore Orlando | 5a33724 | 2014-01-15 22:49:22 +0000 | [diff] [blame] | 597 | # network resources should be set only once from callers |
| 598 | # in order to ensure that even if it's called multiple times in |
| 599 | # a chain of overloaded methods, the attribute is set only |
| 600 | # in the leaf class |
Andrea Frittoli | 7d5ed59 | 2015-02-10 01:10:23 +0000 | [diff] [blame] | 601 | if not cls.network_resources: |
| 602 | cls.network_resources = { |
Salvatore Orlando | 5a33724 | 2014-01-15 22:49:22 +0000 | [diff] [blame] | 603 | 'network': network, |
| 604 | 'router': router, |
| 605 | 'subnet': subnet, |
| 606 | 'dhcp': dhcp} |
Matthew Treinish | 9f756a0 | 2014-01-15 10:26:07 -0500 | [diff] [blame] | 607 | |
Rohan Kanade | 9ce97df | 2013-12-10 18:59:35 +0530 | [diff] [blame] | 608 | @classmethod |
Ryan Rossiter | 9228bf7 | 2016-02-25 03:06:12 +0000 | [diff] [blame] | 609 | def get_tenant_network(cls, credentials_type='primary'): |
Rohan Kanade | 9ce97df | 2013-12-10 18:59:35 +0530 | [diff] [blame] | 610 | """Get the network to be used in testing |
| 611 | |
Ryan Rossiter | 9228bf7 | 2016-02-25 03:06:12 +0000 | [diff] [blame] | 612 | :param credentials_type: The type of credentials for which to get the |
| 613 | tenant network |
| 614 | |
Rohan Kanade | 9ce97df | 2013-12-10 18:59:35 +0530 | [diff] [blame] | 615 | :return: network dict including 'id' and 'name' |
| 616 | """ |
Ryan Rossiter | 9228bf7 | 2016-02-25 03:06:12 +0000 | [diff] [blame] | 617 | # Get a manager for the given credentials_type, but at least |
| 618 | # always fall back on getting the manager for primary credentials |
| 619 | if isinstance(credentials_type, six.string_types): |
| 620 | manager = cls.get_client_manager(credential_type=credentials_type) |
| 621 | elif isinstance(credentials_type, list): |
| 622 | manager = cls.get_client_manager(roles=credentials_type[1:]) |
| 623 | else: |
| 624 | manager = cls.get_client_manager() |
| 625 | |
Andrea Frittoli (andreaf) | 17209bb | 2015-05-22 10:16:57 -0700 | [diff] [blame] | 626 | # Make sure cred_provider exists and get a network client |
Ryan Rossiter | 9228bf7 | 2016-02-25 03:06:12 +0000 | [diff] [blame] | 627 | networks_client = manager.compute_networks_client |
Andrea Frittoli (andreaf) | 1f34241 | 2015-05-12 16:37:19 +0100 | [diff] [blame] | 628 | cred_provider = cls._get_credentials_provider() |
Andrea Frittoli | 700711e | 2015-04-02 11:39:38 +0100 | [diff] [blame] | 629 | # In case of nova network, isolated tenants are not able to list the |
Joshua White | bd76960 | 2016-02-02 09:30:11 -0800 | [diff] [blame] | 630 | # network configured in fixed_network_name, even if they can use it |
Andrea Frittoli | 700711e | 2015-04-02 11:39:38 +0100 | [diff] [blame] | 631 | # for their servers, so using an admin network client to validate |
| 632 | # the network name |
| 633 | if (not CONF.service_available.neutron and |
Andrea Frittoli (andreaf) | 32d0de1 | 2015-10-09 14:43:53 +0100 | [diff] [blame] | 634 | credentials.is_admin_available( |
| 635 | identity_version=cls.get_identity_version())): |
Andrea Frittoli (andreaf) | 1f34241 | 2015-05-12 16:37:19 +0100 | [diff] [blame] | 636 | admin_creds = cred_provider.get_admin_creds() |
Andrea Frittoli (andreaf) | 848c4a1 | 2016-06-09 11:09:02 +0100 | [diff] [blame] | 637 | admin_manager = clients.Manager(admin_creds.credentials) |
John Warren | 9487a18 | 2015-09-14 18:12:56 -0400 | [diff] [blame] | 638 | networks_client = admin_manager.compute_networks_client |
Andrea Frittoli (andreaf) | 940f8c6 | 2015-10-30 16:39:24 +0900 | [diff] [blame] | 639 | return fixed_network.get_tenant_network( |
| 640 | cred_provider, networks_client, CONF.compute.fixed_network_name) |
Rohan Kanade | 9ce97df | 2013-12-10 18:59:35 +0530 | [diff] [blame] | 641 | |
Mark Maglana | 5885eb3 | 2014-02-28 10:57:34 -0800 | [diff] [blame] | 642 | def assertEmpty(self, list, msg=None): |
Béla Vancsics | 64862f7 | 2016-11-08 09:12:31 +0100 | [diff] [blame] | 643 | self.assertEqual(0, len(list), msg) |
Mark Maglana | 5885eb3 | 2014-02-28 10:57:34 -0800 | [diff] [blame] | 644 | |
| 645 | def assertNotEmpty(self, list, msg=None): |
Béla Vancsics | 64862f7 | 2016-11-08 09:12:31 +0100 | [diff] [blame] | 646 | self.assertGreater(len(list), 0, msg) |
Mark Maglana | 5885eb3 | 2014-02-28 10:57:34 -0800 | [diff] [blame] | 647 | |
Attila Fazekas | dc21642 | 2013-01-29 15:12:14 +0100 | [diff] [blame] | 648 | |
Jordan Pittier | 35a6375 | 2016-08-30 13:09:12 +0200 | [diff] [blame] | 649 | call_until_true = debtcollector.moves.moved_function( |
| 650 | test_utils.call_until_true, 'call_until_true', __name__, |
| 651 | version='Newton', removal_version='Ocata') |