xref: /openbmc/linux/tools/testing/kunit/kunit.py (revision ff9e09a3)
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
12*ff9e09a3SDaniel Latypovimport re
13*ff9e09a3SDaniel 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
20*ff9e09a3SDaniel 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',
35*ff9e09a3SDaniel 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',
40*ff9e09a3SDaniel 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
94*ff9e09a3SDaniel Latypovdef _list_tests(linux: kunit_kernel.LinuxSourceTree, request: KunitExecRequest) -> List[str]:
95*ff9e09a3SDaniel Latypov	args = ['kunit.action=list']
96*ff9e09a3SDaniel Latypov	if request.kernel_args:
97*ff9e09a3SDaniel Latypov		args.extend(request.kernel_args)
98*ff9e09a3SDaniel Latypov
99*ff9e09a3SDaniel Latypov	output = linux.run_kernel(args=args,
100*ff9e09a3SDaniel Latypov			   timeout=None if request.alltests else request.timeout,
101*ff9e09a3SDaniel Latypov			   filter_glob=request.filter_glob,
102*ff9e09a3SDaniel Latypov			   build_dir=request.build_dir)
103*ff9e09a3SDaniel Latypov	lines = kunit_parser.extract_tap_lines(output)
104*ff9e09a3SDaniel Latypov	# Hack! Drop the dummy TAP version header that the executor prints out.
105*ff9e09a3SDaniel Latypov	lines.pop()
106*ff9e09a3SDaniel Latypov
107*ff9e09a3SDaniel Latypov	# Filter out any extraneous non-test output that might have gotten mixed in.
108*ff9e09a3SDaniel Latypov	return [l for l in lines if re.match('^[^\s.]+\.[^\s.]+$', l)]
109*ff9e09a3SDaniel Latypov
110*ff9e09a3SDaniel Latypovdef _suites_from_test_list(tests: List[str]) -> List[str]:
111*ff9e09a3SDaniel Latypov	"""Extracts all the suites from an ordered list of tests."""
112*ff9e09a3SDaniel Latypov	suites = []  # type: List[str]
113*ff9e09a3SDaniel Latypov	for t in tests:
114*ff9e09a3SDaniel Latypov		parts = t.split('.', maxsplit=2)
115*ff9e09a3SDaniel Latypov		if len(parts) != 2:
116*ff9e09a3SDaniel Latypov			raise ValueError(f'internal KUnit error, test name should be of the form "<suite>.<test>", got "{t}"')
117*ff9e09a3SDaniel Latypov		suite, case = parts
118*ff9e09a3SDaniel Latypov		if not suites or suites[-1] != suite:
119*ff9e09a3SDaniel Latypov			suites.append(suite)
120*ff9e09a3SDaniel Latypov	return suites
121*ff9e09a3SDaniel Latypov
122*ff9e09a3SDaniel Latypov
123*ff9e09a3SDaniel Latypov
1247ef925eaSDaniel Latypovdef exec_tests(linux: kunit_kernel.LinuxSourceTree, request: KunitExecRequest,
1257ef925eaSDaniel Latypov	       parse_request: KunitParseRequest) -> KunitResult:
126*ff9e09a3SDaniel Latypov	filter_globs = [request.filter_glob]
127*ff9e09a3SDaniel Latypov	if request.run_isolated:
128*ff9e09a3SDaniel Latypov		tests = _list_tests(linux, request)
129*ff9e09a3SDaniel Latypov		if request.run_isolated == 'test':
130*ff9e09a3SDaniel Latypov			filter_globs = tests
131*ff9e09a3SDaniel Latypov		if request.run_isolated == 'suite':
132*ff9e09a3SDaniel Latypov			filter_globs = _suites_from_test_list(tests)
133*ff9e09a3SDaniel Latypov			# Apply the test-part of the user's glob, if present.
134*ff9e09a3SDaniel Latypov			if '.' in request.filter_glob:
135*ff9e09a3SDaniel Latypov				test_glob = request.filter_glob.split('.', maxsplit=2)[1]
136*ff9e09a3SDaniel Latypov				filter_globs = [g + '.'+ test_glob for g in filter_globs]
137*ff9e09a3SDaniel Latypov
138*ff9e09a3SDaniel Latypov	overall_status = kunit_parser.TestStatus.SUCCESS
139*ff9e09a3SDaniel Latypov	exec_time = 0.0
140*ff9e09a3SDaniel Latypov	for i, filter_glob in enumerate(filter_globs):
141*ff9e09a3SDaniel Latypov		kunit_parser.print_with_timestamp('Starting KUnit Kernel ({}/{})...'.format(i+1, len(filter_globs)))
142*ff9e09a3SDaniel 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,
147*ff9e09a3SDaniel 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()
155*ff9e09a3SDaniel Latypov		exec_time += test_end - test_start
156*ff9e09a3SDaniel Latypov
157*ff9e09a3SDaniel Latypov		overall_status = kunit_parser.max_status(overall_status, result.status)
1586ebf5866SFelix Guo
1597ef925eaSDaniel Latypov	return KunitResult(status=result.status, result=result.result, elapsed_time=exec_time)
1607ef925eaSDaniel Latypov
1617ef925eaSDaniel Latypovdef parse_tests(request: KunitParseRequest, input_data: Iterable[str]) -> KunitResult:
16245ba7a89SDavid Gow	parse_start = time.time()
16345ba7a89SDavid Gow
16445ba7a89SDavid Gow	test_result = kunit_parser.TestResult(kunit_parser.TestStatus.SUCCESS,
16545ba7a89SDavid Gow					      [],
16645ba7a89SDavid Gow					      'Tests not Parsed.')
16721a6d178SHeidi Fahim
16845ba7a89SDavid Gow	if request.raw_output:
1697ef925eaSDaniel Latypov		output: Iterable[str] = input_data
1706a499c9cSDaniel Latypov		if request.raw_output == 'all':
1716a499c9cSDaniel Latypov			pass
1726a499c9cSDaniel Latypov		elif request.raw_output == 'kunit':
1736a499c9cSDaniel Latypov			output = kunit_parser.extract_tap_lines(output)
1746a499c9cSDaniel Latypov		else:
1756a499c9cSDaniel Latypov			print(f'Unknown --raw_output option "{request.raw_output}"', file=sys.stderr)
1766a499c9cSDaniel Latypov		for line in output:
1776a499c9cSDaniel Latypov			print(line.rstrip())
1786a499c9cSDaniel Latypov
17945ba7a89SDavid Gow	else:
1807ef925eaSDaniel Latypov		test_result = kunit_parser.parse_run_tests(input_data)
18145ba7a89SDavid Gow	parse_end = time.time()
18245ba7a89SDavid Gow
18321a6d178SHeidi Fahim	if request.json:
18421a6d178SHeidi Fahim		json_obj = kunit_json.get_json_result(
18521a6d178SHeidi Fahim					test_result=test_result,
18621a6d178SHeidi Fahim					def_config='kunit_defconfig',
18721a6d178SHeidi Fahim					build_dir=request.build_dir,
18821a6d178SHeidi Fahim					json_path=request.json)
18921a6d178SHeidi Fahim		if request.json == 'stdout':
19021a6d178SHeidi Fahim			print(json_obj)
19121a6d178SHeidi Fahim
19245ba7a89SDavid Gow	if test_result.status != kunit_parser.TestStatus.SUCCESS:
19345ba7a89SDavid Gow		return KunitResult(KunitStatus.TEST_FAILURE, test_result,
19445ba7a89SDavid Gow				   parse_end - parse_start)
19545ba7a89SDavid Gow
19645ba7a89SDavid Gow	return KunitResult(KunitStatus.SUCCESS, test_result,
19745ba7a89SDavid Gow				parse_end - parse_start)
19845ba7a89SDavid Gow
19945ba7a89SDavid Gowdef run_tests(linux: kunit_kernel.LinuxSourceTree,
20045ba7a89SDavid Gow	      request: KunitRequest) -> KunitResult:
20145ba7a89SDavid Gow	run_start = time.time()
20245ba7a89SDavid Gow
20345ba7a89SDavid Gow	config_request = KunitConfigRequest(request.build_dir,
20445ba7a89SDavid Gow					    request.make_options)
20545ba7a89SDavid Gow	config_result = config_tests(linux, config_request)
20645ba7a89SDavid Gow	if config_result.status != KunitStatus.SUCCESS:
20745ba7a89SDavid Gow		return config_result
20845ba7a89SDavid Gow
20945ba7a89SDavid Gow	build_request = KunitBuildRequest(request.jobs, request.build_dir,
21045ba7a89SDavid Gow					  request.alltests,
21145ba7a89SDavid Gow					  request.make_options)
21245ba7a89SDavid Gow	build_result = build_tests(linux, build_request)
21345ba7a89SDavid Gow	if build_result.status != KunitStatus.SUCCESS:
21445ba7a89SDavid Gow		return build_result
21545ba7a89SDavid Gow
21645ba7a89SDavid Gow	exec_request = KunitExecRequest(request.timeout, request.build_dir,
2176cb51a18SDaniel Latypov				 request.alltests, request.filter_glob,
218*ff9e09a3SDaniel Latypov				 request.kernel_args, request.run_isolated)
21945ba7a89SDavid Gow	parse_request = KunitParseRequest(request.raw_output,
22021a6d178SHeidi Fahim					  request.build_dir,
22121a6d178SHeidi Fahim					  request.json)
2227ef925eaSDaniel Latypov
2237ef925eaSDaniel Latypov	exec_result = exec_tests(linux, exec_request, parse_request)
22445ba7a89SDavid Gow
22545ba7a89SDavid Gow	run_end = time.time()
22645ba7a89SDavid Gow
2276ebf5866SFelix Guo	kunit_parser.print_with_timestamp((
2286ebf5866SFelix Guo		'Elapsed time: %.3fs total, %.3fs configuring, %.3fs ' +
2296ebf5866SFelix Guo		'building, %.3fs running\n') % (
23045ba7a89SDavid Gow				run_end - run_start,
23145ba7a89SDavid Gow				config_result.elapsed_time,
23245ba7a89SDavid Gow				build_result.elapsed_time,
23345ba7a89SDavid Gow				exec_result.elapsed_time))
2347ef925eaSDaniel Latypov	return exec_result
2356ebf5866SFelix Guo
236d8c23eadSDaniel Latypov# Problem:
237d8c23eadSDaniel Latypov# $ kunit.py run --json
238d8c23eadSDaniel Latypov# works as one would expect and prints the parsed test results as JSON.
239d8c23eadSDaniel Latypov# $ kunit.py run --json suite_name
240d8c23eadSDaniel Latypov# would *not* pass suite_name as the filter_glob and print as json.
241d8c23eadSDaniel Latypov# argparse will consider it to be another way of writing
242d8c23eadSDaniel Latypov# $ kunit.py run --json=suite_name
243d8c23eadSDaniel Latypov# i.e. it would run all tests, and dump the json to a `suite_name` file.
244d8c23eadSDaniel Latypov# So we hackily automatically rewrite --json => --json=stdout
245d8c23eadSDaniel Latypovpseudo_bool_flag_defaults = {
246d8c23eadSDaniel Latypov		'--json': 'stdout',
247d8c23eadSDaniel Latypov		'--raw_output': 'kunit',
248d8c23eadSDaniel Latypov}
249d8c23eadSDaniel Latypovdef massage_argv(argv: Sequence[str]) -> Sequence[str]:
250d8c23eadSDaniel Latypov	def massage_arg(arg: str) -> str:
251d8c23eadSDaniel Latypov		if arg not in pseudo_bool_flag_defaults:
252d8c23eadSDaniel Latypov			return arg
253d8c23eadSDaniel Latypov		return  f'{arg}={pseudo_bool_flag_defaults[arg]}'
254d8c23eadSDaniel Latypov	return list(map(massage_arg, argv))
255d8c23eadSDaniel Latypov
25609641f7cSDaniel Latypovdef add_common_opts(parser) -> None:
25745ba7a89SDavid Gow	parser.add_argument('--build_dir',
25845ba7a89SDavid Gow			    help='As in the make command, it specifies the build '
25945ba7a89SDavid Gow			    'directory.',
260ddbd60c7SVitor Massaru Iha			    type=str, default='.kunit', metavar='build_dir')
26145ba7a89SDavid Gow	parser.add_argument('--make_options',
26245ba7a89SDavid Gow			    help='X=Y make option, can be repeated.',
26345ba7a89SDavid Gow			    action='append')
26445ba7a89SDavid Gow	parser.add_argument('--alltests',
26545ba7a89SDavid Gow			    help='Run all KUnit tests through allyesconfig',
2666ebf5866SFelix Guo			    action='store_true')
267243180f5SDaniel Latypov	parser.add_argument('--kunitconfig',
2689854781dSDaniel Latypov			     help='Path to Kconfig fragment that enables KUnit tests.'
2699854781dSDaniel Latypov			     ' If given a directory, (e.g. lib/kunit), "/.kunitconfig" '
2709854781dSDaniel Latypov			     'will get  automatically appended.',
271243180f5SDaniel Latypov			     metavar='kunitconfig')
2726ebf5866SFelix Guo
27387c9c163SBrendan Higgins	parser.add_argument('--arch',
27487c9c163SBrendan Higgins			    help=('Specifies the architecture to run tests under. '
27587c9c163SBrendan Higgins				  'The architecture specified here must match the '
27687c9c163SBrendan Higgins				  'string passed to the ARCH make param, '
27787c9c163SBrendan Higgins				  'e.g. i386, x86_64, arm, um, etc. Non-UML '
27887c9c163SBrendan Higgins				  'architectures run on QEMU.'),
27987c9c163SBrendan Higgins			    type=str, default='um', metavar='arch')
28087c9c163SBrendan Higgins
28187c9c163SBrendan Higgins	parser.add_argument('--cross_compile',
28287c9c163SBrendan Higgins			    help=('Sets make\'s CROSS_COMPILE variable; it should '
28387c9c163SBrendan Higgins				  'be set to a toolchain path prefix (the prefix '
28487c9c163SBrendan Higgins				  'of gcc and other tools in your toolchain, for '
28587c9c163SBrendan Higgins				  'example `sparc64-linux-gnu-` if you have the '
28687c9c163SBrendan Higgins				  'sparc toolchain installed on your system, or '
28787c9c163SBrendan Higgins				  '`$HOME/toolchains/microblaze/gcc-9.2.0-nolibc/microblaze-linux/bin/microblaze-linux-` '
28887c9c163SBrendan Higgins				  'if you have downloaded the microblaze toolchain '
28987c9c163SBrendan Higgins				  'from the 0-day website to a directory in your '
29087c9c163SBrendan Higgins				  'home directory called `toolchains`).'),
29187c9c163SBrendan Higgins			    metavar='cross_compile')
29287c9c163SBrendan Higgins
29387c9c163SBrendan Higgins	parser.add_argument('--qemu_config',
29487c9c163SBrendan Higgins			    help=('Takes a path to a path to a file containing '
29587c9c163SBrendan Higgins				  'a QemuArchParams object.'),
29687c9c163SBrendan Higgins			    type=str, metavar='qemu_config')
29787c9c163SBrendan Higgins
29809641f7cSDaniel Latypovdef add_build_opts(parser) -> None:
29945ba7a89SDavid Gow	parser.add_argument('--jobs',
30045ba7a89SDavid Gow			    help='As in the make command, "Specifies  the number of '
30145ba7a89SDavid Gow			    'jobs (commands) to run simultaneously."',
30245ba7a89SDavid Gow			    type=int, default=8, metavar='jobs')
30345ba7a89SDavid Gow
30409641f7cSDaniel Latypovdef add_exec_opts(parser) -> None:
30545ba7a89SDavid Gow	parser.add_argument('--timeout',
3066ebf5866SFelix Guo			    help='maximum number of seconds to allow for all tests '
3076ebf5866SFelix Guo			    'to run. This does not include time taken to build the '
3086ebf5866SFelix Guo			    'tests.',
3096ebf5866SFelix Guo			    type=int,
3106ebf5866SFelix Guo			    default=300,
3116ebf5866SFelix Guo			    metavar='timeout')
312d992880bSDaniel Latypov	parser.add_argument('filter_glob',
313a127b154SDaniel Latypov			    help='Filter which KUnit test suites/tests run at '
314a127b154SDaniel Latypov			    'boot-time, e.g. list* or list*.*del_test',
315d992880bSDaniel Latypov			    type=str,
316d992880bSDaniel Latypov			    nargs='?',
317d992880bSDaniel Latypov			    default='',
318d992880bSDaniel Latypov			    metavar='filter_glob')
3196cb51a18SDaniel Latypov	parser.add_argument('--kernel_args',
3206cb51a18SDaniel Latypov			    help='Kernel command-line parameters. Maybe be repeated',
3216cb51a18SDaniel Latypov			     action='append')
322*ff9e09a3SDaniel Latypov	parser.add_argument('--run_isolated', help='If set, boot the kernel for each '
323*ff9e09a3SDaniel Latypov			    'individual suite/test. This is can be useful for debugging '
324*ff9e09a3SDaniel Latypov			    'a non-hermetic test, one that might pass/fail based on '
325*ff9e09a3SDaniel Latypov			    'what ran before it.',
326*ff9e09a3SDaniel Latypov			    type=str,
327*ff9e09a3SDaniel Latypov			    choices=['suite', 'test']),
3286ebf5866SFelix Guo
32909641f7cSDaniel Latypovdef add_parse_opts(parser) -> None:
3306a499c9cSDaniel Latypov	parser.add_argument('--raw_output', help='If set don\'t format output from kernel. '
3316a499c9cSDaniel Latypov			    'If set to --raw_output=kunit, filters to just KUnit output.',
3326a499c9cSDaniel Latypov			    type=str, nargs='?', const='all', default=None)
33321a6d178SHeidi Fahim	parser.add_argument('--json',
33421a6d178SHeidi Fahim			    nargs='?',
33521a6d178SHeidi Fahim			    help='Stores test results in a JSON, and either '
33621a6d178SHeidi Fahim			    'prints to stdout or saves to file if a '
33721a6d178SHeidi Fahim			    'filename is specified',
33821a6d178SHeidi Fahim			    type=str, const='stdout', default=None)
339021ed9f5SHeidi Fahim
34045ba7a89SDavid Gowdef main(argv, linux=None):
34145ba7a89SDavid Gow	parser = argparse.ArgumentParser(
34245ba7a89SDavid Gow			description='Helps writing and running KUnit tests.')
34345ba7a89SDavid Gow	subparser = parser.add_subparsers(dest='subcommand')
34445ba7a89SDavid Gow
34545ba7a89SDavid Gow	# The 'run' command will config, build, exec, and parse in one go.
34645ba7a89SDavid Gow	run_parser = subparser.add_parser('run', help='Runs KUnit tests.')
34745ba7a89SDavid Gow	add_common_opts(run_parser)
34845ba7a89SDavid Gow	add_build_opts(run_parser)
34945ba7a89SDavid Gow	add_exec_opts(run_parser)
35045ba7a89SDavid Gow	add_parse_opts(run_parser)
35145ba7a89SDavid Gow
35245ba7a89SDavid Gow	config_parser = subparser.add_parser('config',
35345ba7a89SDavid Gow						help='Ensures that .config contains all of '
35445ba7a89SDavid Gow						'the options in .kunitconfig')
35545ba7a89SDavid Gow	add_common_opts(config_parser)
35645ba7a89SDavid Gow
35745ba7a89SDavid Gow	build_parser = subparser.add_parser('build', help='Builds a kernel with KUnit tests')
35845ba7a89SDavid Gow	add_common_opts(build_parser)
35945ba7a89SDavid Gow	add_build_opts(build_parser)
36045ba7a89SDavid Gow
36145ba7a89SDavid Gow	exec_parser = subparser.add_parser('exec', help='Run a kernel with KUnit tests')
36245ba7a89SDavid Gow	add_common_opts(exec_parser)
36345ba7a89SDavid Gow	add_exec_opts(exec_parser)
36445ba7a89SDavid Gow	add_parse_opts(exec_parser)
36545ba7a89SDavid Gow
36645ba7a89SDavid Gow	# The 'parse' option is special, as it doesn't need the kernel source
36745ba7a89SDavid Gow	# (therefore there is no need for a build_dir, hence no add_common_opts)
36845ba7a89SDavid Gow	# and the '--file' argument is not relevant to 'run', so isn't in
36945ba7a89SDavid Gow	# add_parse_opts()
37045ba7a89SDavid Gow	parse_parser = subparser.add_parser('parse',
37145ba7a89SDavid Gow					    help='Parses KUnit results from a file, '
37245ba7a89SDavid Gow					    'and parses formatted results.')
37345ba7a89SDavid Gow	add_parse_opts(parse_parser)
37445ba7a89SDavid Gow	parse_parser.add_argument('file',
37545ba7a89SDavid Gow				  help='Specifies the file to read results from.',
37645ba7a89SDavid Gow				  type=str, nargs='?', metavar='input_file')
3770476e69fSGreg Thelen
378d8c23eadSDaniel Latypov	cli_args = parser.parse_args(massage_argv(argv))
3796ebf5866SFelix Guo
3805578d008SBrendan Higgins	if get_kernel_root_path():
3815578d008SBrendan Higgins		os.chdir(get_kernel_root_path())
3825578d008SBrendan Higgins
3836ebf5866SFelix Guo	if cli_args.subcommand == 'run':
384e3212513SSeongJae Park		if not os.path.exists(cli_args.build_dir):
385e3212513SSeongJae Park			os.mkdir(cli_args.build_dir)
38682206a0cSBrendan Higgins
387ff7b437fSBrendan Higgins		if not linux:
38887c9c163SBrendan Higgins			linux = kunit_kernel.LinuxSourceTree(cli_args.build_dir,
38987c9c163SBrendan Higgins					kunitconfig_path=cli_args.kunitconfig,
39087c9c163SBrendan Higgins					arch=cli_args.arch,
39187c9c163SBrendan Higgins					cross_compile=cli_args.cross_compile,
39287c9c163SBrendan Higgins					qemu_config_path=cli_args.qemu_config)
393fcdb0bc0SAndy Shevchenko
3946ebf5866SFelix Guo		request = KunitRequest(cli_args.raw_output,
3956ebf5866SFelix Guo				       cli_args.timeout,
3966ebf5866SFelix Guo				       cli_args.jobs,
397ff7b437fSBrendan Higgins				       cli_args.build_dir,
3980476e69fSGreg Thelen				       cli_args.alltests,
399d992880bSDaniel Latypov				       cli_args.filter_glob,
4006cb51a18SDaniel Latypov				       cli_args.kernel_args,
401*ff9e09a3SDaniel Latypov				       cli_args.run_isolated,
40221a6d178SHeidi Fahim				       cli_args.json,
4030476e69fSGreg Thelen				       cli_args.make_options)
4046ebf5866SFelix Guo		result = run_tests(linux, request)
4056ebf5866SFelix Guo		if result.status != KunitStatus.SUCCESS:
4066ebf5866SFelix Guo			sys.exit(1)
40745ba7a89SDavid Gow	elif cli_args.subcommand == 'config':
40882206a0cSBrendan Higgins		if cli_args.build_dir and (
40982206a0cSBrendan Higgins				not os.path.exists(cli_args.build_dir)):
41045ba7a89SDavid Gow			os.mkdir(cli_args.build_dir)
41182206a0cSBrendan Higgins
41245ba7a89SDavid Gow		if not linux:
41387c9c163SBrendan Higgins			linux = kunit_kernel.LinuxSourceTree(cli_args.build_dir,
41487c9c163SBrendan Higgins					kunitconfig_path=cli_args.kunitconfig,
41587c9c163SBrendan Higgins					arch=cli_args.arch,
41687c9c163SBrendan Higgins					cross_compile=cli_args.cross_compile,
41787c9c163SBrendan Higgins					qemu_config_path=cli_args.qemu_config)
418fcdb0bc0SAndy Shevchenko
41945ba7a89SDavid Gow		request = KunitConfigRequest(cli_args.build_dir,
42045ba7a89SDavid Gow					     cli_args.make_options)
42145ba7a89SDavid Gow		result = config_tests(linux, request)
42245ba7a89SDavid Gow		kunit_parser.print_with_timestamp((
42345ba7a89SDavid Gow			'Elapsed time: %.3fs\n') % (
42445ba7a89SDavid Gow				result.elapsed_time))
42545ba7a89SDavid Gow		if result.status != KunitStatus.SUCCESS:
42645ba7a89SDavid Gow			sys.exit(1)
42745ba7a89SDavid Gow	elif cli_args.subcommand == 'build':
42845ba7a89SDavid Gow		if not linux:
42987c9c163SBrendan Higgins			linux = kunit_kernel.LinuxSourceTree(cli_args.build_dir,
43087c9c163SBrendan Higgins					kunitconfig_path=cli_args.kunitconfig,
43187c9c163SBrendan Higgins					arch=cli_args.arch,
43287c9c163SBrendan Higgins					cross_compile=cli_args.cross_compile,
43387c9c163SBrendan Higgins					qemu_config_path=cli_args.qemu_config)
434fcdb0bc0SAndy Shevchenko
43545ba7a89SDavid Gow		request = KunitBuildRequest(cli_args.jobs,
43645ba7a89SDavid Gow					    cli_args.build_dir,
43745ba7a89SDavid Gow					    cli_args.alltests,
43845ba7a89SDavid Gow					    cli_args.make_options)
43945ba7a89SDavid Gow		result = build_tests(linux, request)
44045ba7a89SDavid Gow		kunit_parser.print_with_timestamp((
44145ba7a89SDavid Gow			'Elapsed time: %.3fs\n') % (
44245ba7a89SDavid Gow				result.elapsed_time))
44345ba7a89SDavid Gow		if result.status != KunitStatus.SUCCESS:
44445ba7a89SDavid Gow			sys.exit(1)
44545ba7a89SDavid Gow	elif cli_args.subcommand == 'exec':
44645ba7a89SDavid Gow		if not linux:
44787c9c163SBrendan Higgins			linux = kunit_kernel.LinuxSourceTree(cli_args.build_dir,
44887c9c163SBrendan Higgins					kunitconfig_path=cli_args.kunitconfig,
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		exec_request = KunitExecRequest(cli_args.timeout,
45445ba7a89SDavid Gow						cli_args.build_dir,
455d992880bSDaniel Latypov						cli_args.alltests,
4566cb51a18SDaniel Latypov						cli_args.filter_glob,
457*ff9e09a3SDaniel Latypov						cli_args.kernel_args,
458*ff9e09a3SDaniel Latypov						cli_args.run_isolated)
45945ba7a89SDavid Gow		parse_request = KunitParseRequest(cli_args.raw_output,
46021a6d178SHeidi Fahim						  cli_args.build_dir,
46121a6d178SHeidi Fahim						  cli_args.json)
4627ef925eaSDaniel Latypov		result = exec_tests(linux, exec_request, parse_request)
46345ba7a89SDavid Gow		kunit_parser.print_with_timestamp((
4647ef925eaSDaniel Latypov			'Elapsed time: %.3fs\n') % (result.elapsed_time))
46545ba7a89SDavid Gow		if result.status != KunitStatus.SUCCESS:
46645ba7a89SDavid Gow			sys.exit(1)
46745ba7a89SDavid Gow	elif cli_args.subcommand == 'parse':
46845ba7a89SDavid Gow		if cli_args.file == None:
46945ba7a89SDavid Gow			kunit_output = sys.stdin
47045ba7a89SDavid Gow		else:
47145ba7a89SDavid Gow			with open(cli_args.file, 'r') as f:
47245ba7a89SDavid Gow				kunit_output = f.read().splitlines()
47345ba7a89SDavid Gow		request = KunitParseRequest(cli_args.raw_output,
4743959d0a6SDavid Gow					    None,
47521a6d178SHeidi Fahim					    cli_args.json)
4767ef925eaSDaniel Latypov		result = parse_tests(request, kunit_output)
47745ba7a89SDavid Gow		if result.status != KunitStatus.SUCCESS:
47845ba7a89SDavid Gow			sys.exit(1)
4796ebf5866SFelix Guo	else:
4806ebf5866SFelix Guo		parser.print_help()
4816ebf5866SFelix Guo
4826ebf5866SFelix Guoif __name__ == '__main__':
483ff7b437fSBrendan Higgins	main(sys.argv[1:])
484