1c25ce589SFinn Behrens#!/usr/bin/env python3 26ebf5866SFelix Guo# SPDX-License-Identifier: GPL-2.0 36ebf5866SFelix Guo# 46ebf5866SFelix Guo# A thin wrapper on top of the KUnit Kernel 56ebf5866SFelix Guo# 66ebf5866SFelix Guo# Copyright (C) 2019, Google LLC. 76ebf5866SFelix Guo# Author: Felix Guo <felixguoxiuping@gmail.com> 86ebf5866SFelix Guo# Author: Brendan Higgins <brendanhiggins@google.com> 96ebf5866SFelix Guo 106ebf5866SFelix Guoimport argparse 116ebf5866SFelix Guoimport os 12ff9e09a3SDaniel Latypovimport re 13ff9e09a3SDaniel Latypovimport sys 146ebf5866SFelix Guoimport time 156ebf5866SFelix Guo 16df4b0807SSeongJae Parkassert sys.version_info >= (3, 7), "Python version is too old" 17df4b0807SSeongJae Park 18*db167981SDaniel Latypovfrom dataclasses import dataclass 196ebf5866SFelix Guofrom enum import Enum, auto 20*db167981SDaniel Latypovfrom typing import Any, Iterable, Sequence, List, Optional 216ebf5866SFelix Guo 2221a6d178SHeidi Fahimimport kunit_json 236ebf5866SFelix Guoimport kunit_kernel 246ebf5866SFelix Guoimport kunit_parser 256ebf5866SFelix Guo 266ebf5866SFelix Guoclass KunitStatus(Enum): 276ebf5866SFelix Guo SUCCESS = auto() 286ebf5866SFelix Guo CONFIG_FAILURE = auto() 296ebf5866SFelix Guo BUILD_FAILURE = auto() 306ebf5866SFelix Guo TEST_FAILURE = auto() 316ebf5866SFelix Guo 32*db167981SDaniel Latypov@dataclass 33*db167981SDaniel Latypovclass KunitResult: 34*db167981SDaniel Latypov status: KunitStatus 35*db167981SDaniel Latypov result: Any 36*db167981SDaniel Latypov elapsed_time: float 37*db167981SDaniel Latypov 38*db167981SDaniel Latypov@dataclass 39*db167981SDaniel Latypovclass KunitConfigRequest: 40*db167981SDaniel Latypov build_dir: str 41*db167981SDaniel Latypov make_options: Optional[List[str]] 42*db167981SDaniel Latypov 43*db167981SDaniel Latypov@dataclass 44*db167981SDaniel Latypovclass KunitBuildRequest(KunitConfigRequest): 45*db167981SDaniel Latypov jobs: int 46*db167981SDaniel Latypov alltests: bool 47*db167981SDaniel Latypov 48*db167981SDaniel Latypov@dataclass 49*db167981SDaniel Latypovclass KunitParseRequest: 50*db167981SDaniel Latypov raw_output: Optional[str] 51*db167981SDaniel Latypov build_dir: str 52*db167981SDaniel Latypov json: Optional[str] 53*db167981SDaniel Latypov 54*db167981SDaniel Latypov@dataclass 55*db167981SDaniel Latypovclass KunitExecRequest(KunitParseRequest): 56*db167981SDaniel Latypov timeout: int 57*db167981SDaniel Latypov alltests: bool 58*db167981SDaniel Latypov filter_glob: str 59*db167981SDaniel Latypov kernel_args: Optional[List[str]] 60*db167981SDaniel Latypov run_isolated: Optional[str] 61*db167981SDaniel Latypov 62*db167981SDaniel Latypov@dataclass 63*db167981SDaniel Latypovclass KunitRequest(KunitExecRequest, KunitBuildRequest): 64*db167981SDaniel Latypov pass 65*db167981SDaniel Latypov 66*db167981SDaniel Latypov 67*db167981SDaniel LatypovKernelDirectoryPath = sys.argv[0].split('tools/testing/kunit/')[0] 68*db167981SDaniel Latypov 6909641f7cSDaniel Latypovdef get_kernel_root_path() -> str: 7009641f7cSDaniel Latypov path = sys.argv[0] if not __file__ else __file__ 7109641f7cSDaniel Latypov parts = os.path.realpath(path).split('tools/testing/kunit') 72be886ba9SHeidi Fahim if len(parts) != 2: 73be886ba9SHeidi Fahim sys.exit(1) 74be886ba9SHeidi Fahim return parts[0] 75be886ba9SHeidi Fahim 7645ba7a89SDavid Gowdef config_tests(linux: kunit_kernel.LinuxSourceTree, 7745ba7a89SDavid Gow request: KunitConfigRequest) -> KunitResult: 7845ba7a89SDavid Gow kunit_parser.print_with_timestamp('Configuring KUnit Kernel ...') 7945ba7a89SDavid Gow 806ebf5866SFelix Guo config_start = time.time() 810476e69fSGreg Thelen success = linux.build_reconfig(request.build_dir, request.make_options) 826ebf5866SFelix Guo config_end = time.time() 836ebf5866SFelix Guo if not success: 8445ba7a89SDavid Gow return KunitResult(KunitStatus.CONFIG_FAILURE, 8545ba7a89SDavid Gow 'could not configure kernel', 8645ba7a89SDavid Gow config_end - config_start) 8745ba7a89SDavid Gow return KunitResult(KunitStatus.SUCCESS, 8845ba7a89SDavid Gow 'configured kernel successfully', 8945ba7a89SDavid Gow config_end - config_start) 906ebf5866SFelix Guo 9145ba7a89SDavid Gowdef build_tests(linux: kunit_kernel.LinuxSourceTree, 9245ba7a89SDavid Gow request: KunitBuildRequest) -> KunitResult: 936ebf5866SFelix Guo kunit_parser.print_with_timestamp('Building KUnit Kernel ...') 946ebf5866SFelix Guo 956ebf5866SFelix Guo build_start = time.time() 9687c9c163SBrendan Higgins success = linux.build_kernel(request.alltests, 97021ed9f5SHeidi Fahim request.jobs, 980476e69fSGreg Thelen request.build_dir, 990476e69fSGreg Thelen request.make_options) 1006ebf5866SFelix Guo build_end = time.time() 1016ebf5866SFelix Guo if not success: 102ee61492aSDavid Gow return KunitResult(KunitStatus.BUILD_FAILURE, 103ee61492aSDavid Gow 'could not build kernel', 104ee61492aSDavid Gow build_end - build_start) 10545ba7a89SDavid Gow if not success: 10645ba7a89SDavid Gow return KunitResult(KunitStatus.BUILD_FAILURE, 10745ba7a89SDavid Gow 'could not build kernel', 10845ba7a89SDavid Gow build_end - build_start) 10945ba7a89SDavid Gow return KunitResult(KunitStatus.SUCCESS, 11045ba7a89SDavid Gow 'built kernel successfully', 11145ba7a89SDavid Gow build_end - build_start) 1126ebf5866SFelix Guo 113ff9e09a3SDaniel Latypovdef _list_tests(linux: kunit_kernel.LinuxSourceTree, request: KunitExecRequest) -> List[str]: 114ff9e09a3SDaniel Latypov args = ['kunit.action=list'] 115ff9e09a3SDaniel Latypov if request.kernel_args: 116ff9e09a3SDaniel Latypov args.extend(request.kernel_args) 117ff9e09a3SDaniel Latypov 118ff9e09a3SDaniel Latypov output = linux.run_kernel(args=args, 119ff9e09a3SDaniel Latypov timeout=None if request.alltests else request.timeout, 120ff9e09a3SDaniel Latypov filter_glob=request.filter_glob, 121ff9e09a3SDaniel Latypov build_dir=request.build_dir) 122ff9e09a3SDaniel Latypov lines = kunit_parser.extract_tap_lines(output) 123ff9e09a3SDaniel Latypov # Hack! Drop the dummy TAP version header that the executor prints out. 124ff9e09a3SDaniel Latypov lines.pop() 125ff9e09a3SDaniel Latypov 126ff9e09a3SDaniel Latypov # Filter out any extraneous non-test output that might have gotten mixed in. 127ff9e09a3SDaniel Latypov return [l for l in lines if re.match('^[^\s.]+\.[^\s.]+$', l)] 128ff9e09a3SDaniel Latypov 129ff9e09a3SDaniel Latypovdef _suites_from_test_list(tests: List[str]) -> List[str]: 130ff9e09a3SDaniel Latypov """Extracts all the suites from an ordered list of tests.""" 131ff9e09a3SDaniel Latypov suites = [] # type: List[str] 132ff9e09a3SDaniel Latypov for t in tests: 133ff9e09a3SDaniel Latypov parts = t.split('.', maxsplit=2) 134ff9e09a3SDaniel Latypov if len(parts) != 2: 135ff9e09a3SDaniel Latypov raise ValueError(f'internal KUnit error, test name should be of the form "<suite>.<test>", got "{t}"') 136ff9e09a3SDaniel Latypov suite, case = parts 137ff9e09a3SDaniel Latypov if not suites or suites[-1] != suite: 138ff9e09a3SDaniel Latypov suites.append(suite) 139ff9e09a3SDaniel Latypov return suites 140ff9e09a3SDaniel Latypov 141ff9e09a3SDaniel Latypov 142ff9e09a3SDaniel Latypov 143*db167981SDaniel Latypovdef exec_tests(linux: kunit_kernel.LinuxSourceTree, request: KunitExecRequest) -> KunitResult: 144ff9e09a3SDaniel Latypov filter_globs = [request.filter_glob] 145ff9e09a3SDaniel Latypov if request.run_isolated: 146ff9e09a3SDaniel Latypov tests = _list_tests(linux, request) 147ff9e09a3SDaniel Latypov if request.run_isolated == 'test': 148ff9e09a3SDaniel Latypov filter_globs = tests 149ff9e09a3SDaniel Latypov if request.run_isolated == 'suite': 150ff9e09a3SDaniel Latypov filter_globs = _suites_from_test_list(tests) 151ff9e09a3SDaniel Latypov # Apply the test-part of the user's glob, if present. 152ff9e09a3SDaniel Latypov if '.' in request.filter_glob: 153ff9e09a3SDaniel Latypov test_glob = request.filter_glob.split('.', maxsplit=2)[1] 154ff9e09a3SDaniel Latypov filter_globs = [g + '.'+ test_glob for g in filter_globs] 155ff9e09a3SDaniel Latypov 156d65d07cbSRae Moar test_counts = kunit_parser.TestCounts() 157ff9e09a3SDaniel Latypov exec_time = 0.0 158ff9e09a3SDaniel Latypov for i, filter_glob in enumerate(filter_globs): 159ff9e09a3SDaniel Latypov kunit_parser.print_with_timestamp('Starting KUnit Kernel ({}/{})...'.format(i+1, len(filter_globs))) 160ff9e09a3SDaniel Latypov 1616ebf5866SFelix Guo test_start = time.time() 1627ef925eaSDaniel Latypov run_result = linux.run_kernel( 1636cb51a18SDaniel Latypov args=request.kernel_args, 164021ed9f5SHeidi Fahim timeout=None if request.alltests else request.timeout, 165ff9e09a3SDaniel Latypov filter_glob=filter_glob, 1666ec1b81dSSeongJae Park build_dir=request.build_dir) 16745ba7a89SDavid Gow 168*db167981SDaniel Latypov result = parse_tests(request, run_result) 1695f6aa6d8SDaniel Latypov # run_kernel() doesn't block on the kernel exiting. 1705f6aa6d8SDaniel Latypov # That only happens after we get the last line of output from `run_result`. 1715f6aa6d8SDaniel Latypov # So exec_time here actually contains parsing + execution time, which is fine. 1726ebf5866SFelix Guo test_end = time.time() 173ff9e09a3SDaniel Latypov exec_time += test_end - test_start 174ff9e09a3SDaniel Latypov 175d65d07cbSRae Moar test_counts.add_subtest_counts(result.result.test.counts) 1766ebf5866SFelix Guo 1777fa7ffcfSDaniel Latypov if len(filter_globs) == 1 and test_counts.crashed > 0: 1787fa7ffcfSDaniel Latypov bd = request.build_dir 1797fa7ffcfSDaniel Latypov print('The kernel seems to have crashed; you can decode the stack traces with:') 1807fa7ffcfSDaniel Latypov print('$ scripts/decode_stacktrace.sh {}/vmlinux {} < {} | tee {}/decoded.log | {} parse'.format( 1817fa7ffcfSDaniel Latypov bd, bd, kunit_kernel.get_outfile_path(bd), bd, sys.argv[0])) 1827fa7ffcfSDaniel Latypov 183d65d07cbSRae Moar kunit_status = _map_to_overall_status(test_counts.get_status()) 184d65d07cbSRae Moar return KunitResult(status=kunit_status, result=result.result, elapsed_time=exec_time) 185d65d07cbSRae Moar 186d65d07cbSRae Moardef _map_to_overall_status(test_status: kunit_parser.TestStatus) -> KunitStatus: 187d65d07cbSRae Moar if test_status in (kunit_parser.TestStatus.SUCCESS, kunit_parser.TestStatus.SKIPPED): 188d65d07cbSRae Moar return KunitStatus.SUCCESS 189d65d07cbSRae Moar else: 190d65d07cbSRae Moar return KunitStatus.TEST_FAILURE 1917ef925eaSDaniel Latypov 1927ef925eaSDaniel Latypovdef parse_tests(request: KunitParseRequest, input_data: Iterable[str]) -> KunitResult: 19345ba7a89SDavid Gow parse_start = time.time() 19445ba7a89SDavid Gow 19545ba7a89SDavid Gow test_result = kunit_parser.TestResult(kunit_parser.TestStatus.SUCCESS, 196d65d07cbSRae Moar kunit_parser.Test(), 19745ba7a89SDavid Gow 'Tests not Parsed.') 19821a6d178SHeidi Fahim 19945ba7a89SDavid Gow if request.raw_output: 200d65d07cbSRae Moar # Treat unparsed results as one passing test. 201d65d07cbSRae Moar test_result.test.status = kunit_parser.TestStatus.SUCCESS 202d65d07cbSRae Moar test_result.test.counts.passed = 1 203d65d07cbSRae Moar 2047ef925eaSDaniel Latypov output: Iterable[str] = input_data 2056a499c9cSDaniel Latypov if request.raw_output == 'all': 2066a499c9cSDaniel Latypov pass 2076a499c9cSDaniel Latypov elif request.raw_output == 'kunit': 2086a499c9cSDaniel Latypov output = kunit_parser.extract_tap_lines(output) 2096a499c9cSDaniel Latypov else: 2106a499c9cSDaniel Latypov print(f'Unknown --raw_output option "{request.raw_output}"', file=sys.stderr) 2116a499c9cSDaniel Latypov for line in output: 2126a499c9cSDaniel Latypov print(line.rstrip()) 2136a499c9cSDaniel Latypov 21445ba7a89SDavid Gow else: 2157ef925eaSDaniel Latypov test_result = kunit_parser.parse_run_tests(input_data) 21645ba7a89SDavid Gow parse_end = time.time() 21745ba7a89SDavid Gow 21821a6d178SHeidi Fahim if request.json: 21921a6d178SHeidi Fahim json_obj = kunit_json.get_json_result( 22021a6d178SHeidi Fahim test_result=test_result, 22121a6d178SHeidi Fahim def_config='kunit_defconfig', 22221a6d178SHeidi Fahim build_dir=request.build_dir, 22321a6d178SHeidi Fahim json_path=request.json) 22421a6d178SHeidi Fahim if request.json == 'stdout': 22521a6d178SHeidi Fahim print(json_obj) 22621a6d178SHeidi Fahim 22745ba7a89SDavid Gow if test_result.status != kunit_parser.TestStatus.SUCCESS: 22845ba7a89SDavid Gow return KunitResult(KunitStatus.TEST_FAILURE, test_result, 22945ba7a89SDavid Gow parse_end - parse_start) 23045ba7a89SDavid Gow 23145ba7a89SDavid Gow return KunitResult(KunitStatus.SUCCESS, test_result, 23245ba7a89SDavid Gow parse_end - parse_start) 23345ba7a89SDavid Gow 23445ba7a89SDavid Gowdef run_tests(linux: kunit_kernel.LinuxSourceTree, 23545ba7a89SDavid Gow request: KunitRequest) -> KunitResult: 23645ba7a89SDavid Gow run_start = time.time() 23745ba7a89SDavid Gow 238*db167981SDaniel Latypov config_result = config_tests(linux, request) 23945ba7a89SDavid Gow if config_result.status != KunitStatus.SUCCESS: 24045ba7a89SDavid Gow return config_result 24145ba7a89SDavid Gow 242*db167981SDaniel Latypov build_result = build_tests(linux, request) 24345ba7a89SDavid Gow if build_result.status != KunitStatus.SUCCESS: 24445ba7a89SDavid Gow return build_result 24545ba7a89SDavid Gow 246*db167981SDaniel Latypov exec_result = exec_tests(linux, request) 24745ba7a89SDavid Gow 24845ba7a89SDavid Gow run_end = time.time() 24945ba7a89SDavid Gow 2506ebf5866SFelix Guo kunit_parser.print_with_timestamp(( 2516ebf5866SFelix Guo 'Elapsed time: %.3fs total, %.3fs configuring, %.3fs ' + 2526ebf5866SFelix Guo 'building, %.3fs running\n') % ( 25345ba7a89SDavid Gow run_end - run_start, 25445ba7a89SDavid Gow config_result.elapsed_time, 25545ba7a89SDavid Gow build_result.elapsed_time, 25645ba7a89SDavid Gow exec_result.elapsed_time)) 2577ef925eaSDaniel Latypov return exec_result 2586ebf5866SFelix Guo 259d8c23eadSDaniel Latypov# Problem: 260d8c23eadSDaniel Latypov# $ kunit.py run --json 261d8c23eadSDaniel Latypov# works as one would expect and prints the parsed test results as JSON. 262d8c23eadSDaniel Latypov# $ kunit.py run --json suite_name 263d8c23eadSDaniel Latypov# would *not* pass suite_name as the filter_glob and print as json. 264d8c23eadSDaniel Latypov# argparse will consider it to be another way of writing 265d8c23eadSDaniel Latypov# $ kunit.py run --json=suite_name 266d8c23eadSDaniel Latypov# i.e. it would run all tests, and dump the json to a `suite_name` file. 267d8c23eadSDaniel Latypov# So we hackily automatically rewrite --json => --json=stdout 268d8c23eadSDaniel Latypovpseudo_bool_flag_defaults = { 269d8c23eadSDaniel Latypov '--json': 'stdout', 270d8c23eadSDaniel Latypov '--raw_output': 'kunit', 271d8c23eadSDaniel Latypov} 272d8c23eadSDaniel Latypovdef massage_argv(argv: Sequence[str]) -> Sequence[str]: 273d8c23eadSDaniel Latypov def massage_arg(arg: str) -> str: 274d8c23eadSDaniel Latypov if arg not in pseudo_bool_flag_defaults: 275d8c23eadSDaniel Latypov return arg 276d8c23eadSDaniel Latypov return f'{arg}={pseudo_bool_flag_defaults[arg]}' 277d8c23eadSDaniel Latypov return list(map(massage_arg, argv)) 278d8c23eadSDaniel Latypov 27909641f7cSDaniel Latypovdef add_common_opts(parser) -> None: 28045ba7a89SDavid Gow parser.add_argument('--build_dir', 28145ba7a89SDavid Gow help='As in the make command, it specifies the build ' 28245ba7a89SDavid Gow 'directory.', 283ddbd60c7SVitor Massaru Iha type=str, default='.kunit', metavar='build_dir') 28445ba7a89SDavid Gow parser.add_argument('--make_options', 28545ba7a89SDavid Gow help='X=Y make option, can be repeated.', 28645ba7a89SDavid Gow action='append') 28745ba7a89SDavid Gow parser.add_argument('--alltests', 28845ba7a89SDavid Gow help='Run all KUnit tests through allyesconfig', 2896ebf5866SFelix Guo action='store_true') 290243180f5SDaniel Latypov parser.add_argument('--kunitconfig', 2919854781dSDaniel Latypov help='Path to Kconfig fragment that enables KUnit tests.' 2929854781dSDaniel Latypov ' If given a directory, (e.g. lib/kunit), "/.kunitconfig" ' 2939854781dSDaniel Latypov 'will get automatically appended.', 294243180f5SDaniel Latypov metavar='kunitconfig') 2959f57cc76SDaniel Latypov parser.add_argument('--kconfig_add', 2969f57cc76SDaniel Latypov help='Additional Kconfig options to append to the ' 2979f57cc76SDaniel Latypov '.kunitconfig, e.g. CONFIG_KASAN=y. Can be repeated.', 2989f57cc76SDaniel Latypov action='append') 2996ebf5866SFelix Guo 30087c9c163SBrendan Higgins parser.add_argument('--arch', 30187c9c163SBrendan Higgins help=('Specifies the architecture to run tests under. ' 30287c9c163SBrendan Higgins 'The architecture specified here must match the ' 30387c9c163SBrendan Higgins 'string passed to the ARCH make param, ' 30487c9c163SBrendan Higgins 'e.g. i386, x86_64, arm, um, etc. Non-UML ' 30587c9c163SBrendan Higgins 'architectures run on QEMU.'), 30687c9c163SBrendan Higgins type=str, default='um', metavar='arch') 30787c9c163SBrendan Higgins 30887c9c163SBrendan Higgins parser.add_argument('--cross_compile', 30987c9c163SBrendan Higgins help=('Sets make\'s CROSS_COMPILE variable; it should ' 31087c9c163SBrendan Higgins 'be set to a toolchain path prefix (the prefix ' 31187c9c163SBrendan Higgins 'of gcc and other tools in your toolchain, for ' 31287c9c163SBrendan Higgins 'example `sparc64-linux-gnu-` if you have the ' 31387c9c163SBrendan Higgins 'sparc toolchain installed on your system, or ' 31487c9c163SBrendan Higgins '`$HOME/toolchains/microblaze/gcc-9.2.0-nolibc/microblaze-linux/bin/microblaze-linux-` ' 31587c9c163SBrendan Higgins 'if you have downloaded the microblaze toolchain ' 31687c9c163SBrendan Higgins 'from the 0-day website to a directory in your ' 31787c9c163SBrendan Higgins 'home directory called `toolchains`).'), 31887c9c163SBrendan Higgins metavar='cross_compile') 31987c9c163SBrendan Higgins 32087c9c163SBrendan Higgins parser.add_argument('--qemu_config', 32187c9c163SBrendan Higgins help=('Takes a path to a path to a file containing ' 32287c9c163SBrendan Higgins 'a QemuArchParams object.'), 32387c9c163SBrendan Higgins type=str, metavar='qemu_config') 32487c9c163SBrendan Higgins 32509641f7cSDaniel Latypovdef add_build_opts(parser) -> None: 32645ba7a89SDavid Gow parser.add_argument('--jobs', 32745ba7a89SDavid Gow help='As in the make command, "Specifies the number of ' 32845ba7a89SDavid Gow 'jobs (commands) to run simultaneously."', 32945ba7a89SDavid Gow type=int, default=8, metavar='jobs') 33045ba7a89SDavid Gow 33109641f7cSDaniel Latypovdef add_exec_opts(parser) -> None: 33245ba7a89SDavid Gow parser.add_argument('--timeout', 3336ebf5866SFelix Guo help='maximum number of seconds to allow for all tests ' 3346ebf5866SFelix Guo 'to run. This does not include time taken to build the ' 3356ebf5866SFelix Guo 'tests.', 3366ebf5866SFelix Guo type=int, 3376ebf5866SFelix Guo default=300, 3386ebf5866SFelix Guo metavar='timeout') 339d992880bSDaniel Latypov parser.add_argument('filter_glob', 340a127b154SDaniel Latypov help='Filter which KUnit test suites/tests run at ' 341a127b154SDaniel Latypov 'boot-time, e.g. list* or list*.*del_test', 342d992880bSDaniel Latypov type=str, 343d992880bSDaniel Latypov nargs='?', 344d992880bSDaniel Latypov default='', 345d992880bSDaniel Latypov metavar='filter_glob') 3466cb51a18SDaniel Latypov parser.add_argument('--kernel_args', 3476cb51a18SDaniel Latypov help='Kernel command-line parameters. Maybe be repeated', 3486cb51a18SDaniel Latypov action='append') 349ff9e09a3SDaniel Latypov parser.add_argument('--run_isolated', help='If set, boot the kernel for each ' 350ff9e09a3SDaniel Latypov 'individual suite/test. This is can be useful for debugging ' 351ff9e09a3SDaniel Latypov 'a non-hermetic test, one that might pass/fail based on ' 352ff9e09a3SDaniel Latypov 'what ran before it.', 353ff9e09a3SDaniel Latypov type=str, 354ff9e09a3SDaniel Latypov choices=['suite', 'test']), 3556ebf5866SFelix Guo 35609641f7cSDaniel Latypovdef add_parse_opts(parser) -> None: 3576a499c9cSDaniel Latypov parser.add_argument('--raw_output', help='If set don\'t format output from kernel. ' 3586a499c9cSDaniel Latypov 'If set to --raw_output=kunit, filters to just KUnit output.', 3596a499c9cSDaniel Latypov type=str, nargs='?', const='all', default=None) 36021a6d178SHeidi Fahim parser.add_argument('--json', 36121a6d178SHeidi Fahim nargs='?', 36221a6d178SHeidi Fahim help='Stores test results in a JSON, and either ' 36321a6d178SHeidi Fahim 'prints to stdout or saves to file if a ' 36421a6d178SHeidi Fahim 'filename is specified', 36521a6d178SHeidi Fahim type=str, const='stdout', default=None) 366021ed9f5SHeidi Fahim 36745ba7a89SDavid Gowdef main(argv, linux=None): 36845ba7a89SDavid Gow parser = argparse.ArgumentParser( 36945ba7a89SDavid Gow description='Helps writing and running KUnit tests.') 37045ba7a89SDavid Gow subparser = parser.add_subparsers(dest='subcommand') 37145ba7a89SDavid Gow 37245ba7a89SDavid Gow # The 'run' command will config, build, exec, and parse in one go. 37345ba7a89SDavid Gow run_parser = subparser.add_parser('run', help='Runs KUnit tests.') 37445ba7a89SDavid Gow add_common_opts(run_parser) 37545ba7a89SDavid Gow add_build_opts(run_parser) 37645ba7a89SDavid Gow add_exec_opts(run_parser) 37745ba7a89SDavid Gow add_parse_opts(run_parser) 37845ba7a89SDavid Gow 37945ba7a89SDavid Gow config_parser = subparser.add_parser('config', 38045ba7a89SDavid Gow help='Ensures that .config contains all of ' 38145ba7a89SDavid Gow 'the options in .kunitconfig') 38245ba7a89SDavid Gow add_common_opts(config_parser) 38345ba7a89SDavid Gow 38445ba7a89SDavid Gow build_parser = subparser.add_parser('build', help='Builds a kernel with KUnit tests') 38545ba7a89SDavid Gow add_common_opts(build_parser) 38645ba7a89SDavid Gow add_build_opts(build_parser) 38745ba7a89SDavid Gow 38845ba7a89SDavid Gow exec_parser = subparser.add_parser('exec', help='Run a kernel with KUnit tests') 38945ba7a89SDavid Gow add_common_opts(exec_parser) 39045ba7a89SDavid Gow add_exec_opts(exec_parser) 39145ba7a89SDavid Gow add_parse_opts(exec_parser) 39245ba7a89SDavid Gow 39345ba7a89SDavid Gow # The 'parse' option is special, as it doesn't need the kernel source 39445ba7a89SDavid Gow # (therefore there is no need for a build_dir, hence no add_common_opts) 39545ba7a89SDavid Gow # and the '--file' argument is not relevant to 'run', so isn't in 39645ba7a89SDavid Gow # add_parse_opts() 39745ba7a89SDavid Gow parse_parser = subparser.add_parser('parse', 39845ba7a89SDavid Gow help='Parses KUnit results from a file, ' 39945ba7a89SDavid Gow 'and parses formatted results.') 40045ba7a89SDavid Gow add_parse_opts(parse_parser) 40145ba7a89SDavid Gow parse_parser.add_argument('file', 40245ba7a89SDavid Gow help='Specifies the file to read results from.', 40345ba7a89SDavid Gow type=str, nargs='?', metavar='input_file') 4040476e69fSGreg Thelen 405d8c23eadSDaniel Latypov cli_args = parser.parse_args(massage_argv(argv)) 4066ebf5866SFelix Guo 4075578d008SBrendan Higgins if get_kernel_root_path(): 4085578d008SBrendan Higgins os.chdir(get_kernel_root_path()) 4095578d008SBrendan Higgins 4106ebf5866SFelix Guo if cli_args.subcommand == 'run': 411e3212513SSeongJae Park if not os.path.exists(cli_args.build_dir): 412e3212513SSeongJae Park os.mkdir(cli_args.build_dir) 41382206a0cSBrendan Higgins 414ff7b437fSBrendan Higgins if not linux: 41587c9c163SBrendan Higgins linux = kunit_kernel.LinuxSourceTree(cli_args.build_dir, 41687c9c163SBrendan Higgins kunitconfig_path=cli_args.kunitconfig, 4179f57cc76SDaniel Latypov kconfig_add=cli_args.kconfig_add, 41887c9c163SBrendan Higgins arch=cli_args.arch, 41987c9c163SBrendan Higgins cross_compile=cli_args.cross_compile, 42087c9c163SBrendan Higgins qemu_config_path=cli_args.qemu_config) 421fcdb0bc0SAndy Shevchenko 422*db167981SDaniel Latypov request = KunitRequest(build_dir=cli_args.build_dir, 423*db167981SDaniel Latypov make_options=cli_args.make_options, 424*db167981SDaniel Latypov jobs=cli_args.jobs, 425*db167981SDaniel Latypov alltests=cli_args.alltests, 426*db167981SDaniel Latypov raw_output=cli_args.raw_output, 427*db167981SDaniel Latypov json=cli_args.json, 428*db167981SDaniel Latypov timeout=cli_args.timeout, 429*db167981SDaniel Latypov filter_glob=cli_args.filter_glob, 430*db167981SDaniel Latypov kernel_args=cli_args.kernel_args, 431*db167981SDaniel Latypov run_isolated=cli_args.run_isolated) 4326ebf5866SFelix Guo result = run_tests(linux, request) 4336ebf5866SFelix Guo if result.status != KunitStatus.SUCCESS: 4346ebf5866SFelix Guo sys.exit(1) 43545ba7a89SDavid Gow elif cli_args.subcommand == 'config': 43682206a0cSBrendan Higgins if cli_args.build_dir and ( 43782206a0cSBrendan Higgins not os.path.exists(cli_args.build_dir)): 43845ba7a89SDavid Gow os.mkdir(cli_args.build_dir) 43982206a0cSBrendan Higgins 44045ba7a89SDavid Gow if not linux: 44187c9c163SBrendan Higgins linux = kunit_kernel.LinuxSourceTree(cli_args.build_dir, 44287c9c163SBrendan Higgins kunitconfig_path=cli_args.kunitconfig, 4439f57cc76SDaniel Latypov kconfig_add=cli_args.kconfig_add, 44487c9c163SBrendan Higgins arch=cli_args.arch, 44587c9c163SBrendan Higgins cross_compile=cli_args.cross_compile, 44687c9c163SBrendan Higgins qemu_config_path=cli_args.qemu_config) 447fcdb0bc0SAndy Shevchenko 448*db167981SDaniel Latypov request = KunitConfigRequest(build_dir=cli_args.build_dir, 449*db167981SDaniel Latypov make_options=cli_args.make_options) 45045ba7a89SDavid Gow result = config_tests(linux, request) 45145ba7a89SDavid Gow kunit_parser.print_with_timestamp(( 45245ba7a89SDavid Gow 'Elapsed time: %.3fs\n') % ( 45345ba7a89SDavid Gow result.elapsed_time)) 45445ba7a89SDavid Gow if result.status != KunitStatus.SUCCESS: 45545ba7a89SDavid Gow sys.exit(1) 45645ba7a89SDavid Gow elif cli_args.subcommand == 'build': 45745ba7a89SDavid Gow if not linux: 45887c9c163SBrendan Higgins linux = kunit_kernel.LinuxSourceTree(cli_args.build_dir, 45987c9c163SBrendan Higgins kunitconfig_path=cli_args.kunitconfig, 4609f57cc76SDaniel Latypov kconfig_add=cli_args.kconfig_add, 46187c9c163SBrendan Higgins arch=cli_args.arch, 46287c9c163SBrendan Higgins cross_compile=cli_args.cross_compile, 46387c9c163SBrendan Higgins qemu_config_path=cli_args.qemu_config) 464fcdb0bc0SAndy Shevchenko 465*db167981SDaniel Latypov request = KunitBuildRequest(build_dir=cli_args.build_dir, 466*db167981SDaniel Latypov make_options=cli_args.make_options, 467*db167981SDaniel Latypov jobs=cli_args.jobs, 468*db167981SDaniel Latypov alltests=cli_args.alltests) 46945ba7a89SDavid Gow result = build_tests(linux, request) 47045ba7a89SDavid Gow kunit_parser.print_with_timestamp(( 47145ba7a89SDavid Gow 'Elapsed time: %.3fs\n') % ( 47245ba7a89SDavid Gow result.elapsed_time)) 47345ba7a89SDavid Gow if result.status != KunitStatus.SUCCESS: 47445ba7a89SDavid Gow sys.exit(1) 47545ba7a89SDavid Gow elif cli_args.subcommand == 'exec': 47645ba7a89SDavid Gow if not linux: 47787c9c163SBrendan Higgins linux = kunit_kernel.LinuxSourceTree(cli_args.build_dir, 47887c9c163SBrendan Higgins kunitconfig_path=cli_args.kunitconfig, 4799f57cc76SDaniel Latypov kconfig_add=cli_args.kconfig_add, 48087c9c163SBrendan Higgins arch=cli_args.arch, 48187c9c163SBrendan Higgins cross_compile=cli_args.cross_compile, 48287c9c163SBrendan Higgins qemu_config_path=cli_args.qemu_config) 483fcdb0bc0SAndy Shevchenko 484*db167981SDaniel Latypov exec_request = KunitExecRequest(raw_output=cli_args.raw_output, 485*db167981SDaniel Latypov build_dir=cli_args.build_dir, 486*db167981SDaniel Latypov json=cli_args.json, 487*db167981SDaniel Latypov timeout=cli_args.timeout, 488*db167981SDaniel Latypov alltests=cli_args.alltests, 489*db167981SDaniel Latypov filter_glob=cli_args.filter_glob, 490*db167981SDaniel Latypov kernel_args=cli_args.kernel_args, 491*db167981SDaniel Latypov run_isolated=cli_args.run_isolated) 492*db167981SDaniel Latypov result = exec_tests(linux, exec_request) 49345ba7a89SDavid Gow kunit_parser.print_with_timestamp(( 4947ef925eaSDaniel Latypov 'Elapsed time: %.3fs\n') % (result.elapsed_time)) 49545ba7a89SDavid Gow if result.status != KunitStatus.SUCCESS: 49645ba7a89SDavid Gow sys.exit(1) 49745ba7a89SDavid Gow elif cli_args.subcommand == 'parse': 49845ba7a89SDavid Gow if cli_args.file == None: 4992ab5d5e6SDaniel Latypov sys.stdin.reconfigure(errors='backslashreplace') # pytype: disable=attribute-error 50045ba7a89SDavid Gow kunit_output = sys.stdin 50145ba7a89SDavid Gow else: 5022ab5d5e6SDaniel Latypov with open(cli_args.file, 'r', errors='backslashreplace') as f: 50345ba7a89SDavid Gow kunit_output = f.read().splitlines() 504*db167981SDaniel Latypov request = KunitParseRequest(raw_output=cli_args.raw_output, 505*db167981SDaniel Latypov build_dir='', 506*db167981SDaniel Latypov json=cli_args.json) 5077ef925eaSDaniel Latypov result = parse_tests(request, kunit_output) 50845ba7a89SDavid Gow if result.status != KunitStatus.SUCCESS: 50945ba7a89SDavid Gow sys.exit(1) 5106ebf5866SFelix Guo else: 5116ebf5866SFelix Guo parser.print_help() 5126ebf5866SFelix Guo 5136ebf5866SFelix Guoif __name__ == '__main__': 514ff7b437fSBrendan Higgins main(sys.argv[1:]) 515