Files
mtusi2026lab4/student-tests/dependency_checks.py
T

72 lines
2.7 KiB
Python

"""Verify shared-store failures or short session TTL; restores stopped stores."""
import argparse
import json
from pathlib import Path
import subprocess
import sys
import time
import urllib.error
import urllib.request
ROOT = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(ROOT / "tests"))
from client import Client, check
def docker(*args):
return subprocess.run(["docker", *args], cwd=ROOT, check=True,
capture_output=True, text=True).stdout.strip()
def status(url):
try:
response = urllib.request.urlopen(url, timeout=5)
except urllib.error.HTTPError as error:
response = error
with response:
return response.status
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--base-url", default="http://127.0.0.1:8084")
parser.add_argument("--expiry", action="store_true",
help="Requires API SESSION_TTL_SECONDS=2, configured by the operator")
args = parser.parse_args()
client = Client(args.base_url)
actor = client.user("dependencies")
client.call("GET", "/api/v1/users/me", actor=actor, schema="User")
if args.expiry:
time.sleep(2.3)
aliases = set()
for _ in range(12):
response = client.call("GET", "/api/v1/users/me", 401, actor=actor)
aliases.add(response["headers"]["X-Instance-Id"])
check(len(aliases) >= 2, "Expiry not observed through two replicas")
print("PASS: TTL=2 session rejected by both replicas without re-login")
return
try:
docker("compose", "stop", "redis")
client.call("GET", "/api/v1/users/me", actor=actor, schema="User")
client.call("GET", "/health/ready", schema="Health")
print("PASS: Redis stopped; profile and readiness still return 200 via LB")
finally:
docker("compose", "up", "-d", "--wait", "--wait-timeout", "60", "redis")
try:
docker("compose", "stop", "postgres")
check(status(args.base_url + "/health/ready") == 503, "PostgreSQL failure did not disable readiness")
# LB may exclude all APIs, so inspect liveness inside the Docker network.
live = docker("compose", "exec", "-T", "redis", "wget", "-q", "-O", "-", "http://api:8080/health/live")
check(json.loads(live) == {"status": "ok"}, "API process liveness failed")
print("PASS: PostgreSQL stopped; LB readiness=503, API process liveness=200")
finally:
docker("compose", "up", "-d", "--wait", "--wait-timeout", "60")
client.call("GET", "/api/v1/users/me", actor=actor, schema="User")
print("PASS: PostgreSQL restored; original session still valid")
if __name__ == "__main__":
main()