blob: 61d13117e640456693eb280a85a368830023dac0 [file] [log] [blame]
ZhiQiang Fan39f97222013-09-20 04:49:44 +08001# Copyright 2012 OpenStack Foundation
Jay Pipes051075a2012-04-28 17:39:37 -04002# 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 Fazekasf86fa312013-07-30 19:56:39 +020016import atexit
Masayuki Igawa80c1b9f2013-10-07 17:19:11 +090017import functools
Ian Wienand98c35f32013-07-23 20:34:23 +100018import os
Jay Pipes051075a2012-04-28 17:39:37 -040019import time
20
Matthew Treinish78561ad2013-07-26 11:41:56 -040021import fixtures
Chris Yeoh55530bb2013-02-08 16:04:27 +103022import nose.plugins.attrib
Attila Fazekasdc216422013-01-29 15:12:14 +010023import testresources
ivan-zhu1feeb382013-01-24 10:14:39 +080024import testtools
Jay Pipes051075a2012-04-28 17:39:37 -040025
Matthew Treinish3e046852013-07-23 16:00:24 -040026from tempest import clients
Ryan Hsu6c4bb3d2013-10-21 21:22:50 -070027from tempest.common import isolated_creds
Attila Fazekasdc216422013-01-29 15:12:14 +010028from tempest import config
Matthew Treinish16c43792013-09-09 19:55:23 +000029from tempest import exceptions
Matthew Treinishf4a9b0f2013-07-26 16:58:26 -040030from tempest.openstack.common import log as logging
Jay Pipes051075a2012-04-28 17:39:37 -040031
32LOG = logging.getLogger(__name__)
33
Sean Dague86bd8422013-12-20 09:56:44 -050034CONF = config.CONF
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
Ken'ichi Ohmichia1aa44c2013-12-06 20:48:24 +0900113 @param interface: skip the test if it is the same as self._interface
Giulio Fidente83181a92013-10-01 06:02:24 +0200114 """
115 def decorator(f):
Masayuki Igawa80c1b9f2013-10-07 17:19:11 +0900116 @functools.wraps(f)
Ken'ichi Ohmichia1aa44c2013-12-06 20:48:24 +0900117 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 Igawa80c1b9f2013-10-07 17:19:11 +0900131 return wrapper
Giulio Fidente83181a92013-10-01 06:02:24 +0200132 return decorator
133
134
Matthew Treinishe3d26142013-11-26 19:14:58 +0000135def 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
154def is_extension_enabled(extension_name, service):
155 """A function that will check the list of enabled extensions from config
156
157 """
Sean Dague86bd8422013-12-20 09:56:44 -0500158 configs = CONF
Matthew Treinishe3d26142013-11-26 19:14:58 +0000159 config_dict = {
160 'compute': configs.compute_feature_enabled.api_extensions,
161 'compute_v3': configs.compute_feature_enabled.api_v3_extensions,
162 'volume': configs.volume_feature_enabled.api_extensions,
163 'network': configs.network_feature_enabled.api_extensions,
Matthew Treinish20345382013-12-13 17:04:23 +0000164 'object': configs.object_storage_feature_enabled.discoverable_apis,
Matthew Treinishe3d26142013-11-26 19:14:58 +0000165 }
166 if config_dict[service][0] == 'all':
167 return True
168 if extension_name in config_dict[service]:
169 return True
170 return False
171
Ian Wienand98c35f32013-07-23 20:34:23 +1000172# there is a mis-match between nose and testtools for older pythons.
173# testtools will set skipException to be either
174# unittest.case.SkipTest, unittest2.case.SkipTest or an internal skip
175# exception, depending on what it can find. Python <2.7 doesn't have
176# unittest.case.SkipTest; so if unittest2 is not installed it falls
177# back to the internal class.
178#
179# The current nose skip plugin will decide to raise either
180# unittest.case.SkipTest or its own internal exception; it does not
181# look for unittest2 or the internal unittest exception. Thus we must
182# monkey-patch testtools.TestCase.skipException to be the exception
183# the nose skip plugin expects.
184#
185# However, with the switch to testr nose may not be available, so we
186# require you to opt-in to this fix with an environment variable.
187#
188# This is temporary until upstream nose starts looking for unittest2
189# as testtools does; we can then remove this and ensure unittest2 is
190# available for older pythons; then nose and testtools will agree
191# unittest2.case.SkipTest is the one-true skip test exception.
192#
193# https://review.openstack.org/#/c/33056
194# https://github.com/nose-devs/nose/pull/699
195if 'TEMPEST_PY26_NOSE_COMPAT' in os.environ:
196 try:
197 import unittest.case.SkipTest
198 # convince pep8 we're using the import...
199 if unittest.case.SkipTest:
200 pass
201 raise RuntimeError("You have unittest.case.SkipTest; "
202 "no need to override")
203 except ImportError:
204 LOG.info("Overriding skipException to nose SkipTest")
205 testtools.TestCase.skipException = nose.plugins.skip.SkipTest
206
Attila Fazekasf86fa312013-07-30 19:56:39 +0200207at_exit_set = set()
208
209
210def validate_tearDownClass():
211 if at_exit_set:
Alex Gaynor94560d42013-08-23 05:41:23 -0700212 raise RuntimeError("tearDownClass does not calls the super's "
Attila Fazekasf86fa312013-07-30 19:56:39 +0200213 "tearDownClass in these classes: "
Attila Fazekasd5d43b82013-10-09 16:02:19 +0200214 + str(at_exit_set) + "\n"
215 "If you see the exception, with another "
216 "exception please do not report this one!"
217 "If you are changing tempest code, make sure you",
218 "are calling the super class's tearDownClass!")
Attila Fazekasf86fa312013-07-30 19:56:39 +0200219
220atexit.register(validate_tearDownClass)
221
Ian Wienand98c35f32013-07-23 20:34:23 +1000222
Attila Fazekasdc216422013-01-29 15:12:14 +0100223class BaseTestCase(testtools.TestCase,
224 testtools.testcase.WithAttributes,
225 testresources.ResourcedTestCase):
Attila Fazekasc43fec82013-04-09 23:17:52 +0200226
Sean Dague86bd8422013-12-20 09:56:44 -0500227 config = CONF
Attila Fazekasdc216422013-01-29 15:12:14 +0100228
Attila Fazekasf86fa312013-07-30 19:56:39 +0200229 setUpClassCalled = False
230
Matthew Treinish9f756a02014-01-15 10:26:07 -0500231 network_resources = {}
232
Pavel Sedlák1053bd32013-04-16 16:47:40 +0200233 @classmethod
234 def setUpClass(cls):
235 if hasattr(super(BaseTestCase, cls), 'setUpClass'):
236 super(BaseTestCase, cls).setUpClass()
Attila Fazekasf86fa312013-07-30 19:56:39 +0200237 cls.setUpClassCalled = True
Pavel Sedlák1053bd32013-04-16 16:47:40 +0200238
Attila Fazekasf86fa312013-07-30 19:56:39 +0200239 @classmethod
240 def tearDownClass(cls):
Attila Fazekas5d275302013-08-29 12:35:12 +0200241 at_exit_set.discard(cls)
Attila Fazekasf86fa312013-07-30 19:56:39 +0200242 if hasattr(super(BaseTestCase, cls), 'tearDownClass'):
243 super(BaseTestCase, cls).tearDownClass()
244
245 def setUp(self):
246 super(BaseTestCase, self).setUp()
247 if not self.setUpClassCalled:
248 raise RuntimeError("setUpClass does not calls the super's"
249 "setUpClass in the "
250 + self.__class__.__name__)
251 at_exit_set.add(self.__class__)
Matthew Treinish78561ad2013-07-26 11:41:56 -0400252 test_timeout = os.environ.get('OS_TEST_TIMEOUT', 0)
253 try:
254 test_timeout = int(test_timeout)
255 except ValueError:
256 test_timeout = 0
257 if test_timeout > 0:
Attila Fazekasf86fa312013-07-30 19:56:39 +0200258 self.useFixture(fixtures.Timeout(test_timeout, gentle=True))
Matthew Treinish78561ad2013-07-26 11:41:56 -0400259
260 if (os.environ.get('OS_STDOUT_CAPTURE') == 'True' or
261 os.environ.get('OS_STDOUT_CAPTURE') == '1'):
Attila Fazekasf86fa312013-07-30 19:56:39 +0200262 stdout = self.useFixture(fixtures.StringStream('stdout')).stream
263 self.useFixture(fixtures.MonkeyPatch('sys.stdout', stdout))
Matthew Treinish78561ad2013-07-26 11:41:56 -0400264 if (os.environ.get('OS_STDERR_CAPTURE') == 'True' or
265 os.environ.get('OS_STDERR_CAPTURE') == '1'):
Attila Fazekasf86fa312013-07-30 19:56:39 +0200266 stderr = self.useFixture(fixtures.StringStream('stderr')).stream
267 self.useFixture(fixtures.MonkeyPatch('sys.stderr', stderr))
Attila Fazekas31388072013-08-15 08:58:07 +0200268 if (os.environ.get('OS_LOG_CAPTURE') != 'False' and
269 os.environ.get('OS_LOG_CAPTURE') != '0'):
270 log_format = '%(asctime)-15s %(message)s'
271 self.useFixture(fixtures.LoggerFixture(nuke_handlers=False,
Attila Fazekas90445be2013-10-24 16:46:03 +0200272 format=log_format,
273 level=None))
Matthew Treinish78561ad2013-07-26 11:41:56 -0400274
Matthew Treinish3e046852013-07-23 16:00:24 -0400275 @classmethod
Ryan Hsu6c4bb3d2013-10-21 21:22:50 -0700276 def get_client_manager(cls):
277 """
278 Returns an Openstack client manager
279 """
Matthew Treinish9f756a02014-01-15 10:26:07 -0500280 cls.isolated_creds = isolated_creds.IsolatedCreds(
281 cls.__name__, network_resources=cls.network_resources)
Ryan Hsu6c4bb3d2013-10-21 21:22:50 -0700282
283 force_tenant_isolation = getattr(cls, 'force_tenant_isolation', None)
284 if (cls.config.compute.allow_tenant_isolation or
285 force_tenant_isolation):
286 creds = cls.isolated_creds.get_primary_creds()
287 username, tenant_name, password = creds
288 os = clients.Manager(username=username,
289 password=password,
290 tenant_name=tenant_name,
291 interface=cls._interface)
292 else:
293 os = clients.Manager(interface=cls._interface)
294 return os
295
296 @classmethod
297 def clear_isolated_creds(cls):
298 """
299 Clears isolated creds if set
300 """
301 if getattr(cls, 'isolated_creds'):
302 cls.isolated_creds.clear_isolated_creds()
303
304 @classmethod
Matthew Treinish3e046852013-07-23 16:00:24 -0400305 def _get_identity_admin_client(cls):
306 """
307 Returns an instance of the Identity Admin API client
308 """
309 os = clients.AdminManager(interface=cls._interface)
310 admin_client = os.identity_client
311 return admin_client
312
313 @classmethod
314 def _get_client_args(cls):
315
316 return (
317 cls.config,
318 cls.config.identity.admin_username,
319 cls.config.identity.admin_password,
320 cls.config.identity.uri
321 )
322
Matthew Treinish9f756a02014-01-15 10:26:07 -0500323 @classmethod
324 def set_network_resources(self, network=False, router=False, subnet=False,
325 dhcp=False):
326 """Specify which network resources should be created
327
328 @param network
329 @param router
330 @param subnet
331 @param dhcp
332 """
Salvatore Orlando5a337242014-01-15 22:49:22 +0000333 # network resources should be set only once from callers
334 # in order to ensure that even if it's called multiple times in
335 # a chain of overloaded methods, the attribute is set only
336 # in the leaf class
337 if not self.network_resources:
338 self.network_resources = {
339 'network': network,
340 'router': router,
341 'subnet': subnet,
342 'dhcp': dhcp}
Matthew Treinish9f756a02014-01-15 10:26:07 -0500343
Attila Fazekasdc216422013-01-29 15:12:14 +0100344
Sean Dague35a7caf2013-05-10 10:38:22 -0400345def call_until_true(func, duration, sleep_for):
346 """
347 Call the given function until it returns True (and return True) or
348 until the specified duration (in seconds) elapses (and return
349 False).
350
351 :param func: A zero argument callable that returns True on success.
352 :param duration: The number of seconds for which to attempt a
353 successful call of the function.
354 :param sleep_for: The number of seconds to sleep after an unsuccessful
355 invocation of the function.
356 """
357 now = time.time()
358 timeout = now + duration
359 while now < timeout:
360 if func():
361 return True
362 LOG.debug("Sleeping for %d seconds", sleep_for)
363 time.sleep(sleep_for)
364 now = time.time()
365 return False