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