fix for sdnvpn CI test failure
[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 functest.utils import openstack_utils as os_utils
20 from sdnvpn.lib import config as sdnvpn_config
21 from sdnvpn.lib.gather_logs import gather_logs
22 from sdnvpn.lib import utils as test_utils
23
24 COMMON_CONFIG = sdnvpn_config.CommonConfig()
25
26
27 class SdnvpnFunctest(base.Feature):
28
29     __logger = logging.getLogger(__name__)
30
31     def execute(self):
32
33         nova_client = os_utils.get_nova_client()
34         neutron_client = os_utils.get_neutron_client()
35
36         tenant_id = os_utils.get_tenant_id(os_utils.get_keystone_client(),
37                                            os.environ['OS_PROJECT_NAME'])
38
39         neutron_quota = test_utils.get_neutron_quota(neutron_client, tenant_id)
40         (neutron_nw_quota, neutron_subnet_quota, neutron_port_quota,
41          neutron_router_quota) = (
42             neutron_quota['network'], neutron_quota['subnet'],
43             neutron_quota['port'], neutron_quota['router'])
44         instances_quota = test_utils.get_nova_instances_quota(nova_client)
45
46         self.__logger.info("Setting net/subnet/port/router "
47                            "quota to unlimited")
48         test_utils.update_nw_subnet_port_quota(
49             neutron_client,
50             tenant_id,
51             COMMON_CONFIG.neutron_nw_quota,
52             COMMON_CONFIG.neutron_subnet_quota,
53             COMMON_CONFIG.neutron_port_quota,
54             COMMON_CONFIG.neutron_router_quota)
55         test_utils.create_default_flavor()
56
57         # Workaround for
58         # https://jira.opnfv.org/projects/SDNVPN/issues/SDNVPN-115
59         self.__logger.info("Setting instances quota class to unlimited")
60         test_utils.update_instance_quota_class(
61             nova_client,
62             COMMON_CONFIG.nova_instances_quota_class)
63
64         with open(COMMON_CONFIG.config_file) as f:
65             config_yaml = yaml.safe_load(f)
66
67         testcases = config_yaml.get("testcases")
68         overall_status = "PASS"
69         for tc in testcases:
70             if testcases[tc]['enabled']:
71                 test_name = tc
72                 test_descr = testcases[tc]['description']
73                 title = ("Running '%s - %s'" %
74                          (test_name, test_descr))
75                 self.__logger.info(title)
76                 self.__logger.info("%s\n" % ("=" * len(title)))
77                 t = importlib.import_module(test_name, package=None)
78                 try:
79                     result = t.main()
80                 except Exception as ex:
81                     result = -1
82                     self.__logger.info("Caught Exception in %s: %s Trace: %s"
83                                        % (test_name, ex,
84                                           traceback.format_exc()))
85                 if result < 0:
86                     status = "FAIL"
87                     overall_status = "FAIL"
88                     self.__logger.info("Testcase %s failed" % test_name)
89                 else:
90                     status = result.get("status")
91                     self.details.update(
92                         {test_name: {'status': status,
93                                      'details': result.get("details")}})
94                     self.__logger.info("Results of test case '%s - %s':\n%s\n"
95                                        % (test_name, test_descr, result))
96
97                     if status == "FAIL":
98                         overall_status = "FAIL"
99
100         self.__logger.info("Resetting subnet/net/port quota")
101         test_utils.update_nw_subnet_port_quota(neutron_client,
102                                                tenant_id,
103                                                neutron_nw_quota,
104                                                neutron_subnet_quota,
105                                                neutron_port_quota,
106                                                neutron_router_quota)
107
108         self.__logger.info("Resetting instances quota class")
109         test_utils.update_instance_quota_class(nova_client, instances_quota)
110
111         try:
112             installer_type = str(os.environ['INSTALLER_TYPE'].lower())
113             if installer_type in ["fuel", "apex"]:
114                 gather_logs('overall')
115             else:
116                 self.__logger.info("Skipping log gathering because installer"
117                                    "type %s is neither fuel nor apex" %
118                                    installer_type)
119         except Exception as ex:
120             self.__logger.error(('Something went wrong in the Log gathering.'
121                                  'Ex: %s, Trace: %s')
122                                 % (ex, traceback.format_exc()))
123
124         if overall_status == "PASS":
125             self.result = 100
126             return base.Feature.EX_OK
127
128         return base.Feature.EX_RUN_ERROR
129
130
131 if __name__ == '__main__':
132     logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s'
133                         '- %(levelname)s - %(message)s')
134     SDNVPN = SdnvpnFunctest()
135     sys.exit(SDNVPN.execute())