forked from mtusi2026/mtusi2026lab3
51 lines
1.7 KiB
Python
51 lines
1.7 KiB
Python
#!/usr/bin/env python3
|
|
"""Stop/start Valkey and PostgreSQL with volumes preserved; verify bounded failures and recovery."""
|
|
import subprocess
|
|
import time
|
|
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "tests"))
|
|
|
|
from client import Client, Failure, arguments, check, run
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
|
|
def compose(action, service):
|
|
subprocess.run(["docker", "compose", "--env-file", ".env", action, service], cwd=ROOT,
|
|
check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
|
|
|
def main():
|
|
args=arguments("Check session fallback and database dependency failure")
|
|
c=Client(args.base_url,args.ca_file,timeout=8)
|
|
actor=c.user("dependency")
|
|
|
|
compose("stop","valkey")
|
|
try:
|
|
c.call("GET","/health/ready",200,schema="Health")
|
|
me=c.call("GET","/api/v1/users/me",200,actor=actor,schema="User")["json"]
|
|
check(me["id"]==actor["id"],"session failed while Valkey was down")
|
|
finally:
|
|
compose("start","valkey")
|
|
|
|
compose("stop","postgres")
|
|
try:
|
|
c.call("GET","/health/ready",503)
|
|
c.call("GET","/api/v1/users/me",503,actor=actor)
|
|
finally:
|
|
compose("start","postgres")
|
|
|
|
deadline=time.monotonic()+60
|
|
while True:
|
|
try:
|
|
c.call("GET","/health/ready",200,schema="Health")
|
|
break
|
|
except Failure:
|
|
check(time.monotonic()<deadline,"PostgreSQL did not recover within 60 seconds")
|
|
time.sleep(.5)
|
|
c.call("GET","/api/v1/users/me",200,actor=actor,schema="User")
|
|
print("PASS: Valkey outage fell back to PostgreSQL; PostgreSQL outage returned bounded 503 and recovered with the same session")
|
|
|
|
if __name__=="__main__": run(main)
|