Implement lab4 S3 attachments

This commit is contained in:
2026-11-11 18:55:53 +03:00
parent ec540a1a72
commit d894764b39
44 changed files with 1333 additions and 23 deletions
+35
View File
@@ -0,0 +1,35 @@
#!/usr/bin/env python3
"""Additional negative integration checks against a running lab4 stack."""
import sys
from pathlib import Path
sys.path.insert(0,str(Path(__file__).resolve().parents[1]/'tests'))
from client import Client, arguments, run
from uuid import uuid4
def main():
args=arguments('Negative attachment checks')
c=Client(args.base_url,args.ca_file)
alice,bob,eve,chat=c.bootstrap()
prefix='/api/v1/chats/'+chat['id']+'/attachments?filename='
fixture=(Path(__file__).resolve().parents[1]/'tests/fixtures/sample.png').read_bytes()
hdr={'Content-Type':'image/png'}
c.call('POST',prefix+'../bad.png',400,actor=alice,raw=fixture,headers=hdr)
c.call('POST',prefix+'bad%5Cname.png',400,actor=alice,raw=fixture,headers=hdr)
c.call('POST',prefix+'too-large.png',413,actor=alice,raw=fixture+b'\0'*(10*1024*1024+1),headers=hdr)
c.call('POST',prefix+'corrupt.png',415,actor=alice,raw=b'not an image',headers=hdr)
c.call('POST',prefix+'spoof.png',415,actor=alice,raw=fixture,headers={'Content-Type':'image/jpeg'})
att,_=c.attachment(alice,chat)
for suffix in ('','/content'):
c.call('GET','/api/v1/attachments/'+att['id']+suffix,404,actor=eve)
other=c.call('POST','/api/v1/chats',201,actor=alice,body={
'title':'Other chat','member_ids':[alice['id'],eve['id']]},schema='Chat')['json']
c.message(alice,chat,'',attachments=[att['id']])
c.call('POST','/api/v1/chats/'+other['id']+'/messages',404,actor=alice,
body={'text':'','attachment_ids':[att['id']]},headers={'Idempotency-Key':uuid4().hex})
c.call('POST','/api/v1/chats/'+chat['id']+'/messages',404,actor=alice,
body={'text':'','attachment_ids':[str(uuid4())]},headers={'Idempotency-Key':uuid4().hex})
print('PASS: invalid size/image/MIME/filename, outsider ACL and cross-chat bindings')
if __name__=='__main__':
run(main)
+38
View File
@@ -0,0 +1,38 @@
#!/usr/bin/env python3
"""Integration check: concurrent same-key POSTs via LB, no custom test dependencies."""
import concurrent.futures
import json
import sys
import uuid
from pathlib import Path
sys.path.insert(0,str(Path(__file__).resolve().parents[1] / 'tests'))
from client import Client, run, arguments, check
def main():
args=arguments('Concurrent idempotency and LB check')
c=Client(args.base_url,args.ca_file,timeout=8)
alice,bob,eve,chat=c.bootstrap()
key=uuid.uuid4().hex
body={'text':'Конкурентное сообщение '+key}
path='/api/v1/chats/'+chat['id']+'/messages'
def send(_):
# Each worker has its own client, simulating independent processes.
worker=Client(args.base_url,args.ca_file,timeout=8)
return worker.call('POST',path,201,actor=alice,body=body,
headers={'Idempotency-Key':key},schema='Message')
with concurrent.futures.ThreadPoolExecutor(max_workers=16) as pool:
results=list(pool.map(send,range(32)))
ids={r['json']['id'] for r in results}
instances={r['headers'].get('X-Instance-Id') for r in results}
check(len(ids)==1,'Concurrent messages were duplicated')
check(len(instances)>=2,'Only one instance was observed; check LB')
c.call('POST',path,409,actor=alice,body={'text':'different text'},headers={'Idempotency-Key':key})
stored=c.call('GET',path,actor=bob,schema='MessagePage')['json']['items']
check(sum(x['id'] in ids for x in stored)==1,'History contains duplicate or no record')
print(json.dumps({'concurrent_requests':len(results),'unique_messages':len(ids),
'observed_replicas':sorted(instances)},ensure_ascii=False))
print('PASS: shared idempotency is atomic across replicas')
if __name__=='__main__':
run(main)