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 |
Attila Fazekas | dc21642 | 2013-01-29 15:12:14 +0100 | [diff] [blame] | 29 | from tempest import config |
Matthew Treinish | 3787e4c | 2016-10-07 21:25:33 -0400 | [diff] [blame] | 30 | from tempest.lib.common import cred_client |
Matthew Treinish | b19c55d | 2017-07-17 12:38:35 -0400 | [diff] [blame] | 31 | from tempest.lib.common import fixed_network |
Andrea Frittoli | 0477acc | 2017-08-09 21:14:53 +0100 | [diff] [blame] | 32 | from tempest.lib.common import validation_resources as vr |
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 | |
Andrea Frittoli | b21de6c | 2015-02-06 20:12:38 +0000 | [diff] [blame] | 101 | # 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] | 102 | # at class setup time. Credential types can be 'primary', 'alt', 'admin' or |
| 103 | # a list of roles - the first element of the list being a label, and the |
| 104 | # rest the actual roles |
Andrea Frittoli | b21de6c | 2015-02-06 20:12:38 +0000 | [diff] [blame] | 105 | credentials = [] |
Andrea Frittoli | bcbf1af1 | 2017-08-14 11:53:35 +0100 | [diff] [blame] | 106 | |
Andrea Frittoli | ba712ac | 2017-09-13 16:54:47 -0600 | [diff] [blame^] | 107 | # Track if setUpClass was invoked |
| 108 | __setupclass_called = False |
| 109 | |
Andrea Frittoli | bcbf1af1 | 2017-08-14 11:53:35 +0100 | [diff] [blame] | 110 | # Network resources to be provisioned for the requested test credentials. |
| 111 | # Only used with the dynamic credentials provider. |
| 112 | _network_resources = {} |
Matthew Treinish | 9f756a0 | 2014-01-15 10:26:07 -0500 | [diff] [blame] | 113 | |
Andrea Frittoli | 3be5748 | 2017-08-25 22:41:26 +0100 | [diff] [blame] | 114 | # Stack of resource cleanups |
| 115 | _class_cleanups = [] |
| 116 | |
Andrea Frittoli | 9f416dd | 2017-08-10 15:38:00 +0100 | [diff] [blame] | 117 | # Resources required to validate a server using ssh |
| 118 | _validation_resources = {} |
| 119 | |
Sean Dague | 2ef32ac | 2014-06-09 11:32:23 -0400 | [diff] [blame] | 120 | # NOTE(sdague): log_format is defined inline here instead of using the oslo |
| 121 | # default because going through the config path recouples config to the |
| 122 | # stress tests too early, and depending on testr order will fail unit tests |
| 123 | log_format = ('%(asctime)s %(process)d %(levelname)-8s ' |
| 124 | '[%(name)s] %(message)s') |
| 125 | |
Ryota MIBU | 60687e5 | 2015-12-09 18:37:39 +0900 | [diff] [blame] | 126 | # Client manager class to use in this test case. |
| 127 | client_manager = clients.Manager |
| 128 | |
Sean Dague | 02620fd | 2016-03-02 15:52:51 -0500 | [diff] [blame] | 129 | # A way to adjust slow test classes |
| 130 | TIMEOUT_SCALING_FACTOR = 1 |
| 131 | |
Pavel Sedlák | 1053bd3 | 2013-04-16 16:47:40 +0200 | [diff] [blame] | 132 | @classmethod |
Andrea Frittoli | 3be5748 | 2017-08-25 22:41:26 +0100 | [diff] [blame] | 133 | def _reset_class(cls): |
Andrea Frittoli | bcbf1af1 | 2017-08-14 11:53:35 +0100 | [diff] [blame] | 134 | cls.__setup_credentials_called = False |
Andrea Frittoli (andreaf) | 08e42d4 | 2017-09-07 17:09:13 +0100 | [diff] [blame] | 135 | cls.__resource_cleanup_called = False |
Andrea Frittoli | 421dc3c | 2017-08-15 12:17:42 +0100 | [diff] [blame] | 136 | cls.__skip_checks_called = False |
Andrea Frittoli | ba712ac | 2017-09-13 16:54:47 -0600 | [diff] [blame^] | 137 | # Stack of callable to be invoked in reverse order |
Andrea Frittoli | 3be5748 | 2017-08-25 22:41:26 +0100 | [diff] [blame] | 138 | cls._class_cleanups = [] |
Andrea Frittoli | ba712ac | 2017-09-13 16:54:47 -0600 | [diff] [blame^] | 139 | # Stack of (name, callable) to be invoked in reverse order at teardown |
| 140 | cls._teardowns = [] |
Andrea Frittoli | 3be5748 | 2017-08-25 22:41:26 +0100 | [diff] [blame] | 141 | |
| 142 | @classmethod |
Pavel Sedlák | 1053bd3 | 2013-04-16 16:47:40 +0200 | [diff] [blame] | 143 | def setUpClass(cls): |
Andrea Frittoli | ba712ac | 2017-09-13 16:54:47 -0600 | [diff] [blame^] | 144 | cls.__setupclass_called = True |
Andrea Frittoli | 3be5748 | 2017-08-25 22:41:26 +0100 | [diff] [blame] | 145 | # Reset state |
| 146 | cls._reset_class() |
Andrea Frittoli | 73ee247 | 2014-09-15 12:31:53 +0100 | [diff] [blame] | 147 | # It should never be overridden by descendants |
Pavel Sedlák | 1053bd3 | 2013-04-16 16:47:40 +0200 | [diff] [blame] | 148 | if hasattr(super(BaseTestCase, cls), 'setUpClass'): |
| 149 | super(BaseTestCase, cls).setUpClass() |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 150 | # All the configuration checks that may generate a skip |
| 151 | cls.skip_checks() |
Andrea Frittoli | 421dc3c | 2017-08-15 12:17:42 +0100 | [diff] [blame] | 152 | if not cls.__skip_checks_called: |
| 153 | raise RuntimeError("skip_checks for %s did not call the super's " |
| 154 | "skip_checks" % cls.__name__) |
Andrea Frittoli | 73ee247 | 2014-09-15 12:31:53 +0100 | [diff] [blame] | 155 | try: |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 156 | # Allocation of all required credentials and client managers |
Andrea Frittoli (andreaf) | 08e42d4 | 2017-09-07 17:09:13 +0100 | [diff] [blame] | 157 | cls._teardowns.append(('credentials', cls.clear_credentials)) |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 158 | cls.setup_credentials() |
Andrea Frittoli | 421dc3c | 2017-08-15 12:17:42 +0100 | [diff] [blame] | 159 | if not cls.__setup_credentials_called: |
| 160 | raise RuntimeError("setup_credentials for %s did not call the " |
| 161 | "super's setup_credentials" % cls.__name__) |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 162 | # Shortcuts to clients |
| 163 | cls.setup_clients() |
| 164 | # Additional class-wide test resources |
Andrea Frittoli (andreaf) | 08e42d4 | 2017-09-07 17:09:13 +0100 | [diff] [blame] | 165 | cls._teardowns.append(('resources', cls.resource_cleanup)) |
Andrea Frittoli | 73ee247 | 2014-09-15 12:31:53 +0100 | [diff] [blame] | 166 | cls.resource_setup() |
| 167 | except Exception: |
| 168 | etype, value, trace = sys.exc_info() |
Jordan Pittier | 525ec71 | 2016-12-07 17:51:26 +0100 | [diff] [blame] | 169 | LOG.info("%s raised in %s.setUpClass. Invoking tearDownClass.", |
| 170 | etype, cls.__name__) |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 171 | cls.tearDownClass() |
Andrea Frittoli | 73ee247 | 2014-09-15 12:31:53 +0100 | [diff] [blame] | 172 | try: |
Matthew Treinish | 843227d | 2015-04-23 10:17:17 -0400 | [diff] [blame] | 173 | six.reraise(etype, value, trace) |
Andrea Frittoli | 73ee247 | 2014-09-15 12:31:53 +0100 | [diff] [blame] | 174 | finally: |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 175 | del trace # to avoid circular refs |
Pavel Sedlák | 1053bd3 | 2013-04-16 16:47:40 +0200 | [diff] [blame] | 176 | |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 177 | @classmethod |
| 178 | def tearDownClass(cls): |
Martin Kopec | ae155b7 | 2017-06-26 09:41:21 +0000 | [diff] [blame] | 179 | # insert pdb breakpoint when pause_teardown is enabled |
| 180 | if CONF.pause_teardown: |
| 181 | cls.insert_pdb_breakpoint() |
Attila Fazekas | 5d27530 | 2013-08-29 12:35:12 +0200 | [diff] [blame] | 182 | at_exit_set.discard(cls) |
Andrea Frittoli | 73ee247 | 2014-09-15 12:31:53 +0100 | [diff] [blame] | 183 | # It should never be overridden by descendants |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 184 | if hasattr(super(BaseTestCase, cls), 'tearDownClass'): |
| 185 | super(BaseTestCase, cls).tearDownClass() |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 186 | # Save any existing exception, we always want to re-raise the original |
| 187 | # exception only |
| 188 | etype, value, trace = sys.exc_info() |
| 189 | # If there was no exception during setup we shall re-raise the first |
| 190 | # exception in teardown |
| 191 | re_raise = (etype is None) |
Andrea Frittoli (andreaf) | 08e42d4 | 2017-09-07 17:09:13 +0100 | [diff] [blame] | 192 | while cls._teardowns: |
| 193 | name, teardown = cls._teardowns.pop() |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 194 | # Catch any exception in tearDown so we can re-raise the original |
| 195 | # exception at the end |
| 196 | try: |
| 197 | teardown() |
Andrea Frittoli | 3be5748 | 2017-08-25 22:41:26 +0100 | [diff] [blame] | 198 | if name == 'resources': |
Andrea Frittoli (andreaf) | 08e42d4 | 2017-09-07 17:09:13 +0100 | [diff] [blame] | 199 | if not cls.__resource_cleanup_called: |
Andrea Frittoli | 3be5748 | 2017-08-25 22:41:26 +0100 | [diff] [blame] | 200 | raise RuntimeError( |
| 201 | "resource_cleanup for %s did not call the " |
| 202 | "super's resource_cleanup" % cls.__name__) |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 203 | except Exception as te: |
| 204 | sys_exec_info = sys.exc_info() |
| 205 | tetype = sys_exec_info[0] |
Andrea Frittoli | 3be5748 | 2017-08-25 22:41:26 +0100 | [diff] [blame] | 206 | # TODO(andreaf): Resource cleanup is often implemented by |
| 207 | # storing an array of resources at class level, and cleaning |
| 208 | # them up during `resource_cleanup`. |
| 209 | # In case of failure during setup, some resource arrays might |
| 210 | # not be defined at all, in which case the cleanup code might |
| 211 | # trigger an AttributeError. In such cases we log |
| 212 | # AttributeError as info instead of exception. Once all |
| 213 | # cleanups are migrated to addClassResourceCleanup we can |
| 214 | # remove this. |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 215 | if tetype is AttributeError and name == 'resources': |
Jordan Pittier | 525ec71 | 2016-12-07 17:51:26 +0100 | [diff] [blame] | 216 | LOG.info("tearDownClass of %s failed: %s", name, te) |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 217 | else: |
Jordan Pittier | 525ec71 | 2016-12-07 17:51:26 +0100 | [diff] [blame] | 218 | LOG.exception("teardown of %s failed: %s", name, te) |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 219 | if not etype: |
| 220 | etype, value, trace = sys_exec_info |
Joshua White | bd76960 | 2016-02-02 09:30:11 -0800 | [diff] [blame] | 221 | # If exceptions were raised during teardown, and not before, re-raise |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 222 | # the first one |
| 223 | if re_raise and etype is not None: |
| 224 | try: |
Matthew Treinish | 843227d | 2015-04-23 10:17:17 -0400 | [diff] [blame] | 225 | six.reraise(etype, value, trace) |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 226 | finally: |
| 227 | del trace # to avoid circular refs |
Andrea Frittoli | 73ee247 | 2014-09-15 12:31:53 +0100 | [diff] [blame] | 228 | |
Martin Kopec | ae155b7 | 2017-06-26 09:41:21 +0000 | [diff] [blame] | 229 | def tearDown(self): |
| 230 | super(BaseTestCase, self).tearDown() |
| 231 | # insert pdb breakpoint when pause_teardown is enabled |
| 232 | if CONF.pause_teardown: |
| 233 | BaseTestCase.insert_pdb_breakpoint() |
| 234 | |
| 235 | @classmethod |
| 236 | def insert_pdb_breakpoint(cls): |
| 237 | """Add pdb breakpoint. |
| 238 | |
| 239 | This can help in debugging process, cleaning of resources is |
| 240 | paused, so they can be examined. |
| 241 | """ |
| 242 | import pdb |
| 243 | pdb.set_trace() |
| 244 | |
Andrea Frittoli | 73ee247 | 2014-09-15 12:31:53 +0100 | [diff] [blame] | 245 | @classmethod |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 246 | def skip_checks(cls): |
Ken'ichi Ohmichi | 2e2ee19 | 2015-11-19 09:48:27 +0000 | [diff] [blame] | 247 | """Class level skip checks. |
| 248 | |
| 249 | Subclasses verify in here all conditions that might prevent the |
Andrea Frittoli | a5440c8 | 2017-08-23 18:11:21 +0100 | [diff] [blame] | 250 | execution of the entire test class. Skipping here prevents any other |
| 251 | class fixture from being executed i.e. no credentials or other |
| 252 | resource allocation will happen. |
| 253 | |
| 254 | Tests defined in the test class will no longer appear in test results. |
| 255 | The `setUpClass` for the entire test class will be marked as SKIPPED |
| 256 | instead. |
| 257 | |
| 258 | At this stage no test credentials are available, so skip checks |
| 259 | should rely on configuration alone. This is deliberate since skips |
| 260 | based on the result of an API call are discouraged. |
| 261 | |
| 262 | The following checks are implemented in `test.py` already: |
| 263 | - check that alt credentials are available when requested by the test |
| 264 | - check that admin credentials are available when requested by the test |
| 265 | - check that the identity version specified by the test is marked as |
| 266 | enabled in the configuration |
| 267 | |
| 268 | Overriders of skip_checks must always invoke skip_check on `super` |
| 269 | first. |
| 270 | |
| 271 | Example:: |
| 272 | |
| 273 | @classmethod |
| 274 | def skip_checks(cls): |
| 275 | super(Example, cls).skip_checks() |
| 276 | if not CONF.service_available.my_service: |
| 277 | skip_msg = ("%s skipped as my_service is not available") |
| 278 | raise cls.skipException(skip_msg % cls.__name__) |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 279 | """ |
Andrea Frittoli | 421dc3c | 2017-08-15 12:17:42 +0100 | [diff] [blame] | 280 | cls.__skip_checks_called = True |
Andrea Frittoli (andreaf) | 32d0de1 | 2015-10-09 14:43:53 +0100 | [diff] [blame] | 281 | identity_version = cls.get_identity_version() |
zhufl | 75498ba | 2017-08-25 10:49:35 +0800 | [diff] [blame] | 282 | # setting force_tenant_isolation to True also needs admin credentials. |
| 283 | if ('admin' in cls.credentials or |
| 284 | getattr(cls, 'force_tenant_isolation', False)): |
| 285 | if not credentials.is_admin_available( |
| 286 | identity_version=identity_version): |
| 287 | raise cls.skipException( |
| 288 | "Missing Identity Admin API credentials in configuration.") |
Andrea Frittoli (andreaf) | 32d0de1 | 2015-10-09 14:43:53 +0100 | [diff] [blame] | 289 | if 'alt' in cls.credentials and not credentials.is_alt_available( |
| 290 | identity_version=identity_version): |
Andrea Frittoli | b21de6c | 2015-02-06 20:12:38 +0000 | [diff] [blame] | 291 | msg = "Missing a 2nd set of API credentials in configuration." |
| 292 | raise cls.skipException(msg) |
Andrea Frittoli (andreaf) | 4160141 | 2015-05-12 16:39:03 +0100 | [diff] [blame] | 293 | if hasattr(cls, 'identity_version'): |
| 294 | if cls.identity_version == 'v2': |
| 295 | if not CONF.identity_feature_enabled.api_v2: |
| 296 | raise cls.skipException("Identity api v2 is not enabled") |
| 297 | elif cls.identity_version == 'v3': |
| 298 | if not CONF.identity_feature_enabled.api_v3: |
| 299 | raise cls.skipException("Identity api v3 is not enabled") |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 300 | |
| 301 | @classmethod |
| 302 | def setup_credentials(cls): |
edannon | 6cc6fbc | 2016-05-03 11:56:12 +0300 | [diff] [blame] | 303 | """Allocate credentials and create the client managers from them. |
Ken'ichi Ohmichi | 2e2ee19 | 2015-11-19 09:48:27 +0000 | [diff] [blame] | 304 | |
Andrea Frittoli | a6c885a | 2017-08-23 19:37:50 +0100 | [diff] [blame] | 305 | `setup_credentials` looks for the content of the `credentials` |
| 306 | attribute in the test class. If the value is a non-empty collection, |
| 307 | a credentials provider is setup, and credentials are provisioned or |
| 308 | allocated based on the content of the collection. Every set of |
| 309 | credentials is associated to an object of type `cls.client_manager`. |
| 310 | The client manager is accessible by tests via class attribute |
| 311 | `os_[type]`: |
edannon | 6cc6fbc | 2016-05-03 11:56:12 +0300 | [diff] [blame] | 312 | |
Andrea Frittoli | a6c885a | 2017-08-23 19:37:50 +0100 | [diff] [blame] | 313 | Valid values in `credentials` are: |
| 314 | - 'primary': |
| 315 | A normal user is provisioned. |
| 316 | It can be used only once. Multiple entries will be ignored. |
| 317 | Clients are available at os_primary. |
| 318 | - 'alt': |
| 319 | A normal user other than 'primary' is provisioned. |
| 320 | It can be used only once. Multiple entries will be ignored. |
| 321 | Clients are available at os_alt. |
| 322 | - 'admin': |
| 323 | An admin user is provisioned. |
| 324 | It can be used only once. Multiple entries will be ignored. |
| 325 | Clients are available at os_admin. |
| 326 | - A list in the format ['any_label', 'role1', ... , 'roleN']: |
| 327 | A client with roles <list>[1:] is provisioned. |
| 328 | It can be used multiple times, with unique labels. |
| 329 | Clients are available at os_roles_<list>[0]. |
| 330 | |
| 331 | By default network resources are allocated (in case of dynamic |
| 332 | credentials). Tests that do not need network or that require a |
| 333 | custom network setup must specify which network resources shall |
| 334 | be provisioned using the `set_network_resources()` method (note |
| 335 | that it must be invoked before the `setup_credentials` is |
| 336 | invoked on super). |
| 337 | |
| 338 | Example:: |
| 339 | |
| 340 | class TestWithCredentials(test.BaseTestCase): |
| 341 | |
| 342 | credentials = ['primary', 'admin', |
| 343 | ['special', 'special_role1']] |
| 344 | |
| 345 | @classmethod |
| 346 | def setup_credentials(cls): |
| 347 | # set_network_resources must be called first |
| 348 | cls.set_network_resources(network=True) |
| 349 | super(TestWithCredentials, cls).setup_credentials() |
| 350 | |
| 351 | @classmethod |
| 352 | def setup_clients(cls): |
| 353 | cls.servers = cls.os_primary.compute.ServersClient() |
| 354 | cls.admin_servers = cls.os_admin.compute.ServersClient() |
| 355 | # certain API calls may require a user with a specific |
| 356 | # role assigned. In this example `special_role1` is |
| 357 | # assigned to the user in `cls.os_roles_special`. |
| 358 | cls.special_servers = ( |
| 359 | cls.os_roles_special.compute.ServersClient()) |
| 360 | |
| 361 | def test_special_servers(self): |
| 362 | # Do something with servers |
| 363 | pass |
Andrea Frittoli | b21de6c | 2015-02-06 20:12:38 +0000 | [diff] [blame] | 364 | """ |
Andrea Frittoli | bcbf1af1 | 2017-08-14 11:53:35 +0100 | [diff] [blame] | 365 | cls.__setup_credentials_called = True |
Andrea Frittoli | b21de6c | 2015-02-06 20:12:38 +0000 | [diff] [blame] | 366 | for credentials_type in cls.credentials: |
| 367 | # This may raise an exception in case credentials are not available |
| 368 | # In that case we want to let the exception through and the test |
| 369 | # fail accordingly |
Andrea Frittoli (andreaf) | 825b2d3 | 2015-04-08 20:58:01 +0100 | [diff] [blame] | 370 | if isinstance(credentials_type, six.string_types): |
| 371 | manager = cls.get_client_manager( |
| 372 | credential_type=credentials_type) |
| 373 | setattr(cls, 'os_%s' % credentials_type, manager) |
Jordan Pittier | 8160d31 | 2017-04-18 11:52:23 +0200 | [diff] [blame] | 374 | # NOTE(jordanP): Tempest should use os_primary, os_admin |
| 375 | # and os_alt throughout its code base but we keep the aliases |
| 376 | # around for a while for Tempest plugins. Aliases should be |
| 377 | # removed eventually. |
Andrea Frittoli (andreaf) | 825b2d3 | 2015-04-08 20:58:01 +0100 | [diff] [blame] | 378 | # Setup some common aliases |
Andrea Frittoli (andreaf) | 825b2d3 | 2015-04-08 20:58:01 +0100 | [diff] [blame] | 379 | if credentials_type == 'primary': |
Jordan Pittier | 8160d31 | 2017-04-18 11:52:23 +0200 | [diff] [blame] | 380 | cls.os = debtcollector.moves.moved_read_only_property( |
| 381 | 'os', 'os_primary', version='Pike', |
Jakub Libosvar | 7835ca1 | 2017-05-04 16:44:23 +0200 | [diff] [blame] | 382 | removal_version='Queens') |
Jordan Pittier | 8160d31 | 2017-04-18 11:52:23 +0200 | [diff] [blame] | 383 | cls.manager =\ |
| 384 | debtcollector.moves.moved_read_only_property( |
| 385 | 'manager', 'os_primary', version='Pike', |
Jakub Libosvar | 7835ca1 | 2017-05-04 16:44:23 +0200 | [diff] [blame] | 386 | removal_version='Queens') |
Andrea Frittoli (andreaf) | 825b2d3 | 2015-04-08 20:58:01 +0100 | [diff] [blame] | 387 | if credentials_type == 'admin': |
Jordan Pittier | 8160d31 | 2017-04-18 11:52:23 +0200 | [diff] [blame] | 388 | cls.os_adm = debtcollector.moves.moved_read_only_property( |
| 389 | 'os_adm', 'os_admin', version='Pike', |
Jakub Libosvar | 7835ca1 | 2017-05-04 16:44:23 +0200 | [diff] [blame] | 390 | removal_version='Queens') |
Jordan Pittier | 8160d31 | 2017-04-18 11:52:23 +0200 | [diff] [blame] | 391 | cls.admin_manager =\ |
| 392 | debtcollector.moves.moved_read_only_property( |
| 393 | 'admin_manager', 'os_admin', version='Pike', |
Jakub Libosvar | 7835ca1 | 2017-05-04 16:44:23 +0200 | [diff] [blame] | 394 | removal_version='Queens') |
Andrea Frittoli (andreaf) | 825b2d3 | 2015-04-08 20:58:01 +0100 | [diff] [blame] | 395 | if credentials_type == 'alt': |
Jordan Pittier | 8160d31 | 2017-04-18 11:52:23 +0200 | [diff] [blame] | 396 | cls.alt_manager =\ |
| 397 | debtcollector.moves.moved_read_only_property( |
| 398 | 'alt_manager', 'os_alt', version='Pike', |
Jakub Libosvar | 7835ca1 | 2017-05-04 16:44:23 +0200 | [diff] [blame] | 399 | removal_version='Queens') |
Andrea Frittoli (andreaf) | 825b2d3 | 2015-04-08 20:58:01 +0100 | [diff] [blame] | 400 | elif isinstance(credentials_type, list): |
| 401 | manager = cls.get_client_manager(roles=credentials_type[1:], |
| 402 | force_new=True) |
| 403 | setattr(cls, 'os_roles_%s' % credentials_type[0], manager) |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 404 | |
| 405 | @classmethod |
| 406 | def setup_clients(cls): |
Andrea Frittoli | 9958367 | 2017-08-24 15:39:20 +0100 | [diff] [blame] | 407 | """Create aliases to the clients in the client managers. |
| 408 | |
| 409 | `setup_clients` is invoked after the credential provisioning step. |
| 410 | Client manager objects are available to tests already. The purpose |
| 411 | of this helper is to setup shortcuts to specific clients that are |
| 412 | useful for the tests implemented in the test class. |
| 413 | |
| 414 | Its purpose is mostly for code readability, however it should be used |
| 415 | carefully to avoid doing exactly the opposite, i.e. making the code |
| 416 | unreadable and hard to debug. If aliases are defined in a super class |
| 417 | it won't be obvious what they refer to, so it's good practice to define |
| 418 | all aliases used in the class. Aliases are meant to be shortcuts to |
| 419 | be used in tests, not shortcuts to avoid helper method attributes. |
| 420 | If an helper method starts relying on a client alias and a subclass |
| 421 | overrides that alias, it will become rather difficult to understand |
| 422 | what the helper method actually does. |
| 423 | |
| 424 | Example:: |
| 425 | |
| 426 | class TestDoneItRight(test.BaseTestCase): |
| 427 | |
| 428 | credentials = ['primary', 'alt'] |
| 429 | |
| 430 | @classmethod |
| 431 | def setup_clients(cls): |
| 432 | super(TestDoneItRight, cls).setup_clients() |
| 433 | cls.servers = cls.os_primary.ServersClient() |
| 434 | cls.servers_alt = cls.os_alt.ServersClient() |
| 435 | |
| 436 | def _a_good_helper(self, clients): |
| 437 | # Some complex logic we're going to use many times |
| 438 | servers = clients.ServersClient() |
| 439 | vm = servers.create_server(...) |
| 440 | |
| 441 | def delete_server(): |
| 442 | test_utils.call_and_ignore_notfound_exc( |
| 443 | servers.delete_server, vm['id']) |
| 444 | |
| 445 | self.addCleanup(self.delete_server) |
| 446 | return vm |
| 447 | |
| 448 | def test_with_servers(self): |
| 449 | vm = self._a_good_helper(os.primary) |
| 450 | vm_alt = self._a_good_helper(os.alt) |
| 451 | cls.servers.show_server(vm['id']) |
| 452 | cls.servers_alt.show_server(vm_alt['id']) |
| 453 | """ |
Andrea Frittoli | 73ee247 | 2014-09-15 12:31:53 +0100 | [diff] [blame] | 454 | pass |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 455 | |
Emily Hugenbruch | 5bd4cbf | 2014-12-17 21:38:38 +0000 | [diff] [blame] | 456 | @classmethod |
| 457 | def resource_setup(cls): |
Andrea Frittoli | 3be5748 | 2017-08-25 22:41:26 +0100 | [diff] [blame] | 458 | """Class level resource setup for test cases. |
| 459 | |
| 460 | `resource_setup` is invoked once all credentials (and related network |
| 461 | resources have been provisioned and after client aliases - if any - |
| 462 | have been defined. |
| 463 | |
| 464 | The use case for `resource_setup` is test optimization: provisioning |
| 465 | of project-specific "expensive" resources that are not dirtied by tests |
| 466 | and can thus safely be re-used by multiple tests. |
| 467 | |
| 468 | System wide resources shared by all tests could instead be provisioned |
| 469 | only once, before the test run. |
| 470 | |
| 471 | Resources provisioned here must be cleaned up during |
| 472 | `resource_cleanup`. This is best achieved by scheduling a cleanup via |
| 473 | `addClassResourceCleanup`. |
| 474 | |
| 475 | Some test resources have an asynchronous delete process. It's best |
| 476 | practice for them to schedule a wait for delete via |
| 477 | `addClassResourceCleanup` to avoid having resources in process of |
| 478 | deletion when we reach the credentials cleanup step. |
| 479 | |
| 480 | Example:: |
| 481 | |
| 482 | @classmethod |
| 483 | def resource_setup(cls): |
| 484 | super(MyTest, cls).resource_setup() |
| 485 | servers = cls.os_primary.compute.ServersClient() |
| 486 | # Schedule delete and wait so that we can first delete the |
| 487 | # two servers and then wait for both to delete |
| 488 | # Create server 1 |
| 489 | cls.shared_server = servers.create_server() |
| 490 | # Create server 2. If something goes wrong we schedule cleanup |
| 491 | # of server 1 anyways. |
| 492 | try: |
| 493 | cls.shared_server2 = servers.create_server() |
| 494 | # Wait server 2 |
| 495 | cls.addClassResourceCleanup( |
| 496 | waiters.wait_for_server_termination, |
| 497 | servers, cls.shared_server2['id'], |
| 498 | ignore_error=False) |
| 499 | finally: |
| 500 | # Wait server 1 |
| 501 | cls.addClassResourceCleanup( |
| 502 | waiters.wait_for_server_termination, |
| 503 | servers, cls.shared_server['id'], |
| 504 | ignore_error=False) |
| 505 | # Delete server 1 |
| 506 | cls.addClassResourceCleanup( |
| 507 | test_utils.call_and_ignore_notfound_exc, |
| 508 | servers.delete_server, |
| 509 | cls.shared_server['id']) |
| 510 | # Delete server 2 (if it was created) |
| 511 | if hasattr(cls, 'shared_server2'): |
| 512 | cls.addClassResourceCleanup( |
| 513 | test_utils.call_and_ignore_notfound_exc, |
| 514 | servers.delete_server, |
| 515 | cls.shared_server2['id']) |
| 516 | """ |
Andrea Frittoli | 9f416dd | 2017-08-10 15:38:00 +0100 | [diff] [blame] | 517 | pass |
Emily Hugenbruch | 5bd4cbf | 2014-12-17 21:38:38 +0000 | [diff] [blame] | 518 | |
| 519 | @classmethod |
| 520 | def resource_cleanup(cls): |
| 521 | """Class level resource cleanup for test cases. |
Ken'ichi Ohmichi | 2e2ee19 | 2015-11-19 09:48:27 +0000 | [diff] [blame] | 522 | |
Andrea Frittoli | 9f416dd | 2017-08-10 15:38:00 +0100 | [diff] [blame] | 523 | Resource cleanup processes the stack of cleanups produced by |
Andrea Frittoli | 3be5748 | 2017-08-25 22:41:26 +0100 | [diff] [blame] | 524 | `addClassResourceCleanup` and then cleans up validation resources |
| 525 | if any were provisioned. |
| 526 | |
| 527 | All cleanups are processed whatever the outcome. Exceptions are |
| 528 | accumulated and re-raised as a `MultipleExceptions` at the end. |
| 529 | |
| 530 | In most cases test cases won't need to override `resource_cleanup`, |
| 531 | but if they do they must invoke `resource_cleanup` on super. |
| 532 | |
| 533 | Example:: |
| 534 | |
| 535 | class TestWithReallyComplexCleanup(test.BaseTestCase): |
| 536 | |
| 537 | @classmethod |
| 538 | def resource_setup(cls): |
| 539 | # provision resource A |
| 540 | cls.addClassResourceCleanup(delete_resource, A) |
| 541 | # provision resource B |
| 542 | cls.addClassResourceCleanup(delete_resource, B) |
| 543 | |
| 544 | @classmethod |
| 545 | def resource_cleanup(cls): |
| 546 | # It's possible to override resource_cleanup but in most |
| 547 | # cases it shouldn't be required. Nothing that may fail |
| 548 | # should be executed before the call to super since it |
| 549 | # might cause resource leak in case of error. |
| 550 | super(TestWithReallyComplexCleanup, cls).resource_cleanup() |
| 551 | # At this point test credentials are still available but |
| 552 | # anything from the cleanup stack has been already deleted. |
Emily Hugenbruch | 5bd4cbf | 2014-12-17 21:38:38 +0000 | [diff] [blame] | 553 | """ |
Andrea Frittoli (andreaf) | 08e42d4 | 2017-09-07 17:09:13 +0100 | [diff] [blame] | 554 | cls.__resource_cleanup_called = True |
Andrea Frittoli | 3be5748 | 2017-08-25 22:41:26 +0100 | [diff] [blame] | 555 | cleanup_errors = [] |
| 556 | while cls._class_cleanups: |
| 557 | try: |
| 558 | fn, args, kwargs = cls._class_cleanups.pop() |
| 559 | fn(*args, **kwargs) |
| 560 | except Exception: |
| 561 | cleanup_errors.append(sys.exc_info()) |
Andrea Frittoli | 3be5748 | 2017-08-25 22:41:26 +0100 | [diff] [blame] | 562 | if cleanup_errors: |
| 563 | raise testtools.MultipleExceptions(*cleanup_errors) |
| 564 | |
| 565 | @classmethod |
| 566 | def addClassResourceCleanup(cls, fn, *arguments, **keywordArguments): |
| 567 | """Add a cleanup function to be called during resource_cleanup. |
| 568 | |
| 569 | Functions added with addClassResourceCleanup will be called in reverse |
| 570 | order of adding at the beginning of resource_cleanup, before any |
| 571 | credential, networking or validation resources cleanup is processed. |
| 572 | |
| 573 | If a function added with addClassResourceCleanup raises an exception, |
| 574 | the error will be recorded as a test error, and the next cleanup will |
| 575 | then be run. |
| 576 | |
| 577 | Cleanup functions are always called during the test class tearDown |
| 578 | fixture, even if an exception occured during setUp or tearDown. |
| 579 | """ |
| 580 | cls._class_cleanups.append((fn, arguments, keywordArguments)) |
Emily Hugenbruch | 5bd4cbf | 2014-12-17 21:38:38 +0000 | [diff] [blame] | 581 | |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 582 | def setUp(self): |
| 583 | super(BaseTestCase, self).setUp() |
Andrea Frittoli | ba712ac | 2017-09-13 16:54:47 -0600 | [diff] [blame^] | 584 | if not self.__setupclass_called: |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 585 | raise RuntimeError("setUpClass does not calls the super's" |
| 586 | "setUpClass in the " |
| 587 | + self.__class__.__name__) |
| 588 | at_exit_set.add(self.__class__) |
Matthew Treinish | 78561ad | 2013-07-26 11:41:56 -0400 | [diff] [blame] | 589 | test_timeout = os.environ.get('OS_TEST_TIMEOUT', 0) |
| 590 | try: |
Sean Dague | 02620fd | 2016-03-02 15:52:51 -0500 | [diff] [blame] | 591 | test_timeout = int(test_timeout) * self.TIMEOUT_SCALING_FACTOR |
Matthew Treinish | 78561ad | 2013-07-26 11:41:56 -0400 | [diff] [blame] | 592 | except ValueError: |
| 593 | test_timeout = 0 |
| 594 | if test_timeout > 0: |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 595 | self.useFixture(fixtures.Timeout(test_timeout, gentle=True)) |
Matthew Treinish | 78561ad | 2013-07-26 11:41:56 -0400 | [diff] [blame] | 596 | |
| 597 | if (os.environ.get('OS_STDOUT_CAPTURE') == 'True' or |
| 598 | os.environ.get('OS_STDOUT_CAPTURE') == '1'): |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 599 | stdout = self.useFixture(fixtures.StringStream('stdout')).stream |
| 600 | self.useFixture(fixtures.MonkeyPatch('sys.stdout', stdout)) |
Matthew Treinish | 78561ad | 2013-07-26 11:41:56 -0400 | [diff] [blame] | 601 | if (os.environ.get('OS_STDERR_CAPTURE') == 'True' or |
| 602 | os.environ.get('OS_STDERR_CAPTURE') == '1'): |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 603 | stderr = self.useFixture(fixtures.StringStream('stderr')).stream |
| 604 | self.useFixture(fixtures.MonkeyPatch('sys.stderr', stderr)) |
Attila Fazekas | 3138807 | 2013-08-15 08:58:07 +0200 | [diff] [blame] | 605 | if (os.environ.get('OS_LOG_CAPTURE') != 'False' and |
| 606 | os.environ.get('OS_LOG_CAPTURE') != '0'): |
Attila Fazekas | 3138807 | 2013-08-15 08:58:07 +0200 | [diff] [blame] | 607 | self.useFixture(fixtures.LoggerFixture(nuke_handlers=False, |
Sean Dague | 2ef32ac | 2014-06-09 11:32:23 -0400 | [diff] [blame] | 608 | format=self.log_format, |
Attila Fazekas | 90445be | 2013-10-24 16:46:03 +0200 | [diff] [blame] | 609 | level=None)) |
Matthew Treinish | 78561ad | 2013-07-26 11:41:56 -0400 | [diff] [blame] | 610 | |
Andrea Frittoli (andreaf) | 1f34241 | 2015-05-12 16:37:19 +0100 | [diff] [blame] | 611 | @property |
| 612 | def credentials_provider(self): |
| 613 | return self._get_credentials_provider() |
| 614 | |
Jamie Lennox | 1535017 | 2015-08-17 10:54:25 +1000 | [diff] [blame] | 615 | @property |
| 616 | def identity_utils(self): |
| 617 | """A client that abstracts v2 and v3 identity operations. |
| 618 | |
| 619 | This can be used for creating and tearing down projects in tests. It |
| 620 | should not be used for testing identity features. |
| 621 | """ |
| 622 | if CONF.identity.auth_version == 'v2': |
| 623 | client = self.os_admin.identity_client |
Daniel Mellado | 7aea534 | 2016-02-09 09:10:12 +0000 | [diff] [blame] | 624 | users_client = self.os_admin.users_client |
Daniel Mellado | b04da90 | 2015-11-20 17:43:12 +0100 | [diff] [blame] | 625 | project_client = self.os_admin.tenants_client |
Daniel Mellado | 6b16b92 | 2015-12-07 12:43:08 +0000 | [diff] [blame] | 626 | roles_client = self.os_admin.roles_client |
Daniel Mellado | 91a26b6 | 2016-02-11 11:13:04 +0000 | [diff] [blame] | 627 | domains_client = None |
Jamie Lennox | 1535017 | 2015-08-17 10:54:25 +1000 | [diff] [blame] | 628 | else: |
| 629 | client = self.os_admin.identity_v3_client |
Daniel Mellado | 7aea534 | 2016-02-09 09:10:12 +0000 | [diff] [blame] | 630 | users_client = self.os_admin.users_v3_client |
Arx Cruz | 24bcb88 | 2016-02-10 15:20:16 +0100 | [diff] [blame] | 631 | project_client = self.os_admin.projects_client |
| 632 | roles_client = self.os_admin.roles_v3_client |
Daniel Mellado | 91a26b6 | 2016-02-11 11:13:04 +0000 | [diff] [blame] | 633 | domains_client = self.os_admin.domains_client |
Jamie Lennox | 1535017 | 2015-08-17 10:54:25 +1000 | [diff] [blame] | 634 | |
| 635 | try: |
| 636 | domain = client.auth_provider.credentials.project_domain_name |
| 637 | except AttributeError: |
| 638 | domain = 'Default' |
| 639 | |
Daniel Mellado | b04da90 | 2015-11-20 17:43:12 +0100 | [diff] [blame] | 640 | return cred_client.get_creds_client(client, project_client, |
Daniel Mellado | 82c83a5 | 2015-12-09 15:16:49 +0000 | [diff] [blame] | 641 | users_client, |
Daniel Mellado | 7aea534 | 2016-02-09 09:10:12 +0000 | [diff] [blame] | 642 | roles_client, |
Daniel Mellado | 91a26b6 | 2016-02-11 11:13:04 +0000 | [diff] [blame] | 643 | domains_client, |
Daniel Mellado | b04da90 | 2015-11-20 17:43:12 +0100 | [diff] [blame] | 644 | project_domain_name=domain) |
Jamie Lennox | 1535017 | 2015-08-17 10:54:25 +1000 | [diff] [blame] | 645 | |
Andrea Frittoli (andreaf) | 1f34241 | 2015-05-12 16:37:19 +0100 | [diff] [blame] | 646 | @classmethod |
Andrea Frittoli (andreaf) | 32d0de1 | 2015-10-09 14:43:53 +0100 | [diff] [blame] | 647 | def get_identity_version(cls): |
| 648 | """Returns the identity version used by the test class""" |
| 649 | identity_version = getattr(cls, 'identity_version', None) |
| 650 | return identity_version or CONF.identity.auth_version |
| 651 | |
| 652 | @classmethod |
Andrea Frittoli (andreaf) | 1f34241 | 2015-05-12 16:37:19 +0100 | [diff] [blame] | 653 | def _get_credentials_provider(cls): |
| 654 | """Returns a credentials provider |
| 655 | |
| 656 | If no credential provider exists yet creates one. |
Andrea Frittoli | 9e01dbb | 2017-04-20 15:28:30 +0100 | [diff] [blame] | 657 | It always use the configuration value from identity.auth_version, |
| 658 | since we always want to provision accounts with the current version |
| 659 | of the identity API. |
Andrea Frittoli (andreaf) | 1f34241 | 2015-05-12 16:37:19 +0100 | [diff] [blame] | 660 | """ |
| 661 | if (not hasattr(cls, '_creds_provider') or not cls._creds_provider or |
| 662 | not cls._creds_provider.name == cls.__name__): |
| 663 | force_tenant_isolation = getattr(cls, 'force_tenant_isolation', |
| 664 | False) |
Andrea Frittoli (andreaf) | 1f34241 | 2015-05-12 16:37:19 +0100 | [diff] [blame] | 665 | |
Andrea Frittoli (andreaf) | 17209bb | 2015-05-22 10:16:57 -0700 | [diff] [blame] | 666 | cls._creds_provider = credentials.get_credentials_provider( |
Andrea Frittoli | bcbf1af1 | 2017-08-14 11:53:35 +0100 | [diff] [blame] | 667 | name=cls.__name__, network_resources=cls._network_resources, |
Andrea Frittoli | 9e01dbb | 2017-04-20 15:28:30 +0100 | [diff] [blame] | 668 | force_tenant_isolation=force_tenant_isolation) |
Andrea Frittoli (andreaf) | 1f34241 | 2015-05-12 16:37:19 +0100 | [diff] [blame] | 669 | return cls._creds_provider |
| 670 | |
Matthew Treinish | 3e04685 | 2013-07-23 16:00:24 -0400 | [diff] [blame] | 671 | @classmethod |
Andrea Frittoli (andreaf) | 4160141 | 2015-05-12 16:39:03 +0100 | [diff] [blame] | 672 | def get_client_manager(cls, credential_type=None, roles=None, |
| 673 | force_new=None): |
Andrea Frittoli (andreaf) | 737fac9 | 2015-05-12 16:14:35 +0100 | [diff] [blame] | 674 | """Returns an OpenStack client manager |
| 675 | |
| 676 | Returns an OpenStack client manager based on either credential_type |
| 677 | or a list of roles. If neither is specified, it defaults to |
| 678 | credential_type 'primary' |
Andrea Frittoli (andreaf) | 737fac9 | 2015-05-12 16:14:35 +0100 | [diff] [blame] | 679 | :param credential_type: string - primary, alt or admin |
| 680 | :param roles: list of roles |
| 681 | |
lei zhang | dd552b2 | 2015-11-25 20:41:48 +0800 | [diff] [blame] | 682 | :returns: the created client manager |
Andrea Frittoli (andreaf) | 737fac9 | 2015-05-12 16:14:35 +0100 | [diff] [blame] | 683 | :raises skipException: if the requested credentials are not available |
Ryan Hsu | 6c4bb3d | 2013-10-21 21:22:50 -0700 | [diff] [blame] | 684 | """ |
Andrea Frittoli (andreaf) | 737fac9 | 2015-05-12 16:14:35 +0100 | [diff] [blame] | 685 | if all([roles, credential_type]): |
| 686 | msg = "Cannot get credentials by type and roles at the same time" |
| 687 | raise ValueError(msg) |
| 688 | if not any([roles, credential_type]): |
| 689 | credential_type = 'primary' |
Andrea Frittoli (andreaf) | 1f34241 | 2015-05-12 16:37:19 +0100 | [diff] [blame] | 690 | cred_provider = cls._get_credentials_provider() |
Andrea Frittoli (andreaf) | 737fac9 | 2015-05-12 16:14:35 +0100 | [diff] [blame] | 691 | if roles: |
| 692 | for role in roles: |
Andrea Frittoli (andreaf) | 1f34241 | 2015-05-12 16:37:19 +0100 | [diff] [blame] | 693 | if not cred_provider.is_role_available(role): |
Andrea Frittoli (andreaf) | 737fac9 | 2015-05-12 16:14:35 +0100 | [diff] [blame] | 694 | skip_msg = ( |
| 695 | "%s skipped because the configured credential provider" |
| 696 | " is not able to provide credentials with the %s role " |
| 697 | "assigned." % (cls.__name__, role)) |
| 698 | raise cls.skipException(skip_msg) |
| 699 | params = dict(roles=roles) |
| 700 | if force_new is not None: |
| 701 | params.update(force_new=force_new) |
Andrea Frittoli (andreaf) | 1f34241 | 2015-05-12 16:37:19 +0100 | [diff] [blame] | 702 | creds = cred_provider.get_creds_by_roles(**params) |
Andrea Frittoli | b21de6c | 2015-02-06 20:12:38 +0000 | [diff] [blame] | 703 | else: |
Andrea Frittoli (andreaf) | 737fac9 | 2015-05-12 16:14:35 +0100 | [diff] [blame] | 704 | credentials_method = 'get_%s_creds' % credential_type |
Andrea Frittoli (andreaf) | 1f34241 | 2015-05-12 16:37:19 +0100 | [diff] [blame] | 705 | if hasattr(cred_provider, credentials_method): |
| 706 | creds = getattr(cred_provider, credentials_method)() |
Andrea Frittoli (andreaf) | 737fac9 | 2015-05-12 16:14:35 +0100 | [diff] [blame] | 707 | else: |
Andrea Frittoli (andreaf) | af4f7cf | 2016-06-13 15:12:26 +0100 | [diff] [blame] | 708 | raise lib_exc.InvalidCredentials( |
Andrea Frittoli (andreaf) | 737fac9 | 2015-05-12 16:14:35 +0100 | [diff] [blame] | 709 | "Invalid credentials type %s" % credential_type) |
Jordan Pittier | e4be907 | 2017-01-04 19:17:35 +0100 | [diff] [blame] | 710 | manager = cls.client_manager(credentials=creds.credentials) |
Andrea Frittoli | 7322467 | 2016-12-09 21:08:19 +0000 | [diff] [blame] | 711 | # NOTE(andreaf) Ensure credentials have user and project id fields. |
| 712 | # It may not be the case when using pre-provisioned credentials. |
| 713 | manager.auth_provider.set_auth() |
| 714 | return manager |
Ryan Hsu | 6c4bb3d | 2013-10-21 21:22:50 -0700 | [diff] [blame] | 715 | |
| 716 | @classmethod |
Andrea Frittoli (andreaf) | 17209bb | 2015-05-22 10:16:57 -0700 | [diff] [blame] | 717 | def clear_credentials(cls): |
Ken'ichi Ohmichi | 2e2ee19 | 2015-11-19 09:48:27 +0000 | [diff] [blame] | 718 | """Clears creds if set""" |
Attila Fazekas | 5b0d926 | 2015-05-20 10:17:39 +0200 | [diff] [blame] | 719 | if hasattr(cls, '_creds_provider'): |
Andrea Frittoli (andreaf) | 17209bb | 2015-05-22 10:16:57 -0700 | [diff] [blame] | 720 | cls._creds_provider.clear_creds() |
Ryan Hsu | 6c4bb3d | 2013-10-21 21:22:50 -0700 | [diff] [blame] | 721 | |
Andrea Frittoli | 9f416dd | 2017-08-10 15:38:00 +0100 | [diff] [blame] | 722 | @staticmethod |
| 723 | def _validation_resources_params_from_conf(): |
| 724 | return dict( |
| 725 | keypair=(CONF.validation.auth_method.lower() == "keypair"), |
| 726 | floating_ip=(CONF.validation.connect_method.lower() == "floating"), |
| 727 | security_group=CONF.validation.security_group, |
| 728 | security_group_rules=CONF.validation.security_group_rules, |
| 729 | use_neutron=CONF.service_available.neutron, |
| 730 | ethertype='IPv' + str(CONF.validation.ip_version_for_ssh), |
| 731 | floating_network_id=CONF.network.public_network_id, |
| 732 | floating_network_name=CONF.network.floating_network_name) |
| 733 | |
Ryan Hsu | 6c4bb3d | 2013-10-21 21:22:50 -0700 | [diff] [blame] | 734 | @classmethod |
Andrea Frittoli | 9f416dd | 2017-08-10 15:38:00 +0100 | [diff] [blame] | 735 | def get_class_validation_resources(cls, os_clients): |
| 736 | """Provision validation resources according to configuration |
Ken'ichi Ohmichi | 2e2ee19 | 2015-11-19 09:48:27 +0000 | [diff] [blame] | 737 | |
Andrea Frittoli | 9f416dd | 2017-08-10 15:38:00 +0100 | [diff] [blame] | 738 | This is a wrapper around `create_validation_resources` from |
| 739 | `tempest.common.validation_resources` that passes parameters from |
| 740 | Tempest configuration. Only one instance of class level |
| 741 | validation resources is managed by the helper, so If resources |
| 742 | were already provisioned before, existing ones will be returned. |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 743 | |
Andrea Frittoli | 9f416dd | 2017-08-10 15:38:00 +0100 | [diff] [blame] | 744 | Resources are returned as a dictionary. They are also scheduled for |
| 745 | automatic cleanup during class teardown using |
| 746 | `addClassResourcesCleanup`. |
| 747 | |
| 748 | If `CONF.validation.run_validation` is False no resource will be |
| 749 | provisioned at all. |
| 750 | |
| 751 | @param os_clients: Clients to be used to provision the resources. |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 752 | """ |
Matthew Treinish | e5cca00 | 2015-05-11 15:36:50 -0400 | [diff] [blame] | 753 | if not CONF.validation.run_validation: |
| 754 | return |
Jordan Pittier | 79cd182 | 2016-12-08 17:20:35 +0100 | [diff] [blame] | 755 | |
Andrea Frittoli | 9f416dd | 2017-08-10 15:38:00 +0100 | [diff] [blame] | 756 | if os_clients in cls._validation_resources: |
| 757 | return cls._validation_resources[os_clients] |
Jordan Pittier | 79cd182 | 2016-12-08 17:20:35 +0100 | [diff] [blame] | 758 | |
Andrea Frittoli | 9f416dd | 2017-08-10 15:38:00 +0100 | [diff] [blame] | 759 | if (CONF.validation.ip_version_for_ssh not in (4, 6) and |
| 760 | CONF.service_available.neutron): |
| 761 | msg = "Invalid IP version %s in ip_version_for_ssh. Use 4 or 6" |
| 762 | raise lib_exc.InvalidConfiguration( |
| 763 | msg % CONF.validation.ip_version_for_ssh) |
Jordan Pittier | 79cd182 | 2016-12-08 17:20:35 +0100 | [diff] [blame] | 764 | |
Andrea Frittoli | 9f416dd | 2017-08-10 15:38:00 +0100 | [diff] [blame] | 765 | resources = vr.create_validation_resources( |
| 766 | os_clients, |
| 767 | **cls._validation_resources_params_from_conf()) |
Jordan Pittier | 79cd182 | 2016-12-08 17:20:35 +0100 | [diff] [blame] | 768 | |
Andrea Frittoli | 9f416dd | 2017-08-10 15:38:00 +0100 | [diff] [blame] | 769 | cls.addClassResourceCleanup( |
| 770 | vr.clear_validation_resources, os_clients, |
| 771 | use_neutron=CONF.service_available.neutron, |
| 772 | **resources) |
| 773 | cls._validation_resources[os_clients] = resources |
| 774 | return resources |
Brandon Palm | c6cc91d | 2015-08-19 13:20:21 -0500 | [diff] [blame] | 775 | |
Andrea Frittoli | 9f416dd | 2017-08-10 15:38:00 +0100 | [diff] [blame] | 776 | def get_test_validation_resources(self, os_clients): |
| 777 | """Returns a dict of validation resources according to configuration |
| 778 | |
| 779 | Initialise a validation resources fixture based on configuration. |
| 780 | Start the fixture and returns the validation resources. |
| 781 | |
| 782 | If `CONF.validation.run_validation` is False no resource will be |
| 783 | provisioned at all. |
| 784 | |
| 785 | @param os_clients: Clients to be used to provision the resources. |
| 786 | """ |
| 787 | |
| 788 | params = {} |
| 789 | # Test will try to use the fixture, so for this to be useful |
| 790 | # we must return a fixture. If validation is disabled though |
| 791 | # we don't need to provision anything, which is the default |
| 792 | # behavior for the fixture. |
| 793 | if CONF.validation.run_validation: |
| 794 | params = self._validation_resources_params_from_conf() |
| 795 | |
| 796 | validation = self.useFixture( |
| 797 | vr.ValidationResourcesFixture(os_clients, **params)) |
| 798 | return validation.resources |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 799 | |
| 800 | @classmethod |
Andrea Frittoli | 7d5ed59 | 2015-02-10 01:10:23 +0000 | [diff] [blame] | 801 | def set_network_resources(cls, network=False, router=False, subnet=False, |
Matthew Treinish | 9f756a0 | 2014-01-15 10:26:07 -0500 | [diff] [blame] | 802 | dhcp=False): |
| 803 | """Specify which network resources should be created |
| 804 | |
Andrea Frittoli | bcbf1af1 | 2017-08-14 11:53:35 +0100 | [diff] [blame] | 805 | The dynamic credentials provider by default provisions network |
| 806 | resources for each user/project that is provisioned. This behavior |
| 807 | can be altered using this method, which allows tests to define which |
| 808 | specific network resources to be provisioned - none if no parameter |
| 809 | is specified. |
| 810 | |
Andrea Frittoli | a6c885a | 2017-08-23 19:37:50 +0100 | [diff] [blame] | 811 | This method is designed so that only the network resources set on the |
| 812 | leaf class are honoured. |
| 813 | |
Andrea Frittoli | bcbf1af1 | 2017-08-14 11:53:35 +0100 | [diff] [blame] | 814 | Credentials are provisioned as part of the class setup fixture, |
| 815 | during the `setup_credentials` step. For this to be effective this |
| 816 | helper must be invoked before super's `setup_credentials` is executed. |
| 817 | |
Matthew Treinish | 9f756a0 | 2014-01-15 10:26:07 -0500 | [diff] [blame] | 818 | @param network |
| 819 | @param router |
| 820 | @param subnet |
| 821 | @param dhcp |
Andrea Frittoli | bcbf1af1 | 2017-08-14 11:53:35 +0100 | [diff] [blame] | 822 | |
| 823 | Example:: |
| 824 | |
| 825 | @classmethod |
| 826 | def setup_credentials(cls): |
| 827 | # Do not setup network resources for this test |
| 828 | cls.set_network_resources() |
| 829 | super(MyTest, cls).setup_credentials() |
Matthew Treinish | 9f756a0 | 2014-01-15 10:26:07 -0500 | [diff] [blame] | 830 | """ |
Andrea Frittoli | bcbf1af1 | 2017-08-14 11:53:35 +0100 | [diff] [blame] | 831 | # If this is invoked after the credentials are setup, it won't take |
| 832 | # any effect. To avoid this situation, fail the test in case this was |
| 833 | # invoked too late in the test lifecycle. |
| 834 | if cls.__setup_credentials_called: |
| 835 | raise RuntimeError( |
| 836 | "set_network_resources invoked after setup_credentials on the " |
| 837 | "super class has been already invoked. For " |
| 838 | "set_network_resources to have effect please invoke it before " |
| 839 | "the call to super().setup_credentials") |
| 840 | |
| 841 | # Network resources should be set only once from callers |
Salvatore Orlando | 5a33724 | 2014-01-15 22:49:22 +0000 | [diff] [blame] | 842 | # in order to ensure that even if it's called multiple times in |
| 843 | # a chain of overloaded methods, the attribute is set only |
Andrea Frittoli | bcbf1af1 | 2017-08-14 11:53:35 +0100 | [diff] [blame] | 844 | # in the leaf class. |
| 845 | if not cls._network_resources: |
| 846 | cls._network_resources = { |
Salvatore Orlando | 5a33724 | 2014-01-15 22:49:22 +0000 | [diff] [blame] | 847 | 'network': network, |
| 848 | 'router': router, |
| 849 | 'subnet': subnet, |
| 850 | 'dhcp': dhcp} |
Matthew Treinish | 9f756a0 | 2014-01-15 10:26:07 -0500 | [diff] [blame] | 851 | |
Rohan Kanade | 9ce97df | 2013-12-10 18:59:35 +0530 | [diff] [blame] | 852 | @classmethod |
Ryan Rossiter | 9228bf7 | 2016-02-25 03:06:12 +0000 | [diff] [blame] | 853 | def get_tenant_network(cls, credentials_type='primary'): |
Rohan Kanade | 9ce97df | 2013-12-10 18:59:35 +0530 | [diff] [blame] | 854 | """Get the network to be used in testing |
| 855 | |
Ryan Rossiter | 9228bf7 | 2016-02-25 03:06:12 +0000 | [diff] [blame] | 856 | :param credentials_type: The type of credentials for which to get the |
| 857 | tenant network |
| 858 | |
Rohan Kanade | 9ce97df | 2013-12-10 18:59:35 +0530 | [diff] [blame] | 859 | :return: network dict including 'id' and 'name' |
| 860 | """ |
Ryan Rossiter | 9228bf7 | 2016-02-25 03:06:12 +0000 | [diff] [blame] | 861 | # Get a manager for the given credentials_type, but at least |
| 862 | # always fall back on getting the manager for primary credentials |
| 863 | if isinstance(credentials_type, six.string_types): |
| 864 | manager = cls.get_client_manager(credential_type=credentials_type) |
| 865 | elif isinstance(credentials_type, list): |
| 866 | manager = cls.get_client_manager(roles=credentials_type[1:]) |
| 867 | else: |
| 868 | manager = cls.get_client_manager() |
| 869 | |
Andrea Frittoli (andreaf) | 17209bb | 2015-05-22 10:16:57 -0700 | [diff] [blame] | 870 | # Make sure cred_provider exists and get a network client |
Ryan Rossiter | 9228bf7 | 2016-02-25 03:06:12 +0000 | [diff] [blame] | 871 | networks_client = manager.compute_networks_client |
Andrea Frittoli (andreaf) | 1f34241 | 2015-05-12 16:37:19 +0100 | [diff] [blame] | 872 | cred_provider = cls._get_credentials_provider() |
Andrea Frittoli | 700711e | 2015-04-02 11:39:38 +0100 | [diff] [blame] | 873 | # 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] | 874 | # network configured in fixed_network_name, even if they can use it |
Andrea Frittoli | 700711e | 2015-04-02 11:39:38 +0100 | [diff] [blame] | 875 | # for their servers, so using an admin network client to validate |
| 876 | # the network name |
| 877 | if (not CONF.service_available.neutron and |
Andrea Frittoli (andreaf) | 32d0de1 | 2015-10-09 14:43:53 +0100 | [diff] [blame] | 878 | credentials.is_admin_available( |
| 879 | identity_version=cls.get_identity_version())): |
Andrea Frittoli (andreaf) | 1f34241 | 2015-05-12 16:37:19 +0100 | [diff] [blame] | 880 | admin_creds = cred_provider.get_admin_creds() |
Andrea Frittoli (andreaf) | 848c4a1 | 2016-06-09 11:09:02 +0100 | [diff] [blame] | 881 | admin_manager = clients.Manager(admin_creds.credentials) |
John Warren | 9487a18 | 2015-09-14 18:12:56 -0400 | [diff] [blame] | 882 | networks_client = admin_manager.compute_networks_client |
Andrea Frittoli (andreaf) | 940f8c6 | 2015-10-30 16:39:24 +0900 | [diff] [blame] | 883 | return fixed_network.get_tenant_network( |
| 884 | cred_provider, networks_client, CONF.compute.fixed_network_name) |
Rohan Kanade | 9ce97df | 2013-12-10 18:59:35 +0530 | [diff] [blame] | 885 | |
Andrea Frittoli | 71c71e9 | 2017-04-07 17:45:21 +0100 | [diff] [blame] | 886 | def assertEmpty(self, items, msg=None): |
| 887 | """Asserts whether a sequence or collection is empty |
Mark Maglana | 5885eb3 | 2014-02-28 10:57:34 -0800 | [diff] [blame] | 888 | |
Andrea Frittoli | 71c71e9 | 2017-04-07 17:45:21 +0100 | [diff] [blame] | 889 | :param items: sequence or collection to be tested |
| 890 | :param msg: message to be passed to the AssertionError |
| 891 | :raises AssertionError: when items is not empty |
| 892 | """ |
zhufl | 92ade4b | 2017-03-03 15:20:10 +0800 | [diff] [blame] | 893 | if msg is None: |
Andrea Frittoli | 71c71e9 | 2017-04-07 17:45:21 +0100 | [diff] [blame] | 894 | msg = "sequence or collection is not empty: %s" % items |
Masayuki Igawa | 0c0f014 | 2017-04-10 17:22:02 +0900 | [diff] [blame] | 895 | self.assertFalse(items, msg) |
Andrea Frittoli | 71c71e9 | 2017-04-07 17:45:21 +0100 | [diff] [blame] | 896 | |
| 897 | def assertNotEmpty(self, items, msg=None): |
| 898 | """Asserts whether a sequence or collection is not empty |
| 899 | |
| 900 | :param items: sequence or collection to be tested |
| 901 | :param msg: message to be passed to the AssertionError |
| 902 | :raises AssertionError: when items is empty |
| 903 | """ |
| 904 | if msg is None: |
| 905 | msg = "sequence or collection is empty." |
Masayuki Igawa | 0c0f014 | 2017-04-10 17:22:02 +0900 | [diff] [blame] | 906 | self.assertTrue(items, msg) |