xref: /openbmc/linux/tools/testing/kunit/kunit.py (revision f9834f18)
1#!/usr/bin/python3
2# SPDX-License-Identifier: GPL-2.0
3#
4# A thin wrapper on top of the KUnit Kernel
5#
6# Copyright (C) 2019, Google LLC.
7# Author: Felix Guo <felixguoxiuping@gmail.com>
8# Author: Brendan Higgins <brendanhiggins@google.com>
9
10import argparse
11import sys
12import os
13import time
14import shutil
15
16from collections import namedtuple
17from enum import Enum, auto
18
19import kunit_config
20import kunit_kernel
21import kunit_parser
22
23KunitResult = namedtuple('KunitResult', ['status','result'])
24
25KunitRequest = namedtuple('KunitRequest', ['raw_output','timeout', 'jobs', 'build_dir', 'defconfig'])
26
27KernelDirectoryPath = sys.argv[0].split('tools/testing/kunit/')[0]
28
29class KunitStatus(Enum):
30	SUCCESS = auto()
31	CONFIG_FAILURE = auto()
32	BUILD_FAILURE = auto()
33	TEST_FAILURE = auto()
34
35def create_default_kunitconfig():
36	if not os.path.exists(kunit_kernel.kunitconfig_path):
37		shutil.copyfile('arch/um/configs/kunit_defconfig',
38				kunit_kernel.kunitconfig_path)
39
40def get_kernel_root_path():
41	parts = sys.argv[0] if not __file__ else __file__
42	parts = os.path.realpath(parts).split('tools/testing/kunit')
43	if len(parts) != 2:
44		sys.exit(1)
45	return parts[0]
46
47def run_tests(linux: kunit_kernel.LinuxSourceTree,
48	      request: KunitRequest) -> KunitResult:
49	config_start = time.time()
50	success = linux.build_reconfig(request.build_dir)
51	config_end = time.time()
52	if not success:
53		return KunitResult(KunitStatus.CONFIG_FAILURE, 'could not configure kernel')
54
55	kunit_parser.print_with_timestamp('Building KUnit Kernel ...')
56
57	build_start = time.time()
58	success = linux.build_um_kernel(request.jobs, request.build_dir)
59	build_end = time.time()
60	if not success:
61		return KunitResult(KunitStatus.BUILD_FAILURE, 'could not build kernel')
62
63	kunit_parser.print_with_timestamp('Starting KUnit Kernel ...')
64	test_start = time.time()
65
66	test_result = kunit_parser.TestResult(kunit_parser.TestStatus.SUCCESS,
67					      [],
68					      'Tests not Parsed.')
69	if request.raw_output:
70		kunit_parser.raw_output(
71			linux.run_kernel(timeout=request.timeout,
72					 build_dir=request.build_dir))
73	else:
74		kunit_output = linux.run_kernel(timeout=request.timeout,
75						build_dir=request.build_dir)
76		test_result = kunit_parser.parse_run_tests(kunit_output)
77	test_end = time.time()
78
79	kunit_parser.print_with_timestamp((
80		'Elapsed time: %.3fs total, %.3fs configuring, %.3fs ' +
81		'building, %.3fs running\n') % (
82				test_end - config_start,
83				config_end - config_start,
84				build_end - build_start,
85				test_end - test_start))
86
87	if test_result.status != kunit_parser.TestStatus.SUCCESS:
88		return KunitResult(KunitStatus.TEST_FAILURE, test_result)
89	else:
90		return KunitResult(KunitStatus.SUCCESS, test_result)
91
92def main(argv, linux=None):
93	parser = argparse.ArgumentParser(
94			description='Helps writing and running KUnit tests.')
95	subparser = parser.add_subparsers(dest='subcommand')
96
97	run_parser = subparser.add_parser('run', help='Runs KUnit tests.')
98	run_parser.add_argument('--raw_output', help='don\'t format output from kernel',
99				action='store_true')
100
101	run_parser.add_argument('--timeout',
102				help='maximum number of seconds to allow for all tests '
103				'to run. This does not include time taken to build the '
104				'tests.',
105				type=int,
106				default=300,
107				metavar='timeout')
108
109	run_parser.add_argument('--jobs',
110				help='As in the make command, "Specifies  the number of '
111				'jobs (commands) to run simultaneously."',
112				type=int, default=8, metavar='jobs')
113
114	run_parser.add_argument('--build_dir',
115				help='As in the make command, it specifies the build '
116				'directory.',
117				type=str, default='', metavar='build_dir')
118
119	run_parser.add_argument('--defconfig',
120				help='Uses a default .kunitconfig.',
121				action='store_true')
122
123	cli_args = parser.parse_args(argv)
124
125	if cli_args.subcommand == 'run':
126		if get_kernel_root_path():
127			os.chdir(get_kernel_root_path())
128
129		if cli_args.build_dir:
130			if not os.path.exists(cli_args.build_dir):
131				os.mkdir(cli_args.build_dir)
132			kunit_kernel.kunitconfig_path = os.path.join(
133				cli_args.build_dir,
134				kunit_kernel.kunitconfig_path)
135
136		if cli_args.defconfig:
137			create_default_kunitconfig()
138
139		if not linux:
140			linux = kunit_kernel.LinuxSourceTree()
141
142		request = KunitRequest(cli_args.raw_output,
143				       cli_args.timeout,
144				       cli_args.jobs,
145				       cli_args.build_dir,
146				       cli_args.defconfig)
147		result = run_tests(linux, request)
148		if result.status != KunitStatus.SUCCESS:
149			sys.exit(1)
150	else:
151		parser.print_help()
152
153if __name__ == '__main__':
154	main(sys.argv[1:])
155