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