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 |
Jay Pipes | 051075a | 2012-04-28 17:39:37 -0400 | [diff] [blame] | 19 | import time |
| 20 | |
Matthew Treinish | 78561ad | 2013-07-26 11:41:56 -0400 | [diff] [blame] | 21 | import fixtures |
Chris Yeoh | 55530bb | 2013-02-08 16:04:27 +1030 | [diff] [blame] | 22 | import nose.plugins.attrib |
Attila Fazekas | dc21642 | 2013-01-29 15:12:14 +0100 | [diff] [blame] | 23 | import testresources |
ivan-zhu | 1feeb38 | 2013-01-24 10:14:39 +0800 | [diff] [blame] | 24 | import testtools |
Jay Pipes | 051075a | 2012-04-28 17:39:37 -0400 | [diff] [blame] | 25 | |
Matthew Treinish | 3e04685 | 2013-07-23 16:00:24 -0400 | [diff] [blame] | 26 | from tempest import clients |
Ryan Hsu | 6c4bb3d | 2013-10-21 21:22:50 -0700 | [diff] [blame] | 27 | from tempest.common import isolated_creds |
Attila Fazekas | dc21642 | 2013-01-29 15:12:14 +0100 | [diff] [blame] | 28 | from tempest import config |
Matthew Treinish | 16c4379 | 2013-09-09 19:55:23 +0000 | [diff] [blame] | 29 | from tempest import exceptions |
Matthew Treinish | f4a9b0f | 2013-07-26 16:58:26 -0400 | [diff] [blame] | 30 | from tempest.openstack.common import log as logging |
Jay Pipes | 051075a | 2012-04-28 17:39:37 -0400 | [diff] [blame] | 31 | |
| 32 | LOG = logging.getLogger(__name__) |
| 33 | |
Sean Dague | 86bd842 | 2013-12-20 09:56:44 -0500 | [diff] [blame] | 34 | CONF = config.CONF |
| 35 | |
Samuel Merritt | 0d499bc | 2013-06-19 12:08:23 -0700 | [diff] [blame] | 36 | # All the successful HTTP status codes from RFC 2616 |
| 37 | HTTP_SUCCESS = (200, 201, 202, 203, 204, 205, 206) |
| 38 | |
Jay Pipes | 051075a | 2012-04-28 17:39:37 -0400 | [diff] [blame] | 39 | |
Chris Yeoh | 55530bb | 2013-02-08 16:04:27 +1030 | [diff] [blame] | 40 | def attr(*args, **kwargs): |
| 41 | """A decorator which applies the nose and testtools attr decorator |
| 42 | |
| 43 | This decorator applies the nose attr decorator as well as the |
| 44 | the testtools.testcase.attr if it is in the list of attributes |
Attila Fazekas | b2902af | 2013-02-16 16:22:44 +0100 | [diff] [blame] | 45 | to testtools we want to apply. |
| 46 | """ |
Chris Yeoh | 55530bb | 2013-02-08 16:04:27 +1030 | [diff] [blame] | 47 | |
| 48 | def decorator(f): |
Giulio Fidente | 4946a05 | 2013-05-14 12:23:51 +0200 | [diff] [blame] | 49 | if 'type' in kwargs and isinstance(kwargs['type'], str): |
| 50 | f = testtools.testcase.attr(kwargs['type'])(f) |
Chris Yeoh | cf3fb7c | 2013-05-19 15:59:00 +0930 | [diff] [blame] | 51 | if kwargs['type'] == 'smoke': |
| 52 | f = testtools.testcase.attr('gate')(f) |
Giulio Fidente | 4946a05 | 2013-05-14 12:23:51 +0200 | [diff] [blame] | 53 | elif 'type' in kwargs and isinstance(kwargs['type'], list): |
| 54 | for attr in kwargs['type']: |
| 55 | f = testtools.testcase.attr(attr)(f) |
Chris Yeoh | cf3fb7c | 2013-05-19 15:59:00 +0930 | [diff] [blame] | 56 | if attr == 'smoke': |
| 57 | f = testtools.testcase.attr('gate')(f) |
Giulio Fidente | 4946a05 | 2013-05-14 12:23:51 +0200 | [diff] [blame] | 58 | return nose.plugins.attrib.attr(*args, **kwargs)(f) |
Chris Yeoh | 55530bb | 2013-02-08 16:04:27 +1030 | [diff] [blame] | 59 | |
| 60 | return decorator |
| 61 | |
| 62 | |
Matthew Treinish | 16c4379 | 2013-09-09 19:55:23 +0000 | [diff] [blame] | 63 | def services(*args, **kwargs): |
| 64 | """A decorator used to set an attr for each service used in a test case |
| 65 | |
| 66 | This decorator applies a testtools attr for each service that gets |
| 67 | exercised by a test case. |
| 68 | """ |
| 69 | valid_service_list = ['compute', 'image', 'volume', 'orchestration', |
Matthew Treinish | 03c4f77 | 2014-02-02 13:37:44 -0500 | [diff] [blame] | 70 | 'network', 'identity', 'object_storage', 'dashboard'] |
Matthew Treinish | 16c4379 | 2013-09-09 19:55:23 +0000 | [diff] [blame] | 71 | |
| 72 | def decorator(f): |
| 73 | for service in args: |
| 74 | if service not in valid_service_list: |
| 75 | raise exceptions.InvalidServiceTag('%s is not a valid service' |
| 76 | % service) |
| 77 | attr(type=list(args))(f) |
| 78 | return f |
| 79 | return decorator |
| 80 | |
| 81 | |
Marc Koderer | 32221b8e | 2013-08-23 13:57:50 +0200 | [diff] [blame] | 82 | def stresstest(*args, **kwargs): |
| 83 | """Add stress test decorator |
| 84 | |
| 85 | For all functions with this decorator a attr stress will be |
| 86 | set automatically. |
| 87 | |
| 88 | @param class_setup_per: allowed values are application, process, action |
| 89 | ``application``: once in the stress job lifetime |
| 90 | ``process``: once in the worker process lifetime |
| 91 | ``action``: on each action |
Marc Koderer | b060441 | 2013-09-02 09:43:40 +0200 | [diff] [blame] | 92 | @param allow_inheritance: allows inheritance of this attribute |
Marc Koderer | 32221b8e | 2013-08-23 13:57:50 +0200 | [diff] [blame] | 93 | """ |
| 94 | def decorator(f): |
| 95 | if 'class_setup_per' in kwargs: |
| 96 | setattr(f, "st_class_setup_per", kwargs['class_setup_per']) |
| 97 | else: |
| 98 | setattr(f, "st_class_setup_per", 'process') |
Marc Koderer | b060441 | 2013-09-02 09:43:40 +0200 | [diff] [blame] | 99 | if 'allow_inheritance' in kwargs: |
| 100 | setattr(f, "st_allow_inheritance", kwargs['allow_inheritance']) |
| 101 | else: |
| 102 | setattr(f, "st_allow_inheritance", False) |
Marc Koderer | 32221b8e | 2013-08-23 13:57:50 +0200 | [diff] [blame] | 103 | attr(type='stress')(f) |
| 104 | return f |
| 105 | return decorator |
| 106 | |
| 107 | |
Giulio Fidente | 83181a9 | 2013-10-01 06:02:24 +0200 | [diff] [blame] | 108 | def skip_because(*args, **kwargs): |
| 109 | """A decorator useful to skip tests hitting known bugs |
| 110 | |
| 111 | @param bug: bug number causing the test to skip |
| 112 | @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] | 113 | @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] | 114 | """ |
| 115 | def decorator(f): |
Masayuki Igawa | 80c1b9f | 2013-10-07 17:19:11 +0900 | [diff] [blame] | 116 | @functools.wraps(f) |
Ken'ichi Ohmichi | a1aa44c | 2013-12-06 20:48:24 +0900 | [diff] [blame] | 117 | def wrapper(self, *func_args, **func_kwargs): |
| 118 | skip = False |
| 119 | if "condition" in kwargs: |
| 120 | if kwargs["condition"] is True: |
| 121 | skip = True |
| 122 | elif "interface" in kwargs: |
| 123 | if kwargs["interface"] == self._interface: |
| 124 | skip = True |
| 125 | else: |
| 126 | skip = True |
| 127 | if "bug" in kwargs and skip is True: |
| 128 | msg = "Skipped until Bug: %s is resolved." % kwargs["bug"] |
| 129 | raise testtools.TestCase.skipException(msg) |
| 130 | return f(self, *func_args, **func_kwargs) |
Masayuki Igawa | 80c1b9f | 2013-10-07 17:19:11 +0900 | [diff] [blame] | 131 | return wrapper |
Giulio Fidente | 83181a9 | 2013-10-01 06:02:24 +0200 | [diff] [blame] | 132 | return decorator |
| 133 | |
| 134 | |
Matthew Treinish | e3d2614 | 2013-11-26 19:14:58 +0000 | [diff] [blame] | 135 | def requires_ext(*args, **kwargs): |
| 136 | """A decorator to skip tests if an extension is not enabled |
| 137 | |
| 138 | @param extension |
| 139 | @param service |
| 140 | """ |
| 141 | def decorator(func): |
| 142 | @functools.wraps(func) |
| 143 | def wrapper(*func_args, **func_kwargs): |
| 144 | if not is_extension_enabled(kwargs['extension'], |
| 145 | kwargs['service']): |
| 146 | msg = "Skipped because %s extension: %s is not enabled" % ( |
| 147 | kwargs['service'], kwargs['extension']) |
| 148 | raise testtools.TestCase.skipException(msg) |
| 149 | return func(*func_args, **func_kwargs) |
| 150 | return wrapper |
| 151 | return decorator |
| 152 | |
| 153 | |
| 154 | def is_extension_enabled(extension_name, service): |
| 155 | """A function that will check the list of enabled extensions from config |
| 156 | |
| 157 | """ |
Matthew Treinish | e3d2614 | 2013-11-26 19:14:58 +0000 | [diff] [blame] | 158 | config_dict = { |
Matthew Treinish | bc0e03e | 2014-01-30 16:51:06 +0000 | [diff] [blame] | 159 | 'compute': CONF.compute_feature_enabled.api_extensions, |
| 160 | 'compute_v3': CONF.compute_feature_enabled.api_v3_extensions, |
| 161 | 'volume': CONF.volume_feature_enabled.api_extensions, |
| 162 | 'network': CONF.network_feature_enabled.api_extensions, |
| 163 | 'object': CONF.object_storage_feature_enabled.discoverable_apis, |
Matthew Treinish | e3d2614 | 2013-11-26 19:14:58 +0000 | [diff] [blame] | 164 | } |
| 165 | if config_dict[service][0] == 'all': |
| 166 | return True |
| 167 | if extension_name in config_dict[service]: |
| 168 | return True |
| 169 | return False |
| 170 | |
Ian Wienand | 98c35f3 | 2013-07-23 20:34:23 +1000 | [diff] [blame] | 171 | # there is a mis-match between nose and testtools for older pythons. |
| 172 | # testtools will set skipException to be either |
| 173 | # unittest.case.SkipTest, unittest2.case.SkipTest or an internal skip |
| 174 | # exception, depending on what it can find. Python <2.7 doesn't have |
| 175 | # unittest.case.SkipTest; so if unittest2 is not installed it falls |
| 176 | # back to the internal class. |
| 177 | # |
| 178 | # The current nose skip plugin will decide to raise either |
| 179 | # unittest.case.SkipTest or its own internal exception; it does not |
| 180 | # look for unittest2 or the internal unittest exception. Thus we must |
| 181 | # monkey-patch testtools.TestCase.skipException to be the exception |
| 182 | # the nose skip plugin expects. |
| 183 | # |
| 184 | # However, with the switch to testr nose may not be available, so we |
| 185 | # require you to opt-in to this fix with an environment variable. |
| 186 | # |
| 187 | # This is temporary until upstream nose starts looking for unittest2 |
| 188 | # as testtools does; we can then remove this and ensure unittest2 is |
| 189 | # available for older pythons; then nose and testtools will agree |
| 190 | # unittest2.case.SkipTest is the one-true skip test exception. |
| 191 | # |
| 192 | # https://review.openstack.org/#/c/33056 |
| 193 | # https://github.com/nose-devs/nose/pull/699 |
| 194 | if 'TEMPEST_PY26_NOSE_COMPAT' in os.environ: |
| 195 | try: |
| 196 | import unittest.case.SkipTest |
| 197 | # convince pep8 we're using the import... |
| 198 | if unittest.case.SkipTest: |
| 199 | pass |
| 200 | raise RuntimeError("You have unittest.case.SkipTest; " |
| 201 | "no need to override") |
| 202 | except ImportError: |
| 203 | LOG.info("Overriding skipException to nose SkipTest") |
| 204 | testtools.TestCase.skipException = nose.plugins.skip.SkipTest |
| 205 | |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 206 | at_exit_set = set() |
| 207 | |
| 208 | |
| 209 | def validate_tearDownClass(): |
| 210 | if at_exit_set: |
Vladislav Kuzmin | 0df88bb | 2014-01-28 14:58:48 +0400 | [diff] [blame] | 211 | raise RuntimeError("tearDownClass does not call the super's " |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 212 | "tearDownClass in these classes: " |
Attila Fazekas | d5d43b8 | 2013-10-09 16:02:19 +0200 | [diff] [blame] | 213 | + str(at_exit_set) + "\n" |
| 214 | "If you see the exception, with another " |
Vladislav Kuzmin | 0df88bb | 2014-01-28 14:58:48 +0400 | [diff] [blame] | 215 | "exception please do not report this one! " |
| 216 | "If you are changing tempest code, make sure you " |
Attila Fazekas | d5d43b8 | 2013-10-09 16:02:19 +0200 | [diff] [blame] | 217 | "are calling the super class's tearDownClass!") |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 218 | |
| 219 | atexit.register(validate_tearDownClass) |
| 220 | |
Ian Wienand | 98c35f3 | 2013-07-23 20:34:23 +1000 | [diff] [blame] | 221 | |
Attila Fazekas | dc21642 | 2013-01-29 15:12:14 +0100 | [diff] [blame] | 222 | class BaseTestCase(testtools.TestCase, |
| 223 | testtools.testcase.WithAttributes, |
| 224 | testresources.ResourcedTestCase): |
Attila Fazekas | c43fec8 | 2013-04-09 23:17:52 +0200 | [diff] [blame] | 225 | |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 226 | setUpClassCalled = False |
| 227 | |
Matthew Treinish | 9f756a0 | 2014-01-15 10:26:07 -0500 | [diff] [blame] | 228 | network_resources = {} |
| 229 | |
Pavel Sedlák | 1053bd3 | 2013-04-16 16:47:40 +0200 | [diff] [blame] | 230 | @classmethod |
| 231 | def setUpClass(cls): |
| 232 | if hasattr(super(BaseTestCase, cls), 'setUpClass'): |
| 233 | super(BaseTestCase, cls).setUpClass() |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 234 | cls.setUpClassCalled = True |
Pavel Sedlák | 1053bd3 | 2013-04-16 16:47:40 +0200 | [diff] [blame] | 235 | |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 236 | @classmethod |
| 237 | def tearDownClass(cls): |
Attila Fazekas | 5d27530 | 2013-08-29 12:35:12 +0200 | [diff] [blame] | 238 | at_exit_set.discard(cls) |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 239 | if hasattr(super(BaseTestCase, cls), 'tearDownClass'): |
| 240 | super(BaseTestCase, cls).tearDownClass() |
| 241 | |
| 242 | def setUp(self): |
| 243 | super(BaseTestCase, self).setUp() |
| 244 | if not self.setUpClassCalled: |
| 245 | raise RuntimeError("setUpClass does not calls the super's" |
| 246 | "setUpClass in the " |
| 247 | + self.__class__.__name__) |
| 248 | at_exit_set.add(self.__class__) |
Matthew Treinish | 78561ad | 2013-07-26 11:41:56 -0400 | [diff] [blame] | 249 | test_timeout = os.environ.get('OS_TEST_TIMEOUT', 0) |
| 250 | try: |
| 251 | test_timeout = int(test_timeout) |
| 252 | except ValueError: |
| 253 | test_timeout = 0 |
| 254 | if test_timeout > 0: |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 255 | self.useFixture(fixtures.Timeout(test_timeout, gentle=True)) |
Matthew Treinish | 78561ad | 2013-07-26 11:41:56 -0400 | [diff] [blame] | 256 | |
| 257 | if (os.environ.get('OS_STDOUT_CAPTURE') == 'True' or |
| 258 | os.environ.get('OS_STDOUT_CAPTURE') == '1'): |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 259 | stdout = self.useFixture(fixtures.StringStream('stdout')).stream |
| 260 | self.useFixture(fixtures.MonkeyPatch('sys.stdout', stdout)) |
Matthew Treinish | 78561ad | 2013-07-26 11:41:56 -0400 | [diff] [blame] | 261 | if (os.environ.get('OS_STDERR_CAPTURE') == 'True' or |
| 262 | os.environ.get('OS_STDERR_CAPTURE') == '1'): |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 263 | stderr = self.useFixture(fixtures.StringStream('stderr')).stream |
| 264 | self.useFixture(fixtures.MonkeyPatch('sys.stderr', stderr)) |
Attila Fazekas | 3138807 | 2013-08-15 08:58:07 +0200 | [diff] [blame] | 265 | if (os.environ.get('OS_LOG_CAPTURE') != 'False' and |
| 266 | os.environ.get('OS_LOG_CAPTURE') != '0'): |
| 267 | log_format = '%(asctime)-15s %(message)s' |
| 268 | self.useFixture(fixtures.LoggerFixture(nuke_handlers=False, |
Attila Fazekas | 90445be | 2013-10-24 16:46:03 +0200 | [diff] [blame] | 269 | format=log_format, |
| 270 | level=None)) |
Matthew Treinish | 78561ad | 2013-07-26 11:41:56 -0400 | [diff] [blame] | 271 | |
Matthew Treinish | 3e04685 | 2013-07-23 16:00:24 -0400 | [diff] [blame] | 272 | @classmethod |
Matthew Treinish | 8004e8c | 2014-01-27 23:03:14 +0000 | [diff] [blame] | 273 | def get_client_manager(cls, interface=None): |
Ryan Hsu | 6c4bb3d | 2013-10-21 21:22:50 -0700 | [diff] [blame] | 274 | """ |
| 275 | Returns an Openstack client manager |
| 276 | """ |
Matthew Treinish | 9f756a0 | 2014-01-15 10:26:07 -0500 | [diff] [blame] | 277 | cls.isolated_creds = isolated_creds.IsolatedCreds( |
| 278 | cls.__name__, network_resources=cls.network_resources) |
Ryan Hsu | 6c4bb3d | 2013-10-21 21:22:50 -0700 | [diff] [blame] | 279 | |
| 280 | force_tenant_isolation = getattr(cls, 'force_tenant_isolation', None) |
Matthew Treinish | bc0e03e | 2014-01-30 16:51:06 +0000 | [diff] [blame] | 281 | if (CONF.compute.allow_tenant_isolation or |
Ryan Hsu | 6c4bb3d | 2013-10-21 21:22:50 -0700 | [diff] [blame] | 282 | force_tenant_isolation): |
| 283 | creds = cls.isolated_creds.get_primary_creds() |
| 284 | username, tenant_name, password = creds |
Matthew Treinish | 8004e8c | 2014-01-27 23:03:14 +0000 | [diff] [blame] | 285 | if getattr(cls, '_interface', None): |
| 286 | os = clients.Manager(username=username, |
| 287 | password=password, |
| 288 | tenant_name=tenant_name, |
| 289 | interface=cls._interface) |
| 290 | elif interface: |
| 291 | os = clients.Manager(username=username, |
| 292 | password=password, |
| 293 | tenant_name=tenant_name, |
| 294 | interface=interface) |
| 295 | else: |
| 296 | os = clients.Manager(username=username, |
| 297 | password=password, |
| 298 | tenant_name=tenant_name) |
Ryan Hsu | 6c4bb3d | 2013-10-21 21:22:50 -0700 | [diff] [blame] | 299 | else: |
Matthew Treinish | 8004e8c | 2014-01-27 23:03:14 +0000 | [diff] [blame] | 300 | if getattr(cls, '_interface', None): |
| 301 | os = clients.Manager(interface=cls._interface) |
| 302 | elif interface: |
| 303 | os = clients.Manager(interface=interface) |
| 304 | else: |
| 305 | os = clients.Manager() |
Ryan Hsu | 6c4bb3d | 2013-10-21 21:22:50 -0700 | [diff] [blame] | 306 | return os |
| 307 | |
| 308 | @classmethod |
| 309 | def clear_isolated_creds(cls): |
| 310 | """ |
| 311 | Clears isolated creds if set |
| 312 | """ |
| 313 | if getattr(cls, 'isolated_creds'): |
| 314 | cls.isolated_creds.clear_isolated_creds() |
| 315 | |
| 316 | @classmethod |
Matthew Treinish | 3e04685 | 2013-07-23 16:00:24 -0400 | [diff] [blame] | 317 | def _get_identity_admin_client(cls): |
| 318 | """ |
| 319 | Returns an instance of the Identity Admin API client |
| 320 | """ |
| 321 | os = clients.AdminManager(interface=cls._interface) |
| 322 | admin_client = os.identity_client |
| 323 | return admin_client |
| 324 | |
| 325 | @classmethod |
Matthew Treinish | 9f756a0 | 2014-01-15 10:26:07 -0500 | [diff] [blame] | 326 | def set_network_resources(self, network=False, router=False, subnet=False, |
| 327 | dhcp=False): |
| 328 | """Specify which network resources should be created |
| 329 | |
| 330 | @param network |
| 331 | @param router |
| 332 | @param subnet |
| 333 | @param dhcp |
| 334 | """ |
Salvatore Orlando | 5a33724 | 2014-01-15 22:49:22 +0000 | [diff] [blame] | 335 | # network resources should be set only once from callers |
| 336 | # in order to ensure that even if it's called multiple times in |
| 337 | # a chain of overloaded methods, the attribute is set only |
| 338 | # in the leaf class |
| 339 | if not self.network_resources: |
| 340 | self.network_resources = { |
| 341 | 'network': network, |
| 342 | 'router': router, |
| 343 | 'subnet': subnet, |
| 344 | 'dhcp': dhcp} |
Matthew Treinish | 9f756a0 | 2014-01-15 10:26:07 -0500 | [diff] [blame] | 345 | |
Attila Fazekas | dc21642 | 2013-01-29 15:12:14 +0100 | [diff] [blame] | 346 | |
Sean Dague | 35a7caf | 2013-05-10 10:38:22 -0400 | [diff] [blame] | 347 | def call_until_true(func, duration, sleep_for): |
| 348 | """ |
| 349 | Call the given function until it returns True (and return True) or |
| 350 | until the specified duration (in seconds) elapses (and return |
| 351 | False). |
| 352 | |
| 353 | :param func: A zero argument callable that returns True on success. |
| 354 | :param duration: The number of seconds for which to attempt a |
| 355 | successful call of the function. |
| 356 | :param sleep_for: The number of seconds to sleep after an unsuccessful |
| 357 | invocation of the function. |
| 358 | """ |
| 359 | now = time.time() |
| 360 | timeout = now + duration |
| 361 | while now < timeout: |
| 362 | if func(): |
| 363 | return True |
| 364 | LOG.debug("Sleeping for %d seconds", sleep_for) |
| 365 | time.sleep(sleep_for) |
| 366 | now = time.time() |
| 367 | return False |