Files
mtusi2026lab2/student-tests/dependencies.py
T
koichakaev 5b47c579cc
Skeleton contracts / contracts (pull_request) Canceled after 0s
Skeleton contracts / application (pull_request) Canceled after 0s
ЛР 2: PostgreSQL, сессии, Valkey и исследование восстановления
2026-10-30 21:03:37 +03:00

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)