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
8 # http://www.apache.org/licenses/LICENSE-2.0
12 import functest.utils.functest_logger as ft_logger
13 import functest.utils.functest_utils as ft_utils
15 logger = ft_logger.Logger("sdnvpn-results").getLogger()
18 class Results(object):
20 def __init__(self, line_length):
21 self.line_length = line_length
22 self.test_result = "PASS"
26 self.num_tests_failed = 0
28 def get_ping_status(self,
31 expected="PASS", timeout=30):
32 console_log = vm_source.get_console_output()
34 ip_source = vm_source.networks.itervalues().next()[0]
35 ip_target = vm_target.networks.itervalues().next()[0]
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 "
43 tab = ("%s" % (" " * 53))
44 expected_result = 'can ping' if expected == 'PASS' \
46 test_case_name = ("'%s' %s '%s'" %
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))
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)
68 logger.debug("[FAIL] %s" % msg)
69 self.add_failure(test_case_name)
70 logger.debug("\n%s" % last_n_lines)
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)
79 logger.debug("[FAIL] %s" % msg)
80 self.add_failure(test_case_name)
85 logger.debug("[FAIL] Timeout reached for '%s'. "
86 "No ping output captured in the console log"
88 self.add_failure(test_case_name)
91 def add_to_summary(self, num_cols, col1, col2=""):
93 self.summary += ("+%s+\n" % (col1 * (self.line_length - 2)))
95 self.summary += ("| " + col1.ljust(self.line_length - 3) + "|\n")
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})
103 self.num_tests_failed += 1
105 def record_action(self, msg):
106 """Record and log an action and display it in the summary."""
108 self.add_to_summary(1, msg)
110 def add_failure(self, test):
111 self.add_to_summary(2, "FAIL", test)
112 self.test_result = "FAIL"
114 def add_success(self, test):
115 self.add_to_summary(2, "PASS", test)
117 def check_ssh_output(self, vm_source, vm_target,
118 expected, timeout=30):
119 console_log = vm_source.get_console_output()
120 ip_source = vm_source.networks.itervalues().next()[0]
121 ip_target = vm_target.networks.itervalues().next()[0]
123 if "request failed" in console_log:
124 # Normally, cirros displays this message when userdata fails
125 logger.debug("It seems userdata is not supported in "
129 tab = ("%s" % (" " * 53))
130 test_case_name = ("[%s] returns 'I am %s' to '%s'[%s]" %
131 (ip_target, expected,
132 vm_source.name, ip_source))
133 logger.debug("%sSSH\n%sfrom '%s' (%s)\n%sto '%s' (%s).\n"
134 "%s-->Expected result: %s.\n"
135 % (tab, tab, vm_source.name, ip_source,
136 tab, vm_target.name, ip_target,
139 console_log = vm_source.get_console_output()
140 # the console_log is a long string, we want to take
141 # the last 4 lines (for example)
142 lines = console_log.split('\n')
143 last_n_lines = lines[-5:]
144 if ("%s %s" % (ip_target, expected)) in last_n_lines:
145 logger.debug("[PASS] %s" % test_case_name)
146 self.add_success(test_case_name)
148 elif ("%s not reachable" % ip_target) in last_n_lines:
149 logger.debug("[FAIL] %s" % test_case_name)
150 self.add_failure(test_case_name)
155 logger.debug("[FAIL] Timeout reached for '%s'."
156 " No ping output captured in the console log"
158 self.add_failure(test_case_name)
161 def ping_ip_test(self, address):
162 ping = "ping %s -c 3" % address
163 testcase_name = "Ping IP %s" % address
164 logger.debug(testcase_name)
165 exit_code = ft_utils.execute_command(ping)
168 self.add_failure(testcase_name)
170 self.add_success(testcase_name)
172 def compile_summary(self):
173 success_message = "All the subtests have passed."
174 failure_message = "One or more subtests have failed."
176 self.add_to_summary(0, "=")
177 logger.info("\n%s" % self.summary)
178 if self.test_result == "PASS":
179 logger.info(success_message)
181 logger.info(failure_message)
183 return {"status": self.test_result, "details": self.details}