blob: 3476ef046681666990d4459d59e5d48787ea7431 [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 Sekera9b6ece72018-03-23 10:50:11 +010015from subprocess import check_output, CalledProcessError
Andrew Yourtchenko57612eb2018-03-28 15:32:10 +020016from util import check_core_path
Klement Sekera993e0ed2017-03-16 09:14:59 +010017
Klement Sekera05742262018-03-14 18:14:49 +010018# timeout which controls how long the child has to finish after seeing
19# a core dump in test temporary directory. If this is exceeded, parent assumes
20# that child process is stuck (e.g. waiting for shm mutex, which will never
21# get unlocked) and kill the child
22core_timeout = 3
23
Klement Sekera909a6a12017-08-08 04:33:53 +020024
Klement Sekeradf2b9802017-10-05 10:26:03 +020025def test_runner_wrapper(suite, keep_alive_pipe, result_pipe, failed_pipe):
Klement Sekera909a6a12017-08-08 04:33:53 +020026 result = not VppTestRunner(
Klement Sekeradf2b9802017-10-05 10:26:03 +020027 keep_alive_pipe=keep_alive_pipe,
28 failed_pipe=failed_pipe,
Klement Sekera909a6a12017-08-08 04:33:53 +020029 verbosity=verbose,
30 failfast=failfast).run(suite).wasSuccessful()
31 result_pipe.send(result)
32 result_pipe.close()
33 keep_alive_pipe.close()
Klement Sekeradf2b9802017-10-05 10:26:03 +020034 failed_pipe.close()
Klement Sekera909a6a12017-08-08 04:33:53 +020035
36
Klement Sekerafcbf4442017-08-17 07:38:42 +020037class add_to_suite_callback:
38 def __init__(self, suite):
39 self.suite = suite
40
41 def __call__(self, file_name, cls, method):
42 suite.addTest(cls(method))
43
44
Klement Sekeradf2b9802017-10-05 10:26:03 +020045class Filter_by_class_list:
46 def __init__(self, class_list):
47 self.class_list = class_list
48
49 def __call__(self, file_name, class_name, func_name):
50 return class_name in self.class_list
51
52
53def suite_from_failed(suite, failed):
54 filter_cb = Filter_by_class_list(failed)
55 return VppTestRunner.filter_tests(suite, filter_cb)
56
57
Klement Sekera3f6ff192017-08-11 06:56:05 +020058def run_forked(suite):
Klement Sekera909a6a12017-08-08 04:33:53 +020059 keep_alive_parent_end, keep_alive_child_end = Pipe(duplex=False)
60 result_parent_end, result_child_end = Pipe(duplex=False)
Klement Sekeradf2b9802017-10-05 10:26:03 +020061 failed_parent_end, failed_child_end = Pipe(duplex=False)
Klement Sekera909a6a12017-08-08 04:33:53 +020062
Klement Sekera3f6ff192017-08-11 06:56:05 +020063 child = Process(target=test_runner_wrapper,
Klement Sekeradf2b9802017-10-05 10:26:03 +020064 args=(suite, keep_alive_child_end, result_child_end,
65 failed_child_end))
Klement Sekera3f6ff192017-08-11 06:56:05 +020066 child.start()
Klement Sekera909a6a12017-08-08 04:33:53 +020067 last_test_temp_dir = None
68 last_test_vpp_binary = None
69 last_test = None
70 result = None
Klement Sekeradf2b9802017-10-05 10:26:03 +020071 failed = set()
Klement Sekera545be522018-02-16 19:25:06 +010072 last_heard = time.time()
Klement Sekera05742262018-03-14 18:14:49 +010073 core_detected_at = None
74 debug_core = os.getenv("DEBUG", "").lower() == "core"
Klement Sekera545be522018-02-16 19:25:06 +010075 while True:
Klement Sekera909a6a12017-08-08 04:33:53 +020076 readable = select.select([keep_alive_parent_end.fileno(),
77 result_parent_end.fileno(),
Klement Sekeradf2b9802017-10-05 10:26:03 +020078 failed_parent_end.fileno(),
Klement Sekera909a6a12017-08-08 04:33:53 +020079 ],
Klement Sekera545be522018-02-16 19:25:06 +010080 [], [], 1)[0]
Klement Sekera909a6a12017-08-08 04:33:53 +020081 if result_parent_end.fileno() in readable:
82 result = result_parent_end.recv()
Klement Sekera545be522018-02-16 19:25:06 +010083 break
Klement Sekeradf2b9802017-10-05 10:26:03 +020084 if keep_alive_parent_end.fileno() in readable:
Klement Sekera909a6a12017-08-08 04:33:53 +020085 while keep_alive_parent_end.poll():
Dave Wallacee2efd122017-09-30 22:04:21 -040086 last_test, last_test_vpp_binary,\
87 last_test_temp_dir, vpp_pid = keep_alive_parent_end.recv()
Klement Sekera545be522018-02-16 19:25:06 +010088 last_heard = time.time()
Klement Sekeradf2b9802017-10-05 10:26:03 +020089 if failed_parent_end.fileno() in readable:
90 while failed_parent_end.poll():
91 failed_test = failed_parent_end.recv()
92 failed.add(failed_test.__name__)
Klement Sekera545be522018-02-16 19:25:06 +010093 last_heard = time.time()
94 fail = False
Klement Sekera05742262018-03-14 18:14:49 +010095 if last_heard + test_timeout < time.time() and \
96 not os.path.isfile("%s/_core_handled" % last_test_temp_dir):
Klement Sekera545be522018-02-16 19:25:06 +010097 fail = True
Klement Sekera909a6a12017-08-08 04:33:53 +020098 global_logger.critical("Timeout while waiting for child test "
99 "runner process (last test running was "
100 "`%s' in `%s')!" %
101 (last_test, last_test_temp_dir))
Klement Sekera545be522018-02-16 19:25:06 +0100102 elif not child.is_alive():
103 fail = True
Klement Sekera9b6ece72018-03-23 10:50:11 +0100104 global_logger.critical("Child python process unexpectedly died "
105 "(last test running was `%s' in `%s')!" %
Klement Sekera545be522018-02-16 19:25:06 +0100106 (last_test, last_test_temp_dir))
Klement Sekera05742262018-03-14 18:14:49 +0100107 elif last_test_temp_dir and last_test_vpp_binary:
108 core_path = "%s/core" % last_test_temp_dir
109 if os.path.isfile(core_path):
110 if core_detected_at is None:
111 core_detected_at = time.time()
112 elif core_detected_at + core_timeout < time.time():
113 if not os.path.isfile(
114 "%s/_core_handled" % last_test_temp_dir):
115 global_logger.critical(
Klement Sekera9b6ece72018-03-23 10:50:11 +0100116 "Child python process unresponsive and core-file "
117 "exists in test temporary directory!")
Klement Sekera05742262018-03-14 18:14:49 +0100118 fail = True
119
Klement Sekera545be522018-02-16 19:25:06 +0100120 if fail:
Dave Wallace981fadf2017-09-30 15:12:19 -0400121 failed_dir = os.getenv('VPP_TEST_FAILED_DIR')
122 lttd = last_test_temp_dir.split("/")[-1]
123 link_path = '%s%s-FAILED' % (failed_dir, lttd)
124 global_logger.error("Creating a link to the failed " +
125 "test: %s -> %s" % (link_path, lttd))
Klement Sekera833e7612018-03-13 21:22:32 +0100126 try:
127 os.symlink(last_test_temp_dir, link_path)
Klement Sekera9b6ece72018-03-23 10:50:11 +0100128 except Exception:
Klement Sekera833e7612018-03-13 21:22:32 +0100129 pass
Dave Wallacee2efd122017-09-30 22:04:21 -0400130 api_post_mortem_path = "/tmp/api_post_mortem.%d" % vpp_pid
131 if os.path.isfile(api_post_mortem_path):
132 global_logger.error("Copying api_post_mortem.%d to %s" %
133 (vpp_pid, last_test_temp_dir))
134 shutil.copy2(api_post_mortem_path, last_test_temp_dir)
Klement Sekera909a6a12017-08-08 04:33:53 +0200135 if last_test_temp_dir and last_test_vpp_binary:
136 core_path = "%s/core" % last_test_temp_dir
137 if os.path.isfile(core_path):
138 global_logger.error("Core-file exists in test temporary "
139 "directory: %s!" % core_path)
Andrew Yourtchenko57612eb2018-03-28 15:32:10 +0200140 check_core_path(global_logger, core_path)
Klement Sekera9b6ece72018-03-23 10:50:11 +0100141 global_logger.debug("Running `file %s':" % core_path)
142 try:
143 info = check_output(["file", core_path])
144 global_logger.debug(info)
145 except CalledProcessError as e:
146 global_logger.error(
147 "Could not run `file' utility on core-file, "
148 "rc=%s" % e.returncode)
149 pass
Klement Sekera05742262018-03-14 18:14:49 +0100150 if debug_core:
Klement Sekera3f6ff192017-08-11 06:56:05 +0200151 spawn_gdb(last_test_vpp_binary, core_path,
152 global_logger)
153 child.terminate()
Klement Sekera909a6a12017-08-08 04:33:53 +0200154 result = -1
Klement Sekera545be522018-02-16 19:25:06 +0100155 break
Klement Sekera909a6a12017-08-08 04:33:53 +0200156 keep_alive_parent_end.close()
157 result_parent_end.close()
Klement Sekeradf2b9802017-10-05 10:26:03 +0200158 failed_parent_end.close()
159 return result, failed
Klement Sekera3f6ff192017-08-11 06:56:05 +0200160
161
162if __name__ == '__main__':
163
164 try:
165 verbose = int(os.getenv("V", 0))
Klement Sekera9b6ece72018-03-23 10:50:11 +0100166 except ValueError:
Klement Sekera3f6ff192017-08-11 06:56:05 +0200167 verbose = 0
168
169 default_test_timeout = 600 # 10 minutes
170 try:
171 test_timeout = int(os.getenv("TIMEOUT", default_test_timeout))
Klement Sekera9b6ece72018-03-23 10:50:11 +0100172 except ValueError:
Klement Sekera3f6ff192017-08-11 06:56:05 +0200173 test_timeout = default_test_timeout
174
Klement Sekera9b6ece72018-03-23 10:50:11 +0100175 debug = os.getenv("DEBUG")
Klement Sekera3f6ff192017-08-11 06:56:05 +0200176
Klement Sekera13a83ef2018-03-21 12:35:51 +0100177 s = os.getenv("STEP", "n")
178 step = True if s.lower() in ("y", "yes", "1") else False
179
Klement Sekera3f6ff192017-08-11 06:56:05 +0200180 parser = argparse.ArgumentParser(description="VPP unit tests")
181 parser.add_argument("-f", "--failfast", action='count',
182 help="fast failure flag")
183 parser.add_argument("-d", "--dir", action='append', type=str,
184 help="directory containing test files "
185 "(may be specified multiple times)")
186 args = parser.parse_args()
187 failfast = True if args.failfast == 1 else False
188
189 suite = unittest.TestSuite()
Klement Sekerafcbf4442017-08-17 07:38:42 +0200190 cb = add_to_suite_callback(suite)
Klement Sekera3f6ff192017-08-11 06:56:05 +0200191 for d in args.dir:
Klement Sekeradf2b9802017-10-05 10:26:03 +0200192 print("Adding tests from directory tree %s" % d)
Klement Sekerafcbf4442017-08-17 07:38:42 +0200193 discover_tests(d, cb)
Klement Sekera3f6ff192017-08-11 06:56:05 +0200194
Klement Sekeradf2b9802017-10-05 10:26:03 +0200195 try:
Klement Sekera9b6ece72018-03-23 10:50:11 +0100196 retries = int(os.getenv("RETRIES", 0))
197 except ValueError:
Klement Sekeradf2b9802017-10-05 10:26:03 +0200198 retries = 0
199 attempts = retries + 1
200 if attempts > 1:
201 print("Perform %s attempts to pass the suite..." % attempts)
Klement Sekera13a83ef2018-03-21 12:35:51 +0100202 if (debug is not None and debug.lower() in ["gdb", "gdbserver"]) or step:
203 # don't fork if requiring interactive terminal..
204 sys.exit(not VppTestRunner(
205 verbosity=verbose, failfast=failfast).run(suite).wasSuccessful())
206 else:
Klement Sekeradf2b9802017-10-05 10:26:03 +0200207 while True:
208 result, failed = run_forked(suite)
209 attempts = attempts - 1
210 print("%s test(s) failed, %s attempt(s) left" %
211 (len(failed), attempts))
212 if len(failed) > 0 and attempts > 0:
213 suite = suite_from_failed(suite, failed)
214 continue
215 sys.exit(result)