#!/usr/bin/env python3 """在开发容器内启动独立 PostgreSQL,运行实库矩阵并关闭自己的实例。""" import argparse import os from pathlib import Path import subprocess import tempfile parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--bin", type=Path, required=True, help="PostgreSQL bin directory") parser.add_argument("--suite", action="append", choices=["build", "runtime"], help="Run only a selected test suite") parser.add_argument("--load", choices=["probe", "soak"], help="Run the two-instance PostgreSQL load fixture") parser.add_argument("--filter", help="Run PostgreSQL tests whose full names contain this text") args = parser.parse_args() if args.load and (args.suite or args.filter): parser.error("--load selects its own build fixture") server = [] server_limits = "" if args.load: cores = os.environ["DEVER_TEST_LOAD_CPUS"].split(",") if len(cores) != 4 or len(set(cores)) != 4 or not all(core.isdecimal() for core in cores): parser.error("DEVER_TEST_LOAD_CPUS requires four distinct CPU IDs") server = ["taskset", "-c", ",".join(cores)] # 两实例各有 32 租户池和一个控制池,每池三个连接。 server_limits = " -c max_connections=200" print(f"PostgreSQL CPU affinity: server={','.join(cores)}; {server_limits.strip()}", flush=True) binary = args.bin.resolve() root = Path(__file__).resolve().parents[1] (root / "target").mkdir(exist_ok=True) with tempfile.TemporaryDirectory(prefix="pg-test-", dir=root / "target") as temporary: work = Path(temporary) data = work / "data" socket = work / "socket" socket.mkdir() subprocess.run([binary / "initdb", "-D", data, "--no-locale", "-E", "UTF8", "--auth=trust", "--username=dever_test"], check=True, stdout=subprocess.DEVNULL) started = False try: subprocess.run(server + [binary / "pg_ctl", "-D", data, "-l", work / "server.log", "-o", f"-k {socket} -c listen_addresses=''{server_limits}", "-w", "start"], check=True) started = True pid = (data / "postmaster.pid").read_text().splitlines()[0] for suite in ["build"] if args.load else args.suite or ["build", "runtime"]: command = ["cargo", "+1.95.0", "test", "--locked", "--offline", "--test", suite] if args.load: tests = [f"api::tenant::large_project_pg_replica_{args.load}"] else: listed = subprocess.run(command + ["postgres_", "--", "--ignored", "--list"], cwd=root / "rust", check=True, capture_output=True, text=True) tests = [line.removesuffix(": test") for line in listed.stdout.splitlines() if line.endswith(": test") and (args.filter is None or args.filter in line)] if not tests: raise RuntimeError(f"No PostgreSQL tests found in {suite}") for index, test in enumerate(tests): name = f"dever_{suite}_{index}" subprocess.run([binary / "createdb", "-h", socket, "-U", "dever_test", name], check=True) env = dict(os.environ, DEVER_TEST_POSTGRES=f"host={socket} user=dever_test dbname={name}", DEVER_TEST_POSTGRES_PID=pid) subprocess.run(command + [test, "--", "--ignored", "--exact", "--nocapture"], cwd=root / "rust", env=env, check=True) except BaseException: log = work / "server.log" if log.exists(): print(log.read_text()[-8000:]) raise finally: if started: subprocess.run([binary / "pg_ctl", "-D", data, "-m", "fast", "-w", "stop"], check=True)