Jay Pipes | 051075a | 2012-04-28 17:39:37 -0400 | [diff] [blame] | 1 | # vim: tabstop=4 shiftwidth=4 softtabstop=4 |
| 2 | |
ZhiQiang Fan | 39f9722 | 2013-09-20 04:49:44 +0800 | [diff] [blame] | 3 | # Copyright 2012 OpenStack Foundation |
Jay Pipes | 051075a | 2012-04-28 17:39:37 -0400 | [diff] [blame] | 4 | # All Rights Reserved. |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 7 | # not use this file except in compliance with the License. You may obtain |
| 8 | # a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 15 | # License for the specific language governing permissions and limitations |
| 16 | # under the License. |
| 17 | |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 18 | import atexit |
Masayuki Igawa | 80c1b9f | 2013-10-07 17:19:11 +0900 | [diff] [blame] | 19 | import functools |
Ian Wienand | 98c35f3 | 2013-07-23 20:34:23 +1000 | [diff] [blame] | 20 | import os |
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 |
Chris Yeoh | 55530bb | 2013-02-08 16:04:27 +1030 | [diff] [blame] | 24 | import nose.plugins.attrib |
Attila Fazekas | dc21642 | 2013-01-29 15:12:14 +0100 | [diff] [blame] | 25 | import testresources |
ivan-zhu | 1feeb38 | 2013-01-24 10:14:39 +0800 | [diff] [blame] | 26 | import testtools |
Jay Pipes | 051075a | 2012-04-28 17:39:37 -0400 | [diff] [blame] | 27 | |
Matthew Treinish | 3e04685 | 2013-07-23 16:00:24 -0400 | [diff] [blame] | 28 | from tempest import clients |
Attila Fazekas | dc21642 | 2013-01-29 15:12:14 +0100 | [diff] [blame] | 29 | from tempest import config |
Matthew Treinish | 16c4379 | 2013-09-09 19:55:23 +0000 | [diff] [blame] | 30 | from tempest import exceptions |
Matthew Treinish | f4a9b0f | 2013-07-26 16:58:26 -0400 | [diff] [blame] | 31 | from tempest.openstack.common import log as logging |
Jay Pipes | 051075a | 2012-04-28 17:39:37 -0400 | [diff] [blame] | 32 | |
| 33 | LOG = logging.getLogger(__name__) |
| 34 | |
Samuel Merritt | 0d499bc | 2013-06-19 12:08:23 -0700 | [diff] [blame] | 35 | # All the successful HTTP status codes from RFC 2616 |
| 36 | HTTP_SUCCESS = (200, 201, 202, 203, 204, 205, 206) |
| 37 | |
Jay Pipes | 051075a | 2012-04-28 17:39:37 -0400 | [diff] [blame] | 38 | |
Chris Yeoh | 55530bb | 2013-02-08 16:04:27 +1030 | [diff] [blame] | 39 | def attr(*args, **kwargs): |
| 40 | """A decorator which applies the nose and testtools attr decorator |
| 41 | |
| 42 | This decorator applies the nose attr decorator as well as the |
| 43 | the testtools.testcase.attr if it is in the list of attributes |
Attila Fazekas | b2902af | 2013-02-16 16:22:44 +0100 | [diff] [blame] | 44 | to testtools we want to apply. |
| 45 | """ |
Chris Yeoh | 55530bb | 2013-02-08 16:04:27 +1030 | [diff] [blame] | 46 | |
| 47 | def decorator(f): |
Giulio Fidente | 4946a05 | 2013-05-14 12:23:51 +0200 | [diff] [blame] | 48 | if 'type' in kwargs and isinstance(kwargs['type'], str): |
| 49 | f = testtools.testcase.attr(kwargs['type'])(f) |
Chris Yeoh | cf3fb7c | 2013-05-19 15:59:00 +0930 | [diff] [blame] | 50 | if kwargs['type'] == 'smoke': |
| 51 | f = testtools.testcase.attr('gate')(f) |
Giulio Fidente | 4946a05 | 2013-05-14 12:23:51 +0200 | [diff] [blame] | 52 | elif 'type' in kwargs and isinstance(kwargs['type'], list): |
| 53 | for attr in kwargs['type']: |
| 54 | f = testtools.testcase.attr(attr)(f) |
Chris Yeoh | cf3fb7c | 2013-05-19 15:59:00 +0930 | [diff] [blame] | 55 | if attr == 'smoke': |
| 56 | f = testtools.testcase.attr('gate')(f) |
Giulio Fidente | 4946a05 | 2013-05-14 12:23:51 +0200 | [diff] [blame] | 57 | return nose.plugins.attrib.attr(*args, **kwargs)(f) |
Chris Yeoh | 55530bb | 2013-02-08 16:04:27 +1030 | [diff] [blame] | 58 | |
| 59 | return decorator |
| 60 | |
| 61 | |
Matthew Treinish | 16c4379 | 2013-09-09 19:55:23 +0000 | [diff] [blame] | 62 | def services(*args, **kwargs): |
| 63 | """A decorator used to set an attr for each service used in a test case |
| 64 | |
| 65 | This decorator applies a testtools attr for each service that gets |
| 66 | exercised by a test case. |
| 67 | """ |
| 68 | valid_service_list = ['compute', 'image', 'volume', 'orchestration', |
| 69 | 'network', 'identity', 'object', 'dashboard'] |
| 70 | |
| 71 | def decorator(f): |
| 72 | for service in args: |
| 73 | if service not in valid_service_list: |
| 74 | raise exceptions.InvalidServiceTag('%s is not a valid service' |
| 75 | % service) |
| 76 | attr(type=list(args))(f) |
| 77 | return f |
| 78 | return decorator |
| 79 | |
| 80 | |
Marc Koderer | 32221b8e | 2013-08-23 13:57:50 +0200 | [diff] [blame] | 81 | def stresstest(*args, **kwargs): |
| 82 | """Add stress test decorator |
| 83 | |
| 84 | For all functions with this decorator a attr stress will be |
| 85 | set automatically. |
| 86 | |
| 87 | @param class_setup_per: allowed values are application, process, action |
| 88 | ``application``: once in the stress job lifetime |
| 89 | ``process``: once in the worker process lifetime |
| 90 | ``action``: on each action |
Marc Koderer | b060441 | 2013-09-02 09:43:40 +0200 | [diff] [blame] | 91 | @param allow_inheritance: allows inheritance of this attribute |
Marc Koderer | 32221b8e | 2013-08-23 13:57:50 +0200 | [diff] [blame] | 92 | """ |
| 93 | def decorator(f): |
| 94 | if 'class_setup_per' in kwargs: |
| 95 | setattr(f, "st_class_setup_per", kwargs['class_setup_per']) |
| 96 | else: |
| 97 | setattr(f, "st_class_setup_per", 'process') |
Marc Koderer | b060441 | 2013-09-02 09:43:40 +0200 | [diff] [blame] | 98 | if 'allow_inheritance' in kwargs: |
| 99 | setattr(f, "st_allow_inheritance", kwargs['allow_inheritance']) |
| 100 | else: |
| 101 | setattr(f, "st_allow_inheritance", False) |
Marc Koderer | 32221b8e | 2013-08-23 13:57:50 +0200 | [diff] [blame] | 102 | attr(type='stress')(f) |
| 103 | return f |
| 104 | return decorator |
| 105 | |
| 106 | |
Giulio Fidente | 83181a9 | 2013-10-01 06:02:24 +0200 | [diff] [blame] | 107 | def skip_because(*args, **kwargs): |
| 108 | """A decorator useful to skip tests hitting known bugs |
| 109 | |
| 110 | @param bug: bug number causing the test to skip |
| 111 | @param condition: optional condition to be True for the skip to have place |
| 112 | """ |
| 113 | def decorator(f): |
Masayuki Igawa | 80c1b9f | 2013-10-07 17:19:11 +0900 | [diff] [blame] | 114 | @functools.wraps(f) |
| 115 | def wrapper(*func_args, **func_kwargs): |
| 116 | if "bug" in kwargs: |
| 117 | if "condition" not in kwargs or kwargs["condition"] is True: |
| 118 | msg = "Skipped until Bug: %s is resolved." % kwargs["bug"] |
| 119 | raise testtools.TestCase.skipException(msg) |
| 120 | return f(*func_args, **func_kwargs) |
| 121 | return wrapper |
Giulio Fidente | 83181a9 | 2013-10-01 06:02:24 +0200 | [diff] [blame] | 122 | return decorator |
| 123 | |
| 124 | |
Ian Wienand | 98c35f3 | 2013-07-23 20:34:23 +1000 | [diff] [blame] | 125 | # there is a mis-match between nose and testtools for older pythons. |
| 126 | # testtools will set skipException to be either |
| 127 | # unittest.case.SkipTest, unittest2.case.SkipTest or an internal skip |
| 128 | # exception, depending on what it can find. Python <2.7 doesn't have |
| 129 | # unittest.case.SkipTest; so if unittest2 is not installed it falls |
| 130 | # back to the internal class. |
| 131 | # |
| 132 | # The current nose skip plugin will decide to raise either |
| 133 | # unittest.case.SkipTest or its own internal exception; it does not |
| 134 | # look for unittest2 or the internal unittest exception. Thus we must |
| 135 | # monkey-patch testtools.TestCase.skipException to be the exception |
| 136 | # the nose skip plugin expects. |
| 137 | # |
| 138 | # However, with the switch to testr nose may not be available, so we |
| 139 | # require you to opt-in to this fix with an environment variable. |
| 140 | # |
| 141 | # This is temporary until upstream nose starts looking for unittest2 |
| 142 | # as testtools does; we can then remove this and ensure unittest2 is |
| 143 | # available for older pythons; then nose and testtools will agree |
| 144 | # unittest2.case.SkipTest is the one-true skip test exception. |
| 145 | # |
| 146 | # https://review.openstack.org/#/c/33056 |
| 147 | # https://github.com/nose-devs/nose/pull/699 |
| 148 | if 'TEMPEST_PY26_NOSE_COMPAT' in os.environ: |
| 149 | try: |
| 150 | import unittest.case.SkipTest |
| 151 | # convince pep8 we're using the import... |
| 152 | if unittest.case.SkipTest: |
| 153 | pass |
| 154 | raise RuntimeError("You have unittest.case.SkipTest; " |
| 155 | "no need to override") |
| 156 | except ImportError: |
| 157 | LOG.info("Overriding skipException to nose SkipTest") |
| 158 | testtools.TestCase.skipException = nose.plugins.skip.SkipTest |
| 159 | |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 160 | at_exit_set = set() |
| 161 | |
| 162 | |
| 163 | def validate_tearDownClass(): |
| 164 | if at_exit_set: |
Alex Gaynor | 94560d4 | 2013-08-23 05:41:23 -0700 | [diff] [blame] | 165 | raise RuntimeError("tearDownClass does not calls the super's " |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 166 | "tearDownClass in these classes: " |
| 167 | + str(at_exit_set)) |
| 168 | |
| 169 | atexit.register(validate_tearDownClass) |
| 170 | |
Ian Wienand | 98c35f3 | 2013-07-23 20:34:23 +1000 | [diff] [blame] | 171 | |
Attila Fazekas | dc21642 | 2013-01-29 15:12:14 +0100 | [diff] [blame] | 172 | class BaseTestCase(testtools.TestCase, |
| 173 | testtools.testcase.WithAttributes, |
| 174 | testresources.ResourcedTestCase): |
Attila Fazekas | c43fec8 | 2013-04-09 23:17:52 +0200 | [diff] [blame] | 175 | |
| 176 | config = config.TempestConfig() |
Attila Fazekas | dc21642 | 2013-01-29 15:12:14 +0100 | [diff] [blame] | 177 | |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 178 | setUpClassCalled = False |
| 179 | |
Pavel Sedlák | 1053bd3 | 2013-04-16 16:47:40 +0200 | [diff] [blame] | 180 | @classmethod |
| 181 | def setUpClass(cls): |
| 182 | if hasattr(super(BaseTestCase, cls), 'setUpClass'): |
| 183 | super(BaseTestCase, cls).setUpClass() |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 184 | cls.setUpClassCalled = True |
Pavel Sedlák | 1053bd3 | 2013-04-16 16:47:40 +0200 | [diff] [blame] | 185 | |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 186 | @classmethod |
| 187 | def tearDownClass(cls): |
Attila Fazekas | 5d27530 | 2013-08-29 12:35:12 +0200 | [diff] [blame] | 188 | at_exit_set.discard(cls) |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 189 | if hasattr(super(BaseTestCase, cls), 'tearDownClass'): |
| 190 | super(BaseTestCase, cls).tearDownClass() |
| 191 | |
| 192 | def setUp(self): |
| 193 | super(BaseTestCase, self).setUp() |
| 194 | if not self.setUpClassCalled: |
| 195 | raise RuntimeError("setUpClass does not calls the super's" |
| 196 | "setUpClass in the " |
| 197 | + self.__class__.__name__) |
| 198 | at_exit_set.add(self.__class__) |
Matthew Treinish | 78561ad | 2013-07-26 11:41:56 -0400 | [diff] [blame] | 199 | test_timeout = os.environ.get('OS_TEST_TIMEOUT', 0) |
| 200 | try: |
| 201 | test_timeout = int(test_timeout) |
| 202 | except ValueError: |
| 203 | test_timeout = 0 |
| 204 | if test_timeout > 0: |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 205 | self.useFixture(fixtures.Timeout(test_timeout, gentle=True)) |
Matthew Treinish | 78561ad | 2013-07-26 11:41:56 -0400 | [diff] [blame] | 206 | |
| 207 | if (os.environ.get('OS_STDOUT_CAPTURE') == 'True' or |
| 208 | os.environ.get('OS_STDOUT_CAPTURE') == '1'): |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 209 | stdout = self.useFixture(fixtures.StringStream('stdout')).stream |
| 210 | self.useFixture(fixtures.MonkeyPatch('sys.stdout', stdout)) |
Matthew Treinish | 78561ad | 2013-07-26 11:41:56 -0400 | [diff] [blame] | 211 | if (os.environ.get('OS_STDERR_CAPTURE') == 'True' or |
| 212 | os.environ.get('OS_STDERR_CAPTURE') == '1'): |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 213 | stderr = self.useFixture(fixtures.StringStream('stderr')).stream |
| 214 | self.useFixture(fixtures.MonkeyPatch('sys.stderr', stderr)) |
Attila Fazekas | 3138807 | 2013-08-15 08:58:07 +0200 | [diff] [blame] | 215 | if (os.environ.get('OS_LOG_CAPTURE') != 'False' and |
| 216 | os.environ.get('OS_LOG_CAPTURE') != '0'): |
| 217 | log_format = '%(asctime)-15s %(message)s' |
| 218 | self.useFixture(fixtures.LoggerFixture(nuke_handlers=False, |
| 219 | format=log_format)) |
Matthew Treinish | 78561ad | 2013-07-26 11:41:56 -0400 | [diff] [blame] | 220 | |
Matthew Treinish | 3e04685 | 2013-07-23 16:00:24 -0400 | [diff] [blame] | 221 | @classmethod |
| 222 | def _get_identity_admin_client(cls): |
| 223 | """ |
| 224 | Returns an instance of the Identity Admin API client |
| 225 | """ |
| 226 | os = clients.AdminManager(interface=cls._interface) |
| 227 | admin_client = os.identity_client |
| 228 | return admin_client |
| 229 | |
| 230 | @classmethod |
| 231 | def _get_client_args(cls): |
| 232 | |
| 233 | return ( |
| 234 | cls.config, |
| 235 | cls.config.identity.admin_username, |
| 236 | cls.config.identity.admin_password, |
| 237 | cls.config.identity.uri |
| 238 | ) |
| 239 | |
Attila Fazekas | dc21642 | 2013-01-29 15:12:14 +0100 | [diff] [blame] | 240 | |
Sean Dague | 35a7caf | 2013-05-10 10:38:22 -0400 | [diff] [blame] | 241 | def call_until_true(func, duration, sleep_for): |
| 242 | """ |
| 243 | Call the given function until it returns True (and return True) or |
| 244 | until the specified duration (in seconds) elapses (and return |
| 245 | False). |
| 246 | |
| 247 | :param func: A zero argument callable that returns True on success. |
| 248 | :param duration: The number of seconds for which to attempt a |
| 249 | successful call of the function. |
| 250 | :param sleep_for: The number of seconds to sleep after an unsuccessful |
| 251 | invocation of the function. |
| 252 | """ |
| 253 | now = time.time() |
| 254 | timeout = now + duration |
| 255 | while now < timeout: |
| 256 | if func(): |
| 257 | return True |
| 258 | LOG.debug("Sleeping for %d seconds", sleep_for) |
| 259 | time.sleep(sleep_for) |
| 260 | now = time.time() |
| 261 | return False |