Merge "Support different user/project domain values"
[functest.git] / functest / utils / config.py
1 #!/usr/bin/env python
2
3 # pylint: disable=missing-docstring
4
5 import pkg_resources
6 import yaml
7
8 import six
9
10 from functest.utils import env
11
12
13 class Config(object):
14     def __init__(self):
15         try:
16             # pylint: disable=bad-continuation
17             with open(pkg_resources.resource_filename(
18                     'functest', 'ci/config_functest.yaml')) as yfile:
19                 self.functest_yaml = yaml.safe_load(yfile)
20         except Exception as error:
21             raise Exception('Parse config failed: {}'.format(str(error)))
22
23     @staticmethod
24     def _merge_dicts(dict1, dict2):
25         for k in set(dict1.keys()).union(dict2.keys()):
26             if k in dict1 and k in dict2:
27                 if isinstance(dict1[k], dict) and isinstance(dict2[k], dict):
28                     yield (k, dict(Config._merge_dicts(dict1[k], dict2[k])))
29                 else:
30                     yield (k, dict2[k])
31             elif k in dict1:
32                 yield (k, dict1[k])
33             else:
34                 yield (k, dict2[k])
35
36     def patch_file(self, patch_file_path):
37         with open(patch_file_path) as yfile:
38             patch_file = yaml.safe_load(yfile)
39
40         for key in patch_file:
41             if key in env.get('DEPLOY_SCENARIO'):
42                 self.functest_yaml = dict(Config._merge_dicts(
43                     self.functest_yaml, patch_file[key]))
44
45     def _parse(self, attr_now, left_parametes):
46         for param_n, param_v in six.iteritems(left_parametes):
47             attr_further = self._get_attr_further(attr_now, param_n)
48             if attr_further:
49                 setattr(self, attr_further, param_v)
50             if isinstance(param_v, dict):
51                 self._parse(attr_further, param_v)
52
53     @staticmethod
54     def _get_attr_further(attr_now, next):  # pylint: disable=redefined-builtin
55         return attr_now if next == 'general' else (
56             '{}_{}'.format(attr_now, next) if attr_now else next)
57
58     def fill(self):
59         try:
60             self._parse(None, self.functest_yaml)
61         except Exception as error:
62             raise Exception('Parse config failed: {}'.format(str(error)))
63
64
65 CONF = Config()
66 CONF.patch_file(pkg_resources.resource_filename(
67     'functest', 'ci/config_patch.yaml'))
68 if env.get("POD_ARCH") in ['aarch64']:
69     CONF.patch_file(pkg_resources.resource_filename(
70         'functest', 'ci/config_aarch64_patch.yaml'))
71 CONF.fill()