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 |
Ian Wienand | 98c35f3 | 2013-07-23 20:34:23 +1000 | [diff] [blame] | 18 | import os |
Marc Koderer | b2978da | 2014-03-26 13:45:43 +0100 | [diff] [blame] | 19 | import re |
Attila Fazekas | 5394332 | 2014-02-10 16:07:34 +0100 | [diff] [blame] | 20 | import sys |
Jay Pipes | 051075a | 2012-04-28 17:39:37 -0400 | [diff] [blame] | 21 | import time |
| 22 | |
Matthew Treinish | 78561ad | 2013-07-26 11:41:56 -0400 | [diff] [blame] | 23 | import fixtures |
Doug Hellmann | 583ce2c | 2015-03-11 14:55:46 +0000 | [diff] [blame] | 24 | from oslo_log import log as logging |
Matthew Treinish | 2190551 | 2015-07-13 10:33:35 -0400 | [diff] [blame] | 25 | from oslo_serialization import jsonutils as json |
Doug Hellmann | 583ce2c | 2015-03-11 14:55:46 +0000 | [diff] [blame] | 26 | from oslo_utils import importutils |
Chris Hoge | 296558c | 2015-02-19 00:29:49 -0600 | [diff] [blame] | 27 | import six |
Harshada Mangesh Kakad | 71f6b97 | 2015-12-22 09:46:48 -0800 | [diff] [blame] | 28 | from six.moves import urllib |
Marc Koderer | 674c8fc | 2014-03-17 09:45:04 +0100 | [diff] [blame] | 29 | import testscenarios |
ivan-zhu | 1feeb38 | 2013-01-24 10:14:39 +0800 | [diff] [blame] | 30 | import testtools |
Jay Pipes | 051075a | 2012-04-28 17:39:37 -0400 | [diff] [blame] | 31 | |
Matthew Treinish | 3e04685 | 2013-07-23 16:00:24 -0400 | [diff] [blame] | 32 | from tempest import clients |
Jamie Lennox | 1535017 | 2015-08-17 10:54:25 +1000 | [diff] [blame] | 33 | from tempest.common import cred_client |
Andrea Frittoli (andreaf) | 290b3e1 | 2015-10-08 10:25:02 +0100 | [diff] [blame] | 34 | from tempest.common import credentials_factory as credentials |
Rohan Kanade | 9ce97df | 2013-12-10 18:59:35 +0530 | [diff] [blame] | 35 | from tempest.common import fixed_network |
Marc Koderer | 6ee82dc | 2014-02-17 10:26:29 +0100 | [diff] [blame] | 36 | import tempest.common.generator.valid_generator as valid |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 37 | import tempest.common.validation_resources as vresources |
Attila Fazekas | dc21642 | 2013-01-29 15:12:14 +0100 | [diff] [blame] | 38 | from tempest import config |
Matthew Treinish | 16c4379 | 2013-09-09 19:55:23 +0000 | [diff] [blame] | 39 | from tempest import exceptions |
Ken'ichi Ohmichi | d079c89 | 2016-04-19 11:23:36 -0700 | [diff] [blame] | 40 | from tempest.lib.common.utils import data_utils |
Andrea Frittoli (andreaf) | db9672e | 2016-02-23 14:07:24 -0500 | [diff] [blame] | 41 | from tempest.lib import decorators |
Jay Pipes | 051075a | 2012-04-28 17:39:37 -0400 | [diff] [blame] | 42 | |
| 43 | LOG = logging.getLogger(__name__) |
| 44 | |
Sean Dague | 86bd842 | 2013-12-20 09:56:44 -0500 | [diff] [blame] | 45 | CONF = config.CONF |
| 46 | |
Matthew Treinish | c1802bc | 2015-12-03 18:48:11 -0500 | [diff] [blame] | 47 | idempotent_id = decorators.idempotent_id |
| 48 | |
Jay Pipes | 051075a | 2012-04-28 17:39:37 -0400 | [diff] [blame] | 49 | |
Yaroslav Lobankov | da999f7 | 2015-06-30 20:32:55 +0300 | [diff] [blame] | 50 | def attr(**kwargs): |
liuchenhong | 00caec5 | 2015-07-19 22:40:28 +0800 | [diff] [blame] | 51 | """A decorator which applies the testtools attr decorator |
Chris Yeoh | 55530bb | 2013-02-08 16:04:27 +1030 | [diff] [blame] | 52 | |
Matthew Treinish | a74f5d4 | 2014-02-07 20:25:44 -0500 | [diff] [blame] | 53 | This decorator applies the testtools.testcase.attr if it is in the list of |
| 54 | attributes to testtools we want to apply. |
Attila Fazekas | b2902af | 2013-02-16 16:22:44 +0100 | [diff] [blame] | 55 | """ |
Chris Yeoh | 55530bb | 2013-02-08 16:04:27 +1030 | [diff] [blame] | 56 | |
| 57 | def decorator(f): |
Giulio Fidente | 4946a05 | 2013-05-14 12:23:51 +0200 | [diff] [blame] | 58 | if 'type' in kwargs and isinstance(kwargs['type'], str): |
| 59 | f = testtools.testcase.attr(kwargs['type'])(f) |
| 60 | elif 'type' in kwargs and isinstance(kwargs['type'], list): |
| 61 | for attr in kwargs['type']: |
| 62 | f = testtools.testcase.attr(attr)(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, |
Matthew Treinish | b66c94e | 2015-03-11 13:00:48 -0400 | [diff] [blame] | 81 | 'data_processing': CONF.service_available.sahara, |
| 82 | 'database': CONF.service_available.trove |
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 | |
Yaroslav Lobankov | da999f7 | 2015-06-30 20:32:55 +0300 | [diff] [blame] | 87 | def services(*args): |
Matthew Treinish | 3d8c732 | 2014-08-03 23:53:28 -0400 | [diff] [blame] | 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', |
ghanshyam | e4796f8 | 2016-04-13 15:49:22 +0900 | [diff] [blame] | 96 | 'data_processing', 'database'] |
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 | |
Yaroslav Lobankov | da999f7 | 2015-06-30 20:32:55 +0300 | [diff] [blame] | 117 | def stresstest(**kwargs): |
Marc Koderer | 32221b8e | 2013-08-23 13:57:50 +0200 | [diff] [blame] | 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 | |
Yaroslav Lobankov | da999f7 | 2015-06-30 20:32:55 +0300 | [diff] [blame] | 143 | def requires_ext(**kwargs): |
Matthew Treinish | e3d2614 | 2013-11-26 19:14:58 +0000 | [diff] [blame] | 144 | """A decorator to skip tests if an extension is not enabled |
| 145 | |
| 146 | @param extension |
| 147 | @param service |
| 148 | """ |
| 149 | def decorator(func): |
| 150 | @functools.wraps(func) |
| 151 | def wrapper(*func_args, **func_kwargs): |
| 152 | if not is_extension_enabled(kwargs['extension'], |
| 153 | kwargs['service']): |
| 154 | msg = "Skipped because %s extension: %s is not enabled" % ( |
| 155 | kwargs['service'], kwargs['extension']) |
| 156 | raise testtools.TestCase.skipException(msg) |
| 157 | return func(*func_args, **func_kwargs) |
| 158 | return wrapper |
| 159 | return decorator |
| 160 | |
| 161 | |
| 162 | def is_extension_enabled(extension_name, service): |
| 163 | """A function that will check the list of enabled extensions from config |
| 164 | |
| 165 | """ |
Matthew Treinish | e3d2614 | 2013-11-26 19:14:58 +0000 | [diff] [blame] | 166 | config_dict = { |
Matthew Treinish | bc0e03e | 2014-01-30 16:51:06 +0000 | [diff] [blame] | 167 | 'compute': CONF.compute_feature_enabled.api_extensions, |
Matthew Treinish | bc0e03e | 2014-01-30 16:51:06 +0000 | [diff] [blame] | 168 | 'volume': CONF.volume_feature_enabled.api_extensions, |
| 169 | 'network': CONF.network_feature_enabled.api_extensions, |
| 170 | 'object': CONF.object_storage_feature_enabled.discoverable_apis, |
Jane Zadorozhna | 121576d | 2015-06-23 12:57:13 +0300 | [diff] [blame] | 171 | 'identity': CONF.identity_feature_enabled.api_extensions |
Matthew Treinish | e3d2614 | 2013-11-26 19:14:58 +0000 | [diff] [blame] | 172 | } |
Simeon Monov | 5d7effe | 2014-07-16 07:32:38 +0300 | [diff] [blame] | 173 | if len(config_dict[service]) == 0: |
| 174 | return False |
Matthew Treinish | e3d2614 | 2013-11-26 19:14:58 +0000 | [diff] [blame] | 175 | if config_dict[service][0] == 'all': |
| 176 | return True |
| 177 | if extension_name in config_dict[service]: |
| 178 | return True |
| 179 | return False |
| 180 | |
Ian Wienand | 98c35f3 | 2013-07-23 20:34:23 +1000 | [diff] [blame] | 181 | |
Yair Fried | 9591412 | 2016-03-03 09:14:40 +0200 | [diff] [blame] | 182 | def is_scheduler_filter_enabled(filter_name): |
Yair Fried | ca5cfb5 | 2016-01-04 15:41:55 +0200 | [diff] [blame] | 183 | """Check the list of enabled compute scheduler filters from config. """ |
| 184 | |
| 185 | filters = CONF.compute_feature_enabled.scheduler_available_filters |
| 186 | if len(filters) == 0: |
| 187 | return False |
| 188 | if 'all' in filters: |
| 189 | return True |
| 190 | if filter_name in filters: |
| 191 | return True |
| 192 | return False |
| 193 | |
| 194 | |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 195 | at_exit_set = set() |
| 196 | |
| 197 | |
| 198 | def validate_tearDownClass(): |
| 199 | if at_exit_set: |
Sean Dague | eb1523b | 2014-03-10 10:17:44 -0400 | [diff] [blame] | 200 | LOG.error( |
| 201 | "tearDownClass does not call the super's " |
| 202 | "tearDownClass in these classes: \n" |
| 203 | + str(at_exit_set)) |
| 204 | |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 205 | |
| 206 | atexit.register(validate_tearDownClass) |
| 207 | |
Attila Fazekas | 5394332 | 2014-02-10 16:07:34 +0100 | [diff] [blame] | 208 | |
Matthew Treinish | 2474f41 | 2014-11-17 18:11:56 -0500 | [diff] [blame] | 209 | class BaseTestCase(testtools.testcase.WithAttributes, |
| 210 | testtools.TestCase): |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 211 | """The test base class defines Tempest framework for class level fixtures. |
Ken'ichi Ohmichi | 2e2ee19 | 2015-11-19 09:48:27 +0000 | [diff] [blame] | 212 | |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 213 | `setUpClass` and `tearDownClass` are defined here and cannot be overwritten |
| 214 | by subclasses (enforced via hacking rule T105). |
| 215 | |
| 216 | Set-up is split in a series of steps (setup stages), which can be |
| 217 | overwritten by test classes. Set-up stages are: |
| 218 | - skip_checks |
| 219 | - setup_credentials |
| 220 | - setup_clients |
| 221 | - resource_setup |
| 222 | |
| 223 | Tear-down is also split in a series of steps (teardown stages), which are |
| 224 | stacked for execution only if the corresponding setup stage had been |
| 225 | reached during the setup phase. Tear-down stages are: |
Andrea Frittoli (andreaf) | 17209bb | 2015-05-22 10:16:57 -0700 | [diff] [blame] | 226 | - clear_credentials (defined in the base test class) |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 227 | - resource_cleanup |
| 228 | """ |
Attila Fazekas | c43fec8 | 2013-04-09 23:17:52 +0200 | [diff] [blame] | 229 | |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 230 | setUpClassCalled = False |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 231 | _service = None |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 232 | |
Andrea Frittoli | b21de6c | 2015-02-06 20:12:38 +0000 | [diff] [blame] | 233 | # 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] | 234 | # at class setup time. Credential types can be 'primary', 'alt', 'admin' or |
| 235 | # a list of roles - the first element of the list being a label, and the |
| 236 | # rest the actual roles |
Andrea Frittoli | b21de6c | 2015-02-06 20:12:38 +0000 | [diff] [blame] | 237 | credentials = [] |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 238 | # Resources required to validate a server using ssh |
| 239 | validation_resources = {} |
Matthew Treinish | 9f756a0 | 2014-01-15 10:26:07 -0500 | [diff] [blame] | 240 | network_resources = {} |
| 241 | |
Sean Dague | 2ef32ac | 2014-06-09 11:32:23 -0400 | [diff] [blame] | 242 | # NOTE(sdague): log_format is defined inline here instead of using the oslo |
| 243 | # default because going through the config path recouples config to the |
| 244 | # stress tests too early, and depending on testr order will fail unit tests |
| 245 | log_format = ('%(asctime)s %(process)d %(levelname)-8s ' |
| 246 | '[%(name)s] %(message)s') |
| 247 | |
Ryota MIBU | 60687e5 | 2015-12-09 18:37:39 +0900 | [diff] [blame] | 248 | # Client manager class to use in this test case. |
| 249 | client_manager = clients.Manager |
| 250 | |
Sean Dague | 02620fd | 2016-03-02 15:52:51 -0500 | [diff] [blame] | 251 | # A way to adjust slow test classes |
| 252 | TIMEOUT_SCALING_FACTOR = 1 |
| 253 | |
Pavel Sedlák | 1053bd3 | 2013-04-16 16:47:40 +0200 | [diff] [blame] | 254 | @classmethod |
| 255 | def setUpClass(cls): |
Andrea Frittoli | 73ee247 | 2014-09-15 12:31:53 +0100 | [diff] [blame] | 256 | # It should never be overridden by descendants |
Pavel Sedlák | 1053bd3 | 2013-04-16 16:47:40 +0200 | [diff] [blame] | 257 | if hasattr(super(BaseTestCase, cls), 'setUpClass'): |
| 258 | super(BaseTestCase, cls).setUpClass() |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 259 | cls.setUpClassCalled = True |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 260 | # Stack of (name, callable) to be invoked in reverse order at teardown |
| 261 | cls.teardowns = [] |
| 262 | # All the configuration checks that may generate a skip |
| 263 | cls.skip_checks() |
Andrea Frittoli | 73ee247 | 2014-09-15 12:31:53 +0100 | [diff] [blame] | 264 | try: |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 265 | # Allocation of all required credentials and client managers |
Andrea Frittoli (andreaf) | 17209bb | 2015-05-22 10:16:57 -0700 | [diff] [blame] | 266 | cls.teardowns.append(('credentials', cls.clear_credentials)) |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 267 | cls.setup_credentials() |
| 268 | # Shortcuts to clients |
| 269 | cls.setup_clients() |
| 270 | # Additional class-wide test resources |
| 271 | cls.teardowns.append(('resources', cls.resource_cleanup)) |
Andrea Frittoli | 73ee247 | 2014-09-15 12:31:53 +0100 | [diff] [blame] | 272 | cls.resource_setup() |
| 273 | except Exception: |
| 274 | etype, value, trace = sys.exc_info() |
Matthew Treinish | ed2ad4f | 2014-12-23 15:18:32 -0500 | [diff] [blame] | 275 | LOG.info("%s raised in %s.setUpClass. Invoking tearDownClass." % ( |
| 276 | etype, cls.__name__)) |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 277 | cls.tearDownClass() |
Andrea Frittoli | 73ee247 | 2014-09-15 12:31:53 +0100 | [diff] [blame] | 278 | try: |
Matthew Treinish | 843227d | 2015-04-23 10:17:17 -0400 | [diff] [blame] | 279 | six.reraise(etype, value, trace) |
Andrea Frittoli | 73ee247 | 2014-09-15 12:31:53 +0100 | [diff] [blame] | 280 | finally: |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 281 | del trace # to avoid circular refs |
Pavel Sedlák | 1053bd3 | 2013-04-16 16:47:40 +0200 | [diff] [blame] | 282 | |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 283 | @classmethod |
| 284 | def tearDownClass(cls): |
Attila Fazekas | 5d27530 | 2013-08-29 12:35:12 +0200 | [diff] [blame] | 285 | at_exit_set.discard(cls) |
Andrea Frittoli | 73ee247 | 2014-09-15 12:31:53 +0100 | [diff] [blame] | 286 | # It should never be overridden by descendants |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 287 | if hasattr(super(BaseTestCase, cls), 'tearDownClass'): |
| 288 | super(BaseTestCase, cls).tearDownClass() |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 289 | # Save any existing exception, we always want to re-raise the original |
| 290 | # exception only |
| 291 | etype, value, trace = sys.exc_info() |
| 292 | # If there was no exception during setup we shall re-raise the first |
| 293 | # exception in teardown |
| 294 | re_raise = (etype is None) |
| 295 | while cls.teardowns: |
| 296 | name, teardown = cls.teardowns.pop() |
| 297 | # Catch any exception in tearDown so we can re-raise the original |
| 298 | # exception at the end |
| 299 | try: |
| 300 | teardown() |
| 301 | except Exception as te: |
| 302 | sys_exec_info = sys.exc_info() |
| 303 | tetype = sys_exec_info[0] |
| 304 | # TODO(andreaf): Till we have the ability to cleanup only |
| 305 | # resources that were successfully setup in resource_cleanup, |
| 306 | # log AttributeError as info instead of exception. |
| 307 | if tetype is AttributeError and name == 'resources': |
| 308 | LOG.info("tearDownClass of %s failed: %s" % (name, te)) |
| 309 | else: |
| 310 | LOG.exception("teardown of %s failed: %s" % (name, te)) |
| 311 | if not etype: |
| 312 | etype, value, trace = sys_exec_info |
Joshua White | bd76960 | 2016-02-02 09:30:11 -0800 | [diff] [blame] | 313 | # If exceptions were raised during teardown, and not before, re-raise |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 314 | # the first one |
| 315 | if re_raise and etype is not None: |
| 316 | try: |
Matthew Treinish | 843227d | 2015-04-23 10:17:17 -0400 | [diff] [blame] | 317 | six.reraise(etype, value, trace) |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 318 | finally: |
| 319 | del trace # to avoid circular refs |
Andrea Frittoli | 73ee247 | 2014-09-15 12:31:53 +0100 | [diff] [blame] | 320 | |
| 321 | @classmethod |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 322 | def skip_checks(cls): |
Ken'ichi Ohmichi | 2e2ee19 | 2015-11-19 09:48:27 +0000 | [diff] [blame] | 323 | """Class level skip checks. |
| 324 | |
| 325 | Subclasses verify in here all conditions that might prevent the |
| 326 | execution of the entire test class. |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 327 | Checks implemented here may not make use API calls, and should rely on |
| 328 | configuration alone. |
| 329 | In general skip checks that require an API call are discouraged. |
| 330 | If one is really needed it may be implemented either in the |
| 331 | resource_setup or at test level. |
| 332 | """ |
Andrea Frittoli (andreaf) | 32d0de1 | 2015-10-09 14:43:53 +0100 | [diff] [blame] | 333 | identity_version = cls.get_identity_version() |
| 334 | if 'admin' in cls.credentials and not credentials.is_admin_available( |
| 335 | identity_version=identity_version): |
Andrea Frittoli | b21de6c | 2015-02-06 20:12:38 +0000 | [diff] [blame] | 336 | msg = "Missing Identity Admin API credentials in configuration." |
| 337 | raise cls.skipException(msg) |
Andrea Frittoli (andreaf) | 32d0de1 | 2015-10-09 14:43:53 +0100 | [diff] [blame] | 338 | if 'alt' in cls.credentials and not credentials.is_alt_available( |
| 339 | identity_version=identity_version): |
Andrea Frittoli | b21de6c | 2015-02-06 20:12:38 +0000 | [diff] [blame] | 340 | msg = "Missing a 2nd set of API credentials in configuration." |
| 341 | raise cls.skipException(msg) |
Andrea Frittoli (andreaf) | 4160141 | 2015-05-12 16:39:03 +0100 | [diff] [blame] | 342 | if hasattr(cls, 'identity_version'): |
| 343 | if cls.identity_version == 'v2': |
| 344 | if not CONF.identity_feature_enabled.api_v2: |
| 345 | raise cls.skipException("Identity api v2 is not enabled") |
| 346 | elif cls.identity_version == 'v3': |
| 347 | if not CONF.identity_feature_enabled.api_v3: |
| 348 | raise cls.skipException("Identity api v3 is not enabled") |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 349 | |
| 350 | @classmethod |
| 351 | def setup_credentials(cls): |
edannon | 6cc6fbc | 2016-05-03 11:56:12 +0300 | [diff] [blame] | 352 | """Allocate credentials and create the client managers from them. |
Ken'ichi Ohmichi | 2e2ee19 | 2015-11-19 09:48:27 +0000 | [diff] [blame] | 353 | |
edannon | 6cc6fbc | 2016-05-03 11:56:12 +0300 | [diff] [blame] | 354 | For every element of credentials param function creates tenant/user, |
| 355 | Then it creates client manager for that credential. |
| 356 | |
| 357 | Network related tests must override this function with |
| 358 | set_network_resources() method, otherwise it will create |
| 359 | network resources(network resources are created in a later step). |
Andrea Frittoli | b21de6c | 2015-02-06 20:12:38 +0000 | [diff] [blame] | 360 | """ |
| 361 | for credentials_type in cls.credentials: |
| 362 | # This may raise an exception in case credentials are not available |
| 363 | # In that case we want to let the exception through and the test |
| 364 | # fail accordingly |
Andrea Frittoli (andreaf) | 825b2d3 | 2015-04-08 20:58:01 +0100 | [diff] [blame] | 365 | if isinstance(credentials_type, six.string_types): |
| 366 | manager = cls.get_client_manager( |
| 367 | credential_type=credentials_type) |
| 368 | setattr(cls, 'os_%s' % credentials_type, manager) |
| 369 | # Setup some common aliases |
| 370 | # TODO(andreaf) The aliases below are a temporary hack |
| 371 | # to avoid changing too much code in one patch. They should |
| 372 | # be removed eventually |
| 373 | if credentials_type == 'primary': |
| 374 | cls.os = cls.manager = cls.os_primary |
| 375 | if credentials_type == 'admin': |
| 376 | cls.os_adm = cls.admin_manager = cls.os_admin |
| 377 | if credentials_type == 'alt': |
| 378 | cls.alt_manager = cls.os_alt |
| 379 | elif isinstance(credentials_type, list): |
| 380 | manager = cls.get_client_manager(roles=credentials_type[1:], |
| 381 | force_new=True) |
| 382 | setattr(cls, 'os_roles_%s' % credentials_type[0], manager) |
Andrea Frittoli | a5ddd55 | 2014-08-19 18:30:00 +0100 | [diff] [blame] | 383 | |
| 384 | @classmethod |
| 385 | def setup_clients(cls): |
| 386 | """Create links to the clients into the test object.""" |
| 387 | # TODO(andreaf) There is a fair amount of code that could me moved from |
| 388 | # base / test classes in here. Ideally tests should be able to only |
| 389 | # specify which client is `client` and nothing else. |
Andrea Frittoli | 73ee247 | 2014-09-15 12:31:53 +0100 | [diff] [blame] | 390 | pass |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 391 | |
Emily Hugenbruch | 5bd4cbf | 2014-12-17 21:38:38 +0000 | [diff] [blame] | 392 | @classmethod |
| 393 | def resource_setup(cls): |
Ken'ichi Ohmichi | 2e2ee19 | 2015-11-19 09:48:27 +0000 | [diff] [blame] | 394 | """Class level resource setup for test cases.""" |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 395 | if hasattr(cls, "os"): |
| 396 | cls.validation_resources = vresources.create_validation_resources( |
| 397 | cls.os, cls.validation_resources) |
| 398 | else: |
zhangguoqing | 6c09664 | 2016-01-04 06:17:21 +0000 | [diff] [blame] | 399 | LOG.warning("Client manager not found, validation resources not" |
| 400 | " created") |
Emily Hugenbruch | 5bd4cbf | 2014-12-17 21:38:38 +0000 | [diff] [blame] | 401 | |
| 402 | @classmethod |
| 403 | def resource_cleanup(cls): |
| 404 | """Class level resource cleanup for test cases. |
Ken'ichi Ohmichi | 2e2ee19 | 2015-11-19 09:48:27 +0000 | [diff] [blame] | 405 | |
Emily Hugenbruch | 5bd4cbf | 2014-12-17 21:38:38 +0000 | [diff] [blame] | 406 | Resource cleanup must be able to handle the case of partially setup |
| 407 | resources, in case a failure during `resource_setup` should happen. |
| 408 | """ |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 409 | if cls.validation_resources: |
| 410 | if hasattr(cls, "os"): |
| 411 | vresources.clear_validation_resources(cls.os, |
| 412 | cls.validation_resources) |
| 413 | cls.validation_resources = {} |
| 414 | else: |
zhangguoqing | 6c09664 | 2016-01-04 06:17:21 +0000 | [diff] [blame] | 415 | LOG.warning("Client manager not found, validation resources " |
| 416 | "not deleted") |
Emily Hugenbruch | 5bd4cbf | 2014-12-17 21:38:38 +0000 | [diff] [blame] | 417 | |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 418 | def setUp(self): |
| 419 | super(BaseTestCase, self).setUp() |
| 420 | if not self.setUpClassCalled: |
| 421 | raise RuntimeError("setUpClass does not calls the super's" |
| 422 | "setUpClass in the " |
| 423 | + self.__class__.__name__) |
| 424 | at_exit_set.add(self.__class__) |
Matthew Treinish | 78561ad | 2013-07-26 11:41:56 -0400 | [diff] [blame] | 425 | test_timeout = os.environ.get('OS_TEST_TIMEOUT', 0) |
| 426 | try: |
Sean Dague | 02620fd | 2016-03-02 15:52:51 -0500 | [diff] [blame] | 427 | test_timeout = int(test_timeout) * self.TIMEOUT_SCALING_FACTOR |
Matthew Treinish | 78561ad | 2013-07-26 11:41:56 -0400 | [diff] [blame] | 428 | except ValueError: |
| 429 | test_timeout = 0 |
| 430 | if test_timeout > 0: |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 431 | self.useFixture(fixtures.Timeout(test_timeout, gentle=True)) |
Matthew Treinish | 78561ad | 2013-07-26 11:41:56 -0400 | [diff] [blame] | 432 | |
| 433 | if (os.environ.get('OS_STDOUT_CAPTURE') == 'True' or |
| 434 | os.environ.get('OS_STDOUT_CAPTURE') == '1'): |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 435 | stdout = self.useFixture(fixtures.StringStream('stdout')).stream |
| 436 | self.useFixture(fixtures.MonkeyPatch('sys.stdout', stdout)) |
Matthew Treinish | 78561ad | 2013-07-26 11:41:56 -0400 | [diff] [blame] | 437 | if (os.environ.get('OS_STDERR_CAPTURE') == 'True' or |
| 438 | os.environ.get('OS_STDERR_CAPTURE') == '1'): |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 439 | stderr = self.useFixture(fixtures.StringStream('stderr')).stream |
| 440 | self.useFixture(fixtures.MonkeyPatch('sys.stderr', stderr)) |
Attila Fazekas | 3138807 | 2013-08-15 08:58:07 +0200 | [diff] [blame] | 441 | if (os.environ.get('OS_LOG_CAPTURE') != 'False' and |
| 442 | os.environ.get('OS_LOG_CAPTURE') != '0'): |
Attila Fazekas | 3138807 | 2013-08-15 08:58:07 +0200 | [diff] [blame] | 443 | self.useFixture(fixtures.LoggerFixture(nuke_handlers=False, |
Sean Dague | 2ef32ac | 2014-06-09 11:32:23 -0400 | [diff] [blame] | 444 | format=self.log_format, |
Attila Fazekas | 90445be | 2013-10-24 16:46:03 +0200 | [diff] [blame] | 445 | level=None)) |
Matthew Treinish | 78561ad | 2013-07-26 11:41:56 -0400 | [diff] [blame] | 446 | |
Andrea Frittoli (andreaf) | 1f34241 | 2015-05-12 16:37:19 +0100 | [diff] [blame] | 447 | @property |
| 448 | def credentials_provider(self): |
| 449 | return self._get_credentials_provider() |
| 450 | |
Jamie Lennox | 1535017 | 2015-08-17 10:54:25 +1000 | [diff] [blame] | 451 | @property |
| 452 | def identity_utils(self): |
| 453 | """A client that abstracts v2 and v3 identity operations. |
| 454 | |
| 455 | This can be used for creating and tearing down projects in tests. It |
| 456 | should not be used for testing identity features. |
| 457 | """ |
| 458 | if CONF.identity.auth_version == 'v2': |
| 459 | client = self.os_admin.identity_client |
Daniel Mellado | 7aea534 | 2016-02-09 09:10:12 +0000 | [diff] [blame] | 460 | users_client = self.os_admin.users_client |
Daniel Mellado | b04da90 | 2015-11-20 17:43:12 +0100 | [diff] [blame] | 461 | project_client = self.os_admin.tenants_client |
Daniel Mellado | 6b16b92 | 2015-12-07 12:43:08 +0000 | [diff] [blame] | 462 | roles_client = self.os_admin.roles_client |
Daniel Mellado | 91a26b6 | 2016-02-11 11:13:04 +0000 | [diff] [blame] | 463 | domains_client = None |
Jamie Lennox | 1535017 | 2015-08-17 10:54:25 +1000 | [diff] [blame] | 464 | else: |
| 465 | client = self.os_admin.identity_v3_client |
Daniel Mellado | 7aea534 | 2016-02-09 09:10:12 +0000 | [diff] [blame] | 466 | users_client = self.os_admin.users_v3_client |
Arx Cruz | 24bcb88 | 2016-02-10 15:20:16 +0100 | [diff] [blame] | 467 | project_client = self.os_admin.projects_client |
| 468 | roles_client = self.os_admin.roles_v3_client |
Daniel Mellado | 91a26b6 | 2016-02-11 11:13:04 +0000 | [diff] [blame] | 469 | domains_client = self.os_admin.domains_client |
Jamie Lennox | 1535017 | 2015-08-17 10:54:25 +1000 | [diff] [blame] | 470 | |
| 471 | try: |
| 472 | domain = client.auth_provider.credentials.project_domain_name |
| 473 | except AttributeError: |
| 474 | domain = 'Default' |
| 475 | |
Daniel Mellado | b04da90 | 2015-11-20 17:43:12 +0100 | [diff] [blame] | 476 | return cred_client.get_creds_client(client, project_client, |
Daniel Mellado | 82c83a5 | 2015-12-09 15:16:49 +0000 | [diff] [blame] | 477 | users_client, |
Daniel Mellado | 7aea534 | 2016-02-09 09:10:12 +0000 | [diff] [blame] | 478 | roles_client, |
Daniel Mellado | 91a26b6 | 2016-02-11 11:13:04 +0000 | [diff] [blame] | 479 | domains_client, |
Daniel Mellado | b04da90 | 2015-11-20 17:43:12 +0100 | [diff] [blame] | 480 | project_domain_name=domain) |
Jamie Lennox | 1535017 | 2015-08-17 10:54:25 +1000 | [diff] [blame] | 481 | |
Andrea Frittoli (andreaf) | 1f34241 | 2015-05-12 16:37:19 +0100 | [diff] [blame] | 482 | @classmethod |
Andrea Frittoli (andreaf) | 32d0de1 | 2015-10-09 14:43:53 +0100 | [diff] [blame] | 483 | def get_identity_version(cls): |
| 484 | """Returns the identity version used by the test class""" |
| 485 | identity_version = getattr(cls, 'identity_version', None) |
| 486 | return identity_version or CONF.identity.auth_version |
| 487 | |
| 488 | @classmethod |
Andrea Frittoli (andreaf) | 1f34241 | 2015-05-12 16:37:19 +0100 | [diff] [blame] | 489 | def _get_credentials_provider(cls): |
| 490 | """Returns a credentials provider |
| 491 | |
| 492 | If no credential provider exists yet creates one. |
| 493 | It uses self.identity_version if defined, or the configuration value |
| 494 | """ |
| 495 | if (not hasattr(cls, '_creds_provider') or not cls._creds_provider or |
| 496 | not cls._creds_provider.name == cls.__name__): |
| 497 | force_tenant_isolation = getattr(cls, 'force_tenant_isolation', |
| 498 | False) |
Andrea Frittoli (andreaf) | 1f34241 | 2015-05-12 16:37:19 +0100 | [diff] [blame] | 499 | |
Andrea Frittoli (andreaf) | 17209bb | 2015-05-22 10:16:57 -0700 | [diff] [blame] | 500 | cls._creds_provider = credentials.get_credentials_provider( |
Andrea Frittoli (andreaf) | 1f34241 | 2015-05-12 16:37:19 +0100 | [diff] [blame] | 501 | name=cls.__name__, network_resources=cls.network_resources, |
| 502 | force_tenant_isolation=force_tenant_isolation, |
Andrea Frittoli (andreaf) | 32d0de1 | 2015-10-09 14:43:53 +0100 | [diff] [blame] | 503 | identity_version=cls.get_identity_version()) |
Andrea Frittoli (andreaf) | 1f34241 | 2015-05-12 16:37:19 +0100 | [diff] [blame] | 504 | return cls._creds_provider |
| 505 | |
Matthew Treinish | 3e04685 | 2013-07-23 16:00:24 -0400 | [diff] [blame] | 506 | @classmethod |
Andrea Frittoli (andreaf) | 4160141 | 2015-05-12 16:39:03 +0100 | [diff] [blame] | 507 | def get_client_manager(cls, credential_type=None, roles=None, |
| 508 | force_new=None): |
Andrea Frittoli (andreaf) | 737fac9 | 2015-05-12 16:14:35 +0100 | [diff] [blame] | 509 | """Returns an OpenStack client manager |
| 510 | |
| 511 | Returns an OpenStack client manager based on either credential_type |
| 512 | or a list of roles. If neither is specified, it defaults to |
| 513 | credential_type 'primary' |
Andrea Frittoli (andreaf) | 737fac9 | 2015-05-12 16:14:35 +0100 | [diff] [blame] | 514 | :param credential_type: string - primary, alt or admin |
| 515 | :param roles: list of roles |
| 516 | |
lei zhang | dd552b2 | 2015-11-25 20:41:48 +0800 | [diff] [blame] | 517 | :returns: the created client manager |
Andrea Frittoli (andreaf) | 737fac9 | 2015-05-12 16:14:35 +0100 | [diff] [blame] | 518 | :raises skipException: if the requested credentials are not available |
Ryan Hsu | 6c4bb3d | 2013-10-21 21:22:50 -0700 | [diff] [blame] | 519 | """ |
Andrea Frittoli (andreaf) | 737fac9 | 2015-05-12 16:14:35 +0100 | [diff] [blame] | 520 | if all([roles, credential_type]): |
| 521 | msg = "Cannot get credentials by type and roles at the same time" |
| 522 | raise ValueError(msg) |
| 523 | if not any([roles, credential_type]): |
| 524 | credential_type = 'primary' |
Andrea Frittoli (andreaf) | 1f34241 | 2015-05-12 16:37:19 +0100 | [diff] [blame] | 525 | cred_provider = cls._get_credentials_provider() |
Andrea Frittoli (andreaf) | 737fac9 | 2015-05-12 16:14:35 +0100 | [diff] [blame] | 526 | if roles: |
| 527 | for role in roles: |
Andrea Frittoli (andreaf) | 1f34241 | 2015-05-12 16:37:19 +0100 | [diff] [blame] | 528 | if not cred_provider.is_role_available(role): |
Andrea Frittoli (andreaf) | 737fac9 | 2015-05-12 16:14:35 +0100 | [diff] [blame] | 529 | skip_msg = ( |
| 530 | "%s skipped because the configured credential provider" |
| 531 | " is not able to provide credentials with the %s role " |
| 532 | "assigned." % (cls.__name__, role)) |
| 533 | raise cls.skipException(skip_msg) |
| 534 | params = dict(roles=roles) |
| 535 | if force_new is not None: |
| 536 | params.update(force_new=force_new) |
Andrea Frittoli (andreaf) | 1f34241 | 2015-05-12 16:37:19 +0100 | [diff] [blame] | 537 | creds = cred_provider.get_creds_by_roles(**params) |
Andrea Frittoli | b21de6c | 2015-02-06 20:12:38 +0000 | [diff] [blame] | 538 | else: |
Andrea Frittoli (andreaf) | 737fac9 | 2015-05-12 16:14:35 +0100 | [diff] [blame] | 539 | credentials_method = 'get_%s_creds' % credential_type |
Andrea Frittoli (andreaf) | 1f34241 | 2015-05-12 16:37:19 +0100 | [diff] [blame] | 540 | if hasattr(cred_provider, credentials_method): |
| 541 | creds = getattr(cred_provider, credentials_method)() |
Andrea Frittoli (andreaf) | 737fac9 | 2015-05-12 16:14:35 +0100 | [diff] [blame] | 542 | else: |
| 543 | raise exceptions.InvalidCredentials( |
| 544 | "Invalid credentials type %s" % credential_type) |
Ghanshyam | 05049dd | 2016-02-12 17:44:48 +0900 | [diff] [blame] | 545 | return cls.client_manager(credentials=creds, service=cls._service) |
Ryan Hsu | 6c4bb3d | 2013-10-21 21:22:50 -0700 | [diff] [blame] | 546 | |
| 547 | @classmethod |
Andrea Frittoli (andreaf) | 17209bb | 2015-05-22 10:16:57 -0700 | [diff] [blame] | 548 | def clear_credentials(cls): |
Ken'ichi Ohmichi | 2e2ee19 | 2015-11-19 09:48:27 +0000 | [diff] [blame] | 549 | """Clears creds if set""" |
Attila Fazekas | 5b0d926 | 2015-05-20 10:17:39 +0200 | [diff] [blame] | 550 | if hasattr(cls, '_creds_provider'): |
Andrea Frittoli (andreaf) | 17209bb | 2015-05-22 10:16:57 -0700 | [diff] [blame] | 551 | cls._creds_provider.clear_creds() |
Ryan Hsu | 6c4bb3d | 2013-10-21 21:22:50 -0700 | [diff] [blame] | 552 | |
| 553 | @classmethod |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 554 | def set_validation_resources(cls, keypair=None, floating_ip=None, |
| 555 | security_group=None, |
| 556 | security_group_rules=None): |
| 557 | """Specify which ssh server validation resources should be created. |
Ken'ichi Ohmichi | 2e2ee19 | 2015-11-19 09:48:27 +0000 | [diff] [blame] | 558 | |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 559 | Each of the argument must be set to either None, True or False, with |
| 560 | None - use default from config (security groups and security group |
| 561 | rules get created when set to None) |
| 562 | False - Do not create the validation resource |
| 563 | True - create the validation resource |
| 564 | |
| 565 | @param keypair |
| 566 | @param security_group |
| 567 | @param security_group_rules |
| 568 | @param floating_ip |
| 569 | """ |
Matthew Treinish | e5cca00 | 2015-05-11 15:36:50 -0400 | [diff] [blame] | 570 | if not CONF.validation.run_validation: |
| 571 | return |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 572 | if keypair is None: |
| 573 | if CONF.validation.auth_method.lower() == "keypair": |
| 574 | keypair = True |
| 575 | else: |
| 576 | keypair = False |
| 577 | if floating_ip is None: |
| 578 | if CONF.validation.connect_method.lower() == "floating": |
| 579 | floating_ip = True |
| 580 | else: |
| 581 | floating_ip = False |
| 582 | if security_group is None: |
Brandon Palm | c6cc91d | 2015-08-19 13:20:21 -0500 | [diff] [blame] | 583 | security_group = CONF.validation.security_group |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 584 | if security_group_rules is None: |
Brandon Palm | c6cc91d | 2015-08-19 13:20:21 -0500 | [diff] [blame] | 585 | security_group_rules = CONF.validation.security_group_rules |
| 586 | |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 587 | if not cls.validation_resources: |
| 588 | cls.validation_resources = { |
| 589 | 'keypair': keypair, |
| 590 | 'security_group': security_group, |
| 591 | 'security_group_rules': security_group_rules, |
| 592 | 'floating_ip': floating_ip} |
| 593 | |
| 594 | @classmethod |
Andrea Frittoli | 7d5ed59 | 2015-02-10 01:10:23 +0000 | [diff] [blame] | 595 | def set_network_resources(cls, network=False, router=False, subnet=False, |
Matthew Treinish | 9f756a0 | 2014-01-15 10:26:07 -0500 | [diff] [blame] | 596 | dhcp=False): |
| 597 | """Specify which network resources should be created |
| 598 | |
| 599 | @param network |
| 600 | @param router |
| 601 | @param subnet |
| 602 | @param dhcp |
| 603 | """ |
Salvatore Orlando | 5a33724 | 2014-01-15 22:49:22 +0000 | [diff] [blame] | 604 | # network resources should be set only once from callers |
| 605 | # in order to ensure that even if it's called multiple times in |
| 606 | # a chain of overloaded methods, the attribute is set only |
| 607 | # in the leaf class |
Andrea Frittoli | 7d5ed59 | 2015-02-10 01:10:23 +0000 | [diff] [blame] | 608 | if not cls.network_resources: |
| 609 | cls.network_resources = { |
Salvatore Orlando | 5a33724 | 2014-01-15 22:49:22 +0000 | [diff] [blame] | 610 | 'network': network, |
| 611 | 'router': router, |
| 612 | 'subnet': subnet, |
| 613 | 'dhcp': dhcp} |
Matthew Treinish | 9f756a0 | 2014-01-15 10:26:07 -0500 | [diff] [blame] | 614 | |
Rohan Kanade | 9ce97df | 2013-12-10 18:59:35 +0530 | [diff] [blame] | 615 | @classmethod |
Ryan Rossiter | 9228bf7 | 2016-02-25 03:06:12 +0000 | [diff] [blame] | 616 | def get_tenant_network(cls, credentials_type='primary'): |
Rohan Kanade | 9ce97df | 2013-12-10 18:59:35 +0530 | [diff] [blame] | 617 | """Get the network to be used in testing |
| 618 | |
Ryan Rossiter | 9228bf7 | 2016-02-25 03:06:12 +0000 | [diff] [blame] | 619 | :param credentials_type: The type of credentials for which to get the |
| 620 | tenant network |
| 621 | |
Rohan Kanade | 9ce97df | 2013-12-10 18:59:35 +0530 | [diff] [blame] | 622 | :return: network dict including 'id' and 'name' |
| 623 | """ |
Ryan Rossiter | 9228bf7 | 2016-02-25 03:06:12 +0000 | [diff] [blame] | 624 | # Get a manager for the given credentials_type, but at least |
| 625 | # always fall back on getting the manager for primary credentials |
| 626 | if isinstance(credentials_type, six.string_types): |
| 627 | manager = cls.get_client_manager(credential_type=credentials_type) |
| 628 | elif isinstance(credentials_type, list): |
| 629 | manager = cls.get_client_manager(roles=credentials_type[1:]) |
| 630 | else: |
| 631 | manager = cls.get_client_manager() |
| 632 | |
Andrea Frittoli (andreaf) | 17209bb | 2015-05-22 10:16:57 -0700 | [diff] [blame] | 633 | # Make sure cred_provider exists and get a network client |
Ryan Rossiter | 9228bf7 | 2016-02-25 03:06:12 +0000 | [diff] [blame] | 634 | networks_client = manager.compute_networks_client |
Andrea Frittoli (andreaf) | 1f34241 | 2015-05-12 16:37:19 +0100 | [diff] [blame] | 635 | cred_provider = cls._get_credentials_provider() |
Andrea Frittoli | 700711e | 2015-04-02 11:39:38 +0100 | [diff] [blame] | 636 | # 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] | 637 | # network configured in fixed_network_name, even if they can use it |
Andrea Frittoli | 700711e | 2015-04-02 11:39:38 +0100 | [diff] [blame] | 638 | # for their servers, so using an admin network client to validate |
| 639 | # the network name |
| 640 | if (not CONF.service_available.neutron and |
Andrea Frittoli (andreaf) | 32d0de1 | 2015-10-09 14:43:53 +0100 | [diff] [blame] | 641 | credentials.is_admin_available( |
| 642 | identity_version=cls.get_identity_version())): |
Andrea Frittoli (andreaf) | 1f34241 | 2015-05-12 16:37:19 +0100 | [diff] [blame] | 643 | admin_creds = cred_provider.get_admin_creds() |
Ghanshyam | 05049dd | 2016-02-12 17:44:48 +0900 | [diff] [blame] | 644 | admin_manager = clients.Manager(admin_creds) |
John Warren | 9487a18 | 2015-09-14 18:12:56 -0400 | [diff] [blame] | 645 | networks_client = admin_manager.compute_networks_client |
Andrea Frittoli (andreaf) | 940f8c6 | 2015-10-30 16:39:24 +0900 | [diff] [blame] | 646 | return fixed_network.get_tenant_network( |
| 647 | cred_provider, networks_client, CONF.compute.fixed_network_name) |
Rohan Kanade | 9ce97df | 2013-12-10 18:59:35 +0530 | [diff] [blame] | 648 | |
Mark Maglana | 5885eb3 | 2014-02-28 10:57:34 -0800 | [diff] [blame] | 649 | def assertEmpty(self, list, msg=None): |
| 650 | self.assertTrue(len(list) == 0, msg) |
| 651 | |
| 652 | def assertNotEmpty(self, list, msg=None): |
| 653 | self.assertTrue(len(list) > 0, msg) |
| 654 | |
Attila Fazekas | dc21642 | 2013-01-29 15:12:14 +0100 | [diff] [blame] | 655 | |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 656 | class NegativeAutoTest(BaseTestCase): |
| 657 | |
| 658 | _resources = {} |
| 659 | |
| 660 | @classmethod |
| 661 | def setUpClass(cls): |
| 662 | super(NegativeAutoTest, cls).setUpClass() |
Andrea Frittoli | b21de6c | 2015-02-06 20:12:38 +0000 | [diff] [blame] | 663 | os = cls.get_client_manager(credential_type='primary') |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 664 | cls.client = os.negative_client |
| 665 | |
| 666 | @staticmethod |
Marc Koderer | 674c8fc | 2014-03-17 09:45:04 +0100 | [diff] [blame] | 667 | def load_tests(*args): |
Ken'ichi Ohmichi | 2e2ee19 | 2015-11-19 09:48:27 +0000 | [diff] [blame] | 668 | """Wrapper for testscenarios |
| 669 | |
| 670 | To set the mandatory scenarios variable only in case a real test |
| 671 | loader is in place. Will be automatically called in case the variable |
| 672 | "load_tests" is set. |
Marc Koderer | 674c8fc | 2014-03-17 09:45:04 +0100 | [diff] [blame] | 673 | """ |
| 674 | if getattr(args[0], 'suiteClass', None) is not None: |
| 675 | loader, standard_tests, pattern = args |
| 676 | else: |
| 677 | standard_tests, module, loader = args |
| 678 | for test in testtools.iterate_tests(standard_tests): |
Marc Koderer | 4f44d72 | 2014-08-07 14:04:58 +0200 | [diff] [blame] | 679 | schema = getattr(test, '_schema', None) |
Marc Koderer | 3dd3105 | 2014-11-27 09:31:00 +0100 | [diff] [blame] | 680 | if schema is not None: |
Marc Koderer | 4f44d72 | 2014-08-07 14:04:58 +0200 | [diff] [blame] | 681 | setattr(test, 'scenarios', |
| 682 | NegativeAutoTest.generate_scenario(schema)) |
Marc Koderer | 674c8fc | 2014-03-17 09:45:04 +0100 | [diff] [blame] | 683 | return testscenarios.load_tests_apply_scenarios(*args) |
| 684 | |
| 685 | @staticmethod |
Marc Koderer | 4f44d72 | 2014-08-07 14:04:58 +0200 | [diff] [blame] | 686 | def generate_scenario(description): |
Ken'ichi Ohmichi | 2e2ee19 | 2015-11-19 09:48:27 +0000 | [diff] [blame] | 687 | """Generates the test scenario list for a given description. |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 688 | |
Marc Koderer | 4f44d72 | 2014-08-07 14:04:58 +0200 | [diff] [blame] | 689 | :param description: A file or dictionary with the following entries: |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 690 | name (required) name for the api |
| 691 | http-method (required) one of HEAD,GET,PUT,POST,PATCH,DELETE |
| 692 | url (required) the url to be appended to the catalog url with '%s' |
| 693 | for each resource mentioned |
| 694 | resources: (optional) A list of resource names such as "server", |
| 695 | "flavor", etc. with an element for each '%s' in the url. This |
| 696 | method will call self.get_resource for each element when |
| 697 | constructing the positive test case template so negative |
| 698 | subclasses are expected to return valid resource ids when |
| 699 | appropriate. |
| 700 | json-schema (optional) A valid json schema that will be used to |
| 701 | create invalid data for the api calls. For "GET" and "HEAD", |
| 702 | the data is used to generate query strings appended to the url, |
| 703 | otherwise for the body of the http call. |
| 704 | """ |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 705 | LOG.debug(description) |
Marc Koderer | 674c8fc | 2014-03-17 09:45:04 +0100 | [diff] [blame] | 706 | generator = importutils.import_class( |
| 707 | CONF.negative.test_generator)() |
Marc Koderer | 6ee82dc | 2014-02-17 10:26:29 +0100 | [diff] [blame] | 708 | generator.validate_schema(description) |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 709 | schema = description.get("json-schema", None) |
| 710 | resources = description.get("resources", []) |
| 711 | scenario_list = [] |
Marc Koderer | 424c84f | 2014-02-06 17:02:19 +0100 | [diff] [blame] | 712 | expected_result = None |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 713 | for resource in resources: |
Marc Koderer | 424c84f | 2014-02-06 17:02:19 +0100 | [diff] [blame] | 714 | if isinstance(resource, dict): |
| 715 | expected_result = resource['expected_result'] |
| 716 | resource = resource['name'] |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 717 | LOG.debug("Add resource to test %s" % resource) |
| 718 | scn_name = "inv_res_%s" % (resource) |
Ken'ichi Ohmichi | d079c89 | 2016-04-19 11:23:36 -0700 | [diff] [blame] | 719 | scenario_list.append((scn_name, { |
| 720 | "resource": (resource, data_utils.rand_uuid()), |
| 721 | "expected_result": expected_result |
| 722 | })) |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 723 | if schema is not None: |
Marc Koderer | f07f5d1 | 2014-09-01 09:47:23 +0200 | [diff] [blame] | 724 | for scenario in generator.generate_scenarios(schema): |
| 725 | scenario_list.append((scenario['_negtest_name'], |
| 726 | scenario)) |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 727 | LOG.debug(scenario_list) |
| 728 | return scenario_list |
| 729 | |
Marc Koderer | 4f44d72 | 2014-08-07 14:04:58 +0200 | [diff] [blame] | 730 | def execute(self, description): |
Ken'ichi Ohmichi | 2e2ee19 | 2015-11-19 09:48:27 +0000 | [diff] [blame] | 731 | """Execute a http call |
| 732 | |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 733 | Execute a http call on an api that are expected to |
| 734 | result in client errors. First it uses invalid resources that are part |
| 735 | of the url, and then invalid data for queries and http request bodies. |
| 736 | |
Marc Koderer | 4f44d72 | 2014-08-07 14:04:58 +0200 | [diff] [blame] | 737 | :param description: A json file or dictionary with the following |
| 738 | entries: |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 739 | name (required) name for the api |
| 740 | http-method (required) one of HEAD,GET,PUT,POST,PATCH,DELETE |
| 741 | url (required) the url to be appended to the catalog url with '%s' |
| 742 | for each resource mentioned |
| 743 | resources: (optional) A list of resource names such as "server", |
| 744 | "flavor", etc. with an element for each '%s' in the url. This |
| 745 | method will call self.get_resource for each element when |
| 746 | constructing the positive test case template so negative |
| 747 | subclasses are expected to return valid resource ids when |
| 748 | appropriate. |
| 749 | json-schema (optional) A valid json schema that will be used to |
| 750 | create invalid data for the api calls. For "GET" and "HEAD", |
| 751 | the data is used to generate query strings appended to the url, |
| 752 | otherwise for the body of the http call. |
| 753 | |
| 754 | """ |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 755 | LOG.info("Executing %s" % description["name"]) |
| 756 | LOG.debug(description) |
Marc Koderer | f07f5d1 | 2014-09-01 09:47:23 +0200 | [diff] [blame] | 757 | generator = importutils.import_class( |
| 758 | CONF.negative.test_generator)() |
| 759 | schema = description.get("json-schema", None) |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 760 | method = description["http-method"] |
| 761 | url = description["url"] |
Marc Koderer | f07f5d1 | 2014-09-01 09:47:23 +0200 | [diff] [blame] | 762 | expected_result = None |
| 763 | if "default_result_code" in description: |
| 764 | expected_result = description["default_result_code"] |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 765 | |
| 766 | resources = [self.get_resource(r) for |
| 767 | r in description.get("resources", [])] |
| 768 | |
| 769 | if hasattr(self, "resource"): |
| 770 | # Note(mkoderer): The resources list already contains an invalid |
| 771 | # entry (see get_resource). |
| 772 | # We just send a valid json-schema with it |
Marc Koderer | 6ee82dc | 2014-02-17 10:26:29 +0100 | [diff] [blame] | 773 | valid_schema = None |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 774 | if schema: |
Marc Koderer | 6ee82dc | 2014-02-17 10:26:29 +0100 | [diff] [blame] | 775 | valid_schema = \ |
| 776 | valid.ValidTestGenerator().generate_valid(schema) |
| 777 | new_url, body = self._http_arguments(valid_schema, url, method) |
Marc Koderer | f07f5d1 | 2014-09-01 09:47:23 +0200 | [diff] [blame] | 778 | elif hasattr(self, "_negtest_name"): |
| 779 | schema_under_test = \ |
| 780 | valid.ValidTestGenerator().generate_valid(schema) |
| 781 | local_expected_result = \ |
| 782 | generator.generate_payload(self, schema_under_test) |
| 783 | if local_expected_result is not None: |
| 784 | expected_result = local_expected_result |
| 785 | new_url, body = \ |
| 786 | self._http_arguments(schema_under_test, url, method) |
Marc Koderer | 1c247c8 | 2014-03-20 08:24:38 +0100 | [diff] [blame] | 787 | else: |
| 788 | raise Exception("testscenarios are not active. Please make sure " |
| 789 | "that your test runner supports the load_tests " |
| 790 | "mechanism") |
Marc Koderer | 424c84f | 2014-02-06 17:02:19 +0100 | [diff] [blame] | 791 | |
Marc Koderer | f857fda | 2014-03-05 15:58:00 +0100 | [diff] [blame] | 792 | if "admin_client" in description and description["admin_client"]: |
Andrea Frittoli (andreaf) | 32d0de1 | 2015-10-09 14:43:53 +0100 | [diff] [blame] | 793 | if not credentials.is_admin_available( |
| 794 | identity_version=self.get_identity_version()): |
David Kranz | afecec0 | 2015-03-23 14:27:15 -0400 | [diff] [blame] | 795 | msg = ("Missing Identity Admin API credentials in" |
| 796 | "configuration.") |
| 797 | raise self.skipException(msg) |
Andrea Frittoli (andreaf) | 1f34241 | 2015-05-12 16:37:19 +0100 | [diff] [blame] | 798 | creds = self.credentials_provider.get_admin_creds() |
David Kranz | afecec0 | 2015-03-23 14:27:15 -0400 | [diff] [blame] | 799 | os_adm = clients.Manager(credentials=creds) |
| 800 | client = os_adm.negative_client |
Marc Koderer | f857fda | 2014-03-05 15:58:00 +0100 | [diff] [blame] | 801 | else: |
| 802 | client = self.client |
| 803 | resp, resp_body = client.send_request(method, new_url, |
| 804 | resources, body=body) |
Marc Koderer | f07f5d1 | 2014-09-01 09:47:23 +0200 | [diff] [blame] | 805 | self._check_negative_response(expected_result, resp.status, resp_body) |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 806 | |
| 807 | def _http_arguments(self, json_dict, url, method): |
| 808 | LOG.debug("dict: %s url: %s method: %s" % (json_dict, url, method)) |
| 809 | if not json_dict: |
| 810 | return url, None |
| 811 | elif method in ["GET", "HEAD", "PUT", "DELETE"]: |
Harshada Mangesh Kakad | 71f6b97 | 2015-12-22 09:46:48 -0800 | [diff] [blame] | 812 | return "%s?%s" % (url, urllib.parse.urlencode(json_dict)), None |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 813 | else: |
| 814 | return url, json.dumps(json_dict) |
| 815 | |
Marc Koderer | f07f5d1 | 2014-09-01 09:47:23 +0200 | [diff] [blame] | 816 | def _check_negative_response(self, expected_result, result, body): |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 817 | self.assertTrue(result >= 400 and result < 500 and result != 413, |
| 818 | "Expected client error, got %s:%s" % |
| 819 | (result, body)) |
| 820 | self.assertTrue(expected_result is None or expected_result == result, |
| 821 | "Expected %s, got %s:%s" % |
| 822 | (expected_result, result, body)) |
| 823 | |
| 824 | @classmethod |
| 825 | def set_resource(cls, name, resource): |
Joshua White | bd76960 | 2016-02-02 09:30:11 -0800 | [diff] [blame] | 826 | """Register a resource for a test |
Ken'ichi Ohmichi | 2e2ee19 | 2015-11-19 09:48:27 +0000 | [diff] [blame] | 827 | |
Joshua White | bd76960 | 2016-02-02 09:30:11 -0800 | [diff] [blame] | 828 | This function can be used in setUpClass context to register a resource |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 829 | for a test. |
| 830 | |
| 831 | :param name: The name of the kind of resource such as "flavor", "role", |
| 832 | etc. |
| 833 | :resource: The id of the resource |
| 834 | """ |
| 835 | cls._resources[name] = resource |
| 836 | |
| 837 | def get_resource(self, name): |
Ken'ichi Ohmichi | 2e2ee19 | 2015-11-19 09:48:27 +0000 | [diff] [blame] | 838 | """Return a valid uuid for a type of resource. |
| 839 | |
| 840 | If a real resource is needed as part of a url then this method should |
| 841 | return one. Otherwise it can return None. |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 842 | |
| 843 | :param name: The name of the kind of resource such as "flavor", "role", |
| 844 | etc. |
| 845 | """ |
Marc Koderer | 424c84f | 2014-02-06 17:02:19 +0100 | [diff] [blame] | 846 | if isinstance(name, dict): |
| 847 | name = name['name'] |
Marc Koderer | 24eb89c | 2014-01-31 11:23:33 +0100 | [diff] [blame] | 848 | if hasattr(self, "resource") and self.resource[0] == name: |
| 849 | LOG.debug("Return invalid resource (%s) value: %s" % |
| 850 | (self.resource[0], self.resource[1])) |
| 851 | return self.resource[1] |
| 852 | if name in self._resources: |
| 853 | return self._resources[name] |
| 854 | return None |
| 855 | |
| 856 | |
Marc Koderer | b2978da | 2014-03-26 13:45:43 +0100 | [diff] [blame] | 857 | def SimpleNegativeAutoTest(klass): |
Ken'ichi Ohmichi | 2e2ee19 | 2015-11-19 09:48:27 +0000 | [diff] [blame] | 858 | """This decorator registers a test function on basis of the class name.""" |
Sean Dague | 5e1bcd9 | 2015-04-27 09:08:36 -0400 | [diff] [blame] | 859 | @attr(type=['negative']) |
Marc Koderer | b2978da | 2014-03-26 13:45:43 +0100 | [diff] [blame] | 860 | def generic_test(self): |
Marc Koderer | 4f44d72 | 2014-08-07 14:04:58 +0200 | [diff] [blame] | 861 | if hasattr(self, '_schema'): |
| 862 | self.execute(self._schema) |
Marc Koderer | b2978da | 2014-03-26 13:45:43 +0100 | [diff] [blame] | 863 | |
| 864 | cn = klass.__name__ |
| 865 | cn = cn.replace('JSON', '') |
| 866 | cn = cn.replace('Test', '') |
| 867 | # NOTE(mkoderer): replaces uppercase chars inside the class name with '_' |
| 868 | lower_cn = re.sub('(?<!^)(?=[A-Z])', '_', cn).lower() |
| 869 | func_name = 'test_%s' % lower_cn |
| 870 | setattr(klass, func_name, generic_test) |
| 871 | return klass |
| 872 | |
| 873 | |
Sean Dague | 35a7caf | 2013-05-10 10:38:22 -0400 | [diff] [blame] | 874 | def call_until_true(func, duration, sleep_for): |
Ken'ichi Ohmichi | 2e2ee19 | 2015-11-19 09:48:27 +0000 | [diff] [blame] | 875 | """Call the given function until it returns True (and return True) |
| 876 | |
| 877 | or until the specified duration (in seconds) elapses (and return False). |
Sean Dague | 35a7caf | 2013-05-10 10:38:22 -0400 | [diff] [blame] | 878 | |
| 879 | :param func: A zero argument callable that returns True on success. |
| 880 | :param duration: The number of seconds for which to attempt a |
| 881 | successful call of the function. |
| 882 | :param sleep_for: The number of seconds to sleep after an unsuccessful |
| 883 | invocation of the function. |
| 884 | """ |
| 885 | now = time.time() |
| 886 | timeout = now + duration |
| 887 | while now < timeout: |
| 888 | if func(): |
| 889 | return True |
Sean Dague | 35a7caf | 2013-05-10 10:38:22 -0400 | [diff] [blame] | 890 | time.sleep(sleep_for) |
| 891 | now = time.time() |
| 892 | return False |