write sdnvpn test result into db
[sdnvpn.git] / sdnvpn / test / functest / run_sdnvpn_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 importlib
12 import logging
13 import os
14 import sys
15 import time
16 import traceback
17 import yaml
18
19 from functest.core import testcase
20 from sdnvpn.lib import config as sdnvpn_config
21 from sdnvpn.lib.gather_logs import gather_logs
22
23 COMMON_CONFIG = sdnvpn_config.CommonConfig()
24
25
26 class SdnvpnFunctest(testcase.TestCase):
27
28     __logger = logging.getLogger(__name__)
29
30     def run(self):
31         self.start_time = time.time()
32
33         cmd_line = "neutron quota-update --subnet -1 --network -1 --port -1"
34         self.__logger.info("Setting subnet/net quota to unlimited : %s"
35                            % cmd_line)
36         cmd = os.popen(cmd_line)
37         output = cmd.read()
38         self.__logger.debug(output)
39
40         # Workaround for
41         # https://jira.opnfv.org/projects/SDNVPN/issues/SDNVPN-115
42         cmd_line = "nova quota-class-update --instances -1 default"
43         self.__logger.info("Setting instances quota to unlimited : %s"
44                            % cmd_line)
45         cmd = os.popen(cmd_line)
46         output = cmd.read()
47         self.__logger.debug(output)
48
49         with open(COMMON_CONFIG.config_file) as f:
50             config_yaml = yaml.safe_load(f)
51
52         testcases = config_yaml.get("testcases")
53         overall_status = "PASS"
54         for tc in testcases:
55             if testcases[tc]['enabled']:
56                 test_name = tc
57                 test_descr = testcases[tc]['description']
58                 title = ("Running '%s - %s'" %
59                          (test_name, test_descr))
60                 self.__logger.info(title)
61                 self.__logger.info("%s\n" % ("=" * len(title)))
62                 t = importlib.import_module(test_name, package=None)
63                 try:
64                     result = t.main()
65                 except Exception as ex:
66                     result = -1
67                     self.__logger.info("Caught Exception in %s: %s Trace: %s"
68                                        % (test_name, ex,
69                                           traceback.format_exc()))
70                 if result < 0:
71                     status = "FAIL"
72                     overall_status = "FAIL"
73                     self.__logger.info("Testcase %s failed" % test_name)
74                 else:
75                     status = result.get("status")
76                     self.details.update(
77                         {test_name: {'status': status,
78                                      'details': result.get("details")}})
79                     self.__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         self.stop_time = time.time()
86
87         # Ignoring the return value of push_to_db because we shouldn't make
88         # sdnvpn to fail for an issue related to db write.
89         self.push_to_db()
90
91         try:
92             gather_logs('overall')
93         except Exception as ex:
94             self.__logger.error(('Something went wrong in the Log gathering.'
95                                  'Ex: %s, Trace: %s')
96                                 % (ex, traceback.format_exc()))
97
98         if overall_status == "PASS":
99             self.result = 100
100             return testcase.TestCase.EX_OK
101
102         return testcase.TestCase.EX_RUN_ERROR
103
104
105 if __name__ == '__main__':
106     logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s'
107                         '- %(levelname)s - %(message)s')
108     SDNVPN = SdnvpnFunctest()
109     sys.exit(SDNVPN.run())