ClickHouse/tests/ci/fast_test_check.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

255 lines
8.2 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
import atexit
from pathlib import Path
2022-11-15 17:02:42 +00:00
from typing import List, Tuple
2021-11-25 10:11:47 +00:00
from github import Github
2021-11-26 14:00:09 +00:00
from build_check import get_release_or_pr
from clickhouse_helper import (
ClickHouseHelper,
prepare_tests_results_for_clickhouse,
)
from commit_status_helper import (
RerunHelper,
get_commit,
post_commit_status,
update_mergeable_check,
2023-09-06 22:49:28 +00:00
format_description,
)
2023-09-22 11:16:46 +00:00
from docker_pull_helper import get_image_with_version, DockerImage
2023-09-20 16:02:59 +00:00
from env_helper import S3_BUILDS_BUCKET, TEMP_PATH, REPO_COPY
from get_robot_token import get_best_robot_token
from pr_info import FORCE_TESTS_LABEL, PRInfo
2023-09-06 11:02:17 +00:00
from report import TestResult, TestResults, read_test_results
from s3_helper import S3Helper
from stopwatch import Stopwatch
2021-12-03 08:33:16 +00:00
from tee_popen import TeePopen
from upload_result_helper import upload_results
from version_helper import get_version_from_repo
2021-10-22 09:26:39 +00:00
NAME = "Fast test"
2021-10-22 09:26:39 +00:00
# Will help to avoid errors like _csv.Error: field larger than field limit (131072)
csv.field_size_limit(sys.maxsize)
2023-09-22 11:16:46 +00:00
def get_fasttest_cmd(
workspace: Path,
output_path: Path,
repo_path: Path,
pr_number: int,
commit_sha: str,
image: DockerImage,
) -> str:
return (
2023-09-20 17:07:42 +00:00
f"docker run --cap-add=SYS_PTRACE --user={os.geteuid()}:{os.getegid()} "
2023-03-27 13:35:51 +00:00
"--network=host " # required to get access to IAM credentials
f"-e FASTTEST_WORKSPACE=/fasttest-workspace -e FASTTEST_OUTPUT=/test_output "
f"-e FASTTEST_SOURCE=/ClickHouse --cap-add=SYS_PTRACE "
f"-e FASTTEST_CMAKE_FLAGS='-DCOMPILER_CACHE=sccache' "
f"-e PULL_REQUEST_NUMBER={pr_number} -e COMMIT_SHA={commit_sha} "
f"-e COPY_CLICKHOUSE_BINARY_TO_OUTPUT=1 "
2023-03-27 13:35:51 +00:00
f"-e SCCACHE_BUCKET={S3_BUILDS_BUCKET} -e SCCACHE_S3_KEY_PREFIX=ccache/sccache "
2023-09-20 16:02:59 +00:00
"-e stage=clone_submodules "
f"--volume={workspace}:/fasttest-workspace --volume={repo_path}:/ClickHouse "
2023-03-27 13:35:51 +00:00
f"--volume={output_path}:/test_output {image}"
)
2021-10-22 09:26:39 +00:00
2023-09-22 11:16:46 +00:00
def process_results(result_directory: Path) -> Tuple[str, str, TestResults]:
test_results = [] # type: TestResults
2023-09-22 11:16:46 +00:00
# Just upload all files from result_directory.
# If task provides processed results, then it's responsible for content of
2023-09-22 11:16:46 +00:00
# result_directory
2021-10-22 09:26:39 +00:00
2021-12-12 12:09:44 +00:00
status = []
2023-09-22 11:16:46 +00:00
status_path = result_directory / "check_status.tsv"
2023-08-29 14:35:53 +00:00
if status_path.exists():
2021-12-12 12:09:44 +00:00
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:
2023-09-22 11:16:46 +00:00
logging.info("Files in result folder %s", os.listdir(result_directory))
return "error", "Invalid check_status.tsv", test_results
2021-10-22 09:26:39 +00:00
state, description = status[0][0], status[0][1]
try:
2023-09-22 11:16:46 +00:00
results_path = result_directory / "test_results.tsv"
test_results = read_test_results(results_path)
if len(test_results) == 0:
2023-09-22 11:16:46 +00:00
return "error", "Empty test_results.tsv", test_results
except Exception as e:
2023-09-22 11:16:46 +00:00
return ("error", f"Cannot parse test_results.tsv ({e})", test_results)
2021-10-22 09:26:39 +00:00
2023-09-22 11:16:46 +00:00
return state, description, test_results
2021-10-22 09:26:39 +00:00
def main():
2021-10-22 09:26:39 +00:00
logging.basicConfig(level=logging.INFO)
2021-11-19 14:47:04 +00:00
stopwatch = Stopwatch()
2023-08-29 14:35:53 +00:00
temp_path = Path(TEMP_PATH)
2021-10-22 09:26:39 +00:00
2023-08-29 14:35:53 +00:00
temp_path.mkdir(parents=True, exist_ok=True)
2021-10-22 09:26:39 +00:00
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(), per_page=100)
commit = get_commit(gh, pr_info.sha)
atexit.register(update_mergeable_check, gh, pr_info, NAME)
2021-10-22 09:26:39 +00:00
rerun_helper = RerunHelper(commit, NAME)
2021-12-01 14:23:51 +00:00
if rerun_helper.is_already_finished_by_status():
logging.info("Check is already finished according to github status, exiting")
status = rerun_helper.get_finished_status()
if status is not None and status.state != "success":
sys.exit(1)
2021-12-01 14:23:51 +00:00
sys.exit(0)
docker_image = get_image_with_version(temp_path, "clickhouse/fasttest")
2021-10-22 09:26:39 +00:00
2022-08-11 13:01:32 +00:00
s3_helper = S3Helper()
2021-10-22 09:26:39 +00:00
2023-08-29 14:35:53 +00:00
workspace = temp_path / "fasttest-workspace"
workspace.mkdir(parents=True, exist_ok=True)
2021-10-22 09:26:39 +00:00
2023-08-29 14:35:53 +00:00
output_path = temp_path / "fasttest-output"
output_path.mkdir(parents=True, exist_ok=True)
2021-10-22 09:26:39 +00:00
2023-09-20 16:02:59 +00:00
repo_path = Path(REPO_COPY)
2021-10-22 09:38:41 +00:00
run_cmd = get_fasttest_cmd(
workspace,
output_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)
2023-08-29 14:35:53 +00:00
logs_path = temp_path / "fasttest-logs"
logs_path.mkdir(parents=True, exist_ok=True)
2021-10-22 09:26:39 +00:00
2023-08-29 14:35:53 +00:00
run_log_path = logs_path / "run.log"
2023-09-06 11:51:43 +00:00
timeout_expired = False
# Do not increase this timeout
# https://pastila.nl/?146195b6/9bb99293535e3817a9ea82c3f0f7538d.link#5xtClOjkaPLEjSuZ92L2/g==
#
# SELECT toStartOfWeek(started_at) AS hour,
# avg(completed_at - started_at) AS avg_runtime from default.workflow_jobs
# WHERE
# conclusion = 'success' AND
# name = 'FastTest'
# GROUP BY hour
# ORDER BY hour
#
# Our fast tests finish in less than 10 minutes average, and very rarely it builds
# longer, but the next run will reuse the sccache
# SO DO NOT INCREASE IT
timeout = 40 * 60
2023-09-06 11:51:43 +00:00
with TeePopen(run_cmd, run_log_path, timeout=timeout) as process:
2023-09-06 11:59:51 +00:00
retcode = process.wait()
if process.timeout_exceeded:
2023-09-12 13:41:00 +00:00
logging.info("Timeout expired for command: %s", run_cmd)
2023-09-06 11:59:51 +00:00
timeout_expired = True
elif retcode == 0:
logging.info("Run successfully")
else:
logging.info("Run failed")
2021-10-22 09:26:39 +00:00
subprocess.check_call(f"sudo chown -R ubuntu:ubuntu {temp_path}", shell=True)
test_output_files = os.listdir(output_path)
2023-09-22 11:16:46 +00:00
additional_logs = [f for f in output_path.iterdir() if f.is_file()]
additional_logs.append(run_log_path)
2021-10-22 09:26:39 +00:00
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
2023-01-24 23:13:28 +00:00
test_results = [] # type: TestResults
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:
2023-09-22 11:16:46 +00:00
state, description, test_results = process_results(output_path)
2021-10-22 09:26:39 +00:00
2023-09-07 12:56:22 +00:00
if timeout_expired:
2023-09-12 18:06:00 +00:00
test_results.append(TestResult.create_check_timeout_expired(timeout))
2023-09-06 12:44:24 +00:00
state = "failure"
2023-09-12 18:06:00 +00:00
description = format_description(test_results[-1].name)
2023-09-04 21:16:25 +00:00
2021-11-19 14:47:04 +00:00
ch_helper = ClickHouseHelper()
2023-09-27 14:27:37 +00:00
s3_path_prefix = "/".join(
(
get_release_or_pr(pr_info, get_version_from_repo())[0],
pr_info.sha,
"fast_tests",
)
)
2023-08-29 14:35:53 +00:00
build_urls = s3_helper.upload_build_directory_to_s3(
output_path / "binaries",
s3_path_prefix,
keep_dirs_in_s3_path=False,
upload_symlinks=False,
)
2021-11-19 14:47:04 +00:00
report_url = upload_results(
s3_helper,
pr_info.number,
pr_info.sha,
test_results,
2023-09-22 11:16:46 +00:00
additional_logs,
NAME,
build_urls,
)
print(f"::notice ::Report url: {report_url}")
post_commit_status(commit, state, report_url, description, NAME, pr_info)
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,
)
2022-03-29 19:06:50 +00:00
ch_helper.insert_events_into(db="default", 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":
2023-05-08 17:23:46 +00:00
if state == "error":
print("The status is 'error', report failure disregard the labels")
sys.exit(1)
elif FORCE_TESTS_LABEL in pr_info.labels:
print(f"'{FORCE_TESTS_LABEL}' enabled, reporting success")
2021-12-01 09:50:36 +00:00
else:
sys.exit(1)
if __name__ == "__main__":
main()