Implement lab 5 durable image processing worker

This commit is contained in:
2026-11-20 19:08:09 +03:00
parent 276dbdad8a
commit d8332e17f6
28 changed files with 1330 additions and 24 deletions
+60
View File
@@ -0,0 +1,60 @@
"""Attachment negative checks, private-bucket check, and concurrent upload budget."""
import base64
import hashlib
import os
import subprocess
import sys
import threading
import time
import uuid
from concurrent.futures import ThreadPoolExecutor
from urllib.request import Request, urlopen
from urllib.error import HTTPError, URLError
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / 'tests'))
from client import Client, check, Failure
PNG = (b'\x89PNG\r\n\x1a\n' + b'\x00' * 100)
def main():
c = Client()
alice, bob, eve, chat = c.bootstrap()
path = '/api/v1/chats/' + chat['id'] + '/attachments?filename=photo.png'
att, content = c.attachment(alice, chat)
check(att['status'] == 'ready' and att['sha256'] == hashlib.sha256(content).hexdigest(), 'metadata mismatch')
downloaded = c.call('GET', '/api/v1/attachments/' + att['id'] + '/content', actor=bob)
check(downloaded['bytes'] == content, 'download mismatch')
c.call('GET', '/api/v1/attachments/' + att['id'], 404, actor=eve)
c.call('GET', '/api/v1/attachments/' + att['id'] + '/content', 404, actor=eve)
c.call('GET', '/api/v1/attachments/' + str(uuid.uuid4()), 404, actor=alice)
c.call('POST', '/api/v1/chats/' + chat['id'] + '/attachments?filename=bad/name.png', 400,
actor=alice, raw=content, headers={'Content-Type': 'image/png'})
c.call('POST', '/api/v1/chats/' + chat['id'] + '/attachments?filename=spoof.png', 415,
actor=alice, raw=b'not-an-image', headers={'Content-Type': 'image/png'})
c.call('POST', '/api/v1/chats/' + chat['id'] + '/attachments?filename=spoof.txt', 415,
actor=alice, raw=content, headers={'Content-Type': 'text/plain'})
c.call('POST', '/api/v1/chats/' + chat['id'] + '/attachments?filename=huge.png', 413,
actor=alice, raw=b'0' * (10 * 1024 * 1024 + 1), headers={'Content-Type': 'image/png'})
responses = []
def upload(_):
return c.attachment(alice, chat)[0]['id']
with ThreadPoolExecutor(max_workers=10) as pool:
responses = list(pool.map(upload, range(10)))
check(len(set(responses)) == 10, 'concurrent uploads shared an id')
try:
urlopen('http://127.0.0.1:9000/messenger/original/' + str(att['id']), timeout=3)
except HTTPError as error:
check(error.code in (400, 403), 'bucket/object is publicly readable')
else:
raise AssertionError('bucket/object is publicly readable')
stats = subprocess.check_output(['docker', 'stats', '--no-stream', '--format', '{{.Name}}={{.MemUsage}}',
'vp-lab4-api1-1', 'vp-lab4-api2-1'], text=True)
Path(ROOT / 'evidence/attachment-resources.txt').write_text(stats, encoding='utf-8')
print('PASS: attachment access, validation, private bucket and 10 concurrent uploads')
if __name__ == '__main__':
main()