xref: /openbmc/linux/tools/testing/kunit/kunit.py (revision 3e30a927)
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
27class KunitStatus(Enum):
28	SUCCESS = auto()
29	CONFIG_FAILURE = auto()
30	BUILD_FAILURE = auto()
31	TEST_FAILURE = auto()
32
33def create_default_kunitconfig():
34	if not os.path.exists(kunit_kernel.kunitconfig_path):
35		shutil.copyfile('arch/um/configs/kunit_defconfig',
36				kunit_kernel.kunitconfig_path)
37
38def run_tests(linux: kunit_kernel.LinuxSourceTree,
39	      request: KunitRequest) -> KunitResult:
40	config_start = time.time()
41	success = linux.build_reconfig(request.build_dir)
42	config_end = time.time()
43	if not success:
44		return KunitResult(KunitStatus.CONFIG_FAILURE, 'could not configure kernel')
45
46	kunit_parser.print_with_timestamp('Building KUnit Kernel ...')
47
48	build_start = time.time()
49	success = linux.build_um_kernel(request.jobs, request.build_dir)
50	build_end = time.time()
51	if not success:
52		return KunitResult(KunitStatus.BUILD_FAILURE, 'could not build kernel')
53
54	kunit_parser.print_with_timestamp('Starting KUnit Kernel ...')
55	test_start = time.time()
56
57	test_result = kunit_parser.TestResult(kunit_parser.TestStatus.SUCCESS,
58					      [],
59					      'Tests not Parsed.')
60	if request.raw_output:
61		kunit_parser.raw_output(
62			linux.run_kernel(timeout=request.timeout,
63					 build_dir=request.build_dir))
64	else:
65		kunit_output = linux.run_kernel(timeout=request.timeout,
66						build_dir=request.build_dir)
67		test_result = kunit_parser.parse_run_tests(kunit_output)
68	test_end = time.time()
69
70	kunit_parser.print_with_timestamp((
71		'Elapsed time: %.3fs total, %.3fs configuring, %.3fs ' +
72		'building, %.3fs running\n') % (
73				test_end - config_start,
74				config_end - config_start,
75				build_end - build_start,
76				test_end - test_start))
77
78	if test_result.status != kunit_parser.TestStatus.SUCCESS:
79		return KunitResult(KunitStatus.TEST_FAILURE, test_result)
80	else:
81		return KunitResult(KunitStatus.SUCCESS, test_result)
82
83def main(argv, linux=None):
84	parser = argparse.ArgumentParser(
85			description='Helps writing and running KUnit tests.')
86	subparser = parser.add_subparsers(dest='subcommand')
87
88	run_parser = subparser.add_parser('run', help='Runs KUnit tests.')
89	run_parser.add_argument('--raw_output', help='don\'t format output from kernel',
90				action='store_true')
91
92	run_parser.add_argument('--timeout',
93				help='maximum number of seconds to allow for all tests '
94				'to run. This does not include time taken to build the '
95				'tests.',
96				type=int,
97				default=300,
98				metavar='timeout')
99
100	run_parser.add_argument('--jobs',
101				help='As in the make command, "Specifies  the number of '
102				'jobs (commands) to run simultaneously."',
103				type=int, default=8, metavar='jobs')
104
105	run_parser.add_argument('--build_dir',
106				help='As in the make command, it specifies the build '
107				'directory.',
108				type=str, default='', metavar='build_dir')
109
110	run_parser.add_argument('--defconfig',
111				help='Uses a default .kunitconfig.',
112				action='store_true')
113
114	cli_args = parser.parse_args(argv)
115
116	if cli_args.subcommand == 'run':
117		if cli_args.build_dir:
118			if not os.path.exists(cli_args.build_dir):
119				os.mkdir(cli_args.build_dir)
120			kunit_kernel.kunitconfig_path = os.path.join(
121				cli_args.build_dir,
122				kunit_kernel.kunitconfig_path)
123
124		if cli_args.defconfig:
125			create_default_kunitconfig()
126
127		if not linux:
128			linux = kunit_kernel.LinuxSourceTree()
129
130		request = KunitRequest(cli_args.raw_output,
131				       cli_args.timeout,
132				       cli_args.jobs,
133				       cli_args.build_dir,
134				       cli_args.defconfig)
135		result = run_tests(linux, request)
136		if result.status != KunitStatus.SUCCESS:
137			sys.exit(1)
138	else:
139		parser.print_help()
140
141if __name__ == '__main__':
142	main(sys.argv[1:])
143