blob: b07a923ade238d73991ccd7ffcacb1b14dc562b8 [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 Sekera3f6ff192017-08-11 06:56:05 +020016def test_runner_wrapper(suite, keep_alive_pipe, result_pipe):
Klement Sekera909a6a12017-08-08 04:33:53 +020017 result = not VppTestRunner(
18 pipe=keep_alive_pipe,
19 verbosity=verbose,
20 failfast=failfast).run(suite).wasSuccessful()
21 result_pipe.send(result)
22 result_pipe.close()
23 keep_alive_pipe.close()
24
25
Klement Sekerafcbf4442017-08-17 07:38:42 +020026class add_to_suite_callback:
27 def __init__(self, suite):
28 self.suite = suite
29
30 def __call__(self, file_name, cls, method):
31 suite.addTest(cls(method))
32
33
Klement Sekera3f6ff192017-08-11 06:56:05 +020034def run_forked(suite):
Klement Sekera909a6a12017-08-08 04:33:53 +020035 keep_alive_parent_end, keep_alive_child_end = Pipe(duplex=False)
36 result_parent_end, result_child_end = Pipe(duplex=False)
37
Klement Sekera3f6ff192017-08-11 06:56:05 +020038 child = Process(target=test_runner_wrapper,
39 args=(suite, keep_alive_child_end, result_child_end))
40 child.start()
Klement Sekera909a6a12017-08-08 04:33:53 +020041 last_test_temp_dir = None
42 last_test_vpp_binary = None
43 last_test = None
44 result = None
45 while result is None:
46 readable = select.select([keep_alive_parent_end.fileno(),
47 result_parent_end.fileno(),
48 ],
49 [], [], test_timeout)[0]
50 if result_parent_end.fileno() in readable:
51 result = result_parent_end.recv()
52 elif keep_alive_parent_end.fileno() in readable:
53 while keep_alive_parent_end.poll():
Dave Wallacee2efd122017-09-30 22:04:21 -040054 last_test, last_test_vpp_binary,\
55 last_test_temp_dir, vpp_pid = keep_alive_parent_end.recv()
Klement Sekera909a6a12017-08-08 04:33:53 +020056 else:
57 global_logger.critical("Timeout while waiting for child test "
58 "runner process (last test running was "
59 "`%s' in `%s')!" %
60 (last_test, last_test_temp_dir))
Dave Wallace981fadf2017-09-30 15:12:19 -040061 failed_dir = os.getenv('VPP_TEST_FAILED_DIR')
62 lttd = last_test_temp_dir.split("/")[-1]
63 link_path = '%s%s-FAILED' % (failed_dir, lttd)
64 global_logger.error("Creating a link to the failed " +
65 "test: %s -> %s" % (link_path, lttd))
66 os.symlink(last_test_temp_dir, link_path)
Dave Wallacee2efd122017-09-30 22:04:21 -040067 api_post_mortem_path = "/tmp/api_post_mortem.%d" % vpp_pid
68 if os.path.isfile(api_post_mortem_path):
69 global_logger.error("Copying api_post_mortem.%d to %s" %
70 (vpp_pid, last_test_temp_dir))
71 shutil.copy2(api_post_mortem_path, last_test_temp_dir)
Klement Sekera909a6a12017-08-08 04:33:53 +020072 if last_test_temp_dir and last_test_vpp_binary:
73 core_path = "%s/core" % last_test_temp_dir
74 if os.path.isfile(core_path):
75 global_logger.error("Core-file exists in test temporary "
76 "directory: %s!" % core_path)
Klement Sekera3f6ff192017-08-11 06:56:05 +020077 if d and d.lower() == "core":
78 spawn_gdb(last_test_vpp_binary, core_path,
79 global_logger)
80 child.terminate()
Klement Sekera909a6a12017-08-08 04:33:53 +020081 result = -1
82 keep_alive_parent_end.close()
83 result_parent_end.close()
Klement Sekera3f6ff192017-08-11 06:56:05 +020084 return result
85
86
87if __name__ == '__main__':
88
89 try:
90 verbose = int(os.getenv("V", 0))
91 except:
92 verbose = 0
93
94 default_test_timeout = 600 # 10 minutes
95 try:
96 test_timeout = int(os.getenv("TIMEOUT", default_test_timeout))
97 except:
98 test_timeout = default_test_timeout
99
100 try:
101 debug = os.getenv("DEBUG")
102 except:
103 debug = None
104
105 parser = argparse.ArgumentParser(description="VPP unit tests")
106 parser.add_argument("-f", "--failfast", action='count',
107 help="fast failure flag")
108 parser.add_argument("-d", "--dir", action='append', type=str,
109 help="directory containing test files "
110 "(may be specified multiple times)")
111 args = parser.parse_args()
112 failfast = True if args.failfast == 1 else False
113
114 suite = unittest.TestSuite()
Klement Sekerafcbf4442017-08-17 07:38:42 +0200115 cb = add_to_suite_callback(suite)
Klement Sekera3f6ff192017-08-11 06:56:05 +0200116 for d in args.dir:
117 global_logger.info("Adding tests from directory tree %s" % d)
Klement Sekerafcbf4442017-08-17 07:38:42 +0200118 discover_tests(d, cb)
Klement Sekera3f6ff192017-08-11 06:56:05 +0200119
120 if debug is None or debug.lower() not in ["gdb", "gdbserver"]:
121 sys.exit(run_forked(suite))
122
123 # don't fork if debugging..
124 sys.exit(not VppTestRunner(verbosity=verbose,
125 failfast=failfast).run(suite).wasSuccessful())