ClickHouse/tests/ci/sqltest.py

158 lines
4.5 KiB
Python
Raw Normal View History

2023-07-19 01:42:32 +00:00
#!/usr/bin/env python3
import logging
import subprocess
import os
import sys
2023-08-29 14:35:53 +00:00
from pathlib import Path
from typing import Dict
2023-07-19 01:42:32 +00:00
from github import Github
from build_download_helper import get_build_name_for_check, read_build_urls
from clickhouse_helper import ClickHouseHelper, prepare_tests_results_for_clickhouse
from commit_status_helper import (
RerunHelper,
get_commit,
post_commit_status,
)
from docker_pull_helper import get_image_with_version
from env_helper import (
GITHUB_RUN_URL,
REPORTS_PATH,
TEMP_PATH,
)
from get_robot_token import get_best_robot_token
from pr_info import PRInfo
from report import TestResult
from s3_helper import S3Helper
from stopwatch import Stopwatch
IMAGE_NAME = "clickhouse/sqltest"
def get_run_command(pr_number, sha, download_url, workspace_path, image):
return (
f"docker run "
# For sysctl
"--privileged "
"--network=host "
f"--volume={workspace_path}:/workspace "
"--cap-add syslog --cap-add sys_admin --cap-add=SYS_PTRACE "
f'-e PR_TO_TEST={pr_number} -e SHA_TO_TEST={sha} -e BINARY_URL_TO_DOWNLOAD="{download_url}" '
f"{image}"
)
def main():
logging.basicConfig(level=logging.INFO)
stopwatch = Stopwatch()
2023-08-29 14:35:53 +00:00
temp_path = Path(TEMP_PATH)
reports_path = Path(REPORTS_PATH)
2023-07-19 01:42:32 +00:00
check_name = sys.argv[1]
2023-08-29 14:35:53 +00:00
temp_path.mkdir(parents=True, exist_ok=True)
2023-07-19 01:42:32 +00:00
pr_info = PRInfo()
gh = Github(get_best_robot_token(), per_page=100)
commit = get_commit(gh, pr_info.sha)
rerun_helper = RerunHelper(commit, check_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(reports_path, IMAGE_NAME)
build_name = get_build_name_for_check(check_name)
print(build_name)
urls = read_build_urls(build_name, reports_path)
if not urls:
raise Exception("No build URLs found")
for url in urls:
if url.endswith("/clickhouse"):
build_url = url
break
else:
raise Exception("Cannot find the clickhouse binary among build results")
logging.info("Got build url %s", build_url)
2023-08-29 14:35:53 +00:00
workspace_path = temp_path / "workspace"
2023-07-19 01:42:32 +00:00
if not os.path.exists(workspace_path):
os.makedirs(workspace_path)
run_command = get_run_command(
pr_info.number, pr_info.sha, build_url, workspace_path, docker_image
)
logging.info("Going to run %s", run_command)
2023-08-29 14:35:53 +00:00
run_log_path = temp_path / "run.log"
2023-07-19 01:42:32 +00:00
with open(run_log_path, "w", encoding="utf-8") as log:
with subprocess.Popen(
run_command, shell=True, stderr=log, stdout=log
) as process:
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)
check_name_lower = (
check_name.lower().replace("(", "").replace(")", "").replace(" ", "")
)
s3_prefix = f"{pr_info.number}/{pr_info.sha}/sqltest_{check_name_lower}/"
paths = {
"run.log": run_log_path,
2023-08-29 14:35:53 +00:00
"server.log.zst": workspace_path / "server.log.zst",
"server.err.log.zst": workspace_path / "server.err.log.zst",
"report.html": workspace_path / "report.html",
"test.log": workspace_path / "test.log",
2023-07-19 01:42:32 +00:00
}
2023-08-29 14:35:53 +00:00
path_urls = {} # type: Dict[str, str]
2023-07-19 01:42:32 +00:00
s3_helper = S3Helper()
for f in paths:
try:
2023-08-29 14:35:53 +00:00
path_urls[f] = s3_helper.upload_test_report_to_s3(paths[f], s3_prefix + f)
2023-07-19 01:42:32 +00:00
except Exception as ex:
logging.info("Exception uploading file %s text %s", f, ex)
2023-08-29 14:35:53 +00:00
path_urls[f] = ""
2023-07-19 01:42:32 +00:00
report_url = GITHUB_RUN_URL
2023-08-29 14:35:53 +00:00
if path_urls["report.html"]:
report_url = path_urls["report.html"]
2023-07-19 01:42:32 +00:00
status = "success"
description = "See the report"
test_result = TestResult(description, "OK")
ch_helper = ClickHouseHelper()
prepared_events = prepare_tests_results_for_clickhouse(
pr_info,
[test_result],
status,
stopwatch.duration_seconds,
stopwatch.start_time_str,
report_url,
check_name,
)
ch_helper.insert_events_into(db="default", table="checks", events=prepared_events)
logging.info("Result: '%s', '%s', '%s'", status, description, report_url)
print(f"::notice ::Report url: {report_url}")
post_commit_status(commit, status, report_url, description, check_name, pr_info)
if __name__ == "__main__":
main()