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
13 from random import randint
16 import functest.utils.openstack_utils as os_utils
18 import sdnvpn.lib.utils as test_utils
19 from sdnvpn.lib.results import Results
20 import sdnvpn.lib.config as sdnvpn_config
22 parser = argparse.ArgumentParser()
24 parser.add_argument("-r", "--report",
25 help="Create json result file",
28 args = parser.parse_args()
30 logger = logging.getLogger('sdnvpn-testcase-2')
32 COMMON_CONFIG = sdnvpn_config.CommonConfig()
33 TESTCASE_CONFIG = sdnvpn_config.TestcaseConfig('testcase_2')
37 results = Results(COMMON_CONFIG.line_length)
39 results.add_to_summary(0, "=")
40 results.add_to_summary(2, "STATUS", "SUBTEST")
41 results.add_to_summary(0, "=")
43 nova_client = os_utils.get_nova_client()
44 neutron_client = os_utils.get_neutron_client()
45 glance_client = os_utils.get_glance_client()
47 (floatingip_ids, instance_ids, router_ids, network_ids, image_ids,
48 subnet_ids, interfaces, bgpvpn_ids) = ([] for i in range(8))
50 logger.debug("Using private key %s injected to the VMs."
51 % COMMON_CONFIG.keyfile_path)
52 keyfile = open(COMMON_CONFIG.keyfile_path, 'r')
55 files = {"/home/cirros/id_rsa": key}
57 image_id = os_utils.create_glance_image(glance_client,
58 TESTCASE_CONFIG.image_name,
59 COMMON_CONFIG.image_path,
60 disk=COMMON_CONFIG.image_format,
63 image_ids.append(image_id)
65 network_1_id = test_utils.create_net(
67 TESTCASE_CONFIG.net_1_name)
68 subnet_1a_id = test_utils.create_subnet(
70 TESTCASE_CONFIG.subnet_1a_name,
71 TESTCASE_CONFIG.subnet_1a_cidr,
73 subnet_1b_id = test_utils.create_subnet(
75 TESTCASE_CONFIG.subnet_1b_name,
76 TESTCASE_CONFIG.subnet_1b_cidr,
79 network_2_id = test_utils.create_net(
81 TESTCASE_CONFIG.net_2_name)
82 subnet_2a_id = test_utils.create_subnet(
84 TESTCASE_CONFIG.subnet_2a_name,
85 TESTCASE_CONFIG.subnet_2a_cidr,
87 subnet_2b_id = test_utils.create_subnet(
89 TESTCASE_CONFIG.subnet_2b_name,
90 TESTCASE_CONFIG.subnet_2b_cidr,
92 network_ids.extend([network_1_id, network_2_id])
93 subnet_ids.extend([subnet_1a_id, subnet_1b_id, subnet_2a_id, subnet_2b_id])
95 sg_id = os_utils.create_security_group_full(neutron_client,
96 TESTCASE_CONFIG.secgroup_name,
97 TESTCASE_CONFIG.secgroup_descr)
99 compute_nodes = test_utils.assert_and_get_compute_nodes(nova_client)
101 av_zone_1 = "nova:" + compute_nodes[0]
102 av_zone_2 = "nova:" + compute_nodes[1]
105 userdata_common = test_utils.generate_userdata_common()
106 vm_2 = test_utils.create_instance(
108 TESTCASE_CONFIG.instance_2_name,
112 fixed_ip=TESTCASE_CONFIG.instance_2_ip,
113 secgroup_name=TESTCASE_CONFIG.secgroup_name,
114 compute_node=av_zone_1,
115 userdata=userdata_common)
117 vm_3 = test_utils.create_instance(
119 TESTCASE_CONFIG.instance_3_name,
123 fixed_ip=TESTCASE_CONFIG.instance_3_ip,
124 secgroup_name=TESTCASE_CONFIG.secgroup_name,
125 compute_node=av_zone_2,
126 userdata=userdata_common)
128 vm_5 = test_utils.create_instance(
130 TESTCASE_CONFIG.instance_5_name,
134 fixed_ip=TESTCASE_CONFIG.instance_5_ip,
135 secgroup_name=TESTCASE_CONFIG.secgroup_name,
136 compute_node=av_zone_2,
137 userdata=userdata_common)
139 # We boot vm5 first because we need vm5_ip for vm4 userdata
140 u4 = test_utils.generate_userdata_with_ssh(
141 [TESTCASE_CONFIG.instance_1_ip,
142 TESTCASE_CONFIG.instance_3_ip,
143 TESTCASE_CONFIG.instance_5_ip])
144 vm_4 = test_utils.create_instance(
146 TESTCASE_CONFIG.instance_4_name,
150 fixed_ip=TESTCASE_CONFIG.instance_4_ip,
151 secgroup_name=TESTCASE_CONFIG.secgroup_name,
152 compute_node=av_zone_1,
156 # We boot VM1 at the end because we need to get the IPs first to generate
158 u1 = test_utils.generate_userdata_with_ssh(
159 [TESTCASE_CONFIG.instance_2_ip,
160 TESTCASE_CONFIG.instance_3_ip,
161 TESTCASE_CONFIG.instance_4_ip,
162 TESTCASE_CONFIG.instance_5_ip])
163 vm_1 = test_utils.create_instance(
165 TESTCASE_CONFIG.instance_1_name,
169 fixed_ip=TESTCASE_CONFIG.instance_1_ip,
170 secgroup_name=TESTCASE_CONFIG.secgroup_name,
171 compute_node=av_zone_1,
174 instance_ids.extend([vm_1.id, vm_2.id, vm_3.id, vm_4.id, vm_5.id])
176 msg = ("Create VPN1 with eRT=iRT")
177 results.record_action(msg)
178 vpn1_name = "sdnvpn-1-" + str(randint(100000, 999999))
179 kwargs = {"import_targets": TESTCASE_CONFIG.targets2,
180 "export_targets": TESTCASE_CONFIG.targets2,
181 "route_targets": TESTCASE_CONFIG.targets2,
182 "route_distinguishers": TESTCASE_CONFIG.route_distinguishers1,
184 bgpvpn1 = test_utils.create_bgpvpn(neutron_client, **kwargs)
185 bgpvpn1_id = bgpvpn1['bgpvpn']['id']
186 logger.debug("VPN1 created details: %s" % bgpvpn1)
187 bgpvpn_ids.append(bgpvpn1_id)
189 msg = ("Associate network '%s' to the VPN." % TESTCASE_CONFIG.net_1_name)
190 results.record_action(msg)
191 results.add_to_summary(0, "-")
193 test_utils.create_network_association(
194 neutron_client, bgpvpn1_id, network_1_id)
196 # Wait for VMs to get ips.
197 instances_up = test_utils.wait_for_instances_up(vm_1, vm_2,
202 logger.error("One or more instances is down")
205 logger.info("Waiting for the VMs to connect to each other using the"
206 " updated network configuration")
207 test_utils.wait_before_subtest()
209 # 10.10.10.12 should return sdnvpn-2 to sdnvpn-1
210 results.check_ssh_output(vm_1, vm_2,
211 expected=TESTCASE_CONFIG.instance_2_name,
213 # 10.10.11.13 should return sdnvpn-3 to sdnvpn-1
214 results.check_ssh_output(vm_1, vm_3,
215 expected=TESTCASE_CONFIG.instance_3_name,
218 results.add_to_summary(0, "-")
219 msg = ("Create VPN2 with eRT=iRT")
220 results.record_action(msg)
221 vpn2_name = "sdnvpn-2-" + str(randint(100000, 999999))
222 kwargs = {"import_targets": TESTCASE_CONFIG.targets1,
223 "export_targets": TESTCASE_CONFIG.targets1,
224 "route_targets": TESTCASE_CONFIG.targets1,
225 "route_distinguishers": TESTCASE_CONFIG.route_distinguishers2,
227 bgpvpn2 = test_utils.create_bgpvpn(neutron_client, **kwargs)
228 bgpvpn2_id = bgpvpn2['bgpvpn']['id']
229 logger.debug("VPN created details: %s" % bgpvpn2)
230 bgpvpn_ids.append(bgpvpn2_id)
232 msg = ("Associate network '%s' to the VPN2." % TESTCASE_CONFIG.net_2_name)
233 results.record_action(msg)
234 results.add_to_summary(0, "-")
236 test_utils.create_network_association(
237 neutron_client, bgpvpn2_id, network_2_id)
239 test_utils.wait_for_bgp_net_assoc(neutron_client, bgpvpn1_id, network_1_id)
240 test_utils.wait_for_bgp_net_assoc(neutron_client, bgpvpn2_id, network_2_id)
242 logger.info("Waiting for the VMs to connect to each other using the"
243 " updated network configuration")
244 test_utils.wait_before_subtest()
246 # 10.10.11.13 should return sdnvpn-5 to sdnvpn-4
247 results.check_ssh_output(vm_4, vm_5,
248 expected=TESTCASE_CONFIG.instance_5_name,
251 # 10.10.10.11 should return "not reachable" to sdnvpn-4
252 results.check_ssh_output(vm_4, vm_1,
253 expected="not reachable",
256 test_utils.cleanup_nova(nova_client, floatingip_ids, instance_ids,
258 test_utils.cleanup_neutron(neutron_client, bgpvpn_ids, interfaces,
259 subnet_ids, router_ids, network_ids)
261 return results.compile_summary()
264 if __name__ == '__main__':
265 logging.basicConfig(level=logging.INFO)