kunit_json.py 1.6 KB

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