Files
mtusi2026lab4/scripts/check_uploader_failover.py

45 lines
1.9 KiB
Python

"""Stop the API that actually accepted the binary upload, then download via LB."""
import hashlib
import json
from pathlib import Path
import sys
import time
ROOT=Path(__file__).resolve().parents[1]
sys.path[:0]=[str(ROOT/'tests'),str(ROOT/'scripts')]
from client import Client, Failure
from ops import containers, command, compose
def main():
c=Client(timeout=5)
alice,bob,eve,chat=c.bootstrap()
raw=(ROOT/'tests/fixtures/sample.png').read_bytes()
response=c.call('POST','/api/v1/chats/'+chat['id']+'/attachments?filename=failover.png',201,
actor=alice,raw=raw,headers={'Content-Type':'image/png'},schema='Attachment')
instance=response['headers']['X-Instance-Id']
matches=[r for r in containers() if instance.startswith(r['Config']['Hostname']+'-')]
assert len(matches)==1
victim=matches[0]['Id']; started=time.monotonic()
command(['docker','stop','--time','6',victim])
try:
deadline=time.monotonic()+10
while True:
try:
result=c.call('GET','/api/v1/attachments/'+response['json']['id']+'/content',actor=bob)
break
except Failure:
if time.monotonic()>deadline: raise
time.sleep(.2)
assert result['headers']['X-Instance-Id']!=instance
assert result['bytes']==raw
assert command(['docker','inspect','--format','{{.State.Running}}',victim])=='false'
report={'upload_instance':instance,'stopped_container':victim,
'download_instance':result['headers']['X-Instance-Id'],
'sha256':hashlib.sha256(raw).hexdigest(),'size_bytes':len(raw),
'stop_and_download_seconds':round(time.monotonic()-started,3),'status':'PASS'}
(ROOT/'evidence/uploader-failover.json').write_text(json.dumps(report,indent=2))
finally:
compose('start','api')
print('PASS: original downloaded through another replica after uploader stopped')
if __name__=='__main__': main()