forked from mtusi2026/mtusi2026lab4
Add two API replicas, safe object reconciliation, upload validation, regression tests and documented load experiments.
96 lines
4.7 KiB
Python
96 lines
4.7 KiB
Python
"""Shared direct-upload fixtures. Signed URLs and credentials are never logged or persisted."""
|
|
import base64
|
|
from contextlib import contextmanager
|
|
import hashlib
|
|
import json
|
|
from pathlib import Path
|
|
import subprocess
|
|
import sys
|
|
import urllib.error
|
|
import urllib.request
|
|
import uuid
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(ROOT / 'tests'))
|
|
from client import Client, Failure, check
|
|
|
|
|
|
def docker(*args, **kwargs):
|
|
return subprocess.run(['docker', *args], cwd=ROOT, capture_output=True,
|
|
text=True, timeout=120, check=True, **kwargs).stdout
|
|
|
|
|
|
def grant(client, owner, chat, payload, **overrides):
|
|
fields = {'filename': 'direct.png', 'content_type': 'image/png',
|
|
'size_bytes': len(payload), 'sha256': hashlib.sha256(payload).hexdigest(), **overrides}
|
|
response = client.call('POST', f"/api/v1/chats/{chat['id']}/attachment-uploads", 201, actor=owner, body=fields)
|
|
check(response['headers'].get('Cache-Control') == 'no-store', 'Capability response must not be cached')
|
|
value = json.loads(response['bytes'])
|
|
check(value['method'] == 'PUT' and value['headers']['If-None-Match'] == '*', 'Incorrect upload constraints')
|
|
return value
|
|
|
|
|
|
def put(client, capability, payload, expected=200, headers=None, url=None):
|
|
request = urllib.request.Request(url or capability['url'], data=payload, method='PUT',
|
|
headers=headers if headers is not None else capability['headers'])
|
|
try:
|
|
response = client.opener.open(request, timeout=60)
|
|
except urllib.error.HTTPError as error:
|
|
response = error
|
|
except (OSError, urllib.error.URLError) as error:
|
|
raise Failure('Signed PUT connection failure: ' + type(error).__name__) from None
|
|
with response:
|
|
status = response.code
|
|
response.read(1024 * 1024)
|
|
allowed = [expected] if isinstance(expected, int) else expected
|
|
check(status in allowed, f'Signed PUT returned {status}; expected {allowed}')
|
|
return status
|
|
|
|
|
|
def complete(client, owner, capability, expected=201, path=None):
|
|
return client.call('POST', path or capability['complete_url'], expected, actor=owner, body={},
|
|
schema='Attachment' if expected == 201 else None)
|
|
|
|
|
|
def verify(client, reader, attachment, payload):
|
|
path = '/api/v1/attachments/' + attachment['id']
|
|
check(client.call('GET', path, actor=reader, schema='Attachment')['json'] == attachment, 'Metadata changed')
|
|
downloaded = client.call('GET', path + '/content', actor=reader)['bytes']
|
|
check(downloaded == payload, 'Published original bytes differ')
|
|
check(attachment['sha256'] == hashlib.sha256(payload).hexdigest(), 'Published checksum differs')
|
|
|
|
|
|
@contextmanager
|
|
def fixture():
|
|
client = Client('http://127.0.0.1:8080', timeout=60)
|
|
run_id = str(uuid.uuid4())
|
|
actors, chats = [], []
|
|
try:
|
|
for suffix in ['a', 'b']:
|
|
actors.append(client.user('cu' + run_id[:8] + suffix))
|
|
for members, actor in [([a['id'] for a in actors], actors[0]), ([actors[1]['id']], actors[1])]:
|
|
chats.append(client.call('POST', '/api/v1/chats', 201, actor=actor, schema='Chat',
|
|
body={'title': 'direct-upload:' + run_id, 'member_ids': members})['json'])
|
|
yield client, actors[0], actors[1], chats[0], chats[1]
|
|
finally:
|
|
source = (ROOT / 'test/concurrent-cleanup.mjs').read_text(encoding='utf-8')
|
|
payload = json.dumps({'runId': run_id, 'chatIds': [chat['id'] for chat in chats],
|
|
'userIds': [actor['id'] for actor in actors], 'kind': 'direct-upload'})
|
|
docker('compose', 'exec', '-T', '--index', '1', 'api', 'node', '--input-type=module', '-e', source, input=payload)
|
|
|
|
|
|
def cleanup_staging(chat_id):
|
|
source = """const {createRequire}=await import('node:module');
|
|
const require=createRequire(process.cwd()+'/package.json');
|
|
const {PrismaService}=require('./dist/prisma/prisma.service.js');
|
|
const {StorageService}=require('./dist/Storage/storage.service.js');
|
|
const {cleanStaging}=require('./dist/Attachment/staging-cleanup.js');
|
|
let input='';for await(const chunk of process.stdin)input+=chunk;
|
|
const chat=JSON.parse(input).chat;
|
|
if(!/^[0-9a-f-]{36}$/.test(chat))throw Error('invalid fixture chat');
|
|
const db=new PrismaService();const storage=new StorageService();
|
|
try{await db.onModuleInit();const report=await cleanStaging(db,storage,{apply:true,graceSeconds:1,prefix:'originals/'+chat+'/'},new Date());console.log(JSON.stringify(report));}
|
|
finally{storage.onModuleDestroy();await db.onModuleDestroy();}"""
|
|
return json.loads(docker('compose', 'exec', '-T', 'reconciler', 'node', '--input-type=module', '-e', source,
|
|
input=json.dumps({'chat': chat_id})).strip())
|