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