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