Files
mtusi2026lab7/scripts/verify_public.py
T

56 lines
2.7 KiB
Python

"""Unchanged public checks + real restart/failover hooks; evidence kept verbatim."""
import json
import os
from pathlib import Path
import subprocess
import sys
import time
import ssl
import urllib.request
from stack import docker,ready_replicas,replicas
ROOT=Path(__file__).resolve().parents[1]
ENV={**os.environ,'LAB7_PYTHON':sys.executable}
def check(script,*args,name=None):
result=subprocess.run([sys.executable,'-X','utf8',script,*args],cwd=ROOT,env=ENV,text=True,encoding='utf-8',capture_output=True)
(ROOT/'evidence'/((name or Path(script).stem)+'.txt')).write_text(result.stdout+result.stderr,encoding='utf-8')
print((name or script)+': '+('PASS' if result.returncode==0 else 'FAIL'),flush=True)
if result.returncode:raise RuntimeError('Public check failed; inspect its evidence transcript')
def main():
ready_replicas(timeout=90)
time.sleep(3) # HAProxy healthy rise after native replica readiness.
opts=['--base-url','https://127.0.0.1:8443','--ca-file',str(ROOT/'secrets/trust/ca.crt')]
check('scripts/check_contract.py')
check('scripts/validate_openapi.py')
check('tests/smoke.py',*opts)
check('tests/security.py',*opts)
ext='cmd' if os.name=='nt' else 'sh'
time.sleep(10)
check('tests/resilience.py','--mode','persistence','--action-script','scripts/restart.'+ext,*opts,name='persistence')
stopped=[]
for i in range(6):
docker('compose','stop','--timeout','2','api1-tls','api2-tls','api1','api2')
docker('compose','start','api1','api2','api1-tls','api2-tls')
ready_replicas(timeout=90)
# The LB chooses the actual target; continue until both containers have
# really been stopped. Never override the public test's selected target.
time.sleep(10)
for _ in range(i):
with urllib.request.urlopen('https://127.0.0.1:8443/health/live',context=ssl.create_default_context(cafile=str(ROOT/'secrets/trust/ca.crt')),timeout=3):pass
check('tests/resilience.py','--mode','failover','--action-script','scripts/stop-one.'+ext,*opts,name='failover-'+str(i+1))
proof=json.loads((ROOT/'evidence/stop-one.json').read_text())
stopped.append(proof)
(ROOT/'evidence'/('failover-'+str(i+1)+'-process.json')).write_text(json.dumps(proof,indent=2))
if len({p['stopped_container'] for p in stopped})==2:break
docker('compose','stop','--timeout','2','api1-tls','api2-tls','api1','api2')
docker('compose','start','api1','api2','api1-tls','api2-tls');ready_replicas(timeout=90)
assert len({p['stopped_container'] for p in stopped})==2,'Both tests selected same container; repeat second target explicitly'
print('PASS: both distinct API containers failed over; all replicas restored',flush=True)
if __name__=='__main__':main()