xref: /linux/tools/testing/kunit/kunit.py (revision 7fa7ffcf9babaea2f0a81681b4ef460ee4b93278)
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
159*7fa7ffcfSDaniel Latypov	if len(filter_globs) == 1 and test_counts.crashed > 0:
160*7fa7ffcfSDaniel Latypov		bd = request.build_dir
161*7fa7ffcfSDaniel Latypov		print('The kernel seems to have crashed; you can decode the stack traces with:')
162*7fa7ffcfSDaniel Latypov		print('$ scripts/decode_stacktrace.sh {}/vmlinux {} < {} | tee {}/decoded.log | {} parse'.format(
163*7fa7ffcfSDaniel Latypov				bd, bd, kunit_kernel.get_outfile_path(bd), bd, sys.argv[0]))
164*7fa7ffcfSDaniel Latypov
165d65d07cbSRae Moar	kunit_status = _map_to_overall_status(test_counts.get_status())
166d65d07cbSRae Moar	return KunitResult(status=kunit_status, result=result.result, elapsed_time=exec_time)
167d65d07cbSRae Moar
168d65d07cbSRae Moardef _map_to_overall_status(test_status: kunit_parser.TestStatus) -> KunitStatus:
169d65d07cbSRae Moar	if test_status in (kunit_parser.TestStatus.SUCCESS, kunit_parser.TestStatus.SKIPPED):
170d65d07cbSRae Moar		return KunitStatus.SUCCESS
171d65d07cbSRae Moar	else:
172d65d07cbSRae Moar		return KunitStatus.TEST_FAILURE
1737ef925eaSDaniel Latypov
1747ef925eaSDaniel Latypovdef parse_tests(request: KunitParseRequest, input_data: Iterable[str]) -> KunitResult:
17545ba7a89SDavid Gow	parse_start = time.time()
17645ba7a89SDavid Gow
17745ba7a89SDavid Gow	test_result = kunit_parser.TestResult(kunit_parser.TestStatus.SUCCESS,
178d65d07cbSRae Moar					      kunit_parser.Test(),
17945ba7a89SDavid Gow					      'Tests not Parsed.')
18021a6d178SHeidi Fahim
18145ba7a89SDavid Gow	if request.raw_output:
182d65d07cbSRae Moar		# Treat unparsed results as one passing test.
183d65d07cbSRae Moar		test_result.test.status = kunit_parser.TestStatus.SUCCESS
184d65d07cbSRae Moar		test_result.test.counts.passed = 1
185d65d07cbSRae Moar
1867ef925eaSDaniel Latypov		output: Iterable[str] = input_data
1876a499c9cSDaniel Latypov		if request.raw_output == 'all':
1886a499c9cSDaniel Latypov			pass
1896a499c9cSDaniel Latypov		elif request.raw_output == 'kunit':
1906a499c9cSDaniel Latypov			output = kunit_parser.extract_tap_lines(output)
1916a499c9cSDaniel Latypov		else:
1926a499c9cSDaniel Latypov			print(f'Unknown --raw_output option "{request.raw_output}"', file=sys.stderr)
1936a499c9cSDaniel Latypov		for line in output:
1946a499c9cSDaniel Latypov			print(line.rstrip())
1956a499c9cSDaniel Latypov
19645ba7a89SDavid Gow	else:
1977ef925eaSDaniel Latypov		test_result = kunit_parser.parse_run_tests(input_data)
19845ba7a89SDavid Gow	parse_end = time.time()
19945ba7a89SDavid Gow
20021a6d178SHeidi Fahim	if request.json:
20121a6d178SHeidi Fahim		json_obj = kunit_json.get_json_result(
20221a6d178SHeidi Fahim					test_result=test_result,
20321a6d178SHeidi Fahim					def_config='kunit_defconfig',
20421a6d178SHeidi Fahim					build_dir=request.build_dir,
20521a6d178SHeidi Fahim					json_path=request.json)
20621a6d178SHeidi Fahim		if request.json == 'stdout':
20721a6d178SHeidi Fahim			print(json_obj)
20821a6d178SHeidi Fahim
20945ba7a89SDavid Gow	if test_result.status != kunit_parser.TestStatus.SUCCESS:
21045ba7a89SDavid Gow		return KunitResult(KunitStatus.TEST_FAILURE, test_result,
21145ba7a89SDavid Gow				   parse_end - parse_start)
21245ba7a89SDavid Gow
21345ba7a89SDavid Gow	return KunitResult(KunitStatus.SUCCESS, test_result,
21445ba7a89SDavid Gow				parse_end - parse_start)
21545ba7a89SDavid Gow
21645ba7a89SDavid Gowdef run_tests(linux: kunit_kernel.LinuxSourceTree,
21745ba7a89SDavid Gow	      request: KunitRequest) -> KunitResult:
21845ba7a89SDavid Gow	run_start = time.time()
21945ba7a89SDavid Gow
22045ba7a89SDavid Gow	config_request = KunitConfigRequest(request.build_dir,
22145ba7a89SDavid Gow					    request.make_options)
22245ba7a89SDavid Gow	config_result = config_tests(linux, config_request)
22345ba7a89SDavid Gow	if config_result.status != KunitStatus.SUCCESS:
22445ba7a89SDavid Gow		return config_result
22545ba7a89SDavid Gow
22645ba7a89SDavid Gow	build_request = KunitBuildRequest(request.jobs, request.build_dir,
22745ba7a89SDavid Gow					  request.alltests,
22845ba7a89SDavid Gow					  request.make_options)
22945ba7a89SDavid Gow	build_result = build_tests(linux, build_request)
23045ba7a89SDavid Gow	if build_result.status != KunitStatus.SUCCESS:
23145ba7a89SDavid Gow		return build_result
23245ba7a89SDavid Gow
23345ba7a89SDavid Gow	exec_request = KunitExecRequest(request.timeout, request.build_dir,
2346cb51a18SDaniel Latypov				 request.alltests, request.filter_glob,
235ff9e09a3SDaniel Latypov				 request.kernel_args, request.run_isolated)
23645ba7a89SDavid Gow	parse_request = KunitParseRequest(request.raw_output,
23721a6d178SHeidi Fahim					  request.build_dir,
23821a6d178SHeidi Fahim					  request.json)
2397ef925eaSDaniel Latypov
2407ef925eaSDaniel Latypov	exec_result = exec_tests(linux, exec_request, parse_request)
24145ba7a89SDavid Gow
24245ba7a89SDavid Gow	run_end = time.time()
24345ba7a89SDavid Gow
2446ebf5866SFelix Guo	kunit_parser.print_with_timestamp((
2456ebf5866SFelix Guo		'Elapsed time: %.3fs total, %.3fs configuring, %.3fs ' +
2466ebf5866SFelix Guo		'building, %.3fs running\n') % (
24745ba7a89SDavid Gow				run_end - run_start,
24845ba7a89SDavid Gow				config_result.elapsed_time,
24945ba7a89SDavid Gow				build_result.elapsed_time,
25045ba7a89SDavid Gow				exec_result.elapsed_time))
2517ef925eaSDaniel Latypov	return exec_result
2526ebf5866SFelix Guo
253d8c23eadSDaniel Latypov# Problem:
254d8c23eadSDaniel Latypov# $ kunit.py run --json
255d8c23eadSDaniel Latypov# works as one would expect and prints the parsed test results as JSON.
256d8c23eadSDaniel Latypov# $ kunit.py run --json suite_name
257d8c23eadSDaniel Latypov# would *not* pass suite_name as the filter_glob and print as json.
258d8c23eadSDaniel Latypov# argparse will consider it to be another way of writing
259d8c23eadSDaniel Latypov# $ kunit.py run --json=suite_name
260d8c23eadSDaniel Latypov# i.e. it would run all tests, and dump the json to a `suite_name` file.
261d8c23eadSDaniel Latypov# So we hackily automatically rewrite --json => --json=stdout
262d8c23eadSDaniel Latypovpseudo_bool_flag_defaults = {
263d8c23eadSDaniel Latypov		'--json': 'stdout',
264d8c23eadSDaniel Latypov		'--raw_output': 'kunit',
265d8c23eadSDaniel Latypov}
266d8c23eadSDaniel Latypovdef massage_argv(argv: Sequence[str]) -> Sequence[str]:
267d8c23eadSDaniel Latypov	def massage_arg(arg: str) -> str:
268d8c23eadSDaniel Latypov		if arg not in pseudo_bool_flag_defaults:
269d8c23eadSDaniel Latypov			return arg
270d8c23eadSDaniel Latypov		return  f'{arg}={pseudo_bool_flag_defaults[arg]}'
271d8c23eadSDaniel Latypov	return list(map(massage_arg, argv))
272d8c23eadSDaniel Latypov
27309641f7cSDaniel Latypovdef add_common_opts(parser) -> None:
27445ba7a89SDavid Gow	parser.add_argument('--build_dir',
27545ba7a89SDavid Gow			    help='As in the make command, it specifies the build '
27645ba7a89SDavid Gow			    'directory.',
277ddbd60c7SVitor Massaru Iha			    type=str, default='.kunit', metavar='build_dir')
27845ba7a89SDavid Gow	parser.add_argument('--make_options',
27945ba7a89SDavid Gow			    help='X=Y make option, can be repeated.',
28045ba7a89SDavid Gow			    action='append')
28145ba7a89SDavid Gow	parser.add_argument('--alltests',
28245ba7a89SDavid Gow			    help='Run all KUnit tests through allyesconfig',
2836ebf5866SFelix Guo			    action='store_true')
284243180f5SDaniel Latypov	parser.add_argument('--kunitconfig',
2859854781dSDaniel Latypov			     help='Path to Kconfig fragment that enables KUnit tests.'
2869854781dSDaniel Latypov			     ' If given a directory, (e.g. lib/kunit), "/.kunitconfig" '
2879854781dSDaniel Latypov			     'will get  automatically appended.',
288243180f5SDaniel Latypov			     metavar='kunitconfig')
2899f57cc76SDaniel Latypov	parser.add_argument('--kconfig_add',
2909f57cc76SDaniel Latypov			     help='Additional Kconfig options to append to the '
2919f57cc76SDaniel Latypov			     '.kunitconfig, e.g. CONFIG_KASAN=y. Can be repeated.',
2929f57cc76SDaniel Latypov			    action='append')
2936ebf5866SFelix Guo
29487c9c163SBrendan Higgins	parser.add_argument('--arch',
29587c9c163SBrendan Higgins			    help=('Specifies the architecture to run tests under. '
29687c9c163SBrendan Higgins				  'The architecture specified here must match the '
29787c9c163SBrendan Higgins				  'string passed to the ARCH make param, '
29887c9c163SBrendan Higgins				  'e.g. i386, x86_64, arm, um, etc. Non-UML '
29987c9c163SBrendan Higgins				  'architectures run on QEMU.'),
30087c9c163SBrendan Higgins			    type=str, default='um', metavar='arch')
30187c9c163SBrendan Higgins
30287c9c163SBrendan Higgins	parser.add_argument('--cross_compile',
30387c9c163SBrendan Higgins			    help=('Sets make\'s CROSS_COMPILE variable; it should '
30487c9c163SBrendan Higgins				  'be set to a toolchain path prefix (the prefix '
30587c9c163SBrendan Higgins				  'of gcc and other tools in your toolchain, for '
30687c9c163SBrendan Higgins				  'example `sparc64-linux-gnu-` if you have the '
30787c9c163SBrendan Higgins				  'sparc toolchain installed on your system, or '
30887c9c163SBrendan Higgins				  '`$HOME/toolchains/microblaze/gcc-9.2.0-nolibc/microblaze-linux/bin/microblaze-linux-` '
30987c9c163SBrendan Higgins				  'if you have downloaded the microblaze toolchain '
31087c9c163SBrendan Higgins				  'from the 0-day website to a directory in your '
31187c9c163SBrendan Higgins				  'home directory called `toolchains`).'),
31287c9c163SBrendan Higgins			    metavar='cross_compile')
31387c9c163SBrendan Higgins
31487c9c163SBrendan Higgins	parser.add_argument('--qemu_config',
31587c9c163SBrendan Higgins			    help=('Takes a path to a path to a file containing '
31687c9c163SBrendan Higgins				  'a QemuArchParams object.'),
31787c9c163SBrendan Higgins			    type=str, metavar='qemu_config')
31887c9c163SBrendan Higgins
31909641f7cSDaniel Latypovdef add_build_opts(parser) -> None:
32045ba7a89SDavid Gow	parser.add_argument('--jobs',
32145ba7a89SDavid Gow			    help='As in the make command, "Specifies  the number of '
32245ba7a89SDavid Gow			    'jobs (commands) to run simultaneously."',
32345ba7a89SDavid Gow			    type=int, default=8, metavar='jobs')
32445ba7a89SDavid Gow
32509641f7cSDaniel Latypovdef add_exec_opts(parser) -> None:
32645ba7a89SDavid Gow	parser.add_argument('--timeout',
3276ebf5866SFelix Guo			    help='maximum number of seconds to allow for all tests '
3286ebf5866SFelix Guo			    'to run. This does not include time taken to build the '
3296ebf5866SFelix Guo			    'tests.',
3306ebf5866SFelix Guo			    type=int,
3316ebf5866SFelix Guo			    default=300,
3326ebf5866SFelix Guo			    metavar='timeout')
333d992880bSDaniel Latypov	parser.add_argument('filter_glob',
334a127b154SDaniel Latypov			    help='Filter which KUnit test suites/tests run at '
335a127b154SDaniel Latypov			    'boot-time, e.g. list* or list*.*del_test',
336d992880bSDaniel Latypov			    type=str,
337d992880bSDaniel Latypov			    nargs='?',
338d992880bSDaniel Latypov			    default='',
339d992880bSDaniel Latypov			    metavar='filter_glob')
3406cb51a18SDaniel Latypov	parser.add_argument('--kernel_args',
3416cb51a18SDaniel Latypov			    help='Kernel command-line parameters. Maybe be repeated',
3426cb51a18SDaniel Latypov			     action='append')
343ff9e09a3SDaniel Latypov	parser.add_argument('--run_isolated', help='If set, boot the kernel for each '
344ff9e09a3SDaniel Latypov			    'individual suite/test. This is can be useful for debugging '
345ff9e09a3SDaniel Latypov			    'a non-hermetic test, one that might pass/fail based on '
346ff9e09a3SDaniel Latypov			    'what ran before it.',
347ff9e09a3SDaniel Latypov			    type=str,
348ff9e09a3SDaniel Latypov			    choices=['suite', 'test']),
3496ebf5866SFelix Guo
35009641f7cSDaniel Latypovdef add_parse_opts(parser) -> None:
3516a499c9cSDaniel Latypov	parser.add_argument('--raw_output', help='If set don\'t format output from kernel. '
3526a499c9cSDaniel Latypov			    'If set to --raw_output=kunit, filters to just KUnit output.',
3536a499c9cSDaniel Latypov			    type=str, nargs='?', const='all', default=None)
35421a6d178SHeidi Fahim	parser.add_argument('--json',
35521a6d178SHeidi Fahim			    nargs='?',
35621a6d178SHeidi Fahim			    help='Stores test results in a JSON, and either '
35721a6d178SHeidi Fahim			    'prints to stdout or saves to file if a '
35821a6d178SHeidi Fahim			    'filename is specified',
35921a6d178SHeidi Fahim			    type=str, const='stdout', default=None)
360021ed9f5SHeidi Fahim
36145ba7a89SDavid Gowdef main(argv, linux=None):
36245ba7a89SDavid Gow	parser = argparse.ArgumentParser(
36345ba7a89SDavid Gow			description='Helps writing and running KUnit tests.')
36445ba7a89SDavid Gow	subparser = parser.add_subparsers(dest='subcommand')
36545ba7a89SDavid Gow
36645ba7a89SDavid Gow	# The 'run' command will config, build, exec, and parse in one go.
36745ba7a89SDavid Gow	run_parser = subparser.add_parser('run', help='Runs KUnit tests.')
36845ba7a89SDavid Gow	add_common_opts(run_parser)
36945ba7a89SDavid Gow	add_build_opts(run_parser)
37045ba7a89SDavid Gow	add_exec_opts(run_parser)
37145ba7a89SDavid Gow	add_parse_opts(run_parser)
37245ba7a89SDavid Gow
37345ba7a89SDavid Gow	config_parser = subparser.add_parser('config',
37445ba7a89SDavid Gow						help='Ensures that .config contains all of '
37545ba7a89SDavid Gow						'the options in .kunitconfig')
37645ba7a89SDavid Gow	add_common_opts(config_parser)
37745ba7a89SDavid Gow
37845ba7a89SDavid Gow	build_parser = subparser.add_parser('build', help='Builds a kernel with KUnit tests')
37945ba7a89SDavid Gow	add_common_opts(build_parser)
38045ba7a89SDavid Gow	add_build_opts(build_parser)
38145ba7a89SDavid Gow
38245ba7a89SDavid Gow	exec_parser = subparser.add_parser('exec', help='Run a kernel with KUnit tests')
38345ba7a89SDavid Gow	add_common_opts(exec_parser)
38445ba7a89SDavid Gow	add_exec_opts(exec_parser)
38545ba7a89SDavid Gow	add_parse_opts(exec_parser)
38645ba7a89SDavid Gow
38745ba7a89SDavid Gow	# The 'parse' option is special, as it doesn't need the kernel source
38845ba7a89SDavid Gow	# (therefore there is no need for a build_dir, hence no add_common_opts)
38945ba7a89SDavid Gow	# and the '--file' argument is not relevant to 'run', so isn't in
39045ba7a89SDavid Gow	# add_parse_opts()
39145ba7a89SDavid Gow	parse_parser = subparser.add_parser('parse',
39245ba7a89SDavid Gow					    help='Parses KUnit results from a file, '
39345ba7a89SDavid Gow					    'and parses formatted results.')
39445ba7a89SDavid Gow	add_parse_opts(parse_parser)
39545ba7a89SDavid Gow	parse_parser.add_argument('file',
39645ba7a89SDavid Gow				  help='Specifies the file to read results from.',
39745ba7a89SDavid Gow				  type=str, nargs='?', metavar='input_file')
3980476e69fSGreg Thelen
399d8c23eadSDaniel Latypov	cli_args = parser.parse_args(massage_argv(argv))
4006ebf5866SFelix Guo
4015578d008SBrendan Higgins	if get_kernel_root_path():
4025578d008SBrendan Higgins		os.chdir(get_kernel_root_path())
4035578d008SBrendan Higgins
4046ebf5866SFelix Guo	if cli_args.subcommand == 'run':
405e3212513SSeongJae Park		if not os.path.exists(cli_args.build_dir):
406e3212513SSeongJae Park			os.mkdir(cli_args.build_dir)
40782206a0cSBrendan Higgins
408ff7b437fSBrendan Higgins		if not linux:
40987c9c163SBrendan Higgins			linux = kunit_kernel.LinuxSourceTree(cli_args.build_dir,
41087c9c163SBrendan Higgins					kunitconfig_path=cli_args.kunitconfig,
4119f57cc76SDaniel Latypov					kconfig_add=cli_args.kconfig_add,
41287c9c163SBrendan Higgins					arch=cli_args.arch,
41387c9c163SBrendan Higgins					cross_compile=cli_args.cross_compile,
41487c9c163SBrendan Higgins					qemu_config_path=cli_args.qemu_config)
415fcdb0bc0SAndy Shevchenko
4166ebf5866SFelix Guo		request = KunitRequest(cli_args.raw_output,
4176ebf5866SFelix Guo				       cli_args.timeout,
4186ebf5866SFelix Guo				       cli_args.jobs,
419ff7b437fSBrendan Higgins				       cli_args.build_dir,
4200476e69fSGreg Thelen				       cli_args.alltests,
421d992880bSDaniel Latypov				       cli_args.filter_glob,
4226cb51a18SDaniel Latypov				       cli_args.kernel_args,
423ff9e09a3SDaniel Latypov				       cli_args.run_isolated,
42421a6d178SHeidi Fahim				       cli_args.json,
4250476e69fSGreg Thelen				       cli_args.make_options)
4266ebf5866SFelix Guo		result = run_tests(linux, request)
4276ebf5866SFelix Guo		if result.status != KunitStatus.SUCCESS:
4286ebf5866SFelix Guo			sys.exit(1)
42945ba7a89SDavid Gow	elif cli_args.subcommand == 'config':
43082206a0cSBrendan Higgins		if cli_args.build_dir and (
43182206a0cSBrendan Higgins				not os.path.exists(cli_args.build_dir)):
43245ba7a89SDavid Gow			os.mkdir(cli_args.build_dir)
43382206a0cSBrendan Higgins
43445ba7a89SDavid Gow		if not linux:
43587c9c163SBrendan Higgins			linux = kunit_kernel.LinuxSourceTree(cli_args.build_dir,
43687c9c163SBrendan Higgins					kunitconfig_path=cli_args.kunitconfig,
4379f57cc76SDaniel Latypov					kconfig_add=cli_args.kconfig_add,
43887c9c163SBrendan Higgins					arch=cli_args.arch,
43987c9c163SBrendan Higgins					cross_compile=cli_args.cross_compile,
44087c9c163SBrendan Higgins					qemu_config_path=cli_args.qemu_config)
441fcdb0bc0SAndy Shevchenko
44245ba7a89SDavid Gow		request = KunitConfigRequest(cli_args.build_dir,
44345ba7a89SDavid Gow					     cli_args.make_options)
44445ba7a89SDavid Gow		result = config_tests(linux, request)
44545ba7a89SDavid Gow		kunit_parser.print_with_timestamp((
44645ba7a89SDavid Gow			'Elapsed time: %.3fs\n') % (
44745ba7a89SDavid Gow				result.elapsed_time))
44845ba7a89SDavid Gow		if result.status != KunitStatus.SUCCESS:
44945ba7a89SDavid Gow			sys.exit(1)
45045ba7a89SDavid Gow	elif cli_args.subcommand == 'build':
45145ba7a89SDavid Gow		if not linux:
45287c9c163SBrendan Higgins			linux = kunit_kernel.LinuxSourceTree(cli_args.build_dir,
45387c9c163SBrendan Higgins					kunitconfig_path=cli_args.kunitconfig,
4549f57cc76SDaniel Latypov					kconfig_add=cli_args.kconfig_add,
45587c9c163SBrendan Higgins					arch=cli_args.arch,
45687c9c163SBrendan Higgins					cross_compile=cli_args.cross_compile,
45787c9c163SBrendan Higgins					qemu_config_path=cli_args.qemu_config)
458fcdb0bc0SAndy Shevchenko
45945ba7a89SDavid Gow		request = KunitBuildRequest(cli_args.jobs,
46045ba7a89SDavid Gow					    cli_args.build_dir,
46145ba7a89SDavid Gow					    cli_args.alltests,
46245ba7a89SDavid Gow					    cli_args.make_options)
46345ba7a89SDavid Gow		result = build_tests(linux, request)
46445ba7a89SDavid Gow		kunit_parser.print_with_timestamp((
46545ba7a89SDavid Gow			'Elapsed time: %.3fs\n') % (
46645ba7a89SDavid Gow				result.elapsed_time))
46745ba7a89SDavid Gow		if result.status != KunitStatus.SUCCESS:
46845ba7a89SDavid Gow			sys.exit(1)
46945ba7a89SDavid Gow	elif cli_args.subcommand == 'exec':
47045ba7a89SDavid Gow		if not linux:
47187c9c163SBrendan Higgins			linux = kunit_kernel.LinuxSourceTree(cli_args.build_dir,
47287c9c163SBrendan Higgins					kunitconfig_path=cli_args.kunitconfig,
4739f57cc76SDaniel Latypov					kconfig_add=cli_args.kconfig_add,
47487c9c163SBrendan Higgins					arch=cli_args.arch,
47587c9c163SBrendan Higgins					cross_compile=cli_args.cross_compile,
47687c9c163SBrendan Higgins					qemu_config_path=cli_args.qemu_config)
477fcdb0bc0SAndy Shevchenko
47845ba7a89SDavid Gow		exec_request = KunitExecRequest(cli_args.timeout,
47945ba7a89SDavid Gow						cli_args.build_dir,
480d992880bSDaniel Latypov						cli_args.alltests,
4816cb51a18SDaniel Latypov						cli_args.filter_glob,
482ff9e09a3SDaniel Latypov						cli_args.kernel_args,
483ff9e09a3SDaniel Latypov						cli_args.run_isolated)
48445ba7a89SDavid Gow		parse_request = KunitParseRequest(cli_args.raw_output,
48521a6d178SHeidi Fahim						  cli_args.build_dir,
48621a6d178SHeidi Fahim						  cli_args.json)
4877ef925eaSDaniel Latypov		result = exec_tests(linux, exec_request, parse_request)
48845ba7a89SDavid Gow		kunit_parser.print_with_timestamp((
4897ef925eaSDaniel Latypov			'Elapsed time: %.3fs\n') % (result.elapsed_time))
49045ba7a89SDavid Gow		if result.status != KunitStatus.SUCCESS:
49145ba7a89SDavid Gow			sys.exit(1)
49245ba7a89SDavid Gow	elif cli_args.subcommand == 'parse':
49345ba7a89SDavid Gow		if cli_args.file == None:
4942ab5d5e6SDaniel Latypov			sys.stdin.reconfigure(errors='backslashreplace')  # pytype: disable=attribute-error
49545ba7a89SDavid Gow			kunit_output = sys.stdin
49645ba7a89SDavid Gow		else:
4972ab5d5e6SDaniel Latypov			with open(cli_args.file, 'r', errors='backslashreplace') as f:
49845ba7a89SDavid Gow				kunit_output = f.read().splitlines()
49945ba7a89SDavid Gow		request = KunitParseRequest(cli_args.raw_output,
5003959d0a6SDavid Gow					    None,
50121a6d178SHeidi Fahim					    cli_args.json)
5027ef925eaSDaniel Latypov		result = parse_tests(request, kunit_output)
50345ba7a89SDavid Gow		if result.status != KunitStatus.SUCCESS:
50445ba7a89SDavid Gow			sys.exit(1)
5056ebf5866SFelix Guo	else:
5066ebf5866SFelix Guo		parser.print_help()
5076ebf5866SFelix Guo
5086ebf5866SFelix Guoif __name__ == '__main__':
509ff7b437fSBrendan Higgins	main(sys.argv[1:])
510