Files
mtusi2026lab4-fork/student-tests/outage.py
T

67 lines
2.7 KiB
Python

"""Exercise interrupted HTTP upload and unavailable S3 on this Compose project."""
import http.client
import json
import os
from pathlib import Path
import subprocess
import sys
import time
from urllib.parse import urlsplit
sys.path.insert(0,str(Path(__file__).resolve().parents[1]/'tests'))
from client import Client, check
c=Client(timeout=45)
a,b,e,chat=c.bootstrap()
att,png=c.attachment(a,chat)
base=urlsplit(c.base)
check(base.hostname in ('localhost','127.0.0.1'),'test is limited to the local lab')
# Close while the body is incomplete. No intent should be created before validation.
connection=http.client.HTTPConnection(base.hostname,base.port,timeout=5)
connection.putrequest('POST','/api/v1/chats/'+chat['id']+'/attachments?filename=interrupted.png')
connection.putheader('Authorization','Bearer '+a['token'])
connection.putheader('Content-Type','image/png')
connection.putheader('Content-Length',str(len(png)))
connection.endheaders()
connection.send(png[:16])
connection.close()
# Query only the synthetic chat through stdin; credentials/tokens are never printed.
probe="""
import sys
sys.path.insert(0,'/srv/app')
import psycopg,storage
with psycopg.connect(storage.secret('DATABASE_URL')) as db:
n=db.execute("SELECT count(*) FROM attachments WHERE chat_id=%s", (CHAT,)).fetchone()[0]
assert n==1, 'interrupted upload created metadata'
print('PASS: interrupted upload did not create an attachment')
""".replace('CHAT',repr(chat['id']))
subprocess.run(['docker','compose','exec','-T','api1','python','-'],input=probe,text=True,check=True)
# Direct request within api1 ensures the API's structured 503 is tested,
# independently of HAProxy taking all unready instances out of rotation.
probe="""
import urllib.request,urllib.error
req=urllib.request.Request('http://localhost:8080/api/v1/attachments/ATT/content',headers={'Authorization':'Bearer TOKEN'})
try:
urllib.request.urlopen(req,timeout=20)
raise AssertionError('unavailable S3 returned success')
except urllib.error.HTTPError as error:
assert error.code==503, error.code
print('PASS: unavailable S3 returns 503 from API')
""".replace('ATT',att['id']).replace('TOKEN',a['token'])
try:
subprocess.run(['docker','compose','stop','s3'],check=True)
subprocess.run(['docker','compose','exec','-T','api1','python','-'],input=probe,text=True,check=True)
finally:
subprocess.run(['docker','compose','up','-d','--wait','s3'],check=True)
for attempt in range(30):
try:
result=c.call('GET','/api/v1/attachments/'+att['id']+'/content',actor=b)
check(result['bytes']==png,'recovered file differs')
break
except AssertionError:
if attempt==29:
raise
time.sleep(1)
print('PASS: S3 recovery preserves the original bytes')