#!/usr/bin/env python3 """Stop each real container, then replace them in sequence under writes.""" import json,os,subprocess,sys,threading,time,uuid from pathlib import Path ROOT=Path(__file__).resolve().parents[1] sys.path.insert(0,str(ROOT/'tests')) from client import Client,Failure,check BASE=os.environ.get('BASE_URL','http://localhost:8086') def dc(*args):return subprocess.check_output(['docker','compose',*args],cwd=ROOT,text=True,stderr=subprocess.DEVNULL).strip() def inspect(id):return json.loads(subprocess.check_output(['docker','inspect',id],text=True))[0] def ready(): deadline=time.monotonic()+45 while time.monotonic()=action_done] check(successes_after,'no successful write after action') recovery=successes_after[0]-started return {'successful_writes':len(results),'errors':failed,'successful_rps':len(results)/(time.monotonic()-started+1),'first_recovery_seconds':round(recovery,3),'action_seconds':round(action_done-started,3),'instances':sorted(seen),'confirmed_preserved':True} def main(): ready();c=Client(BASE);a,b,e,chat=c.bootstrap();results={'failures':[]} ids=dc('ps','-q','api').splitlines();check(len(ids)==2,'requires two API containers') for id in ids: alias=inspect(id)['Config']['Hostname'] env=dict(os.environ,TARGET_INSTANCE_ID=alias) def action():subprocess.run([str(ROOT/'scripts/stop-one.sh')],env=env,check=True,stdout=subprocess.DEVNULL) try: result=run_phase(c,a,chat,action);result['stopped_alias']=alias check(not inspect(id)['State']['Running'],'target is still running') check(result['first_recovery_seconds']<10,'recovery >10s') results['failures'].append(result) finally:ready() results['rolling']=run_phase(c,a,chat,lambda:subprocess.run([str(ROOT/'scripts/rolling.sh')],check=True,stdout=subprocess.DEVNULL),seconds=2) check(len(dc('ps','-q','api').splitlines())==2,'rolling lost replicas') (ROOT/'evidence/operations.json').write_text(json.dumps(results,indent=2)+'\n') print('PASS: each API stopped and rolling replacement completed; confirmed writes preserved') if __name__=='__main__':main()