CIAssignExampleTest.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. EXAMPLE_BUILD_JOB_NAMES = ["build_examples_cmake_esp32", "build_examples_cmake_esp32s2"]
  25. IDF_PATH_FROM_ENV = os.getenv("IDF_PATH")
  26. if IDF_PATH_FROM_ENV:
  27. ARTIFACT_INDEX_FILE = os.path.join(IDF_PATH_FROM_ENV,
  28. "build_examples", "artifact_index.json")
  29. else:
  30. ARTIFACT_INDEX_FILE = "artifact_index.json"
  31. class ExampleGroup(CIAssignTest.Group):
  32. SORT_KEYS = CI_JOB_MATCH_KEYS = ["env_tag", "chip"]
  33. class CIExampleAssignTest(CIAssignTest.AssignTest):
  34. CI_TEST_JOB_PATTERN = re.compile(r"^example_test_.+")
  35. def create_artifact_index_file(project_id=None, pipeline_id=None):
  36. if project_id is None:
  37. project_id = os.getenv("CI_PROJECT_ID")
  38. if pipeline_id is None:
  39. pipeline_id = os.getenv("CI_PIPELINE_ID")
  40. gitlab_inst = gitlab_api.Gitlab(project_id)
  41. artifact_index_list = []
  42. def format_build_log_path():
  43. return "build_examples/list_job_{}.json".format(job_info["parallel_num"])
  44. for build_job_name in EXAMPLE_BUILD_JOB_NAMES:
  45. job_info_list = gitlab_inst.find_job_id(build_job_name, pipeline_id=pipeline_id)
  46. for job_info in job_info_list:
  47. raw_data = gitlab_inst.download_artifact(job_info["id"], [format_build_log_path()])[0]
  48. build_info_list = [json.loads(line) for line in raw_data.decode().splitlines()]
  49. for build_info in build_info_list:
  50. build_info["ci_job_id"] = job_info["id"]
  51. artifact_index_list.append(build_info)
  52. try:
  53. os.makedirs(os.path.dirname(ARTIFACT_INDEX_FILE))
  54. except OSError:
  55. # already created
  56. pass
  57. with open(ARTIFACT_INDEX_FILE, "w") as f:
  58. json.dump(artifact_index_list, f)
  59. if __name__ == '__main__':
  60. parser = argparse.ArgumentParser()
  61. parser.add_argument("test_case",
  62. help="test case folder or file")
  63. parser.add_argument("ci_config_file",
  64. help="gitlab ci config file")
  65. parser.add_argument("output_path",
  66. help="output path of config files")
  67. parser.add_argument("--pipeline_id", "-p", type=int, default=None,
  68. help="pipeline_id")
  69. args = parser.parse_args()
  70. assign_test = CIExampleAssignTest(args.test_case, args.ci_config_file, case_group=ExampleGroup)
  71. assign_test.assign_cases()
  72. assign_test.output_configs(args.output_path)
  73. create_artifact_index_file()