blob: fd8ca1f50ab86c363b347cc9e0fb3ca11f3cd7d8 [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 Sekera909a6a12017-08-08 04:33:53 +020016
Klement Sekeradf2b9802017-10-05 10:26:03 +020017def test_runner_wrapper(suite, keep_alive_pipe, result_pipe, failed_pipe):
Klement Sekera909a6a12017-08-08 04:33:53 +020018 result = not VppTestRunner(
Klement Sekeradf2b9802017-10-05 10:26:03 +020019 keep_alive_pipe=keep_alive_pipe,
20 failed_pipe=failed_pipe,
Klement Sekera909a6a12017-08-08 04:33:53 +020021 verbosity=verbose,
22 failfast=failfast).run(suite).wasSuccessful()
23 result_pipe.send(result)
24 result_pipe.close()
25 keep_alive_pipe.close()
Klement Sekeradf2b9802017-10-05 10:26:03 +020026 failed_pipe.close()
Klement Sekera909a6a12017-08-08 04:33:53 +020027
28
Klement Sekerafcbf4442017-08-17 07:38:42 +020029class add_to_suite_callback:
30 def __init__(self, suite):
31 self.suite = suite
32
33 def __call__(self, file_name, cls, method):
34 suite.addTest(cls(method))
35
36
Klement Sekeradf2b9802017-10-05 10:26:03 +020037class Filter_by_class_list:
38 def __init__(self, class_list):
39 self.class_list = class_list
40
41 def __call__(self, file_name, class_name, func_name):
42 return class_name in self.class_list
43
44
45def suite_from_failed(suite, failed):
46 filter_cb = Filter_by_class_list(failed)
47 return VppTestRunner.filter_tests(suite, filter_cb)
48
49
Klement Sekera3f6ff192017-08-11 06:56:05 +020050def run_forked(suite):
Klement Sekera909a6a12017-08-08 04:33:53 +020051 keep_alive_parent_end, keep_alive_child_end = Pipe(duplex=False)
52 result_parent_end, result_child_end = Pipe(duplex=False)
Klement Sekeradf2b9802017-10-05 10:26:03 +020053 failed_parent_end, failed_child_end = Pipe(duplex=False)
Klement Sekera909a6a12017-08-08 04:33:53 +020054
Klement Sekera3f6ff192017-08-11 06:56:05 +020055 child = Process(target=test_runner_wrapper,
Klement Sekeradf2b9802017-10-05 10:26:03 +020056 args=(suite, keep_alive_child_end, result_child_end,
57 failed_child_end))
Klement Sekera3f6ff192017-08-11 06:56:05 +020058 child.start()
Klement Sekera909a6a12017-08-08 04:33:53 +020059 last_test_temp_dir = None
60 last_test_vpp_binary = None
61 last_test = None
62 result = None
Klement Sekeradf2b9802017-10-05 10:26:03 +020063 failed = set()
Klement Sekera545be522018-02-16 19:25:06 +010064 last_heard = time.time()
65 while True:
Klement Sekera909a6a12017-08-08 04:33:53 +020066 readable = select.select([keep_alive_parent_end.fileno(),
67 result_parent_end.fileno(),
Klement Sekeradf2b9802017-10-05 10:26:03 +020068 failed_parent_end.fileno(),
Klement Sekera909a6a12017-08-08 04:33:53 +020069 ],
Klement Sekera545be522018-02-16 19:25:06 +010070 [], [], 1)[0]
Klement Sekera909a6a12017-08-08 04:33:53 +020071 if result_parent_end.fileno() in readable:
72 result = result_parent_end.recv()
Klement Sekera545be522018-02-16 19:25:06 +010073 break
Klement Sekeradf2b9802017-10-05 10:26:03 +020074 if keep_alive_parent_end.fileno() in readable:
Klement Sekera909a6a12017-08-08 04:33:53 +020075 while keep_alive_parent_end.poll():
Dave Wallacee2efd122017-09-30 22:04:21 -040076 last_test, last_test_vpp_binary,\
77 last_test_temp_dir, vpp_pid = keep_alive_parent_end.recv()
Klement Sekera545be522018-02-16 19:25:06 +010078 last_heard = time.time()
Klement Sekeradf2b9802017-10-05 10:26:03 +020079 if failed_parent_end.fileno() in readable:
80 while failed_parent_end.poll():
81 failed_test = failed_parent_end.recv()
82 failed.add(failed_test.__name__)
Klement Sekera545be522018-02-16 19:25:06 +010083 last_heard = time.time()
84 fail = False
85 if last_heard + test_timeout < time.time():
86 fail = True
Klement Sekera909a6a12017-08-08 04:33:53 +020087 global_logger.critical("Timeout while waiting for child test "
88 "runner process (last test running was "
89 "`%s' in `%s')!" %
90 (last_test, last_test_temp_dir))
Klement Sekera545be522018-02-16 19:25:06 +010091 elif not child.is_alive():
92 fail = True
93 global_logger.critical("Child process unexpectedly died (last "
94 "test running was `%s' in `%s')!" %
95 (last_test, last_test_temp_dir))
96 if fail:
Dave Wallace981fadf2017-09-30 15:12:19 -040097 failed_dir = os.getenv('VPP_TEST_FAILED_DIR')
98 lttd = last_test_temp_dir.split("/")[-1]
99 link_path = '%s%s-FAILED' % (failed_dir, lttd)
100 global_logger.error("Creating a link to the failed " +
101 "test: %s -> %s" % (link_path, lttd))
102 os.symlink(last_test_temp_dir, link_path)
Dave Wallacee2efd122017-09-30 22:04:21 -0400103 api_post_mortem_path = "/tmp/api_post_mortem.%d" % vpp_pid
104 if os.path.isfile(api_post_mortem_path):
105 global_logger.error("Copying api_post_mortem.%d to %s" %
106 (vpp_pid, last_test_temp_dir))
107 shutil.copy2(api_post_mortem_path, last_test_temp_dir)
Klement Sekera909a6a12017-08-08 04:33:53 +0200108 if last_test_temp_dir and last_test_vpp_binary:
109 core_path = "%s/core" % last_test_temp_dir
110 if os.path.isfile(core_path):
111 global_logger.error("Core-file exists in test temporary "
112 "directory: %s!" % core_path)
Klement Sekera3f6ff192017-08-11 06:56:05 +0200113 if d and d.lower() == "core":
114 spawn_gdb(last_test_vpp_binary, core_path,
115 global_logger)
116 child.terminate()
Klement Sekera909a6a12017-08-08 04:33:53 +0200117 result = -1
Klement Sekera545be522018-02-16 19:25:06 +0100118 break
Klement Sekera909a6a12017-08-08 04:33:53 +0200119 keep_alive_parent_end.close()
120 result_parent_end.close()
Klement Sekeradf2b9802017-10-05 10:26:03 +0200121 failed_parent_end.close()
122 return result, failed
Klement Sekera3f6ff192017-08-11 06:56:05 +0200123
124
125if __name__ == '__main__':
126
127 try:
128 verbose = int(os.getenv("V", 0))
129 except:
130 verbose = 0
131
132 default_test_timeout = 600 # 10 minutes
133 try:
134 test_timeout = int(os.getenv("TIMEOUT", default_test_timeout))
135 except:
136 test_timeout = default_test_timeout
137
138 try:
139 debug = os.getenv("DEBUG")
140 except:
141 debug = None
142
143 parser = argparse.ArgumentParser(description="VPP unit tests")
144 parser.add_argument("-f", "--failfast", action='count',
145 help="fast failure flag")
146 parser.add_argument("-d", "--dir", action='append', type=str,
147 help="directory containing test files "
148 "(may be specified multiple times)")
149 args = parser.parse_args()
150 failfast = True if args.failfast == 1 else False
151
152 suite = unittest.TestSuite()
Klement Sekerafcbf4442017-08-17 07:38:42 +0200153 cb = add_to_suite_callback(suite)
Klement Sekera3f6ff192017-08-11 06:56:05 +0200154 for d in args.dir:
Klement Sekeradf2b9802017-10-05 10:26:03 +0200155 print("Adding tests from directory tree %s" % d)
Klement Sekerafcbf4442017-08-17 07:38:42 +0200156 discover_tests(d, cb)
Klement Sekera3f6ff192017-08-11 06:56:05 +0200157
Klement Sekeradf2b9802017-10-05 10:26:03 +0200158 try:
159 retries = int(os.getenv("RETRIES"))
160 except:
161 retries = 0
162 if retries is None:
163 retries = 0
164 attempts = retries + 1
165 if attempts > 1:
166 print("Perform %s attempts to pass the suite..." % attempts)
Klement Sekera3f6ff192017-08-11 06:56:05 +0200167 if debug is None or debug.lower() not in ["gdb", "gdbserver"]:
Klement Sekeradf2b9802017-10-05 10:26:03 +0200168 while True:
169 result, failed = run_forked(suite)
170 attempts = attempts - 1
171 print("%s test(s) failed, %s attempt(s) left" %
172 (len(failed), attempts))
173 if len(failed) > 0 and attempts > 0:
174 suite = suite_from_failed(suite, failed)
175 continue
176 sys.exit(result)
Klement Sekera3f6ff192017-08-11 06:56:05 +0200177
178 # don't fork if debugging..
179 sys.exit(not VppTestRunner(verbosity=verbose,
180 failfast=failfast).run(suite).wasSuccessful())