xref: /linux/tools/testing/kunit/kunit.py (revision a127b154a8f231709754b5d56a501163dd837459)
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 sys
126ebf5866SFelix Guoimport os
136ebf5866SFelix Guoimport time
146ebf5866SFelix Guo
15df4b0807SSeongJae Parkassert sys.version_info >= (3, 7), "Python version is too old"
16df4b0807SSeongJae Park
176ebf5866SFelix Guofrom collections import namedtuple
186ebf5866SFelix Guofrom enum import Enum, auto
19d8c23eadSDaniel Latypovfrom typing import Iterable, Sequence
206ebf5866SFelix Guo
216ebf5866SFelix Guoimport kunit_config
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',
356cb51a18SDaniel Latypov                               'filter_glob', 'kernel_args'])
3645ba7a89SDavid GowKunitParseRequest = namedtuple('KunitParseRequest',
3721a6d178SHeidi Fahim			       ['raw_output', 'input_data', 'build_dir', 'json'])
38021ed9f5SHeidi FahimKunitRequest = namedtuple('KunitRequest', ['raw_output','timeout', 'jobs',
39d992880bSDaniel Latypov					   'build_dir', 'alltests', 'filter_glob',
406cb51a18SDaniel Latypov					   'kernel_args', '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
9445ba7a89SDavid Gowdef exec_tests(linux: kunit_kernel.LinuxSourceTree,
9545ba7a89SDavid Gow	       request: KunitExecRequest) -> KunitResult:
966ebf5866SFelix Guo	kunit_parser.print_with_timestamp('Starting KUnit Kernel ...')
976ebf5866SFelix Guo	test_start = time.time()
9845ba7a89SDavid Gow	result = linux.run_kernel(
996cb51a18SDaniel Latypov		args=request.kernel_args,
100021ed9f5SHeidi Fahim		timeout=None if request.alltests else request.timeout,
101d992880bSDaniel Latypov                filter_glob=request.filter_glob,
1026ec1b81dSSeongJae Park		build_dir=request.build_dir)
10345ba7a89SDavid Gow
1046ebf5866SFelix Guo	test_end = time.time()
1056ebf5866SFelix Guo
10645ba7a89SDavid Gow	return KunitResult(KunitStatus.SUCCESS,
10745ba7a89SDavid Gow			   result,
10845ba7a89SDavid Gow			   test_end - test_start)
10945ba7a89SDavid Gow
11045ba7a89SDavid Gowdef parse_tests(request: KunitParseRequest) -> KunitResult:
11145ba7a89SDavid Gow	parse_start = time.time()
11245ba7a89SDavid Gow
11345ba7a89SDavid Gow	test_result = kunit_parser.TestResult(kunit_parser.TestStatus.SUCCESS,
11445ba7a89SDavid Gow					      [],
11545ba7a89SDavid Gow					      'Tests not Parsed.')
11621a6d178SHeidi Fahim
11745ba7a89SDavid Gow	if request.raw_output:
1186a499c9cSDaniel Latypov		output: Iterable[str] = request.input_data
1196a499c9cSDaniel Latypov		if request.raw_output == 'all':
1206a499c9cSDaniel Latypov			pass
1216a499c9cSDaniel Latypov		elif request.raw_output == 'kunit':
1226a499c9cSDaniel Latypov			output = kunit_parser.extract_tap_lines(output)
1236a499c9cSDaniel Latypov		else:
1246a499c9cSDaniel Latypov			print(f'Unknown --raw_output option "{request.raw_output}"', file=sys.stderr)
1256a499c9cSDaniel Latypov		for line in output:
1266a499c9cSDaniel Latypov			print(line.rstrip())
1276a499c9cSDaniel Latypov
12845ba7a89SDavid Gow	else:
12945ba7a89SDavid Gow		test_result = kunit_parser.parse_run_tests(request.input_data)
13045ba7a89SDavid Gow	parse_end = time.time()
13145ba7a89SDavid Gow
13221a6d178SHeidi Fahim	if request.json:
13321a6d178SHeidi Fahim		json_obj = kunit_json.get_json_result(
13421a6d178SHeidi Fahim					test_result=test_result,
13521a6d178SHeidi Fahim					def_config='kunit_defconfig',
13621a6d178SHeidi Fahim					build_dir=request.build_dir,
13721a6d178SHeidi Fahim					json_path=request.json)
13821a6d178SHeidi Fahim		if request.json == 'stdout':
13921a6d178SHeidi Fahim			print(json_obj)
14021a6d178SHeidi Fahim
14145ba7a89SDavid Gow	if test_result.status != kunit_parser.TestStatus.SUCCESS:
14245ba7a89SDavid Gow		return KunitResult(KunitStatus.TEST_FAILURE, test_result,
14345ba7a89SDavid Gow				   parse_end - parse_start)
14445ba7a89SDavid Gow
14545ba7a89SDavid Gow	return KunitResult(KunitStatus.SUCCESS, test_result,
14645ba7a89SDavid Gow				parse_end - parse_start)
14745ba7a89SDavid Gow
14845ba7a89SDavid Gowdef run_tests(linux: kunit_kernel.LinuxSourceTree,
14945ba7a89SDavid Gow	      request: KunitRequest) -> KunitResult:
15045ba7a89SDavid Gow	run_start = time.time()
15145ba7a89SDavid Gow
15245ba7a89SDavid Gow	config_request = KunitConfigRequest(request.build_dir,
15345ba7a89SDavid Gow					    request.make_options)
15445ba7a89SDavid Gow	config_result = config_tests(linux, config_request)
15545ba7a89SDavid Gow	if config_result.status != KunitStatus.SUCCESS:
15645ba7a89SDavid Gow		return config_result
15745ba7a89SDavid Gow
15845ba7a89SDavid Gow	build_request = KunitBuildRequest(request.jobs, request.build_dir,
15945ba7a89SDavid Gow					  request.alltests,
16045ba7a89SDavid Gow					  request.make_options)
16145ba7a89SDavid Gow	build_result = build_tests(linux, build_request)
16245ba7a89SDavid Gow	if build_result.status != KunitStatus.SUCCESS:
16345ba7a89SDavid Gow		return build_result
16445ba7a89SDavid Gow
16545ba7a89SDavid Gow	exec_request = KunitExecRequest(request.timeout, request.build_dir,
1666cb51a18SDaniel Latypov				 request.alltests, request.filter_glob,
1676cb51a18SDaniel Latypov				 request.kernel_args)
16845ba7a89SDavid Gow	exec_result = exec_tests(linux, exec_request)
16945ba7a89SDavid Gow	if exec_result.status != KunitStatus.SUCCESS:
17045ba7a89SDavid Gow		return exec_result
17145ba7a89SDavid Gow
17245ba7a89SDavid Gow	parse_request = KunitParseRequest(request.raw_output,
17321a6d178SHeidi Fahim					  exec_result.result,
17421a6d178SHeidi Fahim					  request.build_dir,
17521a6d178SHeidi Fahim					  request.json)
17645ba7a89SDavid Gow	parse_result = parse_tests(parse_request)
17745ba7a89SDavid Gow
17845ba7a89SDavid Gow	run_end = time.time()
17945ba7a89SDavid Gow
1806ebf5866SFelix Guo	kunit_parser.print_with_timestamp((
1816ebf5866SFelix Guo		'Elapsed time: %.3fs total, %.3fs configuring, %.3fs ' +
1826ebf5866SFelix Guo		'building, %.3fs running\n') % (
18345ba7a89SDavid Gow				run_end - run_start,
18445ba7a89SDavid Gow				config_result.elapsed_time,
18545ba7a89SDavid Gow				build_result.elapsed_time,
18645ba7a89SDavid Gow				exec_result.elapsed_time))
18745ba7a89SDavid Gow	return parse_result
1886ebf5866SFelix Guo
189d8c23eadSDaniel Latypov# Problem:
190d8c23eadSDaniel Latypov# $ kunit.py run --json
191d8c23eadSDaniel Latypov# works as one would expect and prints the parsed test results as JSON.
192d8c23eadSDaniel Latypov# $ kunit.py run --json suite_name
193d8c23eadSDaniel Latypov# would *not* pass suite_name as the filter_glob and print as json.
194d8c23eadSDaniel Latypov# argparse will consider it to be another way of writing
195d8c23eadSDaniel Latypov# $ kunit.py run --json=suite_name
196d8c23eadSDaniel Latypov# i.e. it would run all tests, and dump the json to a `suite_name` file.
197d8c23eadSDaniel Latypov# So we hackily automatically rewrite --json => --json=stdout
198d8c23eadSDaniel Latypovpseudo_bool_flag_defaults = {
199d8c23eadSDaniel Latypov		'--json': 'stdout',
200d8c23eadSDaniel Latypov		'--raw_output': 'kunit',
201d8c23eadSDaniel Latypov}
202d8c23eadSDaniel Latypovdef massage_argv(argv: Sequence[str]) -> Sequence[str]:
203d8c23eadSDaniel Latypov	def massage_arg(arg: str) -> str:
204d8c23eadSDaniel Latypov		if arg not in pseudo_bool_flag_defaults:
205d8c23eadSDaniel Latypov			return arg
206d8c23eadSDaniel Latypov		return  f'{arg}={pseudo_bool_flag_defaults[arg]}'
207d8c23eadSDaniel Latypov	return list(map(massage_arg, argv))
208d8c23eadSDaniel Latypov
20909641f7cSDaniel Latypovdef add_common_opts(parser) -> None:
21045ba7a89SDavid Gow	parser.add_argument('--build_dir',
21145ba7a89SDavid Gow			    help='As in the make command, it specifies the build '
21245ba7a89SDavid Gow			    'directory.',
213ddbd60c7SVitor Massaru Iha			    type=str, default='.kunit', metavar='build_dir')
21445ba7a89SDavid Gow	parser.add_argument('--make_options',
21545ba7a89SDavid Gow			    help='X=Y make option, can be repeated.',
21645ba7a89SDavid Gow			    action='append')
21745ba7a89SDavid Gow	parser.add_argument('--alltests',
21845ba7a89SDavid Gow			    help='Run all KUnit tests through allyesconfig',
2196ebf5866SFelix Guo			    action='store_true')
220243180f5SDaniel Latypov	parser.add_argument('--kunitconfig',
2219854781dSDaniel Latypov			     help='Path to Kconfig fragment that enables KUnit tests.'
2229854781dSDaniel Latypov			     ' If given a directory, (e.g. lib/kunit), "/.kunitconfig" '
2239854781dSDaniel Latypov			     'will get  automatically appended.',
224243180f5SDaniel Latypov			     metavar='kunitconfig')
2256ebf5866SFelix Guo
22687c9c163SBrendan Higgins	parser.add_argument('--arch',
22787c9c163SBrendan Higgins			    help=('Specifies the architecture to run tests under. '
22887c9c163SBrendan Higgins				  'The architecture specified here must match the '
22987c9c163SBrendan Higgins				  'string passed to the ARCH make param, '
23087c9c163SBrendan Higgins				  'e.g. i386, x86_64, arm, um, etc. Non-UML '
23187c9c163SBrendan Higgins				  'architectures run on QEMU.'),
23287c9c163SBrendan Higgins			    type=str, default='um', metavar='arch')
23387c9c163SBrendan Higgins
23487c9c163SBrendan Higgins	parser.add_argument('--cross_compile',
23587c9c163SBrendan Higgins			    help=('Sets make\'s CROSS_COMPILE variable; it should '
23687c9c163SBrendan Higgins				  'be set to a toolchain path prefix (the prefix '
23787c9c163SBrendan Higgins				  'of gcc and other tools in your toolchain, for '
23887c9c163SBrendan Higgins				  'example `sparc64-linux-gnu-` if you have the '
23987c9c163SBrendan Higgins				  'sparc toolchain installed on your system, or '
24087c9c163SBrendan Higgins				  '`$HOME/toolchains/microblaze/gcc-9.2.0-nolibc/microblaze-linux/bin/microblaze-linux-` '
24187c9c163SBrendan Higgins				  'if you have downloaded the microblaze toolchain '
24287c9c163SBrendan Higgins				  'from the 0-day website to a directory in your '
24387c9c163SBrendan Higgins				  'home directory called `toolchains`).'),
24487c9c163SBrendan Higgins			    metavar='cross_compile')
24587c9c163SBrendan Higgins
24687c9c163SBrendan Higgins	parser.add_argument('--qemu_config',
24787c9c163SBrendan Higgins			    help=('Takes a path to a path to a file containing '
24887c9c163SBrendan Higgins				  'a QemuArchParams object.'),
24987c9c163SBrendan Higgins			    type=str, metavar='qemu_config')
25087c9c163SBrendan Higgins
25109641f7cSDaniel Latypovdef add_build_opts(parser) -> None:
25245ba7a89SDavid Gow	parser.add_argument('--jobs',
25345ba7a89SDavid Gow			    help='As in the make command, "Specifies  the number of '
25445ba7a89SDavid Gow			    'jobs (commands) to run simultaneously."',
25545ba7a89SDavid Gow			    type=int, default=8, metavar='jobs')
25645ba7a89SDavid Gow
25709641f7cSDaniel Latypovdef add_exec_opts(parser) -> None:
25845ba7a89SDavid Gow	parser.add_argument('--timeout',
2596ebf5866SFelix Guo			    help='maximum number of seconds to allow for all tests '
2606ebf5866SFelix Guo			    'to run. This does not include time taken to build the '
2616ebf5866SFelix Guo			    'tests.',
2626ebf5866SFelix Guo			    type=int,
2636ebf5866SFelix Guo			    default=300,
2646ebf5866SFelix Guo			    metavar='timeout')
265d992880bSDaniel Latypov	parser.add_argument('filter_glob',
266*a127b154SDaniel Latypov			    help='Filter which KUnit test suites/tests run at '
267*a127b154SDaniel Latypov			    'boot-time, e.g. list* or list*.*del_test',
268d992880bSDaniel Latypov			    type=str,
269d992880bSDaniel Latypov			    nargs='?',
270d992880bSDaniel Latypov			    default='',
271d992880bSDaniel Latypov			    metavar='filter_glob')
2726cb51a18SDaniel Latypov	parser.add_argument('--kernel_args',
2736cb51a18SDaniel Latypov			    help='Kernel command-line parameters. Maybe be repeated',
2746cb51a18SDaniel Latypov			     action='append')
2756ebf5866SFelix Guo
27609641f7cSDaniel Latypovdef add_parse_opts(parser) -> None:
2776a499c9cSDaniel Latypov	parser.add_argument('--raw_output', help='If set don\'t format output from kernel. '
2786a499c9cSDaniel Latypov			    'If set to --raw_output=kunit, filters to just KUnit output.',
2796a499c9cSDaniel Latypov			    type=str, nargs='?', const='all', default=None)
28021a6d178SHeidi Fahim	parser.add_argument('--json',
28121a6d178SHeidi Fahim			    nargs='?',
28221a6d178SHeidi Fahim			    help='Stores test results in a JSON, and either '
28321a6d178SHeidi Fahim			    'prints to stdout or saves to file if a '
28421a6d178SHeidi Fahim			    'filename is specified',
28521a6d178SHeidi Fahim			    type=str, const='stdout', default=None)
286021ed9f5SHeidi Fahim
28745ba7a89SDavid Gowdef main(argv, linux=None):
28845ba7a89SDavid Gow	parser = argparse.ArgumentParser(
28945ba7a89SDavid Gow			description='Helps writing and running KUnit tests.')
29045ba7a89SDavid Gow	subparser = parser.add_subparsers(dest='subcommand')
29145ba7a89SDavid Gow
29245ba7a89SDavid Gow	# The 'run' command will config, build, exec, and parse in one go.
29345ba7a89SDavid Gow	run_parser = subparser.add_parser('run', help='Runs KUnit tests.')
29445ba7a89SDavid Gow	add_common_opts(run_parser)
29545ba7a89SDavid Gow	add_build_opts(run_parser)
29645ba7a89SDavid Gow	add_exec_opts(run_parser)
29745ba7a89SDavid Gow	add_parse_opts(run_parser)
29845ba7a89SDavid Gow
29945ba7a89SDavid Gow	config_parser = subparser.add_parser('config',
30045ba7a89SDavid Gow						help='Ensures that .config contains all of '
30145ba7a89SDavid Gow						'the options in .kunitconfig')
30245ba7a89SDavid Gow	add_common_opts(config_parser)
30345ba7a89SDavid Gow
30445ba7a89SDavid Gow	build_parser = subparser.add_parser('build', help='Builds a kernel with KUnit tests')
30545ba7a89SDavid Gow	add_common_opts(build_parser)
30645ba7a89SDavid Gow	add_build_opts(build_parser)
30745ba7a89SDavid Gow
30845ba7a89SDavid Gow	exec_parser = subparser.add_parser('exec', help='Run a kernel with KUnit tests')
30945ba7a89SDavid Gow	add_common_opts(exec_parser)
31045ba7a89SDavid Gow	add_exec_opts(exec_parser)
31145ba7a89SDavid Gow	add_parse_opts(exec_parser)
31245ba7a89SDavid Gow
31345ba7a89SDavid Gow	# The 'parse' option is special, as it doesn't need the kernel source
31445ba7a89SDavid Gow	# (therefore there is no need for a build_dir, hence no add_common_opts)
31545ba7a89SDavid Gow	# and the '--file' argument is not relevant to 'run', so isn't in
31645ba7a89SDavid Gow	# add_parse_opts()
31745ba7a89SDavid Gow	parse_parser = subparser.add_parser('parse',
31845ba7a89SDavid Gow					    help='Parses KUnit results from a file, '
31945ba7a89SDavid Gow					    'and parses formatted results.')
32045ba7a89SDavid Gow	add_parse_opts(parse_parser)
32145ba7a89SDavid Gow	parse_parser.add_argument('file',
32245ba7a89SDavid Gow				  help='Specifies the file to read results from.',
32345ba7a89SDavid Gow				  type=str, nargs='?', metavar='input_file')
3240476e69fSGreg Thelen
325d8c23eadSDaniel Latypov	cli_args = parser.parse_args(massage_argv(argv))
3266ebf5866SFelix Guo
3275578d008SBrendan Higgins	if get_kernel_root_path():
3285578d008SBrendan Higgins		os.chdir(get_kernel_root_path())
3295578d008SBrendan Higgins
3306ebf5866SFelix Guo	if cli_args.subcommand == 'run':
331e3212513SSeongJae Park		if not os.path.exists(cli_args.build_dir):
332e3212513SSeongJae Park			os.mkdir(cli_args.build_dir)
33382206a0cSBrendan Higgins
334ff7b437fSBrendan Higgins		if not linux:
33587c9c163SBrendan Higgins			linux = kunit_kernel.LinuxSourceTree(cli_args.build_dir,
33687c9c163SBrendan Higgins					kunitconfig_path=cli_args.kunitconfig,
33787c9c163SBrendan Higgins					arch=cli_args.arch,
33887c9c163SBrendan Higgins					cross_compile=cli_args.cross_compile,
33987c9c163SBrendan Higgins					qemu_config_path=cli_args.qemu_config)
340fcdb0bc0SAndy Shevchenko
3416ebf5866SFelix Guo		request = KunitRequest(cli_args.raw_output,
3426ebf5866SFelix Guo				       cli_args.timeout,
3436ebf5866SFelix Guo				       cli_args.jobs,
344ff7b437fSBrendan Higgins				       cli_args.build_dir,
3450476e69fSGreg Thelen				       cli_args.alltests,
346d992880bSDaniel Latypov				       cli_args.filter_glob,
3476cb51a18SDaniel Latypov				       cli_args.kernel_args,
34821a6d178SHeidi Fahim				       cli_args.json,
3490476e69fSGreg Thelen				       cli_args.make_options)
3506ebf5866SFelix Guo		result = run_tests(linux, request)
3516ebf5866SFelix Guo		if result.status != KunitStatus.SUCCESS:
3526ebf5866SFelix Guo			sys.exit(1)
35345ba7a89SDavid Gow	elif cli_args.subcommand == 'config':
35482206a0cSBrendan Higgins		if cli_args.build_dir and (
35582206a0cSBrendan Higgins				not os.path.exists(cli_args.build_dir)):
35645ba7a89SDavid Gow			os.mkdir(cli_args.build_dir)
35782206a0cSBrendan Higgins
35845ba7a89SDavid Gow		if not linux:
35987c9c163SBrendan Higgins			linux = kunit_kernel.LinuxSourceTree(cli_args.build_dir,
36087c9c163SBrendan Higgins					kunitconfig_path=cli_args.kunitconfig,
36187c9c163SBrendan Higgins					arch=cli_args.arch,
36287c9c163SBrendan Higgins					cross_compile=cli_args.cross_compile,
36387c9c163SBrendan Higgins					qemu_config_path=cli_args.qemu_config)
364fcdb0bc0SAndy Shevchenko
36545ba7a89SDavid Gow		request = KunitConfigRequest(cli_args.build_dir,
36645ba7a89SDavid Gow					     cli_args.make_options)
36745ba7a89SDavid Gow		result = config_tests(linux, request)
36845ba7a89SDavid Gow		kunit_parser.print_with_timestamp((
36945ba7a89SDavid Gow			'Elapsed time: %.3fs\n') % (
37045ba7a89SDavid Gow				result.elapsed_time))
37145ba7a89SDavid Gow		if result.status != KunitStatus.SUCCESS:
37245ba7a89SDavid Gow			sys.exit(1)
37345ba7a89SDavid Gow	elif cli_args.subcommand == 'build':
37445ba7a89SDavid Gow		if not linux:
37587c9c163SBrendan Higgins			linux = kunit_kernel.LinuxSourceTree(cli_args.build_dir,
37687c9c163SBrendan Higgins					kunitconfig_path=cli_args.kunitconfig,
37787c9c163SBrendan Higgins					arch=cli_args.arch,
37887c9c163SBrendan Higgins					cross_compile=cli_args.cross_compile,
37987c9c163SBrendan Higgins					qemu_config_path=cli_args.qemu_config)
380fcdb0bc0SAndy Shevchenko
38145ba7a89SDavid Gow		request = KunitBuildRequest(cli_args.jobs,
38245ba7a89SDavid Gow					    cli_args.build_dir,
38345ba7a89SDavid Gow					    cli_args.alltests,
38445ba7a89SDavid Gow					    cli_args.make_options)
38545ba7a89SDavid Gow		result = build_tests(linux, request)
38645ba7a89SDavid Gow		kunit_parser.print_with_timestamp((
38745ba7a89SDavid Gow			'Elapsed time: %.3fs\n') % (
38845ba7a89SDavid Gow				result.elapsed_time))
38945ba7a89SDavid Gow		if result.status != KunitStatus.SUCCESS:
39045ba7a89SDavid Gow			sys.exit(1)
39145ba7a89SDavid Gow	elif cli_args.subcommand == 'exec':
39245ba7a89SDavid Gow		if not linux:
39387c9c163SBrendan Higgins			linux = kunit_kernel.LinuxSourceTree(cli_args.build_dir,
39487c9c163SBrendan Higgins					kunitconfig_path=cli_args.kunitconfig,
39587c9c163SBrendan Higgins					arch=cli_args.arch,
39687c9c163SBrendan Higgins					cross_compile=cli_args.cross_compile,
39787c9c163SBrendan Higgins					qemu_config_path=cli_args.qemu_config)
398fcdb0bc0SAndy Shevchenko
39945ba7a89SDavid Gow		exec_request = KunitExecRequest(cli_args.timeout,
40045ba7a89SDavid Gow						cli_args.build_dir,
401d992880bSDaniel Latypov						cli_args.alltests,
4026cb51a18SDaniel Latypov						cli_args.filter_glob,
4036cb51a18SDaniel Latypov						cli_args.kernel_args)
40445ba7a89SDavid Gow		exec_result = exec_tests(linux, exec_request)
40545ba7a89SDavid Gow		parse_request = KunitParseRequest(cli_args.raw_output,
40621a6d178SHeidi Fahim						  exec_result.result,
40721a6d178SHeidi Fahim						  cli_args.build_dir,
40821a6d178SHeidi Fahim						  cli_args.json)
40945ba7a89SDavid Gow		result = parse_tests(parse_request)
41045ba7a89SDavid Gow		kunit_parser.print_with_timestamp((
41145ba7a89SDavid Gow			'Elapsed time: %.3fs\n') % (
41245ba7a89SDavid Gow				exec_result.elapsed_time))
41345ba7a89SDavid Gow		if result.status != KunitStatus.SUCCESS:
41445ba7a89SDavid Gow			sys.exit(1)
41545ba7a89SDavid Gow	elif cli_args.subcommand == 'parse':
41645ba7a89SDavid Gow		if cli_args.file == None:
41745ba7a89SDavid Gow			kunit_output = sys.stdin
41845ba7a89SDavid Gow		else:
41945ba7a89SDavid Gow			with open(cli_args.file, 'r') as f:
42045ba7a89SDavid Gow				kunit_output = f.read().splitlines()
42145ba7a89SDavid Gow		request = KunitParseRequest(cli_args.raw_output,
42221a6d178SHeidi Fahim					    kunit_output,
4233959d0a6SDavid Gow					    None,
42421a6d178SHeidi Fahim					    cli_args.json)
42545ba7a89SDavid Gow		result = parse_tests(request)
42645ba7a89SDavid Gow		if result.status != KunitStatus.SUCCESS:
42745ba7a89SDavid Gow			sys.exit(1)
4286ebf5866SFelix Guo	else:
4296ebf5866SFelix Guo		parser.print_help()
4306ebf5866SFelix Guo
4316ebf5866SFelix Guoif __name__ == '__main__':
432ff7b437fSBrendan Higgins	main(sys.argv[1:])
433