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 9from dataclasses import dataclass 10import json 11import os 12 13import kunit_parser 14 15from kunit_parser import Test, TestStatus 16from typing import Any, Dict 17 18@dataclass 19class Metadata: 20 """Stores metadata about this run to include in get_json_result().""" 21 arch: str = '' 22 def_config: str = '' 23 build_dir: str = '' 24 25JsonObj = Dict[str, Any] 26 27_status_map: Dict[TestStatus, str] = { 28 TestStatus.SUCCESS: "PASS", 29 TestStatus.SKIPPED: "SKIP", 30 TestStatus.TEST_CRASHED: "ERROR", 31} 32 33def _get_group_json(test: Test, common_fields: JsonObj) -> JsonObj: 34 sub_groups = [] # List[JsonObj] 35 test_cases = [] # List[JsonObj] 36 37 for subtest in test.subtests: 38 if subtest.subtests: 39 sub_group = _get_group_json(subtest, common_fields) 40 sub_groups.append(sub_group) 41 continue 42 status = _status_map.get(subtest.status, "FAIL") 43 test_cases.append({"name": subtest.name, "status": status}) 44 45 test_group = { 46 "name": test.name, 47 "sub_groups": sub_groups, 48 "test_cases": test_cases, 49 } 50 test_group.update(common_fields) 51 return test_group 52 53def get_json_result(test: Test, metadata: Metadata) -> str: 54 common_fields = { 55 "arch": metadata.arch, 56 "defconfig": metadata.def_config, 57 "build_environment": metadata.build_dir, 58 "lab_name": None, 59 "kernel": None, 60 "job": None, 61 "git_branch": "kselftest", 62 } 63 64 test_group = _get_group_json(test, common_fields) 65 test_group["name"] = "KUnit Test Group" 66 return json.dumps(test_group, indent=4) 67