Back to home page

OSCL-LXR

 
 

    


0001 # SPDX-License-Identifier: GPL-2.0
0002 #
0003 # Generates JSON from KUnit results according to
0004 # KernelCI spec: https://github.com/kernelci/kernelci-doc/wiki/Test-API
0005 #
0006 # Copyright (C) 2020, Google LLC.
0007 # Author: Heidi Fahim <heidifahim@google.com>
0008 
0009 from dataclasses import dataclass
0010 import json
0011 from typing import Any, Dict
0012 
0013 from kunit_parser import Test, TestStatus
0014 
0015 @dataclass
0016 class Metadata:
0017     """Stores metadata about this run to include in get_json_result()."""
0018     arch: str = ''
0019     def_config: str = ''
0020     build_dir: str = ''
0021 
0022 JsonObj = Dict[str, Any]
0023 
0024 _status_map: Dict[TestStatus, str] = {
0025     TestStatus.SUCCESS: "PASS",
0026     TestStatus.SKIPPED: "SKIP",
0027     TestStatus.TEST_CRASHED: "ERROR",
0028 }
0029 
0030 def _get_group_json(test: Test, common_fields: JsonObj) -> JsonObj:
0031     sub_groups = []  # List[JsonObj]
0032     test_cases = []  # List[JsonObj]
0033 
0034     for subtest in test.subtests:
0035         if subtest.subtests:
0036             sub_group = _get_group_json(subtest, common_fields)
0037             sub_groups.append(sub_group)
0038             continue
0039         status = _status_map.get(subtest.status, "FAIL")
0040         test_cases.append({"name": subtest.name, "status": status})
0041 
0042     test_group = {
0043         "name": test.name,
0044         "sub_groups": sub_groups,
0045         "test_cases": test_cases,
0046     }
0047     test_group.update(common_fields)
0048     return test_group
0049 
0050 def get_json_result(test: Test, metadata: Metadata) -> str:
0051     common_fields = {
0052         "arch": metadata.arch,
0053         "defconfig": metadata.def_config,
0054         "build_environment": metadata.build_dir,
0055         "lab_name": None,
0056         "kernel": None,
0057         "job": None,
0058         "git_branch": "kselftest",
0059     }
0060 
0061     test_group = _get_group_json(test, common_fields)
0062     test_group["name"] = "KUnit Test Group"
0063     return json.dumps(test_group, indent=4)