CIScanTests.py 6.5 KB

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