ClickHouse/tests/ci/sqllogic_test.py

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

191 lines
5.2 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
from pathlib import Path
from typing import Tuple
2023-04-16 10:11:35 +00:00
from build_download_helper import download_all_deb_packages
from docker_images_helper import DockerImage, pull_image, get_docker_image
from env_helper import REPORT_PATH, TEMP_PATH, REPO_COPY
from report import (
ERROR,
2024-02-02 14:33:08 +00:00
FAIL,
OK,
SUCCESS,
JobReport,
StatusType,
TestResult,
2024-02-02 14:33:08 +00:00
TestResults,
read_test_results,
)
2023-04-16 10:11:35 +00:00
from stopwatch import Stopwatch
from tee_popen import TeePopen
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
)
def read_check_status(result_folder: Path) -> Tuple[StatusType, str]:
2023-09-22 11:16:46 +00:00
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:
return row[0], row[1] # type: ignore
2023-04-16 10:11:35 +00:00
return status_rows[-1][0], status_rows[-1][1] # type: ignore
2023-04-16 10:11:35 +00:00
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
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))
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
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
# Until it pass all tests, do not block CI, report SUCCESS
2023-04-16 10:11:35 +00:00
assert description is not None
2023-09-27 17:46:36 +00:00
# FIXME: force SUCCESS until all cases are fixed
status = SUCCESS
JobReport(
description=description,
test_results=test_results,
status=status,
start_time=stopwatch.start_time_str,
duration=stopwatch.duration_seconds,
additional_files=additional_logs,
).dump()
2023-09-22 11:16:46 +00:00
if __name__ == "__main__":
main()