| # -*- coding: utf-8 -*- |
| ''' |
| Management of runtest resources |
| =============================== |
| :depends: - jsonpath-rw Python module |
| :configuration: See :py:mod:`salt.modules.runtest` for setup instructions. |
| ''' |
| |
| import importlib |
| import logging |
| import os |
| from functools import wraps |
| LOG = logging.getLogger(__name__) |
| |
| DEPENDENCIES = ['jsonpath_rw'] |
| |
| def __virtual__(): |
| ''' |
| Only load if runtest module if dependencies are present |
| ''' |
| failures = [] |
| for module in DEPENDENCIES: |
| try: |
| importlib.import_module(module) |
| except ImportError: |
| failures.append(module) |
| if failures: |
| log.error("The required modules are not present %s" % ','.join(failures)) |
| return False |
| return 'runtest' |
| |
| def _test_call(method): |
| (resource, functionality) = method.func_name.split('_') |
| if functionality == 'present': |
| functionality = 'updated' |
| else: |
| functionality = 'removed' |
| |
| @wraps(method) |
| def check_for_testing(name, *args, **kwargs): |
| if __opts__.get('test', None): |
| return _no_change(name, resource, test=functionality) |
| return method(name, *args, **kwargs) |
| return check_for_testing |
| |
| @_test_call |
| def tempestconf_present(name, regenerate=True): |
| """ Checks that file with config present.""" |
| |
| |
| if os.path.isfile(name) and not regenerate: |
| return _no_change(name, 'tempest_config') |
| |
| cfg = __salt__['runtest.generate_tempest_config'](name) |
| return _created(name, 'tempest_config', cfg) |
| |
| |
| def _created(name, resource, resource_definition): |
| changes_dict = {'name': name, |
| 'changes': resource_definition, |
| 'result': True, |
| 'comment': '{0} {1} created'.format(resource, name)} |
| return changes_dict |
| |
| def _no_change(name, resource, test=False): |
| changes_dict = {'name': name, |
| 'changes': {}, |
| 'result': True} |
| if test: |
| changes_dict['comment'] = \ |
| '{0} {1} will be {2}'.format(resource, name, test) |
| else: |
| changes_dict['comment'] = \ |
| '{0} {1} is in correct state'.format(resource, name) |
| return changes_dict |