aboutsummaryrefslogtreecommitdiffstats
path: root/run_test.py
blob: a87f6a3eae8fc221549fbdc5d1b616ee7b92b18f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import configparser
import importlib
import logging.config
import os
import sys

import onaptests.utils.exceptions as onap_test_exceptions


def get_entrypoints():
    config = configparser.ConfigParser()
    config.read('setup.cfg')
    entry_points = config['entry_points']['xtesting.testcase']
    config = configparser.ConfigParser()
    config.read_string(f"[entry_points]\n{entry_points}")
    entry_points = config['entry_points']
    entry_points_result = {}
    for test_name, entrypoint in entry_points.items():
        test_scenario_module, test_class = entrypoint.split(":")
        entry_points_result[test_name] = {
            "module": test_scenario_module,
            "class": test_class
        }
    return entry_points_result

def run_test(test_name, validation, entry_point, settings_module):
    settings_env = "ONAP_PYTHON_SDK_SETTINGS"
    if validation:
        validation_env = "PYTHON_SDK_TESTS_VALIDATION"
        os.environ[validation_env] = "True"
    os.environ[settings_env] = f"onaptests.configuration.{test_name}_settings"
    if not settings_module:
        settings_module = importlib.import_module("onapsdk.configuration")
    else:
        settings_module = importlib.reload(settings_module)
    settings = settings_module.settings
    # logging configuration for onapsdk, it is not requested for onaptests
    # Correction requested in onapsdk to avoid having this duplicate code
    logging.config.dictConfig(settings.LOG_CONFIG)
    logger = logging.getLogger(test_name)
    logger.info(f"Running {test_name} test")

    test_module = importlib.import_module(entry_point["module"])

    test_instance = getattr(test_module, entry_point["class"])()
    try:
        test_instance.run()
        test_instance.clean()
        if validation:
            test_instance.validate_execution()
    except onap_test_exceptions.TestConfigurationException:
        logger.error("Status Check configuration error")
    return settings_module

def main(argv):
    """Script is used to run one or all the tests.
    
    You need to specify a name of the test like 'basic_cps' or
    keyword 'all' that tells to run all the tests. You can also
    pass a second argument of any value that tells the script to run
    test(s) in the validation mode that checks only a basic setup of
    steps (like cleanup) and their execution in a certain order.

    Examplary use:
    - python run_test.py basic_vm_macro
    - python run_test.py basic_cps validation
    - python run_test.py all true
    """
    if len(argv) == 0:
        print("Required test name argument missing", file=sys.stderr)
        print("\nExample: python run_test.py basic_cps\n")
        exit(1)
    validation = len(argv) > 1
    test_name = argv[0]
    entry_points = get_entrypoints()
    if test_name == "all":
        settings_module = None
        for test_name, entry_point in entry_points.items():
            settings_module = run_test(test_name, validation, entry_point, settings_module)
    else:
        entry_point = entry_points[test_name]
        run_test(test_name, validation, entry_point, None)

if __name__ == "__main__":
    main(sys.argv[1:])