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
15 from functest.utils import openstack_utils as os_utils
16 from random import randint
17 from sdnvpn.lib import config as sdnvpn_config
18 from sdnvpn.lib import utils as test_utils
19 from sdnvpn.lib.results import Results
22 logger = logging.getLogger(__name__)
24 COMMON_CONFIG = sdnvpn_config.CommonConfig()
25 TESTCASE_CONFIG = sdnvpn_config.TestcaseConfig(
26 'sdnvpn.test.functest.testcase_4')
30 results = Results(COMMON_CONFIG.line_length)
32 results.add_to_summary(0, "=")
33 results.add_to_summary(2, "STATUS", "SUBTEST")
34 results.add_to_summary(0, "=")
36 nova_client = os_utils.get_nova_client()
37 neutron_client = os_utils.get_neutron_client()
38 glance_client = os_utils.get_glance_client()
40 (floatingip_ids, instance_ids, router_ids, network_ids, image_ids,
41 subnet_ids, interfaces, bgpvpn_ids) = ([] for i in range(8))
44 image_id = os_utils.create_glance_image(
45 glance_client, TESTCASE_CONFIG.image_name,
46 COMMON_CONFIG.image_path, disk=COMMON_CONFIG.image_format,
47 container="bare", public='public')
48 image_ids.append(image_id)
50 network_1_id, subnet_1_id, router_1_id = test_utils.create_network(
52 TESTCASE_CONFIG.net_1_name,
53 TESTCASE_CONFIG.subnet_1_name,
54 TESTCASE_CONFIG.subnet_1_cidr,
55 TESTCASE_CONFIG.router_1_name)
57 network_2_id = test_utils.create_net(
59 TESTCASE_CONFIG.net_2_name)
61 subnet_2_id = test_utils.create_subnet(
63 TESTCASE_CONFIG.subnet_2_name,
64 TESTCASE_CONFIG.subnet_2_cidr,
66 interfaces.append(tuple((router_1_id, subnet_1_id)))
67 network_ids.extend([network_1_id, network_2_id])
68 router_ids.append(router_1_id)
69 subnet_ids.extend([subnet_1_id, subnet_2_id])
71 sg_id = os_utils.create_security_group_full(
73 TESTCASE_CONFIG.secgroup_name,
74 TESTCASE_CONFIG.secgroup_descr)
76 compute_nodes = test_utils.assert_and_get_compute_nodes(nova_client)
78 av_zone_1 = "nova:" + compute_nodes[0]
79 av_zone_2 = "nova:" + compute_nodes[1]
82 vm_2 = test_utils.create_instance(
84 TESTCASE_CONFIG.instance_2_name,
88 secgroup_name=TESTCASE_CONFIG.secgroup_name,
89 compute_node=av_zone_1)
90 vm_2_ip = test_utils.get_instance_ip(vm_2)
92 vm_3 = test_utils.create_instance(
94 TESTCASE_CONFIG.instance_3_name,
98 secgroup_name=TESTCASE_CONFIG.secgroup_name,
99 compute_node=av_zone_2)
100 vm_3_ip = test_utils.get_instance_ip(vm_3)
102 vm_5 = test_utils.create_instance(
104 TESTCASE_CONFIG.instance_5_name,
108 secgroup_name=TESTCASE_CONFIG.secgroup_name,
109 compute_node=av_zone_2)
110 vm_5_ip = test_utils.get_instance_ip(vm_5)
112 # We boot vm5 first because we need vm5_ip for vm4 userdata
113 u4 = test_utils.generate_ping_userdata([vm_5_ip])
114 vm_4 = test_utils.create_instance(
116 TESTCASE_CONFIG.instance_4_name,
120 secgroup_name=TESTCASE_CONFIG.secgroup_name,
121 compute_node=av_zone_1,
123 vm_4_ip = test_utils.get_instance_ip(vm_4)
125 # We boot VM1 at the end because we need to get the IPs
126 # first to generate the userdata
127 u1 = test_utils.generate_ping_userdata([vm_2_ip,
131 vm_1 = test_utils.create_instance(
133 TESTCASE_CONFIG.instance_1_name,
137 secgroup_name=TESTCASE_CONFIG.secgroup_name,
138 compute_node=av_zone_1,
141 instance_ids.extend([vm_1.id, vm_2.id, vm_3.id, vm_4.id, vm_5.id])
143 msg = ("Create VPN with eRT<>iRT")
144 results.record_action(msg)
145 vpn_name = "sdnvpn-" + str(randint(100000, 999999))
147 "import_targets": TESTCASE_CONFIG.targets1,
148 "export_targets": TESTCASE_CONFIG.targets2,
149 "route_distinguishers": TESTCASE_CONFIG.route_distinguishers,
152 bgpvpn = test_utils.create_bgpvpn(neutron_client, **kwargs)
153 bgpvpn_id = bgpvpn['bgpvpn']['id']
154 logger.debug("VPN created details: %s" % bgpvpn)
155 bgpvpn_ids.append(bgpvpn_id)
157 msg = ("Associate router '%s' to the VPN." %
158 TESTCASE_CONFIG.router_1_name)
159 results.record_action(msg)
160 results.add_to_summary(0, "-")
162 test_utils.create_router_association(
163 neutron_client, bgpvpn_id, router_1_id)
165 # Wait for VMs to get ips.
166 instances_up = test_utils.wait_for_instances_up(vm_2, vm_3, vm_5)
167 instances_dhcp_up = test_utils.wait_for_instances_get_dhcp(vm_1, vm_4)
169 if (not instances_up or not instances_dhcp_up):
170 logger.error("One or more instances are down")
171 # TODO: Handle this appropriately
173 results.get_ping_status(vm_1, vm_2, expected="PASS", timeout=200)
174 results.get_ping_status(vm_1, vm_3, expected="PASS", timeout=30)
175 results.get_ping_status(vm_1, vm_4, expected="FAIL", timeout=30)
177 msg = ("Associate network '%s' to the VPN." %
178 TESTCASE_CONFIG.net_2_name)
179 results.add_to_summary(0, "-")
180 results.record_action(msg)
181 results.add_to_summary(0, "-")
182 test_utils.create_network_association(
183 neutron_client, bgpvpn_id, network_2_id)
185 test_utils.wait_for_bgp_router_assoc(
186 neutron_client, bgpvpn_id, router_1_id)
187 test_utils.wait_for_bgp_net_assoc(
188 neutron_client, bgpvpn_id, network_2_id)
190 logger.info("Waiting for the VMs to connect to each other using the"
191 " updated network configuration")
192 test_utils.wait_before_subtest()
194 results.get_ping_status(vm_4, vm_5, expected="PASS", timeout=30)
195 # TODO enable again when isolation in VPN with iRT != eRT works
196 # results.get_ping_status(vm_1, vm_4, expected="FAIL", timeout=30)
197 # results.get_ping_status(vm_1, vm_5, expected="FAIL", timeout=30)
199 msg = ("Update VPN with eRT=iRT ...")
200 results.add_to_summary(0, "-")
201 results.record_action(msg)
202 results.add_to_summary(0, "-")
204 # use bgpvpn-create instead of update till NETVIRT-1067 bug is fixed
205 # kwargs = {"import_targets": TESTCASE_CONFIG.targets1,
206 # "export_targets": TESTCASE_CONFIG.targets1,
208 # bgpvpn = test_utils.update_bgpvpn(neutron_client,
209 # bgpvpn_id, **kwargs)
211 test_utils.delete_bgpvpn(neutron_client, bgpvpn_id)
212 bgpvpn_ids.remove(bgpvpn_id)
214 "import_targets": TESTCASE_CONFIG.targets1,
215 "export_targets": TESTCASE_CONFIG.targets1,
216 "route_distinguishers": TESTCASE_CONFIG.route_distinguishers,
220 test_utils.wait_before_subtest()
222 bgpvpn = test_utils.create_bgpvpn(neutron_client, **kwargs)
223 bgpvpn_id = bgpvpn['bgpvpn']['id']
224 logger.debug("VPN re-created details: %s" % bgpvpn)
225 bgpvpn_ids.append(bgpvpn_id)
227 msg = ("Associate again network '%s' and router '%s 'to the VPN."
228 % (TESTCASE_CONFIG.net_2_name,
229 TESTCASE_CONFIG.router_1_name))
230 results.add_to_summary(0, "-")
231 results.record_action(msg)
232 results.add_to_summary(0, "-")
234 test_utils.create_router_association(
235 neutron_client, bgpvpn_id, router_1_id)
237 test_utils.create_network_association(
238 neutron_client, bgpvpn_id, network_2_id)
240 test_utils.wait_for_bgp_router_assoc(
241 neutron_client, bgpvpn_id, router_1_id)
242 test_utils.wait_for_bgp_net_assoc(
243 neutron_client, bgpvpn_id, network_2_id)
244 # The above code has to be removed after re-enabling bgpvpn-update
246 logger.info("Waiting for the VMs to connect to each other using the"
247 " updated network configuration")
248 test_utils.wait_before_subtest()
250 results.get_ping_status(vm_1, vm_4, expected="PASS", timeout=30)
251 results.get_ping_status(vm_1, vm_5, expected="PASS", timeout=30)
253 results.add_to_summary(0, "=")
254 logger.info("\n%s" % results.summary)
256 except Exception as e:
257 logger.error("exception occurred while executing testcase_4: %s", e)
260 test_utils.cleanup_nova(nova_client, instance_ids)
261 test_utils.cleanup_glance(glance_client, image_ids)
262 test_utils.cleanup_neutron(neutron_client, floatingip_ids,
263 bgpvpn_ids, interfaces, subnet_ids,
264 router_ids, network_ids)
266 return results.compile_summary()
269 if __name__ == '__main__':
270 logging.basicConfig(level=logging.INFO)