kunit.py (89aa72cd3052b70ade40fa02a018f8f509355790) kunit.py (ee96d25f2fa657a29ab59345898dc4ff616cfe84)
1#!/usr/bin/env 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>

--- 33 unchanged lines hidden (view full) ---

42@dataclass
43class KunitBuildRequest(KunitConfigRequest):
44 jobs: int
45 alltests: bool
46
47@dataclass
48class KunitParseRequest:
49 raw_output: Optional[str]
1#!/usr/bin/env 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>

--- 33 unchanged lines hidden (view full) ---

42@dataclass
43class KunitBuildRequest(KunitConfigRequest):
44 jobs: int
45 alltests: bool
46
47@dataclass
48class KunitParseRequest:
49 raw_output: Optional[str]
50 build_dir: str
51 json: Optional[str]
52
53@dataclass
54class KunitExecRequest(KunitParseRequest):
50 json: Optional[str]
51
52@dataclass
53class KunitExecRequest(KunitParseRequest):
54 build_dir: str
55 timeout: int
56 alltests: bool
57 filter_glob: str
58 kernel_args: Optional[List[str]]
59 run_isolated: Optional[str]
60
61@dataclass
62class KunitRequest(KunitExecRequest, KunitBuildRequest):

--- 85 unchanged lines hidden (view full) ---

148 filter_globs = tests
149 if request.run_isolated == 'suite':
150 filter_globs = _suites_from_test_list(tests)
151 # Apply the test-part of the user's glob, if present.
152 if '.' in request.filter_glob:
153 test_glob = request.filter_glob.split('.', maxsplit=2)[1]
154 filter_globs = [g + '.'+ test_glob for g in filter_globs]
155
55 timeout: int
56 alltests: bool
57 filter_glob: str
58 kernel_args: Optional[List[str]]
59 run_isolated: Optional[str]
60
61@dataclass
62class KunitRequest(KunitExecRequest, KunitBuildRequest):

--- 85 unchanged lines hidden (view full) ---

148 filter_globs = tests
149 if request.run_isolated == 'suite':
150 filter_globs = _suites_from_test_list(tests)
151 # Apply the test-part of the user's glob, if present.
152 if '.' in request.filter_glob:
153 test_glob = request.filter_glob.split('.', maxsplit=2)[1]
154 filter_globs = [g + '.'+ test_glob for g in filter_globs]
155
156 metadata = kunit_json.Metadata(build_dir=request.build_dir)
157
156 test_counts = kunit_parser.TestCounts()
157 exec_time = 0.0
158 for i, filter_glob in enumerate(filter_globs):
159 kunit_parser.print_with_timestamp('Starting KUnit Kernel ({}/{})...'.format(i+1, len(filter_globs)))
160
161 test_start = time.time()
162 run_result = linux.run_kernel(
163 args=request.kernel_args,
164 timeout=None if request.alltests else request.timeout,
165 filter_glob=filter_glob,
166 build_dir=request.build_dir)
167
158 test_counts = kunit_parser.TestCounts()
159 exec_time = 0.0
160 for i, filter_glob in enumerate(filter_globs):
161 kunit_parser.print_with_timestamp('Starting KUnit Kernel ({}/{})...'.format(i+1, len(filter_globs)))
162
163 test_start = time.time()
164 run_result = linux.run_kernel(
165 args=request.kernel_args,
166 timeout=None if request.alltests else request.timeout,
167 filter_glob=filter_glob,
168 build_dir=request.build_dir)
169
168 _, test_result = parse_tests(request, run_result)
170 _, test_result = parse_tests(request, metadata, run_result)
169 # run_kernel() doesn't block on the kernel exiting.
170 # That only happens after we get the last line of output from `run_result`.
171 # So exec_time here actually contains parsing + execution time, which is fine.
172 test_end = time.time()
173 exec_time += test_end - test_start
174
175 test_counts.add_subtest_counts(test_result.counts)
176

--- 7 unchanged lines hidden (view full) ---

184 return KunitResult(status=kunit_status, elapsed_time=exec_time)
185
186def _map_to_overall_status(test_status: kunit_parser.TestStatus) -> KunitStatus:
187 if test_status in (kunit_parser.TestStatus.SUCCESS, kunit_parser.TestStatus.SKIPPED):
188 return KunitStatus.SUCCESS
189 else:
190 return KunitStatus.TEST_FAILURE
191
171 # run_kernel() doesn't block on the kernel exiting.
172 # That only happens after we get the last line of output from `run_result`.
173 # So exec_time here actually contains parsing + execution time, which is fine.
174 test_end = time.time()
175 exec_time += test_end - test_start
176
177 test_counts.add_subtest_counts(test_result.counts)
178

--- 7 unchanged lines hidden (view full) ---

186 return KunitResult(status=kunit_status, elapsed_time=exec_time)
187
188def _map_to_overall_status(test_status: kunit_parser.TestStatus) -> KunitStatus:
189 if test_status in (kunit_parser.TestStatus.SUCCESS, kunit_parser.TestStatus.SKIPPED):
190 return KunitStatus.SUCCESS
191 else:
192 return KunitStatus.TEST_FAILURE
193
192def parse_tests(request: KunitParseRequest, input_data: Iterable[str]) -> Tuple[KunitResult, kunit_parser.Test]:
194def parse_tests(request: KunitParseRequest, metadata: kunit_json.Metadata, input_data: Iterable[str]) -> Tuple[KunitResult, kunit_parser.Test]:
193 parse_start = time.time()
194
195 test_result = kunit_parser.Test()
196
197 if request.raw_output:
198 # Treat unparsed results as one passing test.
199 test_result.status = kunit_parser.TestStatus.SUCCESS
200 test_result.counts.passed = 1

--- 10 unchanged lines hidden (view full) ---

211
212 else:
213 test_result = kunit_parser.parse_run_tests(input_data)
214 parse_end = time.time()
215
216 if request.json:
217 json_str = kunit_json.get_json_result(
218 test=test_result,
195 parse_start = time.time()
196
197 test_result = kunit_parser.Test()
198
199 if request.raw_output:
200 # Treat unparsed results as one passing test.
201 test_result.status = kunit_parser.TestStatus.SUCCESS
202 test_result.counts.passed = 1

--- 10 unchanged lines hidden (view full) ---

213
214 else:
215 test_result = kunit_parser.parse_run_tests(input_data)
216 parse_end = time.time()
217
218 if request.json:
219 json_str = kunit_json.get_json_result(
220 test=test_result,
219 def_config='kunit_defconfig',
220 build_dir=request.build_dir)
221 metadata=metadata)
221 if request.json == 'stdout':
222 print(json_str)
223 else:
224 with open(request.json, 'w') as f:
225 f.write(json_str)
226 kunit_parser.print_with_timestamp("Test results stored in %s" %
227 os.path.abspath(request.json))
228

--- 270 unchanged lines hidden (view full) ---

499 sys.exit(1)
500 elif cli_args.subcommand == 'parse':
501 if cli_args.file == None:
502 sys.stdin.reconfigure(errors='backslashreplace') # pytype: disable=attribute-error
503 kunit_output = sys.stdin
504 else:
505 with open(cli_args.file, 'r', errors='backslashreplace') as f:
506 kunit_output = f.read().splitlines()
222 if request.json == 'stdout':
223 print(json_str)
224 else:
225 with open(request.json, 'w') as f:
226 f.write(json_str)
227 kunit_parser.print_with_timestamp("Test results stored in %s" %
228 os.path.abspath(request.json))
229

--- 270 unchanged lines hidden (view full) ---

500 sys.exit(1)
501 elif cli_args.subcommand == 'parse':
502 if cli_args.file == None:
503 sys.stdin.reconfigure(errors='backslashreplace') # pytype: disable=attribute-error
504 kunit_output = sys.stdin
505 else:
506 with open(cli_args.file, 'r', errors='backslashreplace') as f:
507 kunit_output = f.read().splitlines()
508 # We know nothing about how the result was created!
509 metadata = kunit_json.Metadata()
507 request = KunitParseRequest(raw_output=cli_args.raw_output,
510 request = KunitParseRequest(raw_output=cli_args.raw_output,
508 build_dir='',
509 json=cli_args.json)
511 json=cli_args.json)
510 result, _ = parse_tests(request, kunit_output)
512 result, _ = parse_tests(request, metadata, kunit_output)
511 if result.status != KunitStatus.SUCCESS:
512 sys.exit(1)
513 else:
514 parser.print_help()
515
516if __name__ == '__main__':
517 main(sys.argv[1:])
513 if result.status != KunitStatus.SUCCESS:
514 sys.exit(1)
515 else:
516 parser.print_help()
517
518if __name__ == '__main__':
519 main(sys.argv[1:])