ZhiQiang Fan | 39f9722 | 2013-09-20 04:49:44 +0800 | [diff] [blame] | 1 | # Copyright 2012 OpenStack Foundation |
Jay Pipes | 32621f9 | 2012-01-05 20:41:40 -0500 | [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 | |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 16 | """Common utilities used in testing.""" |
Jay Pipes | 32621f9 | 2012-01-05 20:41:40 -0500 | [diff] [blame] | 17 | |
Masayuki Igawa | ba7bcf6 | 2014-02-17 14:56:41 +0900 | [diff] [blame] | 18 | from tempest import test |
Jay Pipes | 32621f9 | 2012-01-05 20:41:40 -0500 | [diff] [blame] | 19 | |
| 20 | |
| 21 | class 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 Kranz | d3bb405 | 2012-01-19 10:10:05 -0500 | [diff] [blame] | 29 | def _skipper(*args, **kw): |
Jay Pipes | 32621f9 | 2012-01-05 20:41:40 -0500 | [diff] [blame] | 30 | """Wrapped skipper function.""" |
| 31 | testobj = args[0] |
| 32 | if not getattr(testobj, self.attr, False): |
Masayuki Igawa | ba7bcf6 | 2014-02-17 14:56:41 +0900 | [diff] [blame] | 33 | raise test.BaseTestCase.skipException(self.message) |
Jay Pipes | 32621f9 | 2012-01-05 20:41:40 -0500 | [diff] [blame] | 34 | func(*args, **kw) |
| 35 | _skipper.__name__ = func.__name__ |
| 36 | _skipper.__doc__ = func.__doc__ |
| 37 | return _skipper |