CIAssignUnitTest.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 = ["Test App", "SDK", "test environment", "multi_device", "multi_stage"]
  15. CI_JOB_MATCH_KEYS = ["Test App", "SDK", "test environment"]
  16. MAX_CASE = 30
  17. ATTR_CONVERT_TABLE = {
  18. "execution_time": "execution time"
  19. }
  20. @staticmethod
  21. def _get_case_attr(case, attr):
  22. if attr in Group.ATTR_CONVERT_TABLE:
  23. attr = Group.ATTR_CONVERT_TABLE[attr]
  24. return case[attr]
  25. @staticmethod
  26. def _get_ut_config(test_app):
  27. # we format test app "UT_ + config" when parsing test cases
  28. # now we need to extract config
  29. assert test_app[:3] == "UT_"
  30. return test_app[3:]
  31. def _create_extra_data(self, test_function):
  32. case_data = []
  33. for case in self.case_list:
  34. one_case_data = {
  35. "config": self._get_ut_config(self._get_case_attr(case, "Test App")),
  36. "name": self._get_case_attr(case, "summary"),
  37. "reset": self._get_case_attr(case, "reset"),
  38. }
  39. if test_function in ["run_multiple_devices_cases", "run_multiple_stage_cases"]:
  40. try:
  41. one_case_data["child case num"] = self._get_case_attr(case, "child case num")
  42. except KeyError as e:
  43. print("multiple devices/stages cases must contains at least two test functions")
  44. print("case name: {}".format(one_case_data["name"]))
  45. raise e
  46. case_data.append(one_case_data)
  47. return case_data
  48. def _map_test_function(self):
  49. """
  50. determine which test function to use according to current test case
  51. :return: test function name to use
  52. """
  53. if self.filters["multi_device"] == "Yes":
  54. test_function = "run_multiple_devices_cases"
  55. elif self.filters["multi_stage"] == "Yes":
  56. test_function = "run_multiple_stage_cases"
  57. else:
  58. test_function = "run_unit_test_cases"
  59. return test_function
  60. def output(self):
  61. """
  62. output data for job configs
  63. :return: {"Filter": case filter, "CaseConfig": list of case configs for cases in this group}
  64. """
  65. test_function = self._map_test_function()
  66. output_data = {
  67. # we don't need filter for test function, as UT uses a few test functions for all cases
  68. "CaseConfig": [
  69. {
  70. "name": test_function,
  71. "extra_data": self._create_extra_data(test_function),
  72. }
  73. ]
  74. }
  75. return output_data
  76. class UnitTestAssignTest(CIAssignTest.AssignTest):
  77. CI_TEST_JOB_PATTERN = re.compile(r"^UT_.+")
  78. def __init__(self, test_case_path, ci_config_file):
  79. CIAssignTest.AssignTest.__init__(self, test_case_path, ci_config_file, case_group=Group)
  80. @staticmethod
  81. def _search_cases(test_case_path, case_filter=None):
  82. """
  83. For unit test case, we don't search for test functions.
  84. The unit test cases is stored in a yaml file which is created in job build-idf-test.
  85. """
  86. with open(test_case_path, "r") as f:
  87. raw_data = yaml.load(f)
  88. test_cases = raw_data["test cases"]
  89. if case_filter:
  90. for key in case_filter:
  91. filtered_cases = []
  92. for case in test_cases:
  93. try:
  94. # bot converts string to lower case
  95. if isinstance(case[key], str):
  96. _value = case[key].lower()
  97. else:
  98. _value = case[key]
  99. if _value in case_filter[key]:
  100. filtered_cases.append(case)
  101. except KeyError:
  102. # case don't have this key, regard as filter success
  103. filtered_cases.append(case)
  104. test_cases = filtered_cases
  105. return test_cases
  106. if __name__ == '__main__':
  107. parser = argparse.ArgumentParser()
  108. parser.add_argument("test_case",
  109. help="test case folder or file")
  110. parser.add_argument("ci_config_file",
  111. help="gitlab ci config file")
  112. parser.add_argument("output_path",
  113. help="output path of config files")
  114. args = parser.parse_args()
  115. assign_test = UnitTestAssignTest(args.test_case, args.ci_config_file)
  116. assign_test.assign_cases()
  117. assign_test.output_configs(args.output_path)