blob: 6ae7925dc76766fd426a67542a0ec050fef416fc [file] [log] [blame]
Jay Pipes051075a2012-04-28 17:39:37 -04001# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
ZhiQiang Fan39f97222013-09-20 04:49:44 +08003# Copyright 2012 OpenStack Foundation
Jay Pipes051075a2012-04-28 17:39:37 -04004# 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 Fazekasf86fa312013-07-30 19:56:39 +020018import atexit
Masayuki Igawa80c1b9f2013-10-07 17:19:11 +090019import functools
Ian Wienand98c35f32013-07-23 20:34:23 +100020import os
Jay Pipes051075a2012-04-28 17:39:37 -040021import time
22
Matthew Treinish78561ad2013-07-26 11:41:56 -040023import fixtures
Chris Yeoh55530bb2013-02-08 16:04:27 +103024import nose.plugins.attrib
Attila Fazekasdc216422013-01-29 15:12:14 +010025import testresources
ivan-zhu1feeb382013-01-24 10:14:39 +080026import testtools
Jay Pipes051075a2012-04-28 17:39:37 -040027
Matthew Treinish3e046852013-07-23 16:00:24 -040028from tempest import clients
Ryan Hsu6c4bb3d2013-10-21 21:22:50 -070029from tempest.common import isolated_creds
Attila Fazekasdc216422013-01-29 15:12:14 +010030from tempest import config
Matthew Treinish16c43792013-09-09 19:55:23 +000031from tempest import exceptions
Matthew Treinishf4a9b0f2013-07-26 16:58:26 -040032from tempest.openstack.common import log as logging
Jay Pipes051075a2012-04-28 17:39:37 -040033
34LOG = logging.getLogger(__name__)
35
Samuel Merritt0d499bc2013-06-19 12:08:23 -070036# All the successful HTTP status codes from RFC 2616
37HTTP_SUCCESS = (200, 201, 202, 203, 204, 205, 206)
38
Jay Pipes051075a2012-04-28 17:39:37 -040039
Chris Yeoh55530bb2013-02-08 16:04:27 +103040def 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 Fazekasb2902af2013-02-16 16:22:44 +010045 to testtools we want to apply.
46 """
Chris Yeoh55530bb2013-02-08 16:04:27 +103047
48 def decorator(f):
Giulio Fidente4946a052013-05-14 12:23:51 +020049 if 'type' in kwargs and isinstance(kwargs['type'], str):
50 f = testtools.testcase.attr(kwargs['type'])(f)
Chris Yeohcf3fb7c2013-05-19 15:59:00 +093051 if kwargs['type'] == 'smoke':
52 f = testtools.testcase.attr('gate')(f)
Giulio Fidente4946a052013-05-14 12:23:51 +020053 elif 'type' in kwargs and isinstance(kwargs['type'], list):
54 for attr in kwargs['type']:
55 f = testtools.testcase.attr(attr)(f)
Chris Yeohcf3fb7c2013-05-19 15:59:00 +093056 if attr == 'smoke':
57 f = testtools.testcase.attr('gate')(f)
Giulio Fidente4946a052013-05-14 12:23:51 +020058 return nose.plugins.attrib.attr(*args, **kwargs)(f)
Chris Yeoh55530bb2013-02-08 16:04:27 +103059
60 return decorator
61
62
Matthew Treinish16c43792013-09-09 19:55:23 +000063def 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',
70 'network', 'identity', 'object', 'dashboard']
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 Koderer32221b8e2013-08-23 13:57:50 +020082def 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 Kodererb0604412013-09-02 09:43:40 +020092 @param allow_inheritance: allows inheritance of this attribute
Marc Koderer32221b8e2013-08-23 13:57:50 +020093 """
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 Kodererb0604412013-09-02 09:43:40 +020099 if 'allow_inheritance' in kwargs:
100 setattr(f, "st_allow_inheritance", kwargs['allow_inheritance'])
101 else:
102 setattr(f, "st_allow_inheritance", False)
Marc Koderer32221b8e2013-08-23 13:57:50 +0200103 attr(type='stress')(f)
104 return f
105 return decorator
106
107
Giulio Fidente83181a92013-10-01 06:02:24 +0200108def 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
113 """
114 def decorator(f):
Masayuki Igawa80c1b9f2013-10-07 17:19:11 +0900115 @functools.wraps(f)
116 def wrapper(*func_args, **func_kwargs):
117 if "bug" in kwargs:
118 if "condition" not in kwargs or kwargs["condition"] is True:
119 msg = "Skipped until Bug: %s is resolved." % kwargs["bug"]
120 raise testtools.TestCase.skipException(msg)
121 return f(*func_args, **func_kwargs)
122 return wrapper
Giulio Fidente83181a92013-10-01 06:02:24 +0200123 return decorator
124
125
Ian Wienand98c35f32013-07-23 20:34:23 +1000126# there is a mis-match between nose and testtools for older pythons.
127# testtools will set skipException to be either
128# unittest.case.SkipTest, unittest2.case.SkipTest or an internal skip
129# exception, depending on what it can find. Python <2.7 doesn't have
130# unittest.case.SkipTest; so if unittest2 is not installed it falls
131# back to the internal class.
132#
133# The current nose skip plugin will decide to raise either
134# unittest.case.SkipTest or its own internal exception; it does not
135# look for unittest2 or the internal unittest exception. Thus we must
136# monkey-patch testtools.TestCase.skipException to be the exception
137# the nose skip plugin expects.
138#
139# However, with the switch to testr nose may not be available, so we
140# require you to opt-in to this fix with an environment variable.
141#
142# This is temporary until upstream nose starts looking for unittest2
143# as testtools does; we can then remove this and ensure unittest2 is
144# available for older pythons; then nose and testtools will agree
145# unittest2.case.SkipTest is the one-true skip test exception.
146#
147# https://review.openstack.org/#/c/33056
148# https://github.com/nose-devs/nose/pull/699
149if 'TEMPEST_PY26_NOSE_COMPAT' in os.environ:
150 try:
151 import unittest.case.SkipTest
152 # convince pep8 we're using the import...
153 if unittest.case.SkipTest:
154 pass
155 raise RuntimeError("You have unittest.case.SkipTest; "
156 "no need to override")
157 except ImportError:
158 LOG.info("Overriding skipException to nose SkipTest")
159 testtools.TestCase.skipException = nose.plugins.skip.SkipTest
160
Attila Fazekasf86fa312013-07-30 19:56:39 +0200161at_exit_set = set()
162
163
164def validate_tearDownClass():
165 if at_exit_set:
Alex Gaynor94560d42013-08-23 05:41:23 -0700166 raise RuntimeError("tearDownClass does not calls the super's "
Attila Fazekasf86fa312013-07-30 19:56:39 +0200167 "tearDownClass in these classes: "
Attila Fazekasd5d43b82013-10-09 16:02:19 +0200168 + str(at_exit_set) + "\n"
169 "If you see the exception, with another "
170 "exception please do not report this one!"
171 "If you are changing tempest code, make sure you",
172 "are calling the super class's tearDownClass!")
Attila Fazekasf86fa312013-07-30 19:56:39 +0200173
174atexit.register(validate_tearDownClass)
175
Ian Wienand98c35f32013-07-23 20:34:23 +1000176
Attila Fazekasdc216422013-01-29 15:12:14 +0100177class BaseTestCase(testtools.TestCase,
178 testtools.testcase.WithAttributes,
179 testresources.ResourcedTestCase):
Attila Fazekasc43fec82013-04-09 23:17:52 +0200180
181 config = config.TempestConfig()
Attila Fazekasdc216422013-01-29 15:12:14 +0100182
Attila Fazekasf86fa312013-07-30 19:56:39 +0200183 setUpClassCalled = False
184
Pavel Sedlák1053bd32013-04-16 16:47:40 +0200185 @classmethod
186 def setUpClass(cls):
187 if hasattr(super(BaseTestCase, cls), 'setUpClass'):
188 super(BaseTestCase, cls).setUpClass()
Attila Fazekasf86fa312013-07-30 19:56:39 +0200189 cls.setUpClassCalled = True
Pavel Sedlák1053bd32013-04-16 16:47:40 +0200190
Attila Fazekasf86fa312013-07-30 19:56:39 +0200191 @classmethod
192 def tearDownClass(cls):
Attila Fazekas5d275302013-08-29 12:35:12 +0200193 at_exit_set.discard(cls)
Attila Fazekasf86fa312013-07-30 19:56:39 +0200194 if hasattr(super(BaseTestCase, cls), 'tearDownClass'):
195 super(BaseTestCase, cls).tearDownClass()
196
197 def setUp(self):
198 super(BaseTestCase, self).setUp()
199 if not self.setUpClassCalled:
200 raise RuntimeError("setUpClass does not calls the super's"
201 "setUpClass in the "
202 + self.__class__.__name__)
203 at_exit_set.add(self.__class__)
Matthew Treinish78561ad2013-07-26 11:41:56 -0400204 test_timeout = os.environ.get('OS_TEST_TIMEOUT', 0)
205 try:
206 test_timeout = int(test_timeout)
207 except ValueError:
208 test_timeout = 0
209 if test_timeout > 0:
Attila Fazekasf86fa312013-07-30 19:56:39 +0200210 self.useFixture(fixtures.Timeout(test_timeout, gentle=True))
Matthew Treinish78561ad2013-07-26 11:41:56 -0400211
212 if (os.environ.get('OS_STDOUT_CAPTURE') == 'True' or
213 os.environ.get('OS_STDOUT_CAPTURE') == '1'):
Attila Fazekasf86fa312013-07-30 19:56:39 +0200214 stdout = self.useFixture(fixtures.StringStream('stdout')).stream
215 self.useFixture(fixtures.MonkeyPatch('sys.stdout', stdout))
Matthew Treinish78561ad2013-07-26 11:41:56 -0400216 if (os.environ.get('OS_STDERR_CAPTURE') == 'True' or
217 os.environ.get('OS_STDERR_CAPTURE') == '1'):
Attila Fazekasf86fa312013-07-30 19:56:39 +0200218 stderr = self.useFixture(fixtures.StringStream('stderr')).stream
219 self.useFixture(fixtures.MonkeyPatch('sys.stderr', stderr))
Attila Fazekas31388072013-08-15 08:58:07 +0200220 if (os.environ.get('OS_LOG_CAPTURE') != 'False' and
221 os.environ.get('OS_LOG_CAPTURE') != '0'):
222 log_format = '%(asctime)-15s %(message)s'
223 self.useFixture(fixtures.LoggerFixture(nuke_handlers=False,
Attila Fazekas90445be2013-10-24 16:46:03 +0200224 format=log_format,
225 level=None))
Matthew Treinish78561ad2013-07-26 11:41:56 -0400226
Matthew Treinish3e046852013-07-23 16:00:24 -0400227 @classmethod
Ryan Hsu6c4bb3d2013-10-21 21:22:50 -0700228 def get_client_manager(cls):
229 """
230 Returns an Openstack client manager
231 """
232 cls.isolated_creds = isolated_creds.IsolatedCreds(cls.__name__)
233
234 force_tenant_isolation = getattr(cls, 'force_tenant_isolation', None)
235 if (cls.config.compute.allow_tenant_isolation or
236 force_tenant_isolation):
237 creds = cls.isolated_creds.get_primary_creds()
238 username, tenant_name, password = creds
239 os = clients.Manager(username=username,
240 password=password,
241 tenant_name=tenant_name,
242 interface=cls._interface)
243 else:
244 os = clients.Manager(interface=cls._interface)
245 return os
246
247 @classmethod
248 def clear_isolated_creds(cls):
249 """
250 Clears isolated creds if set
251 """
252 if getattr(cls, 'isolated_creds'):
253 cls.isolated_creds.clear_isolated_creds()
254
255 @classmethod
Matthew Treinish3e046852013-07-23 16:00:24 -0400256 def _get_identity_admin_client(cls):
257 """
258 Returns an instance of the Identity Admin API client
259 """
260 os = clients.AdminManager(interface=cls._interface)
261 admin_client = os.identity_client
262 return admin_client
263
264 @classmethod
265 def _get_client_args(cls):
266
267 return (
268 cls.config,
269 cls.config.identity.admin_username,
270 cls.config.identity.admin_password,
271 cls.config.identity.uri
272 )
273
Attila Fazekasdc216422013-01-29 15:12:14 +0100274
Sean Dague35a7caf2013-05-10 10:38:22 -0400275def call_until_true(func, duration, sleep_for):
276 """
277 Call the given function until it returns True (and return True) or
278 until the specified duration (in seconds) elapses (and return
279 False).
280
281 :param func: A zero argument callable that returns True on success.
282 :param duration: The number of seconds for which to attempt a
283 successful call of the function.
284 :param sleep_for: The number of seconds to sleep after an unsuccessful
285 invocation of the function.
286 """
287 now = time.time()
288 timeout = now + duration
289 while now < timeout:
290 if func():
291 return True
292 LOG.debug("Sleeping for %d seconds", sleep_for)
293 time.sleep(sleep_for)
294 now = time.time()
295 return False