Make sdnvpn a package
[sdnvpn.git] / sdnvpn / test / functest / run_tests.py
1 #!/bin/python
2 #
3 # Copyright (c) 2015 All rights reserved
4 # This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10
11 import argparse
12 import importlib
13 import sys
14 import time
15 import yaml
16
17 import functest.utils.functest_logger as ft_logger
18 import functest.utils.functest_utils as ft_utils
19 from sdnvpn.lib import config as sdnvpn_config
20
21
22 parser = argparse.ArgumentParser()
23 parser.add_argument("-r", "--report",
24                     help="Create json result file",
25                     action="store_true")
26 args = parser.parse_args()
27
28 TEST_DB_URL = ft_utils.get_functest_config('results.test_db_url')
29 logger = ft_logger.Logger("sdnvpn-run-tests").getLogger()
30
31 COMMON_CONFIG = sdnvpn_config.CommonConfig()
32
33
34 def push_results(testname, start_time, end_time, criteria, details):
35     logger.info("Push testcase '%s' results into the DB...\n" % testname)
36     ft_utils.push_results_to_db("sdnvpn",
37                                 testname,
38                                 start_time,
39                                 end_time,
40                                 criteria,
41                                 details)
42
43
44 def main():
45
46     with open(COMMON_CONFIG.config_file) as f:
47         config_yaml = yaml.safe_load(f)
48
49     testcases = config_yaml.get("testcases")
50     overall_status = "PASS"
51     for testcase in testcases:
52         if testcases[testcase]['enabled']:
53             test_name = testcase
54             test_descr = testcases[testcase]['description']
55             test_name_db = testcases[testcase]['testname_db']
56             title = ("Running '%s - %s'" %
57                      (test_name, test_descr))
58             logger.info(title)
59             logger.info("%s\n" % ("=" * len(title)))
60             t = importlib.import_module(testcase, package=None)
61             start_time = time.time()
62             result = t.main()
63             end_time = time.time()
64             if result < 0:
65                 status = "FAIL"
66                 overall_status = "FAIL"
67             else:
68                 status = result.get("status")
69                 details = result.get("details")
70                 logger.info("Results of test case '%s - %s':\n%s\n" %
71                             (test_name, test_descr, result))
72
73                 if status == "FAIL":
74                     overall_status = "FAIL"
75
76             if args.report:
77                 push_results(
78                     test_name_db, start_time, end_time, status, details)
79
80     if overall_status == "FAIL":
81         sys.exit(-1)
82
83     sys.exit(0)
84
85
86 if __name__ == '__main__':
87     main()