| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 |
|
| 9 | from dataclasses import dataclass
|
| 10 | import json
|
| 11 | from typing import Any, Dict
|
| 12 |
|
| 13 | from kunit_parser import Test, TestStatus
|
| 14 |
|
| 15 | @dataclass
|
| 16 | class Metadata:
|
| 17 |
|
| 18 | arch: str = ''
|
| 19 | def_config: str = ''
|
| 20 | build_dir: str = ''
|
| 21 |
|
| 22 | JsonObj = 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 |
|
| 30 | def _get_group_json(test: Test, common_fields: JsonObj) -> JsonObj:
|
| 31 | sub_groups = [] |
| 32 | test_cases = [] |
| 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_counts = test.counts
|
| 43 | counts_json = {
|
| 44 | "tests": test_counts.total(),
|
| 45 | "passed": test_counts.passed,
|
| 46 | "failed": test_counts.failed,
|
| 47 | "crashed": test_counts.crashed,
|
| 48 | "skipped": test_counts.skipped,
|
| 49 | "errors": test_counts.errors,
|
| 50 | }
|
| 51 | test_group = {
|
| 52 | "name": test.name,
|
| 53 | "sub_groups": sub_groups,
|
| 54 | "test_cases": test_cases,
|
| 55 | "misc": counts_json
|
| 56 | }
|
| 57 | test_group.update(common_fields)
|
| 58 | return test_group
|
| 59 |
|
| 60 | def get_json_result(test: Test, metadata: Metadata) -> str:
|
| 61 | common_fields = {
|
| 62 | "arch": metadata.arch,
|
| 63 | "defconfig": metadata.def_config,
|
| 64 | "build_environment": metadata.build_dir,
|
| 65 | "lab_name": None,
|
| 66 | "kernel": None,
|
| 67 | "job": None,
|
| 68 | "git_branch": "kselftest",
|
| 69 | }
|
| 70 |
|
| 71 | test_group = _get_group_json(test, common_fields)
|
| 72 | test_group["name"] = "KUnit Test Group"
|
| 73 | return json.dumps(test_group, indent=4)
|