Merge "Prepare run_tests to be called directly to main()"
[sdnvpn.git] / sdnvpn / test / functest / run_tests.py
1 #!/bin/python
2 #
3 # Copyright (c) 2017 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 os
14 import sys
15 import time
16 import yaml
17
18 import functest.utils.functest_logger as ft_logger
19 import functest.utils.functest_utils as ft_utils
20 from sdnvpn.lib import config as sdnvpn_config
21
22
23 logger = ft_logger.Logger(__name__).getLogger()
24
25 COMMON_CONFIG = sdnvpn_config.CommonConfig()
26 TEST_DB_URL = COMMON_CONFIG.test_db
27
28
29 def push_results(testname, start_time, end_time, criteria, details):
30     logger.info("Push testcase '%s' results into the DB...\n" % testname)
31     ft_utils.push_results_to_db("sdnvpn",
32                                 testname,
33                                 start_time,
34                                 end_time,
35                                 criteria,
36                                 details)
37
38
39 def main(report=False):
40     # Workaround for https://jira.opnfv.org/projects/SDNVPN/issues/SDNVPN-100
41     # and SDNVPN-126
42     cmd_line = "neutron quota-update --subnet -1 --network -1 --port -1"
43     logger.info("Setting subnet/net quota to unlimited : %s" % cmd_line)
44     cmd = os.popen(cmd_line)
45     output = cmd.read()
46     logger.debug(output)
47
48     # Workaround for https://jira.opnfv.org/projects/SDNVPN/issues/SDNVPN-115
49     cmd_line = "nova quota-class-update --instances -1 default"
50     logger.info("Setting instances quota to unlimited : %s" % cmd_line)
51     cmd = os.popen(cmd_line)
52     output = cmd.read()
53     logger.debug(output)
54
55     with open(COMMON_CONFIG.config_file) as f:
56         config_yaml = yaml.safe_load(f)
57
58     testcases = config_yaml.get("testcases")
59     overall_status = "PASS"
60     for testcase in testcases:
61         if testcases[testcase]['enabled']:
62             test_name = testcase
63             test_descr = testcases[testcase]['description']
64             test_name_db = testcases[testcase]['testname_db']
65             title = ("Running '%s - %s'" %
66                      (test_name, test_descr))
67             logger.info(title)
68             logger.info("%s\n" % ("=" * len(title)))
69             t = importlib.import_module(testcase, package=None)
70             start_time = time.time()
71             result = t.main()
72             end_time = time.time()
73             if result < 0:
74                 status = "FAIL"
75                 overall_status = "FAIL"
76             else:
77                 status = result.get("status")
78                 details = result.get("details")
79                 logger.info("Results of test case '%s - %s':\n%s\n" %
80                             (test_name, test_descr, result))
81
82                 if status == "FAIL":
83                     overall_status = "FAIL"
84
85             if report:
86                 push_results(
87                     test_name_db, start_time, end_time, status, details)
88
89     if overall_status == "FAIL":
90         sys.exit(-1)
91
92     sys.exit(0)
93
94
95 if __name__ == '__main__':
96     parser = argparse.ArgumentParser()
97     parser.add_argument("-r", "--report",
98                         help="Create json result file",
99                         action="store_true")
100     args = parser.parse_args()
101     main(report=args.report)