blob: df6bf6cbaf4ec95ce7a09b68b325f06648e7dda3 [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 Sekera909a6a12017-08-08 04:33:53 +02009from multiprocessing import Process, Pipe
Damjan Marionf56b77a2016-10-03 19:44:57 +020010from framework import VppTestRunner
Klement Sekera909a6a12017-08-08 04:33:53 +020011from debug import spawn_gdb
12from log import global_logger
Klement Sekerafcbf4442017-08-17 07:38:42 +020013from discover_tests import discover_tests
Klement Sekera993e0ed2017-03-16 09:14:59 +010014
Klement Sekera909a6a12017-08-08 04:33:53 +020015
Klement Sekeradf2b9802017-10-05 10:26:03 +020016def test_runner_wrapper(suite, keep_alive_pipe, result_pipe, failed_pipe):
Klement Sekera909a6a12017-08-08 04:33:53 +020017 result = not VppTestRunner(
Klement Sekeradf2b9802017-10-05 10:26:03 +020018 keep_alive_pipe=keep_alive_pipe,
19 failed_pipe=failed_pipe,
Klement Sekera909a6a12017-08-08 04:33:53 +020020 verbosity=verbose,
21 failfast=failfast).run(suite).wasSuccessful()
22 result_pipe.send(result)
23 result_pipe.close()
24 keep_alive_pipe.close()
Klement Sekeradf2b9802017-10-05 10:26:03 +020025 failed_pipe.close()
Klement Sekera909a6a12017-08-08 04:33:53 +020026
27
Klement Sekerafcbf4442017-08-17 07:38:42 +020028class add_to_suite_callback:
29 def __init__(self, suite):
30 self.suite = suite
31
32 def __call__(self, file_name, cls, method):
33 suite.addTest(cls(method))
34
35
Klement Sekeradf2b9802017-10-05 10:26:03 +020036class Filter_by_class_list:
37 def __init__(self, class_list):
38 self.class_list = class_list
39
40 def __call__(self, file_name, class_name, func_name):
41 return class_name in self.class_list
42
43
44def suite_from_failed(suite, failed):
45 filter_cb = Filter_by_class_list(failed)
46 return VppTestRunner.filter_tests(suite, filter_cb)
47
48
Klement Sekera3f6ff192017-08-11 06:56:05 +020049def run_forked(suite):
Klement Sekera909a6a12017-08-08 04:33:53 +020050 keep_alive_parent_end, keep_alive_child_end = Pipe(duplex=False)
51 result_parent_end, result_child_end = Pipe(duplex=False)
Klement Sekeradf2b9802017-10-05 10:26:03 +020052 failed_parent_end, failed_child_end = Pipe(duplex=False)
Klement Sekera909a6a12017-08-08 04:33:53 +020053
Klement Sekera3f6ff192017-08-11 06:56:05 +020054 child = Process(target=test_runner_wrapper,
Klement Sekeradf2b9802017-10-05 10:26:03 +020055 args=(suite, keep_alive_child_end, result_child_end,
56 failed_child_end))
Klement Sekera3f6ff192017-08-11 06:56:05 +020057 child.start()
Klement Sekera909a6a12017-08-08 04:33:53 +020058 last_test_temp_dir = None
59 last_test_vpp_binary = None
60 last_test = None
61 result = None
Klement Sekeradf2b9802017-10-05 10:26:03 +020062 failed = set()
Klement Sekera909a6a12017-08-08 04:33:53 +020063 while result is None:
64 readable = select.select([keep_alive_parent_end.fileno(),
65 result_parent_end.fileno(),
Klement Sekeradf2b9802017-10-05 10:26:03 +020066 failed_parent_end.fileno(),
Klement Sekera909a6a12017-08-08 04:33:53 +020067 ],
68 [], [], test_timeout)[0]
Klement Sekeradf2b9802017-10-05 10:26:03 +020069 timeout = True
Klement Sekera909a6a12017-08-08 04:33:53 +020070 if result_parent_end.fileno() in readable:
71 result = result_parent_end.recv()
Klement Sekeradf2b9802017-10-05 10:26:03 +020072 timeout = False
73 if keep_alive_parent_end.fileno() in readable:
Klement Sekera909a6a12017-08-08 04:33:53 +020074 while keep_alive_parent_end.poll():
Dave Wallacee2efd122017-09-30 22:04:21 -040075 last_test, last_test_vpp_binary,\
76 last_test_temp_dir, vpp_pid = keep_alive_parent_end.recv()
Klement Sekeradf2b9802017-10-05 10:26:03 +020077 timeout = False
78 if failed_parent_end.fileno() in readable:
79 while failed_parent_end.poll():
80 failed_test = failed_parent_end.recv()
81 failed.add(failed_test.__name__)
82 timeout = False
83 if timeout:
Klement Sekera909a6a12017-08-08 04:33:53 +020084 global_logger.critical("Timeout while waiting for child test "
85 "runner process (last test running was "
86 "`%s' in `%s')!" %
87 (last_test, last_test_temp_dir))
Dave Wallace981fadf2017-09-30 15:12:19 -040088 failed_dir = os.getenv('VPP_TEST_FAILED_DIR')
89 lttd = last_test_temp_dir.split("/")[-1]
90 link_path = '%s%s-FAILED' % (failed_dir, lttd)
91 global_logger.error("Creating a link to the failed " +
92 "test: %s -> %s" % (link_path, lttd))
93 os.symlink(last_test_temp_dir, link_path)
Dave Wallacee2efd122017-09-30 22:04:21 -040094 api_post_mortem_path = "/tmp/api_post_mortem.%d" % vpp_pid
95 if os.path.isfile(api_post_mortem_path):
96 global_logger.error("Copying api_post_mortem.%d to %s" %
97 (vpp_pid, last_test_temp_dir))
98 shutil.copy2(api_post_mortem_path, last_test_temp_dir)
Klement Sekera909a6a12017-08-08 04:33:53 +020099 if last_test_temp_dir and last_test_vpp_binary:
100 core_path = "%s/core" % last_test_temp_dir
101 if os.path.isfile(core_path):
102 global_logger.error("Core-file exists in test temporary "
103 "directory: %s!" % core_path)
Klement Sekera3f6ff192017-08-11 06:56:05 +0200104 if d and d.lower() == "core":
105 spawn_gdb(last_test_vpp_binary, core_path,
106 global_logger)
107 child.terminate()
Klement Sekera909a6a12017-08-08 04:33:53 +0200108 result = -1
109 keep_alive_parent_end.close()
110 result_parent_end.close()
Klement Sekeradf2b9802017-10-05 10:26:03 +0200111 failed_parent_end.close()
112 return result, failed
Klement Sekera3f6ff192017-08-11 06:56:05 +0200113
114
115if __name__ == '__main__':
116
117 try:
118 verbose = int(os.getenv("V", 0))
119 except:
120 verbose = 0
121
122 default_test_timeout = 600 # 10 minutes
123 try:
124 test_timeout = int(os.getenv("TIMEOUT", default_test_timeout))
125 except:
126 test_timeout = default_test_timeout
127
128 try:
129 debug = os.getenv("DEBUG")
130 except:
131 debug = None
132
133 parser = argparse.ArgumentParser(description="VPP unit tests")
134 parser.add_argument("-f", "--failfast", action='count',
135 help="fast failure flag")
136 parser.add_argument("-d", "--dir", action='append', type=str,
137 help="directory containing test files "
138 "(may be specified multiple times)")
139 args = parser.parse_args()
140 failfast = True if args.failfast == 1 else False
141
142 suite = unittest.TestSuite()
Klement Sekerafcbf4442017-08-17 07:38:42 +0200143 cb = add_to_suite_callback(suite)
Klement Sekera3f6ff192017-08-11 06:56:05 +0200144 for d in args.dir:
Klement Sekeradf2b9802017-10-05 10:26:03 +0200145 print("Adding tests from directory tree %s" % d)
Klement Sekerafcbf4442017-08-17 07:38:42 +0200146 discover_tests(d, cb)
Klement Sekera3f6ff192017-08-11 06:56:05 +0200147
Klement Sekeradf2b9802017-10-05 10:26:03 +0200148 try:
149 retries = int(os.getenv("RETRIES"))
150 except:
151 retries = 0
152 if retries is None:
153 retries = 0
154 attempts = retries + 1
155 if attempts > 1:
156 print("Perform %s attempts to pass the suite..." % attempts)
Klement Sekera3f6ff192017-08-11 06:56:05 +0200157 if debug is None or debug.lower() not in ["gdb", "gdbserver"]:
Klement Sekeradf2b9802017-10-05 10:26:03 +0200158 while True:
159 result, failed = run_forked(suite)
160 attempts = attempts - 1
161 print("%s test(s) failed, %s attempt(s) left" %
162 (len(failed), attempts))
163 if len(failed) > 0 and attempts > 0:
164 suite = suite_from_failed(suite, failed)
165 continue
166 sys.exit(result)
Klement Sekera3f6ff192017-08-11 06:56:05 +0200167
168 # don't fork if debugging..
169 sys.exit(not VppTestRunner(verbosity=verbose,
170 failfast=failfast).run(suite).wasSuccessful())