Completed for a grade of three

This commit is contained in:
2026-11-18 13:55:24 +03:00
parent cd84a6fd36
commit 248f8a5cb0
77 changed files with 4100 additions and 251 deletions
+3 -3
View File
@@ -19,11 +19,11 @@ def main():
alice, bob, eve, chat = c.bootstrap()
content = (ROOT / 'tests/fixtures/sample.png').read_bytes()
result = c.call('POST', '/api/v1/chats/' + chat['id'] + '/attachments?filename=sample.png',
201, actor=alice, raw=content, headers={'Content-Type': 'image/png'}, schema='Attachment')
attachment = result['json']
202, actor=alice, raw=content, headers={'Content-Type': 'image/png'}, schema='Attachment')
attachment = c.wait_ready(bob, result['json'])
target = result['headers']['X-Instance-Id']
c.message(alice, chat, '', attachments=[attachment['id']])
print('Upload: 201 ready; instance=' + target, flush=True)
print('Upload: 202 queued, then ready; instance=' + target, flush=True)
# Probe the real known object anonymously from the internal network, not an unknown key.
probe = subprocess.run(['docker', 'compose', 'exec', '-T', 'lb', 'wget', '-S', '-O', '/dev/null',
'http://s3:9000/' + os.environ.get('S3_BUCKET', 'messenger-attachments') + '/originals/' + attachment['id']],
+2 -1
View File
@@ -60,8 +60,9 @@ def main():
client = Client(timeout=60)
barrier.wait(timeout=30)
result = client.call('POST', '/api/v1/chats/' + chat['id'] + '/attachments?filename=load.png',
201, actor=alice, raw=data, headers={'Content-Type': 'image/png'}, schema='Attachment')
202, actor=alice, raw=data, headers={'Content-Type': 'image/png'}, schema='Attachment')
check(result['json']['sha256'] == expected, 'uploaded checksum mismatch')
client.wait_ready(bob, result['json'])
downloaded = client.call('GET', '/api/v1/attachments/' + result['json']['id'] + '/content', actor=bob)
check(hashlib.sha256(downloaded['bytes']).hexdigest() == expected, 'download checksum mismatch')
return {'upload': index, 'seconds': round(result['elapsed'], 3), 'instance': result['headers']['X-Instance-Id']}
+66
View File
@@ -0,0 +1,66 @@
#!/usr/bin/env python3
"""Verify accepted work survives stopped worker and API/broker restart with volumes."""
import hashlib
from pathlib import Path
import subprocess
import sys
import time
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / 'tests'))
from client import Client, Failure, check
from image_probe import jpeg_size
def compose(*args):
return subprocess.check_output(['docker', 'compose', *args], cwd=ROOT, text=True, stderr=subprocess.STDOUT)
def ready(client):
deadline = time.monotonic() + 90
while True:
try:
client.call('GET', '/health/ready')
return
except Failure:
check(time.monotonic() < deadline, 'API did not recover')
time.sleep(0.5)
def main():
client = Client(timeout=15)
alice, bob, eve, chat = client.bootstrap()
compose('stop', 'worker')
try:
attachment, original = client.attachment(alice, chat)
path = '/api/v1/attachments/' + attachment['id']
client.message(alice, chat, '', attachments=[attachment['id']])
client.call('GET', path + '/content', 409, actor=bob)
check(client.call('GET', path, actor=bob, schema='Attachment')['json']['status'] == 'queued', 'worker was not stopped')
print(f"PASS: worker stopped; upload 202 queued; message accepted; content 409; attachment_id={attachment['id']}", flush=True)
ids = compose('ps', '-q', 'api', 'broker').split()
before = {cid: subprocess.check_output(['docker', 'inspect', '-f', '{{.State.StartedAt}}', cid], text=True).strip() for cid in ids}
compose('restart', 'api', 'broker')
ready(client)
after = {cid: subprocess.check_output(['docker', 'inspect', '-f', '{{.State.StartedAt}}', cid], text=True).strip() for cid in ids}
check(all(before[cid] != after[cid] for cid in ids), 'API/broker restart was incomplete')
check(client.call('GET', path, actor=bob, schema='Attachment')['json']['status'] == 'queued', 'queued metadata lost')
print(f"PASS: both APIs and broker restarted with existing volumes; same attachment remains queued; attachment_id={attachment['id']}", flush=True)
compose('start', 'worker')
attachment = client.wait_ready(bob, attachment)
downloaded = client.call('GET', path + '/content', actor=bob)['bytes']
thumbnail = client.call('GET', path + '/content?variant=thumbnail', actor=bob)['bytes']
check(downloaded == original, 'original changed')
check(jpeg_size(thumbnail) == (256, 160), 'incorrect thumbnail dimensions')
variant = attachment['variants'][0]
check(variant['sha256'] == hashlib.sha256(thumbnail).hexdigest(), 'thumbnail checksum mismatch')
client.call('GET', path, 404, actor=eve)
print(f"PASS: same accepted job reached ready; original unchanged; thumbnail JPEG 256x160 with matching checksum; attachment_id={attachment['id']}", flush=True)
finally:
compose('start', 'worker')
if __name__ == '__main__':
main()
+1 -1
View File
@@ -9,7 +9,7 @@ mkdir -p secrets
# Readable by the non-root users inside the containers (bind-mounted as is).
umask 022
for name in db_password redis_password s3_access_key_id s3_secret_access_key s3_api_access_key_id s3_api_secret_access_key; do
for name in db_password redis_password broker_password s3_access_key_id s3_secret_access_key s3_api_access_key_id s3_api_secret_access_key; do
if [ ! -s "secrets/$name" ]; then
openssl rand -hex 24 > "secrets/$name"
echo "created secrets/$name"
+2 -2
View File
@@ -1,7 +1,7 @@
#!/bin/sh
# Restarts the balancer, every API replica, PostgreSQL, Redis and S3, keeping the named volume (no `down -v`, no re-seed).
# Restarts the balancer, every API replica, PostgreSQL, Redis, S3 and broker, keeping the named volume (no `down -v`, no re-seed).
set -eu
cd "$(dirname "$0")/.."
docker compose stop lb api redis postgres s3
docker compose stop lb api worker redis postgres s3 broker
docker compose up -d --wait