kunit.py 22 KB

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