Files
mtusi2026lab2/student-tests/stack.py
T

48 lines
1.6 KiB
Python
Executable File

"""Shared helpers for student tests: public client + direct access to the compose stack."""
import os
import subprocess
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "tests"))
from client import Client, Failure, check # noqa: E402,F401
BASE_URL = os.environ.get("BASE_URL", "http://localhost:8080")
SHORT_TTL_URL = os.environ.get("SHORT_TTL_URL", "http://localhost:8081")
PASSED = []
def ok(label):
PASSED.append(label)
print("ok -", label)
def compose(*args, check_rc=True):
"""docker compose in the repository root; output is captured, never printed raw."""
proc = subprocess.run(["docker", "compose", *args], cwd=ROOT, capture_output=True, text=True)
if check_rc and proc.returncode != 0:
raise Failure("docker compose %s failed: %s" % (args[0], proc.stderr.strip()[-300:]))
return proc.stdout
def psql(sql):
"""Run SQL inside the postgres container (local socket, no password on the wire)."""
return compose("exec", "-T", "postgres", "psql", "-U", "messenger", "-d", "messenger", "-tAX", "-c", sql).strip()
def valkey(*args):
"""valkey-cli with the password read inside the container, never on this host's argv."""
cmd = 'valkey-cli --no-auth-warning -a "$(sed -n "s/^requirepass //p" /run/secrets/valkey_conf)" "$@"'
return compose("exec", "-T", "valkey", "sh", "-c", cmd, "valkey-cli", *args).strip()
def main_wrapper(main):
try:
main()
except (AssertionError, Failure) as exc:
print("FAIL:", exc)
raise SystemExit(1)
print("PASS: %d checks" % len(PASSED))