3 # Copyright (c) 2016 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.test_result = "FAIL"
70 self.add_failure(test_case_name)
71 logger.debug("\n%s" % last_n_lines)
73 elif ("ping %s KO" % ip_target) in last_n_lines:
74 msg = ("'%s' cannot ping '%s'" %
75 (vm_source.name, vm_target.name))
76 if expected == "FAIL":
77 logger.debug("[PASS] %s" % msg)
78 self.add_success(test_case_name)
80 logger.debug("[FAIL] %s" % msg)
81 self.test_result = "FAIL"
82 self.add_failure(test_case_name)
87 self.test_result = "FAIL"
88 logger.debug("[FAIL] Timeout reached for '%s'. "
89 "No ping output captured in the console log"
91 self.add_failure(test_case_name)
94 def add_to_summary(self, num_cols, col1, col2=""):
96 self.summary += ("+%s+\n" % (col1 * (self.line_length - 2)))
98 self.summary += ("| " + col1.ljust(self.line_length - 3) + "|\n")
100 self.summary += ("| %s" % col1.ljust(7) + "| ")
101 self.summary += (col2.ljust(self.line_length - 12) + "|\n")
102 if col1 in ("FAIL", "PASS"):
103 self.details.append({col2: col1})
106 self.num_tests_failed += 1
108 def record_action(self, msg):
109 """Record and log an action and display it in the summary."""
111 self.add_to_summary(1, msg)
113 def add_failure(self, test):
114 self.add_to_summary(2, "FAIL", test)
116 def add_success(self, test):
117 self.add_to_summary(2, "PASS", test)
119 def check_ssh_output(self, vm_source, vm_target,
120 expected, timeout=30):
121 console_log = vm_source.get_console_output()
122 ip_source = vm_source.networks.itervalues().next()[0]
123 ip_target = vm_target.networks.itervalues().next()[0]
125 if "request failed" in console_log:
126 # Normally, cirros displays this message when userdata fails
127 logger.debug("It seems userdata is not supported in "
131 tab = ("%s" % (" " * 53))
132 test_case_name = ("[%s] returns 'I am %s' to '%s'[%s]" %
133 (ip_target, expected,
134 vm_source.name, ip_source))
135 logger.debug("%sSSH\n%sfrom '%s' (%s)\n%sto '%s' (%s).\n"
136 "%s-->Expected result: %s.\n"
137 % (tab, tab, vm_source.name, ip_source,
138 tab, vm_target.name, ip_target,
141 console_log = vm_source.get_console_output()
142 # the console_log is a long string, we want to take
143 # the last 4 lines (for example)
144 lines = console_log.split('\n')
145 last_n_lines = lines[-5:]
146 if ("%s %s" % (ip_target, expected)) in last_n_lines:
147 logger.debug("[PASS] %s" % test_case_name)
148 self.add_success(test_case_name)
150 elif ("%s not reachable" % ip_target) in last_n_lines:
151 logger.debug("[FAIL] %s" % test_case_name)
152 self.add_failure(test_case_name)
153 self.test_result = "FAIL"
158 self.test_result = "FAIL"
159 logger.debug("[FAIL] Timeout reached for '%s'."
160 " No ping output captured in the console log"
162 self.add_failure(test_case_name)
165 def ping_ip_test(self, address):
166 ping = "ping %s -c 3" % address
167 testcase_name = "Ping IP %s" % address
168 exit_code = ft_utils.execute_command(ping)
171 self.add_failure(testcase_name)
173 self.add_success(testcase_name)
175 def compile_summary(self, SUCCESS_CRITERIA):
176 success_message = "All the subtests have passed."
177 failure_message = "One or more subtests have failed."
179 self.add_to_summary(0, "=")
180 logger.info("\n%s" % self.summary)
181 if self.test_result == "PASS":
182 logger.info(success_message)
184 logger.info(failure_message)
188 (100 * int(self.num_tests_failed) / int(self.num_tests))
189 if success < int(SUCCESS_CRITERIA):
192 return {"status": status, "details": self.details}