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