Skip to content

Commit ee92ed3

Browse files
dlatypovshuahkh
authored andcommitted
kunit: add run_checks.py script to validate kunit changes
This formalizes the checks KUnit maintainers have been running (or in other cases: forgetting to run). This script also runs them all in parallel to minimize friction (pytype can be fairly slow, but not slower than running kunit.py). Example output: $ ./tools/testing/kunit/run_checks.py Waiting on 4 checks (kunit_tool_test.py, kunit smoke test, pytype, mypy)... kunit_tool_test.py: PASSED mypy: PASSED pytype: PASSED kunit smoke test: PASSED On failure or timeout (5 minutes), it'll dump out the stdout/stderr. E.g. adding in a type-checking error: mypy: FAILED > kunit.py:54: error: Name 'nonexistent_function' is not defined > Found 1 error in 1 file (checked 8 source files) mypy and pytype are two Python type-checkers and must be installed. This file treats them as optional and will mark them as SKIPPED if not installed. This tool also runs `kunit.py run --kunitconfig=lib/kunit` to run KUnit's own KUnit tests and to verify KUnit kernel code and kunit.py play nicely together. It uses --build_dir=kunit_run_checks so as not to clobber the default build_dir, which helps make it faster by reducing the need to rebuild, esp. if you're been passing in --arch instead of using UML. Signed-off-by: Daniel Latypov <[email protected]> Reviewed-by: David Gow <[email protected]> Reviewed-by: David Gow <[email protected]> Reviewed-by: Brendan Higgins <[email protected]> Signed-off-by: Shuah Khan <[email protected]>
1 parent 58b391d commit ee92ed3

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

tools/testing/kunit/run_checks.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#!/usr/bin/env python3
2+
# SPDX-License-Identifier: GPL-2.0
3+
#
4+
# This file runs some basic checks to verify kunit works.
5+
# It is only of interest if you're making changes to KUnit itself.
6+
#
7+
# Copyright (C) 2021, Google LLC.
8+
# Author: Daniel Latypov <[email protected]>
9+
10+
from concurrent import futures
11+
import datetime
12+
import os
13+
import shutil
14+
import subprocess
15+
import sys
16+
import textwrap
17+
from typing import Dict, List, Sequence, Tuple
18+
19+
ABS_TOOL_PATH = os.path.abspath(os.path.dirname(__file__))
20+
TIMEOUT = datetime.timedelta(minutes=5).total_seconds()
21+
22+
commands: Dict[str, Sequence[str]] = {
23+
'kunit_tool_test.py': ['./kunit_tool_test.py'],
24+
'kunit smoke test': ['./kunit.py', 'run', '--kunitconfig=lib/kunit', '--build_dir=kunit_run_checks'],
25+
'pytype': ['/bin/sh', '-c', 'pytype *.py'],
26+
'mypy': ['/bin/sh', '-c', 'mypy *.py'],
27+
}
28+
29+
# The user might not have mypy or pytype installed, skip them if so.
30+
# Note: you can install both via `$ pip install mypy pytype`
31+
necessary_deps : Dict[str, str] = {
32+
'pytype': 'pytype',
33+
'mypy': 'mypy',
34+
}
35+
36+
def main(argv: Sequence[str]) -> None:
37+
if argv:
38+
raise RuntimeError('This script takes no arguments')
39+
40+
future_to_name: Dict[futures.Future, str] = {}
41+
executor = futures.ThreadPoolExecutor(max_workers=len(commands))
42+
for name, argv in commands.items():
43+
if name in necessary_deps and shutil.which(necessary_deps[name]) is None:
44+
print(f'{name}: SKIPPED, {necessary_deps[name]} not in $PATH')
45+
continue
46+
f = executor.submit(run_cmd, argv)
47+
future_to_name[f] = name
48+
49+
has_failures = False
50+
print(f'Waiting on {len(future_to_name)} checks ({", ".join(future_to_name.values())})...')
51+
for f in futures.as_completed(future_to_name.keys()):
52+
name = future_to_name[f]
53+
ex = f.exception()
54+
if not ex:
55+
print(f'{name}: PASSED')
56+
continue
57+
58+
has_failures = True
59+
if isinstance(ex, subprocess.TimeoutExpired):
60+
print(f'{name}: TIMED OUT')
61+
elif isinstance(ex, subprocess.CalledProcessError):
62+
print(f'{name}: FAILED')
63+
else:
64+
print('{name}: unexpected exception: {ex}')
65+
continue
66+
67+
output = ex.output
68+
if output:
69+
print(textwrap.indent(output.decode(), '> '))
70+
executor.shutdown()
71+
72+
if has_failures:
73+
sys.exit(1)
74+
75+
76+
def run_cmd(argv: Sequence[str]):
77+
subprocess.check_output(argv, stderr=subprocess.STDOUT, cwd=ABS_TOOL_PATH, timeout=TIMEOUT)
78+
79+
80+
if __name__ == '__main__':
81+
main(sys.argv[1:])

0 commit comments

Comments
 (0)