3 # Copyright (c) 2015 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
12 from random import randint
15 import functest.utils.functest_logger as ft_logger
16 import functest.utils.openstack_utils as os_utils
18 import utils as test_utils
19 from results import Results
20 import 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 = ft_logger.Logger("sdnvpn-testcase-2").getLogger()
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 logger.debug("Using private key %s injected to the VMs."
48 % COMMON_CONFIG.keyfile_path)
49 keyfile = open(COMMON_CONFIG.keyfile_path, 'r')
52 files = {"/home/cirros/id_rsa": key}
54 image_id = os_utils.create_glance_image(glance_client,
55 TESTCASE_CONFIG.image_name,
56 COMMON_CONFIG.image_path,
57 disk=COMMON_CONFIG.image_format,
60 network_1_id, _, _ = test_utils.create_network(
62 TESTCASE_CONFIG.net_1_name,
63 TESTCASE_CONFIG.subnet_1a_name,
64 TESTCASE_CONFIG.subnet_1a_cidr,
65 TESTCASE_CONFIG.router_1_name,
66 TESTCASE_CONFIG.subnet_1b_name,
67 TESTCASE_CONFIG.subnet_1b_cidr)
69 network_2_id, _, _ = test_utils.create_network(
71 TESTCASE_CONFIG.net_2_name,
72 TESTCASE_CONFIG.subnet_2a_name,
73 TESTCASE_CONFIG.subnet_2a_cidr,
74 TESTCASE_CONFIG.router_2_name,
75 TESTCASE_CONFIG.subnet_2b_name,
76 TESTCASE_CONFIG.subnet_2b_cidr)
78 sg_id = os_utils.create_security_group_full(neutron_client,
79 TESTCASE_CONFIG.secgroup_name,
80 TESTCASE_CONFIG.secgroup_descr)
82 compute_nodes = test_utils.assert_and_get_compute_nodes(nova_client)
84 av_zone_1 = "nova:" + compute_nodes[0]
85 av_zone_2 = "nova:" + compute_nodes[1]
88 userdata_common = test_utils.generate_userdata_common()
89 vm_2 = test_utils.create_instance(
91 TESTCASE_CONFIG.instance_2_name,
95 fixed_ip=TESTCASE_CONFIG.instance_2_ip,
96 secgroup_name=TESTCASE_CONFIG.secgroup_name,
97 compute_node=av_zone_1,
98 userdata=userdata_common)
99 vm_2_ip = vm_2.networks.itervalues().next()[0]
101 vm_3 = test_utils.create_instance(
103 TESTCASE_CONFIG.instance_3_name,
107 fixed_ip=TESTCASE_CONFIG.instance_3_ip,
108 secgroup_name=TESTCASE_CONFIG.secgroup_name,
109 compute_node=av_zone_2,
110 userdata=userdata_common)
111 vm_3_ip = vm_3.networks.itervalues().next()[0]
113 vm_5 = test_utils.create_instance(
115 TESTCASE_CONFIG.instance_5_name,
119 fixed_ip=TESTCASE_CONFIG.instance_5_ip,
120 secgroup_name=TESTCASE_CONFIG.secgroup_name,
121 compute_node=av_zone_2,
122 userdata=userdata_common)
123 vm_5_ip = vm_5.networks.itervalues().next()[0]
125 # We boot vm5 first because we need vm5_ip for vm4 userdata
126 u4 = test_utils.generate_userdata_with_ssh(
127 [TESTCASE_CONFIG.instance_1_ip,
128 TESTCASE_CONFIG.instance_3_ip,
129 TESTCASE_CONFIG.instance_5_ip])
130 vm_4 = test_utils.create_instance(
132 TESTCASE_CONFIG.instance_4_name,
136 fixed_ip=TESTCASE_CONFIG.instance_4_ip,
137 secgroup_name=TESTCASE_CONFIG.secgroup_name,
138 compute_node=av_zone_1,
141 vm_4_ip = vm_4.networks.itervalues().next()[0]
143 # We boot VM1 at the end because we need to get the IPs first to generate
145 u1 = test_utils.generate_userdata_with_ssh(
146 [TESTCASE_CONFIG.instance_2_ip,
147 TESTCASE_CONFIG.instance_3_ip,
148 TESTCASE_CONFIG.instance_4_ip,
149 TESTCASE_CONFIG.instance_5_ip])
150 vm_1 = test_utils.create_instance(
152 TESTCASE_CONFIG.instance_1_name,
156 fixed_ip=TESTCASE_CONFIG.instance_1_ip,
157 secgroup_name=TESTCASE_CONFIG.secgroup_name,
158 compute_node=av_zone_1,
161 vm_1_ip = vm_1.networks.itervalues().next()[0]
163 msg = ("Create VPN1 with eRT=iRT")
165 results.add_to_summary(1, msg)
166 vpn1_name = "sdnvpn-1-" + str(randint(100000, 999999))
167 kwargs = {"import_targets": TESTCASE_CONFIG.targets2,
168 "export_targets": TESTCASE_CONFIG.targets2,
169 "route_targets": TESTCASE_CONFIG.targets2,
171 bgpvpn1 = os_utils.create_bgpvpn(neutron_client, **kwargs)
172 bgpvpn1_id = bgpvpn1['bgpvpn']['id']
173 logger.debug("VPN1 created details: %s" % bgpvpn1)
175 msg = ("Associate network '%s' to the VPN." % TESTCASE_CONFIG.net_1_name)
177 results.add_to_summary(1, msg)
178 results.add_to_summary(0, "-")
180 os_utils.create_network_association(
181 neutron_client, bgpvpn1_id, network_1_id)
183 # Wait for VMs to get ips.
184 instances_up = test_utils.wait_for_instances_up(vm_1, vm_2,
189 logger.error("One or more instances is down")
192 logger.info("Waiting for the VMs to connect to each other using the"
193 " updated network configuration")
194 test_utils.wait_before_subtest()
196 # 10.10.10.12 should return sdnvpn-2 to sdnvpn-1
197 results.check_ssh_output(
200 expected=TESTCASE_CONFIG.instance_2_name,
202 # 10.10.11.13 should return sdnvpn-3 to sdnvpn-1
203 results.check_ssh_output(
206 expected=TESTCASE_CONFIG.instance_3_name,
209 results.add_to_summary(0, "-")
210 msg = ("Create VPN2 with eRT=iRT")
212 results.add_to_summary(1, msg)
213 vpn2_name = "sdnvpn-2-" + str(randint(100000, 999999))
214 kwargs = {"import_targets": TESTCASE_CONFIG.targets1,
215 "export_targets": TESTCASE_CONFIG.targets1,
216 "route_targets": TESTCASE_CONFIG.targets1,
218 bgpvpn2 = os_utils.create_bgpvpn(neutron_client, **kwargs)
219 bgpvpn2_id = bgpvpn2['bgpvpn']['id']
220 logger.debug("VPN created details: %s" % bgpvpn2)
222 msg = ("Associate network '%s' to the VPN2." % TESTCASE_CONFIG.net_2_name)
224 results.add_to_summary(1, msg)
225 results.add_to_summary(0, "-")
227 os_utils.create_network_association(
228 neutron_client, bgpvpn2_id, network_2_id)
230 test_utils.wait_for_bgp_net_assoc(neutron_client, bgpvpn1_id, network_1_id)
231 test_utils.wait_for_bgp_net_assoc(neutron_client, bgpvpn2_id, network_2_id)
233 logger.info("Waiting for the VMs to connect to each other using the"
234 " updated network configuration")
235 test_utils.wait_before_subtest()
237 # 10.10.11.13 should return sdnvpn-5 to sdnvpn-4
238 results.check_ssh_output(
241 expected=TESTCASE_CONFIG.instance_5_name,
244 # 10.10.10.11 should return "not reachable" to sdnvpn-4
245 results.check_ssh_output(
246 vm_4, vm_4_ip, vm_1, vm_1_ip, expected="not reachable", timeout=30)
248 return results.compile_summary(TESTCASE_CONFIG.success_criteria)
251 if __name__ == '__main__':