Files
mtusi2026lab4/tests/s3_unavailable.py
T
2026-11-06 17:03:03 +03:00

36 lines
1.9 KiB
Python
Executable File

#!/usr/bin/env python3
"""Stop RustFS briefly; confirm upload fails cleanly without DB metadata and recover it."""
import subprocess, sys, time
from pathlib import Path
from client import Client, LAB, arguments, check, run
ROOT=Path(__file__).resolve().parents[1]
def compose(*args): subprocess.run(['docker','compose',*args],cwd=ROOT,check=True,stdout=subprocess.DEVNULL)
def attachment_count():
code="from app.main import pool\nwith pool.connection() as c: print(c.execute('SELECT count(*) AS n FROM attachments').fetchone()['n'])"
# A dedicated short-lived process reads the count; no values other than the integer are emitted.
out=subprocess.run(['docker','compose','exec','-T','api','python','-c',code],cwd=ROOT,check=True,capture_output=True,text=True).stdout.strip()
return int(out.splitlines()[-1])
def main():
a=arguments('RustFS outage check; briefly stops the local RustFS service')
check(LAB==4,'scenario targets lab4')
c=Client(a.base_url,a.ca_file,timeout=10); alice,bob,eve,chat=c.bootstrap()
raw=(ROOT/'tests/fixtures/sample.png').read_bytes()
before=attachment_count(); stopped=False
try:
compose('stop','rustfs'); stopped=True
c.call('POST','/api/v1/chats/'+chat['id']+'/attachments?filename=outage.png',503,actor=alice,raw=raw,headers={'Content-Type':'image/png'})
finally:
if stopped: compose('start','rustfs')
deadline=time.monotonic()+60
while True:
try:
c.call('GET','/health/ready')
break
except Exception:
check(time.monotonic()<deadline,'RustFS/API did not recover within 60 seconds')
time.sleep(.5)
after=attachment_count()
check(after==before,'failed S3 upload left attachment metadata behind')
print('PASS: S3 outage returned 503; no attachment metadata persisted; RustFS recovered')
if __name__=='__main__': run(main)