CIScanTests.py 6.5 KB

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