2017-10-10 02:44:55 +00:00
|
|
|
# Copyright 2015-2017 Espressif Systems (Shanghai) PTE LTD
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http:#www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
"""
|
|
|
|
Command line tool to assign example tests to CI test jobs.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# TODO: Need to handle running examples on different chips
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import argparse
|
2019-11-27 03:14:29 +00:00
|
|
|
import json
|
2017-10-10 02:44:55 +00:00
|
|
|
|
2019-11-27 03:21:33 +00:00
|
|
|
import gitlab_api
|
|
|
|
from tiny_test_fw.Utility import CIAssignTest
|
2018-12-04 12:46:48 +00:00
|
|
|
|
2019-11-27 03:14:29 +00:00
|
|
|
IDF_PATH_FROM_ENV = os.getenv("IDF_PATH")
|
|
|
|
|
|
|
|
|
2019-11-27 03:21:33 +00:00
|
|
|
class ExampleGroup(CIAssignTest.Group):
|
2020-04-03 08:16:40 +00:00
|
|
|
SORT_KEYS = CI_JOB_MATCH_KEYS = ["env_tag", "target"]
|
2020-01-27 11:12:49 +00:00
|
|
|
BUILD_LOCAL_DIR = "build_examples"
|
|
|
|
BUILD_JOB_NAMES = ["build_examples_cmake_esp32", "build_examples_cmake_esp32s2"]
|
|
|
|
|
|
|
|
|
|
|
|
class TestAppsGroup(ExampleGroup):
|
|
|
|
BUILD_LOCAL_DIR = "build_test_apps"
|
|
|
|
BUILD_JOB_NAMES = ["build_test_apps_esp32", "build_test_apps_esp32s2"]
|
2017-10-10 02:44:55 +00:00
|
|
|
|
|
|
|
|
2019-11-27 03:21:33 +00:00
|
|
|
class CIExampleAssignTest(CIAssignTest.AssignTest):
|
2017-10-10 02:44:55 +00:00
|
|
|
CI_TEST_JOB_PATTERN = re.compile(r"^example_test_.+")
|
|
|
|
|
|
|
|
|
2020-01-27 11:12:49 +00:00
|
|
|
def get_artifact_index_file(case_group=ExampleGroup):
|
|
|
|
if IDF_PATH_FROM_ENV:
|
|
|
|
artifact_index_file = os.path.join(IDF_PATH_FROM_ENV,
|
|
|
|
case_group.BUILD_LOCAL_DIR, "artifact_index.json")
|
|
|
|
else:
|
|
|
|
artifact_index_file = "artifact_index.json"
|
|
|
|
return artifact_index_file
|
|
|
|
|
|
|
|
|
|
|
|
def create_artifact_index_file(project_id=None, pipeline_id=None, case_group=ExampleGroup):
|
2019-11-27 03:14:29 +00:00
|
|
|
if project_id is None:
|
|
|
|
project_id = os.getenv("CI_PROJECT_ID")
|
|
|
|
if pipeline_id is None:
|
|
|
|
pipeline_id = os.getenv("CI_PIPELINE_ID")
|
|
|
|
gitlab_inst = gitlab_api.Gitlab(project_id)
|
|
|
|
artifact_index_list = []
|
2019-11-27 03:21:33 +00:00
|
|
|
|
2019-11-27 03:14:29 +00:00
|
|
|
def format_build_log_path():
|
2020-04-03 08:16:40 +00:00
|
|
|
parallel = job_info["parallel_num"] # Could be None if "parallel_num" not defined for the job
|
2020-01-27 11:12:49 +00:00
|
|
|
return "{}/list_job_{}.json".format(case_group.BUILD_LOCAL_DIR, parallel or 1)
|
2019-11-27 03:21:33 +00:00
|
|
|
|
2020-01-27 11:12:49 +00:00
|
|
|
for build_job_name in case_group.BUILD_JOB_NAMES:
|
2020-04-24 02:39:44 +00:00
|
|
|
job_info_list = gitlab_inst.find_job_id(build_job_name, pipeline_id=pipeline_id)
|
2019-11-27 03:14:29 +00:00
|
|
|
for job_info in job_info_list:
|
|
|
|
raw_data = gitlab_inst.download_artifact(job_info["id"], [format_build_log_path()])[0]
|
|
|
|
build_info_list = [json.loads(line) for line in raw_data.splitlines()]
|
|
|
|
for build_info in build_info_list:
|
|
|
|
build_info["ci_job_id"] = job_info["id"]
|
|
|
|
artifact_index_list.append(build_info)
|
2020-01-27 11:12:49 +00:00
|
|
|
artifact_index_file = get_artifact_index_file(case_group=case_group)
|
2019-11-27 03:14:29 +00:00
|
|
|
try:
|
2020-01-27 11:12:49 +00:00
|
|
|
os.makedirs(os.path.dirname(artifact_index_file))
|
2019-11-27 03:14:29 +00:00
|
|
|
except OSError:
|
|
|
|
# already created
|
2019-11-27 03:21:33 +00:00
|
|
|
pass
|
|
|
|
|
2020-01-27 11:12:49 +00:00
|
|
|
with open(artifact_index_file, "w") as f:
|
2019-11-27 03:14:29 +00:00
|
|
|
json.dump(artifact_index_list, f)
|
|
|
|
|
2019-11-27 03:21:33 +00:00
|
|
|
|
2017-10-10 02:44:55 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument("test_case",
|
|
|
|
help="test case folder or file")
|
|
|
|
parser.add_argument("ci_config_file",
|
|
|
|
help="gitlab ci config file")
|
|
|
|
parser.add_argument("output_path",
|
|
|
|
help="output path of config files")
|
2019-11-27 03:14:29 +00:00
|
|
|
parser.add_argument("--pipeline_id", "-p", type=int, default=None,
|
|
|
|
help="pipeline_id")
|
2019-10-20 18:55:11 +00:00
|
|
|
parser.add_argument("--job-prefix",
|
|
|
|
help="prefix of the test job name in CI yml file")
|
|
|
|
parser.add_argument("--test-case-file-pattern",
|
|
|
|
help="file name pattern used to find Python test case files")
|
2020-01-27 11:12:49 +00:00
|
|
|
parser.add_argument('--custom-group',
|
|
|
|
help='select custom-group for the test cases, if other than ExampleTest',
|
2020-04-03 08:16:40 +00:00
|
|
|
choices=['example', 'test-apps'], default='example')
|
2020-01-27 11:12:49 +00:00
|
|
|
|
2017-10-10 02:44:55 +00:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
2019-10-20 18:55:11 +00:00
|
|
|
if args.job_prefix:
|
|
|
|
CIExampleAssignTest.CI_TEST_JOB_PATTERN = re.compile(r"^{}.+".format(args.job_prefix))
|
|
|
|
|
2020-01-27 11:12:49 +00:00
|
|
|
case_group = ExampleGroup if args.custom_group == 'example' else TestAppsGroup
|
2019-10-20 18:55:11 +00:00
|
|
|
|
2020-01-27 11:12:49 +00:00
|
|
|
assign_test = CIExampleAssignTest(args.test_case, args.ci_config_file, case_group=case_group)
|
2017-10-10 02:44:55 +00:00
|
|
|
assign_test.assign_cases()
|
|
|
|
assign_test.output_configs(args.output_path)
|
2020-01-27 11:12:49 +00:00
|
|
|
create_artifact_index_file(case_group=case_group)
|