1 # SPDX-License-Identifier: GPL-2.0
3 # Generates JSON from KUnit results according to
4 # KernelCI spec: https://github.com/kernelci/kernelci-doc/wiki/Test-API
6 # Copyright (C) 2020, Google LLC.
7 # Author: Heidi Fahim <heidifahim@google.com>
9 from dataclasses import dataclass
11 from typing import Any, Dict
13 from kunit_parser import Test, TestStatus
17 """Stores metadata about this run to include in get_json_result()."""
22 JsonObj = Dict[str, Any]
24 _status_map: Dict[TestStatus, str] = {
25 TestStatus.SUCCESS: "PASS",
26 TestStatus.SKIPPED: "SKIP",
27 TestStatus.TEST_CRASHED: "ERROR",
30 def _get_group_json(test: Test, common_fields: JsonObj) -> JsonObj:
31 sub_groups = [] # List[JsonObj]
32 test_cases = [] # List[JsonObj]
34 for subtest in test.subtests:
36 sub_group = _get_group_json(subtest, common_fields)
37 sub_groups.append(sub_group)
39 status = _status_map.get(subtest.status, "FAIL")
40 test_cases.append({"name": subtest.name, "status": status})
44 "sub_groups": sub_groups,
45 "test_cases": test_cases,
47 test_group.update(common_fields)
50 def get_json_result(test: Test, metadata: Metadata) -> str:
52 "arch": metadata.arch,
53 "defconfig": metadata.def_config,
54 "build_environment": metadata.build_dir,
58 "git_branch": "kselftest",
61 test_group = _get_group_json(test, common_fields)
62 test_group["name"] = "KUnit Test Group"
63 return json.dumps(test_group, indent=4)