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