Adapt some calls to functest_utils that don't require logger
[sdnvpn.git] / test / functest / testcase_4.py
1 #!/usr/bin/python
2 #
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
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10
11 import argparse
12 import os
13 from random import randint
14 import sys
15 import time
16
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
20
21
22 parser = argparse.ArgumentParser()
23
24 parser.add_argument("-r", "--report",
25                     help="Create json result file",
26                     action="store_true")
27
28 args = parser.parse_args()
29
30 logger = ft_logger.Logger("sdnvpn-testcase-4").getLogger()
31
32 REPO_PATH = os.environ['repos_dir'] + '/sdnvpn/'
33
34 VM_BOOT_TIMEOUT = 180
35
36 config_file = REPO_PATH + 'test/functest/config.yaml'
37
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
58
59 # NEUTRON Private Network parameters
60
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")
88
89 TEST_RESULT = "PASS"
90 SUMMARY = ""
91 LINE_LENGTH = 60  # length for the summary table
92 DETAILS = []
93 NUM_TESTS = 0
94 NUM_TESTS_FAILED = 0
95
96
97 def create_network(neutron_client, net, subnet, router, cidr):
98     network_dic = os_utils.create_network_full(neutron_client,
99                                                net,
100                                                subnet,
101                                                router,
102                                                cidr)
103     if not network_dic:
104         logger.error(
105             "There has been a problem when creating the neutron network")
106         sys.exit(-1)
107     return network_dic["net_id"], \
108         network_dic["subnet_id"], \
109         network_dic["router_id"]
110
111
112 def create_instance(nova_client,
113                     name,
114                     flavor,
115                     image_id,
116                     network_id,
117                     sg_id,
118                     compute_node='',
119                     userdata=None):
120     logger.info("Creating instance '%s'..." % name)
121     logger.debug(
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(
126         flavor,
127         image_id,
128         network_id,
129         name,
130         config_drive=True,
131         userdata=userdata,
132         av_zone=compute_node)
133
134     if instance is None:
135         logger.error("Error while booting instance.")
136         sys.exit(-1)
137     # Retrieve IP of INSTANCE
138     # instance_ip = instance.networks.get(network_id)[0]
139
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)
143
144     return instance
145
146
147 def generate_ping_userdata(ips_array):
148     ips = ""
149     for ip in ips_array:
150         ips = ("%s %s" % (ips, ip))
151
152     ips = ips.replace('  ', ' ')
153     return ("#!/bin/sh\n"
154             "set%s\n"
155             "while true; do\n"
156             " for i do\n"
157             "  ip=$i\n"
158             "  ping -c 1 $ip 2>&1 >/dev/null\n"
159             "  RES=$?\n"
160             "  if [ \"Z$RES\" = \"Z0\" ] ; then\n"
161             "   echo ping $ip OK\n"
162             "  else echo ping $ip KO\n"
163             "  fi\n"
164             " done\n"
165             " sleep 1\n"
166             "done\n"
167             % ips)
168
169
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()
174
175     global TEST_RESULT
176
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 "
180                      "nova boot...")
181         return False
182     else:
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))
192         while True:
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)
203                 else:
204                     logger.debug("[FAIL] %s" % msg)
205                     TEST_RESULT = "FAIL"
206                     add_to_summary(2, "FAIL", test_case_name)
207                     logger.debug("\n%s" % last_n_lines)
208                 break
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)
215                 else:
216                     logger.debug("[FAIL] %s" % msg)
217                     TEST_RESULT = "FAIL"
218                     add_to_summary(2, "FAIL", test_case_name)
219                 break
220             time.sleep(1)
221             timeout -= 1
222             if timeout == 0:
223                 TEST_RESULT = "FAIL"
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)
227                 break
228
229
230 def add_to_summary(num_cols, col1, col2=""):
231     global SUMMARY, LINE_LENGTH, DETAILS, NUM_TESTS, NUM_TESTS_FAILED
232     if num_cols == 0:
233         SUMMARY += ("+%s+\n" % (col1 * (LINE_LENGTH - 2)))
234     elif num_cols == 1:
235         SUMMARY += ("| " + col1.ljust(LINE_LENGTH - 3) + "|\n")
236     elif num_cols == 2:
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})
241             NUM_TESTS += 1
242             if col1 == "FAIL":
243                 NUM_TESTS_FAILED += 1
244
245
246 def main():
247     global TEST_RESULT, SUMMARY
248
249     add_to_summary(0, "=")
250     add_to_summary(2, "STATUS", "SUBTEST")
251     add_to_summary(0, "=")
252
253     nova_client = os_utils.get_nova_client()
254     neutron_client = os_utils.get_neutron_client()
255     glance_client = os_utils.get_glance_client()
256
257     image_id = os_utils.create_glance_image(glance_client,
258                                             IMAGE_NAME,
259                                             IMAGE_PATH,
260                                             disk=IMAGE_FORMAT,
261                                             container="bare",
262                                             public=True)
263     network_1_id, _, router_1_id = create_network(neutron_client,
264                                                   NET_1_NAME,
265                                                   SUBNET_1_NAME,
266                                                   ROUTER_1_NAME,
267                                                   SUBNET_1_CIDR)
268     network_2_id, _, router_2_id = create_network(neutron_client,
269                                                   NET_2_NAME,
270                                                   SUBNET_2_NAME,
271                                                   ROUTER_2_NAME,
272                                                   SUBNET_2_CIDR)
273     sg_id = os_utils.create_security_group_full(neutron_client,
274                                                 SECGROUP_NAME, SECGROUP_DESCR)
275
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."
282                      % num_compute_nodes)
283         sys.exit(-1)
284
285     logger.debug("Compute nodes: %s" % compute_nodes)
286     av_zone_1 = "nova:" + compute_nodes[0]
287     av_zone_2 = "nova:" + compute_nodes[1]
288
289     # boot INTANCES
290     vm_2 = create_instance(nova_client,
291                            INSTANCE_2_NAME,
292                            FLAVOR,
293                            image_id,
294                            network_1_id,
295                            sg_id,
296                            av_zone_1)
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))
300
301     vm_3 = create_instance(nova_client,
302                            INSTANCE_3_NAME,
303                            FLAVOR, image_id,
304                            network_1_id,
305                            sg_id,
306                            av_zone_2)
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))
310
311     vm_5 = create_instance(nova_client,
312                            INSTANCE_5_NAME,
313                            FLAVOR,
314                            image_id,
315                            network_2_id,
316                            sg_id,
317                            av_zone_2)
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))
321
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,
325                            INSTANCE_4_NAME,
326                            FLAVOR,
327                            image_id,
328                            network_2_id,
329                            sg_id,
330                            av_zone_1,
331                            userdata=u4)
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))
335
336     # We boot VM1 at the end because we need to get the IPs first to generate
337     # the userdata
338     u1 = generate_ping_userdata([vm_2_ip, vm_3_ip, vm_4_ip, vm_5_ip])
339     vm_1 = create_instance(nova_client,
340                            INSTANCE_1_NAME,
341                            FLAVOR,
342                            image_id,
343                            network_1_id,
344                            sg_id,
345                            av_zone_1,
346                            userdata=u1)
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")
351     logger.info(msg)
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,
356               "name": vpn_name}
357     bgpvpn = os_utils.create_bgpvpn(neutron_client, **kwargs)
358     bgpvpn_id = bgpvpn['bgpvpn']['id']
359     logger.debug("VPN created details: %s" % bgpvpn)
360
361     msg = ("Associate router '%s' to the VPN." % ROUTER_1_NAME)
362     logger.info(msg)
363     add_to_summary(1, msg)
364     add_to_summary(0, "-")
365
366     os_utils.create_router_association(
367         neutron_client, bgpvpn_id, router_1_id)
368
369     # Wait for VMs to get ips.
370     time.sleep(80)
371
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)
378
379     msg = ("Associate network '%s' to the VPN." % NET_2_NAME)
380     logger.info(msg)
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)
386
387     # Wait a bit for this to take effect
388     time.sleep(30)
389
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)
396
397     msg = ("Update VPN with eRT=iRT ...")
398     logger.info(msg)
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,
404               "name": vpn_name}
405     bgpvpn = os_utils.update_bgpvpn(neutron_client, bgpvpn_id, **kwargs)
406     # Wait a bit for this to take effect
407     time.sleep(30)
408
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)
413
414     add_to_summary(0, "=")
415     logger.info("\n%s" % SUMMARY)
416
417     if TEST_RESULT == "PASS":
418         logger.info("All the ping tests have passed as expected.")
419     else:
420         logger.info("One or more ping tests have failed.")
421
422     status = "PASS"
423     success = 100 - (100 * int(NUM_TESTS_FAILED) / int(NUM_TESTS))
424     if success < int(SUCCESS_CRITERIA):
425         status = "FAILED"
426
427     return {"status": status, "details": DETAILS}
428
429
430 if __name__ == '__main__':
431     main()