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
8 # http://www.apache.org/licenses/LICENSE-2.0
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
25 logger = logging.getLogger(__name__)
27 COMMON_CONFIG = sdnvpn_config.CommonConfig()
30 class SdnvpnFunctest(feature.Feature):
34 nova_client = os_utils.get_nova_client()
35 neutron_client = os_utils.get_neutron_client()
37 tenant_id = os_utils.get_tenant_id(os_utils.get_keystone_client(),
38 os.environ['OS_PROJECT_NAME'])
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(nova_client)
47 logger.info("Setting net/subnet/port/router "
49 test_utils.update_nw_subnet_port_quota(
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()
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(
63 COMMON_CONFIG.nova_instances_quota_class)
65 # Clean up the stale floating ip's so that required
66 # ip addresses are available for sdnvpn testcases
67 logger.info("Cleaning up the Floating IP Addresses")
68 floating_ips = os_utils.get_floating_ips(neutron_client)
69 if floating_ips is not None:
70 for floating_ip in floating_ips:
71 os_utils.delete_floating_ip(
72 neutron_client, floating_ip['id'])
75 # https://jira.opnfv.org/browse/SNAPS-318
76 # Clean up the stale routers
77 logger.info("Cleaning up the stale routers")
78 ports = os_utils.get_port_list(neutron_client)
81 if port['device_owner'] == 'network:router_interface':
82 os_utils.delete_neutron_port(
83 neutron_client, port['id'])
84 routers = os_utils.get_router_list(neutron_client)
85 if routers is not None:
86 for router in routers:
87 os_utils.remove_gateway_router(
88 neutron_client, router['id'])
89 os_utils.delete_neutron_router(
90 neutron_client, router['id'])
92 with open(COMMON_CONFIG.config_file) as f:
93 config_yaml = yaml.safe_load(f)
95 testcases = config_yaml.get("testcases")
96 testcases_ordered = OrderedDict(sorted(testcases.items(),
97 key=lambda x: x[1]['order']))
98 overall_status = "PASS"
99 for tc, test_sdnvpn in testcases_ordered.items():
100 if test_sdnvpn['enabled']:
102 test_descr = testcases[tc]['description']
103 title = ("Running '%s - %s'" %
104 (test_name, test_descr))
106 logger.info("%s\n" % ("=" * len(title)))
108 logger.info("Importing the testcase %s" % test_name)
109 t = importlib.import_module(test_name, package=None)
110 logger.info("Calling the testcase %s main method"
113 logger.info("Execution is complete for the"
114 " testcase %s" % test_name)
115 except Exception as ex:
117 logger.info("Caught Exception in %s: %s Trace: %s"
119 traceback.format_exc()))
122 overall_status = "FAIL"
123 logger.info("Testcase %s failed" % test_name)
125 status = result.get("status")
127 {test_name: {'status': status,
128 'details': result.get("details")}})
129 logger.info("Results of test case '%s - %s':\n%s\n"
130 % (test_name, test_descr, result))
133 overall_status = "FAIL"
135 logger.info("Resetting subnet/net/port quota")
136 test_utils.update_nw_subnet_port_quota(neutron_client,
139 neutron_subnet_quota,
141 neutron_router_quota)
143 logger.info("Resetting instances quota class")
144 test_utils.update_instance_quota_class(nova_client, instances_quota)
147 installer_type = str(os.environ['INSTALLER_TYPE'].lower())
148 if installer_type in ["fuel", "apex"]:
149 gather_logs('overall')
151 logger.info("Skipping log gathering because installer"
152 "type %s is neither fuel nor apex" %
154 except Exception as ex:
155 logger.error(('Something went wrong in the Log gathering.'
157 % (ex, traceback.format_exc()))
159 if overall_status == "PASS":
161 return feature.Feature.EX_OK
163 return feature.Feature.EX_RUN_ERROR
166 if __name__ == '__main__':
167 logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s'
168 '- %(levelname)s - %(message)s')
169 SDNVPN = SdnvpnFunctest()
170 sys.exit(SDNVPN.execute())