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, Optional 16 17JsonObj = Dict[str, Any] 18 19def _get_group_json(test: Test, def_config: str, 20 build_dir: Optional[str]) -> JsonObj: 21 sub_groups = [] # List[JsonObj] 22 test_cases = [] # List[JsonObj] 23 24 for subtest in test.subtests: 25 if len(subtest.subtests): 26 sub_group = _get_group_json(subtest, def_config, 27 build_dir) 28 sub_groups.append(sub_group) 29 else: 30 test_case = {"name": subtest.name, "status": "FAIL"} 31 if subtest.status == TestStatus.SUCCESS: 32 test_case["status"] = "PASS" 33 elif subtest.status == TestStatus.SKIPPED: 34 test_case["status"] = "SKIP" 35 elif subtest.status == TestStatus.TEST_CRASHED: 36 test_case["status"] = "ERROR" 37 test_cases.append(test_case) 38 39 test_group = { 40 "name": test.name, 41 "arch": "UM", 42 "defconfig": def_config, 43 "build_environment": build_dir, 44 "sub_groups": sub_groups, 45 "test_cases": test_cases, 46 "lab_name": None, 47 "kernel": None, 48 "job": None, 49 "git_branch": "kselftest", 50 } 51 return test_group 52 53def get_json_result(test: Test, def_config: str, 54 build_dir: Optional[str], json_path: str) -> str: 55 test_group = _get_group_json(test, def_config, build_dir) 56 test_group["name"] = "KUnit Test Group" 57 json_obj = json.dumps(test_group, indent=4) 58 if json_path != 'stdout': 59 with open(json_path, 'w') as result_path: 60 result_path.write(json_obj) 61 root = __file__.split('tools/testing/kunit/')[0] 62 kunit_parser.print_with_timestamp( 63 "Test results stored in %s" % 64 os.path.join(root, result_path.name)) 65 return json_obj 66