Files
mtusi2026lab2/student-tests/test_compose_failures.py

52 lines
1.8 KiB
Python

"""Opt-in HTTP checks that stop only disposable Compose dependencies."""
import os
import subprocess
import sys
import unittest
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "tests"))
from client import Client
def compose(*args):
subprocess.run(
["docker", "compose", "--env-file", ".env", *args],
check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
)
@unittest.skipUnless(os.environ.get("CHECK_COMPOSE_FAILURES") == "1", "opt in on the disposable Compose stack")
class ComposeFailureTests(unittest.TestCase):
def test_cache_bypass_database_refusal_and_revocation(self):
client = Client(base_url="http://127.0.0.1:8080", timeout=15)
actor = client.user("outage")
client.call("GET", "/api/v1/users/me", actor=actor, schema="User") # fill User cache
try:
compose("stop", "valkey")
client.call("GET", "/api/v1/users/me", actor=actor, schema="User")
finally:
compose("up", "-d", "--no-deps", "--wait", "valkey")
try:
compose("stop", "postgres")
client.call("GET", "/health/ready", expected=503)
client.call("GET", "/api/v1/users/me", actor=actor, expected=503)
finally:
compose("up", "-d", "--no-deps", "--wait", "postgres")
client.call("GET", "/api/v1/users/me", actor=actor, schema="User")
client.call("DELETE", "/api/v1/auth/sessions/current", actor=actor, expected=204)
try:
compose("restart", "valkey")
finally:
compose("up", "-d", "--no-deps", "--wait", "valkey")
client.call("GET", "/api/v1/users/me", actor=actor, expected=401)
print("Valkey outage: 200; PostgreSQL outage: readiness/auth 503; revoked token after Valkey restart: 401")
if __name__ == "__main__":
unittest.main()