feat:report

This commit is contained in:
Оля Мороз
2026-11-16 20:30:12 +03:00
parent 90f407b174
commit c91ec7790f
37 changed files with 1159 additions and 209 deletions
+25
View File
@@ -0,0 +1,25 @@
#!/usr/bin/env python3
"""В Stream нет filename/URL/секретов — только id/version/attempt."""
import json, sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
from tests.client import Client, check
from student_tests.common import upload, wait_status, docker, ok, fail
def main():
c = Client()
a, b, e, ch = c.bootstrap()
content = Path('tests/fixtures/sample.png').read_bytes()
upload(c, a, ch, content)
raw = docker('exec', '-T', 'redis', 'redis-cli', '--raw', 'XRANGE', 'attachment.jobs', '-', '+')
ok('stream sample:\n' + raw)
lower = raw.lower()
for bad in ('filename', 'sample.png', 'staging/', 'final/', 'http', 'secret'):
check(bad not in lower, 'broker payload contains forbidden token: %s' % bad)
ok('broker payload has only ids/version/attempt')
if __name__ == '__main__':
try:
main()
except Exception as exc:
fail(str(exc))
+73
View File
@@ -0,0 +1,73 @@
"""Shared helpers for student lab5 tests; no tokens are logged."""
import base64, json, os, subprocess, sys, time, uuid
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
from tests.client import Client, Failure, check # noqa: E402
def fail(msg):
print('FAIL:', msg)
raise SystemExit(1)
def ok(msg='PASS'):
print(msg)
def docker(*args, check_rc=True):
"""Run docker compose <args>; return stdout. Raises if rc!=0 and check_rc."""
rc = subprocess.run(['docker', 'compose', *args], capture_output=True, text=True)
if check_rc and rc.returncode != 0:
raise Failure('docker compose %s: %s' % (' '.join(args), rc.stderr.strip()))
return rc.stdout
def psql(sql):
return docker('exec', '-T', 'postgres', 'psql', '-U', 'messenger', '-d', 'messenger',
'-tAc', sql).strip()
def upload(c, actor, chat, content, filename='sample.png', ct='image/png'):
return c.call('POST',
'/api/v1/chats/' + chat['id'] + '/attachments?filename=' + filename,
202, actor=actor, raw=content, headers={'Content-Type': ct},
schema='Attachment')['json']
def wait_status(c, actor, att_id, want, seconds=60):
deadline = time.monotonic() + seconds
while time.monotonic() < deadline:
att = c.call('GET', '/api/v1/attachments/' + att_id, actor=actor, schema='Attachment')['json']
if att['status'] == want:
return att
if att['status'] == 'failed' and want != 'failed':
fail('attachment failed: ' + str(att.get('error_code')))
time.sleep(0.25)
fail('attachment did not reach %s in %ss' % (want, seconds))
def png_bytes(w, h, seed=0):
"""Generate a valid PNG without external libs.
Быстро и с малой памятью: одна строка пикселей переиспользуется.
Для 7000x7000 занимает ~1-2 секунды и ~150 MiB RAM.
"""
import struct, zlib
# одна строка: filter byte 0 + w * 3 байта RGB
row = bytearray(1 + w * 3)
for x in range(w):
row[1 + x*3] = (x * 7 + seed) & 0xFF
row[2 + x*3] = (x * 11) & 0xFF
row[3 + x*3] = (x * 13) & 0xFF
row = bytes(row)
# zlib потоково, без накопления всего raw
co = zlib.compressobj(level=6)
idat_parts = []
for y in range(h):
idat_parts.append(co.compress(row))
idat_parts.append(co.flush())
idat = b''.join(idat_parts)
def chunk(t, d):
return struct.pack('>I', len(d)) + t + d + struct.pack('>I', zlib.crc32(t + d) & 0xffffffff)
ihdr = struct.pack('>IIBBBBB', w, h, 8, 2, 0, 0, 0)
return (b'\x89PNG\r\n\x1a\n'
+ chunk(b'IHDR', ihdr)
+ chunk(b'IDAT', idat)
+ chunk(b'IEND', b''))
+41
View File
@@ -0,0 +1,41 @@
#!/usr/bin/env python3
"""kill -9 worker после S3 write до ack → повтор не создаёт дублей."""
import sys, time, signal
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
from tests.client import Client, check
from student_tests.common import upload, wait_status, psql, docker, ok, fail, png_bytes
def main():
c = Client()
a, b, e, ch = c.bootstrap()
content = png_bytes(4000, 4000)
# Сначала готовим staging+job, но без запуска worker’а.
docker('stop', 'worker')
att = upload(c, a, ch, content)
# Запускаем worker — он возьмёт processing и, скорее всего, быстро закончит.
docker('start', 'worker')
# Ловим момент processing и убиваем процесс.
deadline = time.monotonic() + 30
while time.monotonic() < deadline:
st = psql("SELECT status FROM attachment_jobs WHERE attachment_id='%s'" % att['id'])
if st == 'processing':
break
time.sleep(0.1)
docker('kill', '-s', 'SIGKILL', 'worker')
ok('worker killed during processing')
# После lease (60s) + reaper (30s) job должна снова стать queued.
time.sleep(100)
docker('start', 'worker')
att = wait_status(c, a, att['id'], 'ready', seconds=120)
n = int(psql("SELECT count(*) FROM attachment_variants WHERE attachment_id='%s'" % att['id']))
check(n == 3, 'expected exactly 3 variants, got %d' % n)
ok('recovery produced exactly 3 non-original variants, no duplicates (original in object_key)')
if __name__ == '__main__':
try:
main()
except Exception as exc:
fail(str(exc))
+25
View File
@@ -0,0 +1,25 @@
#!/usr/bin/env python3
"""Реестр failed доступен оператору (SELECT из attachment_jobs)."""
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
from tests.client import Client, check
from student_tests.common import upload, wait_status, psql, ok, fail
def main():
c = Client()
a, b, e, ch = c.bootstrap()
content = Path('tests/fixtures/sample.png').read_bytes()[:-20]
att = upload(c, a, ch, content)
wait_status(c, a, att['id'], 'failed', seconds=180)
rows = psql(
"SELECT attachment_id, last_error_code, attempt FROM attachment_jobs "
"WHERE status='failed' ORDER BY updated_at DESC LIMIT 5")
check(rows, 'no failed jobs visible to operator')
ok('failed registry:\n' + rows)
if __name__ == '__main__':
try:
main()
except Exception as exc:
fail(str(exc))
+29
View File
@@ -0,0 +1,29 @@
#!/usr/bin/env python3
"""Битый файл: 5 attempt → failed, error_code=invalid_image."""
import sys, time
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
from tests.client import Client, check
from student_tests.common import upload, wait_status, psql, ok, fail
def main():
c = Client()
a, b, e, ch = c.bootstrap()
# Заведомо валидный PNG в API (MIME проходит), но worker получит
# битый заголовок, если подменить staging — здесь проверяем
# реалистичный сценарий: JPEG с обрезанным хвостом.
content = (Path('tests/fixtures/sample.png')).read_bytes()[:-20]
att = upload(c, a, ch, content)
# API пропустит (DecodeConfig проходит), worker упадёт на Decode.
att = wait_status(c, a, att['id'], 'failed', seconds=180)
check(att.get('error_code') == 'invalid_image', 'unexpected error_code: %s' % att.get('error_code'))
attempts = int(psql("SELECT attempt FROM attachment_jobs WHERE attachment_id='%s'" % att['id']))
# invalid_image — постоянная ошибка, worker не должен ретраить.
check(attempts == 1, 'permanent error must not be retried: attempt=%d' % attempts)
ok('failed immediately (attempt=1), error_code=invalid_image — permanent, not retried')
if __name__ == '__main__':
try:
main()
except Exception as exc:
fail(str(exc))
+28
View File
@@ -0,0 +1,28 @@
"""Мини-парсер SOF для JPEG; без внешних библиотек."""
from tests.client import Failure, check
def jpeg_size(data):
check(data[:2] == b'\xff\xd8', 'thumbnail is not JPEG')
i = 2
sof = {0xc0, 0xc1, 0xc2, 0xc3, 0xc5, 0xc6, 0xc7, 0xc9, 0xca, 0xcb, 0xcd, 0xce, 0xcf}
while i < len(data):
check(data[i] == 0xff, 'invalid JPEG marker')
while i < len(data) and data[i] == 0xff:
i += 1
check(i < len(data), 'truncated JPEG marker')
marker = data[i]
i += 1
if marker in {0xd9, 0xda}:
break
if marker in {0x01, 0xd8} or 0xd0 <= marker <= 0xd7:
continue
check(i + 2 <= len(data), 'truncated JPEG segment')
length = int.from_bytes(data[i:i + 2], 'big')
check(length >= 2 and i + length <= len(data), 'invalid JPEG segment length')
if marker in sof:
check(length >= 8, 'invalid JPEG SOF')
return (int.from_bytes(data[i + 5:i + 7], 'big'),
int.from_bytes(data[i + 3:i + 5], 'big'))
i += length
raise Failure('JPEG dimensions not found')
+68
View File
@@ -0,0 +1,68 @@
#!/usr/bin/env python3
"""Reaper восстанавливает job после паузы worker’а дольше lease (60с)."""
import sys, time
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
from tests.client import Client, check
from student_tests.common import upload, wait_status, psql, docker, ok, fail, png_bytes
def main():
c = Client()
a, b, e, ch = c.bootstrap()
content = png_bytes(4000, 4000)
# 1) Готовим job, но worker не даём её взять.
docker('pause', 'worker')
try:
att = upload(c, a, ch, content)
finally:
pass # unpause ниже — после того, как настроим SIGSTOP
# 2) Возобновляем worker: он возьмёт job и сразу поймает SIGSTOP.
# Используем docker kill --signal=SIGSTOP, чтобы остановить процесс
# (не контейнер). Контейнер "жив", но worker заморожен.
docker('unpause', 'worker')
# 3) Ждём processing; как только взял — сразу SIGSTOP.
deadline = time.monotonic() + 15
job = None
while time.monotonic() < deadline:
st = psql("SELECT status FROM attachment_jobs WHERE attachment_id='%s'" % att['id'])
if st == 'processing':
# Останавливаем процесс, пока он внутри processing.
docker('kill', '-s', 'SIGSTOP', 'worker')
job = psql("SELECT job_id FROM attachment_jobs WHERE attachment_id='%s'" % att['id'])
break
if st == 'done':
fail('job finished too fast to simulate stuck worker')
time.sleep(0.02)
check(job is not None, 'worker never entered processing')
# 4) Фиксируем lease_expires_at на момент SIGSTOP.
lease = psql("SELECT lease_expires_at FROM attachment_jobs WHERE job_id='%s'" % job)
ok('worker stopped at processing (job=%s, lease_expires_at=%s)' % (job, lease))
# 5) Ждём, пока lease протухнет и reaper вернёт job в queued.
# lease=60s, reaper=30s ⇒ до 100s; берём 120 с запасом.
deadline = time.monotonic() + 120
requeued = False
while time.monotonic() < deadline:
st = psql("SELECT status FROM attachment_jobs WHERE job_id='%s'" % job)
if st == 'queued':
requeued = True
break
time.sleep(2)
check(requeued, 'reaper did not requeue stuck job (status=%s)' % st)
ok('reaper requeued the stuck job')
# 6) Возобновляем worker: он должен доделать job до ready.
docker('kill', '-s', 'SIGCONT', 'worker')
att = wait_status(c, a, att['id'], 'ready', seconds=120)
ok('ready after unstuck')
if __name__ == '__main__':
try:
main()
except Exception as exc:
fail(str(exc))
+43
View File
@@ -0,0 +1,43 @@
#!/usr/bin/env python3
"""413, 415, 400 invalid_image — лимиты bytes/pixels/MIME."""
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
from tests.client import Client, check
from student_tests.common import png_bytes, ok, fail
def main():
c = Client()
a, b, e, ch = c.bootstrap()
url = '/api/v1/chats/' + ch['id'] + '/attachments?filename=x.bin'
# 413: > 10 MiB
big = b'\x89PNG\r\n\x1a\n' + b'\x00' * (11 * 1024 * 1024)
r = c.call('POST', url, 413, actor=a, raw=big, headers={'Content-Type': 'image/png'})
ok('413 payload_too_large')
# 415: text/plain
r = c.call('POST', url, 415, actor=a, raw=b'hello', headers={'Content-Type': 'text/plain'})
ok('415 unsupported_media_type')
# 415: клиент говорит image/png, но содержимое не PNG/JPEG по DetectContentType
r = c.call('POST', url, 415, actor=a, raw=b'\x00' * 100, headers={'Content-Type': 'image/png'})
ok('415 mismatched content (client MIME not trusted)')
# 400: PNG-сигнатура валидна (DetectContentType -> image/png),
# но IHDR/IDAT битые -> DecodeConfig падает -> invalid_image (400)
broken_png = b'\x89PNG\r\n\x1a\n' + b'\x00' * 40
r = c.call('POST', url, 400, actor=a, raw=broken_png, headers={'Content-Type': 'image/png'})
ok('400 invalid image (signature ok, header broken)')
# 400: пиксели > MAX_IMAGE_PIXELS (если выставите 1e8 — этот тест не сработает;
# по умолчанию 40 MP, значит нужно 7000x7000 ≈ 49 MP)
huge = png_bytes(7000, 7000)
r = c.call('POST', url, 400, actor=a, raw=huge, headers={'Content-Type': 'image/png'})
ok('400 too many pixels')
if __name__ == '__main__':
try:
main()
except Exception as exc:
fail(str(exc))
+32
View File
@@ -0,0 +1,32 @@
#!/usr/bin/env python3
"""Outbox: пауза outbox → 202 → unpause → ready; published_at=NULL в промежутке."""
import sys, time
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
from tests.client import Client, check
from student_tests.common import upload, wait_status, psql, docker, ok, fail
def main():
c = Client()
a, b, e, ch = c.bootstrap()
content = Path('tests/fixtures/sample.png').read_bytes()
docker('pause', 'outbox')
try:
att = upload(c, a, ch, content)
check(att['status'] == 'queued', 'expected queued')
time.sleep(3)
published = psql("SELECT coalesce(published_at::text,'NULL') FROM attachment_jobs WHERE attachment_id='%s'" % att['id'])
check(published == 'NULL', 'outbox paused but published_at=%s' % published)
ok('outbox paused: published_at IS NULL, upload accepted')
finally:
docker('unpause', 'outbox')
att = wait_status(c, a, att['id'], 'ready', seconds=90)
ok('after unpause: ready')
if __name__ == '__main__':
try:
main()
except Exception as exc:
fail(str(exc))
+35
View File
@@ -0,0 +1,35 @@
#!/usr/bin/env python3
"""Повторный XADD того же job_id не создаёт дублей вариантов."""
import json, subprocess, sys, time
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
from tests.client import Client, check
from student_tests.common import upload, wait_status, psql, docker, ok, fail
def main():
c = Client()
a, b, e, ch = c.bootstrap()
content = Path('tests/fixtures/sample.png').read_bytes()
att = upload(c, a, ch, content)
att = wait_status(c, a, att['id'], 'ready')
before = psql("SELECT count(*) FROM attachment_variants WHERE attachment_id='%s'" % att['id'])
check(before == '3', 'expected 3 variants, got %s' % before)
job = psql("SELECT job_id FROM attachment_jobs WHERE attachment_id='%s'" % att['id'])
payload = json.dumps({
"schema_version": 1, "job_id": job, "type": "attachment.process.v1",
"attachment_id": att['id'], "pipeline_version": 1, "attempt": 1,
"created_at": "2026-01-01T00:00:00Z",
})
# Повторная доставка через Redis CLI.
docker('exec', '-T', 'redis', 'redis-cli', 'XADD', 'attachment.jobs', '*', 'payload', payload)
time.sleep(5)
after = psql("SELECT count(*) FROM attachment_variants WHERE attachment_id='%s'" % att['id'])
check(after == '3', 'redelivery created duplicate variants: %s' % after)
ok('redelivery is no-op (still 3 non-original variants; original via object_key)')
if __name__ == '__main__':
try:
main()
except Exception as exc:
fail(str(exc))
+33
View File
@@ -0,0 +1,33 @@
#!/usr/bin/env python3
"""Replay failed → снова queued."""
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
from tests.client import Client, check
from student_tests.common import upload, wait_status, psql, ok, fail
def main():
c = Client()
a, b, e, ch = c.bootstrap()
# Форсим failed (битый файл проходит DecodeConfig, но не Decode).
content = Path('tests/fixtures/sample.png').read_bytes()[:-20]
att = upload(c, a, ch, content)
att = wait_status(c, a, att['id'], 'failed', seconds=180)
ok('failed reached')
c.call('POST', '/api/v1/attachments/' + att['id'] + '/replay', 202, actor=a)
st = psql("SELECT status FROM attachment_jobs WHERE attachment_id='%s'" % att['id'])
check(st == 'queued', 'after replay expected queued, got %s' % st)
ok('replay -> queued')
# Replay дал одну новую попытку; ошибка permanent, поэтому снова failed при attempt=1.
att = wait_status(c, a, att['id'], 'failed', seconds=180)
atts = int(psql("SELECT attempt FROM attachment_jobs WHERE attachment_id='%s'" % att['id']))
check(atts == 1, 'after replay: permanent error must fail at attempt=1, got %d' % atts)
ok('replay reset attempt to 1; permanent error failed again — not retried')
if __name__ == '__main__':
try:
main()
except Exception as exc:
fail(str(exc))
+80
View File
@@ -0,0 +1,80 @@
#!/usr/bin/env python3
"""Временная ошибка S3: attempt растёт, backoff+jitter, затем ready."""
import subprocess, sys, time
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
from tests.client import Client, check
from student_tests.common import upload, wait_status, psql, docker, ok, fail
def main():
c = Client()
a, b, e, ch = c.bootstrap()
content = Path('tests/fixtures/sample.png').read_bytes()
# 1) Настраиваем mc с кредами — иначе удалить/положить объект не сможем.
root_user = open('secrets/minio_root_user.txt').read().strip()
root_pass = open('secrets/minio_root_password.txt').read().strip()
docker('exec', '-T', 's3', 'mc', 'alias', 'set', 'loc',
'http://localhost:9000', root_user, root_pass, '--api', 'S3v4')
# 2) Пауза worker’а: job не будет взят до unpause.
docker('pause', 'worker')
try:
att = upload(c, a, ch, content)
staging = psql("SELECT staging_key FROM attachments WHERE id='%s'" % att['id'])
check(staging.startswith('staging/'), 'unexpected staging_key')
# 3) Удаляем staging: это гарантирует, что worker НЕ прочитает объект.
# Если worker ещё не стартовал — GetObject вернёт NoSuchKey.
subprocess.run(
['docker', 'compose', 'exec', '-T', 's3', 'mc', 'rm',
'loc/messenger-attachments/' + staging],
capture_output=True, text=True, check=True)
# 4) Отдаём job worker’у.
docker('unpause', 'worker')
# 5) Ждём, пока attempt >= 2.
job = psql("SELECT job_id FROM attachment_jobs WHERE attachment_id='%s'" % att['id'])
deadline = time.monotonic() + 90
attempt = 1
while time.monotonic() < deadline:
attempt = int(psql("SELECT attempt FROM attachment_jobs WHERE job_id='%s'" % job))
if attempt >= 2:
break
time.sleep(1)
check(attempt >= 2,
'retry did not happen on missing staging object (attempt=%d)' % attempt)
ok('transient error retried: attempt=%d' % attempt)
# 6) Логи retry именно для нашего job_id.
logs = docker('logs', '--tail=500', 'worker')
lines = [l for l in logs.splitlines()
if ('retry job=' + job) in l]
check(len(lines) >= 1, 'no retry log lines for job %s:\n%s' % (job, logs[-1500:]))
ok('retry log lines:')
for l in lines:
print(' ' + l)
# 7) Возвращаем staging — job должен пройти retry и стать ready.
# Перезаписываем объект с правильным содержимым.
# Через mc pipe: передаём байты на stdin.
r = subprocess.run(
['docker', 'compose', 'exec', '-T', 's3',
'mc', 'pipe', 'loc/messenger-attachments/' + staging],
input=content, capture_output=True)
check(r.returncode == 0, 'failed to restore staging: %s' % r.stderr.decode())
att = wait_status(c, a, att['id'], 'ready', seconds=180)
ok('recovered to ready after staging restored (attempt=%d)' % attempt)
finally:
# На всякий случай — если упали в середине, worker не должен остаться на паузе.
subprocess.run(['docker', 'compose', 'unpause', 'worker'],
capture_output=True)
if __name__ == '__main__':
try:
main()
except Exception as exc:
fail(str(exc))
+36
View File
@@ -0,0 +1,36 @@
#!/usr/bin/env python3
"""После ready staging пуст (объект перенесён в final)."""
import sys
import subprocess
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
from tests.client import Client, check
from student_tests.common import upload, wait_status, psql, docker, ok, fail
def main():
c = Client()
a, b, e, ch = c.bootstrap()
content = Path('tests/fixtures/sample.png').read_bytes()
att = upload(c, a, ch, content)
staging = psql("SELECT staging_key FROM attachments WHERE id='%s'" % att['id'])
check(staging.startswith('staging/'), 'staging_key does not start with staging/')
att = wait_status(c, a, att['id'], 'ready')
root_user = open('secrets/minio_root_user.txt').read().strip()
root_pass = open('secrets/minio_root_password.txt').read().strip()
docker('exec', '-T', 's3', 'mc', 'alias', 'set', 'local_check',
'http://localhost:9000', root_user, root_pass, '--api', 'S3v4')
r = subprocess.run(
['docker', 'compose', 'exec', '-T', 's3', 'mc', 'stat',
'local_check/messenger-attachments/' + staging],
capture_output=True, text=True)
combined = ((r.stdout or '') + (r.stderr or '')).lower()
check('does not exist' in combined or 'not found' in combined,
'staging object still present: %s\n%s' % (staging, combined))
ok('staging object removed after ready (mc rc=%d)' % r.returncode)
if __name__ == '__main__':
try:
main()
except Exception as exc:
fail(str(exc))
+45
View File
@@ -0,0 +1,45 @@
#!/usr/bin/env python3
"""queued → processing → ready через остановку worker’а."""
import time, sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
from tests.client import Client, check
from student_tests.common import upload, wait_status, docker, ok, fail
def main():
c = Client()
a, b, e, ch = c.bootstrap()
content = Path('tests/fixtures/sample.png').read_bytes()
docker('pause', 'worker')
try:
att = upload(c, a, ch, content)
check(att['status'] == 'queued', 'expected queued after upload')
ok('upload -> queued')
# Не должно перейти в processing, пока worker на паузе.
time.sleep(3)
att = c.call('GET', '/api/v1/attachments/' + att['id'], actor=a, schema='Attachment')['json']
check(att['status'] == 'queued', 'worker paused but status changed to %s' % att['status'])
ok('status stays queued while worker paused')
finally:
docker('unpause', 'worker')
att = wait_status(c, a, att['id'], 'ready', seconds=90)
names = {v['name'] for v in att['variants']}
# original в variants[] нет по контракту OpenAPI — он в object_key/оригинальном файле.
check(names == {'thumbnail', 'crop', 'optimized'},
'variants mismatch: %s' % names)
ok('ready with thumbnail/crop/optimized (original available via ?variant=original)')
# original доступен отдельно — проверим, что эндпоинт отвечает 200.
r = c.call('GET',
'/api/v1/attachments/' + att['id'] + '/content?variant=original',
200, actor=a)
check(len(r['bytes']) > 0, 'original empty')
ok('original served via ?variant=original (%d bytes)' % len(r['bytes']))
if __name__ == '__main__':
try:
main()
except Exception as exc:
fail(str(exc))
+52
View File
@@ -0,0 +1,52 @@
#!/usr/bin/env python3
"""original неизменен, thumbnail 256x256, crop 128x128, optimized существует."""
import hashlib, sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
from tests.client import Client, check
from student_tests.common import upload, wait_status, ok, fail
from student_tests.jpeg_probe import jpeg_size as dims_jpeg
def main():
c = Client()
a, b, e, ch = c.bootstrap()
content = Path('tests/fixtures/sample.png').read_bytes()
att = upload(c, a, ch, content)
att = wait_status(c, a, att['id'], 'ready')
src_sha = hashlib.sha256(content).hexdigest()
def get(v):
return c.call('GET', '/api/v1/attachments/%s/content?variant=%s' % (att['id'], v),
200, actor=a)['bytes']
orig = get('original')
check(hashlib.sha256(orig).hexdigest() == src_sha, 'original changed')
ok('original byte-identical')
th = get('thumbnail')
w, h = dims_jpeg(th)
check(max(w, h) == 256 and w <= 256 and h <= 256,
'thumbnail is %dx%d, expected fit in 256x256' % (w, h))
check(w * 160 == h * 256 if h == 160 else True, ...) # необязательно
# Проще всего — сверить с metadata:
thumb_meta = next(v for v in att['variants'] if v['name'] == 'thumbnail')
check((w, h) == (thumb_meta['width'], thumb_meta['height']),
'thumbnail metadata %sx%s != actual %dx%d' % (thumb_meta['width'], thumb_meta['height'], w, h))
ok('thumbnail %dx%d (metadata matches)' % (w, h))
cr = get('crop')
w, h = dims_jpeg(cr)
check((w, h) == (128, 128), 'crop is %dx%d' % (w, h))
ok('crop 128x128 JPEG')
opt = get('optimized')
check(len(opt) > 0, 'optimized empty')
# optimized может быть JPEG или WebP; проверяем, что не пусто и похоже на изображение
check(opt[:2] == b'\xff\xd8' or opt[:4] == b'RIFF', 'optimized is not JPEG/WebP')
ok('optimized present (%d bytes)' % len(opt))
if __name__ == '__main__':
try:
main()
except Exception as exc:
fail(str(exc))