Refactor config global variables
[sdnvpn.git] / test / functest / config.py
1 import yaml
2 import os
3
4 import functest.utils.functest_logger as ft_logger
5 import functest.utils.functest_utils as ft_utils
6
7 logger = ft_logger.Logger("sndvpn_test_config").getLogger()
8
9
10 class CommonConfig(object):
11     """
12     Common configuration parameters across testcases
13     """
14     def __init__(self):
15         self.repo_path = os.path.join(os.environ['repos_dir'], 'sdnvpn')
16         self.config_file = os.path.join(self.repo_path,
17                                         'test/functest/config.yaml')
18         self.keyfile_path = os.path.join(self.repo_path,
19                                          'test/functest/id_rsa')
20         self.test_db = ft_utils.get_functest_config("results.test_db_url")
21         self.line_length = 90  # length for the summary table
22         self.vm_boot_timeout = 180
23         self.default_flavor = ft_utils.get_parameter_from_yaml(
24             "defaults.flavor", self.config_file)
25         self.image_filename = ft_utils.get_functest_config(
26             "general.openstack.image_file_name")
27         self.image_format = ft_utils.get_functest_config(
28             "general.openstack.image_disk_format")
29         self.image_path = '{0}/{1}'.format(
30             ft_utils.get_functest_config(
31                 "general.directories.dir_functest_data"),
32             self.image_filename)
33
34
35 class TestcaseConfig(object):
36     """
37     Configuration for a testcase.
38     Parse config.yaml into a dict and create an object out of it.
39     """
40     def __init__(self, testcase):
41         common_config = CommonConfig()
42         test_config = None
43         with open(common_config.config_file) as f:
44             testcases_yaml = yaml.safe_load(f)
45             test_config = testcases_yaml['testcases'].get(testcase, None)
46         if test_config is None:
47             logger.error('Test {0} configuration is not present in {1}'
48                          .format(testcase, common_config.config_file))
49         # Update class fields with configuration variables dynamically
50         self.__dict__.update(**test_config)