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
17 import functest.utils.functest_logger as ft_logger
18 import functest.utils.functest_utils as ft_utils
19 import functest.utils.openstack_utils as os_utils
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-4").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_4.instance_1_name", config_file)
40 INSTANCE_2_NAME = ft_utils.get_parameter_from_yaml(
41 "testcases.testcase_4.instance_2_name", config_file)
42 INSTANCE_3_NAME = ft_utils.get_parameter_from_yaml(
43 "testcases.testcase_4.instance_3_name", config_file)
44 INSTANCE_4_NAME = ft_utils.get_parameter_from_yaml(
45 "testcases.testcase_4.instance_4_name", config_file)
46 INSTANCE_5_NAME = ft_utils.get_parameter_from_yaml(
47 "testcases.testcase_4.instance_5_name", config_file)
48 FLAVOR = ft_utils.get_parameter_from_yaml(
49 "testcases.testcase_4.flavor", config_file)
50 IMAGE_NAME = ft_utils.get_parameter_from_yaml(
51 "testcases.testcase_4.image_name", config_file)
52 IMAGE_FILENAME = ft_utils.get_parameter_from_yaml(
53 "general.openstack.image_file_name")
54 IMAGE_FORMAT = ft_utils.get_parameter_from_yaml(
55 "general.openstack.image_disk_format")
56 IMAGE_PATH = ft_utils.get_parameter_from_yaml(
57 "general.directories.dir_functest_data") + "/" + IMAGE_FILENAME
59 # NEUTRON Private Network parameters
61 NET_1_NAME = ft_utils.get_parameter_from_yaml(
62 "testcases.testcase_4.net_1_name", config_file)
63 SUBNET_1_NAME = ft_utils.get_parameter_from_yaml(
64 "testcases.testcase_4.subnet_1_name", config_file)
65 SUBNET_1_CIDR = ft_utils.get_parameter_from_yaml(
66 "testcases.testcase_4.subnet_1_cidr", config_file)
67 ROUTER_1_NAME = ft_utils.get_parameter_from_yaml(
68 "testcases.testcase_4.router_1_name", config_file)
69 NET_2_NAME = ft_utils.get_parameter_from_yaml(
70 "testcases.testcase_4.net_2_name", config_file)
71 SUBNET_2_NAME = ft_utils.get_parameter_from_yaml(
72 "testcases.testcase_4.subnet_2_name", config_file)
73 SUBNET_2_CIDR = ft_utils.get_parameter_from_yaml(
74 "testcases.testcase_4.subnet_2_cidr", config_file)
75 ROUTER_2_NAME = ft_utils.get_parameter_from_yaml(
76 "testcases.testcase_4.router_2_name", config_file)
77 SECGROUP_NAME = ft_utils.get_parameter_from_yaml(
78 "testcases.testcase_4.sdnvpn_sg_name", config_file)
79 SECGROUP_DESCR = ft_utils.get_parameter_from_yaml(
80 "testcases.testcase_4.sdnvpn_sg_descr", config_file)
81 TARGETS_1 = ft_utils.get_parameter_from_yaml(
82 "testcases.testcase_4.targets1", config_file)
83 TARGETS_2 = ft_utils.get_parameter_from_yaml(
84 "testcases.testcase_4.targets2", config_file)
85 SUCCESS_CRITERIA = ft_utils.get_parameter_from_yaml(
86 "testcases.testcase_4.succes_criteria", config_file)
87 TEST_DB = ft_utils.get_parameter_from_yaml("results.test_db_url")
91 LINE_LENGTH = 60 # length for the summary table
97 def create_network(neutron_client, net, subnet, router, cidr):
98 network_dic = os_utils.create_network_full(neutron_client,
105 "There has been a problem when creating the neutron network")
107 return network_dic["net_id"], \
108 network_dic["subnet_id"], \
109 network_dic["router_id"]
112 def create_instance(nova_client,
120 logger.info("Creating instance '%s'..." % name)
122 "Configuration:\n name=%s \n flavor=%s \n image=%s \n "
123 "network=%s \n secgroup=%s \n hypervisor=%s \n userdata=%s\n"
124 % (name, flavor, image_id, network_id, sg_id, compute_node, userdata))
125 instance = os_utils.create_instance_and_wait_for_active(
132 av_zone=compute_node)
135 logger.error("Error while booting instance.")
137 # Retrieve IP of INSTANCE
138 # instance_ip = instance.networks.get(network_id)[0]
140 logger.debug("Adding '%s' to security group '%s'..."
141 % (name, SECGROUP_NAME))
142 os_utils.add_secgroup_to_instance(nova_client, instance.id, sg_id)
147 def generate_ping_userdata(ips_array):
150 ips = ("%s %s" % (ips, ip))
152 ips = ips.replace(' ', ' ')
153 return ("#!/bin/sh\n"
158 " ping -c 1 $ip 2>&1 >/dev/null\n"
160 " if [ \"Z$RES\" = \"Z0\" ] ; then\n"
161 " echo ping $ip OK\n"
162 " else echo ping $ip KO\n"
170 def get_ping_status(vm_source, ip_source,
171 vm_target, ip_target,
172 expected="PASS", timeout=30):
173 console_log = vm_source.get_console_output()
177 if "request failed" in console_log:
178 # Normally, cirros displays this message when userdata fails
179 logger.debug("It seems userdata is not supported in "
183 tab = ("%s" % (" " * 53))
184 expected_result = 'can ping' if expected == 'PASS' else 'cannot ping'
185 test_case_name = ("'%s' %s '%s'" %
186 (vm_source.name, expected_result, vm_target.name))
187 logger.debug("%sPing\n%sfrom '%s' (%s)\n%sto '%s' (%s).\n"
188 "%s-->Expected result: %s.\n"
189 % (tab, tab, vm_source.name, ip_source,
190 tab, vm_target.name, ip_target,
191 tab, expected_result))
193 console_log = vm_source.get_console_output()
194 # the console_log is a long string, we want to take
195 # the last 4 lines (for example)
196 lines = console_log.split('\n')
197 last_n_lines = lines[-5:]
198 if ("ping %s OK" % ip_target) in last_n_lines:
199 msg = ("'%s' can ping '%s'" % (vm_source.name, vm_target.name))
200 if expected == "PASS":
201 logger.debug("[PASS] %s" % msg)
202 add_to_summary(2, "PASS", test_case_name)
204 logger.debug("[FAIL] %s" % msg)
206 add_to_summary(2, "FAIL", test_case_name)
207 logger.debug("\n%s" % last_n_lines)
209 elif ("ping %s KO" % ip_target) in last_n_lines:
210 msg = ("'%s' cannot ping '%s'" %
211 (vm_source.name, vm_target.name))
212 if expected == "FAIL":
213 logger.debug("[PASS] %s" % msg)
214 add_to_summary(2, "PASS", test_case_name)
216 logger.debug("[FAIL] %s" % msg)
218 add_to_summary(2, "FAIL", test_case_name)
224 logger.debug("[FAIL] Timeout reached for '%s'. No ping output "
225 "captured in the console log" % vm_source.name)
226 add_to_summary(2, "FAIL", test_case_name)
230 def add_to_summary(num_cols, col1, col2=""):
231 global SUMMARY, LINE_LENGTH, DETAILS, NUM_TESTS, NUM_TESTS_FAILED
233 SUMMARY += ("+%s+\n" % (col1 * (LINE_LENGTH - 2)))
235 SUMMARY += ("| " + col1.ljust(LINE_LENGTH - 3) + "|\n")
237 SUMMARY += ("| %s" % col1.ljust(7) + "| ")
238 SUMMARY += (col2.ljust(LINE_LENGTH - 12) + "|\n")
239 if col1 in ("FAIL", "PASS"):
240 DETAILS.append({col2: col1})
243 NUM_TESTS_FAILED += 1
247 global TEST_RESULT, SUMMARY
249 add_to_summary(0, "=")
250 add_to_summary(2, "STATUS", "SUBTEST")
251 add_to_summary(0, "=")
253 nova_client = os_utils.get_nova_client()
254 neutron_client = os_utils.get_neutron_client()
255 glance_client = os_utils.get_glance_client()
257 image_id = os_utils.create_glance_image(glance_client,
263 network_1_id, _, router_1_id = create_network(neutron_client,
268 network_2_id, _, router_2_id = create_network(neutron_client,
273 sg_id = os_utils.create_security_group_full(neutron_client,
274 SECGROUP_NAME, SECGROUP_DESCR)
276 # Get hypervisors zones
277 compute_nodes = os_utils.get_hypervisors(nova_client)
278 num_compute_nodes = len(compute_nodes)
279 if num_compute_nodes < 2:
280 logger.error("There are %s compute nodes in the deployment. "
281 "Minimum number of nodes to complete the test is 2."
285 logger.debug("Compute nodes: %s" % compute_nodes)
286 av_zone_1 = "nova:" + compute_nodes[0]
287 av_zone_2 = "nova:" + compute_nodes[1]
290 vm_2 = create_instance(nova_client,
297 vm_2_ip = vm_2.networks.itervalues().next()[0]
298 logger.debug("Instance '%s' booted successfully. IP='%s'." %
299 (INSTANCE_2_NAME, vm_2_ip))
301 vm_3 = create_instance(nova_client,
307 vm_3_ip = vm_3.networks.itervalues().next()[0]
308 logger.debug("Instance '%s' booted successfully. IP='%s'." %
309 (INSTANCE_3_NAME, vm_3_ip))
311 vm_5 = create_instance(nova_client,
318 vm_5_ip = vm_5.networks.itervalues().next()[0]
319 logger.debug("Instance '%s' booted successfully. IP='%s'." %
320 (INSTANCE_5_NAME, vm_5_ip))
322 # We boot vm5 first because we need vm5_ip for vm4 userdata
323 u4 = generate_ping_userdata([vm_5_ip])
324 vm_4 = create_instance(nova_client,
332 vm_4_ip = vm_4.networks.itervalues().next()[0]
333 logger.debug("Instance '%s' booted successfully. IP='%s'." %
334 (INSTANCE_4_NAME, vm_4_ip))
336 # We boot VM1 at the end because we need to get the IPs first to generate
338 u1 = generate_ping_userdata([vm_2_ip, vm_3_ip, vm_4_ip, vm_5_ip])
339 vm_1 = create_instance(nova_client,
347 vm_1_ip = vm_1.networks.itervalues().next()[0]
348 logger.debug("Instance '%s' booted successfully. IP='%s'." %
349 (INSTANCE_1_NAME, vm_1_ip))
350 msg = ("Create VPN with eRT<>iRT")
352 add_to_summary(1, msg)
353 vpn_name = "sdnvpn-" + str(randint(100000, 999999))
354 kwargs = {"import_targets": TARGETS_1,
355 "export_targets": TARGETS_2,
357 bgpvpn = os_utils.create_bgpvpn(neutron_client, **kwargs)
358 bgpvpn_id = bgpvpn['bgpvpn']['id']
359 logger.debug("VPN created details: %s" % bgpvpn)
361 msg = ("Associate router '%s' to the VPN." % ROUTER_1_NAME)
363 add_to_summary(1, msg)
364 add_to_summary(0, "-")
366 os_utils.create_router_association(
367 neutron_client, bgpvpn_id, router_1_id)
369 # Wait for VMs to get ips.
372 # Ping from VM1 to VM2 should work
373 get_ping_status(vm_1, vm_1_ip, vm_2, vm_2_ip, expected="PASS", timeout=200)
374 # Ping from VM1 to VM3 should work
375 get_ping_status(vm_1, vm_1_ip, vm_3, vm_3_ip, expected="PASS", timeout=30)
376 # Ping from VM1 to VM4 should not work
377 get_ping_status(vm_1, vm_1_ip, vm_4, vm_4_ip, expected="FAIL", timeout=30)
379 msg = ("Associate network '%s' to the VPN." % NET_2_NAME)
381 add_to_summary(0, "-")
382 add_to_summary(1, msg)
383 add_to_summary(0, "-")
384 os_utils.create_network_association(
385 neutron_client, bgpvpn_id, network_2_id)
387 # Wait a bit for this to take effect
390 # Ping from VM4 to VM5 should work
391 get_ping_status(vm_4, vm_4_ip, vm_5, vm_5_ip, expected="PASS", timeout=30)
392 # Ping from VM1 to VM4 should not work
393 get_ping_status(vm_1, vm_1_ip, vm_4, vm_4_ip, expected="FAIL", timeout=30)
394 # Ping from VM1 to VM5 should not work
395 get_ping_status(vm_1, vm_1_ip, vm_5, vm_5_ip, expected="FAIL", timeout=30)
397 msg = ("Update VPN with eRT=iRT ...")
399 add_to_summary(0, "-")
400 add_to_summary(1, msg)
401 add_to_summary(0, "-")
402 kwargs = {"import_targets": TARGETS_1,
403 "export_targets": TARGETS_1,
405 bgpvpn = os_utils.update_bgpvpn(neutron_client, bgpvpn_id, **kwargs)
406 # Wait a bit for this to take effect
409 # Ping from VM1 to VM4 should work
410 get_ping_status(vm_1, vm_1_ip, vm_4, vm_4_ip, expected="PASS", timeout=30)
411 # Ping from VM1 to VM5 should work
412 get_ping_status(vm_1, vm_1_ip, vm_5, vm_5_ip, expected="PASS", timeout=30)
414 add_to_summary(0, "=")
415 logger.info("\n%s" % SUMMARY)
417 if TEST_RESULT == "PASS":
418 logger.info("All the ping tests have passed as expected.")
420 logger.info("One or more ping tests have failed.")
423 success = 100 - (100 * int(NUM_TESTS_FAILED) / int(NUM_TESTS))
424 if success < int(SUCCESS_CRITERIA):
427 return {"status": status, "details": DETAILS}
430 if __name__ == '__main__':