CIAssignUnitTest.py 5.5 KB

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