| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
import abc |
| 17 |
import functools |
| 18 |
import inspect |
| 19 |
import os |
| 20 |
import sys |
| 21 |
|
| 22 |
import eventlet.timeout |
| 23 |
from os_vif import version as osvif_version |
| 24 |
from oslo_config import cfg |
| 25 |
from oslo_log import log as logging |
| 26 |
from oslo_utils import fileutils |
| 27 |
from oslotest import base |
| 28 |
|
| 29 |
|
| 30 |
CONF = cfg.CONF |
| 31 |
LOG = logging.getLogger(__name__) |
| 32 |
|
| 33 |
|
| 34 |
def _get_test_log_path(): |
| 35 |
return os.environ.get('OS_LOG_PATH', '/tmp') |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
DEFAULT_LOG_DIR = os.path.join(_get_test_log_path(), 'osvif-functional-logs') |
| 40 |
|
| 41 |
|
| 42 |
def wait_until_true(predicate, timeout=15, sleep=1): |
| 43 |
"""Wait until callable predicate is evaluated as True |
| 44 |
|
| 45 |
:param predicate: Callable deciding whether waiting should continue. |
| 46 |
Best practice is to instantiate predicate with |
| 47 |
``functools.partial()``. |
| 48 |
:param timeout: Timeout in seconds how long should function wait. |
| 49 |
:param sleep: Polling interval for results in seconds. |
| 50 |
:return: True if the predicate is evaluated as True within the timeout, |
| 51 |
False in case of timeout evaluating the predicate. |
| 52 |
""" |
| 53 |
try: |
| 54 |
with eventlet.Timeout(timeout): |
| 55 |
while not predicate(): |
| 56 |
eventlet.sleep(sleep) |
| 57 |
except eventlet.Timeout: |
| 58 |
return False |
| 59 |
return True |
| 60 |
|
| 61 |
|
| 62 |
class _CatchTimeoutMetaclass(abc.ABCMeta): |
| 63 |
def __init__(cls, name, bases, dct): |
| 64 |
super(_CatchTimeoutMetaclass, cls).__init__(name, bases, dct) |
| 65 |
for name, method in inspect.getmembers( |
| 66 |
|
| 67 |
|
| 68 |
cls, predicate=inspect.isroutine): |
| 69 |
if name.startswith('test_'): |
| 70 |
setattr(cls, name, cls._catch_timeout(method)) |
| 71 |
|
| 72 |
@staticmethod |
| 73 |
def _catch_timeout(f): |
| 74 |
@functools.wraps(f) |
| 75 |
def func(self, *args, **kwargs): |
| 76 |
try: |
| 77 |
return f(self, *args, **kwargs) |
| 78 |
except eventlet.Timeout as e: |
| 79 |
self.fail('Execution of this test timed out: %s' % e) |
| 80 |
return func |
| 81 |
|
| 82 |
|
| 83 |
def setup_logging(component_name): |
| 84 |
"""Sets up the logging options for a log with supplied name.""" |
| 85 |
logging.setup(cfg.CONF, component_name) |
| 86 |
LOG.info("Logging enabled!") |
| 87 |
LOG.info("%(prog)s version %(version)s", |
| 88 |
{'prog': sys.argv[0], 'version': osvif_version.__version__}) |
| 89 |
LOG.debug("command line: %s", " ".join(sys.argv)) |
| 90 |
|
| 91 |
|
| 92 |
def sanitize_log_path(path): |
| 93 |
"""Sanitize the string so that its log path is shell friendly""" |
| 94 |
return path.replace(' ', '-').replace('(', '_').replace(')', '_') |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
class BaseFunctionalTestCase(base.BaseTestCase, |
| 102 |
metaclass=_CatchTimeoutMetaclass): |
| 103 |
"""Base class for functional tests.""" |
| 104 |
|
| 105 |
COMPONENT_NAME = 'os_vif' |
| 106 |
PRIVILEGED_GROUP = 'os_vif_privileged' |
| 107 |
|
| 108 |
def setUp(self): |
| 109 |
super(BaseFunctionalTestCase, self).setUp() |
| 110 |
logging.register_options(CONF) |
| 111 |
setup_logging(self.COMPONENT_NAME) |
| 112 |
fileutils.ensure_tree(DEFAULT_LOG_DIR, mode=0o755) |
| 113 |
log_file = sanitize_log_path( |
| 114 |
os.path.join(DEFAULT_LOG_DIR, "%s.txt" % self.id())) |
| 115 |
self.flags(log_file=log_file) |
| 116 |
privsep_helper = os.path.join( |
| 117 |
os.getenv('VIRTUAL_ENV', os.path.dirname(sys.executable)[:-4]), |
| 118 |
'bin', 'privsep-helper') |
| 119 |
self.flags( |
| 120 |
helper_command=' '.join(['sudo', '-E', privsep_helper]), |
| 121 |
group=self.PRIVILEGED_GROUP) |
| 122 |
|
| 123 |
def flags(self, **kw): |
| 124 |
"""Override some configuration values. |
| 125 |
|
| 126 |
The keyword arguments are the names of configuration options to |
| 127 |
override and their values. |
| 128 |
|
| 129 |
If a group argument is supplied, the overrides are applied to |
| 130 |
the specified configuration option group. |
| 131 |
|
| 132 |
All overrides are automatically cleared at the end of the current |
| 133 |
test by the fixtures cleanup process. |
| 134 |
""" |
| 135 |
group = kw.pop('group', None) |
| 136 |
for k, v in kw.items(): |
| 137 |
CONF.set_override(k, v, group) |