ClickHouse/tests/ci/sqllogic_test.py

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

216 lines
6.0 KiB
Python
Raw Normal View History

2023-04-16 10:11:35 +00:00
#!/usr/bin/env python3
import argparse
import csv
import logging
import os
import subprocess
import sys
from pathlib import Path
2023-09-22 11:16:46 +00:00
from typing import List, Tuple
2023-04-16 10:11:35 +00:00
from github import Github
from build_download_helper import download_all_deb_packages
from commit_status_helper import (
RerunHelper,
get_commit,
override_status,
post_commit_status,
)
from docker_images_helper import DockerImage, pull_image, get_docker_image
from env_helper import REPORT_PATH, TEMP_PATH, REPO_COPY
2023-09-22 11:16:46 +00:00
from get_robot_token import get_best_robot_token
2023-09-27 17:46:36 +00:00
from pr_info import PRInfo
2023-09-22 11:16:46 +00:00
from report import OK, FAIL, ERROR, SUCCESS, TestResults, TestResult, read_test_results
from s3_helper import S3Helper
2023-04-16 10:11:35 +00:00
from stopwatch import Stopwatch
from tee_popen import TeePopen
2023-09-22 11:16:46 +00:00
from upload_result_helper import upload_results
2023-04-16 10:11:35 +00:00
NO_CHANGES_MSG = "Nothing to run"
IMAGE_NAME = "clickhouse/sqllogic-test"
def get_run_command(
2023-09-22 11:16:46 +00:00
builds_path: Path,
repo_tests_path: Path,
result_path: Path,
server_log_path: Path,
image: DockerImage,
) -> str:
2023-04-16 10:11:35 +00:00
return (
f"docker run "
f"--volume={builds_path}:/package_folder "
f"--volume={repo_tests_path}:/clickhouse-tests "
f"--volume={result_path}:/test_output "
f"--volume={server_log_path}:/var/log/clickhouse-server "
2023-09-22 11:16:46 +00:00
f"--cap-add=SYS_PTRACE {image}"
2023-04-16 10:11:35 +00:00
)
2023-09-22 11:16:46 +00:00
def read_check_status(result_folder: Path) -> Tuple[str, str]:
status_path = result_folder / "check_status.tsv"
if not status_path.exists():
return ERROR, "Not found check_status.tsv"
2023-04-16 10:11:35 +00:00
logging.info("Found check_status.tsv")
with open(status_path, "r", encoding="utf-8") as status_file:
status_rows = list(csv.reader(status_file, delimiter="\t"))
for row in status_rows:
if len(row) != 2:
2023-09-22 11:16:46 +00:00
return ERROR, "Invalid check_status.tsv"
if row[0] != SUCCESS:
2023-04-16 10:11:35 +00:00
return row[0], row[1]
return status_rows[-1][0], status_rows[-1][1]
2023-09-22 11:16:46 +00:00
def parse_args() -> argparse.Namespace:
2023-04-16 10:11:35 +00:00
parser = argparse.ArgumentParser()
parser.add_argument(
"--check-name",
required=False,
default="",
)
parser.add_argument(
"--kill-timeout",
required=False,
default=0,
)
2023-04-16 10:11:35 +00:00
return parser.parse_args()
2023-09-22 11:16:46 +00:00
def main():
2023-04-16 10:11:35 +00:00
logging.basicConfig(level=logging.INFO)
stopwatch = Stopwatch()
2023-09-22 11:16:46 +00:00
temp_path = Path(TEMP_PATH)
reports_path = Path(REPORT_PATH)
2023-09-22 11:16:46 +00:00
temp_path.mkdir(parents=True, exist_ok=True)
repo_path = Path(REPO_COPY)
2023-04-16 10:11:35 +00:00
args = parse_args()
check_name = args.check_name
check_name = args.check_name or os.getenv("CHECK_NAME")
assert (
check_name
), "Check name must be provided as an input arg or in CHECK_NAME env"
kill_timeout = args.kill_timeout or int(os.getenv("KILL_TIMEOUT", "0"))
assert (
kill_timeout > 0
), "kill timeout must be provided as an input arg or in KILL_TIMEOUT env"
2023-04-16 10:11:35 +00:00
pr_info = PRInfo()
gh = Github(get_best_robot_token(), per_page=100)
commit = get_commit(gh, pr_info.sha)
2023-04-16 10:11:35 +00:00
rerun_helper = RerunHelper(commit, check_name)
2023-04-16 10:11:35 +00:00
if rerun_helper.is_already_finished_by_status():
logging.info("Check is already finished according to github status, exiting")
sys.exit(0)
docker_image = pull_image(get_docker_image(IMAGE_NAME))
2023-04-16 10:11:35 +00:00
2023-09-22 11:16:46 +00:00
repo_tests_path = repo_path / "tests"
2023-04-16 10:11:35 +00:00
2023-09-22 11:16:46 +00:00
packages_path = temp_path / "packages"
packages_path.mkdir(parents=True, exist_ok=True)
2023-04-16 10:11:35 +00:00
download_all_deb_packages(check_name, reports_path, packages_path)
2023-09-22 11:16:46 +00:00
server_log_path = temp_path / "server_log"
server_log_path.mkdir(parents=True, exist_ok=True)
2023-04-16 10:11:35 +00:00
2023-09-22 11:16:46 +00:00
result_path = temp_path / "result_path"
result_path.mkdir(parents=True, exist_ok=True)
2023-04-16 10:11:35 +00:00
2023-09-22 11:16:46 +00:00
run_log_path = result_path / "runlog.log"
2023-04-16 10:11:35 +00:00
run_command = get_run_command( # run script inside docker
packages_path,
repo_tests_path,
result_path,
server_log_path,
docker_image,
)
logging.info("Going to run func tests: %s", run_command)
with TeePopen(run_command, run_log_path, timeout=kill_timeout) as process:
2023-04-16 10:11:35 +00:00
retcode = process.wait()
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)
logging.info("Files in result folder %s", os.listdir(result_path))
s3_helper = S3Helper()
status = None
description = None
2023-09-22 11:16:46 +00:00
additional_logs = [run_log_path]
if result_path.exists():
additional_logs.extend(p for p in result_path.iterdir() if p.is_file())
2023-04-16 10:11:35 +00:00
2023-09-22 11:16:46 +00:00
if server_log_path.exists():
additional_logs.extend(p for p in server_log_path.iterdir() if p.is_file())
2023-04-16 10:11:35 +00:00
status, description = read_check_status(result_path)
test_results = [] # type: TestResults
test_results_path = Path(result_path) / "test_results.tsv"
if test_results_path.exists():
logging.info("Found test_results.tsv")
test_results = read_test_results(test_results_path)
2023-09-22 11:16:46 +00:00
if len(test_results) > 3000:
2023-04-16 10:11:35 +00:00
test_results = test_results[:1000]
if len(test_results) == 0:
2023-09-22 11:16:46 +00:00
status, description = ERROR, "Empty test_results.tsv"
2023-04-16 10:11:35 +00:00
assert status is not None
status = override_status(status, check_name)
2023-09-22 11:16:46 +00:00
test_results.append(
TestResult(
"All tests",
OK if status == SUCCESS else FAIL,
stopwatch.duration_seconds,
)
)
2023-04-16 10:11:35 +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,
2023-04-16 10:11:35 +00:00
check_name,
)
print(
f"::notice:: {check_name}"
f", Result: '{status}'"
f", Description: '{description}'"
f", Report url: '{report_url}'"
)
# Until it pass all tests, do not block CI, report "success"
assert description is not None
2023-09-27 17:46:36 +00:00
# FIXME: force SUCCESS until all cases are fixed
status = SUCCESS
post_commit_status(
commit, status, report_url, description, check_name, pr_info, dump_to_file=True
)
2023-09-22 11:16:46 +00:00
if __name__ == "__main__":
main()