kunit_json.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. 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_counts = test.counts
  35. counts_json = {
  36. "tests": test_counts.total(),
  37. "passed": test_counts.passed,
  38. "failed": test_counts.failed,
  39. "crashed": test_counts.crashed,
  40. "skipped": test_counts.skipped,
  41. "errors": test_counts.errors,
  42. }
  43. test_group = {
  44. "name": test.name,
  45. "sub_groups": sub_groups,
  46. "test_cases": test_cases,
  47. "misc": counts_json
  48. }
  49. test_group.update(common_fields)
  50. return test_group
  51. def get_json_result(test: Test, metadata: Metadata) -> str:
  52. common_fields = {
  53. "arch": metadata.arch,
  54. "defconfig": metadata.def_config,
  55. "build_environment": metadata.build_dir,
  56. "lab_name": None,
  57. "kernel": None,
  58. "job": None,
  59. "git_branch": "kselftest",
  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)