forked from mtusi2026/mtusi2026lab2
43 lines
1.3 KiB
Python
43 lines
1.3 KiB
Python
#!/usr/bin/env python3
|
|
"""Grade-4 Redis/Postgres failure injection for isolated local Compose environment.
|
|
Requires Docker permissions; never uses 'down -v'. Never prints tokens.
|
|
"""
|
|
import subprocess
|
|
import time
|
|
from client import Client, Failure, check, run
|
|
|
|
|
|
def compose(*args):
|
|
outcome = subprocess.run(['docker','compose','--env-file','.env',*args],
|
|
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
|
check(outcome.returncode==0, 'docker compose action failed; inspect locally')
|
|
|
|
|
|
def wait_ready(c, status):
|
|
until = time.monotonic()+60
|
|
while time.monotonic()<until:
|
|
try:
|
|
c.call('GET','/health/ready',status)
|
|
return
|
|
except Failure:
|
|
time.sleep(0.5)
|
|
raise Failure('readiness did not reach '+str(status))
|
|
|
|
|
|
def main():
|
|
c=Client(timeout=10)
|
|
alice = c.user('dependency')
|
|
for service in ('redis','postgres'):
|
|
try:
|
|
compose('stop',service)
|
|
wait_ready(c,503)
|
|
c.call('GET','/api/v1/users/me',503,actor=alice)
|
|
finally:
|
|
compose('start',service)
|
|
wait_ready(c,200)
|
|
c.call('GET','/api/v1/users/me',actor=alice,schema='User')
|
|
print('PASS:', service, 'outage returned 503; recovered without losing session')
|
|
|
|
if __name__ == '__main__':
|
|
run(main)
|