ClickHouse/tests/ci/fast_test_check.py

196 lines
6.6 KiB
Python
Raw Normal View History

2021-10-22 09:26:39 +00:00
#!/usr/bin/env python3
2023-10-31 11:32:08 +00:00
import argparse
2024-01-30 17:29:55 +00:00
import csv
2021-10-22 09:26:39 +00:00
import logging
import os
2024-01-30 17:29:55 +00:00
import subprocess
2021-11-25 10:11:47 +00:00
import sys
from pathlib import Path
from typing import Tuple
from docker_images_helper import DockerImage, get_docker_image, pull_image
2024-01-30 17:29:55 +00:00
from env_helper import REPO_COPY, S3_BUILDS_BUCKET, TEMP_PATH
from pr_info import FORCE_TESTS_LABEL, PRInfo
from report import JobReport, TestResult, TestResults, read_test_results
from stopwatch import Stopwatch
2021-12-03 08:33:16 +00:00
from tee_popen import TeePopen
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():
logging.info("Found %s", status_path.name)
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
2023-10-31 11:43:32 +00:00
2023-10-31 11:32:08 +00:00
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
description="FastTest script",
)
parser.add_argument(
"--timeout",
type=int,
# Fast tests in most cases done within 10 min and 40 min timout should be sufficient,
# though due to cold cache build time can be much longer
# https://pastila.nl/?146195b6/9bb99293535e3817a9ea82c3f0f7538d.link#5xtClOjkaPLEjSuZ92L2/g==
default=40,
help="Timeout in minutes",
)
2023-10-31 11:47:21 +00:00
args = parser.parse_args()
args.timeout = args.timeout * 60
return args
2021-10-22 09:26:39 +00:00
2023-10-31 11:43:32 +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-10-31 11:32:08 +00:00
args = parse_args()
2023-08-29 14:35:53 +00:00
temp_path = Path(TEMP_PATH)
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
docker_image = pull_image(get_docker_image("clickhouse/fasttest"))
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
2023-10-31 11:01:49 +00:00
2023-10-31 11:47:21 +00:00
with TeePopen(run_cmd, run_log_path, timeout=args.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-10-31 11:47:21 +00:00
test_results.append(TestResult.create_check_timeout_expired(args.timeout))
2023-09-06 12:44:24 +00:00
state = "failure"
description = test_results[-1].name
JobReport(
description=description,
test_results=test_results,
status=state,
start_time=stopwatch.start_time_str,
duration=stopwatch.duration_seconds,
additional_files=additional_logs,
build_dir_for_upload=str(output_path / "binaries"),
).dump()
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()