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