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 random import randint
16 from sdnvpn.lib import config as sdnvpn_config
17 from sdnvpn.lib import openstack_utils as os_utils
18 from sdnvpn.lib import utils as test_utils
19 from sdnvpn.lib.results import Results
21 logger = logging.getLogger(__name__)
23 COMMON_CONFIG = sdnvpn_config.CommonConfig()
24 TESTCASE_CONFIG = sdnvpn_config.TestcaseConfig(
25 'sdnvpn.test.functest.testcase_2')
29 conn = os_utils.get_os_connection()
30 results = Results(COMMON_CONFIG.line_length, conn)
32 results.add_to_summary(0, "=")
33 results.add_to_summary(2, "STATUS", "SUBTEST")
34 results.add_to_summary(0, "=")
36 neutron_client = os_utils.get_neutron_client()
38 (floatingip_ids, instance_ids, router_ids, network_ids, image_ids,
39 subnet_ids, interfaces, bgpvpn_ids) = ([] for i in range(8))
42 logger.debug("Using private key %s injected to the VMs."
43 % COMMON_CONFIG.keyfile_path)
44 keyfile = open(COMMON_CONFIG.keyfile_path, 'r')
47 files = [{'path': '/home/cirros/id_rsa',
48 'contents': base64.b64encode(key)}]
50 image_id = os_utils.create_glance_image(
51 conn, TESTCASE_CONFIG.image_name,
52 COMMON_CONFIG.image_path, disk=COMMON_CONFIG.image_format,
53 container="bare", public='public')
54 image_ids.append(image_id)
56 network_1_id = test_utils.create_net(
58 TESTCASE_CONFIG.net_1_name)
59 subnet_1a_id = test_utils.create_subnet(
61 TESTCASE_CONFIG.subnet_1a_name,
62 TESTCASE_CONFIG.subnet_1a_cidr,
64 # TODO: uncomment the commented lines once ODL has
65 # support for mulitple subnets under same neutron network
66 # subnet_1b_id = test_utils.create_subnet(
68 # TESTCASE_CONFIG.subnet_1b_name,
69 # TESTCASE_CONFIG.subnet_1b_cidr,
72 network_2_id = test_utils.create_net(
74 TESTCASE_CONFIG.net_2_name)
75 # subnet_2a_id = test_utils.create_subnet(
77 # TESTCASE_CONFIG.subnet_2a_name,
78 # TESTCASE_CONFIG.subnet_2a_cidr,
80 subnet_2b_id = test_utils.create_subnet(
82 TESTCASE_CONFIG.subnet_2b_name,
83 TESTCASE_CONFIG.subnet_2b_cidr,
85 network_ids.extend([network_1_id, network_2_id])
86 subnet_ids.extend([subnet_1a_id,
91 sg_id = os_utils.create_security_group_full(
92 conn, TESTCASE_CONFIG.secgroup_name,
93 TESTCASE_CONFIG.secgroup_descr)
95 compute_nodes = test_utils.assert_and_get_compute_nodes(conn)
97 av_zone_1 = "nova:" + compute_nodes[0]
98 # av_zone_2 = "nova:" + compute_nodes[1]
101 userdata_common = test_utils.generate_userdata_common()
102 vm_2 = test_utils.create_instance(
104 TESTCASE_CONFIG.instance_2_name,
108 fixed_ip=TESTCASE_CONFIG.instance_2_ip,
109 secgroup_name=TESTCASE_CONFIG.secgroup_name,
110 compute_node=av_zone_1,
111 userdata=userdata_common)
114 # vm_3 = test_utils.create_instance(
116 # TESTCASE_CONFIG.instance_3_name,
120 # fixed_ip=TESTCASE_CONFIG.instance_3_ip,
121 # secgroup_name=TESTCASE_CONFIG.secgroup_name,
122 # compute_node=av_zone_2,
123 # userdata=userdata_common)
125 # vm_5 = test_utils.create_instance(
127 # TESTCASE_CONFIG.instance_5_name,
131 # fixed_ip=TESTCASE_CONFIG.instance_5_ip,
132 # secgroup_name=TESTCASE_CONFIG.secgroup_name,
133 # compute_node=av_zone_2,
134 # userdata=userdata_common)
136 # We boot vm5 first because we need vm5_ip for vm4 userdata
137 u4 = test_utils.generate_userdata_with_ssh(
138 [TESTCASE_CONFIG.instance_1_ip
139 # TESTCASE_CONFIG.instance_3_ip,
140 # TESTCASE_CONFIG.instance_5_ip
142 vm_4 = test_utils.create_instance(
144 TESTCASE_CONFIG.instance_4_name,
148 fixed_ip=TESTCASE_CONFIG.instance_4_ip,
149 secgroup_name=TESTCASE_CONFIG.secgroup_name,
150 compute_node=av_zone_1,
154 # We boot VM1 at the end because we need to get the IPs first
155 # to generate the userdata
156 u1 = test_utils.generate_userdata_with_ssh(
157 [TESTCASE_CONFIG.instance_2_ip,
158 # TESTCASE_CONFIG.instance_3_ip,
159 TESTCASE_CONFIG.instance_4_ip,
160 # TESTCASE_CONFIG.instance_5_ip
162 vm_1 = test_utils.create_instance(
164 TESTCASE_CONFIG.instance_1_name,
168 fixed_ip=TESTCASE_CONFIG.instance_1_ip,
169 secgroup_name=TESTCASE_CONFIG.secgroup_name,
170 compute_node=av_zone_1,
173 instance_ids.extend([vm_1.id,
180 msg = ("Create VPN1 with eRT=iRT")
181 results.record_action(msg)
182 vpn1_name = "sdnvpn-1-" + str(randint(100000, 999999))
184 "import_targets": TESTCASE_CONFIG.targets2,
185 "export_targets": TESTCASE_CONFIG.targets2,
186 "route_targets": TESTCASE_CONFIG.targets2,
187 "route_distinguishers": TESTCASE_CONFIG.route_distinguishers1,
190 bgpvpn1 = test_utils.create_bgpvpn(neutron_client, **kwargs)
191 bgpvpn1_id = bgpvpn1['bgpvpn']['id']
192 logger.debug("VPN1 created details: %s" % bgpvpn1)
193 bgpvpn_ids.append(bgpvpn1_id)
195 msg = ("Associate network '%s' to the VPN." %
196 TESTCASE_CONFIG.net_1_name)
197 results.record_action(msg)
198 results.add_to_summary(0, "-")
200 test_utils.create_network_association(
201 neutron_client, bgpvpn1_id, network_1_id)
203 # Wait for VMs to get ips.
204 instances_up = test_utils.wait_for_instances_up(vm_2)
205 instances_dhcp_up = test_utils.wait_for_instances_get_dhcp(vm_1, vm_4)
207 if (not instances_up or not instances_dhcp_up):
208 logger.error("One or more instances are down")
209 # TODO: Handle this appropriately
211 logger.info("Waiting for the VMs to connect to each other using the"
212 " updated network configuration")
213 test_utils.wait_before_subtest()
215 # 10.10.10.12 should return sdnvpn-2 to sdnvpn-1
216 results.check_ssh_output(vm_1, vm_2,
217 expected=TESTCASE_CONFIG.instance_2_name,
219 # 10.10.11.13 should return sdnvpn-3 to sdnvpn-1
220 # results.check_ssh_output(vm_1, vm_3,
221 # expected=TESTCASE_CONFIG.instance_3_name,
224 results.add_to_summary(0, "-")
225 msg = ("Create VPN2 with eRT=iRT")
226 results.record_action(msg)
227 vpn2_name = "sdnvpn-2-" + str(randint(100000, 999999))
229 "import_targets": TESTCASE_CONFIG.targets1,
230 "export_targets": TESTCASE_CONFIG.targets1,
231 "route_targets": TESTCASE_CONFIG.targets1,
232 "route_distinguishers": TESTCASE_CONFIG.route_distinguishers2,
235 bgpvpn2 = test_utils.create_bgpvpn(neutron_client, **kwargs)
236 bgpvpn2_id = bgpvpn2['bgpvpn']['id']
237 logger.debug("VPN created details: %s" % bgpvpn2)
238 bgpvpn_ids.append(bgpvpn2_id)
240 msg = ("Associate network '%s' to the VPN2." %
241 TESTCASE_CONFIG.net_2_name)
242 results.record_action(msg)
243 results.add_to_summary(0, "-")
245 test_utils.create_network_association(
246 neutron_client, bgpvpn2_id, network_2_id)
248 test_utils.wait_for_bgp_net_assoc(neutron_client,
249 bgpvpn1_id, network_1_id)
250 test_utils.wait_for_bgp_net_assoc(neutron_client,
251 bgpvpn2_id, network_2_id)
253 logger.info("Waiting for the VMs to connect to each other using the"
254 " updated network configuration")
255 test_utils.wait_before_subtest()
257 # 10.10.11.13 should return sdnvpn-5 to sdnvpn-4
258 # results.check_ssh_output(vm_4, vm_5,
259 # expected=TESTCASE_CONFIG.instance_5_name,
262 # 10.10.10.11 should return "not reachable" to sdnvpn-4
263 results.check_ssh_output(vm_4, vm_1,
264 expected="not reachable",
267 except Exception as e:
268 logger.error("exception occurred while executing testcase_2: %s", e)
271 test_utils.cleanup_nova(conn, instance_ids)
272 test_utils.cleanup_glance(conn, image_ids)
273 test_utils.cleanup_neutron(conn, neutron_client, floatingip_ids,
274 bgpvpn_ids, interfaces, subnet_ids,
275 router_ids, network_ids)
277 return results.compile_summary()
280 if __name__ == '__main__':