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 186ebf5866SFelix Guofrom collections import namedtuple 196ebf5866SFelix Guofrom enum import Enum, auto 20ff9e09a3SDaniel Latypovfrom typing import Iterable, Sequence, List 216ebf5866SFelix Guo 2221a6d178SHeidi Fahimimport kunit_json 236ebf5866SFelix Guoimport kunit_kernel 246ebf5866SFelix Guoimport kunit_parser 256ebf5866SFelix Guo 2645ba7a89SDavid GowKunitResult = namedtuple('KunitResult', ['status','result','elapsed_time']) 276ebf5866SFelix Guo 2845ba7a89SDavid GowKunitConfigRequest = namedtuple('KunitConfigRequest', 2901397e82SVitor Massaru Iha ['build_dir', 'make_options']) 3045ba7a89SDavid GowKunitBuildRequest = namedtuple('KunitBuildRequest', 3145ba7a89SDavid Gow ['jobs', 'build_dir', 'alltests', 3245ba7a89SDavid Gow 'make_options']) 3345ba7a89SDavid GowKunitExecRequest = namedtuple('KunitExecRequest', 346cb51a18SDaniel Latypov ['timeout', 'build_dir', 'alltests', 35ff9e09a3SDaniel Latypov 'filter_glob', 'kernel_args', 'run_isolated']) 3645ba7a89SDavid GowKunitParseRequest = namedtuple('KunitParseRequest', 377ef925eaSDaniel Latypov ['raw_output', 'build_dir', 'json']) 38021ed9f5SHeidi FahimKunitRequest = namedtuple('KunitRequest', ['raw_output','timeout', 'jobs', 39d992880bSDaniel Latypov 'build_dir', 'alltests', 'filter_glob', 40ff9e09a3SDaniel Latypov 'kernel_args', 'run_isolated', 'json', 'make_options']) 416ebf5866SFelix Guo 42be886ba9SHeidi FahimKernelDirectoryPath = sys.argv[0].split('tools/testing/kunit/')[0] 43be886ba9SHeidi Fahim 446ebf5866SFelix Guoclass KunitStatus(Enum): 456ebf5866SFelix Guo SUCCESS = auto() 466ebf5866SFelix Guo CONFIG_FAILURE = auto() 476ebf5866SFelix Guo BUILD_FAILURE = auto() 486ebf5866SFelix Guo TEST_FAILURE = auto() 496ebf5866SFelix Guo 5009641f7cSDaniel Latypovdef get_kernel_root_path() -> str: 5109641f7cSDaniel Latypov path = sys.argv[0] if not __file__ else __file__ 5209641f7cSDaniel Latypov parts = os.path.realpath(path).split('tools/testing/kunit') 53be886ba9SHeidi Fahim if len(parts) != 2: 54be886ba9SHeidi Fahim sys.exit(1) 55be886ba9SHeidi Fahim return parts[0] 56be886ba9SHeidi Fahim 5745ba7a89SDavid Gowdef config_tests(linux: kunit_kernel.LinuxSourceTree, 5845ba7a89SDavid Gow request: KunitConfigRequest) -> KunitResult: 5945ba7a89SDavid Gow kunit_parser.print_with_timestamp('Configuring KUnit Kernel ...') 6045ba7a89SDavid Gow 616ebf5866SFelix Guo config_start = time.time() 620476e69fSGreg Thelen success = linux.build_reconfig(request.build_dir, request.make_options) 636ebf5866SFelix Guo config_end = time.time() 646ebf5866SFelix Guo if not success: 6545ba7a89SDavid Gow return KunitResult(KunitStatus.CONFIG_FAILURE, 6645ba7a89SDavid Gow 'could not configure kernel', 6745ba7a89SDavid Gow config_end - config_start) 6845ba7a89SDavid Gow return KunitResult(KunitStatus.SUCCESS, 6945ba7a89SDavid Gow 'configured kernel successfully', 7045ba7a89SDavid Gow config_end - config_start) 716ebf5866SFelix Guo 7245ba7a89SDavid Gowdef build_tests(linux: kunit_kernel.LinuxSourceTree, 7345ba7a89SDavid Gow request: KunitBuildRequest) -> KunitResult: 746ebf5866SFelix Guo kunit_parser.print_with_timestamp('Building KUnit Kernel ...') 756ebf5866SFelix Guo 766ebf5866SFelix Guo build_start = time.time() 7787c9c163SBrendan Higgins success = linux.build_kernel(request.alltests, 78021ed9f5SHeidi Fahim request.jobs, 790476e69fSGreg Thelen request.build_dir, 800476e69fSGreg Thelen request.make_options) 816ebf5866SFelix Guo build_end = time.time() 826ebf5866SFelix Guo if not success: 83ee61492aSDavid Gow return KunitResult(KunitStatus.BUILD_FAILURE, 84ee61492aSDavid Gow 'could not build kernel', 85ee61492aSDavid Gow build_end - build_start) 8645ba7a89SDavid Gow if not success: 8745ba7a89SDavid Gow return KunitResult(KunitStatus.BUILD_FAILURE, 8845ba7a89SDavid Gow 'could not build kernel', 8945ba7a89SDavid Gow build_end - build_start) 9045ba7a89SDavid Gow return KunitResult(KunitStatus.SUCCESS, 9145ba7a89SDavid Gow 'built kernel successfully', 9245ba7a89SDavid Gow build_end - build_start) 936ebf5866SFelix Guo 94ff9e09a3SDaniel Latypovdef _list_tests(linux: kunit_kernel.LinuxSourceTree, request: KunitExecRequest) -> List[str]: 95ff9e09a3SDaniel Latypov args = ['kunit.action=list'] 96ff9e09a3SDaniel Latypov if request.kernel_args: 97ff9e09a3SDaniel Latypov args.extend(request.kernel_args) 98ff9e09a3SDaniel Latypov 99ff9e09a3SDaniel Latypov output = linux.run_kernel(args=args, 100ff9e09a3SDaniel Latypov timeout=None if request.alltests else request.timeout, 101ff9e09a3SDaniel Latypov filter_glob=request.filter_glob, 102ff9e09a3SDaniel Latypov build_dir=request.build_dir) 103ff9e09a3SDaniel Latypov lines = kunit_parser.extract_tap_lines(output) 104ff9e09a3SDaniel Latypov # Hack! Drop the dummy TAP version header that the executor prints out. 105ff9e09a3SDaniel Latypov lines.pop() 106ff9e09a3SDaniel Latypov 107ff9e09a3SDaniel Latypov # Filter out any extraneous non-test output that might have gotten mixed in. 108ff9e09a3SDaniel Latypov return [l for l in lines if re.match('^[^\s.]+\.[^\s.]+$', l)] 109ff9e09a3SDaniel Latypov 110ff9e09a3SDaniel Latypovdef _suites_from_test_list(tests: List[str]) -> List[str]: 111ff9e09a3SDaniel Latypov """Extracts all the suites from an ordered list of tests.""" 112ff9e09a3SDaniel Latypov suites = [] # type: List[str] 113ff9e09a3SDaniel Latypov for t in tests: 114ff9e09a3SDaniel Latypov parts = t.split('.', maxsplit=2) 115ff9e09a3SDaniel Latypov if len(parts) != 2: 116ff9e09a3SDaniel Latypov raise ValueError(f'internal KUnit error, test name should be of the form "<suite>.<test>", got "{t}"') 117ff9e09a3SDaniel Latypov suite, case = parts 118ff9e09a3SDaniel Latypov if not suites or suites[-1] != suite: 119ff9e09a3SDaniel Latypov suites.append(suite) 120ff9e09a3SDaniel Latypov return suites 121ff9e09a3SDaniel Latypov 122ff9e09a3SDaniel Latypov 123ff9e09a3SDaniel Latypov 1247ef925eaSDaniel Latypovdef exec_tests(linux: kunit_kernel.LinuxSourceTree, request: KunitExecRequest, 1257ef925eaSDaniel Latypov parse_request: KunitParseRequest) -> KunitResult: 126ff9e09a3SDaniel Latypov filter_globs = [request.filter_glob] 127ff9e09a3SDaniel Latypov if request.run_isolated: 128ff9e09a3SDaniel Latypov tests = _list_tests(linux, request) 129ff9e09a3SDaniel Latypov if request.run_isolated == 'test': 130ff9e09a3SDaniel Latypov filter_globs = tests 131ff9e09a3SDaniel Latypov if request.run_isolated == 'suite': 132ff9e09a3SDaniel Latypov filter_globs = _suites_from_test_list(tests) 133ff9e09a3SDaniel Latypov # Apply the test-part of the user's glob, if present. 134ff9e09a3SDaniel Latypov if '.' in request.filter_glob: 135ff9e09a3SDaniel Latypov test_glob = request.filter_glob.split('.', maxsplit=2)[1] 136ff9e09a3SDaniel Latypov filter_globs = [g + '.'+ test_glob for g in filter_globs] 137ff9e09a3SDaniel Latypov 138d65d07cbSRae Moar test_counts = kunit_parser.TestCounts() 139ff9e09a3SDaniel Latypov exec_time = 0.0 140ff9e09a3SDaniel Latypov for i, filter_glob in enumerate(filter_globs): 141ff9e09a3SDaniel Latypov kunit_parser.print_with_timestamp('Starting KUnit Kernel ({}/{})...'.format(i+1, len(filter_globs))) 142ff9e09a3SDaniel Latypov 1436ebf5866SFelix Guo test_start = time.time() 1447ef925eaSDaniel Latypov run_result = linux.run_kernel( 1456cb51a18SDaniel Latypov args=request.kernel_args, 146021ed9f5SHeidi Fahim timeout=None if request.alltests else request.timeout, 147ff9e09a3SDaniel Latypov filter_glob=filter_glob, 1486ec1b81dSSeongJae Park build_dir=request.build_dir) 14945ba7a89SDavid Gow 1505f6aa6d8SDaniel Latypov result = parse_tests(parse_request, run_result) 1515f6aa6d8SDaniel Latypov # run_kernel() doesn't block on the kernel exiting. 1525f6aa6d8SDaniel Latypov # That only happens after we get the last line of output from `run_result`. 1535f6aa6d8SDaniel Latypov # So exec_time here actually contains parsing + execution time, which is fine. 1546ebf5866SFelix Guo test_end = time.time() 155ff9e09a3SDaniel Latypov exec_time += test_end - test_start 156ff9e09a3SDaniel Latypov 157d65d07cbSRae Moar test_counts.add_subtest_counts(result.result.test.counts) 1586ebf5866SFelix Guo 159d65d07cbSRae Moar kunit_status = _map_to_overall_status(test_counts.get_status()) 160d65d07cbSRae Moar return KunitResult(status=kunit_status, result=result.result, elapsed_time=exec_time) 161d65d07cbSRae Moar 162d65d07cbSRae Moardef _map_to_overall_status(test_status: kunit_parser.TestStatus) -> KunitStatus: 163d65d07cbSRae Moar if test_status in (kunit_parser.TestStatus.SUCCESS, kunit_parser.TestStatus.SKIPPED): 164d65d07cbSRae Moar return KunitStatus.SUCCESS 165d65d07cbSRae Moar else: 166d65d07cbSRae Moar return KunitStatus.TEST_FAILURE 1677ef925eaSDaniel Latypov 1687ef925eaSDaniel Latypovdef parse_tests(request: KunitParseRequest, input_data: Iterable[str]) -> KunitResult: 16945ba7a89SDavid Gow parse_start = time.time() 17045ba7a89SDavid Gow 17145ba7a89SDavid Gow test_result = kunit_parser.TestResult(kunit_parser.TestStatus.SUCCESS, 172d65d07cbSRae Moar kunit_parser.Test(), 17345ba7a89SDavid Gow 'Tests not Parsed.') 17421a6d178SHeidi Fahim 17545ba7a89SDavid Gow if request.raw_output: 176d65d07cbSRae Moar # Treat unparsed results as one passing test. 177d65d07cbSRae Moar test_result.test.status = kunit_parser.TestStatus.SUCCESS 178d65d07cbSRae Moar test_result.test.counts.passed = 1 179d65d07cbSRae Moar 1807ef925eaSDaniel Latypov output: Iterable[str] = input_data 1816a499c9cSDaniel Latypov if request.raw_output == 'all': 1826a499c9cSDaniel Latypov pass 1836a499c9cSDaniel Latypov elif request.raw_output == 'kunit': 1846a499c9cSDaniel Latypov output = kunit_parser.extract_tap_lines(output) 1856a499c9cSDaniel Latypov else: 1866a499c9cSDaniel Latypov print(f'Unknown --raw_output option "{request.raw_output}"', file=sys.stderr) 1876a499c9cSDaniel Latypov for line in output: 1886a499c9cSDaniel Latypov print(line.rstrip()) 1896a499c9cSDaniel Latypov 19045ba7a89SDavid Gow else: 1917ef925eaSDaniel Latypov test_result = kunit_parser.parse_run_tests(input_data) 19245ba7a89SDavid Gow parse_end = time.time() 19345ba7a89SDavid Gow 19421a6d178SHeidi Fahim if request.json: 19521a6d178SHeidi Fahim json_obj = kunit_json.get_json_result( 19621a6d178SHeidi Fahim test_result=test_result, 19721a6d178SHeidi Fahim def_config='kunit_defconfig', 19821a6d178SHeidi Fahim build_dir=request.build_dir, 19921a6d178SHeidi Fahim json_path=request.json) 20021a6d178SHeidi Fahim if request.json == 'stdout': 20121a6d178SHeidi Fahim print(json_obj) 20221a6d178SHeidi Fahim 20345ba7a89SDavid Gow if test_result.status != kunit_parser.TestStatus.SUCCESS: 20445ba7a89SDavid Gow return KunitResult(KunitStatus.TEST_FAILURE, test_result, 20545ba7a89SDavid Gow parse_end - parse_start) 20645ba7a89SDavid Gow 20745ba7a89SDavid Gow return KunitResult(KunitStatus.SUCCESS, test_result, 20845ba7a89SDavid Gow parse_end - parse_start) 20945ba7a89SDavid Gow 21045ba7a89SDavid Gowdef run_tests(linux: kunit_kernel.LinuxSourceTree, 21145ba7a89SDavid Gow request: KunitRequest) -> KunitResult: 21245ba7a89SDavid Gow run_start = time.time() 21345ba7a89SDavid Gow 21445ba7a89SDavid Gow config_request = KunitConfigRequest(request.build_dir, 21545ba7a89SDavid Gow request.make_options) 21645ba7a89SDavid Gow config_result = config_tests(linux, config_request) 21745ba7a89SDavid Gow if config_result.status != KunitStatus.SUCCESS: 21845ba7a89SDavid Gow return config_result 21945ba7a89SDavid Gow 22045ba7a89SDavid Gow build_request = KunitBuildRequest(request.jobs, request.build_dir, 22145ba7a89SDavid Gow request.alltests, 22245ba7a89SDavid Gow request.make_options) 22345ba7a89SDavid Gow build_result = build_tests(linux, build_request) 22445ba7a89SDavid Gow if build_result.status != KunitStatus.SUCCESS: 22545ba7a89SDavid Gow return build_result 22645ba7a89SDavid Gow 22745ba7a89SDavid Gow exec_request = KunitExecRequest(request.timeout, request.build_dir, 2286cb51a18SDaniel Latypov request.alltests, request.filter_glob, 229ff9e09a3SDaniel Latypov request.kernel_args, request.run_isolated) 23045ba7a89SDavid Gow parse_request = KunitParseRequest(request.raw_output, 23121a6d178SHeidi Fahim request.build_dir, 23221a6d178SHeidi Fahim request.json) 2337ef925eaSDaniel Latypov 2347ef925eaSDaniel Latypov exec_result = exec_tests(linux, exec_request, parse_request) 23545ba7a89SDavid Gow 23645ba7a89SDavid Gow run_end = time.time() 23745ba7a89SDavid Gow 2386ebf5866SFelix Guo kunit_parser.print_with_timestamp(( 2396ebf5866SFelix Guo 'Elapsed time: %.3fs total, %.3fs configuring, %.3fs ' + 2406ebf5866SFelix Guo 'building, %.3fs running\n') % ( 24145ba7a89SDavid Gow run_end - run_start, 24245ba7a89SDavid Gow config_result.elapsed_time, 24345ba7a89SDavid Gow build_result.elapsed_time, 24445ba7a89SDavid Gow exec_result.elapsed_time)) 2457ef925eaSDaniel Latypov return exec_result 2466ebf5866SFelix Guo 247d8c23eadSDaniel Latypov# Problem: 248d8c23eadSDaniel Latypov# $ kunit.py run --json 249d8c23eadSDaniel Latypov# works as one would expect and prints the parsed test results as JSON. 250d8c23eadSDaniel Latypov# $ kunit.py run --json suite_name 251d8c23eadSDaniel Latypov# would *not* pass suite_name as the filter_glob and print as json. 252d8c23eadSDaniel Latypov# argparse will consider it to be another way of writing 253d8c23eadSDaniel Latypov# $ kunit.py run --json=suite_name 254d8c23eadSDaniel Latypov# i.e. it would run all tests, and dump the json to a `suite_name` file. 255d8c23eadSDaniel Latypov# So we hackily automatically rewrite --json => --json=stdout 256d8c23eadSDaniel Latypovpseudo_bool_flag_defaults = { 257d8c23eadSDaniel Latypov '--json': 'stdout', 258d8c23eadSDaniel Latypov '--raw_output': 'kunit', 259d8c23eadSDaniel Latypov} 260d8c23eadSDaniel Latypovdef massage_argv(argv: Sequence[str]) -> Sequence[str]: 261d8c23eadSDaniel Latypov def massage_arg(arg: str) -> str: 262d8c23eadSDaniel Latypov if arg not in pseudo_bool_flag_defaults: 263d8c23eadSDaniel Latypov return arg 264d8c23eadSDaniel Latypov return f'{arg}={pseudo_bool_flag_defaults[arg]}' 265d8c23eadSDaniel Latypov return list(map(massage_arg, argv)) 266d8c23eadSDaniel Latypov 26709641f7cSDaniel Latypovdef add_common_opts(parser) -> None: 26845ba7a89SDavid Gow parser.add_argument('--build_dir', 26945ba7a89SDavid Gow help='As in the make command, it specifies the build ' 27045ba7a89SDavid Gow 'directory.', 271ddbd60c7SVitor Massaru Iha type=str, default='.kunit', metavar='build_dir') 27245ba7a89SDavid Gow parser.add_argument('--make_options', 27345ba7a89SDavid Gow help='X=Y make option, can be repeated.', 27445ba7a89SDavid Gow action='append') 27545ba7a89SDavid Gow parser.add_argument('--alltests', 27645ba7a89SDavid Gow help='Run all KUnit tests through allyesconfig', 2776ebf5866SFelix Guo action='store_true') 278243180f5SDaniel Latypov parser.add_argument('--kunitconfig', 2799854781dSDaniel Latypov help='Path to Kconfig fragment that enables KUnit tests.' 2809854781dSDaniel Latypov ' If given a directory, (e.g. lib/kunit), "/.kunitconfig" ' 2819854781dSDaniel Latypov 'will get automatically appended.', 282243180f5SDaniel Latypov metavar='kunitconfig') 283*9f57cc76SDaniel Latypov parser.add_argument('--kconfig_add', 284*9f57cc76SDaniel Latypov help='Additional Kconfig options to append to the ' 285*9f57cc76SDaniel Latypov '.kunitconfig, e.g. CONFIG_KASAN=y. Can be repeated.', 286*9f57cc76SDaniel Latypov action='append') 2876ebf5866SFelix Guo 28887c9c163SBrendan Higgins parser.add_argument('--arch', 28987c9c163SBrendan Higgins help=('Specifies the architecture to run tests under. ' 29087c9c163SBrendan Higgins 'The architecture specified here must match the ' 29187c9c163SBrendan Higgins 'string passed to the ARCH make param, ' 29287c9c163SBrendan Higgins 'e.g. i386, x86_64, arm, um, etc. Non-UML ' 29387c9c163SBrendan Higgins 'architectures run on QEMU.'), 29487c9c163SBrendan Higgins type=str, default='um', metavar='arch') 29587c9c163SBrendan Higgins 29687c9c163SBrendan Higgins parser.add_argument('--cross_compile', 29787c9c163SBrendan Higgins help=('Sets make\'s CROSS_COMPILE variable; it should ' 29887c9c163SBrendan Higgins 'be set to a toolchain path prefix (the prefix ' 29987c9c163SBrendan Higgins 'of gcc and other tools in your toolchain, for ' 30087c9c163SBrendan Higgins 'example `sparc64-linux-gnu-` if you have the ' 30187c9c163SBrendan Higgins 'sparc toolchain installed on your system, or ' 30287c9c163SBrendan Higgins '`$HOME/toolchains/microblaze/gcc-9.2.0-nolibc/microblaze-linux/bin/microblaze-linux-` ' 30387c9c163SBrendan Higgins 'if you have downloaded the microblaze toolchain ' 30487c9c163SBrendan Higgins 'from the 0-day website to a directory in your ' 30587c9c163SBrendan Higgins 'home directory called `toolchains`).'), 30687c9c163SBrendan Higgins metavar='cross_compile') 30787c9c163SBrendan Higgins 30887c9c163SBrendan Higgins parser.add_argument('--qemu_config', 30987c9c163SBrendan Higgins help=('Takes a path to a path to a file containing ' 31087c9c163SBrendan Higgins 'a QemuArchParams object.'), 31187c9c163SBrendan Higgins type=str, metavar='qemu_config') 31287c9c163SBrendan Higgins 31309641f7cSDaniel Latypovdef add_build_opts(parser) -> None: 31445ba7a89SDavid Gow parser.add_argument('--jobs', 31545ba7a89SDavid Gow help='As in the make command, "Specifies the number of ' 31645ba7a89SDavid Gow 'jobs (commands) to run simultaneously."', 31745ba7a89SDavid Gow type=int, default=8, metavar='jobs') 31845ba7a89SDavid Gow 31909641f7cSDaniel Latypovdef add_exec_opts(parser) -> None: 32045ba7a89SDavid Gow parser.add_argument('--timeout', 3216ebf5866SFelix Guo help='maximum number of seconds to allow for all tests ' 3226ebf5866SFelix Guo 'to run. This does not include time taken to build the ' 3236ebf5866SFelix Guo 'tests.', 3246ebf5866SFelix Guo type=int, 3256ebf5866SFelix Guo default=300, 3266ebf5866SFelix Guo metavar='timeout') 327d992880bSDaniel Latypov parser.add_argument('filter_glob', 328a127b154SDaniel Latypov help='Filter which KUnit test suites/tests run at ' 329a127b154SDaniel Latypov 'boot-time, e.g. list* or list*.*del_test', 330d992880bSDaniel Latypov type=str, 331d992880bSDaniel Latypov nargs='?', 332d992880bSDaniel Latypov default='', 333d992880bSDaniel Latypov metavar='filter_glob') 3346cb51a18SDaniel Latypov parser.add_argument('--kernel_args', 3356cb51a18SDaniel Latypov help='Kernel command-line parameters. Maybe be repeated', 3366cb51a18SDaniel Latypov action='append') 337ff9e09a3SDaniel Latypov parser.add_argument('--run_isolated', help='If set, boot the kernel for each ' 338ff9e09a3SDaniel Latypov 'individual suite/test. This is can be useful for debugging ' 339ff9e09a3SDaniel Latypov 'a non-hermetic test, one that might pass/fail based on ' 340ff9e09a3SDaniel Latypov 'what ran before it.', 341ff9e09a3SDaniel Latypov type=str, 342ff9e09a3SDaniel Latypov choices=['suite', 'test']), 3436ebf5866SFelix Guo 34409641f7cSDaniel Latypovdef add_parse_opts(parser) -> None: 3456a499c9cSDaniel Latypov parser.add_argument('--raw_output', help='If set don\'t format output from kernel. ' 3466a499c9cSDaniel Latypov 'If set to --raw_output=kunit, filters to just KUnit output.', 3476a499c9cSDaniel Latypov type=str, nargs='?', const='all', default=None) 34821a6d178SHeidi Fahim parser.add_argument('--json', 34921a6d178SHeidi Fahim nargs='?', 35021a6d178SHeidi Fahim help='Stores test results in a JSON, and either ' 35121a6d178SHeidi Fahim 'prints to stdout or saves to file if a ' 35221a6d178SHeidi Fahim 'filename is specified', 35321a6d178SHeidi Fahim type=str, const='stdout', default=None) 354021ed9f5SHeidi Fahim 35545ba7a89SDavid Gowdef main(argv, linux=None): 35645ba7a89SDavid Gow parser = argparse.ArgumentParser( 35745ba7a89SDavid Gow description='Helps writing and running KUnit tests.') 35845ba7a89SDavid Gow subparser = parser.add_subparsers(dest='subcommand') 35945ba7a89SDavid Gow 36045ba7a89SDavid Gow # The 'run' command will config, build, exec, and parse in one go. 36145ba7a89SDavid Gow run_parser = subparser.add_parser('run', help='Runs KUnit tests.') 36245ba7a89SDavid Gow add_common_opts(run_parser) 36345ba7a89SDavid Gow add_build_opts(run_parser) 36445ba7a89SDavid Gow add_exec_opts(run_parser) 36545ba7a89SDavid Gow add_parse_opts(run_parser) 36645ba7a89SDavid Gow 36745ba7a89SDavid Gow config_parser = subparser.add_parser('config', 36845ba7a89SDavid Gow help='Ensures that .config contains all of ' 36945ba7a89SDavid Gow 'the options in .kunitconfig') 37045ba7a89SDavid Gow add_common_opts(config_parser) 37145ba7a89SDavid Gow 37245ba7a89SDavid Gow build_parser = subparser.add_parser('build', help='Builds a kernel with KUnit tests') 37345ba7a89SDavid Gow add_common_opts(build_parser) 37445ba7a89SDavid Gow add_build_opts(build_parser) 37545ba7a89SDavid Gow 37645ba7a89SDavid Gow exec_parser = subparser.add_parser('exec', help='Run a kernel with KUnit tests') 37745ba7a89SDavid Gow add_common_opts(exec_parser) 37845ba7a89SDavid Gow add_exec_opts(exec_parser) 37945ba7a89SDavid Gow add_parse_opts(exec_parser) 38045ba7a89SDavid Gow 38145ba7a89SDavid Gow # The 'parse' option is special, as it doesn't need the kernel source 38245ba7a89SDavid Gow # (therefore there is no need for a build_dir, hence no add_common_opts) 38345ba7a89SDavid Gow # and the '--file' argument is not relevant to 'run', so isn't in 38445ba7a89SDavid Gow # add_parse_opts() 38545ba7a89SDavid Gow parse_parser = subparser.add_parser('parse', 38645ba7a89SDavid Gow help='Parses KUnit results from a file, ' 38745ba7a89SDavid Gow 'and parses formatted results.') 38845ba7a89SDavid Gow add_parse_opts(parse_parser) 38945ba7a89SDavid Gow parse_parser.add_argument('file', 39045ba7a89SDavid Gow help='Specifies the file to read results from.', 39145ba7a89SDavid Gow type=str, nargs='?', metavar='input_file') 3920476e69fSGreg Thelen 393d8c23eadSDaniel Latypov cli_args = parser.parse_args(massage_argv(argv)) 3946ebf5866SFelix Guo 3955578d008SBrendan Higgins if get_kernel_root_path(): 3965578d008SBrendan Higgins os.chdir(get_kernel_root_path()) 3975578d008SBrendan Higgins 3986ebf5866SFelix Guo if cli_args.subcommand == 'run': 399e3212513SSeongJae Park if not os.path.exists(cli_args.build_dir): 400e3212513SSeongJae Park os.mkdir(cli_args.build_dir) 40182206a0cSBrendan Higgins 402ff7b437fSBrendan Higgins if not linux: 40387c9c163SBrendan Higgins linux = kunit_kernel.LinuxSourceTree(cli_args.build_dir, 40487c9c163SBrendan Higgins kunitconfig_path=cli_args.kunitconfig, 405*9f57cc76SDaniel Latypov kconfig_add=cli_args.kconfig_add, 40687c9c163SBrendan Higgins arch=cli_args.arch, 40787c9c163SBrendan Higgins cross_compile=cli_args.cross_compile, 40887c9c163SBrendan Higgins qemu_config_path=cli_args.qemu_config) 409fcdb0bc0SAndy Shevchenko 4106ebf5866SFelix Guo request = KunitRequest(cli_args.raw_output, 4116ebf5866SFelix Guo cli_args.timeout, 4126ebf5866SFelix Guo cli_args.jobs, 413ff7b437fSBrendan Higgins cli_args.build_dir, 4140476e69fSGreg Thelen cli_args.alltests, 415d992880bSDaniel Latypov cli_args.filter_glob, 4166cb51a18SDaniel Latypov cli_args.kernel_args, 417ff9e09a3SDaniel Latypov cli_args.run_isolated, 41821a6d178SHeidi Fahim cli_args.json, 4190476e69fSGreg Thelen cli_args.make_options) 4206ebf5866SFelix Guo result = run_tests(linux, request) 4216ebf5866SFelix Guo if result.status != KunitStatus.SUCCESS: 4226ebf5866SFelix Guo sys.exit(1) 42345ba7a89SDavid Gow elif cli_args.subcommand == 'config': 42482206a0cSBrendan Higgins if cli_args.build_dir and ( 42582206a0cSBrendan Higgins not os.path.exists(cli_args.build_dir)): 42645ba7a89SDavid Gow os.mkdir(cli_args.build_dir) 42782206a0cSBrendan Higgins 42845ba7a89SDavid Gow if not linux: 42987c9c163SBrendan Higgins linux = kunit_kernel.LinuxSourceTree(cli_args.build_dir, 43087c9c163SBrendan Higgins kunitconfig_path=cli_args.kunitconfig, 431*9f57cc76SDaniel Latypov kconfig_add=cli_args.kconfig_add, 43287c9c163SBrendan Higgins arch=cli_args.arch, 43387c9c163SBrendan Higgins cross_compile=cli_args.cross_compile, 43487c9c163SBrendan Higgins qemu_config_path=cli_args.qemu_config) 435fcdb0bc0SAndy Shevchenko 43645ba7a89SDavid Gow request = KunitConfigRequest(cli_args.build_dir, 43745ba7a89SDavid Gow cli_args.make_options) 43845ba7a89SDavid Gow result = config_tests(linux, request) 43945ba7a89SDavid Gow kunit_parser.print_with_timestamp(( 44045ba7a89SDavid Gow 'Elapsed time: %.3fs\n') % ( 44145ba7a89SDavid Gow result.elapsed_time)) 44245ba7a89SDavid Gow if result.status != KunitStatus.SUCCESS: 44345ba7a89SDavid Gow sys.exit(1) 44445ba7a89SDavid Gow elif cli_args.subcommand == 'build': 44545ba7a89SDavid Gow if not linux: 44687c9c163SBrendan Higgins linux = kunit_kernel.LinuxSourceTree(cli_args.build_dir, 44787c9c163SBrendan Higgins kunitconfig_path=cli_args.kunitconfig, 448*9f57cc76SDaniel Latypov kconfig_add=cli_args.kconfig_add, 44987c9c163SBrendan Higgins arch=cli_args.arch, 45087c9c163SBrendan Higgins cross_compile=cli_args.cross_compile, 45187c9c163SBrendan Higgins qemu_config_path=cli_args.qemu_config) 452fcdb0bc0SAndy Shevchenko 45345ba7a89SDavid Gow request = KunitBuildRequest(cli_args.jobs, 45445ba7a89SDavid Gow cli_args.build_dir, 45545ba7a89SDavid Gow cli_args.alltests, 45645ba7a89SDavid Gow cli_args.make_options) 45745ba7a89SDavid Gow result = build_tests(linux, request) 45845ba7a89SDavid Gow kunit_parser.print_with_timestamp(( 45945ba7a89SDavid Gow 'Elapsed time: %.3fs\n') % ( 46045ba7a89SDavid Gow result.elapsed_time)) 46145ba7a89SDavid Gow if result.status != KunitStatus.SUCCESS: 46245ba7a89SDavid Gow sys.exit(1) 46345ba7a89SDavid Gow elif cli_args.subcommand == 'exec': 46445ba7a89SDavid Gow if not linux: 46587c9c163SBrendan Higgins linux = kunit_kernel.LinuxSourceTree(cli_args.build_dir, 46687c9c163SBrendan Higgins kunitconfig_path=cli_args.kunitconfig, 467*9f57cc76SDaniel Latypov kconfig_add=cli_args.kconfig_add, 46887c9c163SBrendan Higgins arch=cli_args.arch, 46987c9c163SBrendan Higgins cross_compile=cli_args.cross_compile, 47087c9c163SBrendan Higgins qemu_config_path=cli_args.qemu_config) 471fcdb0bc0SAndy Shevchenko 47245ba7a89SDavid Gow exec_request = KunitExecRequest(cli_args.timeout, 47345ba7a89SDavid Gow cli_args.build_dir, 474d992880bSDaniel Latypov cli_args.alltests, 4756cb51a18SDaniel Latypov cli_args.filter_glob, 476ff9e09a3SDaniel Latypov cli_args.kernel_args, 477ff9e09a3SDaniel Latypov cli_args.run_isolated) 47845ba7a89SDavid Gow parse_request = KunitParseRequest(cli_args.raw_output, 47921a6d178SHeidi Fahim cli_args.build_dir, 48021a6d178SHeidi Fahim cli_args.json) 4817ef925eaSDaniel Latypov result = exec_tests(linux, exec_request, parse_request) 48245ba7a89SDavid Gow kunit_parser.print_with_timestamp(( 4837ef925eaSDaniel Latypov 'Elapsed time: %.3fs\n') % (result.elapsed_time)) 48445ba7a89SDavid Gow if result.status != KunitStatus.SUCCESS: 48545ba7a89SDavid Gow sys.exit(1) 48645ba7a89SDavid Gow elif cli_args.subcommand == 'parse': 48745ba7a89SDavid Gow if cli_args.file == None: 4882ab5d5e6SDaniel Latypov sys.stdin.reconfigure(errors='backslashreplace') # pytype: disable=attribute-error 48945ba7a89SDavid Gow kunit_output = sys.stdin 49045ba7a89SDavid Gow else: 4912ab5d5e6SDaniel Latypov with open(cli_args.file, 'r', errors='backslashreplace') as f: 49245ba7a89SDavid Gow kunit_output = f.read().splitlines() 49345ba7a89SDavid Gow request = KunitParseRequest(cli_args.raw_output, 4943959d0a6SDavid Gow None, 49521a6d178SHeidi Fahim cli_args.json) 4967ef925eaSDaniel Latypov result = parse_tests(request, kunit_output) 49745ba7a89SDavid Gow if result.status != KunitStatus.SUCCESS: 49845ba7a89SDavid Gow sys.exit(1) 4996ebf5866SFelix Guo else: 5006ebf5866SFelix Guo parser.print_help() 5016ebf5866SFelix Guo 5026ebf5866SFelix Guoif __name__ == '__main__': 503ff7b437fSBrendan Higgins main(sys.argv[1:]) 504