Files
mtusi2026lab6/student-tests/resilience.py

39 lines
1.9 KiB
Python

"""Public persistence and separately stop each real API replica; preserve volumes."""
import os
import subprocess
import time
from common import *
def main():
result={'before':snapshot()}
env=dict(os.environ,PYTHONUTF8='1')
for mode in ['persistence','failover','failover']:
script=ROOT/'scripts'/('restart.cmd' if mode=='persistence' else 'stop-one.cmd')
before=snapshot()
run=subprocess.run([sys.executable,'-X','utf8','tests/resilience.py','--base-url',BASE,
'--mode',mode,'--action-script',str(script)],cwd=ROOT,env=env,capture_output=True,text=True)
after=snapshot()
result.setdefault('runs',[]).append({'mode':mode,'before':before,'after':after,
'exit_code':run.returncode,'output':run.stdout+run.stderr})
# Bring back only our replicas after the public hook; next run targets the other alias below.
dc('start','api')
subprocess.check_call([sys.executable,'scripts/wait_ready.py','--project',PROJECT,'--base-url',BASE],cwd=ROOT)
save('resilience.json',result)
check(run.returncode==0,'public '+mode+' failed')
# Deterministic separate stops, independent of LB choice in the public hook.
c=Client(BASE);alice,bob,eve,chat=c.bootstrap()
for container in dc('ps','-q','api').split():
before=snapshot()
subprocess.check_call(['docker','stop','-t','2',container],stdout=subprocess.DEVNULL)
try:
for i in range(20):c.message(alice,chat,'targeted '+str(i))
finally:subprocess.check_call(['docker','start',container],stdout=subprocess.DEVNULL)
result.setdefault('each_api',[]).append({'stopped_id':container,'before':before,'after':snapshot(),'confirmed':20})
time.sleep(5)
save('resilience.json',result)
print('PASS: persistence, public failover twice, each API stopped separately')
if __name__=='__main__':main()