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