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-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 FLAVOR = ft_utils.get_parameter_from_yaml(
49 "testcases.testcase_1.flavor", config_file)
50 IMAGE_NAME = ft_utils.get_parameter_from_yaml(
51 "testcases.testcase_1.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_1.net_1_name", config_file)
63 SUBNET_1_NAME = ft_utils.get_parameter_from_yaml(
64 "testcases.testcase_1.subnet_1_name", config_file)
65 SUBNET_1_CIDR = ft_utils.get_parameter_from_yaml(
66 "testcases.testcase_1.subnet_1_cidr", config_file)
67 ROUTER_1_NAME = ft_utils.get_parameter_from_yaml(
68 "testcases.testcase_1.router_1_name", config_file)
69 NET_2_NAME = ft_utils.get_parameter_from_yaml(
70 "testcases.testcase_1.net_2_name", config_file)
71 SUBNET_2_NAME = ft_utils.get_parameter_from_yaml(
72 "testcases.testcase_1.subnet_2_name", config_file)
73 SUBNET_2_CIDR = ft_utils.get_parameter_from_yaml(
74 "testcases.testcase_1.subnet_2_cidr", config_file)
75 ROUTER_2_NAME = ft_utils.get_parameter_from_yaml(
76 "testcases.testcase_1.router_2_name", config_file)
77 SECGROUP_NAME = ft_utils.get_parameter_from_yaml(
78 "testcases.testcase_1.sdnvpn_sg_name", config_file)
79 SECGROUP_DESCR = ft_utils.get_parameter_from_yaml(
80 "testcases.testcase_1.sdnvpn_sg_descr", config_file)
81 TARGETS_1 = ft_utils.get_parameter_from_yaml(
82 "testcases.testcase_1.targets1", config_file)
83 TARGETS_2 = ft_utils.get_parameter_from_yaml(
84 "testcases.testcase_1.targets2", config_file)
85 SUCCESS_CRITERIA = ft_utils.get_parameter_from_yaml(
86 "testcases.testcase_1.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(logger,
106 "There has been a problem when creating the neutron network")
108 return network_dic["net_id"]
111 def create_instance(nova_client,
119 logger.info("Creating instance '%s'..." % name)
121 "Configuration:\n name=%s \n flavor=%s \n image=%s \n "
122 "network=%s \n secgroup=%s \n hypervisor=%s \n userdata=%s\n"
123 % (name, flavor, image_id, network_id, sg_id, compute_node, userdata))
124 instance = os_utils.create_instance_and_wait_for_active(
131 av_zone=compute_node)
134 logger.error("Error while booting instance.")
136 # Retrieve IP of INSTANCE
137 # instance_ip = instance.networks.get(network_id)[0]
139 logger.debug("Adding '%s' to security group '%s'..."
140 % (name, SECGROUP_NAME))
141 os_utils.add_secgroup_to_instance(nova_client, instance.id, sg_id)
146 def generate_ping_userdata(ips_array):
149 ips = ("%s %s" % (ips, ip))
151 ips = ips.replace(' ', ' ')
152 return ("#!/bin/sh\n"
157 " ping -c 1 $ip 2>&1 >/dev/null\n"
159 " if [ \"Z$RES\" = \"Z0\" ] ; then\n"
160 " echo ping $ip OK\n"
161 " else echo ping $ip KO\n"
169 def get_ping_status(vm_source, ip_source,
170 vm_target, ip_target,
171 expected="PASS", timeout=30):
172 console_log = vm_source.get_console_output()
176 if "request failed" in console_log:
177 # Normally, cirros displays this message when userdata fails
178 logger.debug("It seems userdata is not supported in "
182 tab = ("%s" % (" " * 53))
183 expected_result = 'can ping' if expected == 'PASS' else 'cannot ping'
184 test_case_name = ("'%s' %s '%s'" %
185 (vm_source.name, expected_result, vm_target.name))
186 logger.debug("%sPing\n%sfrom '%s' (%s)\n%sto '%s' (%s).\n"
187 "%s-->Expected result: %s.\n"
188 % (tab, tab, vm_source.name, ip_source,
189 tab, vm_target.name, ip_target,
190 tab, expected_result))
192 console_log = vm_source.get_console_output()
193 # the console_log is a long string, we want to take
194 # the last 4 lines (for example)
195 lines = console_log.split('\n')
196 last_n_lines = lines[-5:]
197 if ("ping %s OK" % ip_target) in last_n_lines:
198 msg = ("'%s' can ping '%s'" % (vm_source.name, vm_target.name))
199 if expected == "PASS":
200 logger.debug("[PASS] %s" % msg)
201 add_to_summary(2, "PASS", test_case_name)
203 logger.debug("[FAIL] %s" % msg)
205 add_to_summary(2, "FAIL", test_case_name)
206 logger.debug("\n%s" % last_n_lines)
208 elif ("ping %s KO" % ip_target) in last_n_lines:
209 msg = ("'%s' cannot ping '%s'" %
210 (vm_source.name, vm_target.name))
211 if expected == "FAIL":
212 logger.debug("[PASS] %s" % msg)
213 add_to_summary(2, "PASS", test_case_name)
215 logger.debug("[FAIL] %s" % msg)
217 add_to_summary(2, "FAIL", test_case_name)
223 logger.debug("[FAIL] Timeout reached for '%s'. No ping output "
224 "captured in the console log" % vm_source.name)
225 add_to_summary(2, "FAIL", test_case_name)
229 def add_to_summary(num_cols, col1, col2=""):
230 global SUMMARY, LINE_LENGTH, DETAILS, NUM_TESTS, NUM_TESTS_FAILED
232 SUMMARY += ("+%s+\n" % (col1 * (LINE_LENGTH - 2)))
234 SUMMARY += ("| " + col1.ljust(LINE_LENGTH - 3) + "|\n")
236 SUMMARY += ("| %s" % col1.ljust(7) + "| ")
237 SUMMARY += (col2.ljust(LINE_LENGTH - 12) + "|\n")
238 if col1 in ("FAIL", "PASS"):
239 DETAILS.append({col2: col1})
242 NUM_TESTS_FAILED += 1
246 global TEST_RESULT, SUMMARY
248 add_to_summary(0, "=")
249 add_to_summary(2, "STATUS", "SUBTEST")
250 add_to_summary(0, "=")
252 nova_client = os_utils.get_nova_client()
253 neutron_client = os_utils.get_neutron_client()
254 glance_client = os_utils.get_glance_client()
256 image_id = os_utils.create_glance_image(glance_client,
263 network_1_id = create_network(neutron_client,
268 network_2_id = create_network(neutron_client,
273 sg_id = os_utils.create_security_group_full(logger, 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 network '%s' to the VPN." % NET_1_NAME)
363 add_to_summary(1, msg)
364 add_to_summary(0, "-")
366 os_utils.create_network_association(
367 neutron_client, bgpvpn_id, network_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__':