ClickHouse/tests/ci/fast_test_check.py

215 lines
7.1 KiB
Python
Raw Normal View History

2021-10-22 09:26:39 +00:00
#!/usr/bin/env python3
import logging
import subprocess
import os
import csv
2021-11-25 10:11:47 +00:00
import sys
from github import Github
2021-11-26 14:00:09 +00:00
from env_helper import CACHES_PATH, TEMP_PATH
from pr_info import PRInfo
2021-10-22 09:26:39 +00:00
from s3_helper import S3Helper
from get_robot_token import get_best_robot_token
2021-11-12 11:39:00 +00:00
from upload_result_helper import upload_results
2021-11-12 12:13:13 +00:00
from docker_pull_helper import get_image_with_version
2021-11-12 12:36:25 +00:00
from commit_status_helper import post_commit_status
from clickhouse_helper import (
ClickHouseHelper,
mark_flaky_tests,
prepare_tests_results_for_clickhouse,
)
2021-11-19 14:47:04 +00:00
from stopwatch import Stopwatch
2021-12-01 14:23:51 +00:00
from rerun_helper import RerunHelper
2021-12-03 08:33:16 +00:00
from tee_popen import TeePopen
2021-12-03 09:28:17 +00:00
from ccache_utils import get_ccache_if_not_exists, upload_ccache
2021-10-22 09:26:39 +00:00
NAME = "Fast test (actions)"
2021-10-22 09:26:39 +00:00
def get_fasttest_cmd(
workspace, output_path, ccache_path, repo_path, pr_number, commit_sha, image
):
return (
f"docker run --cap-add=SYS_PTRACE "
f"-e FASTTEST_WORKSPACE=/fasttest-workspace -e FASTTEST_OUTPUT=/test_output "
f"-e FASTTEST_SOURCE=/ClickHouse --cap-add=SYS_PTRACE "
f"-e PULL_REQUEST_NUMBER={pr_number} -e COMMIT_SHA={commit_sha} "
f"-e COPY_CLICKHOUSE_BINARY_TO_OUTPUT=1 "
f"--volume={workspace}:/fasttest-workspace --volume={repo_path}:/ClickHouse "
f"--volume={output_path}:/test_output "
2021-10-22 09:26:39 +00:00
f"--volume={ccache_path}:/fasttest-workspace/ccache {image}"
)
2021-10-22 09:26:39 +00:00
def process_results(result_folder):
test_results = []
additional_files = []
# Just upload all files from result_folder.
# If task provides processed results, then it's responsible for content of
# result_folder
2021-10-22 09:26:39 +00:00
if os.path.exists(result_folder):
test_files = [
f
for f in os.listdir(result_folder)
if os.path.isfile(os.path.join(result_folder, f))
]
2021-10-22 09:26:39 +00:00
additional_files = [os.path.join(result_folder, f) for f in test_files]
2021-12-12 12:09:44 +00:00
status = []
2021-10-22 09:26:39 +00:00
status_path = os.path.join(result_folder, "check_status.tsv")
2021-12-12 12:09:44 +00:00
if os.path.exists(status_path):
logging.info("Found test_results.tsv")
with open(status_path, "r", encoding="utf-8") as status_file:
status = list(csv.reader(status_file, delimiter="\t"))
2021-10-22 09:26:39 +00:00
if len(status) != 1 or len(status[0]) != 2:
logging.info("Files in result folder %s", os.listdir(result_folder))
2021-10-22 09:26:39 +00:00
return "error", "Invalid check_status.tsv", test_results, additional_files
state, description = status[0][0], status[0][1]
results_path = os.path.join(result_folder, "test_results.tsv")
if os.path.exists(results_path):
with open(results_path, "r", encoding="utf-8") as results_file:
test_results = list(csv.reader(results_file, delimiter="\t"))
2021-10-22 09:26:39 +00:00
if len(test_results) == 0:
return "error", "Empty test_results.tsv", test_results, additional_files
2021-10-22 09:26:39 +00:00
return state, description, test_results, additional_files
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
2021-11-19 14:47:04 +00:00
stopwatch = Stopwatch()
2021-11-26 14:00:09 +00:00
temp_path = TEMP_PATH
caches_path = CACHES_PATH
2021-10-22 09:26:39 +00:00
if not os.path.exists(temp_path):
os.makedirs(temp_path)
2021-11-26 14:00:09 +00:00
pr_info = PRInfo()
2021-10-22 09:26:39 +00:00
gh = Github(get_best_robot_token())
2021-12-01 14:23:51 +00:00
rerun_helper = RerunHelper(gh, pr_info, NAME)
if rerun_helper.is_already_finished_by_status():
logging.info("Check is already finished according to github status, exiting")
sys.exit(0)
docker_image = get_image_with_version(temp_path, "clickhouse/fasttest")
2021-10-22 09:26:39 +00:00
s3_helper = S3Helper("https://s3.amazonaws.com")
2021-10-22 09:26:39 +00:00
workspace = os.path.join(temp_path, "fasttest-workspace")
if not os.path.exists(workspace):
os.makedirs(workspace)
output_path = os.path.join(temp_path, "fasttest-output")
if not os.path.exists(output_path):
os.makedirs(output_path)
cache_path = os.path.join(caches_path, "fasttest")
2021-12-03 09:28:17 +00:00
logging.info("Will try to fetch cache for our build")
get_ccache_if_not_exists(cache_path, s3_helper, pr_info.number, temp_path)
2021-10-22 09:26:39 +00:00
if not os.path.exists(cache_path):
2021-12-03 09:28:17 +00:00
logging.info("cache was not fetched, will create empty dir")
2021-10-22 09:26:39 +00:00
os.makedirs(cache_path)
2021-10-22 09:38:41 +00:00
repo_path = os.path.join(temp_path, "fasttest-repo")
if not os.path.exists(repo_path):
os.makedirs(repo_path)
run_cmd = get_fasttest_cmd(
workspace,
output_path,
cache_path,
repo_path,
pr_info.number,
pr_info.sha,
docker_image,
)
2021-10-22 09:26:39 +00:00
logging.info("Going to run fasttest with cmd %s", run_cmd)
logs_path = os.path.join(temp_path, "fasttest-logs")
if not os.path.exists(logs_path):
os.makedirs(logs_path)
run_log_path = os.path.join(logs_path, "runlog.log")
with TeePopen(run_cmd, run_log_path, timeout=40 * 60) as process:
2021-12-03 08:33:16 +00:00
retcode = process.wait()
2021-10-22 09:26:39 +00:00
if retcode == 0:
logging.info("Run successfully")
else:
logging.info("Run failed")
subprocess.check_call(f"sudo chown -R ubuntu:ubuntu {temp_path}", shell=True)
subprocess.check_call(f"sudo chown -R ubuntu:ubuntu {cache_path}", shell=True)
test_output_files = os.listdir(output_path)
additional_logs = []
for f in test_output_files:
additional_logs.append(os.path.join(output_path, f))
test_log_exists = (
"test_log.txt" in test_output_files or "test_result.txt" in test_output_files
)
test_result_exists = "test_results.tsv" in test_output_files
2021-10-22 09:26:39 +00:00
test_results = []
if "submodule_log.txt" not in test_output_files:
2021-10-22 09:26:39 +00:00
description = "Cannot clone repository"
state = "failure"
elif "cmake_log.txt" not in test_output_files:
2021-10-22 09:26:39 +00:00
description = "Cannot fetch submodules"
state = "failure"
elif "build_log.txt" not in test_output_files:
2021-10-22 09:26:39 +00:00
description = "Cannot finish cmake"
state = "failure"
elif "install_log.txt" not in test_output_files:
2021-10-22 09:26:39 +00:00
description = "Cannot build ClickHouse"
state = "failure"
elif not test_log_exists and not test_result_exists:
description = "Cannot install or start ClickHouse"
state = "failure"
else:
state, description, test_results, additional_logs = process_results(output_path)
2021-12-03 09:28:17 +00:00
logging.info("Will upload cache")
upload_ccache(cache_path, s3_helper, pr_info.number, temp_path)
2021-11-19 14:47:04 +00:00
ch_helper = ClickHouseHelper()
mark_flaky_tests(ch_helper, NAME, test_results)
report_url = upload_results(
s3_helper,
pr_info.number,
pr_info.sha,
test_results,
[run_log_path] + additional_logs,
NAME,
True,
)
print(f"::notice ::Report url: {report_url}")
2021-11-12 12:36:25 +00:00
post_commit_status(gh, pr_info.sha, NAME, description, state, report_url)
2021-11-19 14:47:04 +00:00
prepared_events = prepare_tests_results_for_clickhouse(
pr_info,
test_results,
state,
stopwatch.duration_seconds,
stopwatch.start_time_str,
report_url,
NAME,
)
2021-11-19 14:47:04 +00:00
ch_helper.insert_events_into(db="gh-data", table="checks", events=prepared_events)
2021-11-25 09:23:45 +00:00
# Refuse other checks to run if fast test failed
if state != "success":
if "force-tests" in pr_info.labels:
2021-12-01 09:50:36 +00:00
print("'force-tests' enabled, will report success")
else:
sys.exit(1)