Add own logger for SdnvpnFuncTest class
[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 logger = logging.getLogger(__name__)
25
26 COMMON_CONFIG = sdnvpn_config.CommonConfig()
27
28
29 class SdnvpnFunctest(feature.Feature):
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         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         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         # 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         with open(COMMON_CONFIG.config_file) as f:
74             config_yaml = yaml.safe_load(f)
75
76         testcases = config_yaml.get("testcases")
77         overall_status = "PASS"
78         for tc in testcases:
79             if testcases[tc]['enabled']:
80                 test_name = tc
81                 test_descr = testcases[tc]['description']
82                 title = ("Running '%s - %s'" %
83                          (test_name, test_descr))
84                 logger.info(title)
85                 logger.info("%s\n" % ("=" * len(title)))
86                 try:
87                     logger.info("Importing the testcase %s" % test_name)
88                     t = importlib.import_module(test_name, package=None)
89                     logger.info("Calling the testcase %s main method"
90                                 % test_name)
91                     result = t.main()
92                     logger.info("Execution is complete for the"
93                                 " testcase %s" % test_name)
94                 except Exception as ex:
95                     result = -1
96                     logger.info("Caught Exception in %s: %s Trace: %s"
97                                 % (test_name, ex,
98                                    traceback.format_exc()))
99                 if result < 0:
100                     status = "FAIL"
101                     overall_status = "FAIL"
102                     logger.info("Testcase %s failed" % test_name)
103                 else:
104                     status = result.get("status")
105                     self.details.update(
106                         {test_name: {'status': status,
107                                      'details': result.get("details")}})
108                     logger.info("Results of test case '%s - %s':\n%s\n"
109                                 % (test_name, test_descr, result))
110
111                     if status == "FAIL":
112                         overall_status = "FAIL"
113
114         logger.info("Resetting subnet/net/port quota")
115         test_utils.update_nw_subnet_port_quota(neutron_client,
116                                                tenant_id,
117                                                neutron_nw_quota,
118                                                neutron_subnet_quota,
119                                                neutron_port_quota,
120                                                neutron_router_quota)
121
122         logger.info("Resetting instances quota class")
123         test_utils.update_instance_quota_class(nova_client, instances_quota)
124
125         try:
126             installer_type = str(os.environ['INSTALLER_TYPE'].lower())
127             if installer_type in ["fuel", "apex"]:
128                 gather_logs('overall')
129             else:
130                 logger.info("Skipping log gathering because installer"
131                             "type %s is neither fuel nor apex" %
132                             installer_type)
133         except Exception as ex:
134             logger.error(('Something went wrong in the Log gathering.'
135                           'Ex: %s, Trace: %s')
136                          % (ex, traceback.format_exc()))
137
138         if overall_status == "PASS":
139             self.result = 100
140             return feature.Feature.EX_OK
141
142         return feature.Feature.EX_RUN_ERROR
143
144
145 if __name__ == '__main__':
146     logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s'
147                         '- %(levelname)s - %(message)s')
148     SDNVPN = SdnvpnFunctest()
149     sys.exit(SDNVPN.execute())