Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 1 | # Copyright 2015 Hewlett-Packard Development Company, L.P. |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | # not use this file except in compliance with the License. You may obtain |
| 5 | # a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | # License for the specific language governing permissions and limitations |
| 13 | # under the License. |
| 14 | |
| 15 | import functools |
| 16 | import uuid |
| 17 | |
| 18 | import six |
| 19 | import testtools |
| 20 | |
| 21 | |
| 22 | def skip_because(*args, **kwargs): |
| 23 | """A decorator useful to skip tests hitting known bugs |
| 24 | |
| 25 | @param bug: bug number causing the test to skip |
| 26 | @param condition: optional condition to be True for the skip to have place |
| 27 | """ |
| 28 | def decorator(f): |
| 29 | @functools.wraps(f) |
| 30 | def wrapper(self, *func_args, **func_kwargs): |
| 31 | skip = False |
| 32 | if "condition" in kwargs: |
| 33 | if kwargs["condition"] is True: |
| 34 | skip = True |
| 35 | else: |
| 36 | skip = True |
| 37 | if "bug" in kwargs and skip is True: |
| 38 | if not kwargs['bug'].isdigit(): |
| 39 | raise ValueError('bug must be a valid bug number') |
| 40 | msg = "Skipped until Bug: %s is resolved." % kwargs["bug"] |
| 41 | raise testtools.TestCase.skipException(msg) |
| 42 | return f(self, *func_args, **func_kwargs) |
| 43 | return wrapper |
| 44 | return decorator |
| 45 | |
| 46 | |
| 47 | def idempotent_id(id): |
| 48 | """Stub for metadata decorator""" |
| 49 | if not isinstance(id, six.string_types): |
| 50 | raise TypeError('Test idempotent_id must be string not %s' |
| 51 | '' % type(id).__name__) |
| 52 | uuid.UUID(id) |
| 53 | |
| 54 | def decorator(f): |
| 55 | f = testtools.testcase.attr('id-%s' % id)(f) |
| 56 | if f.__doc__: |
| 57 | f.__doc__ = 'Test idempotent id: %s\n%s' % (id, f.__doc__) |
| 58 | else: |
| 59 | f.__doc__ = 'Test idempotent id: %s' % id |
| 60 | return f |
| 61 | return decorator |
| 62 | |
| 63 | |
| 64 | class skip_unless_attr(object): |
| 65 | """Decorator to skip tests if a specified attr does not exists or False""" |
| 66 | def __init__(self, attr, msg=None): |
| 67 | self.attr = attr |
| 68 | self.message = msg or ("Test case attribute %s not found " |
| 69 | "or False") % attr |
| 70 | |
| 71 | def __call__(self, func): |
lkuchlan | c5f48b8 | 2016-05-29 13:02:16 +0300 | [diff] [blame] | 72 | @functools.wraps(func) |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 73 | def _skipper(*args, **kw): |
| 74 | """Wrapped skipper function.""" |
| 75 | testobj = args[0] |
| 76 | if not getattr(testobj, self.attr, False): |
| 77 | raise testtools.TestCase.skipException(self.message) |
| 78 | func(*args, **kw) |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 79 | return _skipper |