blob: 419b8c679cbcecc20c27b97dbf55ac516e7155a0 [file] [log] [blame]
Damjan Marionf56b77a2016-10-03 19:44:57 +02001#!/usr/bin/env python
2
Klement Sekera993e0ed2017-03-16 09:14:59 +01003import sys
Dave Wallacee2efd122017-09-30 22:04:21 -04004import shutil
Damjan Marionf56b77a2016-10-03 19:44:57 +02005import os
Klement Sekera909a6a12017-08-08 04:33:53 +02006import select
Damjan Marionf56b77a2016-10-03 19:44:57 +02007import unittest
Klement Sekera993e0ed2017-03-16 09:14:59 +01008import argparse
Klement Sekera545be522018-02-16 19:25:06 +01009import time
Klement Sekera909a6a12017-08-08 04:33:53 +020010from multiprocessing import Process, Pipe
Damjan Marionf56b77a2016-10-03 19:44:57 +020011from framework import VppTestRunner
Klement Sekera909a6a12017-08-08 04:33:53 +020012from debug import spawn_gdb
13from log import global_logger
Klement Sekerafcbf4442017-08-17 07:38:42 +020014from discover_tests import discover_tests
Klement Sekera993e0ed2017-03-16 09:14:59 +010015
Klement Sekera05742262018-03-14 18:14:49 +010016# timeout which controls how long the child has to finish after seeing
17# a core dump in test temporary directory. If this is exceeded, parent assumes
18# that child process is stuck (e.g. waiting for shm mutex, which will never
19# get unlocked) and kill the child
20core_timeout = 3
21
Klement Sekera909a6a12017-08-08 04:33:53 +020022
Klement Sekeradf2b9802017-10-05 10:26:03 +020023def test_runner_wrapper(suite, keep_alive_pipe, result_pipe, failed_pipe):
Klement Sekera909a6a12017-08-08 04:33:53 +020024 result = not VppTestRunner(
Klement Sekeradf2b9802017-10-05 10:26:03 +020025 keep_alive_pipe=keep_alive_pipe,
26 failed_pipe=failed_pipe,
Klement Sekera909a6a12017-08-08 04:33:53 +020027 verbosity=verbose,
28 failfast=failfast).run(suite).wasSuccessful()
29 result_pipe.send(result)
30 result_pipe.close()
31 keep_alive_pipe.close()
Klement Sekeradf2b9802017-10-05 10:26:03 +020032 failed_pipe.close()
Klement Sekera909a6a12017-08-08 04:33:53 +020033
34
Klement Sekerafcbf4442017-08-17 07:38:42 +020035class add_to_suite_callback:
36 def __init__(self, suite):
37 self.suite = suite
38
39 def __call__(self, file_name, cls, method):
40 suite.addTest(cls(method))
41
42
Klement Sekeradf2b9802017-10-05 10:26:03 +020043class Filter_by_class_list:
44 def __init__(self, class_list):
45 self.class_list = class_list
46
47 def __call__(self, file_name, class_name, func_name):
48 return class_name in self.class_list
49
50
51def suite_from_failed(suite, failed):
52 filter_cb = Filter_by_class_list(failed)
53 return VppTestRunner.filter_tests(suite, filter_cb)
54
55
Klement Sekera3f6ff192017-08-11 06:56:05 +020056def run_forked(suite):
Klement Sekera909a6a12017-08-08 04:33:53 +020057 keep_alive_parent_end, keep_alive_child_end = Pipe(duplex=False)
58 result_parent_end, result_child_end = Pipe(duplex=False)
Klement Sekeradf2b9802017-10-05 10:26:03 +020059 failed_parent_end, failed_child_end = Pipe(duplex=False)
Klement Sekera909a6a12017-08-08 04:33:53 +020060
Klement Sekera3f6ff192017-08-11 06:56:05 +020061 child = Process(target=test_runner_wrapper,
Klement Sekeradf2b9802017-10-05 10:26:03 +020062 args=(suite, keep_alive_child_end, result_child_end,
63 failed_child_end))
Klement Sekera3f6ff192017-08-11 06:56:05 +020064 child.start()
Klement Sekera909a6a12017-08-08 04:33:53 +020065 last_test_temp_dir = None
66 last_test_vpp_binary = None
67 last_test = None
68 result = None
Klement Sekeradf2b9802017-10-05 10:26:03 +020069 failed = set()
Klement Sekera545be522018-02-16 19:25:06 +010070 last_heard = time.time()
Klement Sekera05742262018-03-14 18:14:49 +010071 core_detected_at = None
72 debug_core = os.getenv("DEBUG", "").lower() == "core"
Klement Sekera545be522018-02-16 19:25:06 +010073 while True:
Klement Sekera909a6a12017-08-08 04:33:53 +020074 readable = select.select([keep_alive_parent_end.fileno(),
75 result_parent_end.fileno(),
Klement Sekeradf2b9802017-10-05 10:26:03 +020076 failed_parent_end.fileno(),
Klement Sekera909a6a12017-08-08 04:33:53 +020077 ],
Klement Sekera545be522018-02-16 19:25:06 +010078 [], [], 1)[0]
Klement Sekera909a6a12017-08-08 04:33:53 +020079 if result_parent_end.fileno() in readable:
80 result = result_parent_end.recv()
Klement Sekera545be522018-02-16 19:25:06 +010081 break
Klement Sekeradf2b9802017-10-05 10:26:03 +020082 if keep_alive_parent_end.fileno() in readable:
Klement Sekera909a6a12017-08-08 04:33:53 +020083 while keep_alive_parent_end.poll():
Dave Wallacee2efd122017-09-30 22:04:21 -040084 last_test, last_test_vpp_binary,\
85 last_test_temp_dir, vpp_pid = keep_alive_parent_end.recv()
Klement Sekera545be522018-02-16 19:25:06 +010086 last_heard = time.time()
Klement Sekeradf2b9802017-10-05 10:26:03 +020087 if failed_parent_end.fileno() in readable:
88 while failed_parent_end.poll():
89 failed_test = failed_parent_end.recv()
90 failed.add(failed_test.__name__)
Klement Sekera545be522018-02-16 19:25:06 +010091 last_heard = time.time()
92 fail = False
Klement Sekera05742262018-03-14 18:14:49 +010093 if last_heard + test_timeout < time.time() and \
94 not os.path.isfile("%s/_core_handled" % last_test_temp_dir):
Klement Sekera545be522018-02-16 19:25:06 +010095 fail = True
Klement Sekera909a6a12017-08-08 04:33:53 +020096 global_logger.critical("Timeout while waiting for child test "
97 "runner process (last test running was "
98 "`%s' in `%s')!" %
99 (last_test, last_test_temp_dir))
Klement Sekera545be522018-02-16 19:25:06 +0100100 elif not child.is_alive():
101 fail = True
102 global_logger.critical("Child process unexpectedly died (last "
103 "test running was `%s' in `%s')!" %
104 (last_test, last_test_temp_dir))
Klement Sekera05742262018-03-14 18:14:49 +0100105 elif last_test_temp_dir and last_test_vpp_binary:
106 core_path = "%s/core" % last_test_temp_dir
107 if os.path.isfile(core_path):
108 if core_detected_at is None:
109 core_detected_at = time.time()
110 elif core_detected_at + core_timeout < time.time():
111 if not os.path.isfile(
112 "%s/_core_handled" % last_test_temp_dir):
113 global_logger.critical(
114 "Child unresponsive and core-file exists in test "
115 "temporary directory!")
116 fail = True
117
Klement Sekera545be522018-02-16 19:25:06 +0100118 if fail:
Dave Wallace981fadf2017-09-30 15:12:19 -0400119 failed_dir = os.getenv('VPP_TEST_FAILED_DIR')
120 lttd = last_test_temp_dir.split("/")[-1]
121 link_path = '%s%s-FAILED' % (failed_dir, lttd)
122 global_logger.error("Creating a link to the failed " +
123 "test: %s -> %s" % (link_path, lttd))
Klement Sekera833e7612018-03-13 21:22:32 +0100124 try:
125 os.symlink(last_test_temp_dir, link_path)
126 except:
127 pass
Dave Wallacee2efd122017-09-30 22:04:21 -0400128 api_post_mortem_path = "/tmp/api_post_mortem.%d" % vpp_pid
129 if os.path.isfile(api_post_mortem_path):
130 global_logger.error("Copying api_post_mortem.%d to %s" %
131 (vpp_pid, last_test_temp_dir))
132 shutil.copy2(api_post_mortem_path, last_test_temp_dir)
Klement Sekera909a6a12017-08-08 04:33:53 +0200133 if last_test_temp_dir and last_test_vpp_binary:
134 core_path = "%s/core" % last_test_temp_dir
135 if os.path.isfile(core_path):
136 global_logger.error("Core-file exists in test temporary "
137 "directory: %s!" % core_path)
Klement Sekera05742262018-03-14 18:14:49 +0100138 if debug_core:
Klement Sekera3f6ff192017-08-11 06:56:05 +0200139 spawn_gdb(last_test_vpp_binary, core_path,
140 global_logger)
141 child.terminate()
Klement Sekera909a6a12017-08-08 04:33:53 +0200142 result = -1
Klement Sekera545be522018-02-16 19:25:06 +0100143 break
Klement Sekera909a6a12017-08-08 04:33:53 +0200144 keep_alive_parent_end.close()
145 result_parent_end.close()
Klement Sekeradf2b9802017-10-05 10:26:03 +0200146 failed_parent_end.close()
147 return result, failed
Klement Sekera3f6ff192017-08-11 06:56:05 +0200148
149
150if __name__ == '__main__':
151
152 try:
153 verbose = int(os.getenv("V", 0))
154 except:
155 verbose = 0
156
157 default_test_timeout = 600 # 10 minutes
158 try:
159 test_timeout = int(os.getenv("TIMEOUT", default_test_timeout))
160 except:
161 test_timeout = default_test_timeout
162
163 try:
164 debug = os.getenv("DEBUG")
165 except:
166 debug = None
167
168 parser = argparse.ArgumentParser(description="VPP unit tests")
169 parser.add_argument("-f", "--failfast", action='count',
170 help="fast failure flag")
171 parser.add_argument("-d", "--dir", action='append', type=str,
172 help="directory containing test files "
173 "(may be specified multiple times)")
174 args = parser.parse_args()
175 failfast = True if args.failfast == 1 else False
176
177 suite = unittest.TestSuite()
Klement Sekerafcbf4442017-08-17 07:38:42 +0200178 cb = add_to_suite_callback(suite)
Klement Sekera3f6ff192017-08-11 06:56:05 +0200179 for d in args.dir:
Klement Sekeradf2b9802017-10-05 10:26:03 +0200180 print("Adding tests from directory tree %s" % d)
Klement Sekerafcbf4442017-08-17 07:38:42 +0200181 discover_tests(d, cb)
Klement Sekera3f6ff192017-08-11 06:56:05 +0200182
Klement Sekeradf2b9802017-10-05 10:26:03 +0200183 try:
184 retries = int(os.getenv("RETRIES"))
185 except:
186 retries = 0
187 if retries is None:
188 retries = 0
189 attempts = retries + 1
190 if attempts > 1:
191 print("Perform %s attempts to pass the suite..." % attempts)
Klement Sekera3f6ff192017-08-11 06:56:05 +0200192 if debug is None or debug.lower() not in ["gdb", "gdbserver"]:
Klement Sekeradf2b9802017-10-05 10:26:03 +0200193 while True:
194 result, failed = run_forked(suite)
195 attempts = attempts - 1
196 print("%s test(s) failed, %s attempt(s) left" %
197 (len(failed), attempts))
198 if len(failed) > 0 and attempts > 0:
199 suite = suite_from_failed(suite, failed)
200 continue
201 sys.exit(result)
Klement Sekera3f6ff192017-08-11 06:56:05 +0200202
203 # don't fork if debugging..
204 sys.exit(not VppTestRunner(verbosity=verbose,
205 failfast=failfast).run(suite).wasSuccessful())