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 138*d65d07cbSRae 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 157*d65d07cbSRae Moar test_counts.add_subtest_counts(result.result.test.counts) 1586ebf5866SFelix Guo 159*d65d07cbSRae Moar kunit_status = _map_to_overall_status(test_counts.get_status()) 160*d65d07cbSRae Moar return KunitResult(status=kunit_status, result=result.result, elapsed_time=exec_time) 161*d65d07cbSRae Moar 162*d65d07cbSRae Moardef _map_to_overall_status(test_status: kunit_parser.TestStatus) -> KunitStatus: 163*d65d07cbSRae Moar if test_status in (kunit_parser.TestStatus.SUCCESS, kunit_parser.TestStatus.SKIPPED): 164*d65d07cbSRae Moar return KunitStatus.SUCCESS 165*d65d07cbSRae Moar else: 166*d65d07cbSRae 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, 172*d65d07cbSRae Moar kunit_parser.Test(), 17345ba7a89SDavid Gow 'Tests not Parsed.') 17421a6d178SHeidi Fahim 17545ba7a89SDavid Gow if request.raw_output: 176*d65d07cbSRae Moar # Treat unparsed results as one passing test. 177*d65d07cbSRae Moar test_result.test.status = kunit_parser.TestStatus.SUCCESS 178*d65d07cbSRae Moar test_result.test.counts.passed = 1 179*d65d07cbSRae 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') 2836ebf5866SFelix Guo 28487c9c163SBrendan Higgins parser.add_argument('--arch', 28587c9c163SBrendan Higgins help=('Specifies the architecture to run tests under. ' 28687c9c163SBrendan Higgins 'The architecture specified here must match the ' 28787c9c163SBrendan Higgins 'string passed to the ARCH make param, ' 28887c9c163SBrendan Higgins 'e.g. i386, x86_64, arm, um, etc. Non-UML ' 28987c9c163SBrendan Higgins 'architectures run on QEMU.'), 29087c9c163SBrendan Higgins type=str, default='um', metavar='arch') 29187c9c163SBrendan Higgins 29287c9c163SBrendan Higgins parser.add_argument('--cross_compile', 29387c9c163SBrendan Higgins help=('Sets make\'s CROSS_COMPILE variable; it should ' 29487c9c163SBrendan Higgins 'be set to a toolchain path prefix (the prefix ' 29587c9c163SBrendan Higgins 'of gcc and other tools in your toolchain, for ' 29687c9c163SBrendan Higgins 'example `sparc64-linux-gnu-` if you have the ' 29787c9c163SBrendan Higgins 'sparc toolchain installed on your system, or ' 29887c9c163SBrendan Higgins '`$HOME/toolchains/microblaze/gcc-9.2.0-nolibc/microblaze-linux/bin/microblaze-linux-` ' 29987c9c163SBrendan Higgins 'if you have downloaded the microblaze toolchain ' 30087c9c163SBrendan Higgins 'from the 0-day website to a directory in your ' 30187c9c163SBrendan Higgins 'home directory called `toolchains`).'), 30287c9c163SBrendan Higgins metavar='cross_compile') 30387c9c163SBrendan Higgins 30487c9c163SBrendan Higgins parser.add_argument('--qemu_config', 30587c9c163SBrendan Higgins help=('Takes a path to a path to a file containing ' 30687c9c163SBrendan Higgins 'a QemuArchParams object.'), 30787c9c163SBrendan Higgins type=str, metavar='qemu_config') 30887c9c163SBrendan Higgins 30909641f7cSDaniel Latypovdef add_build_opts(parser) -> None: 31045ba7a89SDavid Gow parser.add_argument('--jobs', 31145ba7a89SDavid Gow help='As in the make command, "Specifies the number of ' 31245ba7a89SDavid Gow 'jobs (commands) to run simultaneously."', 31345ba7a89SDavid Gow type=int, default=8, metavar='jobs') 31445ba7a89SDavid Gow 31509641f7cSDaniel Latypovdef add_exec_opts(parser) -> None: 31645ba7a89SDavid Gow parser.add_argument('--timeout', 3176ebf5866SFelix Guo help='maximum number of seconds to allow for all tests ' 3186ebf5866SFelix Guo 'to run. This does not include time taken to build the ' 3196ebf5866SFelix Guo 'tests.', 3206ebf5866SFelix Guo type=int, 3216ebf5866SFelix Guo default=300, 3226ebf5866SFelix Guo metavar='timeout') 323d992880bSDaniel Latypov parser.add_argument('filter_glob', 324a127b154SDaniel Latypov help='Filter which KUnit test suites/tests run at ' 325a127b154SDaniel Latypov 'boot-time, e.g. list* or list*.*del_test', 326d992880bSDaniel Latypov type=str, 327d992880bSDaniel Latypov nargs='?', 328d992880bSDaniel Latypov default='', 329d992880bSDaniel Latypov metavar='filter_glob') 3306cb51a18SDaniel Latypov parser.add_argument('--kernel_args', 3316cb51a18SDaniel Latypov help='Kernel command-line parameters. Maybe be repeated', 3326cb51a18SDaniel Latypov action='append') 333ff9e09a3SDaniel Latypov parser.add_argument('--run_isolated', help='If set, boot the kernel for each ' 334ff9e09a3SDaniel Latypov 'individual suite/test. This is can be useful for debugging ' 335ff9e09a3SDaniel Latypov 'a non-hermetic test, one that might pass/fail based on ' 336ff9e09a3SDaniel Latypov 'what ran before it.', 337ff9e09a3SDaniel Latypov type=str, 338ff9e09a3SDaniel Latypov choices=['suite', 'test']), 3396ebf5866SFelix Guo 34009641f7cSDaniel Latypovdef add_parse_opts(parser) -> None: 3416a499c9cSDaniel Latypov parser.add_argument('--raw_output', help='If set don\'t format output from kernel. ' 3426a499c9cSDaniel Latypov 'If set to --raw_output=kunit, filters to just KUnit output.', 3436a499c9cSDaniel Latypov type=str, nargs='?', const='all', default=None) 34421a6d178SHeidi Fahim parser.add_argument('--json', 34521a6d178SHeidi Fahim nargs='?', 34621a6d178SHeidi Fahim help='Stores test results in a JSON, and either ' 34721a6d178SHeidi Fahim 'prints to stdout or saves to file if a ' 34821a6d178SHeidi Fahim 'filename is specified', 34921a6d178SHeidi Fahim type=str, const='stdout', default=None) 350021ed9f5SHeidi Fahim 35145ba7a89SDavid Gowdef main(argv, linux=None): 35245ba7a89SDavid Gow parser = argparse.ArgumentParser( 35345ba7a89SDavid Gow description='Helps writing and running KUnit tests.') 35445ba7a89SDavid Gow subparser = parser.add_subparsers(dest='subcommand') 35545ba7a89SDavid Gow 35645ba7a89SDavid Gow # The 'run' command will config, build, exec, and parse in one go. 35745ba7a89SDavid Gow run_parser = subparser.add_parser('run', help='Runs KUnit tests.') 35845ba7a89SDavid Gow add_common_opts(run_parser) 35945ba7a89SDavid Gow add_build_opts(run_parser) 36045ba7a89SDavid Gow add_exec_opts(run_parser) 36145ba7a89SDavid Gow add_parse_opts(run_parser) 36245ba7a89SDavid Gow 36345ba7a89SDavid Gow config_parser = subparser.add_parser('config', 36445ba7a89SDavid Gow help='Ensures that .config contains all of ' 36545ba7a89SDavid Gow 'the options in .kunitconfig') 36645ba7a89SDavid Gow add_common_opts(config_parser) 36745ba7a89SDavid Gow 36845ba7a89SDavid Gow build_parser = subparser.add_parser('build', help='Builds a kernel with KUnit tests') 36945ba7a89SDavid Gow add_common_opts(build_parser) 37045ba7a89SDavid Gow add_build_opts(build_parser) 37145ba7a89SDavid Gow 37245ba7a89SDavid Gow exec_parser = subparser.add_parser('exec', help='Run a kernel with KUnit tests') 37345ba7a89SDavid Gow add_common_opts(exec_parser) 37445ba7a89SDavid Gow add_exec_opts(exec_parser) 37545ba7a89SDavid Gow add_parse_opts(exec_parser) 37645ba7a89SDavid Gow 37745ba7a89SDavid Gow # The 'parse' option is special, as it doesn't need the kernel source 37845ba7a89SDavid Gow # (therefore there is no need for a build_dir, hence no add_common_opts) 37945ba7a89SDavid Gow # and the '--file' argument is not relevant to 'run', so isn't in 38045ba7a89SDavid Gow # add_parse_opts() 38145ba7a89SDavid Gow parse_parser = subparser.add_parser('parse', 38245ba7a89SDavid Gow help='Parses KUnit results from a file, ' 38345ba7a89SDavid Gow 'and parses formatted results.') 38445ba7a89SDavid Gow add_parse_opts(parse_parser) 38545ba7a89SDavid Gow parse_parser.add_argument('file', 38645ba7a89SDavid Gow help='Specifies the file to read results from.', 38745ba7a89SDavid Gow type=str, nargs='?', metavar='input_file') 3880476e69fSGreg Thelen 389d8c23eadSDaniel Latypov cli_args = parser.parse_args(massage_argv(argv)) 3906ebf5866SFelix Guo 3915578d008SBrendan Higgins if get_kernel_root_path(): 3925578d008SBrendan Higgins os.chdir(get_kernel_root_path()) 3935578d008SBrendan Higgins 3946ebf5866SFelix Guo if cli_args.subcommand == 'run': 395e3212513SSeongJae Park if not os.path.exists(cli_args.build_dir): 396e3212513SSeongJae Park os.mkdir(cli_args.build_dir) 39782206a0cSBrendan Higgins 398ff7b437fSBrendan Higgins if not linux: 39987c9c163SBrendan Higgins linux = kunit_kernel.LinuxSourceTree(cli_args.build_dir, 40087c9c163SBrendan Higgins kunitconfig_path=cli_args.kunitconfig, 40187c9c163SBrendan Higgins arch=cli_args.arch, 40287c9c163SBrendan Higgins cross_compile=cli_args.cross_compile, 40387c9c163SBrendan Higgins qemu_config_path=cli_args.qemu_config) 404fcdb0bc0SAndy Shevchenko 4056ebf5866SFelix Guo request = KunitRequest(cli_args.raw_output, 4066ebf5866SFelix Guo cli_args.timeout, 4076ebf5866SFelix Guo cli_args.jobs, 408ff7b437fSBrendan Higgins cli_args.build_dir, 4090476e69fSGreg Thelen cli_args.alltests, 410d992880bSDaniel Latypov cli_args.filter_glob, 4116cb51a18SDaniel Latypov cli_args.kernel_args, 412ff9e09a3SDaniel Latypov cli_args.run_isolated, 41321a6d178SHeidi Fahim cli_args.json, 4140476e69fSGreg Thelen cli_args.make_options) 4156ebf5866SFelix Guo result = run_tests(linux, request) 4166ebf5866SFelix Guo if result.status != KunitStatus.SUCCESS: 4176ebf5866SFelix Guo sys.exit(1) 41845ba7a89SDavid Gow elif cli_args.subcommand == 'config': 41982206a0cSBrendan Higgins if cli_args.build_dir and ( 42082206a0cSBrendan Higgins not os.path.exists(cli_args.build_dir)): 42145ba7a89SDavid Gow os.mkdir(cli_args.build_dir) 42282206a0cSBrendan Higgins 42345ba7a89SDavid Gow if not linux: 42487c9c163SBrendan Higgins linux = kunit_kernel.LinuxSourceTree(cli_args.build_dir, 42587c9c163SBrendan Higgins kunitconfig_path=cli_args.kunitconfig, 42687c9c163SBrendan Higgins arch=cli_args.arch, 42787c9c163SBrendan Higgins cross_compile=cli_args.cross_compile, 42887c9c163SBrendan Higgins qemu_config_path=cli_args.qemu_config) 429fcdb0bc0SAndy Shevchenko 43045ba7a89SDavid Gow request = KunitConfigRequest(cli_args.build_dir, 43145ba7a89SDavid Gow cli_args.make_options) 43245ba7a89SDavid Gow result = config_tests(linux, request) 43345ba7a89SDavid Gow kunit_parser.print_with_timestamp(( 43445ba7a89SDavid Gow 'Elapsed time: %.3fs\n') % ( 43545ba7a89SDavid Gow result.elapsed_time)) 43645ba7a89SDavid Gow if result.status != KunitStatus.SUCCESS: 43745ba7a89SDavid Gow sys.exit(1) 43845ba7a89SDavid Gow elif cli_args.subcommand == 'build': 43945ba7a89SDavid Gow if not linux: 44087c9c163SBrendan Higgins linux = kunit_kernel.LinuxSourceTree(cli_args.build_dir, 44187c9c163SBrendan Higgins kunitconfig_path=cli_args.kunitconfig, 44287c9c163SBrendan Higgins arch=cli_args.arch, 44387c9c163SBrendan Higgins cross_compile=cli_args.cross_compile, 44487c9c163SBrendan Higgins qemu_config_path=cli_args.qemu_config) 445fcdb0bc0SAndy Shevchenko 44645ba7a89SDavid Gow request = KunitBuildRequest(cli_args.jobs, 44745ba7a89SDavid Gow cli_args.build_dir, 44845ba7a89SDavid Gow cli_args.alltests, 44945ba7a89SDavid Gow cli_args.make_options) 45045ba7a89SDavid Gow result = build_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 == 'exec': 45745ba7a89SDavid Gow if not linux: 45887c9c163SBrendan Higgins linux = kunit_kernel.LinuxSourceTree(cli_args.build_dir, 45987c9c163SBrendan Higgins kunitconfig_path=cli_args.kunitconfig, 46087c9c163SBrendan Higgins arch=cli_args.arch, 46187c9c163SBrendan Higgins cross_compile=cli_args.cross_compile, 46287c9c163SBrendan Higgins qemu_config_path=cli_args.qemu_config) 463fcdb0bc0SAndy Shevchenko 46445ba7a89SDavid Gow exec_request = KunitExecRequest(cli_args.timeout, 46545ba7a89SDavid Gow cli_args.build_dir, 466d992880bSDaniel Latypov cli_args.alltests, 4676cb51a18SDaniel Latypov cli_args.filter_glob, 468ff9e09a3SDaniel Latypov cli_args.kernel_args, 469ff9e09a3SDaniel Latypov cli_args.run_isolated) 47045ba7a89SDavid Gow parse_request = KunitParseRequest(cli_args.raw_output, 47121a6d178SHeidi Fahim cli_args.build_dir, 47221a6d178SHeidi Fahim cli_args.json) 4737ef925eaSDaniel Latypov result = exec_tests(linux, exec_request, parse_request) 47445ba7a89SDavid Gow kunit_parser.print_with_timestamp(( 4757ef925eaSDaniel Latypov 'Elapsed time: %.3fs\n') % (result.elapsed_time)) 47645ba7a89SDavid Gow if result.status != KunitStatus.SUCCESS: 47745ba7a89SDavid Gow sys.exit(1) 47845ba7a89SDavid Gow elif cli_args.subcommand == 'parse': 47945ba7a89SDavid Gow if cli_args.file == None: 48045ba7a89SDavid Gow kunit_output = sys.stdin 48145ba7a89SDavid Gow else: 48245ba7a89SDavid Gow with open(cli_args.file, 'r') as f: 48345ba7a89SDavid Gow kunit_output = f.read().splitlines() 48445ba7a89SDavid Gow request = KunitParseRequest(cli_args.raw_output, 4853959d0a6SDavid Gow None, 48621a6d178SHeidi Fahim cli_args.json) 4877ef925eaSDaniel Latypov result = parse_tests(request, kunit_output) 48845ba7a89SDavid Gow if result.status != KunitStatus.SUCCESS: 48945ba7a89SDavid Gow sys.exit(1) 4906ebf5866SFelix Guo else: 4916ebf5866SFelix Guo parser.print_help() 4926ebf5866SFelix Guo 4936ebf5866SFelix Guoif __name__ == '__main__': 494ff7b437fSBrendan Higgins main(sys.argv[1:]) 495