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
15 import functest.utils.functest_logger as ft_logger
16 import functest.utils.functest_utils as ft_utils
17 import functest.utils.openstack_utils as os_utils
19 import utils as test_utils
20 from results import Results
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-1").getLogger()
32 REPO_PATH = os.environ['repos_dir'] + '/sdnvpn/'
36 config_file = REPO_PATH + 'test/functest/config.yaml'
38 INSTANCE_1_NAME = ft_utils.get_parameter_from_yaml(
39 "testcases.testcase_1.instance_1_name", config_file)
40 INSTANCE_2_NAME = ft_utils.get_parameter_from_yaml(
41 "testcases.testcase_1.instance_2_name", config_file)
42 INSTANCE_3_NAME = ft_utils.get_parameter_from_yaml(
43 "testcases.testcase_1.instance_3_name", config_file)
44 INSTANCE_4_NAME = ft_utils.get_parameter_from_yaml(
45 "testcases.testcase_1.instance_4_name", config_file)
46 INSTANCE_5_NAME = ft_utils.get_parameter_from_yaml(
47 "testcases.testcase_1.instance_5_name", config_file)
48 IMAGE_NAME = ft_utils.get_parameter_from_yaml(
49 "testcases.testcase_1.image_name", config_file)
50 ROUTE_DISTINGUISHERS = ft_utils.get_parameter_from_yaml(
51 "testcases.testcase_1.route_distinguishers", config_file)
53 IMAGE_FILENAME = ft_utils.get_functest_config(
54 "general.openstack.image_file_name")
55 IMAGE_FORMAT = ft_utils.get_functest_config(
56 "general.openstack.image_disk_format")
57 IMAGE_PATH = ft_utils.get_functest_config(
58 "general.directories.dir_functest_data") + "/" + IMAGE_FILENAME
60 # NEUTRON Private Network parameters
62 NET_1_NAME = ft_utils.get_parameter_from_yaml(
63 "testcases.testcase_1.net_1_name", config_file)
64 SUBNET_1_NAME = ft_utils.get_parameter_from_yaml(
65 "testcases.testcase_1.subnet_1_name", config_file)
66 SUBNET_1_CIDR = ft_utils.get_parameter_from_yaml(
67 "testcases.testcase_1.subnet_1_cidr", config_file)
68 ROUTER_1_NAME = ft_utils.get_parameter_from_yaml(
69 "testcases.testcase_1.router_1_name", config_file)
70 NET_2_NAME = ft_utils.get_parameter_from_yaml(
71 "testcases.testcase_1.net_2_name", config_file)
72 SUBNET_2_NAME = ft_utils.get_parameter_from_yaml(
73 "testcases.testcase_1.subnet_2_name", config_file)
74 SUBNET_2_CIDR = ft_utils.get_parameter_from_yaml(
75 "testcases.testcase_1.subnet_2_cidr", config_file)
76 ROUTER_2_NAME = ft_utils.get_parameter_from_yaml(
77 "testcases.testcase_1.router_2_name", config_file)
78 SECGROUP_NAME = ft_utils.get_parameter_from_yaml(
79 "testcases.testcase_1.sdnvpn_sg_name", config_file)
80 SECGROUP_DESCR = ft_utils.get_parameter_from_yaml(
81 "testcases.testcase_1.sdnvpn_sg_descr", config_file)
82 TARGETS_1 = ft_utils.get_parameter_from_yaml(
83 "testcases.testcase_1.targets1", config_file)
84 TARGETS_2 = ft_utils.get_parameter_from_yaml(
85 "testcases.testcase_1.targets2", config_file)
86 SUCCESS_CRITERIA = ft_utils.get_parameter_from_yaml(
87 "testcases.testcase_1.succes_criteria", config_file)
88 TEST_DB = ft_utils.get_functest_config("results.test_db_url")
95 results = Results(LINE_LENGTH)
97 results.add_to_summary(0, "=")
98 results.add_to_summary(2, "STATUS", "SUBTEST")
99 results.add_to_summary(0, "=")
101 nova_client = os_utils.get_nova_client()
102 neutron_client = os_utils.get_neutron_client()
103 glance_client = os_utils.get_glance_client()
105 image_id = os_utils.create_glance_image(glance_client,
111 network_1_id = test_utils.create_net(neutron_client,
113 test_utils.create_subnet(neutron_client,
118 network_2_id = test_utils.create_net(neutron_client,
121 test_utils.create_subnet(neutron_client,
126 sg_id = os_utils.create_security_group_full(neutron_client,
127 SECGROUP_NAME, SECGROUP_DESCR)
129 compute_nodes = test_utils.assert_and_get_compute_nodes(nova_client)
131 av_zone_1 = "nova:" + compute_nodes[0]
132 av_zone_2 = "nova:" + compute_nodes[1]
135 vm_2 = test_utils.create_instance(nova_client,
140 secgroup_name=SECGROUP_NAME,
141 compute_node=av_zone_1)
142 vm_2_ip = vm_2.networks.itervalues().next()[0]
143 logger.debug("Instance '%s' booted successfully. IP='%s'." %
144 (INSTANCE_2_NAME, vm_2_ip))
146 vm_3 = test_utils.create_instance(nova_client,
151 secgroup_name=SECGROUP_NAME,
152 compute_node=av_zone_2)
153 vm_3_ip = vm_3.networks.itervalues().next()[0]
154 logger.debug("Instance '%s' booted successfully. IP='%s'." %
155 (INSTANCE_3_NAME, vm_3_ip))
157 vm_5 = test_utils.create_instance(nova_client,
162 secgroup_name=SECGROUP_NAME,
163 compute_node=av_zone_2)
164 vm_5_ip = vm_5.networks.itervalues().next()[0]
165 logger.debug("Instance '%s' booted successfully. IP='%s'." %
166 (INSTANCE_5_NAME, vm_5_ip))
168 # We boot vm5 first because we need vm5_ip for vm4 userdata
169 u4 = test_utils.generate_ping_userdata([vm_5_ip])
170 vm_4 = test_utils.create_instance(nova_client,
175 secgroup_name=SECGROUP_NAME,
176 compute_node=av_zone_1,
178 vm_4_ip = vm_4.networks.itervalues().next()[0]
179 logger.debug("Instance '%s' booted successfully. IP='%s'." %
180 (INSTANCE_4_NAME, vm_4_ip))
182 # We boot VM1 at the end because we need to get the IPs first to generate
184 u1 = test_utils.generate_ping_userdata([vm_2_ip,
188 vm_1 = test_utils.create_instance(nova_client,
193 secgroup_name=SECGROUP_NAME,
194 compute_node=av_zone_1,
196 vm_1_ip = vm_1.networks.itervalues().next()[0]
197 logger.debug("Instance '%s' booted successfully. IP='%s'." %
198 (INSTANCE_1_NAME, vm_1_ip))
199 msg = ("Create VPN with eRT<>iRT")
201 results.add_to_summary(1, msg)
202 vpn_name = "sdnvpn-" + str(randint(100000, 999999))
203 kwargs = {"import_targets": TARGETS_1,
204 "export_targets": TARGETS_2,
205 "route_distinguishers": ROUTE_DISTINGUISHERS,
207 bgpvpn = os_utils.create_bgpvpn(neutron_client, **kwargs)
208 bgpvpn_id = bgpvpn['bgpvpn']['id']
209 logger.debug("VPN created details: %s" % bgpvpn)
211 msg = ("Associate network '%s' to the VPN." % NET_1_NAME)
213 results.add_to_summary(1, msg)
214 results.add_to_summary(0, "-")
216 os_utils.create_network_association(
217 neutron_client, bgpvpn_id, network_1_id)
219 # Wait for VMs to get ips.
220 instances_up = test_utils.wait_for_instances_up(vm_1, vm_2,
225 logger.error("One or more instances is down")
226 # TODO: Handle this appropriately
228 # Ping from VM1 to VM2 should work
229 results.get_ping_status(vm_1, vm_1_ip, vm_2, vm_2_ip,
230 expected="PASS", timeout=200)
231 # Ping from VM1 to VM3 should work
232 results.get_ping_status(vm_1, vm_1_ip, vm_3, vm_3_ip,
233 expected="PASS", timeout=30)
234 # Ping from VM1 to VM4 should not work
235 results.get_ping_status(vm_1, vm_1_ip, vm_4, vm_4_ip,
236 expected="FAIL", timeout=30)
238 msg = ("Associate network '%s' to the VPN." % NET_2_NAME)
240 results.add_to_summary(0, "-")
241 results.add_to_summary(1, msg)
242 results.add_to_summary(0, "-")
243 os_utils.create_network_association(
244 neutron_client, bgpvpn_id, network_2_id)
246 test_utils.wait_for_bgp_net_assocs(neutron_client,
251 logger.info("Waiting for the VMs to connect to each other using the"
252 " updated network configuration")
253 test_utils.wait_before_subtest()
255 # Ping from VM4 to VM5 should work
256 results.get_ping_status(vm_4, vm_4_ip, vm_5, vm_5_ip,
257 expected="PASS", timeout=30)
258 # Ping from VM1 to VM4 should not work
259 results.get_ping_status(vm_1, vm_1_ip, vm_4, vm_4_ip,
260 expected="FAIL", timeout=30)
261 # Ping from VM1 to VM5 should not work
262 results.get_ping_status(vm_1, vm_1_ip, vm_5, vm_5_ip,
263 expected="FAIL", timeout=30)
265 msg = ("Update VPN with eRT=iRT ...")
267 results.add_to_summary(0, "-")
268 results.add_to_summary(1, msg)
269 results.add_to_summary(0, "-")
270 kwargs = {"import_targets": TARGETS_1,
271 "export_targets": TARGETS_1,
273 bgpvpn = os_utils.update_bgpvpn(neutron_client, bgpvpn_id, **kwargs)
275 logger.info("Waiting for the VMs to connect to each other using the"
276 " updated network configuration")
277 test_utils.wait_before_subtest()
279 # Ping from VM1 to VM4 should work
280 results.get_ping_status(vm_1, vm_1_ip, vm_4, vm_4_ip,
281 expected="PASS", timeout=30)
282 # Ping from VM1 to VM5 should work
283 results.get_ping_status(vm_1, vm_1_ip, vm_5, vm_5_ip,
284 expected="PASS", timeout=30)
286 return results.compile_summary(SUCCESS_CRITERIA)
289 if __name__ == '__main__':