CIAssignExampleTest.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. # Copyright 2015-2017 Espressif Systems (Shanghai) PTE LTD
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http:#www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """
  15. Command line tool to assign example tests to CI test jobs.
  16. """
  17. # TODO: Need to handle running examples on different chips
  18. import os
  19. import re
  20. import argparse
  21. import json
  22. import gitlab_api
  23. from tiny_test_fw.Utility import CIAssignTest
  24. IDF_PATH_FROM_ENV = os.getenv("IDF_PATH")
  25. class ExampleGroup(CIAssignTest.Group):
  26. SORT_KEYS = CI_JOB_MATCH_KEYS = ["env_tag", "chip"]
  27. BUILD_LOCAL_DIR = "build_examples"
  28. BUILD_JOB_NAMES = ["build_examples_cmake_esp32", "build_examples_cmake_esp32s2"]
  29. class TestAppsGroup(ExampleGroup):
  30. BUILD_LOCAL_DIR = "build_test_apps"
  31. BUILD_JOB_NAMES = ["build_test_apps_esp32", "build_test_apps_esp32s2"]
  32. class CIExampleAssignTest(CIAssignTest.AssignTest):
  33. CI_TEST_JOB_PATTERN = re.compile(r"^example_test_.+")
  34. def get_artifact_index_file(case_group=ExampleGroup):
  35. if IDF_PATH_FROM_ENV:
  36. artifact_index_file = os.path.join(IDF_PATH_FROM_ENV,
  37. case_group.BUILD_LOCAL_DIR, "artifact_index.json")
  38. else:
  39. artifact_index_file = "artifact_index.json"
  40. return artifact_index_file
  41. def create_artifact_index_file(project_id=None, pipeline_id=None, case_group=ExampleGroup):
  42. if project_id is None:
  43. project_id = os.getenv("CI_PROJECT_ID")
  44. if pipeline_id is None:
  45. pipeline_id = os.getenv("CI_PIPELINE_ID")
  46. gitlab_inst = gitlab_api.Gitlab(project_id)
  47. artifact_index_list = []
  48. def format_build_log_path():
  49. parallel = job_info["parallel_num"] # Could be None if "parallel_num" not defined for the job
  50. return "{}/list_job_{}.json".format(case_group.BUILD_LOCAL_DIR, parallel or 1)
  51. for build_job_name in case_group.BUILD_JOB_NAMES:
  52. job_info_list = gitlab_inst.find_job_id(build_job_name, pipeline_id=pipeline_id)
  53. for job_info in job_info_list:
  54. raw_data = gitlab_inst.download_artifact(job_info["id"], [format_build_log_path()])[0]
  55. build_info_list = [json.loads(line) for line in raw_data.splitlines()]
  56. for build_info in build_info_list:
  57. build_info["ci_job_id"] = job_info["id"]
  58. artifact_index_list.append(build_info)
  59. artifact_index_file = get_artifact_index_file(case_group=case_group)
  60. try:
  61. os.makedirs(os.path.dirname(artifact_index_file))
  62. except OSError:
  63. # already created
  64. pass
  65. with open(artifact_index_file, "w") as f:
  66. json.dump(artifact_index_list, f)
  67. if __name__ == '__main__':
  68. parser = argparse.ArgumentParser()
  69. parser.add_argument("test_case",
  70. help="test case folder or file")
  71. parser.add_argument("ci_config_file",
  72. help="gitlab ci config file")
  73. parser.add_argument("output_path",
  74. help="output path of config files")
  75. parser.add_argument("--pipeline_id", "-p", type=int, default=None,
  76. help="pipeline_id")
  77. parser.add_argument("--job-prefix",
  78. help="prefix of the test job name in CI yml file")
  79. parser.add_argument("--test-case-file-pattern",
  80. help="file name pattern used to find Python test case files")
  81. parser.add_argument('--custom-group',
  82. help='select custom-group for the test cases, if other than ExampleTest',
  83. choices=['example','test-apps'], default='example')
  84. args = parser.parse_args()
  85. if args.job_prefix:
  86. CIExampleAssignTest.CI_TEST_JOB_PATTERN = re.compile(r"^{}.+".format(args.job_prefix))
  87. case_group = ExampleGroup if args.custom_group == 'example' else TestAppsGroup
  88. assign_test = CIExampleAssignTest(args.test_case, args.ci_config_file, case_group=case_group)
  89. assign_test.assign_cases()
  90. assign_test.output_configs(args.output_path)
  91. create_artifact_index_file(case_group=case_group)