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
21 parser = argparse.ArgumentParser()
23 parser.add_argument("-r", "--report",
24 help="Create json result file",
27 args = parser.parse_args()
29 logger = logging.getLogger('sdnvpn-testcase-2')
31 COMMON_CONFIG = sdnvpn_config.CommonConfig()
32 TESTCASE_CONFIG = sdnvpn_config.TestcaseConfig('testcase_2')
36 results = Results(COMMON_CONFIG.line_length)
38 results.add_to_summary(0, "=")
39 results.add_to_summary(2, "STATUS", "SUBTEST")
40 results.add_to_summary(0, "=")
42 nova_client = os_utils.get_nova_client()
43 neutron_client = os_utils.get_neutron_client()
44 glance_client = os_utils.get_glance_client()
46 (floatingip_ids, instance_ids, router_ids, network_ids, image_ids,
47 subnet_ids, interfaces, bgpvpn_ids) = ([] for i in range(8))
49 logger.debug("Using private key %s injected to the VMs."
50 % COMMON_CONFIG.keyfile_path)
51 keyfile = open(COMMON_CONFIG.keyfile_path, 'r')
54 files = {"/home/cirros/id_rsa": key}
56 image_id = os_utils.create_glance_image(glance_client,
57 TESTCASE_CONFIG.image_name,
58 COMMON_CONFIG.image_path,
59 disk=COMMON_CONFIG.image_format,
62 image_ids.append(image_id)
64 network_1_id = test_utils.create_net(
66 TESTCASE_CONFIG.net_1_name)
67 subnet_1a_id = test_utils.create_subnet(
69 TESTCASE_CONFIG.subnet_1a_name,
70 TESTCASE_CONFIG.subnet_1a_cidr,
72 subnet_1b_id = test_utils.create_subnet(
74 TESTCASE_CONFIG.subnet_1b_name,
75 TESTCASE_CONFIG.subnet_1b_cidr,
78 network_2_id = test_utils.create_net(
80 TESTCASE_CONFIG.net_2_name)
81 subnet_2a_id = test_utils.create_subnet(
83 TESTCASE_CONFIG.subnet_2a_name,
84 TESTCASE_CONFIG.subnet_2a_cidr,
86 subnet_2b_id = test_utils.create_subnet(
88 TESTCASE_CONFIG.subnet_2b_name,
89 TESTCASE_CONFIG.subnet_2b_cidr,
91 network_ids.extend([network_1_id, network_2_id])
92 subnet_ids.extend([subnet_1a_id, subnet_1b_id, subnet_2a_id, subnet_2b_id])
94 sg_id = os_utils.create_security_group_full(neutron_client,
95 TESTCASE_CONFIG.secgroup_name,
96 TESTCASE_CONFIG.secgroup_descr)
98 compute_nodes = test_utils.assert_and_get_compute_nodes(nova_client)
100 av_zone_1 = "nova:" + compute_nodes[0]
101 av_zone_2 = "nova:" + compute_nodes[1]
104 userdata_common = test_utils.generate_userdata_common()
105 vm_2 = test_utils.create_instance(
107 TESTCASE_CONFIG.instance_2_name,
111 fixed_ip=TESTCASE_CONFIG.instance_2_ip,
112 secgroup_name=TESTCASE_CONFIG.secgroup_name,
113 compute_node=av_zone_1,
114 userdata=userdata_common)
116 vm_3 = test_utils.create_instance(
118 TESTCASE_CONFIG.instance_3_name,
122 fixed_ip=TESTCASE_CONFIG.instance_3_ip,
123 secgroup_name=TESTCASE_CONFIG.secgroup_name,
124 compute_node=av_zone_2,
125 userdata=userdata_common)
127 vm_5 = test_utils.create_instance(
129 TESTCASE_CONFIG.instance_5_name,
133 fixed_ip=TESTCASE_CONFIG.instance_5_ip,
134 secgroup_name=TESTCASE_CONFIG.secgroup_name,
135 compute_node=av_zone_2,
136 userdata=userdata_common)
138 # We boot vm5 first because we need vm5_ip for vm4 userdata
139 u4 = test_utils.generate_userdata_with_ssh(
140 [TESTCASE_CONFIG.instance_1_ip,
141 TESTCASE_CONFIG.instance_3_ip,
142 TESTCASE_CONFIG.instance_5_ip])
143 vm_4 = test_utils.create_instance(
145 TESTCASE_CONFIG.instance_4_name,
149 fixed_ip=TESTCASE_CONFIG.instance_4_ip,
150 secgroup_name=TESTCASE_CONFIG.secgroup_name,
151 compute_node=av_zone_1,
155 # We boot VM1 at the end because we need to get the IPs first to generate
157 u1 = test_utils.generate_userdata_with_ssh(
158 [TESTCASE_CONFIG.instance_2_ip,
159 TESTCASE_CONFIG.instance_3_ip,
160 TESTCASE_CONFIG.instance_4_ip,
161 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, vm_2.id, vm_3.id, vm_4.id, vm_5.id])
175 msg = ("Create VPN1 with eRT=iRT")
176 results.record_action(msg)
177 vpn1_name = "sdnvpn-1-" + str(randint(100000, 999999))
178 kwargs = {"import_targets": TESTCASE_CONFIG.targets2,
179 "export_targets": TESTCASE_CONFIG.targets2,
180 "route_targets": TESTCASE_CONFIG.targets2,
181 "route_distinguishers": TESTCASE_CONFIG.route_distinguishers1,
183 bgpvpn1 = test_utils.create_bgpvpn(neutron_client, **kwargs)
184 bgpvpn1_id = bgpvpn1['bgpvpn']['id']
185 logger.debug("VPN1 created details: %s" % bgpvpn1)
186 bgpvpn_ids.append(bgpvpn1_id)
188 msg = ("Associate network '%s' to the VPN." % TESTCASE_CONFIG.net_1_name)
189 results.record_action(msg)
190 results.add_to_summary(0, "-")
192 test_utils.create_network_association(
193 neutron_client, bgpvpn1_id, network_1_id)
195 # Wait for VMs to get ips.
196 instances_up = test_utils.wait_for_instances_up(vm_1, vm_2,
201 logger.error("One or more instances is down")
204 logger.info("Waiting for the VMs to connect to each other using the"
205 " updated network configuration")
206 test_utils.wait_before_subtest()
208 # 10.10.10.12 should return sdnvpn-2 to sdnvpn-1
209 results.check_ssh_output(vm_1, vm_2,
210 expected=TESTCASE_CONFIG.instance_2_name,
212 # 10.10.11.13 should return sdnvpn-3 to sdnvpn-1
213 results.check_ssh_output(vm_1, vm_3,
214 expected=TESTCASE_CONFIG.instance_3_name,
217 results.add_to_summary(0, "-")
218 msg = ("Create VPN2 with eRT=iRT")
219 results.record_action(msg)
220 vpn2_name = "sdnvpn-2-" + str(randint(100000, 999999))
221 kwargs = {"import_targets": TESTCASE_CONFIG.targets1,
222 "export_targets": TESTCASE_CONFIG.targets1,
223 "route_targets": TESTCASE_CONFIG.targets1,
224 "route_distinguishers": TESTCASE_CONFIG.route_distinguishers2,
226 bgpvpn2 = test_utils.create_bgpvpn(neutron_client, **kwargs)
227 bgpvpn2_id = bgpvpn2['bgpvpn']['id']
228 logger.debug("VPN created details: %s" % bgpvpn2)
229 bgpvpn_ids.append(bgpvpn2_id)
231 msg = ("Associate network '%s' to the VPN2." % TESTCASE_CONFIG.net_2_name)
232 results.record_action(msg)
233 results.add_to_summary(0, "-")
235 test_utils.create_network_association(
236 neutron_client, bgpvpn2_id, network_2_id)
238 test_utils.wait_for_bgp_net_assoc(neutron_client, bgpvpn1_id, network_1_id)
239 test_utils.wait_for_bgp_net_assoc(neutron_client, bgpvpn2_id, network_2_id)
241 logger.info("Waiting for the VMs to connect to each other using the"
242 " updated network configuration")
243 test_utils.wait_before_subtest()
245 # 10.10.11.13 should return sdnvpn-5 to sdnvpn-4
246 results.check_ssh_output(vm_4, vm_5,
247 expected=TESTCASE_CONFIG.instance_5_name,
250 # 10.10.10.11 should return "not reachable" to sdnvpn-4
251 results.check_ssh_output(vm_4, vm_1,
252 expected="not reachable",
255 test_utils.cleanup_nova(nova_client, instance_ids, image_ids)
256 test_utils.cleanup_neutron(neutron_client, floatingip_ids, bgpvpn_ids,
257 interfaces, subnet_ids, router_ids,
259 return results.compile_summary()
262 if __name__ == '__main__':
263 logging.basicConfig(level=logging.INFO)