Report overall status of the test results to DB
[sdnvpn.git] / test / functest / testcase_1.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-1").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_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
58
59 # NEUTRON Private Network parameters
60
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")
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(logger,
99                                                neutron_client,
100                                                net,
101                                                subnet,
102                                                router,
103                                                cidr)
104     if not network_dic:
105         logger.error(
106             "There has been a problem when creating the neutron network")
107         sys.exit(-1)
108     return network_dic["net_id"]
109
110
111 def create_instance(nova_client,
112                     name,
113                     flavor,
114                     image_id,
115                     network_id,
116                     sg_id,
117                     compute_node='',
118                     userdata=None):
119     logger.info("Creating instance '%s'..." % name)
120     logger.debug(
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(
125         flavor,
126         image_id,
127         network_id,
128         name,
129         config_drive=True,
130         userdata=userdata,
131         av_zone=compute_node)
132
133     if instance is None:
134         logger.error("Error while booting instance.")
135         sys.exit(-1)
136     # Retrieve IP of INSTANCE
137     # instance_ip = instance.networks.get(network_id)[0]
138
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)
142
143     return instance
144
145
146 def generate_ping_userdata(ips_array):
147     ips = ""
148     for ip in ips_array:
149         ips = ("%s %s" % (ips, ip))
150
151     ips = ips.replace('  ', ' ')
152     return ("#!/bin/sh\n"
153             "set%s\n"
154             "while true; do\n"
155             " for i do\n"
156             "  ip=$i\n"
157             "  ping -c 1 $ip 2>&1 >/dev/null\n"
158             "  RES=$?\n"
159             "  if [ \"Z$RES\" = \"Z0\" ] ; then\n"
160             "   echo ping $ip OK\n"
161             "  else echo ping $ip KO\n"
162             "  fi\n"
163             " done\n"
164             " sleep 1\n"
165             "done\n"
166             % ips)
167
168
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()
173
174     global TEST_RESULT
175
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 "
179                      "nova boot...")
180         return False
181     else:
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))
191         while True:
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)
202                 else:
203                     logger.debug("[FAIL] %s" % msg)
204                     TEST_RESULT = "FAIL"
205                     add_to_summary(2, "FAIL", test_case_name)
206                     logger.debug("\n%s" % last_n_lines)
207                 break
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)
214                 else:
215                     logger.debug("[FAIL] %s" % msg)
216                     TEST_RESULT = "FAIL"
217                     add_to_summary(2, "FAIL", test_case_name)
218                 break
219             time.sleep(1)
220             timeout -= 1
221             if timeout == 0:
222                 TEST_RESULT = "FAIL"
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)
226                 break
227
228
229 def add_to_summary(num_cols, col1, col2=""):
230     global SUMMARY, LINE_LENGTH, DETAILS, NUM_TESTS, NUM_TESTS_FAILED
231     if num_cols == 0:
232         SUMMARY += ("+%s+\n" % (col1 * (LINE_LENGTH - 2)))
233     elif num_cols == 1:
234         SUMMARY += ("| " + col1.ljust(LINE_LENGTH - 3) + "|\n")
235     elif num_cols == 2:
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})
240             NUM_TESTS += 1
241             if col1 == "FAIL":
242                 NUM_TESTS_FAILED += 1
243
244
245 def main():
246     global TEST_RESULT, SUMMARY
247
248     add_to_summary(0, "=")
249     add_to_summary(2, "STATUS", "SUBTEST")
250     add_to_summary(0, "=")
251
252     nova_client = os_utils.get_nova_client()
253     neutron_client = os_utils.get_neutron_client()
254     glance_client = os_utils.get_glance_client()
255
256     image_id = os_utils.create_glance_image(glance_client,
257                                             IMAGE_NAME,
258                                             IMAGE_PATH,
259                                             disk=IMAGE_FORMAT,
260                                             container="bare",
261                                             public=True,
262                                             logger=logger)
263     network_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 = 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(logger, 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 network '%s' to the VPN." % NET_1_NAME)
362     logger.info(msg)
363     add_to_summary(1, msg)
364     add_to_summary(0, "-")
365
366     os_utils.create_network_association(
367         neutron_client, bgpvpn_id, network_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()