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
14 from random import randint
15 from sdnvpn.lib import config as sdnvpn_config
16 from sdnvpn.lib import openstack_utils as os_utils
17 from sdnvpn.lib import utils as test_utils
18 from sdnvpn.lib.results import Results
20 logger = logging.getLogger(__name__)
22 COMMON_CONFIG = sdnvpn_config.CommonConfig()
23 TESTCASE_CONFIG = sdnvpn_config.TestcaseConfig(
24 'sdnvpn.test.functest.testcase_1')
28 conn = os_utils.get_os_connection()
29 results = Results(COMMON_CONFIG.line_length, conn)
31 results.add_to_summary(0, "=")
32 results.add_to_summary(2, "STATUS", "SUBTEST")
33 results.add_to_summary(0, "=")
35 neutron_client = os_utils.get_neutron_client()
37 (floatingip_ids, instance_ids, router_ids, network_ids, image_ids,
38 subnet_ids, interfaces, bgpvpn_ids) = ([] for i in range(8))
41 image_id = os_utils.create_glance_image(
42 conn, TESTCASE_CONFIG.image_name,
43 COMMON_CONFIG.image_path, disk=COMMON_CONFIG.image_format,
44 container="bare", public='public')
45 image_ids.append(image_id)
47 network_1_id = test_utils.create_net(conn,
48 TESTCASE_CONFIG.net_1_name)
49 subnet_1_id = test_utils.create_subnet(conn,
50 TESTCASE_CONFIG.subnet_1_name,
51 TESTCASE_CONFIG.subnet_1_cidr,
54 network_2_id = test_utils.create_net(conn,
55 TESTCASE_CONFIG.net_2_name)
57 subnet_2_id = test_utils.create_subnet(conn,
58 TESTCASE_CONFIG.subnet_2_name,
59 TESTCASE_CONFIG.subnet_2_cidr,
61 network_ids.extend([network_1_id, network_2_id])
62 subnet_ids.extend([subnet_1_id, subnet_2_id])
64 sg_id = os_utils.create_security_group_full(
65 conn, TESTCASE_CONFIG.secgroup_name,
66 TESTCASE_CONFIG.secgroup_descr)
68 compute_nodes = test_utils.assert_and_get_compute_nodes(conn)
70 av_zone_1 = "nova:" + compute_nodes[0]
71 av_zone_2 = "nova:" + compute_nodes[1]
74 vm_2 = test_utils.create_instance(
76 TESTCASE_CONFIG.instance_2_name,
80 secgroup_name=TESTCASE_CONFIG.secgroup_name,
81 compute_node=av_zone_1)
82 vm_2_ip = test_utils.get_instance_ip(conn, vm_2)
84 vm_3 = test_utils.create_instance(
86 TESTCASE_CONFIG.instance_3_name,
90 secgroup_name=TESTCASE_CONFIG.secgroup_name,
91 compute_node=av_zone_2)
92 vm_3_ip = test_utils.get_instance_ip(conn, vm_3)
94 vm_5 = test_utils.create_instance(
96 TESTCASE_CONFIG.instance_5_name,
100 secgroup_name=TESTCASE_CONFIG.secgroup_name,
101 compute_node=av_zone_2)
102 vm_5_ip = test_utils.get_instance_ip(conn, vm_5)
104 # We boot vm5 first because we need vm5_ip for vm4 userdata
105 u4 = test_utils.generate_ping_userdata([vm_5_ip])
106 vm_4 = test_utils.create_instance(
108 TESTCASE_CONFIG.instance_4_name,
112 secgroup_name=TESTCASE_CONFIG.secgroup_name,
113 compute_node=av_zone_1,
115 vm_4_ip = test_utils.get_instance_ip(conn, vm_4)
117 # We boot VM1 at the end because we need to get the IPs first
118 # to generate the userdata
119 u1 = test_utils.generate_ping_userdata([vm_2_ip,
123 vm_1 = test_utils.create_instance(
125 TESTCASE_CONFIG.instance_1_name,
129 secgroup_name=TESTCASE_CONFIG.secgroup_name,
130 compute_node=av_zone_1,
132 instance_ids.extend([vm_1.id, vm_2.id, vm_3.id, vm_4.id, vm_5.id])
134 msg = ("Create VPN with eRT<>iRT")
135 results.record_action(msg)
136 vpn_name = "sdnvpn-" + str(randint(100000, 999999))
138 "import_targets": TESTCASE_CONFIG.targets1,
139 "export_targets": TESTCASE_CONFIG.targets2,
140 "route_distinguishers": TESTCASE_CONFIG.route_distinguishers,
143 bgpvpn = test_utils.create_bgpvpn(neutron_client, **kwargs)
144 bgpvpn_id = bgpvpn['bgpvpn']['id']
145 logger.debug("VPN created details: %s" % bgpvpn)
146 bgpvpn_ids.append(bgpvpn_id)
148 msg = ("Associate network '%s' to the VPN." %
149 TESTCASE_CONFIG.net_1_name)
150 results.record_action(msg)
151 results.add_to_summary(0, "-")
153 test_utils.create_network_association(
154 neutron_client, bgpvpn_id, network_1_id)
156 # Wait for VMs to be ready.
157 instances_up = test_utils.wait_for_instances_up(vm_2, vm_3, vm_5)
158 instances_dhcp_up = test_utils.wait_for_instances_get_dhcp(vm_1, vm_4)
160 if (not instances_up or not instances_dhcp_up):
161 logger.error("One or more instances are down")
162 # TODO: Handle this appropriately
164 results.get_ping_status(vm_1, vm_2, expected="PASS", timeout=200)
165 results.get_ping_status(vm_1, vm_3, expected="PASS", timeout=30)
166 results.get_ping_status(vm_1, vm_4, expected="FAIL", timeout=30)
168 msg = ("Associate network '%s' to the VPN." %
169 TESTCASE_CONFIG.net_2_name)
170 results.add_to_summary(0, "-")
171 results.record_action(msg)
172 results.add_to_summary(0, "-")
173 test_utils.create_network_association(
174 neutron_client, bgpvpn_id, network_2_id)
176 test_utils.wait_for_bgp_net_assocs(neutron_client,
181 logger.info("Waiting for the VMs to connect to each other using the"
182 " updated network configuration")
183 test_utils.wait_before_subtest()
185 results.get_ping_status(vm_4, vm_5, expected="PASS", timeout=30)
186 # TODO enable again when isolation in VPN with iRT != eRT works
187 # results.get_ping_status(vm_1, vm_4, expected="FAIL", timeout=30)
188 # results.get_ping_status(vm_1, vm_5, expected="FAIL", timeout=30)
190 msg = ("Update VPN with eRT=iRT ...")
191 results.add_to_summary(0, "-")
192 results.record_action(msg)
193 results.add_to_summary(0, "-")
195 # use bgpvpn-create instead of update till NETVIRT-1067 bug is fixed
196 # kwargs = {"import_targets": TESTCASE_CONFIG.targets1,
197 # "export_targets": TESTCASE_CONFIG.targets1,
199 # bgpvpn = test_utils.update_bgpvpn(neutron_client,
200 # bgpvpn_id, **kwargs)
202 test_utils.delete_bgpvpn(neutron_client, bgpvpn_id)
203 bgpvpn_ids.remove(bgpvpn_id)
205 "import_targets": TESTCASE_CONFIG.targets1,
206 "export_targets": TESTCASE_CONFIG.targets1,
207 "route_distinguishers": TESTCASE_CONFIG.route_distinguishers,
211 test_utils.wait_before_subtest()
213 bgpvpn = test_utils.create_bgpvpn(neutron_client, **kwargs)
214 bgpvpn_id = bgpvpn['bgpvpn']['id']
215 logger.debug("VPN re-created details: %s" % bgpvpn)
216 bgpvpn_ids.append(bgpvpn_id)
218 msg = ("Associate network '%s' to the VPN." %
219 TESTCASE_CONFIG.net_1_name)
220 results.record_action(msg)
221 results.add_to_summary(0, "-")
223 test_utils.create_network_association(
224 neutron_client, bgpvpn_id, network_1_id)
226 test_utils.create_network_association(
227 neutron_client, bgpvpn_id, network_2_id)
229 test_utils.wait_for_bgp_net_assocs(neutron_client,
233 # The above code has to be removed after re-enabling bgpvpn-update
235 logger.info("Waiting for the VMs to connect to each other using the"
236 " updated network configuration")
237 test_utils.wait_before_subtest()
239 results.get_ping_status(vm_1, vm_4, expected="PASS", timeout=30)
240 results.get_ping_status(vm_1, vm_5, expected="PASS", timeout=30)
242 except Exception as e:
243 logger.error("exception occurred while executing testcase_1: %s", e)
246 test_utils.cleanup_nova(conn, instance_ids)
247 test_utils.cleanup_glance(conn, image_ids)
248 test_utils.cleanup_neutron(conn, neutron_client, floatingip_ids,
249 bgpvpn_ids, interfaces, subnet_ids,
250 router_ids, network_ids)
252 return results.compile_summary()
255 if __name__ == '__main__':