Files
mtusi2026lab7/student-tests/failover-each.py
T

41 lines
1.9 KiB
Python

#!/usr/bin/env python3
"""Target both concrete replicas in sequence and preserve session/data through each stop."""
import json
import os
from pathlib import Path
import subprocess
import sys
import time
sys.path.insert(0,str(Path(__file__).resolve().parents[1]/'tests'))
from client import Client
c=Client('https://localhost:8443',ca_file='secrets/ca.crt')
alice,bob,eve,chat=c.bootstrap()
ids=subprocess.check_output(['docker','compose','ps','-q','api'],text=True).split()
assert len(ids)==2
saved=[]
for id in ids:
alias=subprocess.check_output(['docker','exec',id,'cat','/tmp/instance-alias'],text=True).strip()
before=c.message(alice,chat,'confirmed-before-'+alias)['json']; saved.append(before)
start=time.monotonic()
subprocess.run(['scripts/stop-one.sh'],env=dict(os.environ,TARGET_INSTANCE_ID=alias),check=True,stdout=subprocess.DEVNULL)
for attempt in range(30):
try:
response=c.message(alice,chat,'confirmed-after-'+alias,key='retry-'+alias)
break
except Exception:
time.sleep(.25)
else: raise AssertionError('no recovery')
assert response['headers']['X-Instance-Id']!=alias
saved.append(response['json'])
page=c.call('GET','/api/v1/chats/'+chat['id']+'/messages',actor=bob,schema='MessagePage')['json']
assert all(x in page['items'] for x in saved)
name=subprocess.check_output(['docker','inspect','--format','{{.Name}}',id],text=True).strip()
print('PASS stopped',name,'confirmed data/session preserved; recovery_seconds',round(time.monotonic()-start,3),flush=True)
subprocess.run(['docker','start',id],check=True,stdout=subprocess.DEVNULL)
for attempt in range(100):
seen={c.call('GET','/health/ready')['headers']['X-Instance-Id'] for _ in range(4)}
if len(seen)>=2: break
time.sleep(.2)
else: raise AssertionError('replica did not rejoin')
print('PASS both distinct API containers stopped in sequence and restored')