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]
100 logger.debug("Instance '%s' booted successfully. IP='%s'." %
101 (TESTCASE_CONFIG.instance_2_name, vm_2_ip))
103 vm_3 = test_utils.create_instance(
105 TESTCASE_CONFIG.instance_3_name,
109 fixed_ip=TESTCASE_CONFIG.instance_3_ip,
110 secgroup_name=TESTCASE_CONFIG.secgroup_name,
111 compute_node=av_zone_2,
112 userdata=userdata_common)
113 vm_3_ip = vm_3.networks.itervalues().next()[0]
114 logger.debug("Instance '%s' booted successfully. IP='%s'." %
115 (TESTCASE_CONFIG.instance_3_name, vm_3_ip))
117 vm_5 = test_utils.create_instance(
119 TESTCASE_CONFIG.instance_5_name,
123 fixed_ip=TESTCASE_CONFIG.instance_5_ip,
124 secgroup_name=TESTCASE_CONFIG.secgroup_name,
125 compute_node=av_zone_2,
126 userdata=userdata_common)
127 vm_5_ip = vm_5.networks.itervalues().next()[0]
128 logger.debug("Instance '%s' booted successfully. IP='%s'." %
129 (TESTCASE_CONFIG.instance_5_name, vm_5_ip))
131 # We boot vm5 first because we need vm5_ip for vm4 userdata
132 u4 = test_utils.generate_userdata_with_ssh(
133 [TESTCASE_CONFIG.instance_1_ip,
134 TESTCASE_CONFIG.instance_3_ip,
135 TESTCASE_CONFIG.instance_5_ip])
136 vm_4 = test_utils.create_instance(
138 TESTCASE_CONFIG.instance_4_name,
142 fixed_ip=TESTCASE_CONFIG.instance_4_ip,
143 secgroup_name=TESTCASE_CONFIG.secgroup_name,
144 compute_node=av_zone_1,
147 vm_4_ip = vm_4.networks.itervalues().next()[0]
148 logger.debug("Instance '%s' booted successfully. IP='%s'." %
149 (TESTCASE_CONFIG.instance_4_name, vm_4_ip))
151 # We boot VM1 at the end because we need to get the IPs first to generate
153 u1 = test_utils.generate_userdata_with_ssh(
154 [TESTCASE_CONFIG.instance_2_ip,
155 TESTCASE_CONFIG.instance_3_ip,
156 TESTCASE_CONFIG.instance_4_ip,
157 TESTCASE_CONFIG.instance_5_ip])
158 vm_1 = test_utils.create_instance(
160 TESTCASE_CONFIG.instance_1_name,
164 fixed_ip=TESTCASE_CONFIG.instance_1_ip,
165 secgroup_name=TESTCASE_CONFIG.secgroup_name,
166 compute_node=av_zone_1,
169 vm_1_ip = vm_1.networks.itervalues().next()[0]
170 logger.debug("Instance '%s' booted successfully. IP='%s'." %
171 (TESTCASE_CONFIG.instance_1_name, vm_1_ip))
173 msg = ("Create VPN1 with eRT=iRT")
175 results.add_to_summary(1, msg)
176 vpn1_name = "sdnvpn-1-" + str(randint(100000, 999999))
177 kwargs = {"import_targets": TESTCASE_CONFIG.targets2,
178 "export_targets": TESTCASE_CONFIG.targets2,
179 "route_targets": TESTCASE_CONFIG.targets2,
181 bgpvpn1 = os_utils.create_bgpvpn(neutron_client, **kwargs)
182 bgpvpn1_id = bgpvpn1['bgpvpn']['id']
183 logger.debug("VPN1 created details: %s" % bgpvpn1)
185 msg = ("Associate network '%s' to the VPN." % TESTCASE_CONFIG.net_1_name)
187 results.add_to_summary(1, msg)
188 results.add_to_summary(0, "-")
190 os_utils.create_network_association(
191 neutron_client, bgpvpn1_id, network_1_id)
193 # Wait for VMs to get ips.
194 instances_up = test_utils.wait_for_instances_up(vm_1, vm_2,
199 logger.error("One or more instances is down")
202 logger.info("Waiting for the VMs to connect to each other using the"
203 " updated network configuration")
204 test_utils.wait_before_subtest()
206 # 10.10.10.12 should return sdnvpn-2 to sdnvpn-1
207 results.check_ssh_output(
210 expected=TESTCASE_CONFIG.instance_2_name,
212 # 10.10.11.13 should return sdnvpn-3 to sdnvpn-1
213 results.check_ssh_output(
216 expected=TESTCASE_CONFIG.instance_3_name,
219 results.add_to_summary(0, "-")
220 msg = ("Create VPN2 with eRT=iRT")
222 results.add_to_summary(1, msg)
223 vpn2_name = "sdnvpn-2-" + str(randint(100000, 999999))
224 kwargs = {"import_targets": TESTCASE_CONFIG.targets1,
225 "export_targets": TESTCASE_CONFIG.targets1,
226 "route_targets": TESTCASE_CONFIG.targets1,
228 bgpvpn2 = os_utils.create_bgpvpn(neutron_client, **kwargs)
229 bgpvpn2_id = bgpvpn2['bgpvpn']['id']
230 logger.debug("VPN created details: %s" % bgpvpn2)
232 msg = ("Associate network '%s' to the VPN2." % TESTCASE_CONFIG.net_2_name)
234 results.add_to_summary(1, msg)
235 results.add_to_summary(0, "-")
237 os_utils.create_network_association(
238 neutron_client, bgpvpn2_id, network_2_id)
240 test_utils.wait_for_bgp_net_assoc(neutron_client, bgpvpn1_id, network_1_id)
241 test_utils.wait_for_bgp_net_assoc(neutron_client, bgpvpn2_id, network_2_id)
243 logger.info("Waiting for the VMs to connect to each other using the"
244 " updated network configuration")
245 test_utils.wait_before_subtest()
247 # 10.10.11.13 should return sdnvpn-5 to sdnvpn-4
248 results.check_ssh_output(
251 expected=TESTCASE_CONFIG.instance_5_name,
254 # 10.10.10.11 should return "not reachable" to sdnvpn-4
255 results.check_ssh_output(
256 vm_4, vm_4_ip, vm_1, vm_1_ip, expected="not reachable", timeout=30)
258 return results.compile_summary(TESTCASE_CONFIG.success_criteria)
261 if __name__ == '__main__':