"""Run unchanged public failover until two distinct real API containers were stopped.""" import json,ssl,subprocess,sys,urllib.request from pathlib import Path ROOT=Path(__file__).resolve().parents[1];ca=ROOT/'.secrets/ca.crt';actions=ROOT/'evidence/failover-actions.jsonl' seen=set();ctx=ssl.create_default_context(cafile=str(ca)) for attempt in range(1,7): subprocess.run(['./scripts/compose.sh','up','-d','api'],check=True,stdout=subprocess.DEVNULL) subprocess.run(['.venv/bin/python','scripts/wait_ready.py'],check=True,stdout=subprocess.DEVNULL) # Offset round robin on alternating attempts; no affinity/test modifications. if attempt%2==0: with urllib.request.urlopen('https://localhost:8443/health/ready',context=ctx,timeout=5) as r:assert r.status==200 previous=len(actions.read_text().splitlines()) if actions.exists() else 0 with (ROOT/f'evidence/failover-each-{attempt}.txt').open('w') as f: subprocess.run(['make','test-failover','ACTION_SCRIPT=scripts/stop-one.sh','CA_FILE='+str(ca)],check=True,stdout=f) rows=[json.loads(x) for x in actions.read_text().splitlines()][previous:];assert len(rows)==1 seen.add(rows[0]['container_name']) if len(seen)>=2:break else:raise AssertionError('Two distinct API containers were not tested') subprocess.run(['./scripts/compose.sh','up','-d','api'],check=True,stdout=subprocess.DEVNULL) subprocess.run(['.venv/bin/python','scripts/wait_ready.py'],check=True,stdout=subprocess.DEVNULL) print(json.dumps({'status':'PASS','distinct_stopped_containers':sorted(seen),'runs':attempt},indent=2))