xref: /linux/tools/testing/kunit/kunit_json.py (revision 6bd0f52ee8f400a558f1c0f33e1f3fd3ef4922a8)
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
9import json
10import os
11
12import kunit_parser
13
14from kunit_parser import Test, TestStatus
15from typing import Any, Dict
16
17JsonObj = Dict[str, Any]
18
19_status_map: Dict[TestStatus, str] = {
20	TestStatus.SUCCESS: "PASS",
21	TestStatus.SKIPPED: "SKIP",
22	TestStatus.TEST_CRASHED: "ERROR",
23}
24
25def _get_group_json(test: Test, def_config: str, build_dir: str) -> JsonObj:
26	sub_groups = []  # List[JsonObj]
27	test_cases = []  # List[JsonObj]
28
29	for subtest in test.subtests:
30		if subtest.subtests:
31			sub_group = _get_group_json(subtest, def_config,
32				build_dir)
33			sub_groups.append(sub_group)
34			continue
35		status = _status_map.get(subtest.status, "FAIL")
36		test_cases.append({"name": subtest.name, "status": status})
37
38	test_group = {
39		"name": test.name,
40		"arch": "UM",
41		"defconfig": def_config,
42		"build_environment": build_dir,
43		"sub_groups": sub_groups,
44		"test_cases": test_cases,
45		"lab_name": None,
46		"kernel": None,
47		"job": None,
48		"git_branch": "kselftest",
49	}
50	return test_group
51
52def get_json_result(test: Test, def_config: str, build_dir: str) -> str:
53	test_group = _get_group_json(test, def_config, build_dir)
54	test_group["name"] = "KUnit Test Group"
55	return json.dumps(test_group, indent=4)
56