"Fossies" - the Fresh Open Source Software Archive

Member "keystone-18.0.0/keystone/tests/unit/utils.py" (14 Oct 2020, 3903 Bytes) of package /linux/misc/openstack/keystone-18.0.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "utils.py": 17.0.0_vs_18.0.0.

    1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
    2 # not use this file except in compliance with the License. You may obtain
    3 # a copy of the License at
    4 #
    5 #      http://www.apache.org/licenses/LICENSE-2.0
    6 #
    7 # Unless required by applicable law or agreed to in writing, software
    8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   10 # License for the specific language governing permissions and limitations
   11 # under the License.
   12 
   13 """Useful utilities for tests."""
   14 
   15 import functools
   16 import os
   17 import time
   18 import uuid
   19 
   20 from testtools import testcase
   21 
   22 
   23 TZ = None
   24 
   25 
   26 def timezone(func):
   27     @functools.wraps(func)
   28     def wrapper(*args, **kwargs):
   29         tz_original = os.environ.get('TZ')
   30         try:
   31             if TZ:
   32                 os.environ['TZ'] = TZ
   33                 time.tzset()
   34             return func(*args, **kwargs)
   35         finally:
   36             if TZ:
   37                 if tz_original:
   38                     os.environ['TZ'] = tz_original
   39                 else:
   40                     if 'TZ' in os.environ:
   41                         del os.environ['TZ']
   42                 time.tzset()
   43     return wrapper
   44 
   45 
   46 def new_uuid():
   47     """Return a string UUID."""
   48     return uuid.uuid4().hex
   49 
   50 
   51 def wip(message, expected_exception=Exception, bug=None):
   52     """Mark a test as work in progress.
   53 
   54     Based on code by Nat Pryce:
   55     https://gist.github.com/npryce/997195#file-wip-py
   56 
   57     The test will always be run. If the test fails then a TestSkipped
   58     exception is raised. If the test passes an AssertionError exception
   59     is raised so that the developer knows they made the test pass. This
   60     is a reminder to remove the decorator.
   61 
   62     :param message: a string message to help clarify why the test is
   63                     marked as a work in progress
   64     :param expected_exception: an exception class that will be checked for
   65                                when @wip verifies an exception is raised. The
   66                                test will fail if a different exception is
   67                                raised. Default is "any" exception is valid
   68     :param bug: (optional) a string for tracking the bug and what bug should
   69                 cause the @wip decorator to be removed from the testcase
   70 
   71     Usage:
   72       >>> @wip('Expected Error', expected_exception=Exception, bug="#000000")
   73       >>> def test():
   74       >>>     pass
   75 
   76     """
   77     if bug:
   78         bugstr = " (BugID " + bug + ")"
   79     else:
   80         bugstr = ""
   81 
   82     def _wip(f):
   83         @functools.wraps(f)
   84         def run_test(*args, **kwargs):
   85             __e = None
   86             try:
   87                 f(*args, **kwargs)
   88             except Exception as __e:  # noqa F841
   89                 if (expected_exception != Exception and
   90                         not isinstance(__e, expected_exception)):
   91                     raise AssertionError(
   92                         'Work In Progress Test Failed%(bugstr)s with '
   93                         'unexpected exception. Expected "%(expected)s" '
   94                         'got "%(exception)s": %(message)s ' %
   95                         {'message': message, 'bugstr': bugstr,
   96                          'expected': expected_exception.__class__.__name__,
   97                          'exception': __e.__class__.__name__})
   98                 # NOTE(notmorgan): We got the expected exception we can safely
   99                 # skip this test.
  100                 raise testcase.TestSkipped(
  101                     'Work In Progress Test Failed as '
  102                     'expected%(bugstr)s: %(message)s' %
  103                     {'message': message, 'bugstr': bugstr})
  104 
  105             raise AssertionError('Work In Progress Test Passed%(bugstr)s: '
  106                                  '%(message)s' % {'message': message,
  107                                                   'bugstr': bugstr})
  108 
  109         return run_test
  110 
  111     return _wip