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