blob: d4f7846d0745b00394fd47197004460e412633f9 [file] [log] [blame]
Felix Guo6ebf5862019-09-23 02:02:43 -07001#!/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
14
15from collections import namedtuple
16from enum import Enum, auto
17
18import kunit_config
Heidi Fahim21a6d172020-08-11 14:27:56 -070019import kunit_json
Felix Guo6ebf5862019-09-23 02:02:43 -070020import kunit_kernel
21import kunit_parser
22
David Gow45ba7a82020-04-30 21:27:01 -070023KunitResult = namedtuple('KunitResult', ['status','result','elapsed_time'])
Felix Guo6ebf5862019-09-23 02:02:43 -070024
David Gow45ba7a82020-04-30 21:27:01 -070025KunitConfigRequest = namedtuple('KunitConfigRequest',
Vitor Massaru Iha01397e82020-05-29 16:28:45 -030026 ['build_dir', 'make_options'])
David Gow45ba7a82020-04-30 21:27:01 -070027KunitBuildRequest = namedtuple('KunitBuildRequest',
28 ['jobs', 'build_dir', 'alltests',
29 'make_options'])
30KunitExecRequest = namedtuple('KunitExecRequest',
31 ['timeout', 'build_dir', 'alltests'])
32KunitParseRequest = namedtuple('KunitParseRequest',
Heidi Fahim21a6d172020-08-11 14:27:56 -070033 ['raw_output', 'input_data', 'build_dir', 'json'])
Heidi Fahim021ed9f2020-03-16 13:21:25 -070034KunitRequest = namedtuple('KunitRequest', ['raw_output','timeout', 'jobs',
Heidi Fahim21a6d172020-08-11 14:27:56 -070035 'build_dir', 'alltests', 'json',
Vitor Massaru Iha9bdf64b2020-04-14 20:37:53 -030036 'make_options'])
Felix Guo6ebf5862019-09-23 02:02:43 -070037
Heidi Fahimbe886ba2020-02-18 14:19:16 -080038KernelDirectoryPath = sys.argv[0].split('tools/testing/kunit/')[0]
39
Felix Guo6ebf5862019-09-23 02:02:43 -070040class KunitStatus(Enum):
41 SUCCESS = auto()
42 CONFIG_FAILURE = auto()
43 BUILD_FAILURE = auto()
44 TEST_FAILURE = auto()
45
Heidi Fahimbe886ba2020-02-18 14:19:16 -080046def get_kernel_root_path():
47 parts = sys.argv[0] if not __file__ else __file__
48 parts = os.path.realpath(parts).split('tools/testing/kunit')
49 if len(parts) != 2:
50 sys.exit(1)
51 return parts[0]
52
David Gow45ba7a82020-04-30 21:27:01 -070053def config_tests(linux: kunit_kernel.LinuxSourceTree,
54 request: KunitConfigRequest) -> KunitResult:
55 kunit_parser.print_with_timestamp('Configuring KUnit Kernel ...')
56
Felix Guo6ebf5862019-09-23 02:02:43 -070057 config_start = time.time()
Greg Thelen0476e692020-03-23 12:04:59 -070058 success = linux.build_reconfig(request.build_dir, request.make_options)
Felix Guo6ebf5862019-09-23 02:02:43 -070059 config_end = time.time()
60 if not success:
David Gow45ba7a82020-04-30 21:27:01 -070061 return KunitResult(KunitStatus.CONFIG_FAILURE,
62 'could not configure kernel',
63 config_end - config_start)
64 return KunitResult(KunitStatus.SUCCESS,
65 'configured kernel successfully',
66 config_end - config_start)
Felix Guo6ebf5862019-09-23 02:02:43 -070067
David Gow45ba7a82020-04-30 21:27:01 -070068def build_tests(linux: kunit_kernel.LinuxSourceTree,
69 request: KunitBuildRequest) -> KunitResult:
Felix Guo6ebf5862019-09-23 02:02:43 -070070 kunit_parser.print_with_timestamp('Building KUnit Kernel ...')
71
72 build_start = time.time()
Heidi Fahim021ed9f2020-03-16 13:21:25 -070073 success = linux.build_um_kernel(request.alltests,
74 request.jobs,
Greg Thelen0476e692020-03-23 12:04:59 -070075 request.build_dir,
76 request.make_options)
Felix Guo6ebf5862019-09-23 02:02:43 -070077 build_end = time.time()
78 if not success:
David Gowee614922020-06-15 23:47:30 -070079 return KunitResult(KunitStatus.BUILD_FAILURE,
80 'could not build kernel',
81 build_end - build_start)
David Gow45ba7a82020-04-30 21:27:01 -070082 if not success:
83 return KunitResult(KunitStatus.BUILD_FAILURE,
84 'could not build kernel',
85 build_end - build_start)
86 return KunitResult(KunitStatus.SUCCESS,
87 'built kernel successfully',
88 build_end - build_start)
Felix Guo6ebf5862019-09-23 02:02:43 -070089
David Gow45ba7a82020-04-30 21:27:01 -070090def exec_tests(linux: kunit_kernel.LinuxSourceTree,
91 request: KunitExecRequest) -> KunitResult:
Felix Guo6ebf5862019-09-23 02:02:43 -070092 kunit_parser.print_with_timestamp('Starting KUnit Kernel ...')
93 test_start = time.time()
David Gow45ba7a82020-04-30 21:27:01 -070094 result = linux.run_kernel(
Heidi Fahim021ed9f2020-03-16 13:21:25 -070095 timeout=None if request.alltests else request.timeout,
96 build_dir=request.build_dir)
David Gow45ba7a82020-04-30 21:27:01 -070097
Felix Guo6ebf5862019-09-23 02:02:43 -070098 test_end = time.time()
99
David Gow45ba7a82020-04-30 21:27:01 -0700100 return KunitResult(KunitStatus.SUCCESS,
101 result,
102 test_end - test_start)
103
104def parse_tests(request: KunitParseRequest) -> KunitResult:
105 parse_start = time.time()
106
107 test_result = kunit_parser.TestResult(kunit_parser.TestStatus.SUCCESS,
108 [],
109 'Tests not Parsed.')
Heidi Fahim21a6d172020-08-11 14:27:56 -0700110
David Gow45ba7a82020-04-30 21:27:01 -0700111 if request.raw_output:
112 kunit_parser.raw_output(request.input_data)
113 else:
114 test_result = kunit_parser.parse_run_tests(request.input_data)
115 parse_end = time.time()
116
Heidi Fahim21a6d172020-08-11 14:27:56 -0700117 if request.json:
118 json_obj = kunit_json.get_json_result(
119 test_result=test_result,
120 def_config='kunit_defconfig',
121 build_dir=request.build_dir,
122 json_path=request.json)
123 if request.json == 'stdout':
124 print(json_obj)
125
David Gow45ba7a82020-04-30 21:27:01 -0700126 if test_result.status != kunit_parser.TestStatus.SUCCESS:
127 return KunitResult(KunitStatus.TEST_FAILURE, test_result,
128 parse_end - parse_start)
129
130 return KunitResult(KunitStatus.SUCCESS, test_result,
131 parse_end - parse_start)
132
133
134def run_tests(linux: kunit_kernel.LinuxSourceTree,
135 request: KunitRequest) -> KunitResult:
136 run_start = time.time()
137
138 config_request = KunitConfigRequest(request.build_dir,
David Gow45ba7a82020-04-30 21:27:01 -0700139 request.make_options)
140 config_result = config_tests(linux, config_request)
141 if config_result.status != KunitStatus.SUCCESS:
142 return config_result
143
144 build_request = KunitBuildRequest(request.jobs, request.build_dir,
145 request.alltests,
146 request.make_options)
147 build_result = build_tests(linux, build_request)
148 if build_result.status != KunitStatus.SUCCESS:
149 return build_result
150
151 exec_request = KunitExecRequest(request.timeout, request.build_dir,
152 request.alltests)
153 exec_result = exec_tests(linux, exec_request)
154 if exec_result.status != KunitStatus.SUCCESS:
155 return exec_result
156
157 parse_request = KunitParseRequest(request.raw_output,
Heidi Fahim21a6d172020-08-11 14:27:56 -0700158 exec_result.result,
159 request.build_dir,
160 request.json)
David Gow45ba7a82020-04-30 21:27:01 -0700161 parse_result = parse_tests(parse_request)
162
163 run_end = time.time()
164
Felix Guo6ebf5862019-09-23 02:02:43 -0700165 kunit_parser.print_with_timestamp((
166 'Elapsed time: %.3fs total, %.3fs configuring, %.3fs ' +
167 'building, %.3fs running\n') % (
David Gow45ba7a82020-04-30 21:27:01 -0700168 run_end - run_start,
169 config_result.elapsed_time,
170 build_result.elapsed_time,
171 exec_result.elapsed_time))
172 return parse_result
Felix Guo6ebf5862019-09-23 02:02:43 -0700173
David Gow45ba7a82020-04-30 21:27:01 -0700174def add_common_opts(parser):
175 parser.add_argument('--build_dir',
176 help='As in the make command, it specifies the build '
177 'directory.',
Vitor Massaru Ihaddbd60c2020-04-14 20:09:50 -0300178 type=str, default='.kunit', metavar='build_dir')
David Gow45ba7a82020-04-30 21:27:01 -0700179 parser.add_argument('--make_options',
180 help='X=Y make option, can be repeated.',
181 action='append')
182 parser.add_argument('--alltests',
183 help='Run all KUnit tests through allyesconfig',
184 action='store_true')
185
David Gow45ba7a82020-04-30 21:27:01 -0700186def add_build_opts(parser):
187 parser.add_argument('--jobs',
188 help='As in the make command, "Specifies the number of '
189 'jobs (commands) to run simultaneously."',
190 type=int, default=8, metavar='jobs')
191
192def add_exec_opts(parser):
193 parser.add_argument('--timeout',
194 help='maximum number of seconds to allow for all tests '
195 'to run. This does not include time taken to build the '
196 'tests.',
197 type=int,
198 default=300,
199 metavar='timeout')
200
201def add_parse_opts(parser):
202 parser.add_argument('--raw_output', help='don\'t format output from kernel',
203 action='store_true')
Heidi Fahim21a6d172020-08-11 14:27:56 -0700204 parser.add_argument('--json',
205 nargs='?',
206 help='Stores test results in a JSON, and either '
207 'prints to stdout or saves to file if a '
208 'filename is specified',
209 type=str, const='stdout', default=None)
Felix Guo6ebf5862019-09-23 02:02:43 -0700210
Brendan Higginsff7b4372019-09-23 02:02:44 -0700211def main(argv, linux=None):
Felix Guo6ebf5862019-09-23 02:02:43 -0700212 parser = argparse.ArgumentParser(
213 description='Helps writing and running KUnit tests.')
214 subparser = parser.add_subparsers(dest='subcommand')
215
David Gow45ba7a82020-04-30 21:27:01 -0700216 # The 'run' command will config, build, exec, and parse in one go.
Felix Guo6ebf5862019-09-23 02:02:43 -0700217 run_parser = subparser.add_parser('run', help='Runs KUnit tests.')
David Gow45ba7a82020-04-30 21:27:01 -0700218 add_common_opts(run_parser)
David Gow45ba7a82020-04-30 21:27:01 -0700219 add_build_opts(run_parser)
220 add_exec_opts(run_parser)
221 add_parse_opts(run_parser)
Felix Guo6ebf5862019-09-23 02:02:43 -0700222
David Gow45ba7a82020-04-30 21:27:01 -0700223 config_parser = subparser.add_parser('config',
224 help='Ensures that .config contains all of '
225 'the options in .kunitconfig')
226 add_common_opts(config_parser)
Felix Guo6ebf5862019-09-23 02:02:43 -0700227
David Gow45ba7a82020-04-30 21:27:01 -0700228 build_parser = subparser.add_parser('build', help='Builds a kernel with KUnit tests')
229 add_common_opts(build_parser)
230 add_build_opts(build_parser)
Felix Guo6ebf5862019-09-23 02:02:43 -0700231
David Gow45ba7a82020-04-30 21:27:01 -0700232 exec_parser = subparser.add_parser('exec', help='Run a kernel with KUnit tests')
233 add_common_opts(exec_parser)
234 add_exec_opts(exec_parser)
235 add_parse_opts(exec_parser)
Felix Guo6ebf5862019-09-23 02:02:43 -0700236
David Gow45ba7a82020-04-30 21:27:01 -0700237 # The 'parse' option is special, as it doesn't need the kernel source
238 # (therefore there is no need for a build_dir, hence no add_common_opts)
239 # and the '--file' argument is not relevant to 'run', so isn't in
240 # add_parse_opts()
241 parse_parser = subparser.add_parser('parse',
242 help='Parses KUnit results from a file, '
243 'and parses formatted results.')
244 add_parse_opts(parse_parser)
245 parse_parser.add_argument('file',
246 help='Specifies the file to read results from.',
247 type=str, nargs='?', metavar='input_file')
Greg Thelen0476e692020-03-23 12:04:59 -0700248
Felix Guo6ebf5862019-09-23 02:02:43 -0700249 cli_args = parser.parse_args(argv)
250
Brendan Higgins5578d002020-08-11 14:27:55 -0700251 if get_kernel_root_path():
252 os.chdir(get_kernel_root_path())
253
Felix Guo6ebf5862019-09-23 02:02:43 -0700254 if cli_args.subcommand == 'run':
Vitor Massaru Iha01397e82020-05-29 16:28:45 -0300255 if not os.path.exists(cli_args.build_dir):
256 os.mkdir(cli_args.build_dir)
Brendan Higgins82206a02020-09-28 13:02:27 -0700257
Brendan Higginsff7b4372019-09-23 02:02:44 -0700258 if not linux:
259 linux = kunit_kernel.LinuxSourceTree()
260
Andy Shevchenkofcdb0bc2020-10-26 18:59:25 +0200261 linux.create_kunitconfig(cli_args.build_dir)
262 linux.read_kunitconfig(cli_args.build_dir)
263
Felix Guo6ebf5862019-09-23 02:02:43 -0700264 request = KunitRequest(cli_args.raw_output,
265 cli_args.timeout,
266 cli_args.jobs,
Brendan Higginsff7b4372019-09-23 02:02:44 -0700267 cli_args.build_dir,
Greg Thelen0476e692020-03-23 12:04:59 -0700268 cli_args.alltests,
Heidi Fahim21a6d172020-08-11 14:27:56 -0700269 cli_args.json,
Greg Thelen0476e692020-03-23 12:04:59 -0700270 cli_args.make_options)
Felix Guo6ebf5862019-09-23 02:02:43 -0700271 result = run_tests(linux, request)
272 if result.status != KunitStatus.SUCCESS:
273 sys.exit(1)
David Gow45ba7a82020-04-30 21:27:01 -0700274 elif cli_args.subcommand == 'config':
Brendan Higgins82206a02020-09-28 13:02:27 -0700275 if cli_args.build_dir and (
276 not os.path.exists(cli_args.build_dir)):
277 os.mkdir(cli_args.build_dir)
278
David Gow45ba7a82020-04-30 21:27:01 -0700279 if not linux:
280 linux = kunit_kernel.LinuxSourceTree()
281
Andy Shevchenkofcdb0bc2020-10-26 18:59:25 +0200282 linux.create_kunitconfig(cli_args.build_dir)
283 linux.read_kunitconfig(cli_args.build_dir)
284
David Gow45ba7a82020-04-30 21:27:01 -0700285 request = KunitConfigRequest(cli_args.build_dir,
David Gow45ba7a82020-04-30 21:27:01 -0700286 cli_args.make_options)
287 result = config_tests(linux, request)
288 kunit_parser.print_with_timestamp((
289 'Elapsed time: %.3fs\n') % (
290 result.elapsed_time))
291 if result.status != KunitStatus.SUCCESS:
292 sys.exit(1)
293 elif cli_args.subcommand == 'build':
David Gow45ba7a82020-04-30 21:27:01 -0700294 if not linux:
295 linux = kunit_kernel.LinuxSourceTree()
296
Andy Shevchenkofcdb0bc2020-10-26 18:59:25 +0200297 linux.create_kunitconfig(cli_args.build_dir)
298 linux.read_kunitconfig(cli_args.build_dir)
299
David Gow45ba7a82020-04-30 21:27:01 -0700300 request = KunitBuildRequest(cli_args.jobs,
301 cli_args.build_dir,
302 cli_args.alltests,
303 cli_args.make_options)
304 result = build_tests(linux, request)
305 kunit_parser.print_with_timestamp((
306 'Elapsed time: %.3fs\n') % (
307 result.elapsed_time))
308 if result.status != KunitStatus.SUCCESS:
309 sys.exit(1)
310 elif cli_args.subcommand == 'exec':
David Gow45ba7a82020-04-30 21:27:01 -0700311 if not linux:
312 linux = kunit_kernel.LinuxSourceTree()
313
Andy Shevchenkofcdb0bc2020-10-26 18:59:25 +0200314 linux.create_kunitconfig(cli_args.build_dir)
315 linux.read_kunitconfig(cli_args.build_dir)
316
David Gow45ba7a82020-04-30 21:27:01 -0700317 exec_request = KunitExecRequest(cli_args.timeout,
318 cli_args.build_dir,
319 cli_args.alltests)
320 exec_result = exec_tests(linux, exec_request)
321 parse_request = KunitParseRequest(cli_args.raw_output,
Heidi Fahim21a6d172020-08-11 14:27:56 -0700322 exec_result.result,
323 cli_args.build_dir,
324 cli_args.json)
David Gow45ba7a82020-04-30 21:27:01 -0700325 result = parse_tests(parse_request)
326 kunit_parser.print_with_timestamp((
327 'Elapsed time: %.3fs\n') % (
328 exec_result.elapsed_time))
329 if result.status != KunitStatus.SUCCESS:
330 sys.exit(1)
331 elif cli_args.subcommand == 'parse':
332 if cli_args.file == None:
333 kunit_output = sys.stdin
334 else:
335 with open(cli_args.file, 'r') as f:
336 kunit_output = f.read().splitlines()
337 request = KunitParseRequest(cli_args.raw_output,
Heidi Fahim21a6d172020-08-11 14:27:56 -0700338 kunit_output,
David Gow3959d0a2020-10-21 00:16:03 -0700339 None,
Heidi Fahim21a6d172020-08-11 14:27:56 -0700340 cli_args.json)
David Gow45ba7a82020-04-30 21:27:01 -0700341 result = parse_tests(request)
342 if result.status != KunitStatus.SUCCESS:
343 sys.exit(1)
Felix Guo6ebf5862019-09-23 02:02:43 -0700344 else:
345 parser.print_help()
346
347if __name__ == '__main__':
Brendan Higginsff7b4372019-09-23 02:02:44 -0700348 main(sys.argv[1:])