CIScanTests.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. import argparse
  2. import errno
  3. import json
  4. import logging
  5. import os
  6. from collections import defaultdict
  7. from copy import deepcopy
  8. try:
  9. from typing import Any
  10. except ImportError:
  11. # Only used for type annotations
  12. pass
  13. from find_apps import find_apps
  14. from find_build_apps import BUILD_SYSTEM_CMAKE, BUILD_SYSTEMS
  15. from idf_py_actions.constants import PREVIEW_TARGETS, SUPPORTED_TARGETS
  16. from ttfw_idf.IDFAssignTest import ExampleAssignTest, TestAppsAssignTest
  17. TEST_LABELS = {
  18. 'example_test': 'BOT_LABEL_EXAMPLE_TEST',
  19. 'test_apps': 'BOT_LABEL_CUSTOM_TEST',
  20. 'component_ut': ['BOT_LABEL_UNIT_TEST',
  21. 'BOT_LABEL_UNIT_TEST_32',
  22. 'BOT_LABEL_UNIT_TEST_S2',
  23. 'BOT_LABEL_UNIT_TEST_C3'],
  24. }
  25. BUILD_ALL_LABELS = [
  26. 'BOT_LABEL_BUILD',
  27. 'BOT_LABEL_BUILD_ALL_APPS',
  28. 'BOT_LABEL_REGULAR_TEST',
  29. 'BOT_LABEL_WEEKEND_TEST',
  30. ]
  31. def _has_build_all_label(): # type: () -> bool
  32. for label in BUILD_ALL_LABELS:
  33. if os.getenv(label):
  34. return True
  35. return False
  36. def _judge_build_or_not(action, build_all): # type: (str, bool) -> tuple[bool, bool]
  37. """
  38. :return: (build_or_not_for_test_related_apps, build_or_not_for_non_related_apps)
  39. """
  40. if build_all or _has_build_all_label() or (not os.getenv('BOT_TRIGGER_WITH_LABEL')):
  41. logging.info('Build all apps')
  42. return True, True
  43. labels = TEST_LABELS[action]
  44. if not isinstance(labels, list):
  45. labels = [labels] # type: ignore
  46. for label in labels:
  47. if os.getenv(label):
  48. logging.info('Build only test cases apps')
  49. return True, False
  50. logging.info('Skip all')
  51. return False, False
  52. def output_json(apps_dict_list, target, build_system, output_dir): # type: (list, str, str, str) -> None
  53. output_path = os.path.join(output_dir, 'scan_{}_{}.json'.format(target.lower(), build_system))
  54. with open(output_path, 'w') as fw:
  55. fw.writelines([json.dumps(app) + '\n' for app in apps_dict_list])
  56. # we might need artifacts to run test cases locally.
  57. # So we need to save artifacts which have test case not executed by CI.
  58. class _ExampleAssignTest(ExampleAssignTest):
  59. DEFAULT_FILTER = {} # type: dict[str, Any]
  60. class _TestAppsAssignTest(TestAppsAssignTest):
  61. DEFAULT_FILTER = {} # type: dict[str, Any]
  62. def main(): # type: () -> None
  63. parser = argparse.ArgumentParser(description='Scan the required build tests')
  64. parser.add_argument('test_type',
  65. choices=TEST_LABELS.keys(),
  66. help='Scan test type')
  67. parser.add_argument('paths', nargs='+',
  68. help='One or more app paths')
  69. parser.add_argument('-b', '--build-system',
  70. choices=BUILD_SYSTEMS.keys(),
  71. default=BUILD_SYSTEM_CMAKE)
  72. parser.add_argument('-c', '--ci-config-file',
  73. required=True,
  74. help='gitlab ci config target-test file')
  75. parser.add_argument('-o', '--output-path',
  76. required=True,
  77. help='output path of the scan result')
  78. parser.add_argument('--exclude', nargs='*',
  79. help='Ignore specified directory. Can be used multiple times.')
  80. parser.add_argument('--preserve', action='store_true',
  81. help='add this flag to preserve artifacts for all apps')
  82. parser.add_argument('--build-all', action='store_true',
  83. help='add this flag to build all apps')
  84. args = parser.parse_args()
  85. build_test_case_apps, build_standalone_apps = _judge_build_or_not(args.test_type, args.build_all)
  86. if not os.path.exists(args.output_path):
  87. try:
  88. os.makedirs(args.output_path)
  89. except OSError as e:
  90. if e.errno != errno.EEXIST:
  91. raise e
  92. SUPPORTED_TARGETS.extend(PREVIEW_TARGETS)
  93. if (not build_standalone_apps) and (not build_test_case_apps):
  94. for target in SUPPORTED_TARGETS:
  95. output_json([], target, args.build_system, args.output_path)
  96. SystemExit(0)
  97. paths = set([os.path.join(str(os.getenv('IDF_PATH')), path) if not os.path.isabs(path) else path for path in args.paths])
  98. test_cases = []
  99. for path in paths:
  100. if args.test_type == 'example_test':
  101. assign = _ExampleAssignTest(path, args.ci_config_file)
  102. elif args.test_type in ['test_apps', 'component_ut']:
  103. assign = _TestAppsAssignTest(path, args.ci_config_file)
  104. else:
  105. raise SystemExit(1) # which is impossible
  106. test_cases.extend(assign.search_cases())
  107. '''
  108. {
  109. <target>: {
  110. 'test_case_apps': [<app_dir>], # which is used in target tests
  111. 'standalone_apps': [<app_dir>], # which is not
  112. },
  113. ...
  114. }
  115. '''
  116. scan_info_dict = defaultdict(dict) # type: dict[str, dict]
  117. # store the test cases dir, exclude these folders when scan for standalone apps
  118. default_exclude = args.exclude if args.exclude else []
  119. build_system = args.build_system.lower()
  120. build_system_class = BUILD_SYSTEMS[build_system]
  121. for target in SUPPORTED_TARGETS:
  122. exclude_apps = deepcopy(default_exclude)
  123. if build_test_case_apps:
  124. scan_info_dict[target]['test_case_apps'] = set()
  125. for case in test_cases:
  126. app_dir = case.case_info['app_dir']
  127. app_target = case.case_info['target']
  128. if app_target.lower() != target.lower():
  129. continue
  130. _apps = find_apps(build_system_class, app_dir, True, exclude_apps, target.lower())
  131. if _apps:
  132. scan_info_dict[target]['test_case_apps'].update(_apps)
  133. exclude_apps.append(app_dir)
  134. else:
  135. scan_info_dict[target]['test_case_apps'] = set()
  136. if build_standalone_apps:
  137. scan_info_dict[target]['standalone_apps'] = set()
  138. for path in paths:
  139. scan_info_dict[target]['standalone_apps'].update(
  140. find_apps(build_system_class, path, True, exclude_apps, target.lower()))
  141. else:
  142. scan_info_dict[target]['standalone_apps'] = set()
  143. test_case_apps_preserve_default = True if build_system == 'cmake' else False
  144. for target in SUPPORTED_TARGETS:
  145. apps = []
  146. for app_dir in scan_info_dict[target]['test_case_apps']:
  147. apps.append({
  148. 'app_dir': app_dir,
  149. 'build_system': args.build_system,
  150. 'target': target,
  151. 'preserve': args.preserve or test_case_apps_preserve_default
  152. })
  153. for app_dir in scan_info_dict[target]['standalone_apps']:
  154. apps.append({
  155. 'app_dir': app_dir,
  156. 'build_system': args.build_system,
  157. 'target': target,
  158. 'preserve': args.preserve
  159. })
  160. output_path = os.path.join(args.output_path, 'scan_{}_{}.json'.format(target.lower(), build_system))
  161. with open(output_path, 'w') as fw:
  162. fw.writelines([json.dumps(app) + '\n' for app in apps])
  163. if __name__ == '__main__':
  164. main()