Files
rust-discovery/crates/oidc-auth/tests/run_postgres.py
BuTzZ 6c3203d725
Rust development slice / linux (push) Failing after 1m10s
feat: snapshot Linux-first RMM MVP and pause handover
2026-11-12 22:06:25 +01:00

73 lines
3.1 KiB
Python

#!/usr/bin/env python3
"""Opt-in disposable PostgreSQL test. No existing container/DB or host trust changes."""
import os
from pathlib import Path
import secrets
import shutil
import subprocess
import sys
import time
NAME = "rust-discovery-oidc-test"
ROOT = Path(__file__).resolve().parents[1]
def quiet(args, **kwargs):
return subprocess.run(args, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, **kwargs)
def main():
if quiet(["docker", "container", "inspect", NAME]).returncode == 0:
raise RuntimeError("dedicated test container name already exists; refusing to touch it")
env = os.environ.copy()
env["POSTGRES_PASSWORD"] = secrets.token_hex(32)
created = False
try:
result = quiet([
"docker", "run", "--detach", "--name", NAME,
"--label", "rmm.oidc.disposable-test=true",
"--publish", "127.0.0.1::5432",
"--env", "POSTGRES_PASSWORD", "--env", "POSTGRES_DB=oidc_test",
"postgres:17.6-bookworm",
], env=env, timeout=30)
if result.returncode != 0:
raise RuntimeError("could not create isolated PostgreSQL test container")
created = True
ready = False
for _ in range(80):
if quiet(["docker", "exec", NAME, "pg_isready", "-q", "-h", "127.0.0.1", "-U", "postgres", "-d", "oidc_test"], timeout=5).returncode == 0:
ready = True
break
time.sleep(0.25)
if not ready:
raise RuntimeError("isolated PostgreSQL did not become ready")
port_result = subprocess.run(["docker", "port", NAME, "5432/tcp"], capture_output=True, text=True, timeout=5)
mapping = port_result.stdout.strip()
if port_result.returncode != 0 or not mapping.startswith("127.0.0.1:") or not mapping.split(":")[-1].isdigit():
raise RuntimeError("test port is not a single IPv4 loopback binding")
port = int(mapping.rsplit(":", 1)[1])
env["RMM_OIDC_TEST_DATABASE_URL"] = f"postgres://postgres:{env['POSTGRES_PASSWORD']}@127.0.0.1:{port}/oidc_test"
env["RMM_OIDC_TEST_ALLOW_OWNER_DDL"] = "isolated"
# No URL/password arguments, env printing, inherited SQL logging or backtraces.
env.pop("RUST_LOG", None)
env.pop("RUST_BACKTRACE", None)
cargo = shutil.which("cargo") or str(Path.home() / ".cargo/bin/cargo")
result = subprocess.run([
cargo, "test", "--manifest-path", str(ROOT / "Cargo.toml"), "--locked",
"--features", "postgres-state", "--test", "postgres_state", "--",
"--ignored", "--nocapture",
], env=env, cwd=ROOT, timeout=180)
return result.returncode
finally:
if created:
if quiet(["docker", "rm", "--force", "--volumes", NAME], timeout=30).returncode != 0:
raise RuntimeError("own disposable test container cleanup failed")
if __name__ == "__main__":
try:
sys.exit(main())
except (RuntimeError, subprocess.SubprocessError):
print("isolated PostgreSQL test failed (details/credentials suppressed)", file=sys.stderr)
sys.exit(1)