Create flavor for Quagga ubuntu VM
[sdnvpn.git] / sdnvpn / lib / results.py
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2017 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 import time
11
12 import functest.utils.functest_logger as ft_logger
13 import functest.utils.functest_utils as ft_utils
14
15 logger = ft_logger.Logger("sdnvpn-results").getLogger()
16
17
18 class Results(object):
19
20     def __init__(self, line_length):
21         self.line_length = line_length
22         self.test_result = "PASS"
23         self.summary = ""
24         self.details = []
25         self.num_tests = 0
26         self.num_tests_failed = 0
27
28     def get_ping_status(self,
29                         vm_source,
30                         vm_target,
31                         expected="PASS", timeout=30):
32         console_log = vm_source.get_console_output()
33
34         ip_source = vm_source.networks.itervalues().next()[0]
35         ip_target = vm_target.networks.itervalues().next()[0]
36
37         if "request failed" in console_log:
38             # Normally, cirros displays this message when userdata fails
39             logger.debug("It seems userdata is not supported in "
40                          "nova boot...")
41             return False
42         else:
43             tab = ("%s" % (" " * 53))
44             expected_result = 'can ping' if expected == 'PASS' \
45                               else 'cannot ping'
46             test_case_name = ("'%s' %s '%s'" %
47                               (vm_source.name,
48                                expected_result,
49                                vm_target.name))
50             logger.debug("%sPing\n%sfrom '%s' (%s)\n%sto '%s' (%s).\n"
51                          "%s-->Expected result: %s.\n"
52                          % (tab, tab, vm_source.name, ip_source,
53                             tab, vm_target.name, ip_target,
54                             tab, expected_result))
55             while True:
56                 console_log = vm_source.get_console_output()
57                 # the console_log is a long string, we want to take
58                 # the last 4 lines (for example)
59                 lines = console_log.split('\n')
60                 last_n_lines = lines[-5:]
61                 if ("ping %s OK" % ip_target) in last_n_lines:
62                     msg = ("'%s' can ping '%s'"
63                            % (vm_source.name, vm_target.name))
64                     if expected == "PASS":
65                         logger.debug("[PASS] %s" % msg)
66                         self.add_success(test_case_name)
67                     else:
68                         logger.debug("[FAIL] %s" % msg)
69                         self.add_failure(test_case_name)
70                         logger.debug("\n%s" % last_n_lines)
71                     break
72                 elif ("ping %s KO" % ip_target) in last_n_lines:
73                     msg = ("'%s' cannot ping '%s'" %
74                            (vm_source.name, vm_target.name))
75                     if expected == "FAIL":
76                         logger.debug("[PASS] %s" % msg)
77                         self.add_success(test_case_name)
78                     else:
79                         logger.debug("[FAIL] %s" % msg)
80                         self.add_failure(test_case_name)
81                     break
82                 time.sleep(1)
83                 timeout -= 1
84                 if timeout == 0:
85                     logger.debug("[FAIL] Timeout reached for '%s'. "
86                                  "No ping output captured in the console log"
87                                  % vm_source.name)
88                     self.add_failure(test_case_name)
89                     break
90
91     def add_to_summary(self, num_cols, col1, col2=""):
92         if num_cols == 0:
93             self.summary += ("+%s+\n" % (col1 * (self.line_length - 2)))
94         elif num_cols == 1:
95             self.summary += ("| " + col1.ljust(self.line_length - 3) + "|\n")
96         elif num_cols == 2:
97             self.summary += ("| %s" % col1.ljust(7) + "| ")
98             self.summary += (col2.ljust(self.line_length - 12) + "|\n")
99             if col1 in ("FAIL", "PASS"):
100                 self.details.append({col2: col1})
101                 self.num_tests += 1
102                 if col1 == "FAIL":
103                     self.num_tests_failed += 1
104
105     def record_action(self, msg):
106         """Record and log an action and display it in the summary."""
107         logger.info(msg)
108         self.add_to_summary(1, msg)
109
110     def add_failure(self, test):
111         self.add_to_summary(2, "FAIL", test)
112         self.test_result = "FAIL"
113
114     def add_success(self, test):
115         self.add_to_summary(2, "PASS", test)
116
117     def add_subtest(self, test, successful):
118         if successful:
119             self.add_success(test)
120         else:
121             self.add_failure(test)
122
123     def check_ssh_output(self, vm_source, vm_target,
124                          expected, timeout=30):
125         console_log = vm_source.get_console_output()
126         ip_source = vm_source.networks.itervalues().next()[0]
127         ip_target = vm_target.networks.itervalues().next()[0]
128
129         if "request failed" in console_log:
130             # Normally, cirros displays this message when userdata fails
131             logger.debug("It seems userdata is not supported in "
132                          "nova boot...")
133             return False
134         else:
135             tab = ("%s" % (" " * 53))
136             test_case_name = ("[%s] returns 'I am %s' to '%s'[%s]" %
137                               (ip_target, expected,
138                                vm_source.name, ip_source))
139             logger.debug("%sSSH\n%sfrom '%s' (%s)\n%sto '%s' (%s).\n"
140                          "%s-->Expected result: %s.\n"
141                          % (tab, tab, vm_source.name, ip_source,
142                             tab, vm_target.name, ip_target,
143                             tab, expected))
144             while True:
145                 console_log = vm_source.get_console_output()
146                 # the console_log is a long string, we want to take
147                 # the last 4 lines (for example)
148                 lines = console_log.split('\n')
149                 last_n_lines = lines[-5:]
150                 if ("%s %s" % (ip_target, expected)) in last_n_lines:
151                     logger.debug("[PASS] %s" % test_case_name)
152                     self.add_success(test_case_name)
153                     break
154                 elif ("%s not reachable" % ip_target) in last_n_lines:
155                     logger.debug("[FAIL] %s" % test_case_name)
156                     self.add_failure(test_case_name)
157                     break
158                 time.sleep(1)
159                 timeout -= 1
160                 if timeout == 0:
161                     logger.debug("[FAIL] Timeout reached for '%s'."
162                                  " No ping output captured in the console log"
163                                  % vm_source.name)
164                     self.add_failure(test_case_name)
165                     break
166
167     def ping_ip_test(self, address):
168         ping = "ping %s -c 3" % address
169         testcase_name = "Ping IP %s" % address
170         logger.debug(testcase_name)
171         exit_code = ft_utils.execute_command(ping)
172
173         if exit_code != 0:
174             self.add_failure(testcase_name)
175         else:
176             self.add_success(testcase_name)
177
178     def compile_summary(self):
179         success_message = "All the subtests have passed."
180         failure_message = "One or more subtests have failed."
181
182         self.add_to_summary(0, "=")
183         logger.info("\n%s" % self.summary)
184         if self.test_result == "PASS":
185             logger.info(success_message)
186         else:
187             logger.info(failure_message)
188
189         return {"status": self.test_result, "details": self.details}