Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
Klement Sekera | 993e0ed | 2017-03-16 09:14:59 +0100 | [diff] [blame] | 3 | import sys |
Dave Wallace | e2efd12 | 2017-09-30 22:04:21 -0400 | [diff] [blame] | 4 | import shutil |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 5 | import os |
Klement Sekera | 909a6a1 | 2017-08-08 04:33:53 +0200 | [diff] [blame] | 6 | import select |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 7 | import unittest |
Klement Sekera | 993e0ed | 2017-03-16 09:14:59 +0100 | [diff] [blame] | 8 | import argparse |
Klement Sekera | 545be52 | 2018-02-16 19:25:06 +0100 | [diff] [blame] | 9 | import time |
Klement Sekera | 909a6a1 | 2017-08-08 04:33:53 +0200 | [diff] [blame] | 10 | from multiprocessing import Process, Pipe |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 11 | from framework import VppTestRunner |
Klement Sekera | 909a6a1 | 2017-08-08 04:33:53 +0200 | [diff] [blame] | 12 | from debug import spawn_gdb |
| 13 | from log import global_logger |
Klement Sekera | fcbf444 | 2017-08-17 07:38:42 +0200 | [diff] [blame] | 14 | from discover_tests import discover_tests |
Klement Sekera | 993e0ed | 2017-03-16 09:14:59 +0100 | [diff] [blame] | 15 | |
Klement Sekera | 0574226 | 2018-03-14 18:14:49 +0100 | [diff] [blame] | 16 | # 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 |
| 20 | core_timeout = 3 |
| 21 | |
Klement Sekera | 909a6a1 | 2017-08-08 04:33:53 +0200 | [diff] [blame] | 22 | |
Klement Sekera | df2b980 | 2017-10-05 10:26:03 +0200 | [diff] [blame] | 23 | def test_runner_wrapper(suite, keep_alive_pipe, result_pipe, failed_pipe): |
Klement Sekera | 909a6a1 | 2017-08-08 04:33:53 +0200 | [diff] [blame] | 24 | result = not VppTestRunner( |
Klement Sekera | df2b980 | 2017-10-05 10:26:03 +0200 | [diff] [blame] | 25 | keep_alive_pipe=keep_alive_pipe, |
| 26 | failed_pipe=failed_pipe, |
Klement Sekera | 909a6a1 | 2017-08-08 04:33:53 +0200 | [diff] [blame] | 27 | verbosity=verbose, |
| 28 | failfast=failfast).run(suite).wasSuccessful() |
| 29 | result_pipe.send(result) |
| 30 | result_pipe.close() |
| 31 | keep_alive_pipe.close() |
Klement Sekera | df2b980 | 2017-10-05 10:26:03 +0200 | [diff] [blame] | 32 | failed_pipe.close() |
Klement Sekera | 909a6a1 | 2017-08-08 04:33:53 +0200 | [diff] [blame] | 33 | |
| 34 | |
Klement Sekera | fcbf444 | 2017-08-17 07:38:42 +0200 | [diff] [blame] | 35 | class 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 Sekera | df2b980 | 2017-10-05 10:26:03 +0200 | [diff] [blame] | 43 | class 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 | |
| 51 | def suite_from_failed(suite, failed): |
| 52 | filter_cb = Filter_by_class_list(failed) |
| 53 | return VppTestRunner.filter_tests(suite, filter_cb) |
| 54 | |
| 55 | |
Klement Sekera | 3f6ff19 | 2017-08-11 06:56:05 +0200 | [diff] [blame] | 56 | def run_forked(suite): |
Klement Sekera | 909a6a1 | 2017-08-08 04:33:53 +0200 | [diff] [blame] | 57 | keep_alive_parent_end, keep_alive_child_end = Pipe(duplex=False) |
| 58 | result_parent_end, result_child_end = Pipe(duplex=False) |
Klement Sekera | df2b980 | 2017-10-05 10:26:03 +0200 | [diff] [blame] | 59 | failed_parent_end, failed_child_end = Pipe(duplex=False) |
Klement Sekera | 909a6a1 | 2017-08-08 04:33:53 +0200 | [diff] [blame] | 60 | |
Klement Sekera | 3f6ff19 | 2017-08-11 06:56:05 +0200 | [diff] [blame] | 61 | child = Process(target=test_runner_wrapper, |
Klement Sekera | df2b980 | 2017-10-05 10:26:03 +0200 | [diff] [blame] | 62 | args=(suite, keep_alive_child_end, result_child_end, |
| 63 | failed_child_end)) |
Klement Sekera | 3f6ff19 | 2017-08-11 06:56:05 +0200 | [diff] [blame] | 64 | child.start() |
Klement Sekera | 909a6a1 | 2017-08-08 04:33:53 +0200 | [diff] [blame] | 65 | last_test_temp_dir = None |
| 66 | last_test_vpp_binary = None |
| 67 | last_test = None |
| 68 | result = None |
Klement Sekera | df2b980 | 2017-10-05 10:26:03 +0200 | [diff] [blame] | 69 | failed = set() |
Klement Sekera | 545be52 | 2018-02-16 19:25:06 +0100 | [diff] [blame] | 70 | last_heard = time.time() |
Klement Sekera | 0574226 | 2018-03-14 18:14:49 +0100 | [diff] [blame] | 71 | core_detected_at = None |
| 72 | debug_core = os.getenv("DEBUG", "").lower() == "core" |
Klement Sekera | 545be52 | 2018-02-16 19:25:06 +0100 | [diff] [blame] | 73 | while True: |
Klement Sekera | 909a6a1 | 2017-08-08 04:33:53 +0200 | [diff] [blame] | 74 | readable = select.select([keep_alive_parent_end.fileno(), |
| 75 | result_parent_end.fileno(), |
Klement Sekera | df2b980 | 2017-10-05 10:26:03 +0200 | [diff] [blame] | 76 | failed_parent_end.fileno(), |
Klement Sekera | 909a6a1 | 2017-08-08 04:33:53 +0200 | [diff] [blame] | 77 | ], |
Klement Sekera | 545be52 | 2018-02-16 19:25:06 +0100 | [diff] [blame] | 78 | [], [], 1)[0] |
Klement Sekera | 909a6a1 | 2017-08-08 04:33:53 +0200 | [diff] [blame] | 79 | if result_parent_end.fileno() in readable: |
| 80 | result = result_parent_end.recv() |
Klement Sekera | 545be52 | 2018-02-16 19:25:06 +0100 | [diff] [blame] | 81 | break |
Klement Sekera | df2b980 | 2017-10-05 10:26:03 +0200 | [diff] [blame] | 82 | if keep_alive_parent_end.fileno() in readable: |
Klement Sekera | 909a6a1 | 2017-08-08 04:33:53 +0200 | [diff] [blame] | 83 | while keep_alive_parent_end.poll(): |
Dave Wallace | e2efd12 | 2017-09-30 22:04:21 -0400 | [diff] [blame] | 84 | last_test, last_test_vpp_binary,\ |
| 85 | last_test_temp_dir, vpp_pid = keep_alive_parent_end.recv() |
Klement Sekera | 545be52 | 2018-02-16 19:25:06 +0100 | [diff] [blame] | 86 | last_heard = time.time() |
Klement Sekera | df2b980 | 2017-10-05 10:26:03 +0200 | [diff] [blame] | 87 | 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 Sekera | 545be52 | 2018-02-16 19:25:06 +0100 | [diff] [blame] | 91 | last_heard = time.time() |
| 92 | fail = False |
Klement Sekera | 0574226 | 2018-03-14 18:14:49 +0100 | [diff] [blame] | 93 | if last_heard + test_timeout < time.time() and \ |
| 94 | not os.path.isfile("%s/_core_handled" % last_test_temp_dir): |
Klement Sekera | 545be52 | 2018-02-16 19:25:06 +0100 | [diff] [blame] | 95 | fail = True |
Klement Sekera | 909a6a1 | 2017-08-08 04:33:53 +0200 | [diff] [blame] | 96 | 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 Sekera | 545be52 | 2018-02-16 19:25:06 +0100 | [diff] [blame] | 100 | 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 Sekera | 0574226 | 2018-03-14 18:14:49 +0100 | [diff] [blame] | 105 | 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 Sekera | 545be52 | 2018-02-16 19:25:06 +0100 | [diff] [blame] | 118 | if fail: |
Dave Wallace | 981fadf | 2017-09-30 15:12:19 -0400 | [diff] [blame] | 119 | 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 Sekera | 833e761 | 2018-03-13 21:22:32 +0100 | [diff] [blame] | 124 | try: |
| 125 | os.symlink(last_test_temp_dir, link_path) |
| 126 | except: |
| 127 | pass |
Dave Wallace | e2efd12 | 2017-09-30 22:04:21 -0400 | [diff] [blame] | 128 | 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 Sekera | 909a6a1 | 2017-08-08 04:33:53 +0200 | [diff] [blame] | 133 | 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 Sekera | 0574226 | 2018-03-14 18:14:49 +0100 | [diff] [blame] | 138 | if debug_core: |
Klement Sekera | 3f6ff19 | 2017-08-11 06:56:05 +0200 | [diff] [blame] | 139 | spawn_gdb(last_test_vpp_binary, core_path, |
| 140 | global_logger) |
| 141 | child.terminate() |
Klement Sekera | 909a6a1 | 2017-08-08 04:33:53 +0200 | [diff] [blame] | 142 | result = -1 |
Klement Sekera | 545be52 | 2018-02-16 19:25:06 +0100 | [diff] [blame] | 143 | break |
Klement Sekera | 909a6a1 | 2017-08-08 04:33:53 +0200 | [diff] [blame] | 144 | keep_alive_parent_end.close() |
| 145 | result_parent_end.close() |
Klement Sekera | df2b980 | 2017-10-05 10:26:03 +0200 | [diff] [blame] | 146 | failed_parent_end.close() |
| 147 | return result, failed |
Klement Sekera | 3f6ff19 | 2017-08-11 06:56:05 +0200 | [diff] [blame] | 148 | |
| 149 | |
| 150 | if __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 Sekera | fcbf444 | 2017-08-17 07:38:42 +0200 | [diff] [blame] | 178 | cb = add_to_suite_callback(suite) |
Klement Sekera | 3f6ff19 | 2017-08-11 06:56:05 +0200 | [diff] [blame] | 179 | for d in args.dir: |
Klement Sekera | df2b980 | 2017-10-05 10:26:03 +0200 | [diff] [blame] | 180 | print("Adding tests from directory tree %s" % d) |
Klement Sekera | fcbf444 | 2017-08-17 07:38:42 +0200 | [diff] [blame] | 181 | discover_tests(d, cb) |
Klement Sekera | 3f6ff19 | 2017-08-11 06:56:05 +0200 | [diff] [blame] | 182 | |
Klement Sekera | df2b980 | 2017-10-05 10:26:03 +0200 | [diff] [blame] | 183 | 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 Sekera | 3f6ff19 | 2017-08-11 06:56:05 +0200 | [diff] [blame] | 192 | if debug is None or debug.lower() not in ["gdb", "gdbserver"]: |
Klement Sekera | df2b980 | 2017-10-05 10:26:03 +0200 | [diff] [blame] | 193 | 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 Sekera | 3f6ff19 | 2017-08-11 06:56:05 +0200 | [diff] [blame] | 202 | |
| 203 | # don't fork if debugging.. |
| 204 | sys.exit(not VppTestRunner(verbosity=verbose, |
| 205 | failfast=failfast).run(suite).wasSuccessful()) |