ZhiQiang Fan | 39f9722 | 2013-09-20 04:49:44 +0800 | [diff] [blame] | 1 | # Copyright 2012 OpenStack Foundation |
Jay Pipes | 051075a | 2012-04-28 17:39:37 -0400 | [diff] [blame] | 2 | # All Rights Reserved. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 | # not use this file except in compliance with the License. You may obtain |
| 6 | # a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | # License for the specific language governing permissions and limitations |
| 14 | # under the License. |
| 15 | |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 16 | import atexit |
Masayuki Igawa | 80c1b9f | 2013-10-07 17:19:11 +0900 | [diff] [blame] | 17 | import functools |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 18 | import json |
Ian Wienand | 98c35f3 | 2013-07-23 20:34:23 +1000 | [diff] [blame] | 19 | import os |
Marc Koderer | b2978da | 2014-03-26 13:45:43 +0100 | [diff] [blame] | 20 | import re |
Attila Fazekas | 5394332 | 2014-02-10 16:07:34 +0100 | [diff] [blame] | 21 | import sys |
Jay Pipes | 051075a | 2012-04-28 17:39:37 -0400 | [diff] [blame] | 22 | import time |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 23 | import urllib |
| 24 | import uuid |
Jay Pipes | 051075a | 2012-04-28 17:39:37 -0400 | [diff] [blame] | 25 | |
Matthew Treinish | 78561ad | 2013-07-26 11:41:56 -0400 | [diff] [blame] | 26 | import fixtures |
Marc Koderer | 674c8fc | 2014-03-17 09:45:04 +0100 | [diff] [blame] | 27 | import testscenarios |
ivan-zhu | 1feeb38 | 2013-01-24 10:14:39 +0800 | [diff] [blame] | 28 | import testtools |
Jay Pipes | 051075a | 2012-04-28 17:39:37 -0400 | [diff] [blame] | 29 | |
Matthew Treinish | 3e04685 | 2013-07-23 16:00:24 -0400 | [diff] [blame] | 30 | from tempest import clients |
Andrea Frittoli | 8283b4e | 2014-07-17 13:28:58 +0100 | [diff] [blame] | 31 | from tempest.common import credentials |
Marc Koderer | 6ee82dc | 2014-02-17 10:26:29 +0100 | [diff] [blame] | 32 | import tempest.common.generator.valid_generator as valid |
Attila Fazekas | dc21642 | 2013-01-29 15:12:14 +0100 | [diff] [blame] | 33 | from tempest import config |
Matthew Treinish | 16c4379 | 2013-09-09 19:55:23 +0000 | [diff] [blame] | 34 | from tempest import exceptions |
Marc Koderer | 6ee82dc | 2014-02-17 10:26:29 +0100 | [diff] [blame] | 35 | from tempest.openstack.common import importutils |
Matthew Treinish | f4a9b0f | 2013-07-26 16:58:26 -0400 | [diff] [blame] | 36 | from tempest.openstack.common import log as logging |
Jay Pipes | 051075a | 2012-04-28 17:39:37 -0400 | [diff] [blame] | 37 | |
| 38 | LOG = logging.getLogger(__name__) |
| 39 | |
Sean Dague | 86bd842 | 2013-12-20 09:56:44 -0500 | [diff] [blame] | 40 | CONF = config.CONF |
| 41 | |
Samuel Merritt | 0d499bc | 2013-06-19 12:08:23 -0700 | [diff] [blame] | 42 | # All the successful HTTP status codes from RFC 2616 |
| 43 | HTTP_SUCCESS = (200, 201, 202, 203, 204, 205, 206) |
| 44 | |
Jay Pipes | 051075a | 2012-04-28 17:39:37 -0400 | [diff] [blame] | 45 | |
Chris Yeoh | 55530bb | 2013-02-08 16:04:27 +1030 | [diff] [blame] | 46 | def attr(*args, **kwargs): |
Matthew Treinish | a74f5d4 | 2014-02-07 20:25:44 -0500 | [diff] [blame] | 47 | """A decorator which applies the testtools attr decorator |
Chris Yeoh | 55530bb | 2013-02-08 16:04:27 +1030 | [diff] [blame] | 48 | |
Matthew Treinish | a74f5d4 | 2014-02-07 20:25:44 -0500 | [diff] [blame] | 49 | This decorator applies the testtools.testcase.attr if it is in the list of |
| 50 | attributes to testtools we want to apply. |
Attila Fazekas | b2902af | 2013-02-16 16:22:44 +0100 | [diff] [blame] | 51 | """ |
Chris Yeoh | 55530bb | 2013-02-08 16:04:27 +1030 | [diff] [blame] | 52 | |
| 53 | def decorator(f): |
Giulio Fidente | 4946a05 | 2013-05-14 12:23:51 +0200 | [diff] [blame] | 54 | if 'type' in kwargs and isinstance(kwargs['type'], str): |
| 55 | f = testtools.testcase.attr(kwargs['type'])(f) |
Chris Yeoh | cf3fb7c | 2013-05-19 15:59:00 +0930 | [diff] [blame] | 56 | if kwargs['type'] == 'smoke': |
| 57 | f = testtools.testcase.attr('gate')(f) |
Giulio Fidente | 4946a05 | 2013-05-14 12:23:51 +0200 | [diff] [blame] | 58 | elif 'type' in kwargs and isinstance(kwargs['type'], list): |
| 59 | for attr in kwargs['type']: |
| 60 | f = testtools.testcase.attr(attr)(f) |
Chris Yeoh | cf3fb7c | 2013-05-19 15:59:00 +0930 | [diff] [blame] | 61 | if attr == 'smoke': |
| 62 | f = testtools.testcase.attr('gate')(f) |
Matthew Treinish | a74f5d4 | 2014-02-07 20:25:44 -0500 | [diff] [blame] | 63 | return f |
Chris Yeoh | 55530bb | 2013-02-08 16:04:27 +1030 | [diff] [blame] | 64 | |
| 65 | return decorator |
| 66 | |
| 67 | |
Matthew Treinish | 3d8c732 | 2014-08-03 23:53:28 -0400 | [diff] [blame] | 68 | def get_service_list(): |
Matthew Treinish | 8afbffd | 2014-01-21 23:56:13 +0000 | [diff] [blame] | 69 | service_list = { |
| 70 | 'compute': CONF.service_available.nova, |
| 71 | 'image': CONF.service_available.glance, |
Adam Gandelman | 4a48a60 | 2014-03-20 18:23:18 -0700 | [diff] [blame] | 72 | 'baremetal': CONF.service_available.ironic, |
Matthew Treinish | 8afbffd | 2014-01-21 23:56:13 +0000 | [diff] [blame] | 73 | 'volume': CONF.service_available.cinder, |
| 74 | 'orchestration': CONF.service_available.heat, |
| 75 | # NOTE(mtreinish) nova-network will provide networking functionality |
| 76 | # if neutron isn't available, so always set to True. |
| 77 | 'network': True, |
| 78 | 'identity': True, |
| 79 | 'object_storage': CONF.service_available.swift, |
| 80 | 'dashboard': CONF.service_available.horizon, |
ekhugen | 7aff099 | 2014-08-04 19:01:57 +0000 | [diff] [blame] | 81 | 'telemetry': CONF.service_available.ceilometer, |
Yaroslav Lobankov | ff42c87 | 2014-06-17 15:39:43 +0400 | [diff] [blame] | 82 | 'data_processing': CONF.service_available.sahara |
Matthew Treinish | 8afbffd | 2014-01-21 23:56:13 +0000 | [diff] [blame] | 83 | } |
Matthew Treinish | 3d8c732 | 2014-08-03 23:53:28 -0400 | [diff] [blame] | 84 | return service_list |
Matthew Treinish | 16c4379 | 2013-09-09 19:55:23 +0000 | [diff] [blame] | 85 | |
Matthew Treinish | 3d8c732 | 2014-08-03 23:53:28 -0400 | [diff] [blame] | 86 | |
| 87 | def services(*args, **kwargs): |
| 88 | """A decorator used to set an attr for each service used in a test case |
| 89 | |
| 90 | This decorator applies a testtools attr for each service that gets |
| 91 | exercised by a test case. |
| 92 | """ |
Matthew Treinish | 16c4379 | 2013-09-09 19:55:23 +0000 | [diff] [blame] | 93 | def decorator(f): |
Matthew Treinish | 3d8c732 | 2014-08-03 23:53:28 -0400 | [diff] [blame] | 94 | services = ['compute', 'image', 'baremetal', 'volume', 'orchestration', |
| 95 | 'network', 'identity', 'object_storage', 'dashboard', |
Matthew Treinish | 6035905 | 2014-09-18 17:39:26 -0400 | [diff] [blame] | 96 | 'telemetry', 'data_processing'] |
Matthew Treinish | 16c4379 | 2013-09-09 19:55:23 +0000 | [diff] [blame] | 97 | for service in args: |
Matthew Treinish | 3d8c732 | 2014-08-03 23:53:28 -0400 | [diff] [blame] | 98 | if service not in services: |
| 99 | raise exceptions.InvalidServiceTag('%s is not a valid ' |
| 100 | 'service' % service) |
Matthew Treinish | 16c4379 | 2013-09-09 19:55:23 +0000 | [diff] [blame] | 101 | attr(type=list(args))(f) |
Matthew Treinish | 8afbffd | 2014-01-21 23:56:13 +0000 | [diff] [blame] | 102 | |
| 103 | @functools.wraps(f) |
| 104 | def wrapper(self, *func_args, **func_kwargs): |
Matthew Treinish | 3d8c732 | 2014-08-03 23:53:28 -0400 | [diff] [blame] | 105 | service_list = get_service_list() |
| 106 | |
Matthew Treinish | 8afbffd | 2014-01-21 23:56:13 +0000 | [diff] [blame] | 107 | for service in args: |
| 108 | if not service_list[service]: |
| 109 | msg = 'Skipped because the %s service is not available' % ( |
| 110 | service) |
| 111 | raise testtools.TestCase.skipException(msg) |
| 112 | return f(self, *func_args, **func_kwargs) |
| 113 | return wrapper |
Matthew Treinish | 16c4379 | 2013-09-09 19:55:23 +0000 | [diff] [blame] | 114 | return decorator |
| 115 | |
| 116 | |
Marc Koderer | 32221b8e | 2013-08-23 13:57:50 +0200 | [diff] [blame] | 117 | def stresstest(*args, **kwargs): |
| 118 | """Add stress test decorator |
| 119 | |
| 120 | For all functions with this decorator a attr stress will be |
| 121 | set automatically. |
| 122 | |
| 123 | @param class_setup_per: allowed values are application, process, action |
| 124 | ``application``: once in the stress job lifetime |
| 125 | ``process``: once in the worker process lifetime |
| 126 | ``action``: on each action |
Marc Koderer | b060441 | 2013-09-02 09:43:40 +0200 | [diff] [blame] | 127 | @param allow_inheritance: allows inheritance of this attribute |
Marc Koderer | 32221b8e | 2013-08-23 13:57:50 +0200 | [diff] [blame] | 128 | """ |
| 129 | def decorator(f): |
| 130 | if 'class_setup_per' in kwargs: |
| 131 | setattr(f, "st_class_setup_per", kwargs['class_setup_per']) |
| 132 | else: |
| 133 | setattr(f, "st_class_setup_per", 'process') |
Marc Koderer | b060441 | 2013-09-02 09:43:40 +0200 | [diff] [blame] | 134 | if 'allow_inheritance' in kwargs: |
| 135 | setattr(f, "st_allow_inheritance", kwargs['allow_inheritance']) |
| 136 | else: |
| 137 | setattr(f, "st_allow_inheritance", False) |
Marc Koderer | 32221b8e | 2013-08-23 13:57:50 +0200 | [diff] [blame] | 138 | attr(type='stress')(f) |
| 139 | return f |
| 140 | return decorator |
| 141 | |
| 142 | |
Giulio Fidente | 83181a9 | 2013-10-01 06:02:24 +0200 | [diff] [blame] | 143 | def skip_because(*args, **kwargs): |
| 144 | """A decorator useful to skip tests hitting known bugs |
| 145 | |
| 146 | @param bug: bug number causing the test to skip |
| 147 | @param condition: optional condition to be True for the skip to have place |
Ken'ichi Ohmichi | a1aa44c | 2013-12-06 20:48:24 +0900 | [diff] [blame] | 148 | @param interface: skip the test if it is the same as self._interface |
Giulio Fidente | 83181a9 | 2013-10-01 06:02:24 +0200 | [diff] [blame] | 149 | """ |
| 150 | def decorator(f): |
Masayuki Igawa | 80c1b9f | 2013-10-07 17:19:11 +0900 | [diff] [blame] | 151 | @functools.wraps(f) |
Ken'ichi Ohmichi | a1aa44c | 2013-12-06 20:48:24 +0900 | [diff] [blame] | 152 | def wrapper(self, *func_args, **func_kwargs): |
| 153 | skip = False |
| 154 | if "condition" in kwargs: |
| 155 | if kwargs["condition"] is True: |
| 156 | skip = True |
| 157 | elif "interface" in kwargs: |
| 158 | if kwargs["interface"] == self._interface: |
| 159 | skip = True |
| 160 | else: |
| 161 | skip = True |
| 162 | if "bug" in kwargs and skip is True: |
Matthew Treinish | ede5027 | 2014-02-11 19:56:11 +0000 | [diff] [blame] | 163 | if not kwargs['bug'].isdigit(): |
| 164 | raise ValueError('bug must be a valid bug number') |
Ken'ichi Ohmichi | a1aa44c | 2013-12-06 20:48:24 +0900 | [diff] [blame] | 165 | msg = "Skipped until Bug: %s is resolved." % kwargs["bug"] |
| 166 | raise testtools.TestCase.skipException(msg) |
| 167 | return f(self, *func_args, **func_kwargs) |
Masayuki Igawa | 80c1b9f | 2013-10-07 17:19:11 +0900 | [diff] [blame] | 168 | return wrapper |
Giulio Fidente | 83181a9 | 2013-10-01 06:02:24 +0200 | [diff] [blame] | 169 | return decorator |
| 170 | |
| 171 | |
Matthew Treinish | e3d2614 | 2013-11-26 19:14:58 +0000 | [diff] [blame] | 172 | def requires_ext(*args, **kwargs): |
| 173 | """A decorator to skip tests if an extension is not enabled |
| 174 | |
| 175 | @param extension |
| 176 | @param service |
| 177 | """ |
| 178 | def decorator(func): |
| 179 | @functools.wraps(func) |
| 180 | def wrapper(*func_args, **func_kwargs): |
| 181 | if not is_extension_enabled(kwargs['extension'], |
| 182 | kwargs['service']): |
| 183 | msg = "Skipped because %s extension: %s is not enabled" % ( |
| 184 | kwargs['service'], kwargs['extension']) |
| 185 | raise testtools.TestCase.skipException(msg) |
| 186 | return func(*func_args, **func_kwargs) |
| 187 | return wrapper |
| 188 | return decorator |
| 189 | |
| 190 | |
| 191 | def is_extension_enabled(extension_name, service): |
| 192 | """A function that will check the list of enabled extensions from config |
| 193 | |
| 194 | """ |
Matthew Treinish | e3d2614 | 2013-11-26 19:14:58 +0000 | [diff] [blame] | 195 | config_dict = { |
Matthew Treinish | bc0e03e | 2014-01-30 16:51:06 +0000 | [diff] [blame] | 196 | 'compute': CONF.compute_feature_enabled.api_extensions, |
Matthew Treinish | bc0e03e | 2014-01-30 16:51:06 +0000 | [diff] [blame] | 197 | 'volume': CONF.volume_feature_enabled.api_extensions, |
| 198 | 'network': CONF.network_feature_enabled.api_extensions, |
| 199 | 'object': CONF.object_storage_feature_enabled.discoverable_apis, |
Matthew Treinish | e3d2614 | 2013-11-26 19:14:58 +0000 | [diff] [blame] | 200 | } |
Simeon Monov | 5d7effe | 2014-07-16 07:32:38 +0300 | [diff] [blame] | 201 | if len(config_dict[service]) == 0: |
| 202 | return False |
Matthew Treinish | e3d2614 | 2013-11-26 19:14:58 +0000 | [diff] [blame] | 203 | if config_dict[service][0] == 'all': |
| 204 | return True |
| 205 | if extension_name in config_dict[service]: |
| 206 | return True |
| 207 | return False |
| 208 | |
Ian Wienand | 98c35f3 | 2013-07-23 20:34:23 +1000 | [diff] [blame] | 209 | |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 210 | at_exit_set = set() |
| 211 | |
| 212 | |
| 213 | def validate_tearDownClass(): |
| 214 | if at_exit_set: |
Sean Dague | eb1523b | 2014-03-10 10:17:44 -0400 | [diff] [blame] | 215 | LOG.error( |
| 216 | "tearDownClass does not call the super's " |
| 217 | "tearDownClass in these classes: \n" |
| 218 | + str(at_exit_set)) |
| 219 | |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 220 | |
| 221 | atexit.register(validate_tearDownClass) |
| 222 | |
Attila Fazekas | 5394332 | 2014-02-10 16:07:34 +0100 | [diff] [blame] | 223 | |
Matthew Treinish | 2474f41 | 2014-11-17 18:11:56 -0500 | [diff] [blame] | 224 | class BaseTestCase(testtools.testcase.WithAttributes, |
| 225 | testtools.TestCase): |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 226 | """The test base class defines Tempest framework for class level fixtures. |
| 227 | `setUpClass` and `tearDownClass` are defined here and cannot be overwritten |
| 228 | by subclasses (enforced via hacking rule T105). |
| 229 | |
| 230 | Set-up is split in a series of steps (setup stages), which can be |
| 231 | overwritten by test classes. Set-up stages are: |
| 232 | - skip_checks |
| 233 | - setup_credentials |
| 234 | - setup_clients |
| 235 | - resource_setup |
| 236 | |
| 237 | Tear-down is also split in a series of steps (teardown stages), which are |
| 238 | stacked for execution only if the corresponding setup stage had been |
| 239 | reached during the setup phase. Tear-down stages are: |
| 240 | - clear_isolated_creds (defined in the base test class) |
| 241 | - resource_cleanup |
| 242 | """ |
Attila Fazekas | c43fec8 | 2013-04-09 23:17:52 +0200 | [diff] [blame] | 243 | |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 244 | setUpClassCalled = False |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 245 | _service = None |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 246 | |
Matthew Treinish | 9f756a0 | 2014-01-15 10:26:07 -0500 | [diff] [blame] | 247 | network_resources = {} |
| 248 | |
Sean Dague | 2ef32ac | 2014-06-09 11:32:23 -0400 | [diff] [blame] | 249 | # NOTE(sdague): log_format is defined inline here instead of using the oslo |
| 250 | # default because going through the config path recouples config to the |
| 251 | # stress tests too early, and depending on testr order will fail unit tests |
| 252 | log_format = ('%(asctime)s %(process)d %(levelname)-8s ' |
| 253 | '[%(name)s] %(message)s') |
| 254 | |
Pavel Sedlák | 1053bd3 | 2013-04-16 16:47:40 +0200 | [diff] [blame] | 255 | @classmethod |
| 256 | def setUpClass(cls): |
Andrea Frittoli | 73ee247 | 2014-09-15 12:31:53 +0100 | [diff] [blame] | 257 | # It should never be overridden by descendants |
Pavel Sedlák | 1053bd3 | 2013-04-16 16:47:40 +0200 | [diff] [blame] | 258 | if hasattr(super(BaseTestCase, cls), 'setUpClass'): |
| 259 | super(BaseTestCase, cls).setUpClass() |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 260 | cls.setUpClassCalled = True |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 261 | # Stack of (name, callable) to be invoked in reverse order at teardown |
| 262 | cls.teardowns = [] |
| 263 | # All the configuration checks that may generate a skip |
| 264 | cls.skip_checks() |
Andrea Frittoli | 73ee247 | 2014-09-15 12:31:53 +0100 | [diff] [blame] | 265 | try: |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 266 | # Allocation of all required credentials and client managers |
| 267 | cls.teardowns.append(('credentials', cls.clear_isolated_creds)) |
| 268 | cls.setup_credentials() |
| 269 | # Shortcuts to clients |
| 270 | cls.setup_clients() |
| 271 | # Additional class-wide test resources |
| 272 | cls.teardowns.append(('resources', cls.resource_cleanup)) |
Andrea Frittoli | 73ee247 | 2014-09-15 12:31:53 +0100 | [diff] [blame] | 273 | cls.resource_setup() |
| 274 | except Exception: |
| 275 | etype, value, trace = sys.exc_info() |
Matthew Treinish | ed2ad4f | 2014-12-23 15:18:32 -0500 | [diff] [blame] | 276 | LOG.info("%s raised in %s.setUpClass. Invoking tearDownClass." % ( |
| 277 | etype, cls.__name__)) |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 278 | cls.tearDownClass() |
Andrea Frittoli | 73ee247 | 2014-09-15 12:31:53 +0100 | [diff] [blame] | 279 | try: |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 280 | raise etype, value, trace |
Andrea Frittoli | 73ee247 | 2014-09-15 12:31:53 +0100 | [diff] [blame] | 281 | finally: |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 282 | del trace # to avoid circular refs |
Pavel Sedlák | 1053bd3 | 2013-04-16 16:47:40 +0200 | [diff] [blame] | 283 | |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 284 | @classmethod |
| 285 | def tearDownClass(cls): |
Attila Fazekas | 5d27530 | 2013-08-29 12:35:12 +0200 | [diff] [blame] | 286 | at_exit_set.discard(cls) |
Andrea Frittoli | 73ee247 | 2014-09-15 12:31:53 +0100 | [diff] [blame] | 287 | # It should never be overridden by descendants |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 288 | if hasattr(super(BaseTestCase, cls), 'tearDownClass'): |
| 289 | super(BaseTestCase, cls).tearDownClass() |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 290 | # Save any existing exception, we always want to re-raise the original |
| 291 | # exception only |
| 292 | etype, value, trace = sys.exc_info() |
| 293 | # If there was no exception during setup we shall re-raise the first |
| 294 | # exception in teardown |
| 295 | re_raise = (etype is None) |
| 296 | while cls.teardowns: |
| 297 | name, teardown = cls.teardowns.pop() |
| 298 | # Catch any exception in tearDown so we can re-raise the original |
| 299 | # exception at the end |
| 300 | try: |
| 301 | teardown() |
| 302 | except Exception as te: |
| 303 | sys_exec_info = sys.exc_info() |
| 304 | tetype = sys_exec_info[0] |
| 305 | # TODO(andreaf): Till we have the ability to cleanup only |
| 306 | # resources that were successfully setup in resource_cleanup, |
| 307 | # log AttributeError as info instead of exception. |
| 308 | if tetype is AttributeError and name == 'resources': |
| 309 | LOG.info("tearDownClass of %s failed: %s" % (name, te)) |
| 310 | else: |
| 311 | LOG.exception("teardown of %s failed: %s" % (name, te)) |
| 312 | if not etype: |
| 313 | etype, value, trace = sys_exec_info |
| 314 | # If exceptions were raised during teardown, an not before, re-raise |
| 315 | # the first one |
| 316 | if re_raise and etype is not None: |
| 317 | try: |
| 318 | raise etype, value, trace |
| 319 | finally: |
| 320 | del trace # to avoid circular refs |
Andrea Frittoli | 73ee247 | 2014-09-15 12:31:53 +0100 | [diff] [blame] | 321 | |
| 322 | @classmethod |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 323 | def skip_checks(cls): |
| 324 | """Class level skip checks. Subclasses verify in here all |
| 325 | conditions that might prevent the execution of the entire test class. |
| 326 | Checks implemented here may not make use API calls, and should rely on |
| 327 | configuration alone. |
| 328 | In general skip checks that require an API call are discouraged. |
| 329 | If one is really needed it may be implemented either in the |
| 330 | resource_setup or at test level. |
| 331 | """ |
| 332 | pass |
| 333 | |
| 334 | @classmethod |
| 335 | def setup_credentials(cls): |
| 336 | """Allocate credentials and the client managers from them.""" |
| 337 | # TODO(andreaf) There is a fair amount of code that could me moved from |
| 338 | # base / test classes in here. Ideally tests should be able to only |
| 339 | # specify a list of (additional) credentials the need to use. |
| 340 | pass |
| 341 | |
| 342 | @classmethod |
| 343 | def setup_clients(cls): |
| 344 | """Create links to the clients into the test object.""" |
| 345 | # TODO(andreaf) There is a fair amount of code that could me moved from |
| 346 | # base / test classes in here. Ideally tests should be able to only |
| 347 | # specify which client is `client` and nothing else. |
Andrea Frittoli | 73ee247 | 2014-09-15 12:31:53 +0100 | [diff] [blame] | 348 | pass |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 349 | |
Emily Hugenbruch | 5bd4cbf | 2014-12-17 21:38:38 +0000 | [diff] [blame] | 350 | @classmethod |
| 351 | def resource_setup(cls): |
| 352 | """Class level resource setup for test cases. |
| 353 | """ |
| 354 | pass |
| 355 | |
| 356 | @classmethod |
| 357 | def resource_cleanup(cls): |
| 358 | """Class level resource cleanup for test cases. |
| 359 | Resource cleanup must be able to handle the case of partially setup |
| 360 | resources, in case a failure during `resource_setup` should happen. |
| 361 | """ |
| 362 | pass |
| 363 | |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 364 | def setUp(self): |
| 365 | super(BaseTestCase, self).setUp() |
| 366 | if not self.setUpClassCalled: |
| 367 | raise RuntimeError("setUpClass does not calls the super's" |
| 368 | "setUpClass in the " |
| 369 | + self.__class__.__name__) |
| 370 | at_exit_set.add(self.__class__) |
Matthew Treinish | 78561ad | 2013-07-26 11:41:56 -0400 | [diff] [blame] | 371 | test_timeout = os.environ.get('OS_TEST_TIMEOUT', 0) |
| 372 | try: |
| 373 | test_timeout = int(test_timeout) |
| 374 | except ValueError: |
| 375 | test_timeout = 0 |
| 376 | if test_timeout > 0: |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 377 | self.useFixture(fixtures.Timeout(test_timeout, gentle=True)) |
Matthew Treinish | 78561ad | 2013-07-26 11:41:56 -0400 | [diff] [blame] | 378 | |
| 379 | if (os.environ.get('OS_STDOUT_CAPTURE') == 'True' or |
| 380 | os.environ.get('OS_STDOUT_CAPTURE') == '1'): |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 381 | stdout = self.useFixture(fixtures.StringStream('stdout')).stream |
| 382 | self.useFixture(fixtures.MonkeyPatch('sys.stdout', stdout)) |
Matthew Treinish | 78561ad | 2013-07-26 11:41:56 -0400 | [diff] [blame] | 383 | if (os.environ.get('OS_STDERR_CAPTURE') == 'True' or |
| 384 | os.environ.get('OS_STDERR_CAPTURE') == '1'): |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 385 | stderr = self.useFixture(fixtures.StringStream('stderr')).stream |
| 386 | self.useFixture(fixtures.MonkeyPatch('sys.stderr', stderr)) |
Attila Fazekas | 3138807 | 2013-08-15 08:58:07 +0200 | [diff] [blame] | 387 | if (os.environ.get('OS_LOG_CAPTURE') != 'False' and |
| 388 | os.environ.get('OS_LOG_CAPTURE') != '0'): |
Attila Fazekas | 3138807 | 2013-08-15 08:58:07 +0200 | [diff] [blame] | 389 | self.useFixture(fixtures.LoggerFixture(nuke_handlers=False, |
Sean Dague | 2ef32ac | 2014-06-09 11:32:23 -0400 | [diff] [blame] | 390 | format=self.log_format, |
Attila Fazekas | 90445be | 2013-10-24 16:46:03 +0200 | [diff] [blame] | 391 | level=None)) |
Matthew Treinish | 78561ad | 2013-07-26 11:41:56 -0400 | [diff] [blame] | 392 | |
Matthew Treinish | 3e04685 | 2013-07-23 16:00:24 -0400 | [diff] [blame] | 393 | @classmethod |
Matthew Treinish | 8004e8c | 2014-01-27 23:03:14 +0000 | [diff] [blame] | 394 | def get_client_manager(cls, interface=None): |
Ryan Hsu | 6c4bb3d | 2013-10-21 21:22:50 -0700 | [diff] [blame] | 395 | """ |
tanlin | 4956a64 | 2014-02-13 16:52:11 +0800 | [diff] [blame] | 396 | Returns an OpenStack client manager |
Ryan Hsu | 6c4bb3d | 2013-10-21 21:22:50 -0700 | [diff] [blame] | 397 | """ |
Ryan Hsu | 6c4bb3d | 2013-10-21 21:22:50 -0700 | [diff] [blame] | 398 | force_tenant_isolation = getattr(cls, 'force_tenant_isolation', None) |
Andrea Frittoli | 8283b4e | 2014-07-17 13:28:58 +0100 | [diff] [blame] | 399 | |
Marc Koderer | 44dce62 | 2014-11-14 10:08:12 +0100 | [diff] [blame] | 400 | if (not hasattr(cls, 'isolated_creds') or |
| 401 | not cls.isolated_creds.name == cls.__name__): |
| 402 | cls.isolated_creds = credentials.get_isolated_credentials( |
| 403 | name=cls.__name__, network_resources=cls.network_resources, |
| 404 | force_tenant_isolation=force_tenant_isolation, |
| 405 | ) |
Andrea Frittoli | 8283b4e | 2014-07-17 13:28:58 +0100 | [diff] [blame] | 406 | |
| 407 | creds = cls.isolated_creds.get_primary_creds() |
| 408 | params = dict(credentials=creds, service=cls._service) |
| 409 | if getattr(cls, '_interface', None): |
| 410 | interface = cls._interface |
| 411 | if interface: |
| 412 | params['interface'] = interface |
| 413 | os = clients.Manager(**params) |
Ryan Hsu | 6c4bb3d | 2013-10-21 21:22:50 -0700 | [diff] [blame] | 414 | return os |
| 415 | |
| 416 | @classmethod |
| 417 | def clear_isolated_creds(cls): |
| 418 | """ |
| 419 | Clears isolated creds if set |
| 420 | """ |
Andrea Frittoli | 73ee247 | 2014-09-15 12:31:53 +0100 | [diff] [blame] | 421 | if hasattr(cls, 'isolated_creds'): |
Ryan Hsu | 6c4bb3d | 2013-10-21 21:22:50 -0700 | [diff] [blame] | 422 | cls.isolated_creds.clear_isolated_creds() |
| 423 | |
| 424 | @classmethod |
Matthew Treinish | 3e04685 | 2013-07-23 16:00:24 -0400 | [diff] [blame] | 425 | def _get_identity_admin_client(cls): |
| 426 | """ |
| 427 | Returns an instance of the Identity Admin API client |
| 428 | """ |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 429 | os = clients.AdminManager(interface=cls._interface, |
| 430 | service=cls._service) |
Matthew Treinish | 3e04685 | 2013-07-23 16:00:24 -0400 | [diff] [blame] | 431 | admin_client = os.identity_client |
| 432 | return admin_client |
| 433 | |
| 434 | @classmethod |
Matthew Treinish | 9f756a0 | 2014-01-15 10:26:07 -0500 | [diff] [blame] | 435 | def set_network_resources(self, network=False, router=False, subnet=False, |
| 436 | dhcp=False): |
| 437 | """Specify which network resources should be created |
| 438 | |
| 439 | @param network |
| 440 | @param router |
| 441 | @param subnet |
| 442 | @param dhcp |
| 443 | """ |
Salvatore Orlando | 5a33724 | 2014-01-15 22:49:22 +0000 | [diff] [blame] | 444 | # network resources should be set only once from callers |
| 445 | # in order to ensure that even if it's called multiple times in |
| 446 | # a chain of overloaded methods, the attribute is set only |
| 447 | # in the leaf class |
| 448 | if not self.network_resources: |
| 449 | self.network_resources = { |
| 450 | 'network': network, |
| 451 | 'router': router, |
| 452 | 'subnet': subnet, |
| 453 | 'dhcp': dhcp} |
Matthew Treinish | 9f756a0 | 2014-01-15 10:26:07 -0500 | [diff] [blame] | 454 | |
Mark Maglana | 5885eb3 | 2014-02-28 10:57:34 -0800 | [diff] [blame] | 455 | def assertEmpty(self, list, msg=None): |
| 456 | self.assertTrue(len(list) == 0, msg) |
| 457 | |
| 458 | def assertNotEmpty(self, list, msg=None): |
| 459 | self.assertTrue(len(list) > 0, msg) |
| 460 | |
Attila Fazekas | dc21642 | 2013-01-29 15:12:14 +0100 | [diff] [blame] | 461 | |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 462 | class NegativeAutoTest(BaseTestCase): |
| 463 | |
| 464 | _resources = {} |
| 465 | |
| 466 | @classmethod |
| 467 | def setUpClass(cls): |
| 468 | super(NegativeAutoTest, cls).setUpClass() |
| 469 | os = cls.get_client_manager() |
| 470 | cls.client = os.negative_client |
Marc Koderer | f857fda | 2014-03-05 15:58:00 +0100 | [diff] [blame] | 471 | os_admin = clients.AdminManager(interface=cls._interface, |
| 472 | service=cls._service) |
| 473 | cls.admin_client = os_admin.negative_client |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 474 | |
| 475 | @staticmethod |
Marc Koderer | 674c8fc | 2014-03-17 09:45:04 +0100 | [diff] [blame] | 476 | def load_tests(*args): |
| 477 | """ |
| 478 | Wrapper for testscenarios to set the mandatory scenarios variable |
| 479 | only in case a real test loader is in place. Will be automatically |
| 480 | called in case the variable "load_tests" is set. |
| 481 | """ |
| 482 | if getattr(args[0], 'suiteClass', None) is not None: |
| 483 | loader, standard_tests, pattern = args |
| 484 | else: |
| 485 | standard_tests, module, loader = args |
| 486 | for test in testtools.iterate_tests(standard_tests): |
Marc Koderer | 4f44d72 | 2014-08-07 14:04:58 +0200 | [diff] [blame] | 487 | schema = getattr(test, '_schema', None) |
Marc Koderer | 3dd3105 | 2014-11-27 09:31:00 +0100 | [diff] [blame] | 488 | if schema is not None: |
Marc Koderer | 4f44d72 | 2014-08-07 14:04:58 +0200 | [diff] [blame] | 489 | setattr(test, 'scenarios', |
| 490 | NegativeAutoTest.generate_scenario(schema)) |
Marc Koderer | 674c8fc | 2014-03-17 09:45:04 +0100 | [diff] [blame] | 491 | return testscenarios.load_tests_apply_scenarios(*args) |
| 492 | |
| 493 | @staticmethod |
Marc Koderer | 4f44d72 | 2014-08-07 14:04:58 +0200 | [diff] [blame] | 494 | def generate_scenario(description): |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 495 | """ |
| 496 | Generates the test scenario list for a given description. |
| 497 | |
Marc Koderer | 4f44d72 | 2014-08-07 14:04:58 +0200 | [diff] [blame] | 498 | :param description: A file or dictionary with the following entries: |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 499 | name (required) name for the api |
| 500 | http-method (required) one of HEAD,GET,PUT,POST,PATCH,DELETE |
| 501 | url (required) the url to be appended to the catalog url with '%s' |
| 502 | for each resource mentioned |
| 503 | resources: (optional) A list of resource names such as "server", |
| 504 | "flavor", etc. with an element for each '%s' in the url. This |
| 505 | method will call self.get_resource for each element when |
| 506 | constructing the positive test case template so negative |
| 507 | subclasses are expected to return valid resource ids when |
| 508 | appropriate. |
| 509 | json-schema (optional) A valid json schema that will be used to |
| 510 | create invalid data for the api calls. For "GET" and "HEAD", |
| 511 | the data is used to generate query strings appended to the url, |
| 512 | otherwise for the body of the http call. |
| 513 | """ |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 514 | LOG.debug(description) |
Marc Koderer | 674c8fc | 2014-03-17 09:45:04 +0100 | [diff] [blame] | 515 | generator = importutils.import_class( |
| 516 | CONF.negative.test_generator)() |
Marc Koderer | 6ee82dc | 2014-02-17 10:26:29 +0100 | [diff] [blame] | 517 | generator.validate_schema(description) |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 518 | schema = description.get("json-schema", None) |
| 519 | resources = description.get("resources", []) |
| 520 | scenario_list = [] |
Marc Koderer | 424c84f | 2014-02-06 17:02:19 +0100 | [diff] [blame] | 521 | expected_result = None |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 522 | for resource in resources: |
Marc Koderer | 424c84f | 2014-02-06 17:02:19 +0100 | [diff] [blame] | 523 | if isinstance(resource, dict): |
| 524 | expected_result = resource['expected_result'] |
| 525 | resource = resource['name'] |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 526 | LOG.debug("Add resource to test %s" % resource) |
| 527 | scn_name = "inv_res_%s" % (resource) |
| 528 | scenario_list.append((scn_name, {"resource": (resource, |
Marc Koderer | 424c84f | 2014-02-06 17:02:19 +0100 | [diff] [blame] | 529 | str(uuid.uuid4())), |
| 530 | "expected_result": expected_result |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 531 | })) |
| 532 | if schema is not None: |
Marc Koderer | f07f5d1 | 2014-09-01 09:47:23 +0200 | [diff] [blame] | 533 | for scenario in generator.generate_scenarios(schema): |
| 534 | scenario_list.append((scenario['_negtest_name'], |
| 535 | scenario)) |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 536 | LOG.debug(scenario_list) |
| 537 | return scenario_list |
| 538 | |
Marc Koderer | 4f44d72 | 2014-08-07 14:04:58 +0200 | [diff] [blame] | 539 | def execute(self, description): |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 540 | """ |
| 541 | Execute a http call on an api that are expected to |
| 542 | result in client errors. First it uses invalid resources that are part |
| 543 | of the url, and then invalid data for queries and http request bodies. |
| 544 | |
Marc Koderer | 4f44d72 | 2014-08-07 14:04:58 +0200 | [diff] [blame] | 545 | :param description: A json file or dictionary with the following |
| 546 | entries: |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 547 | name (required) name for the api |
| 548 | http-method (required) one of HEAD,GET,PUT,POST,PATCH,DELETE |
| 549 | url (required) the url to be appended to the catalog url with '%s' |
| 550 | for each resource mentioned |
| 551 | resources: (optional) A list of resource names such as "server", |
| 552 | "flavor", etc. with an element for each '%s' in the url. This |
| 553 | method will call self.get_resource for each element when |
| 554 | constructing the positive test case template so negative |
| 555 | subclasses are expected to return valid resource ids when |
| 556 | appropriate. |
| 557 | json-schema (optional) A valid json schema that will be used to |
| 558 | create invalid data for the api calls. For "GET" and "HEAD", |
| 559 | the data is used to generate query strings appended to the url, |
| 560 | otherwise for the body of the http call. |
| 561 | |
| 562 | """ |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 563 | LOG.info("Executing %s" % description["name"]) |
| 564 | LOG.debug(description) |
Marc Koderer | f07f5d1 | 2014-09-01 09:47:23 +0200 | [diff] [blame] | 565 | generator = importutils.import_class( |
| 566 | CONF.negative.test_generator)() |
| 567 | schema = description.get("json-schema", None) |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 568 | method = description["http-method"] |
| 569 | url = description["url"] |
Marc Koderer | f07f5d1 | 2014-09-01 09:47:23 +0200 | [diff] [blame] | 570 | expected_result = None |
| 571 | if "default_result_code" in description: |
| 572 | expected_result = description["default_result_code"] |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 573 | |
| 574 | resources = [self.get_resource(r) for |
| 575 | r in description.get("resources", [])] |
| 576 | |
| 577 | if hasattr(self, "resource"): |
| 578 | # Note(mkoderer): The resources list already contains an invalid |
| 579 | # entry (see get_resource). |
| 580 | # We just send a valid json-schema with it |
Marc Koderer | 6ee82dc | 2014-02-17 10:26:29 +0100 | [diff] [blame] | 581 | valid_schema = None |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 582 | if schema: |
Marc Koderer | 6ee82dc | 2014-02-17 10:26:29 +0100 | [diff] [blame] | 583 | valid_schema = \ |
| 584 | valid.ValidTestGenerator().generate_valid(schema) |
| 585 | new_url, body = self._http_arguments(valid_schema, url, method) |
Marc Koderer | f07f5d1 | 2014-09-01 09:47:23 +0200 | [diff] [blame] | 586 | elif hasattr(self, "_negtest_name"): |
| 587 | schema_under_test = \ |
| 588 | valid.ValidTestGenerator().generate_valid(schema) |
| 589 | local_expected_result = \ |
| 590 | generator.generate_payload(self, schema_under_test) |
| 591 | if local_expected_result is not None: |
| 592 | expected_result = local_expected_result |
| 593 | new_url, body = \ |
| 594 | self._http_arguments(schema_under_test, url, method) |
Marc Koderer | 1c247c8 | 2014-03-20 08:24:38 +0100 | [diff] [blame] | 595 | else: |
| 596 | raise Exception("testscenarios are not active. Please make sure " |
| 597 | "that your test runner supports the load_tests " |
| 598 | "mechanism") |
Marc Koderer | 424c84f | 2014-02-06 17:02:19 +0100 | [diff] [blame] | 599 | |
Marc Koderer | f857fda | 2014-03-05 15:58:00 +0100 | [diff] [blame] | 600 | if "admin_client" in description and description["admin_client"]: |
| 601 | client = self.admin_client |
| 602 | else: |
| 603 | client = self.client |
| 604 | resp, resp_body = client.send_request(method, new_url, |
| 605 | resources, body=body) |
Marc Koderer | f07f5d1 | 2014-09-01 09:47:23 +0200 | [diff] [blame] | 606 | self._check_negative_response(expected_result, resp.status, resp_body) |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 607 | |
| 608 | def _http_arguments(self, json_dict, url, method): |
| 609 | LOG.debug("dict: %s url: %s method: %s" % (json_dict, url, method)) |
| 610 | if not json_dict: |
| 611 | return url, None |
| 612 | elif method in ["GET", "HEAD", "PUT", "DELETE"]: |
| 613 | return "%s?%s" % (url, urllib.urlencode(json_dict)), None |
| 614 | else: |
| 615 | return url, json.dumps(json_dict) |
| 616 | |
Marc Koderer | f07f5d1 | 2014-09-01 09:47:23 +0200 | [diff] [blame] | 617 | def _check_negative_response(self, expected_result, result, body): |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 618 | self.assertTrue(result >= 400 and result < 500 and result != 413, |
| 619 | "Expected client error, got %s:%s" % |
| 620 | (result, body)) |
| 621 | self.assertTrue(expected_result is None or expected_result == result, |
| 622 | "Expected %s, got %s:%s" % |
| 623 | (expected_result, result, body)) |
| 624 | |
| 625 | @classmethod |
| 626 | def set_resource(cls, name, resource): |
| 627 | """ |
| 628 | This function can be used in setUpClass context to register a resoruce |
| 629 | for a test. |
| 630 | |
| 631 | :param name: The name of the kind of resource such as "flavor", "role", |
| 632 | etc. |
| 633 | :resource: The id of the resource |
| 634 | """ |
| 635 | cls._resources[name] = resource |
| 636 | |
| 637 | def get_resource(self, name): |
| 638 | """ |
| 639 | Return a valid uuid for a type of resource. If a real resource is |
| 640 | needed as part of a url then this method should return one. Otherwise |
| 641 | it can return None. |
| 642 | |
| 643 | :param name: The name of the kind of resource such as "flavor", "role", |
| 644 | etc. |
| 645 | """ |
Marc Koderer | 424c84f | 2014-02-06 17:02:19 +0100 | [diff] [blame] | 646 | if isinstance(name, dict): |
| 647 | name = name['name'] |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 648 | if hasattr(self, "resource") and self.resource[0] == name: |
| 649 | LOG.debug("Return invalid resource (%s) value: %s" % |
| 650 | (self.resource[0], self.resource[1])) |
| 651 | return self.resource[1] |
| 652 | if name in self._resources: |
| 653 | return self._resources[name] |
| 654 | return None |
| 655 | |
| 656 | |
Marc Koderer | b2978da | 2014-03-26 13:45:43 +0100 | [diff] [blame] | 657 | def SimpleNegativeAutoTest(klass): |
| 658 | """ |
| 659 | This decorator registers a test function on basis of the class name. |
| 660 | """ |
| 661 | @attr(type=['negative', 'gate']) |
| 662 | def generic_test(self): |
Marc Koderer | 4f44d72 | 2014-08-07 14:04:58 +0200 | [diff] [blame] | 663 | if hasattr(self, '_schema'): |
| 664 | self.execute(self._schema) |
Marc Koderer | b2978da | 2014-03-26 13:45:43 +0100 | [diff] [blame] | 665 | |
| 666 | cn = klass.__name__ |
| 667 | cn = cn.replace('JSON', '') |
| 668 | cn = cn.replace('Test', '') |
| 669 | # NOTE(mkoderer): replaces uppercase chars inside the class name with '_' |
| 670 | lower_cn = re.sub('(?<!^)(?=[A-Z])', '_', cn).lower() |
| 671 | func_name = 'test_%s' % lower_cn |
| 672 | setattr(klass, func_name, generic_test) |
| 673 | return klass |
| 674 | |
| 675 | |
Sean Dague | 35a7caf | 2013-05-10 10:38:22 -0400 | [diff] [blame] | 676 | def call_until_true(func, duration, sleep_for): |
| 677 | """ |
| 678 | Call the given function until it returns True (and return True) or |
| 679 | until the specified duration (in seconds) elapses (and return |
| 680 | False). |
| 681 | |
| 682 | :param func: A zero argument callable that returns True on success. |
| 683 | :param duration: The number of seconds for which to attempt a |
| 684 | successful call of the function. |
| 685 | :param sleep_for: The number of seconds to sleep after an unsuccessful |
| 686 | invocation of the function. |
| 687 | """ |
| 688 | now = time.time() |
| 689 | timeout = now + duration |
| 690 | while now < timeout: |
| 691 | if func(): |
| 692 | return True |
Sean Dague | 35a7caf | 2013-05-10 10:38:22 -0400 | [diff] [blame] | 693 | time.sleep(sleep_for) |
| 694 | now = time.time() |
| 695 | return False |