CIAssignUnitTest.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. """
  2. Command line tool to assign unit tests to CI test jobs.
  3. """
  4. import re
  5. import os
  6. import sys
  7. import argparse
  8. import yaml
  9. test_fw_path = os.getenv("TEST_FW_PATH")
  10. if test_fw_path:
  11. sys.path.insert(0, test_fw_path)
  12. from Utility import CIAssignTest
  13. class Group(CIAssignTest.Group):
  14. SORT_KEYS = ["config", "SDK", "test environment", "multi_device", "multi_stage", "tags"]
  15. MAX_CASE = 30
  16. ATTR_CONVERT_TABLE = {
  17. "execution_time": "execution time"
  18. }
  19. # when IDF support multiple chips, SDK will be moved into tags, we can remove it
  20. CI_JOB_MATCH_KEYS = ["test environment", "SDK"]
  21. def __init__(self, case):
  22. super(Group, self).__init__(case)
  23. for tag in self._get_case_attr(case, "tags"):
  24. self.ci_job_match_keys.add(tag)
  25. @staticmethod
  26. def _get_case_attr(case, attr):
  27. if attr in Group.ATTR_CONVERT_TABLE:
  28. attr = Group.ATTR_CONVERT_TABLE[attr]
  29. return case[attr]
  30. def _create_extra_data(self, test_function):
  31. """
  32. For unit test case, we need to copy some attributes of test cases into config file.
  33. So unit test function knows how to run the case.
  34. """
  35. case_data = []
  36. for case in self.case_list:
  37. one_case_data = {
  38. "config": self._get_case_attr(case, "config"),
  39. "name": self._get_case_attr(case, "summary"),
  40. "reset": self._get_case_attr(case, "reset"),
  41. "timeout": self._get_case_attr(case, "timeout"),
  42. }
  43. if test_function in ["run_multiple_devices_cases", "run_multiple_stage_cases"]:
  44. try:
  45. one_case_data["child case num"] = self._get_case_attr(case, "child case num")
  46. except KeyError as e:
  47. print("multiple devices/stages cases must contains at least two test functions")
  48. print("case name: {}".format(one_case_data["name"]))
  49. raise e
  50. case_data.append(one_case_data)
  51. return case_data
  52. def _map_test_function(self):
  53. """
  54. determine which test function to use according to current test case
  55. :return: test function name to use
  56. """
  57. if self.filters["multi_device"] == "Yes":
  58. test_function = "run_multiple_devices_cases"
  59. elif self.filters["multi_stage"] == "Yes":
  60. test_function = "run_multiple_stage_cases"
  61. else:
  62. test_function = "run_unit_test_cases"
  63. return test_function
  64. def output(self):
  65. """
  66. output data for job configs
  67. :return: {"Filter": case filter, "CaseConfig": list of case configs for cases in this group}
  68. """
  69. test_function = self._map_test_function()
  70. output_data = {
  71. # we don't need filter for test function, as UT uses a few test functions for all cases
  72. "CaseConfig": [
  73. {
  74. "name": test_function,
  75. "extra_data": self._create_extra_data(test_function),
  76. }
  77. ]
  78. }
  79. return output_data
  80. class UnitTestAssignTest(CIAssignTest.AssignTest):
  81. CI_TEST_JOB_PATTERN = re.compile(r"^UT_.+")
  82. def __init__(self, test_case_path, ci_config_file):
  83. CIAssignTest.AssignTest.__init__(self, test_case_path, ci_config_file, case_group=Group)
  84. def _search_cases(self, test_case_path, case_filter=None):
  85. """
  86. For unit test case, we don't search for test functions.
  87. The unit test cases is stored in a yaml file which is created in job build-idf-test.
  88. """
  89. with open(test_case_path, "r") as f:
  90. raw_data = yaml.load(f)
  91. test_cases = raw_data["test cases"]
  92. if case_filter:
  93. for key in case_filter:
  94. filtered_cases = []
  95. for case in test_cases:
  96. try:
  97. # bot converts string to lower case
  98. if isinstance(case[key], str):
  99. _value = case[key].lower()
  100. else:
  101. _value = case[key]
  102. if _value in case_filter[key]:
  103. filtered_cases.append(case)
  104. except KeyError:
  105. # case don't have this key, regard as filter success
  106. filtered_cases.append(case)
  107. test_cases = filtered_cases
  108. return test_cases
  109. if __name__ == '__main__':
  110. parser = argparse.ArgumentParser()
  111. parser.add_argument("test_case",
  112. help="test case folder or file")
  113. parser.add_argument("ci_config_file",
  114. help="gitlab ci config file")
  115. parser.add_argument("output_path",
  116. help="output path of config files")
  117. args = parser.parse_args()
  118. assign_test = UnitTestAssignTest(args.test_case, args.ci_config_file)
  119. assign_test.assign_cases()
  120. assign_test.output_configs(args.output_path)