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