Skip to content

Commit 21a6d17

Browse files
Heidifahimshuahkh
authored andcommitted
kunit: tool: allow generating test results in JSON
Add a --json flag, which when specified generates JSON formatted test results conforming to the KernelCI API test_group spec[1]. The user can use the new flag to specify a filename to print the json formatted results to. Link[1]: https://api.kernelci.org/schema-test-group.html#post Signed-off-by: Heidi Fahim <heidifahim@google.com> Signed-off-by: Brendan Higgins <brendanhiggins@google.com> Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
1 parent 5578d00 commit 21a6d17

3 files changed

Lines changed: 125 additions & 6 deletions

File tree

tools/testing/kunit/kunit.py

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from enum import Enum, auto
1818

1919
import kunit_config
20+
import kunit_json
2021
import kunit_kernel
2122
import kunit_parser
2223

@@ -30,9 +31,9 @@
3031
KunitExecRequest = namedtuple('KunitExecRequest',
3132
['timeout', 'build_dir', 'alltests'])
3233
KunitParseRequest = namedtuple('KunitParseRequest',
33-
['raw_output', 'input_data'])
34+
['raw_output', 'input_data', 'build_dir', 'json'])
3435
KunitRequest = namedtuple('KunitRequest', ['raw_output','timeout', 'jobs',
35-
'build_dir', 'alltests',
36+
'build_dir', 'alltests', 'json',
3637
'make_options'])
3738

3839
KernelDirectoryPath = sys.argv[0].split('tools/testing/kunit/')[0]
@@ -113,12 +114,22 @@ def parse_tests(request: KunitParseRequest) -> KunitResult:
113114
test_result = kunit_parser.TestResult(kunit_parser.TestStatus.SUCCESS,
114115
[],
115116
'Tests not Parsed.')
117+
116118
if request.raw_output:
117119
kunit_parser.raw_output(request.input_data)
118120
else:
119121
test_result = kunit_parser.parse_run_tests(request.input_data)
120122
parse_end = time.time()
121123

124+
if request.json:
125+
json_obj = kunit_json.get_json_result(
126+
test_result=test_result,
127+
def_config='kunit_defconfig',
128+
build_dir=request.build_dir,
129+
json_path=request.json)
130+
if request.json == 'stdout':
131+
print(json_obj)
132+
122133
if test_result.status != kunit_parser.TestStatus.SUCCESS:
123134
return KunitResult(KunitStatus.TEST_FAILURE, test_result,
124135
parse_end - parse_start)
@@ -151,7 +162,9 @@ def run_tests(linux: kunit_kernel.LinuxSourceTree,
151162
return exec_result
152163

153164
parse_request = KunitParseRequest(request.raw_output,
154-
exec_result.result)
165+
exec_result.result,
166+
request.build_dir,
167+
request.json)
155168
parse_result = parse_tests(parse_request)
156169

157170
run_end = time.time()
@@ -195,7 +208,12 @@ def add_exec_opts(parser):
195208
def add_parse_opts(parser):
196209
parser.add_argument('--raw_output', help='don\'t format output from kernel',
197210
action='store_true')
198-
211+
parser.add_argument('--json',
212+
nargs='?',
213+
help='Stores test results in a JSON, and either '
214+
'prints to stdout or saves to file if a '
215+
'filename is specified',
216+
type=str, const='stdout', default=None)
199217

200218
def main(argv, linux=None):
201219
parser = argparse.ArgumentParser(
@@ -253,6 +271,7 @@ def main(argv, linux=None):
253271
cli_args.jobs,
254272
cli_args.build_dir,
255273
cli_args.alltests,
274+
cli_args.json,
256275
cli_args.make_options)
257276
result = run_tests(linux, request)
258277
if result.status != KunitStatus.SUCCESS:
@@ -297,7 +316,9 @@ def main(argv, linux=None):
297316
cli_args.alltests)
298317
exec_result = exec_tests(linux, exec_request)
299318
parse_request = KunitParseRequest(cli_args.raw_output,
300-
exec_result.result)
319+
exec_result.result,
320+
cli_args.build_dir,
321+
cli_args.json)
301322
result = parse_tests(parse_request)
302323
kunit_parser.print_with_timestamp((
303324
'Elapsed time: %.3fs\n') % (
@@ -311,7 +332,9 @@ def main(argv, linux=None):
311332
with open(cli_args.file, 'r') as f:
312333
kunit_output = f.read().splitlines()
313334
request = KunitParseRequest(cli_args.raw_output,
314-
kunit_output)
335+
kunit_output,
336+
cli_args.build_dir,
337+
cli_args.json)
315338
result = parse_tests(request)
316339
if result.status != KunitStatus.SUCCESS:
317340
sys.exit(1)

tools/testing/kunit/kunit_json.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# SPDX-License-Identifier: GPL-2.0
2+
#
3+
# Generates JSON from KUnit results according to
4+
# KernelCI spec: https://github.com/kernelci/kernelci-doc/wiki/Test-API
5+
#
6+
# Copyright (C) 2020, Google LLC.
7+
# Author: Heidi Fahim <heidifahim@google.com>
8+
9+
import json
10+
import os
11+
12+
import kunit_parser
13+
14+
from kunit_parser import TestStatus
15+
16+
def get_json_result(test_result, def_config, build_dir, json_path):
17+
sub_groups = []
18+
19+
# Each test suite is mapped to a KernelCI sub_group
20+
for test_suite in test_result.suites:
21+
sub_group = {
22+
"name": test_suite.name,
23+
"arch": "UM",
24+
"defconfig": def_config,
25+
"build_environment": build_dir,
26+
"test_cases": [],
27+
"lab_name": None,
28+
"kernel": None,
29+
"job": None,
30+
"git_branch": "kselftest",
31+
}
32+
test_cases = []
33+
# TODO: Add attachments attribute in test_case with detailed
34+
# failure message, see https://api.kernelci.org/schema-test-case.html#get
35+
for case in test_suite.cases:
36+
test_case = {"name": case.name, "status": "FAIL"}
37+
if case.status == TestStatus.SUCCESS:
38+
test_case["status"] = "PASS"
39+
elif case.status == TestStatus.TEST_CRASHED:
40+
test_case["status"] = "ERROR"
41+
test_cases.append(test_case)
42+
sub_group["test_cases"] = test_cases
43+
sub_groups.append(sub_group)
44+
test_group = {
45+
"name": "KUnit Test Group",
46+
"arch": "UM",
47+
"defconfig": def_config,
48+
"build_environment": build_dir,
49+
"sub_groups": sub_groups,
50+
"lab_name": None,
51+
"kernel": None,
52+
"job": None,
53+
"git_branch": "kselftest",
54+
}
55+
json_obj = json.dumps(test_group, indent=4)
56+
if json_path != 'stdout':
57+
with open(json_path, 'w') as result_path:
58+
result_path.write(json_obj)
59+
root = __file__.split('tools/testing/kunit/')[0]
60+
kunit_parser.print_with_timestamp(
61+
"Test results stored in %s" %
62+
os.path.join(root, result_path.name))
63+
return json_obj

tools/testing/kunit/kunit_tool_test.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111

1212
import tempfile, shutil # Handling test_tmpdir
1313

14+
import json
1415
import os
1516

1617
import kunit_config
1718
import kunit_parser
1819
import kunit_kernel
20+
import kunit_json
1921
import kunit
2022

2123
test_tmpdir = ''
@@ -230,6 +232,37 @@ def test_pound_no_prefix(self):
230232
result = kunit_parser.parse_run_tests(file.readlines())
231233
self.assertEqual('kunit-resource-test', result.suites[0].name)
232234

235+
class KUnitJsonTest(unittest.TestCase):
236+
237+
def _json_for(self, log_file):
238+
with(open(get_absolute_path(log_file))) as file:
239+
test_result = kunit_parser.parse_run_tests(file)
240+
json_obj = kunit_json.get_json_result(
241+
test_result=test_result,
242+
def_config='kunit_defconfig',
243+
build_dir=None,
244+
json_path='stdout')
245+
return json.loads(json_obj)
246+
247+
def test_failed_test_json(self):
248+
result = self._json_for(
249+
'test_data/test_is_test_passed-failure.log')
250+
self.assertEqual(
251+
{'name': 'example_simple_test', 'status': 'FAIL'},
252+
result["sub_groups"][1]["test_cases"][0])
253+
254+
def test_crashed_test_json(self):
255+
result = self._json_for(
256+
'test_data/test_is_test_passed-crash.log')
257+
self.assertEqual(
258+
{'name': 'example_simple_test', 'status': 'ERROR'},
259+
result["sub_groups"][1]["test_cases"][0])
260+
261+
def test_no_tests_json(self):
262+
result = self._json_for(
263+
'test_data/test_is_test_passed-no_tests_run.log')
264+
self.assertEqual(0, len(result['sub_groups']))
265+
233266
class StrContains(str):
234267
def __eq__(self, other):
235268
return self in other

0 commit comments

Comments
 (0)