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