kunit.py 20 KB

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