Loading...
1#!/usr/bin/env python3
2# SPDX-License-Identifier: GPL-2.0
3#
4# A thin wrapper on top of the KUnit Kernel
5#
6# Copyright (C) 2019, Google LLC.
7# Author: Felix Guo <felixguoxiuping@gmail.com>
8# Author: Brendan Higgins <brendanhiggins@google.com>
9
10import argparse
11import os
12import re
13import shlex
14import sys
15import time
16
17assert sys.version_info >= (3, 7), "Python version is too old"
18
19from dataclasses import dataclass
20from enum import Enum, auto
21from typing import Iterable, List, Optional, Sequence, Tuple
22
23import kunit_json
24import kunit_kernel
25import kunit_parser
26from kunit_printer import stdout, null_printer
27
28class KunitStatus(Enum):
29 SUCCESS = auto()
30 CONFIG_FAILURE = auto()
31 BUILD_FAILURE = auto()
32 TEST_FAILURE = auto()
33
34@dataclass
35class KunitResult:
36 status: KunitStatus
37 elapsed_time: float
38
39@dataclass
40class KunitConfigRequest:
41 build_dir: str
42 make_options: Optional[List[str]]
43
44@dataclass
45class KunitBuildRequest(KunitConfigRequest):
46 jobs: int
47
48@dataclass
49class KunitParseRequest:
50 raw_output: Optional[str]
51 json: Optional[str]
52 summary: bool
53 failed: bool
54
55@dataclass
56class KunitExecRequest(KunitParseRequest):
57 build_dir: str
58 timeout: int
59 filter_glob: str
60 filter: str
61 filter_action: Optional[str]
62 kernel_args: Optional[List[str]]
63 run_isolated: Optional[str]
64 list_tests: bool
65 list_tests_attr: bool
66
67@dataclass
68class KunitRequest(KunitExecRequest, KunitBuildRequest):
69 pass
70
71
72def get_kernel_root_path() -> str:
73 path = sys.argv[0] if not __file__ else __file__
74 parts = os.path.realpath(path).split('tools/testing/kunit')
75 if len(parts) != 2:
76 sys.exit(1)
77 return parts[0]
78
79def config_tests(linux: kunit_kernel.LinuxSourceTree,
80 request: KunitConfigRequest) -> KunitResult:
81 stdout.print_with_timestamp('Configuring KUnit Kernel ...')
82
83 config_start = time.time()
84 success = linux.build_reconfig(request.build_dir, request.make_options)
85 config_end = time.time()
86 status = KunitStatus.SUCCESS if success else KunitStatus.CONFIG_FAILURE
87 return KunitResult(status, config_end - config_start)
88
89def build_tests(linux: kunit_kernel.LinuxSourceTree,
90 request: KunitBuildRequest) -> KunitResult:
91 stdout.print_with_timestamp('Building KUnit Kernel ...')
92
93 build_start = time.time()
94 success = linux.build_kernel(request.jobs,
95 request.build_dir,
96 request.make_options)
97 build_end = time.time()
98 status = KunitStatus.SUCCESS if success else KunitStatus.BUILD_FAILURE
99 return KunitResult(status, build_end - build_start)
100
101def config_and_build_tests(linux: kunit_kernel.LinuxSourceTree,
102 request: KunitBuildRequest) -> KunitResult:
103 config_result = config_tests(linux, request)
104 if config_result.status != KunitStatus.SUCCESS:
105 return config_result
106
107 return build_tests(linux, request)
108
109def _list_tests(linux: kunit_kernel.LinuxSourceTree, request: KunitExecRequest) -> List[str]:
110 args = ['kunit.action=list']
111
112 if request.kernel_args:
113 args.extend(request.kernel_args)
114
115 output = linux.run_kernel(args=args,
116 timeout=request.timeout,
117 filter_glob=request.filter_glob,
118 filter=request.filter,
119 filter_action=request.filter_action,
120 build_dir=request.build_dir)
121 lines = kunit_parser.extract_tap_lines(output)
122 # Hack! Drop the dummy TAP version header that the executor prints out.
123 lines.pop()
124
125 # Filter out any extraneous non-test output that might have gotten mixed in.
126 return [l for l in output if re.match(r'^[^\s.]+\.[^\s.]+$', l)]
127
128def _list_tests_attr(linux: kunit_kernel.LinuxSourceTree, request: KunitExecRequest) -> Iterable[str]:
129 args = ['kunit.action=list_attr']
130
131 if request.kernel_args:
132 args.extend(request.kernel_args)
133
134 output = linux.run_kernel(args=args,
135 timeout=request.timeout,
136 filter_glob=request.filter_glob,
137 filter=request.filter,
138 filter_action=request.filter_action,
139 build_dir=request.build_dir)
140 lines = kunit_parser.extract_tap_lines(output)
141 # Hack! Drop the dummy TAP version header that the executor prints out.
142 lines.pop()
143
144 # Filter out any extraneous non-test output that might have gotten mixed in.
145 return lines
146
147def _suites_from_test_list(tests: List[str]) -> List[str]:
148 """Extracts all the suites from an ordered list of tests."""
149 suites = [] # type: List[str]
150 for t in tests:
151 parts = t.split('.', maxsplit=2)
152 if len(parts) != 2:
153 raise ValueError(f'internal KUnit error, test name should be of the form "<suite>.<test>", got "{t}"')
154 suite, _ = parts
155 if not suites or suites[-1] != suite:
156 suites.append(suite)
157 return suites
158
159def exec_tests(linux: kunit_kernel.LinuxSourceTree, request: KunitExecRequest) -> KunitResult:
160 filter_globs = [request.filter_glob]
161 if request.list_tests:
162 output = _list_tests(linux, request)
163 for line in output:
164 print(line.rstrip())
165 return KunitResult(status=KunitStatus.SUCCESS, elapsed_time=0.0)
166 if request.list_tests_attr:
167 attr_output = _list_tests_attr(linux, request)
168 for line in attr_output:
169 print(line.rstrip())
170 return KunitResult(status=KunitStatus.SUCCESS, elapsed_time=0.0)
171 if request.run_isolated:
172 tests = _list_tests(linux, request)
173 if request.run_isolated == 'test':
174 filter_globs = tests
175 elif request.run_isolated == 'suite':
176 filter_globs = _suites_from_test_list(tests)
177 # Apply the test-part of the user's glob, if present.
178 if '.' in request.filter_glob:
179 test_glob = request.filter_glob.split('.', maxsplit=2)[1]
180 filter_globs = [g + '.'+ test_glob for g in filter_globs]
181
182 metadata = kunit_json.Metadata(arch=linux.arch(), build_dir=request.build_dir, def_config='kunit_defconfig')
183
184 test_counts = kunit_parser.TestCounts()
185 exec_time = 0.0
186 for i, filter_glob in enumerate(filter_globs):
187 stdout.print_with_timestamp('Starting KUnit Kernel ({}/{})...'.format(i+1, len(filter_globs)))
188
189 test_start = time.time()
190 run_result = linux.run_kernel(
191 args=request.kernel_args,
192 timeout=request.timeout,
193 filter_glob=filter_glob,
194 filter=request.filter,
195 filter_action=request.filter_action,
196 build_dir=request.build_dir)
197
198 _, test_result = parse_tests(request, metadata, run_result)
199 # run_kernel() doesn't block on the kernel exiting.
200 # That only happens after we get the last line of output from `run_result`.
201 # So exec_time here actually contains parsing + execution time, which is fine.
202 test_end = time.time()
203 exec_time += test_end - test_start
204
205 test_counts.add_subtest_counts(test_result.counts)
206
207 if len(filter_globs) == 1 and test_counts.crashed > 0:
208 bd = request.build_dir
209 print('The kernel seems to have crashed; you can decode the stack traces with:')
210 print('$ scripts/decode_stacktrace.sh {}/vmlinux {} < {} | tee {}/decoded.log | {} parse'.format(
211 bd, bd, kunit_kernel.get_outfile_path(bd), bd, sys.argv[0]))
212
213 kunit_status = _map_to_overall_status(test_counts.get_status())
214 return KunitResult(status=kunit_status, elapsed_time=exec_time)
215
216def _map_to_overall_status(test_status: kunit_parser.TestStatus) -> KunitStatus:
217 if test_status in (kunit_parser.TestStatus.SUCCESS, kunit_parser.TestStatus.SKIPPED):
218 return KunitStatus.SUCCESS
219 return KunitStatus.TEST_FAILURE
220
221def parse_tests(request: KunitParseRequest, metadata: kunit_json.Metadata, input_data: Iterable[str]) -> Tuple[KunitResult, kunit_parser.Test]:
222 parse_start = time.time()
223
224 if request.raw_output:
225 # Treat unparsed results as one passing test.
226 fake_test = kunit_parser.Test()
227 fake_test.status = kunit_parser.TestStatus.SUCCESS
228 fake_test.counts.passed = 1
229
230 output: Iterable[str] = input_data
231 if request.raw_output == 'all':
232 pass
233 elif request.raw_output == 'kunit':
234 output = kunit_parser.extract_tap_lines(output)
235 for line in output:
236 print(line.rstrip())
237 parse_time = time.time() - parse_start
238 return KunitResult(KunitStatus.SUCCESS, parse_time), fake_test
239
240 default_printer = stdout
241 if request.summary or request.failed:
242 default_printer = null_printer
243
244 # Actually parse the test results.
245 test = kunit_parser.parse_run_tests(input_data, default_printer)
246 parse_time = time.time() - parse_start
247
248 if request.failed:
249 kunit_parser.print_test(test, request.failed, stdout)
250 kunit_parser.print_summary_line(test, stdout)
251
252 if request.json:
253 json_str = kunit_json.get_json_result(
254 test=test,
255 metadata=metadata)
256 if request.json == 'stdout':
257 print(json_str)
258 else:
259 with open(request.json, 'w') as f:
260 f.write(json_str)
261 stdout.print_with_timestamp("Test results stored in %s" %
262 os.path.abspath(request.json))
263
264 if test.status != kunit_parser.TestStatus.SUCCESS:
265 return KunitResult(KunitStatus.TEST_FAILURE, parse_time), test
266
267 return KunitResult(KunitStatus.SUCCESS, parse_time), test
268
269def run_tests(linux: kunit_kernel.LinuxSourceTree,
270 request: KunitRequest) -> KunitResult:
271 run_start = time.time()
272
273 config_result = config_tests(linux, request)
274 if config_result.status != KunitStatus.SUCCESS:
275 return config_result
276
277 build_result = build_tests(linux, request)
278 if build_result.status != KunitStatus.SUCCESS:
279 return build_result
280
281 exec_result = exec_tests(linux, request)
282
283 run_end = time.time()
284
285 stdout.print_with_timestamp((
286 'Elapsed time: %.3fs total, %.3fs configuring, %.3fs ' +
287 'building, %.3fs running\n') % (
288 run_end - run_start,
289 config_result.elapsed_time,
290 build_result.elapsed_time,
291 exec_result.elapsed_time))
292 return exec_result
293
294# Problem:
295# $ kunit.py run --json
296# works as one would expect and prints the parsed test results as JSON.
297# $ kunit.py run --json suite_name
298# would *not* pass suite_name as the filter_glob and print as json.
299# argparse will consider it to be another way of writing
300# $ kunit.py run --json=suite_name
301# i.e. it would run all tests, and dump the json to a `suite_name` file.
302# So we hackily automatically rewrite --json => --json=stdout
303pseudo_bool_flag_defaults = {
304 '--json': 'stdout',
305 '--raw_output': 'kunit',
306}
307def massage_argv(argv: Sequence[str]) -> Sequence[str]:
308 def massage_arg(arg: str) -> str:
309 if arg not in pseudo_bool_flag_defaults:
310 return arg
311 return f'{arg}={pseudo_bool_flag_defaults[arg]}'
312 return list(map(massage_arg, argv))
313
314def get_default_jobs() -> int:
315 return len(os.sched_getaffinity(0))
316
317def add_common_opts(parser: argparse.ArgumentParser) -> None:
318 parser.add_argument('--build_dir',
319 help='As in the make command, it specifies the build '
320 'directory.',
321 type=str, default='.kunit', metavar='DIR')
322 parser.add_argument('--make_options',
323 help='X=Y make option, can be repeated.',
324 action='append', metavar='X=Y')
325 parser.add_argument('--alltests',
326 help='Run all KUnit tests via tools/testing/kunit/configs/all_tests.config',
327 action='store_true')
328 parser.add_argument('--kunitconfig',
329 help='Path to Kconfig fragment that enables KUnit tests.'
330 ' If given a directory, (e.g. lib/kunit), "/.kunitconfig" '
331 'will get automatically appended. If repeated, the files '
332 'blindly concatenated, which might not work in all cases.',
333 action='append', metavar='PATHS')
334 parser.add_argument('--kconfig_add',
335 help='Additional Kconfig options to append to the '
336 '.kunitconfig, e.g. CONFIG_KASAN=y. Can be repeated.',
337 action='append', metavar='CONFIG_X=Y')
338
339 parser.add_argument('--arch',
340 help=('Specifies the architecture to run tests under. '
341 'The architecture specified here must match the '
342 'string passed to the ARCH make param, '
343 'e.g. i386, x86_64, arm, um, etc. Non-UML '
344 'architectures run on QEMU.'),
345 type=str, default='um', metavar='ARCH')
346
347 parser.add_argument('--cross_compile',
348 help=('Sets make\'s CROSS_COMPILE variable; it should '
349 'be set to a toolchain path prefix (the prefix '
350 'of gcc and other tools in your toolchain, for '
351 'example `sparc64-linux-gnu-` if you have the '
352 'sparc toolchain installed on your system, or '
353 '`$HOME/toolchains/microblaze/gcc-9.2.0-nolibc/microblaze-linux/bin/microblaze-linux-` '
354 'if you have downloaded the microblaze toolchain '
355 'from the 0-day website to a directory in your '
356 'home directory called `toolchains`).'),
357 metavar='PREFIX')
358
359 parser.add_argument('--qemu_config',
360 help=('Takes a path to a path to a file containing '
361 'a QemuArchParams object.'),
362 type=str, metavar='FILE')
363
364 parser.add_argument('--qemu_args',
365 help='Additional QEMU arguments, e.g. "-smp 8"',
366 action='append', metavar='')
367
368def add_build_opts(parser: argparse.ArgumentParser) -> None:
369 parser.add_argument('--jobs',
370 help='As in the make command, "Specifies the number of '
371 'jobs (commands) to run simultaneously."',
372 type=int, default=get_default_jobs(), metavar='N')
373
374def add_exec_opts(parser: argparse.ArgumentParser) -> None:
375 parser.add_argument('--timeout',
376 help='maximum number of seconds to allow for all tests '
377 'to run. This does not include time taken to build the '
378 'tests.',
379 type=int,
380 default=300,
381 metavar='SECONDS')
382 parser.add_argument('filter_glob',
383 help='Filter which KUnit test suites/tests run at '
384 'boot-time, e.g. list* or list*.*del_test',
385 type=str,
386 nargs='?',
387 default='',
388 metavar='filter_glob')
389 parser.add_argument('--filter',
390 help='Filter KUnit tests with attributes, '
391 'e.g. module=example or speed>slow',
392 type=str,
393 default='')
394 parser.add_argument('--filter_action',
395 help='If set to skip, filtered tests will be skipped, '
396 'e.g. --filter_action=skip. Otherwise they will not run.',
397 type=str,
398 choices=['skip'])
399 parser.add_argument('--kernel_args',
400 help='Kernel command-line parameters. Maybe be repeated',
401 action='append', metavar='')
402 parser.add_argument('--run_isolated', help='If set, boot the kernel for each '
403 'individual suite/test. This is can be useful for debugging '
404 'a non-hermetic test, one that might pass/fail based on '
405 'what ran before it.',
406 type=str,
407 choices=['suite', 'test'])
408 parser.add_argument('--list_tests', help='If set, list all tests that will be '
409 'run.',
410 action='store_true')
411 parser.add_argument('--list_tests_attr', help='If set, list all tests and test '
412 'attributes.',
413 action='store_true')
414
415def add_parse_opts(parser: argparse.ArgumentParser) -> None:
416 parser.add_argument('--raw_output', help='If set don\'t parse output from kernel. '
417 'By default, filters to just KUnit output. Use '
418 '--raw_output=all to show everything',
419 type=str, nargs='?', const='all', default=None, choices=['all', 'kunit'])
420 parser.add_argument('--json',
421 nargs='?',
422 help='Prints parsed test results as JSON to stdout or a file if '
423 'a filename is specified. Does nothing if --raw_output is set.',
424 type=str, const='stdout', default=None, metavar='FILE')
425 parser.add_argument('--summary',
426 help='Prints only the summary line for parsed test results.'
427 'Does nothing if --raw_output is set.',
428 action='store_true')
429 parser.add_argument('--failed',
430 help='Prints only the failed parsed test results and summary line.'
431 'Does nothing if --raw_output is set.',
432 action='store_true')
433
434
435def tree_from_args(cli_args: argparse.Namespace) -> kunit_kernel.LinuxSourceTree:
436 """Returns a LinuxSourceTree based on the user's arguments."""
437 # Allow users to specify multiple arguments in one string, e.g. '-smp 8'
438 qemu_args: List[str] = []
439 if cli_args.qemu_args:
440 for arg in cli_args.qemu_args:
441 qemu_args.extend(shlex.split(arg))
442
443 kunitconfigs = cli_args.kunitconfig if cli_args.kunitconfig else []
444 if cli_args.alltests:
445 # Prepend so user-specified options take prio if we ever allow
446 # --kunitconfig options to have differing options.
447 kunitconfigs = [kunit_kernel.ALL_TESTS_CONFIG_PATH] + kunitconfigs
448
449 return kunit_kernel.LinuxSourceTree(cli_args.build_dir,
450 kunitconfig_paths=kunitconfigs,
451 kconfig_add=cli_args.kconfig_add,
452 arch=cli_args.arch,
453 cross_compile=cli_args.cross_compile,
454 qemu_config_path=cli_args.qemu_config,
455 extra_qemu_args=qemu_args)
456
457
458def run_handler(cli_args: argparse.Namespace) -> None:
459 if not os.path.exists(cli_args.build_dir):
460 os.mkdir(cli_args.build_dir)
461
462 linux = tree_from_args(cli_args)
463 request = KunitRequest(build_dir=cli_args.build_dir,
464 make_options=cli_args.make_options,
465 jobs=cli_args.jobs,
466 raw_output=cli_args.raw_output,
467 json=cli_args.json,
468 summary=cli_args.summary,
469 failed=cli_args.failed,
470 timeout=cli_args.timeout,
471 filter_glob=cli_args.filter_glob,
472 filter=cli_args.filter,
473 filter_action=cli_args.filter_action,
474 kernel_args=cli_args.kernel_args,
475 run_isolated=cli_args.run_isolated,
476 list_tests=cli_args.list_tests,
477 list_tests_attr=cli_args.list_tests_attr)
478 result = run_tests(linux, request)
479 if result.status != KunitStatus.SUCCESS:
480 sys.exit(1)
481
482
483def config_handler(cli_args: argparse.Namespace) -> None:
484 if cli_args.build_dir and (
485 not os.path.exists(cli_args.build_dir)):
486 os.mkdir(cli_args.build_dir)
487
488 linux = tree_from_args(cli_args)
489 request = KunitConfigRequest(build_dir=cli_args.build_dir,
490 make_options=cli_args.make_options)
491 result = config_tests(linux, request)
492 stdout.print_with_timestamp((
493 'Elapsed time: %.3fs\n') % (
494 result.elapsed_time))
495 if result.status != KunitStatus.SUCCESS:
496 sys.exit(1)
497
498
499def build_handler(cli_args: argparse.Namespace) -> None:
500 linux = tree_from_args(cli_args)
501 request = KunitBuildRequest(build_dir=cli_args.build_dir,
502 make_options=cli_args.make_options,
503 jobs=cli_args.jobs)
504 result = config_and_build_tests(linux, request)
505 stdout.print_with_timestamp((
506 'Elapsed time: %.3fs\n') % (
507 result.elapsed_time))
508 if result.status != KunitStatus.SUCCESS:
509 sys.exit(1)
510
511
512def exec_handler(cli_args: argparse.Namespace) -> None:
513 linux = tree_from_args(cli_args)
514 exec_request = KunitExecRequest(raw_output=cli_args.raw_output,
515 build_dir=cli_args.build_dir,
516 json=cli_args.json,
517 summary=cli_args.summary,
518 failed=cli_args.failed,
519 timeout=cli_args.timeout,
520 filter_glob=cli_args.filter_glob,
521 filter=cli_args.filter,
522 filter_action=cli_args.filter_action,
523 kernel_args=cli_args.kernel_args,
524 run_isolated=cli_args.run_isolated,
525 list_tests=cli_args.list_tests,
526 list_tests_attr=cli_args.list_tests_attr)
527 result = exec_tests(linux, exec_request)
528 stdout.print_with_timestamp((
529 'Elapsed time: %.3fs\n') % (result.elapsed_time))
530 if result.status != KunitStatus.SUCCESS:
531 sys.exit(1)
532
533
534def parse_handler(cli_args: argparse.Namespace) -> None:
535 if cli_args.file is None:
536 sys.stdin.reconfigure(errors='backslashreplace') # type: ignore
537 kunit_output = sys.stdin # type: Iterable[str]
538 else:
539 with open(cli_args.file, 'r', errors='backslashreplace') as f:
540 kunit_output = f.read().splitlines()
541 # We know nothing about how the result was created!
542 metadata = kunit_json.Metadata()
543 request = KunitParseRequest(raw_output=cli_args.raw_output,
544 json=cli_args.json, summary=cli_args.summary,
545 failed=cli_args.failed)
546 result, _ = parse_tests(request, metadata, kunit_output)
547 if result.status != KunitStatus.SUCCESS:
548 sys.exit(1)
549
550
551subcommand_handlers_map = {
552 'run': run_handler,
553 'config': config_handler,
554 'build': build_handler,
555 'exec': exec_handler,
556 'parse': parse_handler
557}
558
559
560def main(argv: Sequence[str]) -> None:
561 parser = argparse.ArgumentParser(
562 description='Helps writing and running KUnit tests.')
563 subparser = parser.add_subparsers(dest='subcommand')
564
565 # The 'run' command will config, build, exec, and parse in one go.
566 run_parser = subparser.add_parser('run', help='Runs KUnit tests.')
567 add_common_opts(run_parser)
568 add_build_opts(run_parser)
569 add_exec_opts(run_parser)
570 add_parse_opts(run_parser)
571
572 config_parser = subparser.add_parser('config',
573 help='Ensures that .config contains all of '
574 'the options in .kunitconfig')
575 add_common_opts(config_parser)
576
577 build_parser = subparser.add_parser('build', help='Builds a kernel with KUnit tests')
578 add_common_opts(build_parser)
579 add_build_opts(build_parser)
580
581 exec_parser = subparser.add_parser('exec', help='Run a kernel with KUnit tests')
582 add_common_opts(exec_parser)
583 add_exec_opts(exec_parser)
584 add_parse_opts(exec_parser)
585
586 # The 'parse' option is special, as it doesn't need the kernel source
587 # (therefore there is no need for a build_dir, hence no add_common_opts)
588 # and the '--file' argument is not relevant to 'run', so isn't in
589 # add_parse_opts()
590 parse_parser = subparser.add_parser('parse',
591 help='Parses KUnit results from a file, '
592 'and parses formatted results.')
593 add_parse_opts(parse_parser)
594 parse_parser.add_argument('file',
595 help='Specifies the file to read results from.',
596 type=str, nargs='?', metavar='input_file')
597
598 cli_args = parser.parse_args(massage_argv(argv))
599
600 if get_kernel_root_path():
601 os.chdir(get_kernel_root_path())
602
603 subcomand_handler = subcommand_handlers_map.get(cli_args.subcommand, None)
604
605 if subcomand_handler is None:
606 parser.print_help()
607 return
608
609 subcomand_handler(cli_args)
610
611
612if __name__ == '__main__':
613 main(sys.argv[1:])
1#!/usr/bin/env python3
2# SPDX-License-Identifier: GPL-2.0
3#
4# A thin wrapper on top of the KUnit Kernel
5#
6# Copyright (C) 2019, Google LLC.
7# Author: Felix Guo <felixguoxiuping@gmail.com>
8# Author: Brendan Higgins <brendanhiggins@google.com>
9
10import argparse
11import sys
12import os
13import time
14
15assert sys.version_info >= (3, 7), "Python version is too old"
16
17from collections import namedtuple
18from enum import Enum, auto
19
20import kunit_config
21import kunit_json
22import kunit_kernel
23import kunit_parser
24
25KunitResult = namedtuple('KunitResult', ['status','result','elapsed_time'])
26
27KunitConfigRequest = namedtuple('KunitConfigRequest',
28 ['build_dir', 'make_options'])
29KunitBuildRequest = namedtuple('KunitBuildRequest',
30 ['jobs', 'build_dir', 'alltests',
31 'make_options'])
32KunitExecRequest = namedtuple('KunitExecRequest',
33 ['timeout', 'build_dir', 'alltests', 'filter_glob'])
34KunitParseRequest = namedtuple('KunitParseRequest',
35 ['raw_output', 'input_data', 'build_dir', 'json'])
36KunitRequest = namedtuple('KunitRequest', ['raw_output','timeout', 'jobs',
37 'build_dir', 'alltests', 'filter_glob',
38 'json', 'make_options'])
39
40KernelDirectoryPath = sys.argv[0].split('tools/testing/kunit/')[0]
41
42class KunitStatus(Enum):
43 SUCCESS = auto()
44 CONFIG_FAILURE = auto()
45 BUILD_FAILURE = auto()
46 TEST_FAILURE = auto()
47
48def get_kernel_root_path() -> str:
49 path = sys.argv[0] if not __file__ else __file__
50 parts = os.path.realpath(path).split('tools/testing/kunit')
51 if len(parts) != 2:
52 sys.exit(1)
53 return parts[0]
54
55def config_tests(linux: kunit_kernel.LinuxSourceTree,
56 request: KunitConfigRequest) -> KunitResult:
57 kunit_parser.print_with_timestamp('Configuring KUnit Kernel ...')
58
59 config_start = time.time()
60 success = linux.build_reconfig(request.build_dir, request.make_options)
61 config_end = time.time()
62 if not success:
63 return KunitResult(KunitStatus.CONFIG_FAILURE,
64 'could not configure kernel',
65 config_end - config_start)
66 return KunitResult(KunitStatus.SUCCESS,
67 'configured kernel successfully',
68 config_end - config_start)
69
70def build_tests(linux: kunit_kernel.LinuxSourceTree,
71 request: KunitBuildRequest) -> KunitResult:
72 kunit_parser.print_with_timestamp('Building KUnit Kernel ...')
73
74 build_start = time.time()
75 success = linux.build_kernel(request.alltests,
76 request.jobs,
77 request.build_dir,
78 request.make_options)
79 build_end = time.time()
80 if not success:
81 return KunitResult(KunitStatus.BUILD_FAILURE,
82 'could not build kernel',
83 build_end - build_start)
84 if not success:
85 return KunitResult(KunitStatus.BUILD_FAILURE,
86 'could not build kernel',
87 build_end - build_start)
88 return KunitResult(KunitStatus.SUCCESS,
89 'built kernel successfully',
90 build_end - build_start)
91
92def exec_tests(linux: kunit_kernel.LinuxSourceTree,
93 request: KunitExecRequest) -> KunitResult:
94 kunit_parser.print_with_timestamp('Starting KUnit Kernel ...')
95 test_start = time.time()
96 result = linux.run_kernel(
97 timeout=None if request.alltests else request.timeout,
98 filter_glob=request.filter_glob,
99 build_dir=request.build_dir)
100
101 test_end = time.time()
102
103 return KunitResult(KunitStatus.SUCCESS,
104 result,
105 test_end - test_start)
106
107def parse_tests(request: KunitParseRequest) -> KunitResult:
108 parse_start = time.time()
109
110 test_result = kunit_parser.TestResult(kunit_parser.TestStatus.SUCCESS,
111 [],
112 'Tests not Parsed.')
113
114 if request.raw_output:
115 kunit_parser.raw_output(request.input_data)
116 else:
117 test_result = kunit_parser.parse_run_tests(request.input_data)
118 parse_end = time.time()
119
120 if request.json:
121 json_obj = kunit_json.get_json_result(
122 test_result=test_result,
123 def_config='kunit_defconfig',
124 build_dir=request.build_dir,
125 json_path=request.json)
126 if request.json == 'stdout':
127 print(json_obj)
128
129 if test_result.status != kunit_parser.TestStatus.SUCCESS:
130 return KunitResult(KunitStatus.TEST_FAILURE, test_result,
131 parse_end - parse_start)
132
133 return KunitResult(KunitStatus.SUCCESS, test_result,
134 parse_end - parse_start)
135
136
137def run_tests(linux: kunit_kernel.LinuxSourceTree,
138 request: KunitRequest) -> KunitResult:
139 run_start = time.time()
140
141 config_request = KunitConfigRequest(request.build_dir,
142 request.make_options)
143 config_result = config_tests(linux, config_request)
144 if config_result.status != KunitStatus.SUCCESS:
145 return config_result
146
147 build_request = KunitBuildRequest(request.jobs, request.build_dir,
148 request.alltests,
149 request.make_options)
150 build_result = build_tests(linux, build_request)
151 if build_result.status != KunitStatus.SUCCESS:
152 return build_result
153
154 exec_request = KunitExecRequest(request.timeout, request.build_dir,
155 request.alltests, request.filter_glob)
156 exec_result = exec_tests(linux, exec_request)
157 if exec_result.status != KunitStatus.SUCCESS:
158 return exec_result
159
160 parse_request = KunitParseRequest(request.raw_output,
161 exec_result.result,
162 request.build_dir,
163 request.json)
164 parse_result = parse_tests(parse_request)
165
166 run_end = time.time()
167
168 kunit_parser.print_with_timestamp((
169 'Elapsed time: %.3fs total, %.3fs configuring, %.3fs ' +
170 'building, %.3fs running\n') % (
171 run_end - run_start,
172 config_result.elapsed_time,
173 build_result.elapsed_time,
174 exec_result.elapsed_time))
175 return parse_result
176
177def add_common_opts(parser) -> None:
178 parser.add_argument('--build_dir',
179 help='As in the make command, it specifies the build '
180 'directory.',
181 type=str, default='.kunit', metavar='build_dir')
182 parser.add_argument('--make_options',
183 help='X=Y make option, can be repeated.',
184 action='append')
185 parser.add_argument('--alltests',
186 help='Run all KUnit tests through allyesconfig',
187 action='store_true')
188 parser.add_argument('--kunitconfig',
189 help='Path to Kconfig fragment that enables KUnit tests.'
190 ' If given a directory, (e.g. lib/kunit), "/.kunitconfig" '
191 'will get automatically appended.',
192 metavar='kunitconfig')
193
194 parser.add_argument('--arch',
195 help=('Specifies the architecture to run tests under. '
196 'The architecture specified here must match the '
197 'string passed to the ARCH make param, '
198 'e.g. i386, x86_64, arm, um, etc. Non-UML '
199 'architectures run on QEMU.'),
200 type=str, default='um', metavar='arch')
201
202 parser.add_argument('--cross_compile',
203 help=('Sets make\'s CROSS_COMPILE variable; it should '
204 'be set to a toolchain path prefix (the prefix '
205 'of gcc and other tools in your toolchain, for '
206 'example `sparc64-linux-gnu-` if you have the '
207 'sparc toolchain installed on your system, or '
208 '`$HOME/toolchains/microblaze/gcc-9.2.0-nolibc/microblaze-linux/bin/microblaze-linux-` '
209 'if you have downloaded the microblaze toolchain '
210 'from the 0-day website to a directory in your '
211 'home directory called `toolchains`).'),
212 metavar='cross_compile')
213
214 parser.add_argument('--qemu_config',
215 help=('Takes a path to a path to a file containing '
216 'a QemuArchParams object.'),
217 type=str, metavar='qemu_config')
218
219def add_build_opts(parser) -> None:
220 parser.add_argument('--jobs',
221 help='As in the make command, "Specifies the number of '
222 'jobs (commands) to run simultaneously."',
223 type=int, default=8, metavar='jobs')
224
225def add_exec_opts(parser) -> None:
226 parser.add_argument('--timeout',
227 help='maximum number of seconds to allow for all tests '
228 'to run. This does not include time taken to build the '
229 'tests.',
230 type=int,
231 default=300,
232 metavar='timeout')
233 parser.add_argument('filter_glob',
234 help='maximum number of seconds to allow for all tests '
235 'to run. This does not include time taken to build the '
236 'tests.',
237 type=str,
238 nargs='?',
239 default='',
240 metavar='filter_glob')
241
242def add_parse_opts(parser) -> None:
243 parser.add_argument('--raw_output', help='don\'t format output from kernel',
244 action='store_true')
245 parser.add_argument('--json',
246 nargs='?',
247 help='Stores test results in a JSON, and either '
248 'prints to stdout or saves to file if a '
249 'filename is specified',
250 type=str, const='stdout', default=None)
251
252def main(argv, linux=None):
253 parser = argparse.ArgumentParser(
254 description='Helps writing and running KUnit tests.')
255 subparser = parser.add_subparsers(dest='subcommand')
256
257 # The 'run' command will config, build, exec, and parse in one go.
258 run_parser = subparser.add_parser('run', help='Runs KUnit tests.')
259 add_common_opts(run_parser)
260 add_build_opts(run_parser)
261 add_exec_opts(run_parser)
262 add_parse_opts(run_parser)
263
264 config_parser = subparser.add_parser('config',
265 help='Ensures that .config contains all of '
266 'the options in .kunitconfig')
267 add_common_opts(config_parser)
268
269 build_parser = subparser.add_parser('build', help='Builds a kernel with KUnit tests')
270 add_common_opts(build_parser)
271 add_build_opts(build_parser)
272
273 exec_parser = subparser.add_parser('exec', help='Run a kernel with KUnit tests')
274 add_common_opts(exec_parser)
275 add_exec_opts(exec_parser)
276 add_parse_opts(exec_parser)
277
278 # The 'parse' option is special, as it doesn't need the kernel source
279 # (therefore there is no need for a build_dir, hence no add_common_opts)
280 # and the '--file' argument is not relevant to 'run', so isn't in
281 # add_parse_opts()
282 parse_parser = subparser.add_parser('parse',
283 help='Parses KUnit results from a file, '
284 'and parses formatted results.')
285 add_parse_opts(parse_parser)
286 parse_parser.add_argument('file',
287 help='Specifies the file to read results from.',
288 type=str, nargs='?', metavar='input_file')
289
290 cli_args = parser.parse_args(argv)
291
292 if get_kernel_root_path():
293 os.chdir(get_kernel_root_path())
294
295 if cli_args.subcommand == 'run':
296 if not os.path.exists(cli_args.build_dir):
297 os.mkdir(cli_args.build_dir)
298
299 if not linux:
300 linux = kunit_kernel.LinuxSourceTree(cli_args.build_dir,
301 kunitconfig_path=cli_args.kunitconfig,
302 arch=cli_args.arch,
303 cross_compile=cli_args.cross_compile,
304 qemu_config_path=cli_args.qemu_config)
305
306 request = KunitRequest(cli_args.raw_output,
307 cli_args.timeout,
308 cli_args.jobs,
309 cli_args.build_dir,
310 cli_args.alltests,
311 cli_args.filter_glob,
312 cli_args.json,
313 cli_args.make_options)
314 result = run_tests(linux, request)
315 if result.status != KunitStatus.SUCCESS:
316 sys.exit(1)
317 elif cli_args.subcommand == 'config':
318 if cli_args.build_dir and (
319 not os.path.exists(cli_args.build_dir)):
320 os.mkdir(cli_args.build_dir)
321
322 if not linux:
323 linux = kunit_kernel.LinuxSourceTree(cli_args.build_dir,
324 kunitconfig_path=cli_args.kunitconfig,
325 arch=cli_args.arch,
326 cross_compile=cli_args.cross_compile,
327 qemu_config_path=cli_args.qemu_config)
328
329 request = KunitConfigRequest(cli_args.build_dir,
330 cli_args.make_options)
331 result = config_tests(linux, request)
332 kunit_parser.print_with_timestamp((
333 'Elapsed time: %.3fs\n') % (
334 result.elapsed_time))
335 if result.status != KunitStatus.SUCCESS:
336 sys.exit(1)
337 elif cli_args.subcommand == 'build':
338 if not linux:
339 linux = kunit_kernel.LinuxSourceTree(cli_args.build_dir,
340 kunitconfig_path=cli_args.kunitconfig,
341 arch=cli_args.arch,
342 cross_compile=cli_args.cross_compile,
343 qemu_config_path=cli_args.qemu_config)
344
345 request = KunitBuildRequest(cli_args.jobs,
346 cli_args.build_dir,
347 cli_args.alltests,
348 cli_args.make_options)
349 result = build_tests(linux, request)
350 kunit_parser.print_with_timestamp((
351 'Elapsed time: %.3fs\n') % (
352 result.elapsed_time))
353 if result.status != KunitStatus.SUCCESS:
354 sys.exit(1)
355 elif cli_args.subcommand == 'exec':
356 if not linux:
357 linux = kunit_kernel.LinuxSourceTree(cli_args.build_dir,
358 kunitconfig_path=cli_args.kunitconfig,
359 arch=cli_args.arch,
360 cross_compile=cli_args.cross_compile,
361 qemu_config_path=cli_args.qemu_config)
362
363 exec_request = KunitExecRequest(cli_args.timeout,
364 cli_args.build_dir,
365 cli_args.alltests,
366 cli_args.filter_glob)
367 exec_result = exec_tests(linux, exec_request)
368 parse_request = KunitParseRequest(cli_args.raw_output,
369 exec_result.result,
370 cli_args.build_dir,
371 cli_args.json)
372 result = parse_tests(parse_request)
373 kunit_parser.print_with_timestamp((
374 'Elapsed time: %.3fs\n') % (
375 exec_result.elapsed_time))
376 if result.status != KunitStatus.SUCCESS:
377 sys.exit(1)
378 elif cli_args.subcommand == 'parse':
379 if cli_args.file == None:
380 kunit_output = sys.stdin
381 else:
382 with open(cli_args.file, 'r') as f:
383 kunit_output = f.read().splitlines()
384 request = KunitParseRequest(cli_args.raw_output,
385 kunit_output,
386 None,
387 cli_args.json)
388 result = parse_tests(request)
389 if result.status != KunitStatus.SUCCESS:
390 sys.exit(1)
391 else:
392 parser.print_help()
393
394if __name__ == '__main__':
395 main(sys.argv[1:])