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