Move msb k8s override values configuration on the top settings level
[testsuite/pythonsdk-tests.git] / run_test.py
1 import configparser
2 import importlib
3 import logging.config
4 import os
5 import sys
6
7 import onaptests.utils.exceptions as onap_test_exceptions
8
9
10 def get_entrypoints():
11     config = configparser.ConfigParser()
12     config.read('setup.cfg')
13     entry_points = config['entry_points']['xtesting.testcase']
14     config = configparser.ConfigParser()
15     config.read_string(f"[entry_points]\n{entry_points}")
16     entry_points = config['entry_points']
17     entry_points_result = {}
18     for test_name, entrypoint in entry_points.items():
19         test_scenario_module, test_class = entrypoint.split(":")
20         entry_points_result[test_name] = {
21             "module": test_scenario_module,
22             "class": test_class
23         }
24     return entry_points_result
25
26 def run_test(test_name, validation, entry_point, settings_module):
27     settings_env = "ONAP_PYTHON_SDK_SETTINGS"
28     if validation:
29         validation_env = "PYTHON_SDK_TESTS_VALIDATION"
30         os.environ[validation_env] = "True"
31     os.environ[settings_env] = f"onaptests.configuration.{test_name}_settings"
32     if not settings_module:
33         settings_module = importlib.import_module("onapsdk.configuration")
34     else:
35         settings_module = importlib.reload(settings_module)
36     settings = settings_module.settings
37     # logging configuration for onapsdk, it is not requested for onaptests
38     # Correction requested in onapsdk to avoid having this duplicate code
39     logging.config.dictConfig(settings.LOG_CONFIG)
40     logger = logging.getLogger(test_name)
41     logger.info(f"Running {test_name} test")
42
43     test_module = importlib.import_module(entry_point["module"])
44
45     test_instance = getattr(test_module, entry_point["class"])()
46     try:
47         test_instance.run()
48         test_instance.clean()
49         if validation:
50             test_instance.validate_execution()
51     except onap_test_exceptions.TestConfigurationException:
52         logger.error("Status Check configuration error")
53     return settings_module
54
55 def main(argv):
56     """Script is used to run one or all the tests.
57     
58     You need to specify a name of the test like 'basic_cps' or
59     keyword 'all' that tells to run all the tests. You can also
60     pass a second argument of any value that tells the script to run
61     test(s) in the validation mode that checks only a basic setup of
62     steps (like cleanup) and their execution in a certain order.
63
64     Examplary use:
65     - python run_test.py basic_vm_macro
66     - python run_test.py basic_cps validation
67     - python run_test.py all true
68     """
69     if len(argv) == 0:
70         print("Required test name argument missing", file=sys.stderr)
71         print("\nExample: python run_test.py basic_cps\n")
72         exit(1)
73     validation = len(argv) > 1
74     test_name = argv[0]
75     entry_points = get_entrypoints()
76     if test_name == "all":
77         settings_module = None
78         for test_name, entry_point in entry_points.items():
79             settings_module = run_test(test_name, validation, entry_point, settings_module)
80     else:
81         entry_point = entry_points[test_name]
82         run_test(test_name, validation, entry_point, None)
83
84 if __name__ == "__main__":
85     main(sys.argv[1:])