blob: 00c93b77693fd2aba97f33438518f3d9257d6a4a [file] [log] [blame]
ZhiQiang Fan39f97222013-09-20 04:49:44 +08001# Copyright 2012 OpenStack Foundation
Jay Pipes32621f92012-01-05 20:41:40 -05002# 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
Sean Daguef237ccb2013-01-04 15:19:14 -050016"""Common utilities used in testing."""
Jay Pipes32621f92012-01-05 20:41:40 -050017
Masayuki Igawaba7bcf62014-02-17 14:56:41 +090018from tempest import test
Jay Pipes32621f92012-01-05 20:41:40 -050019
20
21class skip_unless_attr(object):
22 """Decorator that skips a test if a specified attr exists and is True."""
23 def __init__(self, attr, msg=None):
24 self.attr = attr
25 self.message = msg or ("Test case attribute %s not found "
26 "or False") % attr
27
28 def __call__(self, func):
David Kranzd3bb4052012-01-19 10:10:05 -050029 def _skipper(*args, **kw):
Jay Pipes32621f92012-01-05 20:41:40 -050030 """Wrapped skipper function."""
31 testobj = args[0]
32 if not getattr(testobj, self.attr, False):
Masayuki Igawaba7bcf62014-02-17 14:56:41 +090033 raise test.BaseTestCase.skipException(self.message)
Jay Pipes32621f92012-01-05 20:41:40 -050034 func(*args, **kw)
35 _skipper.__name__ = func.__name__
36 _skipper.__doc__ = func.__doc__
37 return _skipper