Files
mtusi2026lab2/student-tests/stack.py

44 lines
1.2 KiB
Python
Executable File

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
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):
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):
return compose("exec", "-T", "postgres", "psql", "-U", "messenger", "-d", "messenger", "-tAX", "-c", sql).strip()
def valkey(*args):
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))