root | d8ab63c | 2017-07-25 07:19:31 +0000 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | ''' |
| 3 | Management of runtest resources |
| 4 | =============================== |
| 5 | :depends: - jsonpath-rw Python module |
| 6 | :configuration: See :py:mod:`salt.modules.runtest` for setup instructions. |
| 7 | ''' |
| 8 | |
| 9 | import importlib |
| 10 | import logging |
| 11 | import os |
| 12 | from functools import wraps |
| 13 | LOG = logging.getLogger(__name__) |
| 14 | |
| 15 | DEPENDENCIES = ['jsonpath_rw'] |
| 16 | |
| 17 | def __virtual__(): |
| 18 | ''' |
| 19 | Only load if runtest module if dependencies are present |
| 20 | ''' |
| 21 | failures = [] |
| 22 | for module in DEPENDENCIES: |
| 23 | try: |
| 24 | importlib.import_module(module) |
| 25 | except ImportError: |
| 26 | failures.append(module) |
| 27 | if failures: |
| 28 | log.error("The required modules are not present %s" % ','.join(failures)) |
| 29 | return False |
| 30 | return 'runtest' |
| 31 | |
| 32 | def _test_call(method): |
| 33 | (resource, functionality) = method.func_name.split('_') |
| 34 | if functionality == 'present': |
| 35 | functionality = 'updated' |
| 36 | else: |
| 37 | functionality = 'removed' |
| 38 | |
| 39 | @wraps(method) |
| 40 | def check_for_testing(name, *args, **kwargs): |
| 41 | if __opts__.get('test', None): |
| 42 | return _no_change(name, resource, test=functionality) |
| 43 | return method(name, *args, **kwargs) |
| 44 | return check_for_testing |
| 45 | |
| 46 | @_test_call |
| 47 | def tempestconf_present(name, regenerate=False): |
| 48 | """ Checks that file with config present.""" |
| 49 | |
| 50 | |
| 51 | if os.path.isfile(name) and not regenerate: |
| 52 | return _no_change(name, 'tempest_config') |
| 53 | |
| 54 | cfg = __salt__['runtest.generate_tempest_config'](name) |
| 55 | return _created(name, 'tempest_config', cfg) |
| 56 | |
| 57 | |
| 58 | def _created(name, resource, resource_definition): |
| 59 | changes_dict = {'name': name, |
| 60 | 'changes': resource_definition, |
| 61 | 'result': True, |
| 62 | 'comment': '{0} {1} created'.format(resource, name)} |
| 63 | return changes_dict |
| 64 | |
| 65 | def _no_change(name, resource, test=False): |
| 66 | changes_dict = {'name': name, |
| 67 | 'changes': {}, |
| 68 | 'result': True} |
| 69 | if test: |
| 70 | changes_dict['comment'] = \ |
| 71 | '{0} {1} will be {2}'.format(resource, name, test) |
| 72 | else: |
| 73 | changes_dict['comment'] = \ |
| 74 | '{0} {1} is in correct state'.format(resource, name) |
| 75 | return changes_dict |