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
13 from random import randint
16 import functest.utils.functest_logger as ft_logger
17 import functest.utils.functest_utils as ft_utils
18 import functest.utils.openstack_utils as os_utils
20 import utils as test_utils
21 from results import Results
23 parser = argparse.ArgumentParser()
25 parser.add_argument("-r", "--report",
26 help="Create json result file",
29 args = parser.parse_args()
31 logger = ft_logger.Logger("sdnvpn-testcase-2").getLogger()
33 REPO_PATH = os.environ['repos_dir'] + '/sdnvpn/'
37 config_file = REPO_PATH + 'test/functest/config.yaml'
39 INSTANCE_1_NAME = ft_utils.get_parameter_from_yaml(
40 "testcases.testcase_2.instance_1_name", config_file)
41 INSTANCE_1_IP = "10.10.10.11"
42 INSTANCE_2_NAME = ft_utils.get_parameter_from_yaml(
43 "testcases.testcase_2.instance_2_name", config_file)
44 INSTANCE_2_IP = "10.10.10.12"
45 INSTANCE_3_NAME = ft_utils.get_parameter_from_yaml(
46 "testcases.testcase_2.instance_3_name", config_file)
47 INSTANCE_3_IP = "10.10.11.13"
48 INSTANCE_4_NAME = ft_utils.get_parameter_from_yaml(
49 "testcases.testcase_2.instance_4_name", config_file)
50 INSTANCE_4_IP = "10.10.10.12"
51 INSTANCE_5_NAME = ft_utils.get_parameter_from_yaml(
52 "testcases.testcase_2.instance_5_name", config_file)
53 INSTANCE_5_IP = "10.10.11.13"
54 IMAGE_NAME = ft_utils.get_parameter_from_yaml(
55 "testcases.testcase_2.image_name", config_file)
56 IMAGE_FILENAME = ft_utils.get_functest_config(
57 "general.openstack.image_file_name")
58 IMAGE_FORMAT = ft_utils.get_functest_config(
59 "general.openstack.image_disk_format")
60 IMAGE_PATH = ft_utils.get_functest_config(
61 "general.directories.dir_functest_data") + "/" + IMAGE_FILENAME
63 KEYFILE_PATH = REPO_PATH + 'test/functest/id_rsa'
65 # NEUTRON Private Network parameters
67 NET_1_NAME = ft_utils.get_parameter_from_yaml(
68 "testcases.testcase_2.net_1_name", config_file)
69 SUBNET_1a_NAME = ft_utils.get_parameter_from_yaml(
70 "testcases.testcase_2.subnet_1a_name", config_file)
71 SUBNET_1a_CIDR = ft_utils.get_parameter_from_yaml(
72 "testcases.testcase_2.subnet_1a_cidr", config_file)
73 SUBNET_1b_NAME = ft_utils.get_parameter_from_yaml(
74 "testcases.testcase_2.subnet_1b_name", config_file)
75 SUBNET_1b_CIDR = ft_utils.get_parameter_from_yaml(
76 "testcases.testcase_2.subnet_1b_cidr", config_file)
77 ROUTER_1_NAME = ft_utils.get_parameter_from_yaml(
78 "testcases.testcase_2.router_1_name", config_file)
79 NET_2_NAME = ft_utils.get_parameter_from_yaml(
80 "testcases.testcase_2.net_2_name", config_file)
81 SUBNET_2a_NAME = ft_utils.get_parameter_from_yaml(
82 "testcases.testcase_2.subnet_2a_name", config_file)
83 SUBNET_2a_CIDR = ft_utils.get_parameter_from_yaml(
84 "testcases.testcase_2.subnet_2a_cidr", config_file)
85 SUBNET_2b_NAME = ft_utils.get_parameter_from_yaml(
86 "testcases.testcase_2.subnet_2b_name", config_file)
87 SUBNET_2b_CIDR = ft_utils.get_parameter_from_yaml(
88 "testcases.testcase_2.subnet_2b_cidr", config_file)
89 ROUTER_1_NAME = ft_utils.get_parameter_from_yaml(
90 "testcases.testcase_2.router_1_name", config_file)
91 ROUTER_2_NAME = ft_utils.get_parameter_from_yaml(
92 "testcases.testcase_2.router_2_name", config_file)
93 SECGROUP_NAME = ft_utils.get_parameter_from_yaml(
94 "testcases.testcase_2.sdnvpn_sg_name", config_file)
95 SECGROUP_DESCR = ft_utils.get_parameter_from_yaml(
96 "testcases.testcase_2.sdnvpn_sg_descr", config_file)
97 TARGETS_1 = ft_utils.get_parameter_from_yaml(
98 "testcases.testcase_2.targets1", config_file)
99 TARGETS_2 = ft_utils.get_parameter_from_yaml(
100 "testcases.testcase_2.targets2", config_file)
101 SUCCESS_CRITERIA = ft_utils.get_parameter_from_yaml(
102 "testcases.testcase_1.succes_criteria", config_file)
103 TEST_DB = ft_utils.get_functest_config("results.test_db_url")
105 LINE_LENGTH = 90 # length for the summary table
111 results = Results(LINE_LENGTH)
113 results.add_to_summary(0, "=")
114 results.add_to_summary(2, "STATUS", "SUBTEST")
115 results.add_to_summary(0, "=")
117 nova_client = os_utils.get_nova_client()
118 neutron_client = os_utils.get_neutron_client()
119 glance_client = os_utils.get_glance_client()
121 logger.debug("Using private key %s injected to the VMs." % KEYFILE_PATH)
122 keyfile = open(KEYFILE_PATH, 'r')
125 files = {"/home/cirros/id_rsa": key}
127 image_id = os_utils.create_glance_image(glance_client,
133 network_1_id, _, _ = test_utils.create_network(neutron_client,
140 network_2_id, _, _ = test_utils.create_network(neutron_client,
147 sg_id = os_utils.create_security_group_full(neutron_client,
148 SECGROUP_NAME, SECGROUP_DESCR)
150 compute_nodes = test_utils.assert_and_get_compute_nodes(nova_client)
152 av_zone_1 = "nova:" + compute_nodes[0]
153 av_zone_2 = "nova:" + compute_nodes[1]
156 userdata_common = test_utils.generate_userdata_common()
157 vm_2 = test_utils.create_instance(nova_client,
162 fixed_ip=INSTANCE_2_IP,
163 secgroup_name=SECGROUP_NAME,
164 compute_node=av_zone_1,
165 userdata=userdata_common)
166 vm_2_ip = vm_2.networks.itervalues().next()[0]
167 logger.debug("Instance '%s' booted successfully. IP='%s'." %
168 (INSTANCE_2_NAME, vm_2_ip))
170 vm_3 = test_utils.create_instance(nova_client,
175 fixed_ip=INSTANCE_3_IP,
176 secgroup_name=SECGROUP_NAME,
177 compute_node=av_zone_2,
178 userdata=userdata_common)
179 vm_3_ip = vm_3.networks.itervalues().next()[0]
180 logger.debug("Instance '%s' booted successfully. IP='%s'." %
181 (INSTANCE_3_NAME, vm_3_ip))
183 vm_5 = test_utils.create_instance(nova_client,
188 fixed_ip=INSTANCE_5_IP,
189 secgroup_name=SECGROUP_NAME,
190 compute_node=av_zone_2,
191 userdata=userdata_common)
192 vm_5_ip = vm_5.networks.itervalues().next()[0]
193 logger.debug("Instance '%s' booted successfully. IP='%s'." %
194 (INSTANCE_5_NAME, vm_5_ip))
196 # We boot vm5 first because we need vm5_ip for vm4 userdata
197 u4 = test_utils.generate_userdata_with_ssh(
198 [INSTANCE_1_IP, INSTANCE_3_IP, INSTANCE_5_IP])
199 vm_4 = test_utils.create_instance(nova_client,
204 fixed_ip=INSTANCE_4_IP,
205 secgroup_name=SECGROUP_NAME,
206 compute_node=av_zone_1,
209 vm_4_ip = vm_4.networks.itervalues().next()[0]
210 logger.debug("Instance '%s' booted successfully. IP='%s'." %
211 (INSTANCE_4_NAME, vm_4_ip))
213 # We boot VM1 at the end because we need to get the IPs first to generate
215 u1 = test_utils.generate_userdata_with_ssh(
216 [INSTANCE_2_IP, INSTANCE_3_IP, INSTANCE_4_IP, INSTANCE_5_IP])
217 vm_1 = test_utils.create_instance(nova_client,
222 fixed_ip=INSTANCE_1_IP,
223 secgroup_name=SECGROUP_NAME,
224 compute_node=av_zone_1,
227 vm_1_ip = vm_1.networks.itervalues().next()[0]
228 logger.debug("Instance '%s' booted successfully. IP='%s'." %
229 (INSTANCE_1_NAME, vm_1_ip))
231 msg = ("Create VPN1 with eRT=iRT")
233 results.add_to_summary(1, msg)
234 vpn1_name = "sdnvpn-1-" + str(randint(100000, 999999))
235 kwargs = {"import_targets": TARGETS_2,
236 "export_targets": TARGETS_2,
237 "route_targets": TARGETS_2,
239 bgpvpn1 = os_utils.create_bgpvpn(neutron_client, **kwargs)
240 bgpvpn1_id = bgpvpn1['bgpvpn']['id']
241 logger.debug("VPN1 created details: %s" % bgpvpn1)
243 msg = ("Associate network '%s' to the VPN." % NET_1_NAME)
245 results.add_to_summary(1, msg)
246 results.add_to_summary(0, "-")
248 os_utils.create_network_association(
249 neutron_client, bgpvpn1_id, network_1_id)
251 # Wait for VMs to get ips.
252 instances_up = test_utils.wait_for_instances_up(vm_1, vm_2,
257 logger.error("One or more instances is down")
260 logger.info("Waiting for the VMs to connect to each other using the"
261 " updated network configuration")
262 test_utils.wait_before_subtest()
264 # 10.10.10.12 should return sdnvpn-2 to sdnvpn-1
265 results.check_ssh_output(
266 vm_1, vm_1_ip, vm_2, vm_2_ip, expected=INSTANCE_2_NAME, timeout=200)
267 # 10.10.11.13 should return sdnvpn-3 to sdnvpn-1
268 results.check_ssh_output(
269 vm_1, vm_1_ip, vm_3, vm_3_ip, expected=INSTANCE_3_NAME, timeout=30)
271 results.add_to_summary(0, "-")
272 msg = ("Create VPN2 with eRT=iRT")
274 results.add_to_summary(1, msg)
275 vpn2_name = "sdnvpn-2-" + str(randint(100000, 999999))
276 kwargs = {"import_targets": TARGETS_1,
277 "export_targets": TARGETS_1,
278 "route_targets": TARGETS_1,
280 bgpvpn2 = os_utils.create_bgpvpn(neutron_client, **kwargs)
281 bgpvpn2_id = bgpvpn2['bgpvpn']['id']
282 logger.debug("VPN created details: %s" % bgpvpn2)
284 msg = ("Associate network '%s' to the VPN2." % NET_2_NAME)
286 results.add_to_summary(1, msg)
287 results.add_to_summary(0, "-")
289 os_utils.create_network_association(
290 neutron_client, bgpvpn2_id, network_2_id)
292 test_utils.wait_for_bgp_net_assoc(neutron_client, bgpvpn1_id, network_1_id)
293 test_utils.wait_for_bgp_net_assoc(neutron_client, bgpvpn2_id, network_2_id)
295 logger.info("Waiting for the VMs to connect to each other using the"
296 " updated network configuration")
297 test_utils.wait_before_subtest()
299 # 10.10.11.13 should return sdnvpn-5 to sdnvpn-4
300 results.check_ssh_output(
301 vm_4, vm_4_ip, vm_5, vm_5_ip, expected=INSTANCE_5_NAME, timeout=30)
303 # 10.10.10.11 should return "not reachable" to sdnvpn-4
304 results.check_ssh_output(
305 vm_4, vm_4_ip, vm_1, vm_1_ip, expected="not reachable", timeout=30)
307 return results.compile_summary(SUCCESS_CRITERIA)
310 if __name__ == '__main__':