ЛР 4 — БПИ2304 — Югай — на 4 #148

Open
ArturYugay wants to merge 2 commits from ArturYugay/mtusi2026lab4:solution/bpi2304-yugay into main
58 changed files with 6062 additions and 41 deletions
+2
View File
@@ -18,3 +18,5 @@ __pycache__
node_modules
evidence
.DS_Store
**/__pycache__
**/*.py[cod]
+4 -17
View File
@@ -1,20 +1,7 @@
# Copy to .env for your LOCAL synthetic test environment; never commit .env.
# These are logical interface suggestions; map actual framework names in REPORT.md.
APP_HOST=0.0.0.0
APP_PORT=8080
APP_ENV=lab
HTTP_PORT=8080
COMPOSE_PROJECT_NAME=mtusi-lab4
SESSION_TTL_SECONDS=3600
# Choose credentials locally. File paths contain no secret values.
DATABASE_URL_FILE=/run/secrets/database_url
# Optional on 3, required use on 4 in lab2; Redis OR Valkey:
SESSION_STORE=postgres
CACHE_URL_FILE=/run/secrets/cache_url
API_REPLICAS=2
IDEMPOTENCY_TTL_SECONDS=86400
S3_ENDPOINT=http://s3:9000
S3_REGION=us-east-1
S3_BUCKET=messenger-attachments
S3_ACCESS_KEY_ID_FILE=/run/secrets/s3_access_key_id
S3_SECRET_ACCESS_KEY_FILE=/run/secrets/s3_secret_access_key
MAX_UPLOAD_BYTES=10485760
MAX_IMAGE_PIXELS=40000000
CLEANUP_INTERVAL_SECONDS=30
CLEANUP_GRACE_SECONDS=600
+2
View File
@@ -0,0 +1,2 @@
* text=auto eol=lf
*.cmd text eol=crlf
+15
View File
@@ -0,0 +1,15 @@
FROM python:3.13.14-slim-bookworm@sha256:67a1e1f215ccda113cfc024e8639049257e88f273898f595b61476d128d387e8
ENV PYTHONDONTWRITEBYTECODE=1 PYTHONUNBUFFERED=1 PYTHONUTF8=1 \
APP_HOST=0.0.0.0 APP_PORT=8080 APP_ENV=lab
WORKDIR /app
COPY requirements.lock .
RUN python -m pip install --no-cache-dir -r requirements.lock \
&& useradd --uid 10001 --create-home messenger
COPY src ./src
COPY migrations ./migrations
USER messenger
EXPOSE 8080
HEALTHCHECK --interval=5s --timeout=4s --start-period=10s --retries=6 \
CMD python -c "import os,urllib.request; urllib.request.urlopen('http://127.0.0.1:'+os.environ['APP_PORT']+'/health/ready',timeout=3).close()"
CMD ["python", "-m", "src"]
+60 -24
View File
@@ -1,37 +1,73 @@
# Отчёт — лабораторная 4
# ЛР4 — S3 и вложения
- Автор, группа: TODO
- Целевая оценка; выбранный трек 5, если нужен: TODO
- Commit/PR: TODO после публикации
- Использованные источники, библиотеки и помощь ИИ: TODO
Югай Артур Рустамович, БПИ2304. На 4.
## Запуск из чистого clone
## Запуск
TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных.
Нужны Git, Python 3.13 и Docker с Compose и Linux-контейнерами. Для memory-check нужен cgroup v2. Порт 8080 должен быть свободен.
## Архитектура и решения
```sh
git clone -b solution/bpi2304-yugay https://gitea.privcloud.tech/ArturYugay/mtusi2026lab4.git lab4
cd lab4
python -X utf8 scripts/lab.py verify
```
TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab3, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации.
В Linux Python может называться python3. Скрипт создаёт .venv, локальные секреты, собирает образ и выполняет проверки. API: http://127.0.0.1:8080. Повторный setup сохраняет ключи и данные.
## Доказательства критериев
Отдельно: setup, build, start, test, restart-check, failover-check, cleanup-check, memory-check, restore, stop. Команды вызываются через тот же scripts/lab.py. Перед первым start нужны setup и build. stop сохраняет volumes. Тесты добавляют синтетические данные.
| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства |
| --- | --- | --- | --- |
| Публичный smoke | make test | TODO | TODO |
| Остальные пункты уровня 3 | TODO | TODO | TODO |
| Все пункты уровня 4, если заявлен | TODO | TODO | TODO |
| Выбранный трек 5, если заявлен | TODO | TODO | TODO |
## Что сделано
TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены.
Клиент → HAProxy → api1/api2 → PostgreSQL + приватный S3 gateway → SeaweedFS storage. Использована своя ЛР3: пользователи, Argon2id, Bearer-сессии, SQL-пагинация и идемпотентная отправка сообщений сохранены. PostgreSQL 17.11, HAProxy 3.2.26, SeaweedFS 4.54; образы закреплены digest, Python-зависимости — lock-файлами.
## Эксперименты и отказы
Оригиналы живут в S3, метаданные и связи с сообщениями — в БД. Файл принимается сырыми байтами во временный файл, проверяется Pillow и сохраняется одним PutObject. Временный файл нужен только для приёма и проверки; постоянных файлов и volumes у API нет. Ответ 201 ready выдаётся после S3 и COMMIT метаданных.
TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения.
PNG/JPEG: до 10 MiB и 40 млн пикселей. MIME определяется по содержимому, опасные имена отклоняются. Пустой текст разрешён с вложениями; максимум 10 уникальных вложений своего чата. Скачивание идёт через авторизованный API блоками по 64 KiB, без redirect и перекодирования. Чужие файлы возвращают 404.
| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных |
| --- | --- | --- | --- |
| TODO | TODO | TODO | TODO |
Наружу открыт только LB на 127.0.0.1:8080. Storage/filer находятся в отдельной внутренней сети; gateway не запускает IAM и дополнительные каталоги. API работает без root и получает только Read/Write/List своего bucket. Административные ключи доступны bootstrap, но не API. setup генерирует секреты в игнорируемом secrets/, Compose монтирует их файлами.
## Ограничения и следующие шаги
Bootstrap повторяемый: создаёт bucket при отсутствии, проверяет ACL и запись/чтение служебного system/readiness. Этот постоянный объект нужен для health. ready проверяет БД, миграцию и чтение байтов S3; live проверяет процесс. После рестарта bootstrap повторно проверяет запись, чтобы не принять запросы раньше восстановления файлового пути.
TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза.
## Очистка и лимиты
До PUT создаётся upload_intent отдельным COMMIT. Публикация держит блокировку intent до записи Attachment и удаления intent одной транзакцией. При сбое остаётся журнал с известным object key.
Cleanup каждые 30 с выбирает intents старше 600 с через FOR UPDATE SKIP LOCKED. Активные загрузки пропускаются. Если готовых метаданных нет, удаляются объект и intent; при ошибке попытка повторится. Если метаданные есть, объект сохраняется. Готовые файлы без сообщения тоже сохраняются. Неизвестный результат COMMIT не вызывает слепое удаление.
На API — 2 активные загрузки, ожидание слота 15 с, затем 429/Retry-After. Чтение тела ограничено 15 с; S3 connect/read — 2/5 с, без автоматических повторов. Лимит API — 768 MiB. HAProxy: round-robin, health раз в 1 с, fall=2/rise=1, connect=1 с/check=3 с, клиентский путь 60 с, повторы POST выключены.
## Проверки
Полный локальный verify прошёл: [лог](evidence/verification.txt), [итог](evidence/summary.json).
| Проверка | Результат |
| --- | --- |
| Структура, OpenAPI, Compose, публичный smoke, 37 собственных тестов | PASS |
| Перезапуск API, БД, обоих компонентов S3, LB и cleanup; прежние токены, курсор, файл и сообщение | [PASS](evidence/persistence.json), [состояния контейнеров](evidence/restart.json) |
| Отказ каждой API, сохранность сообщений и отсутствие дублей | [PASS](evidence/failover.json) |
| Остановка именно загрузившего API, скачивание через другой | [PASS](evidence/attachment-failover.json); 4.805 / 4.552 с |
| Анонимный GET S3, чужой bucket, CreateBucket и изменение ACL ключом API | 403, [проверки прав и сети](evidence/cleanup.json) |
| Обрыв клиента, ошибка БД после PUT, реальный kill API после PUT, отказ S3 при очистке | Остатки очищены, свежие/заблокированные intents и ready-файлы сохранены |
| Повторные миграции и bootstrap, остановка БД и восстановление | PASS |
| TTL=2 в отдельном стенде, expiry после рестарта и logout | [PASS](evidence/expiry.json) |
Чистый клон коммита `74e0750d1895b11f9f2334cdaf08423fbf325a23` прошёл тот же полный цикл с новыми секретами, БД и S3 volumes. Git при начале проверки был чистым. Команда: `python -X utf8 scripts/lab.py verify --project mtusi-lab4-clean-final --port 18082 --test-port 18083`. [Итог](evidence/clean-clone-summary.json), [лог](evidence/clean-clone-verification.txt). Временный стенд после проверки остановлен.
Для сбоев БД тест создаёт временный триггер только для синтетического чата. Для проверки grace period состариваются только тестовые intents. Отладочных HTTP endpoint нет.
## Память
Отдельный проект, свежие API-контейнеры, 10 клиентов одновременно, по 2 активные обработки на реплику. Снимки cgroup/RSS раз в 50 мс и memory.peak; лимит 768 MiB. [Полные результаты](evidence/memory.json).
| Синтетический PNG | Успешных | Максимальный пик cgroup среди API |
| --- | --- | --- |
| 32×24 с дополнительным PNG chunk до 10 MiB − 1 KiB | 10/10 | 169.7 MiB |
| Однотонный 8000×5000 RGBA, 40 млн пикселей | 10/10 | 430.7 MiB |
Все оригиналы скачаны и сравнены. OOM и рестартов нет. В повторе из чистого клона пик составил 442,2 MiB: [результаты](evidence/clean-clone-memory.json). Это два локальных синтетических профиля, не измерение предельной производительности. Стенд: Windows, Docker 29.9.3, 12 CPU и 15,5 GiB RAM доступны Docker.
## Ограничения
Один Docker-host, одна БД и одно S3-хранилище: их постоянный отказ не покрыт. Очистка работает по журналу своего upload-протокола, без полного reconciler. Идемпотентны сообщения; повтор загрузки после потери ответа может создать ещё одно вложение. HTTP используется только для локального учебного стенда.
Документация: [SeaweedFS S3](https://github.com/seaweedfs/seaweedfs/wiki/Amazon-S3-API), [права S3](https://github.com/seaweedfs/seaweedfs/wiki/S3-Credentials), [Pillow](https://pillow.readthedocs.io/en/stable/reference/Image.html), [Boto3](https://docs.aws.amazon.com/boto3/latest/reference/services/s3.html).
+168
View File
@@ -0,0 +1,168 @@
name: mtusi-lab4
x-app: &app
image: mtusi-messenger:lab4
build: .
environment: &environment
APP_HOST: 0.0.0.0
APP_PORT: 8080
APP_ENV: lab
DATABASE_URL_FILE: /run/secrets/database_url
CURSOR_SECRET_FILE: /run/secrets/cursor_key
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600}
IDEMPOTENCY_TTL_SECONDS: ${IDEMPOTENCY_TTL_SECONDS:-86400}
S3_ENDPOINT: http://s3:9000
S3_REGION: us-east-1
S3_BUCKET: ${S3_BUCKET:-messenger-attachments}
S3_ACCESS_KEY_ID_FILE: /run/secrets/s3_access_key_id
S3_SECRET_ACCESS_KEY_FILE: /run/secrets/s3_secret_access_key
CLEANUP_INTERVAL_SECONDS: ${CLEANUP_INTERVAL_SECONDS:-30}
CLEANUP_GRACE_SECONDS: ${CLEANUP_GRACE_SECONDS:-600}
secrets:
- database_url
- cursor_key
- s3_access_key_id
- s3_secret_access_key
stop_grace_period: 3s
services:
db:
image: postgres:17.11-bookworm@sha256:665dc7b37ac52a17e298511414c21d23ffa4e6a6ef6225b3e5d59c7afb35e994
environment:
POSTGRES_USER: messenger
POSTGRES_DB: messenger
POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password
secrets:
- postgres_password
volumes:
- pgdata:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U messenger -d messenger"]
interval: 2s
timeout: 3s
retries: 30
stop_grace_period: 10s
s3-storage:
image: chrislusf/seaweedfs:4.54@sha256:4b42c212543a0acd4c0e675215ecdcaf62a186ef98def76c7e12701871222d15
command: ["server", "-filer", "-dir=/data", "-ip=s3-storage", "-ip.bind=0.0.0.0",
"-master.volumeSizeLimitMB=128", "-volume.max=64", "-master.electionTimeout=1s",
"-master.heartbeatInterval=100ms"]
volumes:
- s3data:/data
networks:
- storage
healthcheck:
test: ["CMD-SHELL", "wget -q -O - http://127.0.0.1:9333/cluster/status | grep -q '\"IsLeader\":true'"]
interval: 2s
timeout: 2s
retries: 45
stop_grace_period: 10s
s3:
image: chrislusf/seaweedfs:4.54@sha256:4b42c212543a0acd4c0e675215ecdcaf62a186ef98def76c7e12701871222d15
command: ["s3", "-filer=s3-storage:8888", "-port=9000", "-ip.bind=0.0.0.0",
"-config=/run/secrets/s3_config", "-iam=false", "-port.iceberg=0", "-port.lance=0"]
secrets:
- s3_config
networks:
- default
- storage
depends_on:
s3-storage:
condition: service_healthy
stop_grace_period: 3s
migrate:
<<: *app
command: ["python", "-m", "src.migrate"]
secrets:
- database_url
healthcheck:
disable: true
depends_on:
db:
condition: service_healthy
bootstrap:
<<: *app
command: ["python", "-m", "src.bootstrap"]
environment:
<<: *environment
S3_ADMIN_ACCESS_KEY_ID_FILE: /run/secrets/s3_admin_access_key_id
S3_ADMIN_SECRET_ACCESS_KEY_FILE: /run/secrets/s3_admin_secret_access_key
secrets:
- s3_access_key_id
- s3_secret_access_key
- s3_admin_access_key_id
- s3_admin_secret_access_key
healthcheck:
disable: true
depends_on:
s3:
condition: service_started
api1: &api
<<: *app
mem_limit: 768m
depends_on:
db:
condition: service_healthy
migrate:
condition: service_completed_successfully
bootstrap:
condition: service_completed_successfully
api2:
<<: *api
cleanup:
<<: *app
command: ["python", "-m", "src.cleanup"]
restart: unless-stopped
healthcheck:
disable: true
depends_on:
migrate:
condition: service_completed_successfully
bootstrap:
condition: service_completed_successfully
lb:
image: haproxy:3.2.26@sha256:1242e4006ac3f1ed15871efadca3d8d089e76f720017a1c51747133ee40cc953
ports:
- "127.0.0.1:${HTTP_PORT:-8080}:8080"
volumes:
- ./infra/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
depends_on:
api1:
condition: service_started
api2:
condition: service_started
stop_grace_period: 3s
volumes:
pgdata:
s3data:
networks:
storage:
internal: true
secrets:
postgres_password:
file: ./secrets/postgres_password
database_url:
file: ./secrets/database_url
cursor_key:
file: ./secrets/cursor_key
s3_config:
file: ./secrets/s3_config.json
s3_access_key_id:
file: ./secrets/s3_access_key_id
s3_secret_access_key:
file: ./secrets/s3_secret_access_key
s3_admin_access_key_id:
file: ./secrets/s3_admin_access_key_id
s3_admin_secret_access_key:
file: ./secrets/s3_admin_secret_access_key
+23
View File
@@ -0,0 +1,23 @@
{
"status": "PASS",
"cases": [
{
"stopped_service": "api1",
"upload_instance": "7f69d9826cae42aa945014734a7f9caa",
"download_instance": "dff2f0274e124a9fa6a264595912a404",
"recovery_seconds": 4.805,
"transitional_errors": 2,
"same_original": true,
"message_preserved": true
},
{
"stopped_service": "api2",
"upload_instance": "dff2f0274e124a9fa6a264595912a404",
"download_instance": "80f0af275acd4085a1ffc538a22458cc",
"recovery_seconds": 4.552,
"transitional_errors": 2,
"same_original": true,
"message_preserved": true
}
]
}
@@ -0,0 +1,23 @@
{
"status": "PASS",
"cases": [
{
"stopped_service": "api1",
"upload_instance": "a5a3a743e21e4bf0a4f8edce8ea76a5d",
"download_instance": "656eb4845ee3414890a69848c4f6dba8",
"recovery_seconds": 4.482,
"transitional_errors": 2,
"same_original": true,
"message_preserved": true
},
{
"stopped_service": "api2",
"upload_instance": "656eb4845ee3414890a69848c4f6dba8",
"download_instance": "fdc90c8ddce040a9a518418be30492df",
"recovery_seconds": 5.367,
"transitional_errors": 3,
"same_original": true,
"message_preserved": true
}
]
}
+23
View File
@@ -0,0 +1,23 @@
{
"privacy": {
"anonymous_get": 403,
"other_bucket_get": 403,
"create_bucket": 403,
"change_acl": 403,
"bootstrap_replay": "PASS",
"storage_network_isolated": true,
"internal_ports_not_published": true
},
"fresh_intent_protected": "PASS",
"locked_intent_skipped": "PASS",
"orphan_removed_and_repeat_safe": "PASS",
"ready_unlinked_attachment_protected": "PASS",
"sql_failure_after_put_compensated": "PASS",
"api_killed_after_put_compensated": {
"status": "PASS",
"service": "api2"
},
"s3_outage_keeps_intent_then_recovers": "PASS",
"client_disconnect_has_no_publication": "PASS",
"periodic_cleanup_worker": "PASS"
}
+7
View File
@@ -0,0 +1,7 @@
{
"ttl_seconds": 2,
"expiry_unchanged_on_restart": true,
"before_restart": 200,
"after_expiry_and_restart": 401,
"new_login": 201
}
+173
View File
@@ -0,0 +1,173 @@
{
"public": {
"service": "api1",
"target_instance": "6c9632529d554184a8a3402b61af9b6e",
"before": {
"api1": {
"instance_id": "6c9632529d554184a8a3402b61af9b6e",
"container_id": "651db4cfc1d4bcd935cb354b66f5018747ebfebeb10aeb98c0a2e18e4a23669a"
},
"api2": {
"instance_id": "71ef8d2668194a41b4dfb5fa678c30e8",
"container_id": "0e20c8a5d15f411cd3807858e8eb198252877c683faff99545fe97da8e124f23"
}
},
"after": {
"api1": {
"id": "651db4cfc1d4bcd935cb354b66f5018747ebfebeb10aeb98c0a2e18e4a23669a",
"image": "sha256:88af0a676201721091455f062d2322cc8a3074f3899dc9c8f878a9c69cdbb908",
"started_at": "2026-11-16T17:23:14.672749421Z",
"running": false,
"user": "messenger",
"volumes": []
},
"api2": {
"id": "0e20c8a5d15f411cd3807858e8eb198252877c683faff99545fe97da8e124f23",
"image": "sha256:88af0a676201721091455f062d2322cc8a3074f3899dc9c8f878a9c69cdbb908",
"started_at": "2026-11-16T17:23:14.597010676Z",
"running": true,
"user": "messenger",
"volumes": []
},
"cleanup": {
"id": "ee0d1bd09495bf1a033549d049440621d61c2b06d23e5bf5de03f0c8b2f69ed8",
"image": "sha256:88af0a676201721091455f062d2322cc8a3074f3899dc9c8f878a9c69cdbb908",
"started_at": "2026-11-16T17:23:24.131692195Z",
"running": true,
"user": "messenger",
"volumes": []
},
"db": {
"id": "4898b2c4c8e0a1bacc7ebcddb7a79e838198fcc5b97727a037ef8b90d66d7070",
"image": "sha256:665dc7b37ac52a17e298511414c21d23ffa4e6a6ef6225b3e5d59c7afb35e994",
"started_at": "2026-11-16T17:23:14.386659112Z",
"running": true,
"user": "",
"volumes": [
"mtusi-lab4-clean-final_pgdata"
]
},
"lb": {
"id": "069c6bb05a7eaf930c026a80530e0c80444753543be323f3ed1b47aafecb4924",
"image": "sha256:1242e4006ac3f1ed15871efadca3d8d089e76f720017a1c51747133ee40cc953",
"started_at": "2026-11-16T17:23:14.266006028Z",
"running": true,
"user": "haproxy",
"volumes": []
},
"s3": {
"id": "9c78cdb5cf993847388bae8d64536acb4c47f18f86337b5a5edadeb20ac5ae64",
"image": "sha256:4b42c212543a0acd4c0e675215ecdcaf62a186ef98def76c7e12701871222d15",
"started_at": "2026-11-16T17:23:14.518159853Z",
"running": true,
"user": "",
"volumes": [
"008d3a7b4ab5027e77c81a992938c4278b09539463fdcd94257a1d7ec758ddf4"
]
},
"s3-storage": {
"id": "eb590a785d4203ccd32106b683385e0e1070cbda0fc346550546d0d294742801",
"image": "sha256:4b42c212543a0acd4c0e675215ecdcaf62a186ef98def76c7e12701871222d15",
"started_at": "2026-11-16T17:23:24.180663379Z",
"running": true,
"user": "",
"volumes": [
"mtusi-lab4-clean-final_s3data"
]
}
},
"stop_seconds": 1.072,
"metrics": {
"observed_instances_before": 2,
"successful_writes": 105,
"failed_attempts": 4,
"first_recovery_seconds": 2.933
}
},
"second_replica": {
"service": "api2",
"target_instance": "71ef8d2668194a41b4dfb5fa678c30e8",
"before": {
"api1": {
"instance_id": "a5a3a743e21e4bf0a4f8edce8ea76a5d",
"container_id": "651db4cfc1d4bcd935cb354b66f5018747ebfebeb10aeb98c0a2e18e4a23669a"
},
"api2": {
"instance_id": "71ef8d2668194a41b4dfb5fa678c30e8",
"container_id": "0e20c8a5d15f411cd3807858e8eb198252877c683faff99545fe97da8e124f23"
}
},
"after": {
"api1": {
"id": "651db4cfc1d4bcd935cb354b66f5018747ebfebeb10aeb98c0a2e18e4a23669a",
"image": "sha256:88af0a676201721091455f062d2322cc8a3074f3899dc9c8f878a9c69cdbb908",
"started_at": "2026-11-16T17:24:03.583043821Z",
"running": true,
"user": "messenger",
"volumes": []
},
"api2": {
"id": "0e20c8a5d15f411cd3807858e8eb198252877c683faff99545fe97da8e124f23",
"image": "sha256:88af0a676201721091455f062d2322cc8a3074f3899dc9c8f878a9c69cdbb908",
"started_at": "2026-11-16T17:23:14.597010676Z",
"running": false,
"user": "messenger",
"volumes": []
},
"cleanup": {
"id": "ee0d1bd09495bf1a033549d049440621d61c2b06d23e5bf5de03f0c8b2f69ed8",
"image": "sha256:88af0a676201721091455f062d2322cc8a3074f3899dc9c8f878a9c69cdbb908",
"started_at": "2026-11-16T17:23:24.131692195Z",
"running": true,
"user": "messenger",
"volumes": []
},
"db": {
"id": "4898b2c4c8e0a1bacc7ebcddb7a79e838198fcc5b97727a037ef8b90d66d7070",
"image": "sha256:665dc7b37ac52a17e298511414c21d23ffa4e6a6ef6225b3e5d59c7afb35e994",
"started_at": "2026-11-16T17:23:14.386659112Z",
"running": true,
"user": "",
"volumes": [
"mtusi-lab4-clean-final_pgdata"
]
},
"lb": {
"id": "069c6bb05a7eaf930c026a80530e0c80444753543be323f3ed1b47aafecb4924",
"image": "sha256:1242e4006ac3f1ed15871efadca3d8d089e76f720017a1c51747133ee40cc953",
"started_at": "2026-11-16T17:23:14.266006028Z",
"running": true,
"user": "haproxy",
"volumes": []
},
"s3": {
"id": "9c78cdb5cf993847388bae8d64536acb4c47f18f86337b5a5edadeb20ac5ae64",
"image": "sha256:4b42c212543a0acd4c0e675215ecdcaf62a186ef98def76c7e12701871222d15",
"started_at": "2026-11-16T17:23:14.518159853Z",
"running": true,
"user": "",
"volumes": [
"008d3a7b4ab5027e77c81a992938c4278b09539463fdcd94257a1d7ec758ddf4"
]
},
"s3-storage": {
"id": "eb590a785d4203ccd32106b683385e0e1070cbda0fc346550546d0d294742801",
"image": "sha256:4b42c212543a0acd4c0e675215ecdcaf62a186ef98def76c7e12701871222d15",
"started_at": "2026-11-16T17:23:24.180663379Z",
"running": true,
"user": "",
"volumes": [
"mtusi-lab4-clean-final_s3data"
]
}
},
"stop_seconds": 1.077,
"metrics": {
"successful_writes": 105,
"failed_attempts": 5,
"first_recovery_seconds": 2.989,
"replay_on_survivor": "PASS"
}
},
"status": "PASS"
}
+92
View File
@@ -0,0 +1,92 @@
{
"clients": 10,
"upload_slots_per_replica": 2,
"sampling_seconds": 0.05,
"profiles": [
{
"name": "near_byte_limit",
"file_bytes": 10484736,
"dimensions": [
32,
24
],
"successes": 10,
"errors": 0,
"seconds": 1.737,
"slowest_upload_seconds": 0.754,
"replicas_used": 2,
"originals_verified": 10,
"memory": {
"api1": {
"sampled_cgroup_peak_bytes": 175509504,
"sampled_rss_peak_bytes": 164024320,
"cgroup_lifetime_peak_bytes": 181035008,
"process_lifetime_hwm_bytes": 164024320,
"memory_limit_bytes": 805306368,
"oom_kills": 0
},
"api2": {
"sampled_cgroup_peak_bytes": 175394816,
"sampled_rss_peak_bytes": 164462592,
"cgroup_lifetime_peak_bytes": 181207040,
"process_lifetime_hwm_bytes": 164462592,
"memory_limit_bytes": 805306368,
"oom_kills": 0
}
}
},
{
"name": "near_pixel_limit",
"file_bytes": 165446,
"dimensions": [
8000,
5000
],
"successes": 10,
"errors": 0,
"seconds": 1.755,
"slowest_upload_seconds": 1.479,
"replicas_used": 2,
"originals_verified": 10,
"memory": {
"api1": {
"sampled_cgroup_peak_bytes": 445038592,
"sampled_rss_peak_bytes": 454270976,
"cgroup_lifetime_peak_bytes": 455401472,
"process_lifetime_hwm_bytes": 464388096,
"memory_limit_bytes": 805306368,
"oom_kills": 0
},
"api2": {
"sampled_cgroup_peak_bytes": 445296640,
"sampled_rss_peak_bytes": 454819840,
"cgroup_lifetime_peak_bytes": 463646720,
"process_lifetime_hwm_bytes": 471842816,
"memory_limit_bytes": 805306368,
"oom_kills": 0
}
}
}
],
"baseline": {
"api1": {
"current": 107724800,
"peak": 111403008,
"limit": 805306368,
"rss": 117374976,
"hwm": 117374976,
"oom_kill": 0,
"received_at": 344460.6947028
},
"api2": {
"current": 107102208,
"peak": 110735360,
"limit": 805306368,
"rss": 117305344,
"hwm": 117305344,
"oom_kill": 0,
"received_at": 344460.6924973
}
},
"status": "PASS"
}
+22
View File
@@ -0,0 +1,22 @@
{
"idempotency_after_restart": "PASS",
"public_persistence": "PASS",
"cursor_after_restart": "PASS",
"logout_after_restart": "PASS",
"migration_replay": "PASS",
"database": {
"users": 118,
"chats": 42,
"members": 82,
"messages": 138,
"sessions": 114,
"migrations": 3,
"idempotency_keys": 137,
"attachments": 14,
"message_attachments": 8,
"upload_intents": 0,
"expiry_fingerprint": "2fd4e27f71b0c5350a83baff44d8f06d",
"passwords_are_argon2id": true,
"session_hashes_only": true
}
}
+131
View File
@@ -0,0 +1,131 @@
{
"project": "mtusi-lab4-clean-final",
"before": {
"api1": {
"id": "651db4cfc1d4bcd935cb354b66f5018747ebfebeb10aeb98c0a2e18e4a23669a",
"image": "sha256:88af0a676201721091455f062d2322cc8a3074f3899dc9c8f878a9c69cdbb908",
"started_at": "2026-11-16T17:21:34.919170061Z",
"running": true,
"user": "messenger",
"volumes": []
},
"api2": {
"id": "0e20c8a5d15f411cd3807858e8eb198252877c683faff99545fe97da8e124f23",
"image": "sha256:88af0a676201721091455f062d2322cc8a3074f3899dc9c8f878a9c69cdbb908",
"started_at": "2026-11-16T17:22:34.979734202Z",
"running": true,
"user": "messenger",
"volumes": []
},
"cleanup": {
"id": "ee0d1bd09495bf1a033549d049440621d61c2b06d23e5bf5de03f0c8b2f69ed8",
"image": "sha256:88af0a676201721091455f062d2322cc8a3074f3899dc9c8f878a9c69cdbb908",
"started_at": "2026-11-16T17:23:05.647019018Z",
"running": true,
"user": "messenger",
"volumes": []
},
"db": {
"id": "4898b2c4c8e0a1bacc7ebcddb7a79e838198fcc5b97727a037ef8b90d66d7070",
"image": "sha256:665dc7b37ac52a17e298511414c21d23ffa4e6a6ef6225b3e5d59c7afb35e994",
"started_at": "2026-11-16T17:21:23.239452374Z",
"running": true,
"user": "",
"volumes": [
"mtusi-lab4-clean-final_pgdata"
]
},
"lb": {
"id": "069c6bb05a7eaf930c026a80530e0c80444753543be323f3ed1b47aafecb4924",
"image": "sha256:1242e4006ac3f1ed15871efadca3d8d089e76f720017a1c51747133ee40cc953",
"started_at": "2026-11-16T17:21:35.383749519Z",
"running": true,
"user": "haproxy",
"volumes": []
},
"s3": {
"id": "9c78cdb5cf993847388bae8d64536acb4c47f18f86337b5a5edadeb20ac5ae64",
"image": "sha256:4b42c212543a0acd4c0e675215ecdcaf62a186ef98def76c7e12701871222d15",
"started_at": "2026-11-16T17:22:54.547045217Z",
"running": true,
"user": "",
"volumes": [
"008d3a7b4ab5027e77c81a992938c4278b09539463fdcd94257a1d7ec758ddf4"
]
},
"s3-storage": {
"id": "eb590a785d4203ccd32106b683385e0e1070cbda0fc346550546d0d294742801",
"image": "sha256:4b42c212543a0acd4c0e675215ecdcaf62a186ef98def76c7e12701871222d15",
"started_at": "2026-11-16T17:21:23.408040603Z",
"running": true,
"user": "",
"volumes": [
"mtusi-lab4-clean-final_s3data"
]
}
},
"after": {
"api1": {
"id": "651db4cfc1d4bcd935cb354b66f5018747ebfebeb10aeb98c0a2e18e4a23669a",
"image": "sha256:88af0a676201721091455f062d2322cc8a3074f3899dc9c8f878a9c69cdbb908",
"started_at": "2026-11-16T17:23:14.672749421Z",
"running": true,
"user": "messenger",
"volumes": []
},
"api2": {
"id": "0e20c8a5d15f411cd3807858e8eb198252877c683faff99545fe97da8e124f23",
"image": "sha256:88af0a676201721091455f062d2322cc8a3074f3899dc9c8f878a9c69cdbb908",
"started_at": "2026-11-16T17:23:14.597010676Z",
"running": true,
"user": "messenger",
"volumes": []
},
"cleanup": {
"id": "ee0d1bd09495bf1a033549d049440621d61c2b06d23e5bf5de03f0c8b2f69ed8",
"image": "sha256:88af0a676201721091455f062d2322cc8a3074f3899dc9c8f878a9c69cdbb908",
"started_at": "2026-11-16T17:23:24.131692195Z",
"running": true,
"user": "messenger",
"volumes": []
},
"db": {
"id": "4898b2c4c8e0a1bacc7ebcddb7a79e838198fcc5b97727a037ef8b90d66d7070",
"image": "sha256:665dc7b37ac52a17e298511414c21d23ffa4e6a6ef6225b3e5d59c7afb35e994",
"started_at": "2026-11-16T17:23:14.386659112Z",
"running": true,
"user": "",
"volumes": [
"mtusi-lab4-clean-final_pgdata"
]
},
"lb": {
"id": "069c6bb05a7eaf930c026a80530e0c80444753543be323f3ed1b47aafecb4924",
"image": "sha256:1242e4006ac3f1ed15871efadca3d8d089e76f720017a1c51747133ee40cc953",
"started_at": "2026-11-16T17:23:14.266006028Z",
"running": true,
"user": "haproxy",
"volumes": []
},
"s3": {
"id": "9c78cdb5cf993847388bae8d64536acb4c47f18f86337b5a5edadeb20ac5ae64",
"image": "sha256:4b42c212543a0acd4c0e675215ecdcaf62a186ef98def76c7e12701871222d15",
"started_at": "2026-11-16T17:23:14.518159853Z",
"running": true,
"user": "",
"volumes": [
"008d3a7b4ab5027e77c81a992938c4278b09539463fdcd94257a1d7ec758ddf4"
]
},
"s3-storage": {
"id": "eb590a785d4203ccd32106b683385e0e1070cbda0fc346550546d0d294742801",
"image": "sha256:4b42c212543a0acd4c0e675215ecdcaf62a186ef98def76c7e12701871222d15",
"started_at": "2026-11-16T17:23:24.180663379Z",
"running": true,
"user": "",
"volumes": [
"mtusi-lab4-clean-final_s3data"
]
}
}
}
+79
View File
@@ -0,0 +1,79 @@
{
"service": "api2",
"target_instance": "656eb4845ee3414890a69848c4f6dba8",
"before": {
"api1": {
"instance_id": "fdc90c8ddce040a9a518418be30492df",
"container_id": "651db4cfc1d4bcd935cb354b66f5018747ebfebeb10aeb98c0a2e18e4a23669a"
},
"api2": {
"instance_id": "656eb4845ee3414890a69848c4f6dba8",
"container_id": "0e20c8a5d15f411cd3807858e8eb198252877c683faff99545fe97da8e124f23"
}
},
"after": {
"api1": {
"id": "651db4cfc1d4bcd935cb354b66f5018747ebfebeb10aeb98c0a2e18e4a23669a",
"image": "sha256:88af0a676201721091455f062d2322cc8a3074f3899dc9c8f878a9c69cdbb908",
"started_at": "2026-11-16T17:24:32.636188849Z",
"running": true,
"user": "messenger",
"volumes": []
},
"api2": {
"id": "0e20c8a5d15f411cd3807858e8eb198252877c683faff99545fe97da8e124f23",
"image": "sha256:88af0a676201721091455f062d2322cc8a3074f3899dc9c8f878a9c69cdbb908",
"started_at": "2026-11-16T17:24:22.6139249Z",
"running": false,
"user": "messenger",
"volumes": []
},
"cleanup": {
"id": "ee0d1bd09495bf1a033549d049440621d61c2b06d23e5bf5de03f0c8b2f69ed8",
"image": "sha256:88af0a676201721091455f062d2322cc8a3074f3899dc9c8f878a9c69cdbb908",
"started_at": "2026-11-16T17:23:24.131692195Z",
"running": true,
"user": "messenger",
"volumes": []
},
"db": {
"id": "4898b2c4c8e0a1bacc7ebcddb7a79e838198fcc5b97727a037ef8b90d66d7070",
"image": "sha256:665dc7b37ac52a17e298511414c21d23ffa4e6a6ef6225b3e5d59c7afb35e994",
"started_at": "2026-11-16T17:23:14.386659112Z",
"running": true,
"user": "",
"volumes": [
"mtusi-lab4-clean-final_pgdata"
]
},
"lb": {
"id": "069c6bb05a7eaf930c026a80530e0c80444753543be323f3ed1b47aafecb4924",
"image": "sha256:1242e4006ac3f1ed15871efadca3d8d089e76f720017a1c51747133ee40cc953",
"started_at": "2026-11-16T17:23:14.266006028Z",
"running": true,
"user": "haproxy",
"volumes": []
},
"s3": {
"id": "9c78cdb5cf993847388bae8d64536acb4c47f18f86337b5a5edadeb20ac5ae64",
"image": "sha256:4b42c212543a0acd4c0e675215ecdcaf62a186ef98def76c7e12701871222d15",
"started_at": "2026-11-16T17:23:14.518159853Z",
"running": true,
"user": "",
"volumes": [
"008d3a7b4ab5027e77c81a992938c4278b09539463fdcd94257a1d7ec758ddf4"
]
},
"s3-storage": {
"id": "eb590a785d4203ccd32106b683385e0e1070cbda0fc346550546d0d294742801",
"image": "sha256:4b42c212543a0acd4c0e675215ecdcaf62a186ef98def76c7e12701871222d15",
"started_at": "2026-11-16T17:23:24.180663379Z",
"running": true,
"user": "",
"volumes": [
"mtusi-lab4-clean-final_s3data"
]
}
},
"stop_seconds": 1.02
}
+423
View File
@@ -0,0 +1,423 @@
{
"date_utc": "2026-11-16T17:20:26.228906+00:00",
"platform": "Windows-11-10.0.26200-SP0",
"source_commit": "74e0750d1895b11f9f2334cdaf08423fbf325a23",
"worktree_dirty_at_start": false,
"project": "mtusi-lab4-clean-final",
"status": "PASS",
"cleanup": {
"privacy": {
"anonymous_get": 403,
"other_bucket_get": 403,
"create_bucket": 403,
"change_acl": 403,
"bootstrap_replay": "PASS",
"storage_network_isolated": true,
"internal_ports_not_published": true
},
"fresh_intent_protected": "PASS",
"locked_intent_skipped": "PASS",
"orphan_removed_and_repeat_safe": "PASS",
"ready_unlinked_attachment_protected": "PASS",
"sql_failure_after_put_compensated": "PASS",
"api_killed_after_put_compensated": {
"status": "PASS",
"service": "api2"
},
"s3_outage_keeps_intent_then_recovers": "PASS",
"client_disconnect_has_no_publication": "PASS",
"periodic_cleanup_worker": "PASS"
},
"persistence": {
"idempotency_after_restart": "PASS",
"public_persistence": "PASS",
"cursor_after_restart": "PASS",
"logout_after_restart": "PASS",
"migration_replay": "PASS",
"database": {
"users": 118,
"chats": 42,
"members": 82,
"messages": 138,
"sessions": 114,
"migrations": 3,
"idempotency_keys": 137,
"attachments": 14,
"message_attachments": 8,
"upload_intents": 0,
"expiry_fingerprint": "2fd4e27f71b0c5350a83baff44d8f06d",
"passwords_are_argon2id": true,
"session_hashes_only": true
}
},
"failover": {
"public": {
"service": "api1",
"target_instance": "6c9632529d554184a8a3402b61af9b6e",
"before": {
"api1": {
"instance_id": "6c9632529d554184a8a3402b61af9b6e",
"container_id": "651db4cfc1d4bcd935cb354b66f5018747ebfebeb10aeb98c0a2e18e4a23669a"
},
"api2": {
"instance_id": "71ef8d2668194a41b4dfb5fa678c30e8",
"container_id": "0e20c8a5d15f411cd3807858e8eb198252877c683faff99545fe97da8e124f23"
}
},
"after": {
"api1": {
"id": "651db4cfc1d4bcd935cb354b66f5018747ebfebeb10aeb98c0a2e18e4a23669a",
"image": "sha256:88af0a676201721091455f062d2322cc8a3074f3899dc9c8f878a9c69cdbb908",
"started_at": "2026-11-16T17:23:14.672749421Z",
"running": false,
"user": "messenger",
"volumes": []
},
"api2": {
"id": "0e20c8a5d15f411cd3807858e8eb198252877c683faff99545fe97da8e124f23",
"image": "sha256:88af0a676201721091455f062d2322cc8a3074f3899dc9c8f878a9c69cdbb908",
"started_at": "2026-11-16T17:23:14.597010676Z",
"running": true,
"user": "messenger",
"volumes": []
},
"cleanup": {
"id": "ee0d1bd09495bf1a033549d049440621d61c2b06d23e5bf5de03f0c8b2f69ed8",
"image": "sha256:88af0a676201721091455f062d2322cc8a3074f3899dc9c8f878a9c69cdbb908",
"started_at": "2026-11-16T17:23:24.131692195Z",
"running": true,
"user": "messenger",
"volumes": []
},
"db": {
"id": "4898b2c4c8e0a1bacc7ebcddb7a79e838198fcc5b97727a037ef8b90d66d7070",
"image": "sha256:665dc7b37ac52a17e298511414c21d23ffa4e6a6ef6225b3e5d59c7afb35e994",
"started_at": "2026-11-16T17:23:14.386659112Z",
"running": true,
"user": "",
"volumes": [
"mtusi-lab4-clean-final_pgdata"
]
},
"lb": {
"id": "069c6bb05a7eaf930c026a80530e0c80444753543be323f3ed1b47aafecb4924",
"image": "sha256:1242e4006ac3f1ed15871efadca3d8d089e76f720017a1c51747133ee40cc953",
"started_at": "2026-11-16T17:23:14.266006028Z",
"running": true,
"user": "haproxy",
"volumes": []
},
"s3": {
"id": "9c78cdb5cf993847388bae8d64536acb4c47f18f86337b5a5edadeb20ac5ae64",
"image": "sha256:4b42c212543a0acd4c0e675215ecdcaf62a186ef98def76c7e12701871222d15",
"started_at": "2026-11-16T17:23:14.518159853Z",
"running": true,
"user": "",
"volumes": [
"008d3a7b4ab5027e77c81a992938c4278b09539463fdcd94257a1d7ec758ddf4"
]
},
"s3-storage": {
"id": "eb590a785d4203ccd32106b683385e0e1070cbda0fc346550546d0d294742801",
"image": "sha256:4b42c212543a0acd4c0e675215ecdcaf62a186ef98def76c7e12701871222d15",
"started_at": "2026-11-16T17:23:24.180663379Z",
"running": true,
"user": "",
"volumes": [
"mtusi-lab4-clean-final_s3data"
]
}
},
"stop_seconds": 1.072,
"metrics": {
"observed_instances_before": 2,
"successful_writes": 105,
"failed_attempts": 4,
"first_recovery_seconds": 2.933
}
},
"second_replica": {
"service": "api2",
"target_instance": "71ef8d2668194a41b4dfb5fa678c30e8",
"before": {
"api1": {
"instance_id": "a5a3a743e21e4bf0a4f8edce8ea76a5d",
"container_id": "651db4cfc1d4bcd935cb354b66f5018747ebfebeb10aeb98c0a2e18e4a23669a"
},
"api2": {
"instance_id": "71ef8d2668194a41b4dfb5fa678c30e8",
"container_id": "0e20c8a5d15f411cd3807858e8eb198252877c683faff99545fe97da8e124f23"
}
},
"after": {
"api1": {
"id": "651db4cfc1d4bcd935cb354b66f5018747ebfebeb10aeb98c0a2e18e4a23669a",
"image": "sha256:88af0a676201721091455f062d2322cc8a3074f3899dc9c8f878a9c69cdbb908",
"started_at": "2026-11-16T17:24:03.583043821Z",
"running": true,
"user": "messenger",
"volumes": []
},
"api2": {
"id": "0e20c8a5d15f411cd3807858e8eb198252877c683faff99545fe97da8e124f23",
"image": "sha256:88af0a676201721091455f062d2322cc8a3074f3899dc9c8f878a9c69cdbb908",
"started_at": "2026-11-16T17:23:14.597010676Z",
"running": false,
"user": "messenger",
"volumes": []
},
"cleanup": {
"id": "ee0d1bd09495bf1a033549d049440621d61c2b06d23e5bf5de03f0c8b2f69ed8",
"image": "sha256:88af0a676201721091455f062d2322cc8a3074f3899dc9c8f878a9c69cdbb908",
"started_at": "2026-11-16T17:23:24.131692195Z",
"running": true,
"user": "messenger",
"volumes": []
},
"db": {
"id": "4898b2c4c8e0a1bacc7ebcddb7a79e838198fcc5b97727a037ef8b90d66d7070",
"image": "sha256:665dc7b37ac52a17e298511414c21d23ffa4e6a6ef6225b3e5d59c7afb35e994",
"started_at": "2026-11-16T17:23:14.386659112Z",
"running": true,
"user": "",
"volumes": [
"mtusi-lab4-clean-final_pgdata"
]
},
"lb": {
"id": "069c6bb05a7eaf930c026a80530e0c80444753543be323f3ed1b47aafecb4924",
"image": "sha256:1242e4006ac3f1ed15871efadca3d8d089e76f720017a1c51747133ee40cc953",
"started_at": "2026-11-16T17:23:14.266006028Z",
"running": true,
"user": "haproxy",
"volumes": []
},
"s3": {
"id": "9c78cdb5cf993847388bae8d64536acb4c47f18f86337b5a5edadeb20ac5ae64",
"image": "sha256:4b42c212543a0acd4c0e675215ecdcaf62a186ef98def76c7e12701871222d15",
"started_at": "2026-11-16T17:23:14.518159853Z",
"running": true,
"user": "",
"volumes": [
"008d3a7b4ab5027e77c81a992938c4278b09539463fdcd94257a1d7ec758ddf4"
]
},
"s3-storage": {
"id": "eb590a785d4203ccd32106b683385e0e1070cbda0fc346550546d0d294742801",
"image": "sha256:4b42c212543a0acd4c0e675215ecdcaf62a186ef98def76c7e12701871222d15",
"started_at": "2026-11-16T17:23:24.180663379Z",
"running": true,
"user": "",
"volumes": [
"mtusi-lab4-clean-final_s3data"
]
}
},
"stop_seconds": 1.077,
"metrics": {
"successful_writes": 105,
"failed_attempts": 5,
"first_recovery_seconds": 2.989,
"replay_on_survivor": "PASS"
}
},
"status": "PASS"
},
"attachments_failover": {
"status": "PASS",
"cases": [
{
"stopped_service": "api1",
"upload_instance": "a5a3a743e21e4bf0a4f8edce8ea76a5d",
"download_instance": "656eb4845ee3414890a69848c4f6dba8",
"recovery_seconds": 4.482,
"transitional_errors": 2,
"same_original": true,
"message_preserved": true
},
{
"stopped_service": "api2",
"upload_instance": "656eb4845ee3414890a69848c4f6dba8",
"download_instance": "fdc90c8ddce040a9a518418be30492df",
"recovery_seconds": 5.367,
"transitional_errors": 3,
"same_original": true,
"message_preserved": true
}
]
},
"expiry": {
"ttl_seconds": 2,
"expiry_unchanged_on_restart": true,
"before_restart": 200,
"after_expiry_and_restart": 401,
"new_login": 201
},
"memory": {
"clients": 10,
"upload_slots_per_replica": 2,
"sampling_seconds": 0.05,
"profiles": [
{
"name": "near_byte_limit",
"file_bytes": 10484736,
"dimensions": [
32,
24
],
"successes": 10,
"errors": 0,
"seconds": 1.737,
"slowest_upload_seconds": 0.754,
"replicas_used": 2,
"originals_verified": 10,
"memory": {
"api1": {
"sampled_cgroup_peak_bytes": 175509504,
"sampled_rss_peak_bytes": 164024320,
"cgroup_lifetime_peak_bytes": 181035008,
"process_lifetime_hwm_bytes": 164024320,
"memory_limit_bytes": 805306368,
"oom_kills": 0
},
"api2": {
"sampled_cgroup_peak_bytes": 175394816,
"sampled_rss_peak_bytes": 164462592,
"cgroup_lifetime_peak_bytes": 181207040,
"process_lifetime_hwm_bytes": 164462592,
"memory_limit_bytes": 805306368,
"oom_kills": 0
}
}
},
{
"name": "near_pixel_limit",
"file_bytes": 165446,
"dimensions": [
8000,
5000
],
"successes": 10,
"errors": 0,
"seconds": 1.755,
"slowest_upload_seconds": 1.479,
"replicas_used": 2,
"originals_verified": 10,
"memory": {
"api1": {
"sampled_cgroup_peak_bytes": 445038592,
"sampled_rss_peak_bytes": 454270976,
"cgroup_lifetime_peak_bytes": 455401472,
"process_lifetime_hwm_bytes": 464388096,
"memory_limit_bytes": 805306368,
"oom_kills": 0
},
"api2": {
"sampled_cgroup_peak_bytes": 445296640,
"sampled_rss_peak_bytes": 454819840,
"cgroup_lifetime_peak_bytes": 463646720,
"process_lifetime_hwm_bytes": 471842816,
"memory_limit_bytes": 805306368,
"oom_kills": 0
}
}
}
],
"baseline": {
"api1": {
"current": 107724800,
"peak": 111403008,
"limit": 805306368,
"rss": 117374976,
"hwm": 117374976,
"oom_kill": 0,
"received_at": 344460.6947028
},
"api2": {
"current": 107102208,
"peak": 110735360,
"limit": 805306368,
"rss": 117305344,
"hwm": 117305344,
"oom_kill": 0,
"received_at": 344460.6924973
}
},
"status": "PASS"
},
"dependency": {
"live_without_db": 200,
"ready_without_db": 503,
"business_without_db": 503,
"recovered": 200
},
"containers": {
"api1": {
"id": "651db4cfc1d4bcd935cb354b66f5018747ebfebeb10aeb98c0a2e18e4a23669a",
"image": "sha256:88af0a676201721091455f062d2322cc8a3074f3899dc9c8f878a9c69cdbb908",
"started_at": "2026-11-16T17:24:32.636188849Z",
"running": true,
"user": "messenger",
"volumes": []
},
"api2": {
"id": "0e20c8a5d15f411cd3807858e8eb198252877c683faff99545fe97da8e124f23",
"image": "sha256:88af0a676201721091455f062d2322cc8a3074f3899dc9c8f878a9c69cdbb908",
"started_at": "2026-11-16T17:24:42.404599852Z",
"running": true,
"user": "messenger",
"volumes": []
},
"cleanup": {
"id": "ee0d1bd09495bf1a033549d049440621d61c2b06d23e5bf5de03f0c8b2f69ed8",
"image": "sha256:88af0a676201721091455f062d2322cc8a3074f3899dc9c8f878a9c69cdbb908",
"started_at": "2026-11-16T17:23:24.131692195Z",
"running": true,
"user": "messenger",
"volumes": []
},
"db": {
"id": "4898b2c4c8e0a1bacc7ebcddb7a79e838198fcc5b97727a037ef8b90d66d7070",
"image": "sha256:665dc7b37ac52a17e298511414c21d23ffa4e6a6ef6225b3e5d59c7afb35e994",
"started_at": "2026-11-16T17:26:18.863944208Z",
"running": true,
"user": "",
"volumes": [
"mtusi-lab4-clean-final_pgdata"
]
},
"lb": {
"id": "069c6bb05a7eaf930c026a80530e0c80444753543be323f3ed1b47aafecb4924",
"image": "sha256:1242e4006ac3f1ed15871efadca3d8d089e76f720017a1c51747133ee40cc953",
"started_at": "2026-11-16T17:23:14.266006028Z",
"running": true,
"user": "haproxy",
"volumes": []
},
"s3": {
"id": "9c78cdb5cf993847388bae8d64536acb4c47f18f86337b5a5edadeb20ac5ae64",
"image": "sha256:4b42c212543a0acd4c0e675215ecdcaf62a186ef98def76c7e12701871222d15",
"started_at": "2026-11-16T17:23:14.518159853Z",
"running": true,
"user": "",
"volumes": [
"008d3a7b4ab5027e77c81a992938c4278b09539463fdcd94257a1d7ec758ddf4"
]
},
"s3-storage": {
"id": "eb590a785d4203ccd32106b683385e0e1070cbda0fc346550546d0d294742801",
"image": "sha256:4b42c212543a0acd4c0e675215ecdcaf62a186ef98def76c7e12701871222d15",
"started_at": "2026-11-16T17:23:24.180663379Z",
"running": true,
"user": "",
"volumes": [
"mtusi-lab4-clean-final_s3data"
]
}
},
"docker": "29.9.3",
"resources": {
"cpus": 12,
"memory_bytes": 16591364096
}
}
+528
View File
@@ -0,0 +1,528 @@
$ .\.venv\Scripts\python.exe -X utf8 -m pip install --disable-pip-version-check --no-input --progress-bar off --quiet -r .\requirements.lock -r .\requirements-validation.lock
Environment ready; existing settings and secrets preserved
$ .\.venv\Scripts\python.exe -X utf8 scripts/check_contract.py
PASS: offline skeleton structure, local refs and Python syntax; application NOT tested.
$ .\.venv\Scripts\python.exe -X utf8 scripts/validate_openapi.py
PASS: full OpenAPI 3.1 and JSON Schema validation; application NOT tested.
$ docker compose --project-name mtusi-lab4-clean-final --env-file .env config --quiet
$ docker compose --project-name mtusi-lab4-clean-final --env-file .env config --quiet
$ docker compose --project-name mtusi-lab4-clean-final --env-file .env build api1
Image mtusi-messenger:lab4 Building
#1 [internal] load local bake definitions
#1 reading from stdin 1.72kB 0.0s done
#1 DONE 0.0s
#2 [internal] load build definition from Dockerfile
#2 transferring dockerfile: 728B done
#2 DONE 0.0s
#3 [internal] load metadata for docker.io/library/python:3.13.14-slim-bookworm@sha256:67a1e1f215ccda113cfc024e8639049257e88f273898f595b61476d128d387e8
#3 DONE 0.0s
#4 [internal] load .dockerignore
#4 transferring context: 224B done
#4 DONE 0.0s
#5 [1/6] FROM docker.io/library/python:3.13.14-slim-bookworm@sha256:67a1e1f215ccda113cfc024e8639049257e88f273898f595b61476d128d387e8
#5 resolve docker.io/library/python:3.13.14-slim-bookworm@sha256:67a1e1f215ccda113cfc024e8639049257e88f273898f595b61476d128d387e8 0.0s done
#5 DONE 0.0s
#6 [internal] load build context
#6 transferring context: 47.24kB 0.0s done
#6 DONE 0.0s
#7 [2/6] WORKDIR /app
#7 CACHED
#8 [3/6] COPY requirements.lock .
#8 CACHED
#9 [4/6] RUN python -m pip install --no-cache-dir -r requirements.lock && useradd --uid 10001 --create-home messenger
#9 CACHED
#10 [5/6] COPY src ./src
#10 CACHED
#11 [6/6] COPY migrations ./migrations
#11 CACHED
#12 exporting to image
#12 exporting layers done
#12 exporting manifest sha256:3755b1b2f8e8c91c3d5b7fca8a2e25ada7444649398a43d55e951eda460e2d51 0.0s done
#12 exporting config sha256:3f86c969e38b9cdc008d8b1e466c16a37349b8fbc1374f7b935a71d443a42693 0.0s done
#12 exporting attestation manifest sha256:f1d1fa34a81b6c0f370f567417310121dea10c08eb650321d1ea20c138e4c27d 0.0s done
#12 exporting manifest list sha256:88af0a676201721091455f062d2322cc8a3074f3899dc9c8f878a9c69cdbb908 0.0s done
#12 naming to docker.io/library/mtusi-messenger:lab4 done
#12 unpacking to docker.io/library/mtusi-messenger:lab4 done
#12 DONE 0.1s
#13 resolving provenance for metadata file
#13 DONE 0.0s
Image mtusi-messenger:lab4 Built
$ docker compose --project-name mtusi-lab4-clean-final --env-file .env run --rm --no-deps lb haproxy -c -f /usr/local/etc/haproxy/haproxy.cfg
Volume mtusi-lab4-clean-final_s3data Creating
Volume mtusi-lab4-clean-final_s3data Creating
Network mtusi-lab4-clean-final_storage Creating
Network mtusi-lab4-clean-final_default Creating
Volume mtusi-lab4-clean-final_pgdata Creating
Network mtusi-lab4-clean-final_default Creating
Network mtusi-lab4-clean-final_storage Creating
Volume mtusi-lab4-clean-final_pgdata Creating
Volume mtusi-lab4-clean-final_s3data Created
Volume mtusi-lab4-clean-final_s3data Created
Volume mtusi-lab4-clean-final_pgdata Created
Volume mtusi-lab4-clean-final_pgdata Created
Network mtusi-lab4-clean-final_storage Created
Network mtusi-lab4-clean-final_storage Created
Network mtusi-lab4-clean-final_default Created
Network mtusi-lab4-clean-final_default Created
Container mtusi-lab4-clean-final-lb-run-1c2bddfccc7b Creating
Container mtusi-lab4-clean-final-lb-run-1c2bddfccc7b Created
[NOTICE] (1) : [/usr/local/etc/haproxy/haproxy.cfg:35] : 'server api_ready/first' : could not resolve address 'api1', disabling server.
[NOTICE] (1) : [/usr/local/etc/haproxy/haproxy.cfg:36] : 'server api_ready/second' : could not resolve address 'api2', disabling server.
[NOTICE] (1) : [/usr/local/etc/haproxy/haproxy.cfg:43] : 'server api_live/first' : could not resolve address 'api1', disabling server.
[NOTICE] (1) : [/usr/local/etc/haproxy/haproxy.cfg:44] : 'server api_live/second' : could not resolve address 'api2', disabling server.
$ docker compose --project-name mtusi-lab4-clean-final --env-file .env up -d --no-build db migrate s3-storage s3 bootstrap api1 api2 cleanup lb
Container mtusi-lab4-clean-final-db-1 Creating
Container mtusi-lab4-clean-final-s3-storage-1 Creating
Container mtusi-lab4-clean-final-s3-storage-1 Created
Container mtusi-lab4-clean-final-s3-1 Creating
Container mtusi-lab4-clean-final-db-1 Created
Container mtusi-lab4-clean-final-migrate-1 Creating
Container mtusi-lab4-clean-final-s3-1 Created
Container mtusi-lab4-clean-final-bootstrap-1 Creating
Container mtusi-lab4-clean-final-migrate-1 Created
Container mtusi-lab4-clean-final-bootstrap-1 Created
Container mtusi-lab4-clean-final-api2-1 Creating
Container mtusi-lab4-clean-final-cleanup-1 Creating
Container mtusi-lab4-clean-final-api1-1 Creating
Container mtusi-lab4-clean-final-api2-1 Created
Container mtusi-lab4-clean-final-cleanup-1 Created
Container mtusi-lab4-clean-final-api1-1 Created
Container mtusi-lab4-clean-final-lb-1 Creating
Container mtusi-lab4-clean-final-lb-1 Created
Container mtusi-lab4-clean-final-db-1 Starting
Container mtusi-lab4-clean-final-s3-storage-1 Starting
Container mtusi-lab4-clean-final-db-1 Started
Container mtusi-lab4-clean-final-db-1 Waiting
Container mtusi-lab4-clean-final-s3-storage-1 Started
Container mtusi-lab4-clean-final-s3-storage-1 Waiting
Container mtusi-lab4-clean-final-db-1 Healthy
Container mtusi-lab4-clean-final-migrate-1 Starting
Container mtusi-lab4-clean-final-s3-storage-1 Healthy
Container mtusi-lab4-clean-final-s3-1 Starting
Container mtusi-lab4-clean-final-migrate-1 Started
Container mtusi-lab4-clean-final-migrate-1 Waiting
Container mtusi-lab4-clean-final-s3-1 Started
Container mtusi-lab4-clean-final-bootstrap-1 Starting
Container mtusi-lab4-clean-final-bootstrap-1 Started
Container mtusi-lab4-clean-final-bootstrap-1 Waiting
Container mtusi-lab4-clean-final-migrate-1 Exited
Container mtusi-lab4-clean-final-bootstrap-1 Exited
Container mtusi-lab4-clean-final-api1-1 Starting
Container mtusi-lab4-clean-final-cleanup-1 Starting
Container mtusi-lab4-clean-final-api2-1 Starting
Container mtusi-lab4-clean-final-api1-1 Started
Container mtusi-lab4-clean-final-cleanup-1 Started
Container mtusi-lab4-clean-final-api2-1 Started
Container mtusi-lab4-clean-final-lb-1 Starting
Container mtusi-lab4-clean-final-lb-1 Started
Two API replicas ready through LB: http://127.0.0.1:18082
$ .\.venv\Scripts\python.exe -X utf8 tests/smoke.py --base-url http://127.0.0.1:18082
PASS: lab4 public API scenario; infrastructure and grading evidence remain separate.
$ .\.venv\Scripts\python.exe -X utf8 -m unittest discover -s student-tests -v
test_auth_and_access (test_api.ApiTests.test_auth_and_access) ... ok
test_bad_cursors_and_limits (test_api.ApiTests.test_bad_cursors_and_limits) ... ok
test_chat_pagination (test_api.ApiTests.test_chat_pagination) ... ok
test_chat_validation_is_atomic (test_api.ApiTests.test_chat_validation_is_atomic) ... ok
test_invalid_json_and_content_type (test_api.ApiTests.test_invalid_json_and_content_type) ... ok
test_message_fields_and_unicode (test_api.ApiTests.test_message_fields_and_unicode) ... ok
test_message_pagination (test_api.ApiTests.test_message_pagination) ... ok
test_parallel_messages (test_api.ApiTests.test_parallel_messages) ... ok
test_parallel_username (test_api.ApiTests.test_parallel_username) ... ok
test_read_after_write (test_api.ApiTests.test_read_after_write) ... ok
test_user_fields (test_api.ApiTests.test_user_fields) ... ok
test_attachment_access_and_unknown_ids (test_attachments.AttachmentTests.test_attachment_access_and_unknown_ids) ... ok
test_bad_attachment_list_and_other_chat_do_not_create_messages (test_attachments.AttachmentTests.test_bad_attachment_list_and_other_chat_do_not_create_messages) ... ok
test_byte_limit_and_original_at_exact_boundary (test_attachments.AttachmentTests.test_byte_limit_and_original_at_exact_boundary) ... ok
test_chunked_body_cannot_bypass_size_limit (test_attachments.AttachmentTests.test_chunked_body_cannot_bypass_size_limit) ... ok
test_corrupt_images_mime_and_multipart (test_attachments.AttachmentTests.test_corrupt_images_mime_and_multipart) ... ok
test_filename_validation (test_attachments.AttachmentTests.test_filename_validation) ... ok
test_idempotency_defaults_and_attachment_replay (test_attachments.AttachmentTests.test_idempotency_defaults_and_attachment_replay) ... ok
test_messages_keep_attachment_order_and_allow_empty_text (test_attachments.AttachmentTests.test_messages_keep_attachment_order_and_allow_empty_text) ... ok
test_original_png_jpeg_and_unicode_filename (test_attachments.AttachmentTests.test_original_png_jpeg_and_unicode_filename) ... ok
test_pixel_boundary_and_decompression_bomb (test_attachments.AttachmentTests.test_pixel_boundary_and_decompression_bomb) ... ok
test_different_body_conflicts (test_idempotency.IdempotencyTests.test_different_body_conflicts) ... ok
test_failure_rolls_back_message_and_key (test_idempotency.IdempotencyTests.test_failure_rolls_back_message_and_key) ... ok
test_invalid_body_does_not_reserve_key (test_idempotency.IdempotencyTests.test_invalid_body_does_not_reserve_key) ... ok
test_key_required_and_format (test_idempotency.IdempotencyTests.test_key_required_and_format) ... ok
test_replay_normalizes_json (test_idempotency.IdempotencyTests.test_replay_normalizes_json) ... ok
test_replay_still_checks_access_and_session (test_idempotency.IdempotencyTests.test_replay_still_checks_access_and_session) ... ok
test_retention_and_expired_key_reuse (test_idempotency.IdempotencyTests.test_retention_and_expired_key_reuse) ... ok
test_same_session_on_both_instances (test_idempotency.IdempotencyTests.test_same_session_on_both_instances) ... ok
test_scope_is_user_and_chat_not_session (test_idempotency.IdempotencyTests.test_scope_is_user_and_chat_not_session) ... ok
test_twenty_concurrent_requests_use_both_replicas (test_idempotency.IdempotencyTests.test_twenty_concurrent_requests_use_both_replicas) ... ok
test_logout_only_revokes_current_session (test_sessions.SessionTests.test_logout_only_revokes_current_session) ... ok
test_malformed_basic_and_body (test_sessions.SessionTests.test_malformed_basic_and_body) ... ok
test_only_bearer_identifies_user (test_sessions.SessionTests.test_only_bearer_identifies_user) ... ok
test_password_boundaries_unicode_colon_spaces (test_sessions.SessionTests.test_password_boundaries_unicode_colon_spaces) ... ok
test_password_required_and_limits (test_sessions.SessionTests.test_password_required_and_limits) ... ok
test_wrong_and_unknown_credentials (test_sessions.SessionTests.test_wrong_and_unknown_credentials) ... ok
----------------------------------------------------------------------
Ran 37 tests in 12.731s
OK
$ .\.venv\Scripts\python.exe -X utf8 student-tests/check_cleanup.py
$ docker compose --project-name mtusi-lab4-clean-final --env-file .env stop --timeout 3 cleanup
Container mtusi-lab4-clean-final-cleanup-1 Stopping
Container mtusi-lab4-clean-final-cleanup-1 Stopped
$ docker compose --project-name mtusi-lab4-clean-final --env-file .env run --rm --no-deps bootstrap
Container mtusi-lab4-clean-final-bootstrap-run-ae7ef15a17b1 Creating
Container mtusi-lab4-clean-final-bootstrap-run-ae7ef15a17b1 Created
PASS: private bucket ready; application credentials work
$ docker kill 0e20c8a5d15f411cd3807858e8eb198252877c683faff99545fe97da8e124f23
0e20c8a5d15f411cd3807858e8eb198252877c683faff99545fe97da8e124f23
$ docker compose --project-name mtusi-lab4-clean-final --env-file .env start api1 api2
Container mtusi-lab4-clean-final-db-1 Waiting
Container mtusi-lab4-clean-final-s3-storage-1 Waiting
Container mtusi-lab4-clean-final-s3-storage-1 Healthy
Container mtusi-lab4-clean-final-bootstrap-1 Starting
Container mtusi-lab4-clean-final-db-1 Healthy
Container mtusi-lab4-clean-final-migrate-1 Starting
Container mtusi-lab4-clean-final-bootstrap-1 Started
Container mtusi-lab4-clean-final-bootstrap-1 Waiting
Container mtusi-lab4-clean-final-migrate-1 Started
Container mtusi-lab4-clean-final-migrate-1 Waiting
Container mtusi-lab4-clean-final-migrate-1 Exited
Container mtusi-lab4-clean-final-bootstrap-1 Exited
Container mtusi-lab4-clean-final-api2-1 Starting
Container mtusi-lab4-clean-final-api2-1 Started
$ docker compose --project-name mtusi-lab4-clean-final --env-file .env stop --timeout 3 s3
Container mtusi-lab4-clean-final-s3-1 Stopping
Container mtusi-lab4-clean-final-s3-1 Stopped
$ docker compose --project-name mtusi-lab4-clean-final --env-file .env start s3
Container mtusi-lab4-clean-final-s3-storage-1 Waiting
Container mtusi-lab4-clean-final-s3-storage-1 Healthy
Container mtusi-lab4-clean-final-s3-1 Starting
Container mtusi-lab4-clean-final-s3-1 Started
$ docker compose --project-name mtusi-lab4-clean-final --env-file .env start cleanup
Container mtusi-lab4-clean-final-s3-storage-1 Waiting
Container mtusi-lab4-clean-final-db-1 Waiting
Container mtusi-lab4-clean-final-s3-storage-1 Healthy
Container mtusi-lab4-clean-final-db-1 Healthy
Container mtusi-lab4-clean-final-bootstrap-1 Starting
Container mtusi-lab4-clean-final-migrate-1 Starting
Container mtusi-lab4-clean-final-bootstrap-1 Started
Container mtusi-lab4-clean-final-bootstrap-1 Waiting
Container mtusi-lab4-clean-final-migrate-1 Started
Container mtusi-lab4-clean-final-migrate-1 Waiting
Container mtusi-lab4-clean-final-migrate-1 Exited
Container mtusi-lab4-clean-final-bootstrap-1 Exited
Container mtusi-lab4-clean-final-cleanup-1 Starting
Container mtusi-lab4-clean-final-cleanup-1 Started
PASS: private S3, restricted credentials, real faults and safe cleanup
$ docker compose --project-name mtusi-lab4-clean-final --env-file .env start s3 cleanup
Container mtusi-lab4-clean-final-s3-storage-1 Waiting
Container mtusi-lab4-clean-final-db-1 Waiting
Container mtusi-lab4-clean-final-db-1 Healthy
Container mtusi-lab4-clean-final-migrate-1 Starting
Container mtusi-lab4-clean-final-s3-storage-1 Healthy
Container mtusi-lab4-clean-final-bootstrap-1 Starting
Container mtusi-lab4-clean-final-migrate-1 Started
Container mtusi-lab4-clean-final-migrate-1 Waiting
Container mtusi-lab4-clean-final-bootstrap-1 Started
Container mtusi-lab4-clean-final-bootstrap-1 Waiting
Container mtusi-lab4-clean-final-migrate-1 Exited
Container mtusi-lab4-clean-final-bootstrap-1 Exited
$ docker compose --project-name mtusi-lab4-clean-final --env-file .env start api1 api2
Container mtusi-lab4-clean-final-s3-storage-1 Waiting
Container mtusi-lab4-clean-final-db-1 Waiting
Container mtusi-lab4-clean-final-s3-storage-1 Healthy
Container mtusi-lab4-clean-final-db-1 Healthy
Container mtusi-lab4-clean-final-bootstrap-1 Starting
Container mtusi-lab4-clean-final-migrate-1 Starting
Container mtusi-lab4-clean-final-bootstrap-1 Started
Container mtusi-lab4-clean-final-bootstrap-1 Waiting
Container mtusi-lab4-clean-final-migrate-1 Started
Container mtusi-lab4-clean-final-migrate-1 Waiting
Container mtusi-lab4-clean-final-migrate-1 Exited
Container mtusi-lab4-clean-final-bootstrap-1 Exited
$ .\.venv\Scripts\python.exe -X utf8 tests/resilience.py --mode persistence --action-script .\scripts\restart.cmd --base-url http://127.0.0.1:18082
Executing explicit persistence hook; hook output suppressed to avoid leaking secrets.
PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence.
$ docker compose --project-name mtusi-lab4-clean-final --env-file .env run --rm --no-deps migrate
Container mtusi-lab4-clean-final-migrate-run-1734bc09b12d Creating
Container mtusi-lab4-clean-final-migrate-run-1734bc09b12d Created
Already applied: 001_initial.sql
Already applied: 002_idempotency.sql
Already applied: 003_attachments.sql
$ docker compose --project-name mtusi-lab4-clean-final --env-file .env run --rm --no-deps migrate
Container mtusi-lab4-clean-final-migrate-run-4a000b9957d3 Creating
Container mtusi-lab4-clean-final-migrate-run-4a000b9957d3 Created
Already applied: 001_initial.sql
Already applied: 002_idempotency.sql
Already applied: 003_attachments.sql
PASS: old session/cursor, durable logout and repeatable migrations
$ .\.venv\Scripts\python.exe -X utf8 tests/resilience.py --mode failover --action-script .\scripts\stop-one.cmd --base-url http://127.0.0.1:18082
Executing explicit failover hook; hook output suppressed to avoid leaking secrets.
{"observed_instances_before": 2, "successful_writes": 105, "failed_attempts": 4, "first_recovery_seconds": 2.933}
PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence.
$ docker compose --project-name mtusi-lab4-clean-final --env-file .env start api1 api2
Container mtusi-lab4-clean-final-s3-storage-1 Waiting
Container mtusi-lab4-clean-final-db-1 Waiting
Container mtusi-lab4-clean-final-s3-storage-1 Healthy
Container mtusi-lab4-clean-final-bootstrap-1 Starting
Container mtusi-lab4-clean-final-db-1 Healthy
Container mtusi-lab4-clean-final-migrate-1 Starting
Container mtusi-lab4-clean-final-bootstrap-1 Started
Container mtusi-lab4-clean-final-bootstrap-1 Waiting
Container mtusi-lab4-clean-final-migrate-1 Started
Container mtusi-lab4-clean-final-migrate-1 Waiting
Container mtusi-lab4-clean-final-migrate-1 Exited
Container mtusi-lab4-clean-final-bootstrap-1 Exited
Container mtusi-lab4-clean-final-api1-1 Starting
Container mtusi-lab4-clean-final-api1-1 Started
PASS: both physical API replicas stopped separately; no lost messages or duplicates
$ docker compose --project-name mtusi-lab4-clean-final --env-file .env start api1 api2
Container mtusi-lab4-clean-final-db-1 Waiting
Container mtusi-lab4-clean-final-s3-storage-1 Waiting
Container mtusi-lab4-clean-final-s3-storage-1 Healthy
Container mtusi-lab4-clean-final-bootstrap-1 Starting
Container mtusi-lab4-clean-final-db-1 Healthy
Container mtusi-lab4-clean-final-migrate-1 Starting
Container mtusi-lab4-clean-final-bootstrap-1 Started
Container mtusi-lab4-clean-final-bootstrap-1 Waiting
Container mtusi-lab4-clean-final-migrate-1 Started
Container mtusi-lab4-clean-final-migrate-1 Waiting
Container mtusi-lab4-clean-final-migrate-1 Exited
Container mtusi-lab4-clean-final-bootstrap-1 Exited
Container mtusi-lab4-clean-final-api2-1 Starting
Container mtusi-lab4-clean-final-api2-1 Started
$ .\scripts\stop-one.cmd
$ docker stop --timeout 3 651db4cfc1d4bcd935cb354b66f5018747ebfebeb10aeb98c0a2e18e4a23669a
651db4cfc1d4bcd935cb354b66f5018747ebfebeb10aeb98c0a2e18e4a23669a
Stopped only api1
$ docker compose --project-name mtusi-lab4-clean-final --env-file .env start api1 api2
Container mtusi-lab4-clean-final-db-1 Waiting
Container mtusi-lab4-clean-final-s3-storage-1 Waiting
Container mtusi-lab4-clean-final-db-1 Healthy
Container mtusi-lab4-clean-final-s3-storage-1 Healthy
Container mtusi-lab4-clean-final-migrate-1 Starting
Container mtusi-lab4-clean-final-bootstrap-1 Starting
Container mtusi-lab4-clean-final-bootstrap-1 Started
Container mtusi-lab4-clean-final-bootstrap-1 Waiting
Container mtusi-lab4-clean-final-migrate-1 Started
Container mtusi-lab4-clean-final-migrate-1 Waiting
Container mtusi-lab4-clean-final-migrate-1 Exited
Container mtusi-lab4-clean-final-bootstrap-1 Exited
Container mtusi-lab4-clean-final-api1-1 Starting
Container mtusi-lab4-clean-final-api1-1 Started
$ .\scripts\stop-one.cmd
$ docker stop --timeout 3 0e20c8a5d15f411cd3807858e8eb198252877c683faff99545fe97da8e124f23
0e20c8a5d15f411cd3807858e8eb198252877c683faff99545fe97da8e124f23
Stopped only api2
$ docker compose --project-name mtusi-lab4-clean-final --env-file .env start api1 api2
Container mtusi-lab4-clean-final-s3-storage-1 Waiting
Container mtusi-lab4-clean-final-db-1 Waiting
Container mtusi-lab4-clean-final-s3-storage-1 Healthy
Container mtusi-lab4-clean-final-bootstrap-1 Starting
Container mtusi-lab4-clean-final-db-1 Healthy
Container mtusi-lab4-clean-final-migrate-1 Starting
Container mtusi-lab4-clean-final-bootstrap-1 Started
Container mtusi-lab4-clean-final-bootstrap-1 Waiting
Container mtusi-lab4-clean-final-migrate-1 Started
Container mtusi-lab4-clean-final-migrate-1 Waiting
Container mtusi-lab4-clean-final-migrate-1 Exited
Container mtusi-lab4-clean-final-bootstrap-1 Exited
Container mtusi-lab4-clean-final-api2-1 Starting
Container mtusi-lab4-clean-final-api2-1 Started
PASS: stopped each uploader; other API downloaded the same original
$ docker compose --project-name mtusi-lab4-clean-final --env-file .env start api1 api2
Container mtusi-lab4-clean-final-s3-storage-1 Waiting
Container mtusi-lab4-clean-final-db-1 Waiting
Container mtusi-lab4-clean-final-s3-storage-1 Healthy
Container mtusi-lab4-clean-final-bootstrap-1 Starting
Container mtusi-lab4-clean-final-db-1 Healthy
Container mtusi-lab4-clean-final-migrate-1 Starting
Container mtusi-lab4-clean-final-bootstrap-1 Started
Container mtusi-lab4-clean-final-bootstrap-1 Waiting
Container mtusi-lab4-clean-final-migrate-1 Started
Container mtusi-lab4-clean-final-migrate-1 Waiting
Container mtusi-lab4-clean-final-migrate-1 Exited
Container mtusi-lab4-clean-final-bootstrap-1 Exited
$ docker compose --project-name mtusi-lab4-clean-final-ttl --env-file .env up -d --no-build db migrate s3-storage s3 bootstrap api1 api2 cleanup lb
Volume mtusi-lab4-clean-final-ttl_pgdata Creating
Volume mtusi-lab4-clean-final-ttl_pgdata Creating
Network mtusi-lab4-clean-final-ttl_default Creating
Volume mtusi-lab4-clean-final-ttl_s3data Creating
Volume mtusi-lab4-clean-final-ttl_s3data Creating
Network mtusi-lab4-clean-final-ttl_storage Creating
Network mtusi-lab4-clean-final-ttl_default Creating
Network mtusi-lab4-clean-final-ttl_storage Creating
Volume mtusi-lab4-clean-final-ttl_s3data Created
Volume mtusi-lab4-clean-final-ttl_s3data Created
Volume mtusi-lab4-clean-final-ttl_pgdata Created
Volume mtusi-lab4-clean-final-ttl_pgdata Created
Network mtusi-lab4-clean-final-ttl_default Created
Network mtusi-lab4-clean-final-ttl_default Created
Container mtusi-lab4-clean-final-ttl-db-1 Creating
Network mtusi-lab4-clean-final-ttl_storage Created
Network mtusi-lab4-clean-final-ttl_storage Created
Container mtusi-lab4-clean-final-ttl-s3-storage-1 Creating
Container mtusi-lab4-clean-final-ttl-db-1 Created
Container mtusi-lab4-clean-final-ttl-migrate-1 Creating
Container mtusi-lab4-clean-final-ttl-s3-storage-1 Created
Container mtusi-lab4-clean-final-ttl-s3-1 Creating
Container mtusi-lab4-clean-final-ttl-migrate-1 Created
Container mtusi-lab4-clean-final-ttl-s3-1 Created
Container mtusi-lab4-clean-final-ttl-bootstrap-1 Creating
Container mtusi-lab4-clean-final-ttl-bootstrap-1 Created
Container mtusi-lab4-clean-final-ttl-cleanup-1 Creating
Container mtusi-lab4-clean-final-ttl-api2-1 Creating
Container mtusi-lab4-clean-final-ttl-api1-1 Creating
Container mtusi-lab4-clean-final-ttl-api1-1 Created
Container mtusi-lab4-clean-final-ttl-api2-1 Created
Container mtusi-lab4-clean-final-ttl-lb-1 Creating
Container mtusi-lab4-clean-final-ttl-cleanup-1 Created
Container mtusi-lab4-clean-final-ttl-lb-1 Created
Container mtusi-lab4-clean-final-ttl-db-1 Starting
Container mtusi-lab4-clean-final-ttl-s3-storage-1 Starting
Container mtusi-lab4-clean-final-ttl-db-1 Started
Container mtusi-lab4-clean-final-ttl-db-1 Waiting
Container mtusi-lab4-clean-final-ttl-s3-storage-1 Started
Container mtusi-lab4-clean-final-ttl-s3-storage-1 Waiting
Container mtusi-lab4-clean-final-ttl-db-1 Healthy
Container mtusi-lab4-clean-final-ttl-migrate-1 Starting
Container mtusi-lab4-clean-final-ttl-migrate-1 Started
Container mtusi-lab4-clean-final-ttl-migrate-1 Waiting
Container mtusi-lab4-clean-final-ttl-s3-storage-1 Healthy
Container mtusi-lab4-clean-final-ttl-s3-1 Starting
Container mtusi-lab4-clean-final-ttl-s3-1 Started
Container mtusi-lab4-clean-final-ttl-bootstrap-1 Starting
Container mtusi-lab4-clean-final-ttl-bootstrap-1 Started
Container mtusi-lab4-clean-final-ttl-bootstrap-1 Waiting
Container mtusi-lab4-clean-final-ttl-migrate-1 Exited
Container mtusi-lab4-clean-final-ttl-bootstrap-1 Exited
Container mtusi-lab4-clean-final-ttl-api1-1 Starting
Container mtusi-lab4-clean-final-ttl-cleanup-1 Starting
Container mtusi-lab4-clean-final-ttl-api2-1 Starting
Container mtusi-lab4-clean-final-ttl-api1-1 Started
Container mtusi-lab4-clean-final-ttl-cleanup-1 Started
Container mtusi-lab4-clean-final-ttl-api2-1 Started
Container mtusi-lab4-clean-final-ttl-lb-1 Starting
Container mtusi-lab4-clean-final-ttl-lb-1 Started
Two API replicas ready through LB: http://127.0.0.1:18083
$ docker compose --project-name mtusi-lab4-clean-final-ttl --env-file .env restart --timeout 3 api1 api2
Container mtusi-lab4-clean-final-ttl-api1-1 Restarting
Container mtusi-lab4-clean-final-ttl-api2-1 Restarting
Container mtusi-lab4-clean-final-ttl-api1-1 Started
Container mtusi-lab4-clean-final-ttl-api2-1 Started
PASS: TTL=2, expired token rejected after API restart; fresh login works
$ docker compose --project-name mtusi-lab4-clean-final-ttl --env-file .env stop --timeout 10 lb api1 api2 cleanup s3 s3-storage db
Container mtusi-lab4-clean-final-ttl-cleanup-1 Stopping
Container mtusi-lab4-clean-final-ttl-lb-1 Stopping
Container mtusi-lab4-clean-final-ttl-lb-1 Stopped
Container mtusi-lab4-clean-final-ttl-api1-1 Stopping
Container mtusi-lab4-clean-final-ttl-api2-1 Stopping
Container mtusi-lab4-clean-final-ttl-api2-1 Stopped
Container mtusi-lab4-clean-final-ttl-api1-1 Stopped
Container mtusi-lab4-clean-final-ttl-cleanup-1 Stopped
Container mtusi-lab4-clean-final-ttl-s3-1 Stopping
Container mtusi-lab4-clean-final-ttl-db-1 Stopping
Container mtusi-lab4-clean-final-ttl-db-1 Stopped
Container mtusi-lab4-clean-final-ttl-s3-1 Stopped
Container mtusi-lab4-clean-final-ttl-s3-storage-1 Stopping
Container mtusi-lab4-clean-final-ttl-s3-storage-1 Stopped
$ .\.venv\Scripts\python.exe -X utf8 student-tests/check_memory.py --project mtusi-lab4-clean-final-memory --port 18084
$ docker compose --project-name mtusi-lab4-clean-final-memory --env-file .env stop --timeout 3 lb api1 api2 cleanup s3 s3-storage db
$ docker compose --project-name mtusi-lab4-clean-final-memory --env-file .env rm -f api1 api2
No stopped containers
$ docker compose --project-name mtusi-lab4-clean-final-memory --env-file .env up -d --no-build db migrate s3-storage s3 bootstrap api1 api2 cleanup lb
Volume mtusi-lab4-clean-final-memory_pgdata Creating
Volume mtusi-lab4-clean-final-memory_pgdata Creating
Network mtusi-lab4-clean-final-memory_storage Creating
Network mtusi-lab4-clean-final-memory_default Creating
Network mtusi-lab4-clean-final-memory_storage Creating
Network mtusi-lab4-clean-final-memory_default Creating
Volume mtusi-lab4-clean-final-memory_s3data Creating
Volume mtusi-lab4-clean-final-memory_s3data Creating
Volume mtusi-lab4-clean-final-memory_s3data Created
Volume mtusi-lab4-clean-final-memory_s3data Created
Volume mtusi-lab4-clean-final-memory_pgdata Created
Volume mtusi-lab4-clean-final-memory_pgdata Created
Network mtusi-lab4-clean-final-memory_default Created
Network mtusi-lab4-clean-final-memory_default Created
Container mtusi-lab4-clean-final-memory-db-1 Creating
Network mtusi-lab4-clean-final-memory_storage Created
Network mtusi-lab4-clean-final-memory_storage Created
Container mtusi-lab4-clean-final-memory-s3-storage-1 Creating
Container mtusi-lab4-clean-final-memory-db-1 Created
Container mtusi-lab4-clean-final-memory-migrate-1 Creating
Container mtusi-lab4-clean-final-memory-s3-storage-1 Created
Container mtusi-lab4-clean-final-memory-s3-1 Creating
Container mtusi-lab4-clean-final-memory-migrate-1 Created
Container mtusi-lab4-clean-final-memory-s3-1 Created
Container mtusi-lab4-clean-final-memory-bootstrap-1 Creating
Container mtusi-lab4-clean-final-memory-bootstrap-1 Created
Container mtusi-lab4-clean-final-memory-cleanup-1 Creating
Container mtusi-lab4-clean-final-memory-api2-1 Creating
Container mtusi-lab4-clean-final-memory-api1-1 Creating
Container mtusi-lab4-clean-final-memory-api1-1 Created
Container mtusi-lab4-clean-final-memory-api2-1 Created
Container mtusi-lab4-clean-final-memory-lb-1 Creating
Container mtusi-lab4-clean-final-memory-cleanup-1 Created
Container mtusi-lab4-clean-final-memory-lb-1 Created
Container mtusi-lab4-clean-final-memory-s3-storage-1 Starting
Container mtusi-lab4-clean-final-memory-db-1 Starting
Container mtusi-lab4-clean-final-memory-s3-storage-1 Started
Container mtusi-lab4-clean-final-memory-s3-storage-1 Waiting
Container mtusi-lab4-clean-final-memory-db-1 Started
Container mtusi-lab4-clean-final-memory-db-1 Waiting
Container mtusi-lab4-clean-final-memory-s3-storage-1 Healthy
Container mtusi-lab4-clean-final-memory-s3-1 Starting
Container mtusi-lab4-clean-final-memory-s3-1 Started
Container mtusi-lab4-clean-final-memory-bootstrap-1 Starting
Container mtusi-lab4-clean-final-memory-db-1 Healthy
Container mtusi-lab4-clean-final-memory-migrate-1 Starting
Container mtusi-lab4-clean-final-memory-bootstrap-1 Started
Container mtusi-lab4-clean-final-memory-bootstrap-1 Waiting
Container mtusi-lab4-clean-final-memory-migrate-1 Started
Container mtusi-lab4-clean-final-memory-migrate-1 Waiting
Container mtusi-lab4-clean-final-memory-migrate-1 Exited
Container mtusi-lab4-clean-final-memory-bootstrap-1 Exited
Container mtusi-lab4-clean-final-memory-api1-1 Starting
Container mtusi-lab4-clean-final-memory-cleanup-1 Starting
Container mtusi-lab4-clean-final-memory-api2-1 Starting
Container mtusi-lab4-clean-final-memory-api1-1 Started
Container mtusi-lab4-clean-final-memory-cleanup-1 Started
Container mtusi-lab4-clean-final-memory-api2-1 Started
Container mtusi-lab4-clean-final-memory-lb-1 Starting
Container mtusi-lab4-clean-final-memory-lb-1 Started
Two API replicas ready through LB: http://127.0.0.1:18084
PASS: 10 concurrent uploads in both profiles; originals intact, no OOM or restarts
$ docker compose --project-name mtusi-lab4-clean-final-memory --env-file .env stop --timeout 10 lb api1 api2 cleanup s3 s3-storage db
Container mtusi-lab4-clean-final-memory-cleanup-1 Stopping
Container mtusi-lab4-clean-final-memory-lb-1 Stopping
Container mtusi-lab4-clean-final-memory-lb-1 Stopped
Container mtusi-lab4-clean-final-memory-api1-1 Stopping
Container mtusi-lab4-clean-final-memory-api2-1 Stopping
Container mtusi-lab4-clean-final-memory-api2-1 Stopped
Container mtusi-lab4-clean-final-memory-api1-1 Stopped
Container mtusi-lab4-clean-final-memory-cleanup-1 Stopped
Container mtusi-lab4-clean-final-memory-db-1 Stopping
Container mtusi-lab4-clean-final-memory-s3-1 Stopping
Container mtusi-lab4-clean-final-memory-db-1 Stopped
Container mtusi-lab4-clean-final-memory-s3-1 Stopped
Container mtusi-lab4-clean-final-memory-s3-storage-1 Stopping
Container mtusi-lab4-clean-final-memory-s3-storage-1 Stopped
$ docker compose --project-name mtusi-lab4-clean-final --env-file .env stop --timeout 10 db
Container mtusi-lab4-clean-final-db-1 Stopping
Container mtusi-lab4-clean-final-db-1 Stopped
$ docker compose --project-name mtusi-lab4-clean-final --env-file .env start db
Container mtusi-lab4-clean-final-db-1 Starting
Container mtusi-lab4-clean-final-db-1 Started
PASS: DB outage -> live=200, ready/business=503; session works after recovery
$ .\.venv\Scripts\python.exe -X utf8 tests/smoke.py --base-url http://127.0.0.1:18082
PASS: lab4 public API scenario; infrastructure and grading evidence remain separate.
PASS: full lab4 verification
+23
View File
@@ -0,0 +1,23 @@
{
"privacy": {
"anonymous_get": 403,
"other_bucket_get": 403,
"create_bucket": 403,
"change_acl": 403,
"bootstrap_replay": "PASS",
"storage_network_isolated": true,
"internal_ports_not_published": true
},
"fresh_intent_protected": "PASS",
"locked_intent_skipped": "PASS",
"orphan_removed_and_repeat_safe": "PASS",
"ready_unlinked_attachment_protected": "PASS",
"sql_failure_after_put_compensated": "PASS",
"api_killed_after_put_compensated": {
"status": "PASS",
"service": "api2"
},
"s3_outage_keeps_intent_then_recovers": "PASS",
"client_disconnect_has_no_publication": "PASS",
"periodic_cleanup_worker": "PASS"
}
+7
View File
@@ -0,0 +1,7 @@
{
"ttl_seconds": 2,
"expiry_unchanged_on_restart": true,
"before_restart": 200,
"after_expiry_and_restart": 401,
"new_login": 201
}
+173
View File
@@ -0,0 +1,173 @@
{
"public": {
"service": "api2",
"target_instance": "ceea7b6044b3461e997e191c46443ba4",
"before": {
"api1": {
"instance_id": "51a44cc9af98424bba787926800c0739",
"container_id": "c88a212d829b1b5154928aafff160caf2f09c3d7ac6b92f632203d0ecdcf704a"
},
"api2": {
"instance_id": "ceea7b6044b3461e997e191c46443ba4",
"container_id": "61c7c7c4ed4cbef04db5a6dab617f7b8805e361f2ce8da7e5e339e85f7e5543b"
}
},
"after": {
"api1": {
"id": "c88a212d829b1b5154928aafff160caf2f09c3d7ac6b92f632203d0ecdcf704a",
"image": "sha256:3a15e7611decf19224c446f3a652ea24cb698e4552756ca63e7df7d27cd2e098",
"started_at": "2026-11-16T17:09:28.828991567Z",
"running": true,
"user": "messenger",
"volumes": []
},
"api2": {
"id": "61c7c7c4ed4cbef04db5a6dab617f7b8805e361f2ce8da7e5e339e85f7e5543b",
"image": "sha256:3a15e7611decf19224c446f3a652ea24cb698e4552756ca63e7df7d27cd2e098",
"started_at": "2026-11-16T17:09:28.754518187Z",
"running": false,
"user": "messenger",
"volumes": []
},
"cleanup": {
"id": "17b7609d7f35945899e1b619821e1afee51dc3f6869854ff6a9fda00e2c71bda",
"image": "sha256:3a15e7611decf19224c446f3a652ea24cb698e4552756ca63e7df7d27cd2e098",
"started_at": "2026-11-16T17:09:38.269172115Z",
"running": true,
"user": "messenger",
"volumes": []
},
"db": {
"id": "a4ea88454a3740aa70ad1afd27ac3c34b04d18a2435ebebb32bed1d33cc3ad70",
"image": "sha256:665dc7b37ac52a17e298511414c21d23ffa4e6a6ef6225b3e5d59c7afb35e994",
"started_at": "2026-11-16T17:09:28.572826955Z",
"running": true,
"user": "",
"volumes": [
"mtusi-lab4_pgdata"
]
},
"lb": {
"id": "8c11ce96e2bd533072d4c60899832d8cf22e5a6154099684832bca363635f83f",
"image": "sha256:1242e4006ac3f1ed15871efadca3d8d089e76f720017a1c51747133ee40cc953",
"started_at": "2026-11-16T17:09:28.448804963Z",
"running": true,
"user": "haproxy",
"volumes": []
},
"s3": {
"id": "57981f69f0ea7827b0e76bcacc206146976175c0cce9bea03e2f052d6b50079b",
"image": "sha256:4b42c212543a0acd4c0e675215ecdcaf62a186ef98def76c7e12701871222d15",
"started_at": "2026-11-16T17:09:28.677472764Z",
"running": true,
"user": "",
"volumes": [
"ccc5a16e7f94640852b33e1c29d82a46a9135ae6aa38e1148549c289666f7ba7"
]
},
"s3-storage": {
"id": "52f1bab5976874758cd0769268b845d0df13b9a0ed697970a33ef2feb2beb529",
"image": "sha256:4b42c212543a0acd4c0e675215ecdcaf62a186ef98def76c7e12701871222d15",
"started_at": "2026-11-16T17:09:38.325685797Z",
"running": true,
"user": "",
"volumes": [
"mtusi-lab4_s3data"
]
}
},
"stop_seconds": 1.002,
"metrics": {
"observed_instances_before": 2,
"successful_writes": 101,
"failed_attempts": 4,
"first_recovery_seconds": 2.753
}
},
"second_replica": {
"service": "api1",
"target_instance": "51a44cc9af98424bba787926800c0739",
"before": {
"api1": {
"instance_id": "51a44cc9af98424bba787926800c0739",
"container_id": "c88a212d829b1b5154928aafff160caf2f09c3d7ac6b92f632203d0ecdcf704a"
},
"api2": {
"instance_id": "dff2f0274e124a9fa6a264595912a404",
"container_id": "61c7c7c4ed4cbef04db5a6dab617f7b8805e361f2ce8da7e5e339e85f7e5543b"
}
},
"after": {
"api1": {
"id": "c88a212d829b1b5154928aafff160caf2f09c3d7ac6b92f632203d0ecdcf704a",
"image": "sha256:3a15e7611decf19224c446f3a652ea24cb698e4552756ca63e7df7d27cd2e098",
"started_at": "2026-11-16T17:09:28.828991567Z",
"running": false,
"user": "messenger",
"volumes": []
},
"api2": {
"id": "61c7c7c4ed4cbef04db5a6dab617f7b8805e361f2ce8da7e5e339e85f7e5543b",
"image": "sha256:3a15e7611decf19224c446f3a652ea24cb698e4552756ca63e7df7d27cd2e098",
"started_at": "2026-11-16T17:10:17.536145783Z",
"running": true,
"user": "messenger",
"volumes": []
},
"cleanup": {
"id": "17b7609d7f35945899e1b619821e1afee51dc3f6869854ff6a9fda00e2c71bda",
"image": "sha256:3a15e7611decf19224c446f3a652ea24cb698e4552756ca63e7df7d27cd2e098",
"started_at": "2026-11-16T17:09:38.269172115Z",
"running": true,
"user": "messenger",
"volumes": []
},
"db": {
"id": "a4ea88454a3740aa70ad1afd27ac3c34b04d18a2435ebebb32bed1d33cc3ad70",
"image": "sha256:665dc7b37ac52a17e298511414c21d23ffa4e6a6ef6225b3e5d59c7afb35e994",
"started_at": "2026-11-16T17:09:28.572826955Z",
"running": true,
"user": "",
"volumes": [
"mtusi-lab4_pgdata"
]
},
"lb": {
"id": "8c11ce96e2bd533072d4c60899832d8cf22e5a6154099684832bca363635f83f",
"image": "sha256:1242e4006ac3f1ed15871efadca3d8d089e76f720017a1c51747133ee40cc953",
"started_at": "2026-11-16T17:09:28.448804963Z",
"running": true,
"user": "haproxy",
"volumes": []
},
"s3": {
"id": "57981f69f0ea7827b0e76bcacc206146976175c0cce9bea03e2f052d6b50079b",
"image": "sha256:4b42c212543a0acd4c0e675215ecdcaf62a186ef98def76c7e12701871222d15",
"started_at": "2026-11-16T17:09:28.677472764Z",
"running": true,
"user": "",
"volumes": [
"ccc5a16e7f94640852b33e1c29d82a46a9135ae6aa38e1148549c289666f7ba7"
]
},
"s3-storage": {
"id": "52f1bab5976874758cd0769268b845d0df13b9a0ed697970a33ef2feb2beb529",
"image": "sha256:4b42c212543a0acd4c0e675215ecdcaf62a186ef98def76c7e12701871222d15",
"started_at": "2026-11-16T17:09:38.325685797Z",
"running": true,
"user": "",
"volumes": [
"mtusi-lab4_s3data"
]
}
},
"stop_seconds": 1.072,
"metrics": {
"successful_writes": 111,
"failed_attempts": 3,
"first_recovery_seconds": 2.892,
"replay_on_survivor": "PASS"
}
},
"status": "PASS"
}
+92
View File
@@ -0,0 +1,92 @@
{
"clients": 10,
"upload_slots_per_replica": 2,
"sampling_seconds": 0.05,
"profiles": [
{
"name": "near_byte_limit",
"file_bytes": 10484736,
"dimensions": [
32,
24
],
"successes": 10,
"errors": 0,
"seconds": 1.688,
"slowest_upload_seconds": 0.744,
"replicas_used": 2,
"originals_verified": 10,
"memory": {
"api1": {
"sampled_cgroup_peak_bytes": 156262400,
"sampled_rss_peak_bytes": 144613376,
"cgroup_lifetime_peak_bytes": 161787904,
"process_lifetime_hwm_bytes": 144613376,
"memory_limit_bytes": 805306368,
"oom_kills": 0
},
"api2": {
"sampled_cgroup_peak_bytes": 174948352,
"sampled_rss_peak_bytes": 163827712,
"cgroup_lifetime_peak_bytes": 177905664,
"process_lifetime_hwm_bytes": 163827712,
"memory_limit_bytes": 805306368,
"oom_kills": 0
}
}
},
{
"name": "near_pixel_limit",
"file_bytes": 165446,
"dimensions": [
8000,
5000
],
"successes": 10,
"errors": 0,
"seconds": 1.508,
"slowest_upload_seconds": 1.316,
"replicas_used": 2,
"originals_verified": 10,
"memory": {
"api1": {
"sampled_cgroup_peak_bytes": 434679808,
"sampled_rss_peak_bytes": 443617280,
"cgroup_lifetime_peak_bytes": 437039104,
"process_lifetime_hwm_bytes": 445100032,
"memory_limit_bytes": 805306368,
"oom_kills": 0
},
"api2": {
"sampled_cgroup_peak_bytes": 448606208,
"sampled_rss_peak_bytes": 456966144,
"cgroup_lifetime_peak_bytes": 451579904,
"process_lifetime_hwm_bytes": 459456512,
"memory_limit_bytes": 805306368,
"oom_kills": 0
}
}
}
],
"baseline": {
"api1": {
"current": 107954176,
"peak": 111235072,
"limit": 805306368,
"rss": 117202944,
"hwm": 117202944,
"oom_kill": 0,
"received_at": 343642.4571251
},
"api2": {
"current": 107565056,
"peak": 110854144,
"limit": 805306368,
"rss": 117194752,
"hwm": 117194752,
"oom_kill": 0,
"received_at": 343642.4558362
}
},
"status": "PASS"
}
+22
View File
@@ -0,0 +1,22 @@
{
"idempotency_after_restart": "PASS",
"public_persistence": "PASS",
"cursor_after_restart": "PASS",
"logout_after_restart": "PASS",
"migration_replay": "PASS",
"database": {
"users": 803,
"chats": 251,
"members": 490,
"messages": 1048,
"sessions": 782,
"migrations": 3,
"idempotency_keys": 1042,
"attachments": 88,
"message_attachments": 49,
"upload_intents": 0,
"expiry_fingerprint": "c0ec375682268715ee075f8eaf362ed6",
"passwords_are_argon2id": true,
"session_hashes_only": true
}
}
+131
View File
@@ -0,0 +1,131 @@
{
"project": "mtusi-lab4",
"before": {
"api1": {
"id": "c88a212d829b1b5154928aafff160caf2f09c3d7ac6b92f632203d0ecdcf704a",
"image": "sha256:3a15e7611decf19224c446f3a652ea24cb698e4552756ca63e7df7d27cd2e098",
"started_at": "2026-11-16T17:04:40.036086508Z",
"running": true,
"user": "messenger",
"volumes": []
},
"api2": {
"id": "61c7c7c4ed4cbef04db5a6dab617f7b8805e361f2ce8da7e5e339e85f7e5543b",
"image": "sha256:3a15e7611decf19224c446f3a652ea24cb698e4552756ca63e7df7d27cd2e098",
"started_at": "2026-11-16T17:08:56.894730335Z",
"running": true,
"user": "messenger",
"volumes": []
},
"cleanup": {
"id": "17b7609d7f35945899e1b619821e1afee51dc3f6869854ff6a9fda00e2c71bda",
"image": "sha256:3a15e7611decf19224c446f3a652ea24cb698e4552756ca63e7df7d27cd2e098",
"started_at": "2026-11-16T17:09:20.10425382Z",
"running": true,
"user": "messenger",
"volumes": []
},
"db": {
"id": "a4ea88454a3740aa70ad1afd27ac3c34b04d18a2435ebebb32bed1d33cc3ad70",
"image": "sha256:665dc7b37ac52a17e298511414c21d23ffa4e6a6ef6225b3e5d59c7afb35e994",
"started_at": "2026-11-16T17:03:23.752535588Z",
"running": true,
"user": "",
"volumes": [
"mtusi-lab4_pgdata"
]
},
"lb": {
"id": "8c11ce96e2bd533072d4c60899832d8cf22e5a6154099684832bca363635f83f",
"image": "sha256:1242e4006ac3f1ed15871efadca3d8d089e76f720017a1c51747133ee40cc953",
"started_at": "2026-11-16T17:03:23.620623554Z",
"running": true,
"user": "haproxy",
"volumes": []
},
"s3": {
"id": "57981f69f0ea7827b0e76bcacc206146976175c0cce9bea03e2f052d6b50079b",
"image": "sha256:4b42c212543a0acd4c0e675215ecdcaf62a186ef98def76c7e12701871222d15",
"started_at": "2026-11-16T17:09:11.262876373Z",
"running": true,
"user": "",
"volumes": [
"ccc5a16e7f94640852b33e1c29d82a46a9135ae6aa38e1148549c289666f7ba7"
]
},
"s3-storage": {
"id": "52f1bab5976874758cd0769268b845d0df13b9a0ed697970a33ef2feb2beb529",
"image": "sha256:4b42c212543a0acd4c0e675215ecdcaf62a186ef98def76c7e12701871222d15",
"started_at": "2026-11-16T17:03:33.502086278Z",
"running": true,
"user": "",
"volumes": [
"mtusi-lab4_s3data"
]
}
},
"after": {
"api1": {
"id": "c88a212d829b1b5154928aafff160caf2f09c3d7ac6b92f632203d0ecdcf704a",
"image": "sha256:3a15e7611decf19224c446f3a652ea24cb698e4552756ca63e7df7d27cd2e098",
"started_at": "2026-11-16T17:09:28.828991567Z",
"running": true,
"user": "messenger",
"volumes": []
},
"api2": {
"id": "61c7c7c4ed4cbef04db5a6dab617f7b8805e361f2ce8da7e5e339e85f7e5543b",
"image": "sha256:3a15e7611decf19224c446f3a652ea24cb698e4552756ca63e7df7d27cd2e098",
"started_at": "2026-11-16T17:09:28.754518187Z",
"running": true,
"user": "messenger",
"volumes": []
},
"cleanup": {
"id": "17b7609d7f35945899e1b619821e1afee51dc3f6869854ff6a9fda00e2c71bda",
"image": "sha256:3a15e7611decf19224c446f3a652ea24cb698e4552756ca63e7df7d27cd2e098",
"started_at": "2026-11-16T17:09:38.269172115Z",
"running": true,
"user": "messenger",
"volumes": []
},
"db": {
"id": "a4ea88454a3740aa70ad1afd27ac3c34b04d18a2435ebebb32bed1d33cc3ad70",
"image": "sha256:665dc7b37ac52a17e298511414c21d23ffa4e6a6ef6225b3e5d59c7afb35e994",
"started_at": "2026-11-16T17:09:28.572826955Z",
"running": true,
"user": "",
"volumes": [
"mtusi-lab4_pgdata"
]
},
"lb": {
"id": "8c11ce96e2bd533072d4c60899832d8cf22e5a6154099684832bca363635f83f",
"image": "sha256:1242e4006ac3f1ed15871efadca3d8d089e76f720017a1c51747133ee40cc953",
"started_at": "2026-11-16T17:09:28.448804963Z",
"running": true,
"user": "haproxy",
"volumes": []
},
"s3": {
"id": "57981f69f0ea7827b0e76bcacc206146976175c0cce9bea03e2f052d6b50079b",
"image": "sha256:4b42c212543a0acd4c0e675215ecdcaf62a186ef98def76c7e12701871222d15",
"started_at": "2026-11-16T17:09:28.677472764Z",
"running": true,
"user": "",
"volumes": [
"ccc5a16e7f94640852b33e1c29d82a46a9135ae6aa38e1148549c289666f7ba7"
]
},
"s3-storage": {
"id": "52f1bab5976874758cd0769268b845d0df13b9a0ed697970a33ef2feb2beb529",
"image": "sha256:4b42c212543a0acd4c0e675215ecdcaf62a186ef98def76c7e12701871222d15",
"started_at": "2026-11-16T17:09:38.325685797Z",
"running": true,
"user": "",
"volumes": [
"mtusi-lab4_s3data"
]
}
}
}
+79
View File
@@ -0,0 +1,79 @@
{
"service": "api2",
"target_instance": "dff2f0274e124a9fa6a264595912a404",
"before": {
"api1": {
"instance_id": "80f0af275acd4085a1ffc538a22458cc",
"container_id": "c88a212d829b1b5154928aafff160caf2f09c3d7ac6b92f632203d0ecdcf704a"
},
"api2": {
"instance_id": "dff2f0274e124a9fa6a264595912a404",
"container_id": "61c7c7c4ed4cbef04db5a6dab617f7b8805e361f2ce8da7e5e339e85f7e5543b"
}
},
"after": {
"api1": {
"id": "c88a212d829b1b5154928aafff160caf2f09c3d7ac6b92f632203d0ecdcf704a",
"image": "sha256:3a15e7611decf19224c446f3a652ea24cb698e4552756ca63e7df7d27cd2e098",
"started_at": "2026-11-16T17:10:48.440024009Z",
"running": true,
"user": "messenger",
"volumes": []
},
"api2": {
"id": "61c7c7c4ed4cbef04db5a6dab617f7b8805e361f2ce8da7e5e339e85f7e5543b",
"image": "sha256:3a15e7611decf19224c446f3a652ea24cb698e4552756ca63e7df7d27cd2e098",
"started_at": "2026-11-16T17:10:17.536145783Z",
"running": false,
"user": "messenger",
"volumes": []
},
"cleanup": {
"id": "17b7609d7f35945899e1b619821e1afee51dc3f6869854ff6a9fda00e2c71bda",
"image": "sha256:3a15e7611decf19224c446f3a652ea24cb698e4552756ca63e7df7d27cd2e098",
"started_at": "2026-11-16T17:09:38.269172115Z",
"running": true,
"user": "messenger",
"volumes": []
},
"db": {
"id": "a4ea88454a3740aa70ad1afd27ac3c34b04d18a2435ebebb32bed1d33cc3ad70",
"image": "sha256:665dc7b37ac52a17e298511414c21d23ffa4e6a6ef6225b3e5d59c7afb35e994",
"started_at": "2026-11-16T17:09:28.572826955Z",
"running": true,
"user": "",
"volumes": [
"mtusi-lab4_pgdata"
]
},
"lb": {
"id": "8c11ce96e2bd533072d4c60899832d8cf22e5a6154099684832bca363635f83f",
"image": "sha256:1242e4006ac3f1ed15871efadca3d8d089e76f720017a1c51747133ee40cc953",
"started_at": "2026-11-16T17:09:28.448804963Z",
"running": true,
"user": "haproxy",
"volumes": []
},
"s3": {
"id": "57981f69f0ea7827b0e76bcacc206146976175c0cce9bea03e2f052d6b50079b",
"image": "sha256:4b42c212543a0acd4c0e675215ecdcaf62a186ef98def76c7e12701871222d15",
"started_at": "2026-11-16T17:09:28.677472764Z",
"running": true,
"user": "",
"volumes": [
"ccc5a16e7f94640852b33e1c29d82a46a9135ae6aa38e1148549c289666f7ba7"
]
},
"s3-storage": {
"id": "52f1bab5976874758cd0769268b845d0df13b9a0ed697970a33ef2feb2beb529",
"image": "sha256:4b42c212543a0acd4c0e675215ecdcaf62a186ef98def76c7e12701871222d15",
"started_at": "2026-11-16T17:09:38.325685797Z",
"running": true,
"user": "",
"volumes": [
"mtusi-lab4_s3data"
]
}
},
"stop_seconds": 1.021
}
+423
View File
@@ -0,0 +1,423 @@
{
"date_utc": "2026-11-16T17:07:51.548950+00:00",
"platform": "Windows-11-10.0.26200-SP0",
"source_commit": "ec540a1a725862a8619fcb758daec4ad197f0dd5",
"worktree_dirty_at_start": true,
"project": "mtusi-lab4",
"status": "PASS",
"cleanup": {
"privacy": {
"anonymous_get": 403,
"other_bucket_get": 403,
"create_bucket": 403,
"change_acl": 403,
"bootstrap_replay": "PASS",
"storage_network_isolated": true,
"internal_ports_not_published": true
},
"fresh_intent_protected": "PASS",
"locked_intent_skipped": "PASS",
"orphan_removed_and_repeat_safe": "PASS",
"ready_unlinked_attachment_protected": "PASS",
"sql_failure_after_put_compensated": "PASS",
"api_killed_after_put_compensated": {
"status": "PASS",
"service": "api2"
},
"s3_outage_keeps_intent_then_recovers": "PASS",
"client_disconnect_has_no_publication": "PASS",
"periodic_cleanup_worker": "PASS"
},
"persistence": {
"idempotency_after_restart": "PASS",
"public_persistence": "PASS",
"cursor_after_restart": "PASS",
"logout_after_restart": "PASS",
"migration_replay": "PASS",
"database": {
"users": 803,
"chats": 251,
"members": 490,
"messages": 1048,
"sessions": 782,
"migrations": 3,
"idempotency_keys": 1042,
"attachments": 88,
"message_attachments": 49,
"upload_intents": 0,
"expiry_fingerprint": "c0ec375682268715ee075f8eaf362ed6",
"passwords_are_argon2id": true,
"session_hashes_only": true
}
},
"failover": {
"public": {
"service": "api2",
"target_instance": "ceea7b6044b3461e997e191c46443ba4",
"before": {
"api1": {
"instance_id": "51a44cc9af98424bba787926800c0739",
"container_id": "c88a212d829b1b5154928aafff160caf2f09c3d7ac6b92f632203d0ecdcf704a"
},
"api2": {
"instance_id": "ceea7b6044b3461e997e191c46443ba4",
"container_id": "61c7c7c4ed4cbef04db5a6dab617f7b8805e361f2ce8da7e5e339e85f7e5543b"
}
},
"after": {
"api1": {
"id": "c88a212d829b1b5154928aafff160caf2f09c3d7ac6b92f632203d0ecdcf704a",
"image": "sha256:3a15e7611decf19224c446f3a652ea24cb698e4552756ca63e7df7d27cd2e098",
"started_at": "2026-11-16T17:09:28.828991567Z",
"running": true,
"user": "messenger",
"volumes": []
},
"api2": {
"id": "61c7c7c4ed4cbef04db5a6dab617f7b8805e361f2ce8da7e5e339e85f7e5543b",
"image": "sha256:3a15e7611decf19224c446f3a652ea24cb698e4552756ca63e7df7d27cd2e098",
"started_at": "2026-11-16T17:09:28.754518187Z",
"running": false,
"user": "messenger",
"volumes": []
},
"cleanup": {
"id": "17b7609d7f35945899e1b619821e1afee51dc3f6869854ff6a9fda00e2c71bda",
"image": "sha256:3a15e7611decf19224c446f3a652ea24cb698e4552756ca63e7df7d27cd2e098",
"started_at": "2026-11-16T17:09:38.269172115Z",
"running": true,
"user": "messenger",
"volumes": []
},
"db": {
"id": "a4ea88454a3740aa70ad1afd27ac3c34b04d18a2435ebebb32bed1d33cc3ad70",
"image": "sha256:665dc7b37ac52a17e298511414c21d23ffa4e6a6ef6225b3e5d59c7afb35e994",
"started_at": "2026-11-16T17:09:28.572826955Z",
"running": true,
"user": "",
"volumes": [
"mtusi-lab4_pgdata"
]
},
"lb": {
"id": "8c11ce96e2bd533072d4c60899832d8cf22e5a6154099684832bca363635f83f",
"image": "sha256:1242e4006ac3f1ed15871efadca3d8d089e76f720017a1c51747133ee40cc953",
"started_at": "2026-11-16T17:09:28.448804963Z",
"running": true,
"user": "haproxy",
"volumes": []
},
"s3": {
"id": "57981f69f0ea7827b0e76bcacc206146976175c0cce9bea03e2f052d6b50079b",
"image": "sha256:4b42c212543a0acd4c0e675215ecdcaf62a186ef98def76c7e12701871222d15",
"started_at": "2026-11-16T17:09:28.677472764Z",
"running": true,
"user": "",
"volumes": [
"ccc5a16e7f94640852b33e1c29d82a46a9135ae6aa38e1148549c289666f7ba7"
]
},
"s3-storage": {
"id": "52f1bab5976874758cd0769268b845d0df13b9a0ed697970a33ef2feb2beb529",
"image": "sha256:4b42c212543a0acd4c0e675215ecdcaf62a186ef98def76c7e12701871222d15",
"started_at": "2026-11-16T17:09:38.325685797Z",
"running": true,
"user": "",
"volumes": [
"mtusi-lab4_s3data"
]
}
},
"stop_seconds": 1.002,
"metrics": {
"observed_instances_before": 2,
"successful_writes": 101,
"failed_attempts": 4,
"first_recovery_seconds": 2.753
}
},
"second_replica": {
"service": "api1",
"target_instance": "51a44cc9af98424bba787926800c0739",
"before": {
"api1": {
"instance_id": "51a44cc9af98424bba787926800c0739",
"container_id": "c88a212d829b1b5154928aafff160caf2f09c3d7ac6b92f632203d0ecdcf704a"
},
"api2": {
"instance_id": "dff2f0274e124a9fa6a264595912a404",
"container_id": "61c7c7c4ed4cbef04db5a6dab617f7b8805e361f2ce8da7e5e339e85f7e5543b"
}
},
"after": {
"api1": {
"id": "c88a212d829b1b5154928aafff160caf2f09c3d7ac6b92f632203d0ecdcf704a",
"image": "sha256:3a15e7611decf19224c446f3a652ea24cb698e4552756ca63e7df7d27cd2e098",
"started_at": "2026-11-16T17:09:28.828991567Z",
"running": false,
"user": "messenger",
"volumes": []
},
"api2": {
"id": "61c7c7c4ed4cbef04db5a6dab617f7b8805e361f2ce8da7e5e339e85f7e5543b",
"image": "sha256:3a15e7611decf19224c446f3a652ea24cb698e4552756ca63e7df7d27cd2e098",
"started_at": "2026-11-16T17:10:17.536145783Z",
"running": true,
"user": "messenger",
"volumes": []
},
"cleanup": {
"id": "17b7609d7f35945899e1b619821e1afee51dc3f6869854ff6a9fda00e2c71bda",
"image": "sha256:3a15e7611decf19224c446f3a652ea24cb698e4552756ca63e7df7d27cd2e098",
"started_at": "2026-11-16T17:09:38.269172115Z",
"running": true,
"user": "messenger",
"volumes": []
},
"db": {
"id": "a4ea88454a3740aa70ad1afd27ac3c34b04d18a2435ebebb32bed1d33cc3ad70",
"image": "sha256:665dc7b37ac52a17e298511414c21d23ffa4e6a6ef6225b3e5d59c7afb35e994",
"started_at": "2026-11-16T17:09:28.572826955Z",
"running": true,
"user": "",
"volumes": [
"mtusi-lab4_pgdata"
]
},
"lb": {
"id": "8c11ce96e2bd533072d4c60899832d8cf22e5a6154099684832bca363635f83f",
"image": "sha256:1242e4006ac3f1ed15871efadca3d8d089e76f720017a1c51747133ee40cc953",
"started_at": "2026-11-16T17:09:28.448804963Z",
"running": true,
"user": "haproxy",
"volumes": []
},
"s3": {
"id": "57981f69f0ea7827b0e76bcacc206146976175c0cce9bea03e2f052d6b50079b",
"image": "sha256:4b42c212543a0acd4c0e675215ecdcaf62a186ef98def76c7e12701871222d15",
"started_at": "2026-11-16T17:09:28.677472764Z",
"running": true,
"user": "",
"volumes": [
"ccc5a16e7f94640852b33e1c29d82a46a9135ae6aa38e1148549c289666f7ba7"
]
},
"s3-storage": {
"id": "52f1bab5976874758cd0769268b845d0df13b9a0ed697970a33ef2feb2beb529",
"image": "sha256:4b42c212543a0acd4c0e675215ecdcaf62a186ef98def76c7e12701871222d15",
"started_at": "2026-11-16T17:09:38.325685797Z",
"running": true,
"user": "",
"volumes": [
"mtusi-lab4_s3data"
]
}
},
"stop_seconds": 1.072,
"metrics": {
"successful_writes": 111,
"failed_attempts": 3,
"first_recovery_seconds": 2.892,
"replay_on_survivor": "PASS"
}
},
"status": "PASS"
},
"attachments_failover": {
"status": "PASS",
"cases": [
{
"stopped_service": "api1",
"upload_instance": "7f69d9826cae42aa945014734a7f9caa",
"download_instance": "dff2f0274e124a9fa6a264595912a404",
"recovery_seconds": 4.805,
"transitional_errors": 2,
"same_original": true,
"message_preserved": true
},
{
"stopped_service": "api2",
"upload_instance": "dff2f0274e124a9fa6a264595912a404",
"download_instance": "80f0af275acd4085a1ffc538a22458cc",
"recovery_seconds": 4.552,
"transitional_errors": 2,
"same_original": true,
"message_preserved": true
}
]
},
"expiry": {
"ttl_seconds": 2,
"expiry_unchanged_on_restart": true,
"before_restart": 200,
"after_expiry_and_restart": 401,
"new_login": 201
},
"memory": {
"clients": 10,
"upload_slots_per_replica": 2,
"sampling_seconds": 0.05,
"profiles": [
{
"name": "near_byte_limit",
"file_bytes": 10484736,
"dimensions": [
32,
24
],
"successes": 10,
"errors": 0,
"seconds": 1.688,
"slowest_upload_seconds": 0.744,
"replicas_used": 2,
"originals_verified": 10,
"memory": {
"api1": {
"sampled_cgroup_peak_bytes": 156262400,
"sampled_rss_peak_bytes": 144613376,
"cgroup_lifetime_peak_bytes": 161787904,
"process_lifetime_hwm_bytes": 144613376,
"memory_limit_bytes": 805306368,
"oom_kills": 0
},
"api2": {
"sampled_cgroup_peak_bytes": 174948352,
"sampled_rss_peak_bytes": 163827712,
"cgroup_lifetime_peak_bytes": 177905664,
"process_lifetime_hwm_bytes": 163827712,
"memory_limit_bytes": 805306368,
"oom_kills": 0
}
}
},
{
"name": "near_pixel_limit",
"file_bytes": 165446,
"dimensions": [
8000,
5000
],
"successes": 10,
"errors": 0,
"seconds": 1.508,
"slowest_upload_seconds": 1.316,
"replicas_used": 2,
"originals_verified": 10,
"memory": {
"api1": {
"sampled_cgroup_peak_bytes": 434679808,
"sampled_rss_peak_bytes": 443617280,
"cgroup_lifetime_peak_bytes": 437039104,
"process_lifetime_hwm_bytes": 445100032,
"memory_limit_bytes": 805306368,
"oom_kills": 0
},
"api2": {
"sampled_cgroup_peak_bytes": 448606208,
"sampled_rss_peak_bytes": 456966144,
"cgroup_lifetime_peak_bytes": 451579904,
"process_lifetime_hwm_bytes": 459456512,
"memory_limit_bytes": 805306368,
"oom_kills": 0
}
}
}
],
"baseline": {
"api1": {
"current": 107954176,
"peak": 111235072,
"limit": 805306368,
"rss": 117202944,
"hwm": 117202944,
"oom_kill": 0,
"received_at": 343642.4571251
},
"api2": {
"current": 107565056,
"peak": 110854144,
"limit": 805306368,
"rss": 117194752,
"hwm": 117194752,
"oom_kill": 0,
"received_at": 343642.4558362
}
},
"status": "PASS"
},
"dependency": {
"live_without_db": 200,
"ready_without_db": 503,
"business_without_db": 503,
"recovered": 200
},
"containers": {
"api1": {
"id": "c88a212d829b1b5154928aafff160caf2f09c3d7ac6b92f632203d0ecdcf704a",
"image": "sha256:3a15e7611decf19224c446f3a652ea24cb698e4552756ca63e7df7d27cd2e098",
"started_at": "2026-11-16T17:10:48.440024009Z",
"running": true,
"user": "messenger",
"volumes": []
},
"api2": {
"id": "61c7c7c4ed4cbef04db5a6dab617f7b8805e361f2ce8da7e5e339e85f7e5543b",
"image": "sha256:3a15e7611decf19224c446f3a652ea24cb698e4552756ca63e7df7d27cd2e098",
"started_at": "2026-11-16T17:10:57.712951729Z",
"running": true,
"user": "messenger",
"volumes": []
},
"cleanup": {
"id": "17b7609d7f35945899e1b619821e1afee51dc3f6869854ff6a9fda00e2c71bda",
"image": "sha256:3a15e7611decf19224c446f3a652ea24cb698e4552756ca63e7df7d27cd2e098",
"started_at": "2026-11-16T17:09:38.269172115Z",
"running": true,
"user": "messenger",
"volumes": []
},
"db": {
"id": "a4ea88454a3740aa70ad1afd27ac3c34b04d18a2435ebebb32bed1d33cc3ad70",
"image": "sha256:665dc7b37ac52a17e298511414c21d23ffa4e6a6ef6225b3e5d59c7afb35e994",
"started_at": "2026-11-16T17:12:40.149610572Z",
"running": true,
"user": "",
"volumes": [
"mtusi-lab4_pgdata"
]
},
"lb": {
"id": "8c11ce96e2bd533072d4c60899832d8cf22e5a6154099684832bca363635f83f",
"image": "sha256:1242e4006ac3f1ed15871efadca3d8d089e76f720017a1c51747133ee40cc953",
"started_at": "2026-11-16T17:09:28.448804963Z",
"running": true,
"user": "haproxy",
"volumes": []
},
"s3": {
"id": "57981f69f0ea7827b0e76bcacc206146976175c0cce9bea03e2f052d6b50079b",
"image": "sha256:4b42c212543a0acd4c0e675215ecdcaf62a186ef98def76c7e12701871222d15",
"started_at": "2026-11-16T17:09:28.677472764Z",
"running": true,
"user": "",
"volumes": [
"ccc5a16e7f94640852b33e1c29d82a46a9135ae6aa38e1148549c289666f7ba7"
]
},
"s3-storage": {
"id": "52f1bab5976874758cd0769268b845d0df13b9a0ed697970a33ef2feb2beb529",
"image": "sha256:4b42c212543a0acd4c0e675215ecdcaf62a186ef98def76c7e12701871222d15",
"started_at": "2026-11-16T17:09:38.325685797Z",
"running": true,
"user": "",
"volumes": [
"mtusi-lab4_s3data"
]
}
},
"docker": "29.9.3",
"resources": {
"cpus": 12,
"memory_bytes": 16591364096
}
}
+464
View File
@@ -0,0 +1,464 @@
Environment ready; existing settings and secrets preserved
$ ".\.venv\Scripts\python.exe" -X utf8 scripts/check_contract.py
PASS: offline skeleton structure, local refs and Python syntax; application NOT tested.
$ ".\.venv\Scripts\python.exe" -X utf8 scripts/validate_openapi.py
PASS: full OpenAPI 3.1 and JSON Schema validation; application NOT tested.
$ docker compose --project-name mtusi-lab4 --env-file .env config --quiet
$ docker compose --project-name mtusi-lab4 --env-file .env config --quiet
$ docker compose --project-name mtusi-lab4 --env-file .env build api1
Image mtusi-messenger:lab4 Building
#1 [internal] load local bake definitions
#1 reading from stdin 1.62kB 0.0s done
#1 DONE 0.0s
#2 [internal] load build definition from Dockerfile
#2 transferring dockerfile: 728B done
#2 DONE 0.0s
#3 [internal] load metadata for docker.io/library/python:3.13.14-slim-bookworm@sha256:67a1e1f215ccda113cfc024e8639049257e88f273898f595b61476d128d387e8
#3 ...
#4 [auth] library/python:pull token for registry-1.docker.io
#4 DONE 0.0s
#3 [internal] load metadata for docker.io/library/python:3.13.14-slim-bookworm@sha256:67a1e1f215ccda113cfc024e8639049257e88f273898f595b61476d128d387e8
#3 DONE 1.4s
#5 [internal] load .dockerignore
#5 transferring context: 246B done
#5 DONE 0.0s
#6 [internal] load build context
#6 transferring context: 718B done
#6 DONE 0.0s
#7 [1/6] FROM docker.io/library/python:3.13.14-slim-bookworm@sha256:67a1e1f215ccda113cfc024e8639049257e88f273898f595b61476d128d387e8
#7 resolve docker.io/library/python:3.13.14-slim-bookworm@sha256:67a1e1f215ccda113cfc024e8639049257e88f273898f595b61476d128d387e8 0.0s done
#7 DONE 0.0s
#8 [5/6] COPY src ./src
#8 CACHED
#9 [3/6] COPY requirements.lock .
#9 CACHED
#10 [4/6] RUN python -m pip install --no-cache-dir -r requirements.lock && useradd --uid 10001 --create-home messenger
#10 CACHED
#11 [2/6] WORKDIR /app
#11 CACHED
#12 [6/6] COPY migrations ./migrations
#12 CACHED
#13 exporting to image
#13 exporting layers done
#13 exporting manifest sha256:15855f982289abaaef8f69d5e09389faced04ee9149468059639ed924dce384b done
#13 exporting config sha256:b50ca6e39592250bbaa24ced9758dc882ac55f1a4155f94c7bcb114893acefff done
#13 exporting attestation manifest sha256:a1bb566d78313386b1f2bd5a4e92e7917c2abeed70e934bb91ddbe3815c24c99 0.0s done
#13 exporting manifest list sha256:e786e7f3cebb1a66cff27c0e3cbbc79c5329685486c7eda9d0378c9661ad49fa 0.0s done
#13 naming to docker.io/library/mtusi-messenger:lab4 done
#13 unpacking to docker.io/library/mtusi-messenger:lab4 done
#13 DONE 0.1s
#14 resolving provenance for metadata file
#14 DONE 0.0s
Image mtusi-messenger:lab4 Built
$ docker compose --project-name mtusi-lab4 --env-file .env run --rm --no-deps lb haproxy -c -f /usr/local/etc/haproxy/haproxy.cfg
Container mtusi-lab4-lb-run-f15b95fdc670 Creating
Container mtusi-lab4-lb-run-f15b95fdc670 Created
$ docker compose --project-name mtusi-lab4 --env-file .env up -d --no-build db migrate s3-storage s3 bootstrap api1 api2 cleanup lb
Container mtusi-lab4-s3-1 Running
Container mtusi-lab4-db-1 Running
Container mtusi-lab4-cleanup-1 Running
Container mtusi-lab4-api2-1 Running
Container mtusi-lab4-api1-1 Running
Container mtusi-lab4-s3-storage-1 Running
Container mtusi-lab4-lb-1 Running
Container mtusi-lab4-s3-storage-1 Waiting
Container mtusi-lab4-db-1 Waiting
Container mtusi-lab4-db-1 Healthy
Container mtusi-lab4-migrate-1 Starting
Container mtusi-lab4-s3-storage-1 Healthy
Container mtusi-lab4-bootstrap-1 Starting
Container mtusi-lab4-migrate-1 Started
Container mtusi-lab4-migrate-1 Waiting
Container mtusi-lab4-bootstrap-1 Started
Container mtusi-lab4-bootstrap-1 Waiting
Container mtusi-lab4-migrate-1 Exited
Container mtusi-lab4-bootstrap-1 Exited
Two API replicas ready through LB: http://127.0.0.1:8080
$ ".\.venv\Scripts\python.exe" -X utf8 tests/smoke.py --base-url http://127.0.0.1:8080
PASS: lab4 public API scenario; infrastructure and grading evidence remain separate.
$ ".\.venv\Scripts\python.exe" -X utf8 -m unittest discover -s student-tests -v
test_auth_and_access (test_api.ApiTests.test_auth_and_access) ... ok
test_bad_cursors_and_limits (test_api.ApiTests.test_bad_cursors_and_limits) ... ok
test_chat_pagination (test_api.ApiTests.test_chat_pagination) ... ok
test_chat_validation_is_atomic (test_api.ApiTests.test_chat_validation_is_atomic) ... ok
test_invalid_json_and_content_type (test_api.ApiTests.test_invalid_json_and_content_type) ... ok
test_message_fields_and_unicode (test_api.ApiTests.test_message_fields_and_unicode) ... ok
test_message_pagination (test_api.ApiTests.test_message_pagination) ... ok
test_parallel_messages (test_api.ApiTests.test_parallel_messages) ... ok
test_parallel_username (test_api.ApiTests.test_parallel_username) ... ok
test_read_after_write (test_api.ApiTests.test_read_after_write) ... ok
test_user_fields (test_api.ApiTests.test_user_fields) ... ok
test_attachment_access_and_unknown_ids (test_attachments.AttachmentTests.test_attachment_access_and_unknown_ids) ... ok
test_bad_attachment_list_and_other_chat_do_not_create_messages (test_attachments.AttachmentTests.test_bad_attachment_list_and_other_chat_do_not_create_messages) ... ok
test_byte_limit_and_original_at_exact_boundary (test_attachments.AttachmentTests.test_byte_limit_and_original_at_exact_boundary) ... ok
test_chunked_body_cannot_bypass_size_limit (test_attachments.AttachmentTests.test_chunked_body_cannot_bypass_size_limit) ... ok
test_corrupt_images_mime_and_multipart (test_attachments.AttachmentTests.test_corrupt_images_mime_and_multipart) ... ok
test_filename_validation (test_attachments.AttachmentTests.test_filename_validation) ... ok
test_idempotency_defaults_and_attachment_replay (test_attachments.AttachmentTests.test_idempotency_defaults_and_attachment_replay) ... ok
test_messages_keep_attachment_order_and_allow_empty_text (test_attachments.AttachmentTests.test_messages_keep_attachment_order_and_allow_empty_text) ... ok
test_original_png_jpeg_and_unicode_filename (test_attachments.AttachmentTests.test_original_png_jpeg_and_unicode_filename) ... ok
test_pixel_boundary_and_decompression_bomb (test_attachments.AttachmentTests.test_pixel_boundary_and_decompression_bomb) ... ok
test_different_body_conflicts (test_idempotency.IdempotencyTests.test_different_body_conflicts) ... ok
test_failure_rolls_back_message_and_key (test_idempotency.IdempotencyTests.test_failure_rolls_back_message_and_key) ... ok
test_invalid_body_does_not_reserve_key (test_idempotency.IdempotencyTests.test_invalid_body_does_not_reserve_key) ... ok
test_key_required_and_format (test_idempotency.IdempotencyTests.test_key_required_and_format) ... ok
test_replay_normalizes_json (test_idempotency.IdempotencyTests.test_replay_normalizes_json) ... ok
test_replay_still_checks_access_and_session (test_idempotency.IdempotencyTests.test_replay_still_checks_access_and_session) ... ok
test_retention_and_expired_key_reuse (test_idempotency.IdempotencyTests.test_retention_and_expired_key_reuse) ... ok
test_same_session_on_both_instances (test_idempotency.IdempotencyTests.test_same_session_on_both_instances) ... ok
test_scope_is_user_and_chat_not_session (test_idempotency.IdempotencyTests.test_scope_is_user_and_chat_not_session) ... ok
test_twenty_concurrent_requests_use_both_replicas (test_idempotency.IdempotencyTests.test_twenty_concurrent_requests_use_both_replicas) ... ok
test_logout_only_revokes_current_session (test_sessions.SessionTests.test_logout_only_revokes_current_session) ... ok
test_malformed_basic_and_body (test_sessions.SessionTests.test_malformed_basic_and_body) ... ok
test_only_bearer_identifies_user (test_sessions.SessionTests.test_only_bearer_identifies_user) ... ok
test_password_boundaries_unicode_colon_spaces (test_sessions.SessionTests.test_password_boundaries_unicode_colon_spaces) ... ok
test_password_required_and_limits (test_sessions.SessionTests.test_password_required_and_limits) ... ok
test_wrong_and_unknown_credentials (test_sessions.SessionTests.test_wrong_and_unknown_credentials) ... ok
----------------------------------------------------------------------
Ran 37 tests in 13.526s
OK
$ ".\.venv\Scripts\python.exe" -X utf8 student-tests/check_cleanup.py
$ docker compose --project-name mtusi-lab4 --env-file .env stop --timeout 3 cleanup
Container mtusi-lab4-cleanup-1 Stopping
Container mtusi-lab4-cleanup-1 Stopped
$ docker compose --project-name mtusi-lab4 --env-file .env run --rm --no-deps bootstrap
Container mtusi-lab4-bootstrap-run-0bf611b5d838 Creating
Container mtusi-lab4-bootstrap-run-0bf611b5d838 Created
PASS: private bucket ready; application credentials work
$ docker kill 61c7c7c4ed4cbef04db5a6dab617f7b8805e361f2ce8da7e5e339e85f7e5543b
61c7c7c4ed4cbef04db5a6dab617f7b8805e361f2ce8da7e5e339e85f7e5543b
$ docker compose --project-name mtusi-lab4 --env-file .env start api1 api2
Container mtusi-lab4-bootstrap-1 Starting
Container mtusi-lab4-db-1 Waiting
Container mtusi-lab4-db-1 Healthy
Container mtusi-lab4-migrate-1 Starting
Container mtusi-lab4-bootstrap-1 Started
Container mtusi-lab4-bootstrap-1 Waiting
Container mtusi-lab4-migrate-1 Started
Container mtusi-lab4-migrate-1 Waiting
Container mtusi-lab4-migrate-1 Exited
Container mtusi-lab4-bootstrap-1 Exited
Container mtusi-lab4-api2-1 Starting
Container mtusi-lab4-api2-1 Started
$ docker compose --project-name mtusi-lab4 --env-file .env stop --timeout 3 s3
Container mtusi-lab4-s3-1 Stopping
Container mtusi-lab4-s3-1 Stopped
$ docker compose --project-name mtusi-lab4 --env-file .env start s3
Container mtusi-lab4-s3-1 Starting
Container mtusi-lab4-s3-1 Started
$ docker compose --project-name mtusi-lab4 --env-file .env start cleanup
Container mtusi-lab4-bootstrap-1 Starting
Container mtusi-lab4-db-1 Waiting
Container mtusi-lab4-db-1 Healthy
Container mtusi-lab4-migrate-1 Starting
Container mtusi-lab4-bootstrap-1 Started
Container mtusi-lab4-bootstrap-1 Waiting
Container mtusi-lab4-migrate-1 Started
Container mtusi-lab4-migrate-1 Waiting
Container mtusi-lab4-migrate-1 Exited
Container mtusi-lab4-bootstrap-1 Exited
Container mtusi-lab4-cleanup-1 Starting
Container mtusi-lab4-cleanup-1 Started
PASS: private S3, restricted credentials, real faults and safe cleanup
$ docker compose --project-name mtusi-lab4 --env-file .env start s3 cleanup
Container mtusi-lab4-bootstrap-1 Starting
Container mtusi-lab4-db-1 Waiting
Container mtusi-lab4-db-1 Healthy
Container mtusi-lab4-migrate-1 Starting
Container mtusi-lab4-bootstrap-1 Started
Container mtusi-lab4-bootstrap-1 Waiting
Container mtusi-lab4-migrate-1 Started
Container mtusi-lab4-migrate-1 Waiting
Container mtusi-lab4-migrate-1 Exited
Container mtusi-lab4-bootstrap-1 Exited
$ docker compose --project-name mtusi-lab4 --env-file .env start api1 api2
Container mtusi-lab4-bootstrap-1 Starting
Container mtusi-lab4-db-1 Waiting
Container mtusi-lab4-db-1 Healthy
Container mtusi-lab4-migrate-1 Starting
Container mtusi-lab4-bootstrap-1 Started
Container mtusi-lab4-bootstrap-1 Waiting
Container mtusi-lab4-migrate-1 Started
Container mtusi-lab4-migrate-1 Waiting
Container mtusi-lab4-migrate-1 Exited
Container mtusi-lab4-bootstrap-1 Exited
$ ".\.venv\Scripts\python.exe" -X utf8 tests/resilience.py --mode persistence --action-script ".\scripts\restart.cmd" --base-url http://127.0.0.1:8080
Executing explicit persistence hook; hook output suppressed to avoid leaking secrets.
PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence.
$ docker compose --project-name mtusi-lab4 --env-file .env run --rm --no-deps migrate
Container mtusi-lab4-migrate-run-3472feb9bc14 Creating
Container mtusi-lab4-migrate-run-3472feb9bc14 Created
Already applied: 001_initial.sql
Already applied: 002_idempotency.sql
Already applied: 003_attachments.sql
$ docker compose --project-name mtusi-lab4 --env-file .env run --rm --no-deps migrate
Container mtusi-lab4-migrate-run-f27a2033193a Creating
Container mtusi-lab4-migrate-run-f27a2033193a Created
Already applied: 001_initial.sql
Already applied: 002_idempotency.sql
Already applied: 003_attachments.sql
PASS: old session/cursor, durable logout and repeatable migrations
$ ".\.venv\Scripts\python.exe" -X utf8 tests/resilience.py --mode failover --action-script ".\scripts\stop-one.cmd" --base-url http://127.0.0.1:8080
Executing explicit failover hook; hook output suppressed to avoid leaking secrets.
{"observed_instances_before": 2, "successful_writes": 101, "failed_attempts": 4, "first_recovery_seconds": 2.753}
PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence.
$ docker compose --project-name mtusi-lab4 --env-file .env start api1 api2
Container mtusi-lab4-bootstrap-1 Starting
Container mtusi-lab4-db-1 Waiting
Container mtusi-lab4-db-1 Healthy
Container mtusi-lab4-migrate-1 Starting
Container mtusi-lab4-bootstrap-1 Started
Container mtusi-lab4-bootstrap-1 Waiting
Container mtusi-lab4-migrate-1 Started
Container mtusi-lab4-migrate-1 Waiting
Container mtusi-lab4-migrate-1 Exited
Container mtusi-lab4-bootstrap-1 Exited
Container mtusi-lab4-api2-1 Starting
Container mtusi-lab4-api2-1 Started
PASS: both physical API replicas stopped separately; no lost messages or duplicates
$ docker compose --project-name mtusi-lab4 --env-file .env start api1 api2
Container mtusi-lab4-bootstrap-1 Starting
Container mtusi-lab4-db-1 Waiting
Container mtusi-lab4-db-1 Healthy
Container mtusi-lab4-migrate-1 Starting
Container mtusi-lab4-bootstrap-1 Started
Container mtusi-lab4-bootstrap-1 Waiting
Container mtusi-lab4-migrate-1 Started
Container mtusi-lab4-migrate-1 Waiting
Container mtusi-lab4-migrate-1 Exited
Container mtusi-lab4-bootstrap-1 Exited
Container mtusi-lab4-api1-1 Starting
Container mtusi-lab4-api1-1 Started
$ ".\scripts\stop-one.cmd"
$ docker stop --timeout 3 c88a212d829b1b5154928aafff160caf2f09c3d7ac6b92f632203d0ecdcf704a
c88a212d829b1b5154928aafff160caf2f09c3d7ac6b92f632203d0ecdcf704a
Stopped only api1
$ docker compose --project-name mtusi-lab4 --env-file .env start api1 api2
Container mtusi-lab4-bootstrap-1 Starting
Container mtusi-lab4-db-1 Waiting
Container mtusi-lab4-db-1 Healthy
Container mtusi-lab4-migrate-1 Starting
Container mtusi-lab4-bootstrap-1 Started
Container mtusi-lab4-bootstrap-1 Waiting
Container mtusi-lab4-migrate-1 Started
Container mtusi-lab4-migrate-1 Waiting
Container mtusi-lab4-migrate-1 Exited
Container mtusi-lab4-bootstrap-1 Exited
Container mtusi-lab4-api1-1 Starting
Container mtusi-lab4-api1-1 Started
$ ".\scripts\stop-one.cmd"
$ docker stop --timeout 3 61c7c7c4ed4cbef04db5a6dab617f7b8805e361f2ce8da7e5e339e85f7e5543b
61c7c7c4ed4cbef04db5a6dab617f7b8805e361f2ce8da7e5e339e85f7e5543b
Stopped only api2
$ docker compose --project-name mtusi-lab4 --env-file .env start api1 api2
Container mtusi-lab4-bootstrap-1 Starting
Container mtusi-lab4-db-1 Waiting
Container mtusi-lab4-db-1 Healthy
Container mtusi-lab4-migrate-1 Starting
Container mtusi-lab4-bootstrap-1 Started
Container mtusi-lab4-bootstrap-1 Waiting
Container mtusi-lab4-migrate-1 Started
Container mtusi-lab4-migrate-1 Waiting
Container mtusi-lab4-migrate-1 Exited
Container mtusi-lab4-bootstrap-1 Exited
Container mtusi-lab4-api2-1 Starting
Container mtusi-lab4-api2-1 Started
PASS: stopped each uploader; other API downloaded the same original
$ docker compose --project-name mtusi-lab4 --env-file .env start api1 api2
Container mtusi-lab4-bootstrap-1 Starting
Container mtusi-lab4-db-1 Waiting
Container mtusi-lab4-db-1 Healthy
Container mtusi-lab4-migrate-1 Starting
Container mtusi-lab4-bootstrap-1 Started
Container mtusi-lab4-bootstrap-1 Waiting
Container mtusi-lab4-migrate-1 Started
Container mtusi-lab4-migrate-1 Waiting
Container mtusi-lab4-migrate-1 Exited
Container mtusi-lab4-bootstrap-1 Exited
$ docker compose --project-name mtusi-lab4-ttl --env-file .env up -d --no-build db migrate s3-storage s3 bootstrap api1 api2 cleanup lb
Network mtusi-lab4-ttl_default Creating
Volume mtusi-lab4-ttl_pgdata Creating
Volume mtusi-lab4-ttl_pgdata Creating
Network mtusi-lab4-ttl_storage Creating
Network mtusi-lab4-ttl_default Creating
Network mtusi-lab4-ttl_storage Creating
Volume mtusi-lab4-ttl_s3data Creating
Volume mtusi-lab4-ttl_s3data Creating
Volume mtusi-lab4-ttl_pgdata Created
Volume mtusi-lab4-ttl_pgdata Created
Volume mtusi-lab4-ttl_s3data Created
Volume mtusi-lab4-ttl_s3data Created
Network mtusi-lab4-ttl_storage Created
Network mtusi-lab4-ttl_storage Created
Container mtusi-lab4-ttl-s3-storage-1 Creating
Network mtusi-lab4-ttl_default Created
Network mtusi-lab4-ttl_default Created
Container mtusi-lab4-ttl-db-1 Creating
Container mtusi-lab4-ttl-s3-storage-1 Created
Container mtusi-lab4-ttl-s3-1 Creating
Container mtusi-lab4-ttl-db-1 Created
Container mtusi-lab4-ttl-migrate-1 Creating
Container mtusi-lab4-ttl-s3-1 Created
Container mtusi-lab4-ttl-bootstrap-1 Creating
Container mtusi-lab4-ttl-migrate-1 Created
Container mtusi-lab4-ttl-bootstrap-1 Created
Container mtusi-lab4-ttl-cleanup-1 Creating
Container mtusi-lab4-ttl-api1-1 Creating
Container mtusi-lab4-ttl-api2-1 Creating
Container mtusi-lab4-ttl-api2-1 Created
Container mtusi-lab4-ttl-api1-1 Created
Container mtusi-lab4-ttl-lb-1 Creating
Container mtusi-lab4-ttl-cleanup-1 Created
Container mtusi-lab4-ttl-lb-1 Created
Container mtusi-lab4-ttl-db-1 Starting
Container mtusi-lab4-ttl-s3-storage-1 Starting
Container mtusi-lab4-ttl-db-1 Started
Container mtusi-lab4-ttl-db-1 Waiting
Container mtusi-lab4-ttl-s3-storage-1 Started
Container mtusi-lab4-ttl-s3-storage-1 Waiting
Container mtusi-lab4-ttl-db-1 Healthy
Container mtusi-lab4-ttl-migrate-1 Starting
Container mtusi-lab4-ttl-migrate-1 Started
Container mtusi-lab4-ttl-migrate-1 Waiting
Container mtusi-lab4-ttl-s3-storage-1 Healthy
Container mtusi-lab4-ttl-s3-1 Starting
Container mtusi-lab4-ttl-s3-1 Started
Container mtusi-lab4-ttl-bootstrap-1 Starting
Container mtusi-lab4-ttl-bootstrap-1 Started
Container mtusi-lab4-ttl-bootstrap-1 Waiting
Container mtusi-lab4-ttl-migrate-1 Exited
Container mtusi-lab4-ttl-bootstrap-1 Exited
Container mtusi-lab4-ttl-api1-1 Starting
Container mtusi-lab4-ttl-cleanup-1 Starting
Container mtusi-lab4-ttl-api2-1 Starting
Container mtusi-lab4-ttl-api1-1 Started
Container mtusi-lab4-ttl-cleanup-1 Started
Container mtusi-lab4-ttl-api2-1 Started
Container mtusi-lab4-ttl-lb-1 Starting
Container mtusi-lab4-ttl-lb-1 Started
Two API replicas ready through LB: http://127.0.0.1:18082
$ docker compose --project-name mtusi-lab4-ttl --env-file .env restart --timeout 3 api1 api2
Container mtusi-lab4-ttl-api2-1 Restarting
Container mtusi-lab4-ttl-api1-1 Restarting
Container mtusi-lab4-ttl-api2-1 Started
Container mtusi-lab4-ttl-api1-1 Started
PASS: TTL=2, expired token rejected after API restart; fresh login works
$ docker compose --project-name mtusi-lab4-ttl --env-file .env stop --timeout 10 lb api1 api2 cleanup s3 s3-storage db
Container mtusi-lab4-ttl-cleanup-1 Stopping
Container mtusi-lab4-ttl-lb-1 Stopping
Container mtusi-lab4-ttl-lb-1 Stopped
Container mtusi-lab4-ttl-api2-1 Stopping
Container mtusi-lab4-ttl-api1-1 Stopping
Container mtusi-lab4-ttl-api2-1 Stopped
Container mtusi-lab4-ttl-api1-1 Stopped
Container mtusi-lab4-ttl-cleanup-1 Stopped
Container mtusi-lab4-ttl-db-1 Stopping
Container mtusi-lab4-ttl-s3-1 Stopping
Container mtusi-lab4-ttl-db-1 Stopped
Container mtusi-lab4-ttl-s3-1 Stopped
Container mtusi-lab4-ttl-s3-storage-1 Stopping
Container mtusi-lab4-ttl-s3-storage-1 Stopped
$ ".\.venv\Scripts\python.exe" -X utf8 student-tests/check_memory.py --project mtusi-lab4-memory --port 18083
$ docker compose --project-name mtusi-lab4-memory --env-file .env stop --timeout 3 lb api1 api2 cleanup s3 s3-storage db
Container mtusi-lab4-memory-lb-1 Stopping
Container mtusi-lab4-memory-cleanup-1 Stopping
Container mtusi-lab4-memory-lb-1 Stopped
Container mtusi-lab4-memory-api2-1 Stopping
Container mtusi-lab4-memory-api1-1 Stopping
Container mtusi-lab4-memory-cleanup-1 Stopped
Container mtusi-lab4-memory-api1-1 Stopped
Container mtusi-lab4-memory-api2-1 Stopped
Container mtusi-lab4-memory-db-1 Stopping
Container mtusi-lab4-memory-s3-1 Stopping
Container mtusi-lab4-memory-s3-1 Stopped
Container mtusi-lab4-memory-db-1 Stopped
Container mtusi-lab4-memory-s3-storage-1 Stopping
Container mtusi-lab4-memory-s3-storage-1 Stopped
$ docker compose --project-name mtusi-lab4-memory --env-file .env rm -f api1 api2
Going to remove mtusi-lab4-memory-api2-1, mtusi-lab4-memory-api1-1
Container mtusi-lab4-memory-api1-1 Removing
Container mtusi-lab4-memory-api2-1 Removing
Container mtusi-lab4-memory-api1-1 Removed
Container mtusi-lab4-memory-api2-1 Removed
$ docker compose --project-name mtusi-lab4-memory --env-file .env up -d --no-build db migrate s3-storage s3 bootstrap api1 api2 cleanup lb
Container mtusi-lab4-memory-migrate-1 Recreate
Container mtusi-lab4-memory-s3-storage-1 Recreate
Container mtusi-lab4-memory-bootstrap-1 Recreate
Container mtusi-lab4-memory-migrate-1 Recreated
Container mtusi-lab4-memory-s3-storage-1 Recreated
Container mtusi-lab4-memory-bootstrap-1 Recreated
Container mtusi-lab4-memory-cleanup-1 Recreate
Container mtusi-lab4-memory-api1-1 Creating
Container mtusi-lab4-memory-api2-1 Creating
Container mtusi-lab4-memory-api1-1 Created
Container mtusi-lab4-memory-api2-1 Created
Container mtusi-lab4-memory-cleanup-1 Recreated
Container mtusi-lab4-memory-db-1 Starting
Container mtusi-lab4-memory-s3-storage-1 Starting
Container mtusi-lab4-memory-db-1 Started
Container mtusi-lab4-memory-db-1 Waiting
Container mtusi-lab4-memory-s3-storage-1 Started
Container mtusi-lab4-memory-s3-storage-1 Waiting
Container mtusi-lab4-memory-db-1 Healthy
Container mtusi-lab4-memory-migrate-1 Starting
Container mtusi-lab4-memory-migrate-1 Started
Container mtusi-lab4-memory-migrate-1 Waiting
Container mtusi-lab4-memory-s3-storage-1 Healthy
Container mtusi-lab4-memory-s3-1 Starting
Container mtusi-lab4-memory-s3-1 Started
Container mtusi-lab4-memory-bootstrap-1 Starting
Container mtusi-lab4-memory-bootstrap-1 Started
Container mtusi-lab4-memory-bootstrap-1 Waiting
Container mtusi-lab4-memory-migrate-1 Exited
Container mtusi-lab4-memory-bootstrap-1 Exited
Container mtusi-lab4-memory-api1-1 Starting
Container mtusi-lab4-memory-cleanup-1 Starting
Container mtusi-lab4-memory-api2-1 Starting
Container mtusi-lab4-memory-cleanup-1 Started
Container mtusi-lab4-memory-api1-1 Started
Container mtusi-lab4-memory-api2-1 Started
Container mtusi-lab4-memory-lb-1 Starting
Container mtusi-lab4-memory-lb-1 Started
Two API replicas ready through LB: http://127.0.0.1:18083
PASS: 10 concurrent uploads in both profiles; originals intact, no OOM or restarts
$ docker compose --project-name mtusi-lab4-memory --env-file .env stop --timeout 10 lb api1 api2 cleanup s3 s3-storage db
Container mtusi-lab4-memory-cleanup-1 Stopping
Container mtusi-lab4-memory-lb-1 Stopping
Container mtusi-lab4-memory-lb-1 Stopped
Container mtusi-lab4-memory-api2-1 Stopping
Container mtusi-lab4-memory-api1-1 Stopping
Container mtusi-lab4-memory-api1-1 Stopped
Container mtusi-lab4-memory-api2-1 Stopped
Container mtusi-lab4-memory-cleanup-1 Stopped
Container mtusi-lab4-memory-s3-1 Stopping
Container mtusi-lab4-memory-db-1 Stopping
Container mtusi-lab4-memory-db-1 Stopped
Container mtusi-lab4-memory-s3-1 Stopped
Container mtusi-lab4-memory-s3-storage-1 Stopping
Container mtusi-lab4-memory-s3-storage-1 Stopped
$ docker compose --project-name mtusi-lab4 --env-file .env stop --timeout 10 db
Container mtusi-lab4-db-1 Stopping
Container mtusi-lab4-db-1 Stopped
$ docker compose --project-name mtusi-lab4 --env-file .env start db
Container mtusi-lab4-db-1 Starting
Container mtusi-lab4-db-1 Started
PASS: DB outage -> live=200, ready/business=503; session works after recovery
$ ".\.venv\Scripts\python.exe" -X utf8 tests/smoke.py --base-url http://127.0.0.1:8080
PASS: lab4 public API scenario; infrastructure and grading evidence remain separate.
PASS: full lab4 verification
+44
View File
@@ -0,0 +1,44 @@
global
log stdout format raw local0
maxconn 256
defaults
mode http
log global
timeout connect 1s
timeout client 60s
timeout server 60s
timeout check 3s
retries 0
http-error status 503 content-type application/json lf-string '{"error":{"code":"unavailable","message":"API temporarily unavailable","request_id":"%[unique-id]"}}' hdr X-Request-Id %[unique-id]
resolvers docker
nameserver local 127.0.0.11:53
resolve_retries 2
timeout resolve 1s
timeout retry 1s
hold valid 1s
hold nx 1s
hold obsolete 1s
frontend public
bind :8080
unique-id-format %[uuid()]
use_backend api_live if { path /health/live }
default_backend api_ready
backend api_ready
balance roundrobin
option httpchk GET /health/ready
http-check expect status 200
default-server inter 1s fall 2 rise 1 resolvers docker init-addr libc,none
server first api1:8080 check
server second api2:8080 check
backend api_live
balance roundrobin
option httpchk GET /health/live
http-check expect status 200
default-server inter 1s fall 2 rise 1 resolvers docker init-addr libc,none
server first api1:8080 check
server second api2:8080 check
+40
View File
@@ -0,0 +1,40 @@
CREATE TABLE users (
id uuid PRIMARY KEY,
username varchar(32) NOT NULL UNIQUE CHECK (username ~ '^[a-z0-9_]{3,32}$'),
display_name varchar(100) NOT NULL CHECK (length(btrim(display_name)) > 0),
password_hash text NOT NULL,
created_at timestamptz NOT NULL DEFAULT clock_timestamp()
);
CREATE TABLE chats (
id uuid PRIMARY KEY,
title varchar(100) NOT NULL CHECK (length(btrim(title)) > 0),
created_at timestamptz NOT NULL DEFAULT clock_timestamp()
);
CREATE TABLE chat_members (
chat_id uuid NOT NULL REFERENCES chats(id),
user_id uuid NOT NULL REFERENCES users(id),
PRIMARY KEY (chat_id, user_id)
);
CREATE INDEX chat_members_user_idx ON chat_members(user_id, chat_id);
CREATE INDEX chats_history_idx ON chats(created_at, id);
CREATE TABLE messages (
id uuid PRIMARY KEY,
chat_id uuid NOT NULL REFERENCES chats(id),
sender_id uuid NOT NULL REFERENCES users(id),
text varchar(4000) NOT NULL CHECK (length(btrim(text)) > 0),
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
FOREIGN KEY (chat_id, sender_id) REFERENCES chat_members(chat_id, user_id)
);
CREATE INDEX messages_history_idx ON messages(chat_id, created_at, id);
CREATE TABLE sessions (
token_hash char(64) PRIMARY KEY,
user_id uuid NOT NULL REFERENCES users(id),
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
expires_at timestamptz NOT NULL,
CHECK (expires_at > created_at)
);
CREATE INDEX sessions_expiry_idx ON sessions(expires_at);
+15
View File
@@ -0,0 +1,15 @@
CREATE TABLE idempotency_keys (
user_id uuid NOT NULL REFERENCES users(id),
method varchar(8) NOT NULL CHECK (method = 'POST'),
path varchar(128) NOT NULL,
request_key varchar(128) NOT NULL CHECK (request_key ~ '^[A-Za-z0-9._:-]{1,128}$'),
request_hash char(64) NOT NULL,
message_id uuid NOT NULL REFERENCES messages(id),
response_status smallint NOT NULL CHECK (response_status = 201),
response_body jsonb NOT NULL,
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
expires_at timestamptz NOT NULL,
PRIMARY KEY (user_id, method, path, request_key),
CHECK (expires_at > created_at)
);
CREATE INDEX idempotency_expiry_idx ON idempotency_keys(expires_at);
+38
View File
@@ -0,0 +1,38 @@
CREATE TABLE attachments (
id uuid PRIMARY KEY,
chat_id uuid NOT NULL REFERENCES chats(id),
owner_id uuid NOT NULL REFERENCES users(id),
filename varchar(255) NOT NULL,
content_type text NOT NULL CHECK (content_type IN ('image/png', 'image/jpeg')),
size_bytes integer NOT NULL CHECK (size_bytes BETWEEN 1 AND 10485760),
sha256 char(64) NOT NULL CHECK (sha256 ~ '^[a-f0-9]{64}$'),
object_key text NOT NULL UNIQUE,
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
updated_at timestamptz NOT NULL DEFAULT clock_timestamp(),
UNIQUE (id, chat_id),
FOREIGN KEY (chat_id, owner_id) REFERENCES chat_members(chat_id, user_id)
);
CREATE INDEX attachments_chat_idx ON attachments(chat_id, created_at, id);
ALTER TABLE messages DROP CONSTRAINT messages_text_check;
ALTER TABLE messages ADD CONSTRAINT messages_id_chat_unique UNIQUE (id, chat_id);
CREATE TABLE message_attachments (
message_id uuid NOT NULL,
attachment_id uuid NOT NULL,
chat_id uuid NOT NULL,
position smallint NOT NULL CHECK (position BETWEEN 0 AND 9),
PRIMARY KEY (message_id, attachment_id),
UNIQUE (message_id, position),
FOREIGN KEY (message_id, chat_id) REFERENCES messages(id, chat_id),
FOREIGN KEY (attachment_id, chat_id) REFERENCES attachments(id, chat_id)
);
CREATE TABLE upload_intents (
id uuid PRIMARY KEY,
chat_id uuid NOT NULL REFERENCES chats(id),
owner_id uuid NOT NULL REFERENCES users(id),
object_key text NOT NULL UNIQUE,
created_at timestamptz NOT NULL DEFAULT clock_timestamp()
);
CREATE INDEX upload_intents_age_idx ON upload_intents(created_at, id);
+18
View File
@@ -0,0 +1,18 @@
attrs==26.1.0
certifi==2026.10.23
charset-normalizer==3.5.2
idna==3.20
jsonschema==4.26.0
jsonschema-path==0.3.4
jsonschema-specifications==2025.9.1
lazy-object-proxy==1.13.0
openapi-schema-validator==0.6.3
openapi-spec-validator==0.7.2
pathable==0.4.4
pyyaml==6.0.3
referencing==0.36.2
requests==2.34.2
rfc3339-validator==0.1.4
rpds-py==2026.9.1
six==1.17.0
urllib3==2.8.0
+30
View File
@@ -0,0 +1,30 @@
annotated-doc==0.0.5
annotated-types==0.8.0
anyio==4.16.0
argon2-cffi==25.1.0
argon2-cffi-bindings==26.1.0
boto3==1.44.3
botocore==1.44.3
cffi==2.1.1
click==8.5.1
fastapi==0.162.2
h11==0.16.0
idna==3.20
jmespath==1.1.0
opentelemetry-api==1.45.2
pillow==13.0.0
psycopg==3.3.8
psycopg-binary==3.3.8
psycopg-pool==3.3.3
pycparser==3.12
pydantic==2.14.3
pydantic_core==2.50.4
python-dateutil==2.9.0.post0
s3transfer==0.19.3
six==1.17.0
starlette==1.10.0
typing-inspection==0.4.5
typing_extensions==4.16.0
tzdata==2026.6
urllib3==2.8.0
uvicorn==0.54.0
+662
View File
@@ -0,0 +1,662 @@
import argparse
from datetime import datetime, timezone
import hashlib
import json
import os
from pathlib import Path
import platform
import secrets
import shutil
import socket
import subprocess
import sys
import time
from urllib.error import URLError
from urllib.request import ProxyHandler, build_opener
import venv
ROOT = Path(__file__).resolve().parents[1]
LOG = []
LOCAL_HTTP = build_opener(ProxyHandler({}))
def settings():
path = ROOT / ".env"
result = {}
if path.exists():
for line in path.read_text(encoding="utf-8-sig").splitlines():
key, separator, value = line.partition("=")
if separator and not key.strip().startswith("#"):
result[key.strip()] = value.strip()
return result
def environment(args):
result = os.environ.copy()
result.update(PYTHONUTF8="1", PYTHONIOENCODING="utf-8", PYTHONDONTWRITEBYTECODE="1",
COMPOSE_PROJECT_NAME=args.project, HTTP_PORT=str(args.port),
SESSION_TTL_SECONDS=str(args.ttl), LAB4_PROJECT=args.project,
LAB4_PORT=str(args.port), LAB4_TTL=str(args.ttl))
for key in ("NO_PROXY", "no_proxy"):
result[key] = ",".join(filter(None, [result.get(key), "localhost", "127.0.0.1"]))
return result
def say(value):
line = str(value).replace(str(ROOT), ".")
print(line, flush=True)
LOG.append(line)
def run(args, command, quiet=False):
command = [str(item) for item in command]
if not quiet:
say("$ " + subprocess.list2cmdline(command))
with subprocess.Popen(command, cwd=ROOT, env=environment(args), stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, text=True, encoding="utf-8", errors="replace") as process:
lines = []
for line in process.stdout:
lines.append(line)
if not quiet:
say(line.rstrip())
code = process.wait()
if code:
raise RuntimeError(f"{command[0]} exited with code {code}")
return "".join(lines)
def compose(args, *command, quiet=False):
return run(args, ["docker", "compose", "--project-name", args.project, "--env-file", ".env", *command], quiet)
def interpreter():
return ROOT / ".venv" / ("Scripts/python.exe" if os.name == "nt" else "bin/python")
def evidence(name, value):
path = ROOT / "evidence" / name
path.parent.mkdir(exist_ok=True)
content = json.dumps(value, ensure_ascii=False, indent=2) if not isinstance(value, str) else value
path.write_text(content.rstrip() + "\n", encoding="utf-8", newline="\n")
def setup(args):
if not (ROOT / ".env").exists():
shutil.copyfile(ROOT / ".env.example", ROOT / ".env")
folder = ROOT / "secrets"
folder.mkdir(exist_ok=True)
password_file = folder / "postgres_password"
if not password_file.exists():
password_file.write_text(secrets.token_hex(32) + "\n", encoding="ascii")
password = password_file.read_text(encoding="ascii").strip()
values = {"database_url": f"postgresql://messenger:{password}@db:5432/messenger",
"cursor_key": secrets.token_hex(32)}
for name, value in values.items():
path = folder / name
if not path.exists():
path.write_text(value + "\n", encoding="ascii")
if os.name != "nt":
path.chmod(0o644)
if os.name != "nt":
folder.chmod(0o700)
password_file.chmod(0o644)
for name in ("s3_access_key_id", "s3_secret_access_key", "s3_admin_access_key_id", "s3_admin_secret_access_key"):
path = folder / name
if not path.exists():
path.write_text(secrets.token_hex(32) + "\n", encoding="ascii")
if os.name != "nt":
path.chmod(0o644)
config = folder / "s3_config.json"
if not config.exists():
def key(name):
return (folder / name).read_text(encoding="ascii").strip()
bucket = settings().get("S3_BUCKET", "messenger-attachments")
identities = [
{"name": "bootstrap", "credentials": [{"accessKey": key("s3_admin_access_key_id"),
"secretKey": key("s3_admin_secret_access_key")}], "actions": ["Admin", "Read", "Write", "List"]},
{"name": "messenger", "credentials": [{"accessKey": key("s3_access_key_id"),
"secretKey": key("s3_secret_access_key")}],
"actions": [action + ":" + bucket for action in ("Read", "Write", "List")]},
]
config.write_text(json.dumps({"identities": identities}) + "\n", encoding="ascii")
if os.name != "nt":
config.chmod(0o644)
if not interpreter().exists():
venv.EnvBuilder(with_pip=True).create(ROOT / ".venv")
lockfiles = [ROOT / "requirements.lock", ROOT / "requirements-validation.lock"]
fingerprint = hashlib.sha256(b"".join(path.read_bytes() for path in lockfiles)).hexdigest()
marker = ROOT / ".venv/.lab-dependencies"
if not marker.exists() or marker.read_text(encoding="ascii") != fingerprint:
run(args, [interpreter(), "-X", "utf8", "-m", "pip", "install", "--disable-pip-version-check",
"--no-input", "--progress-bar", "off", "--quiet", "-r", lockfiles[0], "-r", lockfiles[1]])
marker.write_text(fingerprint, encoding="ascii")
say("Environment ready; existing settings and secrets preserved")
def base_url(args):
return f"http://127.0.0.1:{args.port}"
def wait_storage(args, timeout=90):
deadline = time.monotonic() + timeout
while time.monotonic() < deadline:
container = compose(args, "ps", "--all", "--quiet", "s3-storage", quiet=True).strip()
if container:
status = run(args, ["docker", "inspect", "--format", "{{.State.Health.Status}}", container], quiet=True).strip()
if status == "healthy":
return
time.sleep(0.5)
raise RuntimeError("S3 storage readiness timeout")
def wait_ready(args, timeout=90):
wait_storage(args, timeout)
deadline = time.monotonic() + timeout
while time.monotonic() < deadline:
try:
with LOCAL_HTTP.open(base_url(args) + "/health/ready", timeout=4) as response:
if response.status == 200 and json.load(response) == {"status": "ok"}:
return
except (URLError, OSError, ValueError):
pass
time.sleep(0.3)
raise RuntimeError("API readiness timeout")
def build(args):
compose(args, "config", "--quiet")
compose(args, "build", "api1")
compose(args, "run", "--rm", "--no-deps", "lb", "haproxy", "-c", "-f",
"/usr/local/etc/haproxy/haproxy.cfg")
def start(args):
current = compose(args, "ps", "--all", "--quiet", "lb", quiet=True).strip()
if not current:
with socket.socket() as probe:
try:
probe.bind(("127.0.0.1", args.port))
except OSError:
raise RuntimeError(f"Port {args.port} is busy; stop its container or use --port") from None
compose(args, "up", "-d", "--no-build", "db", "migrate", "s3-storage", "s3", "bootstrap", "api1", "api2", "cleanup", "lb")
wait_ready(args)
wait_replicas(args)
say("Two API replicas ready through LB: " + base_url(args))
def states(args):
services = ("api1", "api2", "db", "lb", "s3", "s3-storage", "cleanup")
containers = compose(args, "ps", "--all", "--quiet", *services, quiet=True).split()
if len(set(containers)) != len(services):
raise RuntimeError("Missing runtime container")
result = {}
for data in json.loads(run(args, ["docker", "inspect", *containers], quiet=True)):
labels = data["Config"].get("Labels", {})
service = labels.get("com.docker.compose.service")
if labels.get("com.docker.compose.project") != args.project or service not in services or service in result:
raise RuntimeError("Unexpected container ownership")
result[service] = {
"id": data["Id"], "image": data["Image"], "started_at": data["State"]["StartedAt"],
"running": data["State"]["Running"], "user": data["Config"]["User"],
"volumes": [mount["Name"] for mount in data["Mounts"] if mount["Type"] == "volume"],
}
return result
def restart(args):
before = states(args)
if not all(item["running"] for item in before.values()):
raise RuntimeError("All runtime containers must be running before restart")
compose(args, "restart", "--timeout", "10", "db", "s3-storage", "s3", "api1", "api2", "cleanup", "lb")
wait_storage(args)
compose(args, "run", "--rm", "--no-deps", "bootstrap")
after = states(args)
for service in before:
if before[service]["started_at"] == after[service]["started_at"]:
raise RuntimeError("Container was not restarted: " + service)
if before[service]["volumes"] != after[service]["volumes"]:
raise RuntimeError("Volume changed during restart")
evidence("restart.json", {"project": args.project, "before": before, "after": after})
say("PASS: both APIs, PostgreSQL, S3 and LB restarted, volumes preserved")
def contract_tests(args):
run(args, [interpreter(), "-X", "utf8", "scripts/check_contract.py"])
run(args, [interpreter(), "-X", "utf8", "scripts/validate_openapi.py"])
compose(args, "config", "--quiet")
def api_tests(args):
wait_ready(args)
run(args, [interpreter(), "-X", "utf8", "tests/smoke.py", "--base-url", base_url(args)])
os.environ["BASE_URL"] = base_url(args)
run(args, [interpreter(), "-X", "utf8", "-m", "unittest", "discover", "-s", "student-tests", "-v"])
def client(args):
sys.path.insert(0, str(ROOT / "tests"))
from client import Client
for key in ("NO_PROXY", "no_proxy"):
os.environ[key] = environment(args)[key]
return Client(base_url(args))
def database_stats(args):
sql = """SELECT json_build_object(
'users',(SELECT count(*) FROM users),'chats',(SELECT count(*) FROM chats),
'members',(SELECT count(*) FROM chat_members),'messages',(SELECT count(*) FROM messages),
'sessions',(SELECT count(*) FROM sessions),'migrations',(SELECT count(*) FROM schema_migrations),
'idempotency_keys',(SELECT count(*) FROM idempotency_keys),
'attachments',(SELECT count(*) FROM attachments),'message_attachments',(SELECT count(*) FROM message_attachments),
'upload_intents',(SELECT count(*) FROM upload_intents),
'expiry_fingerprint',(SELECT md5(COALESCE(string_agg(user_id::text || created_at::text || expires_at::text,
',' ORDER BY token_hash),'')) FROM sessions),
'passwords_are_argon2id',(SELECT bool_and(password_hash LIKE '$argon2id$%') FROM users),
'session_hashes_only',(SELECT bool_and(token_hash ~ '^[0-9a-f]{64}$') FROM sessions))"""
result = compose(args, "exec", "-T", "db", "psql", "-U", "messenger", "-d", "messenger",
"-At", "-c", sql, quiet=True)
return json.loads(result)
def restart_check(args):
from urllib.parse import urlencode
c = client(args)
alice, bob, eve, chat = c.bootstrap()
path = "/api/v1/chats/" + chat["id"] + "/messages"
replay_key = secrets.token_hex(16)
original = c.message(alice, chat, "first", key=replay_key)["json"]
last = c.message(bob, chat, "second")["json"]
page = c.call("GET", path + "?limit=1", actor=alice, schema="MessagePage")["json"]
revoked = c.user("revoked")
c.call("DELETE", "/api/v1/auth/sessions/current", 204, actor=revoked)
hook = ROOT / "scripts" / ("restart.cmd" if os.name == "nt" else "restart.sh")
run(args, [interpreter(), "-X", "utf8", "tests/resilience.py", "--mode", "persistence",
"--action-script", hook, "--base-url", base_url(args)])
continued = c.call("GET", path + "?" + urlencode({"cursor": page["next_cursor"]}),
actor=alice, schema="MessagePage")["json"]
if continued != {"items": [last], "next_cursor": None}:
raise RuntimeError("Old cursor did not survive restart")
c.call("GET", "/api/v1/users/me", 401, actor=revoked)
c.call("GET", "/api/v1/chats/" + chat["id"], 404, actor=eve)
before = database_stats(args)
compose(args, "run", "--rm", "--no-deps", "migrate")
compose(args, "run", "--rm", "--no-deps", "migrate")
after = database_stats(args)
stable_before = {key: value for key, value in before.items() if key != "upload_intents"}
stable_after = {key: value for key, value in after.items() if key != "upload_intents"}
if stable_before != stable_after or after["migrations"] != 3:
raise RuntimeError("Migration replay changed existing data")
if not after["passwords_are_argon2id"] or not after["session_hashes_only"]:
raise RuntimeError("Unexpected credential representation in DB")
repeated = c.message(alice, chat, "first", key=replay_key)["json"]
if repeated != original:
raise RuntimeError("Idempotency result was not preserved")
result = {"idempotency_after_restart": "PASS", "public_persistence": "PASS", "cursor_after_restart": "PASS",
"logout_after_restart": "PASS", "migration_replay": "PASS", "database": after}
evidence("persistence.json", result)
say("PASS: old session/cursor, durable logout and repeatable migrations")
return result
def ttl_check(args):
test = argparse.Namespace(project=args.project + "-ttl", port=args.test_port, ttl=2,
test_port=args.test_port, action="start")
try:
start(test)
c = client(test)
actor = c.user("expiry")
c.call("GET", "/api/v1/users/me", actor=actor, schema="User")
before = database_stats(test)["expiry_fingerprint"]
compose(test, "restart", "--timeout", "3", "api1", "api2")
wait_ready(test)
after = database_stats(test)["expiry_fingerprint"]
if before != after:
raise RuntimeError("Session expiry changed during restart")
time.sleep(2.2)
c.call("GET", "/api/v1/users/me", 401, actor=actor)
registration = actor["registration"]
import base64
basic = base64.b64encode((registration["username"] + ":" + registration["password"]).encode()).decode()
c.call("POST", "/api/v1/auth/sessions", 201, headers={"Authorization": "Basic " + basic}, schema="Session")
say("PASS: TTL=2, expired token rejected after API restart; fresh login works")
result = {"ttl_seconds": 2, "expiry_unchanged_on_restart": True, "before_restart": 200,
"after_expiry_and_restart": 401, "new_login": 201}
evidence("expiry.json", result)
return result
finally:
compose(test, "stop", "--timeout", "10", "lb", "api1", "api2", "cleanup", "s3", "s3-storage", "db")
def dependency_check(args):
c = client(args)
actor = c.user("dbcheck")
try:
compose(args, "stop", "--timeout", "10", "db")
c.call("GET", "/health/live", schema="Health")
outage_response(args, "/health/ready")
outage_response(args, "/api/v1/users/me", actor["token"])
finally:
compose(args, "start", "db")
wait_ready(args)
c.call("GET", "/api/v1/users/me", actor=actor, schema="User")
say("PASS: DB outage -> live=200, ready/business=503; session works after recovery")
return {"live_without_db": 200, "ready_without_db": 503, "business_without_db": 503, "recovered": 200}
def verify(args):
summary = {"date_utc": datetime.now(timezone.utc).isoformat(), "platform": platform.platform(),
"source_commit": run(args, ["git", "rev-parse", "HEAD"], quiet=True).strip(),
"worktree_dirty_at_start": bool(run(args, ["git", "status", "--porcelain"], quiet=True).strip()),
"project": args.project, "status": "FAIL"}
try:
setup(args)
contract_tests(args)
build(args)
start(args)
api_tests(args)
summary["cleanup"] = cleanup_check(args)
summary["persistence"] = restart_check(args)
summary["failover"] = failover_check(args)
summary["attachments_failover"] = attachment_failover_check(args)
summary["expiry"] = ttl_check(args)
summary["memory"] = memory_check(args)
summary["dependency"] = dependency_check(args)
run(args, [interpreter(), "-X", "utf8", "tests/smoke.py", "--base-url", base_url(args)])
summary["containers"] = states(args)
summary["docker"] = run(args, ["docker", "version", "--format", "{{.Server.Version}}"], quiet=True).strip()
summary["resources"] = json.loads(run(
args, ["docker", "info", "--format", '{"cpus":{{.NCPU}},"memory_bytes":{{.MemTotal}}}'], quiet=True))
summary["status"] = "PASS"
say("PASS: full lab4 verification")
finally:
evidence("verification.txt", "\n".join(LOG))
evidence("summary.json", summary)
def instance_map(args):
current = states(args)
result = {}
for service in ("api1", "api2"):
item = current[service]
if not item["running"]:
continue
code = (
"import urllib.request; "
"o=urllib.request.build_opener(urllib.request.ProxyHandler({})); "
"r=o.open('http://127.0.0.1:8080/health/live',timeout=1); "
"print(r.headers['X-Instance-Id'])"
)
alias = run(args, ["docker", "exec", item["id"], "python", "-c", code], quiet=True).strip()
import re
if not re.fullmatch(r"[0-9a-f]{32}", alias):
raise RuntimeError("Invalid API instance alias")
result[service] = {"instance_id": alias, "container_id": item["id"]}
return result
def wait_replicas(args):
deadline = time.monotonic() + 90
while time.monotonic() < deadline:
try:
mapping = instance_map(args)
if len(mapping) == 2:
c = client(args)
seen = {c.call("GET", "/health/ready", schema="Health")["headers"]["X-Instance-Id"]
for _ in range(8)}
if seen == {item["instance_id"] for item in mapping.values()}:
return mapping
except (RuntimeError, OSError, AssertionError):
pass
time.sleep(0.3)
raise RuntimeError("Two healthy API replicas were not observed through LB")
def restore(args):
compose(args, "start", "api1", "api2")
wait_ready(args)
return wait_replicas(args)
def stop_one(args):
target = os.environ.get("TARGET_INSTANCE_ID", "")
before = instance_map(args)
matches = [(name, item) for name, item in before.items() if item["instance_id"] == target]
if len(before) != 2 or len(matches) != 1:
raise RuntimeError("Target must match exactly one of two running API replicas")
service, item = matches[0]
started = time.monotonic()
run(args, ["docker", "stop", "--timeout", "3", item["container_id"]])
after = states(args)
if after[service]["running"] or not after["api1" if service == "api2" else "api2"]["running"]:
raise RuntimeError("Unexpected API state after stop")
elapsed = time.monotonic() - started
if elapsed >= 10:
raise RuntimeError("Stopping the selected API exceeded 10 seconds")
evidence("stop-one.json", {"service": service, "target_instance": target, "before": before,
"after": after, "stop_seconds": round(elapsed, 3)})
say("Stopped only " + service)
def failover_check(args):
wait_replicas(args)
hook = ROOT / "scripts" / ("stop-one.cmd" if os.name == "nt" else "stop-one.sh")
try:
output = run(args, [interpreter(), "-X", "utf8", "tests/resilience.py", "--mode", "failover",
"--action-script", hook, "--base-url", base_url(args)])
first = json.loads((ROOT / "evidence/stop-one.json").read_text(encoding="utf-8"))
first["metrics"] = next(json.loads(line) for line in output.splitlines()
if line.startswith("{") and "first_recovery_seconds" in line)
if first["after"][first["service"]]["running"]:
raise RuntimeError("Public hook did not leave the target stopped")
finally:
restore(args)
other_service = "api2" if first["service"] == "api1" else "api1"
mapping = instance_map(args)
target = mapping[other_service]["instance_id"]
c = client(args)
c.timeout = 2
alice, bob, eve, chat = c.bootstrap()
for _ in range(10):
replay_key = secrets.token_hex(16)
replay_text = "before-failure-" + replay_key
response = c.message(alice, chat, replay_text, key=replay_key)
if response["headers"]["X-Instance-Id"] == target:
original = response["json"]
break
else:
raise RuntimeError("Second API was not reached through LB")
env = environment(args)
env["TARGET_INSTANCE_ID"] = target
started = time.monotonic()
process = subprocess.Popen([str(hook)], cwd=ROOT, env=env, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
from client import Failure
from resilience import history
confirmations, after_instances = [], []
failures = 0
recovered = None
key = secrets.token_hex(16)
text = "during-failure-" + key
try:
while time.monotonic() - started < 15:
code = process.poll()
if code not in (None, 0):
raise RuntimeError("Second stop hook failed")
if code is None and time.monotonic() - started >= 10:
raise RuntimeError("Second stop hook exceeded 10 seconds")
try:
response = c.message(alice, chat, text, key=key)
confirmations.append(response["json"])
if code == 0:
if recovered is None:
recovered = time.monotonic() - started
after_instances.append(response["headers"]["X-Instance-Id"])
key = secrets.token_hex(16)
text = "during-failure-" + key
except Failure:
failures += 1
time.sleep(0.1)
if process.poll() != 0 or recovered is None or recovered > 10:
raise RuntimeError("Second API failure did not recover within 10 seconds")
if len(after_instances) < 5 or target in after_instances[-5:]:
raise RuntimeError("Stopped API still serves requests")
replayed = c.message(alice, chat, replay_text, key=replay_key)
if replayed["json"] != original or replayed["headers"]["X-Instance-Id"] == target:
raise RuntimeError("Message replay through the survivor changed the result")
messages = history(c, bob, chat)
ids = [item["id"] for item in messages]
texts = [item["text"] for item in messages]
if len(ids) != len(set(ids)) or len(texts) != len(set(texts)):
raise RuntimeError("Duplicates appeared during failover")
if any(item not in messages for item in [original, *confirmations]):
raise RuntimeError("A confirmed message was lost")
second = json.loads((ROOT / "evidence/stop-one.json").read_text(encoding="utf-8"))
if second["service"] != other_service:
raise RuntimeError("Wrong second API was stopped")
second["metrics"] = {"successful_writes": len(confirmations), "failed_attempts": failures,
"first_recovery_seconds": round(recovered, 3),
"replay_on_survivor": "PASS"}
result = {"public": first, "second_replica": second, "status": "PASS"}
evidence("failover.json", result)
say("PASS: both physical API replicas stopped separately; no lost messages or duplicates")
return result
finally:
if process.poll() is None:
process.terminate()
process.wait(timeout=5)
restore(args)
def outage_response(args, path, token=None):
from urllib.error import HTTPError
from urllib.request import Request
from client import shape
headers = {"Authorization": "Bearer " + token} if token else {}
try:
response = LOCAL_HTTP.open(Request(base_url(args) + path, headers=headers), timeout=10)
except HTTPError as error:
response = error
with response:
payload = json.load(response)
if response.status != 503 or not response.headers.get("X-Request-Id"):
raise RuntimeError("Unexpected dependency failure response")
shape(payload, "Error")
if payload["error"]["request_id"] != response.headers["X-Request-Id"]:
raise RuntimeError("LB error request ID mismatch")
def cleanup_check(args):
run(args, [interpreter(), "-X", "utf8", "student-tests/check_cleanup.py"])
return json.loads((ROOT / "evidence/cleanup.json").read_text(encoding="utf-8"))
def memory_check(args):
run(args, [interpreter(), "-X", "utf8", "student-tests/check_memory.py",
"--project", args.project + "-memory", "--port", str(args.test_port + 1)])
return json.loads((ROOT / "evidence/memory.json").read_text(encoding="utf-8"))
def attachment_failover_check(args):
import hashlib
results = []
raw = (ROOT / "tests/fixtures/sample.png").read_bytes()
try:
for service in ("api1", "api2"):
mapping = wait_replicas(args)
c = client(args)
alice, bob, eve, chat = c.bootstrap()
target = mapping[service]["instance_id"]
for _ in range(8):
uploaded = c.call(
"POST", "/api/v1/chats/" + chat["id"] + "/attachments?filename=failover.png", 201,
actor=alice, raw=raw, headers={"Content-Type": "image/png"}, schema="Attachment",
)
if uploaded["headers"]["X-Instance-Id"] == target:
break
else:
raise RuntimeError("Selected upload replica was not observed")
attachment = uploaded["json"]
linked = c.message(alice, chat, "", attachments=[attachment["id"]])["json"]
hook = ROOT / "scripts" / ("stop-one.cmd" if os.name == "nt" else "stop-one.sh")
previous = os.environ.get("TARGET_INSTANCE_ID")
os.environ["TARGET_INSTANCE_ID"] = target
started = time.monotonic()
try:
run(args, [str(hook)])
finally:
if previous is None:
os.environ.pop("TARGET_INSTANCE_ID", None)
else:
os.environ["TARGET_INSTANCE_ID"] = previous
from client import Client
reads = Client(base_url(args), timeout=2)
errors = 0
while True:
try:
downloaded = reads.call("GET", "/api/v1/attachments/" + attachment["id"] + "/content", actor=bob)
history_response = reads.call(
"GET", "/api/v1/chats/" + chat["id"] + "/messages", actor=bob, schema="MessagePage",
)
break
except (AssertionError, OSError) as error:
if isinstance(error, AssertionError) and not any(
text in str(error) for text in ("received 502", "received 503", "connection/TLS failure")
):
raise
errors += 1
if time.monotonic() - started >= 10:
raise RuntimeError("File and history did not recover within 10 seconds") from None
time.sleep(0.1)
recovered = time.monotonic() - started
if downloaded["bytes"] != raw or hashlib.sha256(raw).hexdigest() != attachment["sha256"]:
raise RuntimeError("Original file changed after API failure")
if downloaded["headers"]["X-Instance-Id"] == target or recovered > 10:
raise RuntimeError("Upload replica was not replaced")
history = history_response["json"]["items"]
if history_response["headers"]["X-Instance-Id"] == target:
raise RuntimeError("Stopped API returned history")
if history != [linked]:
raise RuntimeError("Attached message changed during failover")
results.append({"stopped_service": service, "upload_instance": target,
"download_instance": downloaded["headers"]["X-Instance-Id"],
"recovery_seconds": round(recovered, 3), "transitional_errors": errors,
"same_original": True, "message_preserved": True})
restore(args)
result = {"status": "PASS", "cases": results}
evidence("attachment-failover.json", result)
say("PASS: stopped each uploader; other API downloaded the same original")
return result
finally:
restore(args)
def main():
saved = settings()
parser = argparse.ArgumentParser()
parser.add_argument("action", choices=["setup", "build", "start", "test", "restart", "stop-one",
"restart-check", "failover-check", "cleanup-check", "memory-check", "restore", "stop", "verify"])
parser.add_argument("--project", default=os.environ.get("LAB4_PROJECT", saved.get("COMPOSE_PROJECT_NAME", "mtusi-lab4")))
parser.add_argument("--port", type=int, default=int(os.environ.get("LAB4_PORT", saved.get("HTTP_PORT", "8080"))))
parser.add_argument("--ttl", type=int, default=int(os.environ.get("LAB4_TTL", saved.get("SESSION_TTL_SECONDS", "3600"))))
parser.add_argument("--test-port", type=int, default=18082)
args = parser.parse_args()
if not 1 <= args.port <= 65535 or not 1 <= args.test_port <= 65534 or not 1 <= args.ttl <= 86400:
parser.error("Invalid port or session TTL")
if args.action in ("setup", "build", "start", "test", "restart-check", "failover-check", "cleanup-check", "memory-check"):
setup(args)
actions = {"setup": lambda item: None, "build": build, "start": start,
"test": lambda item: (contract_tests(item), api_tests(item)),
"restart": restart, "restart-check": restart_check, "verify": verify,
"stop-one": stop_one, "restore": restore, "failover-check": failover_check,
"cleanup-check": cleanup_check, "memory-check": memory_check,
"stop": lambda item: compose(item, "stop", "--timeout", "10", "lb", "api1", "api2", "cleanup", "s3", "s3-storage", "db")}
actions[args.action](args)
if __name__ == "__main__":
try:
main()
except (RuntimeError, OSError, ValueError) as error:
print("FAIL: " + str(error), file=sys.stderr)
raise SystemExit(1)
+3
View File
@@ -0,0 +1,3 @@
@echo off
python "%~dp0lab.py" restart
exit /b %errorlevel%
+4
View File
@@ -0,0 +1,4 @@
#!/bin/sh
set -eu
TASK_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
exec python3 "$TASK_DIR/lab.py" restart
+3
View File
@@ -0,0 +1,3 @@
@echo off
python "%~dp0lab.py" stop-one
exit /b %errorlevel%
+5
View File
@@ -0,0 +1,5 @@
#!/bin/sh
set -eu
: "${TARGET_INSTANCE_ID:?missing TARGET_INSTANCE_ID}"
TASK_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
exec python3 "$TASK_DIR/lab.py" stop-one
+1
View File
@@ -0,0 +1 @@
+21
View File
@@ -0,0 +1,21 @@
import logging
import os
import uvicorn
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO, format="%(levelname)s %(message)s")
logging.getLogger("messenger").info("environment=%s", os.environ.get("APP_ENV", "lab"))
port = int(os.environ.get("APP_PORT", "8080"))
if not 1 <= port <= 65535:
raise SystemExit("APP_PORT должен быть от 1 до 65535")
uvicorn.run(
"src.app:create_app",
factory=True,
host=os.environ.get("APP_HOST", "0.0.0.0"),
port=port,
workers=1,
access_log=False,
timeout_graceful_shutdown=2,
)
+198
View File
@@ -0,0 +1,198 @@
from contextlib import asynccontextmanager
import logging
import re
from uuid import UUID, uuid4
from urllib.parse import quote
from typing import Literal
from fastapi import FastAPI, Request, Response
from fastapi.exceptions import RequestValidationError
from fastapi.responses import JSONResponse, StreamingResponse
from starlette.background import BackgroundTask
from starlette.concurrency import run_in_threadpool
from starlette.exceptions import HTTPException
from .attachments import Attachments
from .objects import Objects
from .errors import ApiError, invalid
from .models import CreateChat, CreateMessage, CreateUser
from .security import basic_credentials, bearer_token
from .storage import PostgresStore
logger = logging.getLogger("messenger")
SESSION_PATH = "/api/v1/auth/sessions"
def uuid_value(value):
try:
return UUID(value)
except ValueError:
raise invalid("Неверный UUID") from None
def page_args(request):
raw = request.query_params.get("limit", "50")
normalized = raw.lstrip("0") or "0"
if re.fullmatch(r"[0-9]+", raw) is None or len(normalized) > 3 or not 1 <= int(normalized) <= 100:
raise invalid("Limit должен быть от 1 до 100")
return int(normalized), request.query_params.get("cursor")
def create_app():
store = PostgresStore()
objects = Objects()
attachments = Attachments(store, objects)
instance_id = uuid4().hex
@asynccontextmanager
async def lifespan(app):
await run_in_threadpool(store.open)
try:
yield
finally:
await run_in_threadpool(store.close)
objects.close()
app = FastAPI(redirect_slashes=False, docs_url=None, redoc_url=None, openapi_url=None, lifespan=lifespan)
app.state.store = store
def error_response(request, error):
request_id = request.state.request_id
headers = dict(error.headers)
if error.status == 401:
headers["WWW-Authenticate"] = (
'Basic realm="messenger", charset="UTF-8"' if request.url.path == SESSION_PATH
else 'Bearer realm="messenger"'
)
return JSONResponse(
status_code=error.status, headers=headers,
content={"error": {"code": error.code, "message": error.message, "request_id": request_id}},
)
@app.middleware("http")
async def request_context(request, call_next):
request.state.request_id = str(uuid4())
try:
path = request.url.path
registration = path == "/api/v1/users" and request.method == "POST"
login = path == SESSION_PATH and request.method == "POST"
if login:
request.state.credentials = basic_credentials(request.headers.get("Authorization"))
async for chunk in request.stream():
if chunk:
raise invalid("Вход не принимает тело запроса")
elif path.startswith("/api/v1/") and not registration:
token = bearer_token(request.headers.get("Authorization"))
request.state.user_id = await run_in_threadpool(store.authenticate, token)
request.state.token = token
upload = re.fullmatch(r"/api/v1/chats/[^/]+/attachments", path) is not None
if request.method == "POST" and path.startswith("/api/v1/") and not login and not upload:
media_type = request.headers.get("Content-Type", "").split(";", 1)[0].strip().lower()
if media_type != "application/json":
raise ApiError(415, "unsupported_media_type", "Нужен application/json")
try:
(await request.body()).decode("utf-8")
except UnicodeDecodeError:
raise invalid("JSON должен быть в UTF-8") from None
response = await call_next(request)
except ApiError as error:
response = error_response(request, error)
except Exception as error:
logger.error("request failed: %s", type(error).__name__)
response = error_response(request, ApiError(500, "internal_error", "Внутренняя ошибка"))
response.headers["X-Instance-Id"] = instance_id
response.headers["X-Request-Id"] = request.state.request_id
logger.info("%s %s %s request_id=%s", request.method, request.url.path, response.status_code, request.state.request_id)
return response
@app.exception_handler(ApiError)
async def api_error(request, error):
return error_response(request, error)
@app.exception_handler(RequestValidationError)
async def validation_error(request, error):
return error_response(request, invalid("Неверные поля запроса"))
@app.exception_handler(HTTPException)
async def http_error(request, error):
code = "not_found" if error.status_code == 404 else "invalid_request"
return error_response(request, ApiError(error.status_code, code, "Неверный запрос"))
@app.get("/health/live")
def live():
return {"status": "ok"}
@app.get("/health/ready")
def ready():
result = store.ready()
objects.ready()
return result
@app.post("/api/v1/users", status_code=201)
def create_user(body: CreateUser):
return store.create_user(body)
@app.post(SESSION_PATH, status_code=201)
def login(request: Request):
return store.create_session(*request.state.credentials)
@app.delete(SESSION_PATH + "/current", status_code=204)
def logout(request: Request):
store.logout(request.state.token)
return Response(status_code=204)
@app.get("/api/v1/users/me")
def me(request: Request):
return store.user(request.state.user_id)
@app.post("/api/v1/chats", status_code=201)
def create_chat(body: CreateChat, request: Request):
return store.create_chat(request.state.user_id, body)
@app.get("/api/v1/chats")
def list_chats(request: Request):
return store.list_chats(request.state.user_id, *page_args(request))
@app.get("/api/v1/chats/{chat_id}")
def chat(chat_id: str, request: Request):
return store.chat(request.state.user_id, uuid_value(chat_id))
@app.post("/api/v1/chats/{chat_id}/messages", status_code=201)
def create_message(chat_id: str, body: CreateMessage, request: Request):
key = request.headers.get("Idempotency-Key", "")
if re.fullmatch(r"[A-Za-z0-9._:-]{1,128}", key) is None:
raise invalid("Неверный Idempotency-Key")
return store.create_message(request.state.user_id, uuid_value(chat_id), body, key)
@app.get("/api/v1/chats/{chat_id}/messages")
def messages(chat_id: str, request: Request):
return store.list_messages(request.state.user_id, uuid_value(chat_id), *page_args(request))
@app.post("/api/v1/chats/{chat_id}/attachments", status_code=201)
async def upload(chat_id: str, filename: str, request: Request):
return await attachments.receive(request, uuid_value(chat_id), filename)
@app.get("/api/v1/attachments/{attachment_id}")
def attachment(attachment_id: str, request: Request):
return attachments.get(request.state.user_id, uuid_value(attachment_id))
@app.get("/api/v1/attachments/{attachment_id}/content")
def download(attachment_id: str, request: Request, variant: Literal["original"] = "original"):
row = attachments.get(request.state.user_id, uuid_value(attachment_id), internal=True)
body = objects.content(row["object_key"])
def chunks():
try:
while data := body.read(65536):
yield data
finally:
body.close()
return StreamingResponse(
chunks(), media_type=row["content_type"], background=BackgroundTask(body.close),
headers={"Content-Length": str(row["size_bytes"]), "X-Content-Type-Options": "nosniff",
"Content-Disposition": "attachment; filename*=UTF-8''" + quote(row["filename"], safe="")},
)
return app
+137
View File
@@ -0,0 +1,137 @@
import asyncio
import hashlib
import tempfile
import unicodedata
import warnings
from uuid import uuid4
import anyio
from PIL import Image, UnidentifiedImageError
from starlette.concurrency import run_in_threadpool
from starlette.requests import ClientDisconnect
from .errors import ApiError, invalid, not_found
from .storage import serialize
MAX_BYTES = 10485760
MAX_PIXELS = 40000000
Image.MAX_IMAGE_PIXELS = MAX_PIXELS
FIELDS = "id,chat_id,owner_id,filename,content_type,size_bytes,sha256,created_at,updated_at"
def public(row):
return {**serialize(row), "status": "ready", "variants": [], "error_code": None}
def filename_value(value):
if not 1 <= len(value) <= 255 or not value.strip():
raise invalid("Неверное имя файла")
if any(c in "/\\" or unicodedata.category(c) == "Cc" for c in value):
raise invalid("Неверное имя файла")
try:
value.encode("utf-8")
except UnicodeError:
raise invalid("Неверное имя файла") from None
return value
def validate_image(source, media_type):
try:
with warnings.catch_warnings():
warnings.simplefilter("error", Image.DecompressionBombWarning)
source.seek(0)
with Image.open(source, formats=["PNG", "JPEG"]) as image:
if image.width * image.height > MAX_PIXELS:
raise ApiError(413, "payload_too_large", "Слишком много пикселей")
actual = {"PNG": "image/png", "JPEG": "image/jpeg"}[image.format]
if actual != media_type:
raise ApiError(415, "unsupported_media_type", "Формат не совпадает с Content-Type")
image.verify()
source.seek(0)
with Image.open(source, formats=["PNG", "JPEG"]) as image:
image.load()
except (Image.DecompressionBombWarning, Image.DecompressionBombError):
raise ApiError(413, "payload_too_large", "Слишком много пикселей") from None
except (UnidentifiedImageError, OSError, ValueError, SyntaxError):
raise ApiError(415, "unsupported_media_type", "Повреждённое изображение") from None
class Attachments:
def __init__(self, store, objects):
self.store = store
self.objects = objects
self.slots = asyncio.Semaphore(2)
async def receive(self, request, chat_id, filename):
user_id = request.state.user_id
await run_in_threadpool(self.store.chat, user_id, chat_id)
filename = filename_value(filename)
media_type = request.headers.get("Content-Type", "").split(";", 1)[0].strip().lower()
if media_type not in ("image/png", "image/jpeg"):
raise ApiError(415, "unsupported_media_type", "Нужен PNG или JPEG")
declared = request.headers.get("Content-Length")
if declared is not None:
if not declared.isascii() or not declared.isdecimal() or len(declared) > 18:
raise invalid("Неверный Content-Length")
if int(declared) > MAX_BYTES:
raise ApiError(413, "payload_too_large", "Файл больше 10 MiB")
try:
await asyncio.wait_for(self.slots.acquire(), timeout=15)
except TimeoutError:
raise ApiError(429, "rate_limited", "Загрузки заняты", {"Retry-After": "1"}) from None
try:
with tempfile.TemporaryFile(mode="w+b") as source:
size, checksum = 0, hashlib.sha256()
try:
with anyio.fail_after(15):
async for chunk in request.stream():
size += len(chunk)
if size > MAX_BYTES:
raise ApiError(413, "payload_too_large", "Файл больше 10 MiB")
checksum.update(chunk)
await run_in_threadpool(source.write, chunk)
except (TimeoutError, ClientDisconnect):
raise invalid("Загрузка прервана или слишком медленная") from None
await run_in_threadpool(validate_image, source, media_type)
return await run_in_threadpool(
self.publish, user_id, chat_id, filename, media_type, size, checksum.hexdigest(), source,
)
finally:
self.slots.release()
def publish(self, user_id, chat_id, filename, media_type, size, checksum, source):
attachment_id = uuid4()
key = "originals/" + attachment_id.hex
with self.store.connection() as connection:
self.store.require_chat(connection, user_id, chat_id)
connection.execute(
"INSERT INTO upload_intents(id,chat_id,owner_id,object_key) VALUES (%s,%s,%s,%s)",
(attachment_id, chat_id, user_id, key),
)
# intent остаётся даже после падения API
with self.store.connection() as connection:
intent = connection.execute(
"SELECT id FROM upload_intents WHERE id=%s FOR UPDATE", (attachment_id,),
).fetchone()
if not intent:
raise ApiError(503, "unavailable", "Загрузка уже закрыта")
self.objects.put(key, source, size, media_type)
row = connection.execute(
"INSERT INTO attachments(id,chat_id,owner_id,filename,content_type,size_bytes,sha256,object_key) "
"VALUES (%s,%s,%s,%s,%s,%s,%s,%s) RETURNING " + FIELDS,
(attachment_id, chat_id, user_id, filename, media_type, size, checksum, key),
).fetchone()
connection.execute("DELETE FROM upload_intents WHERE id=%s", (attachment_id,))
return public(row)
def get(self, user_id, attachment_id, internal=False):
with self.store.connection() as connection:
row = connection.execute(
"SELECT " + FIELDS + (",object_key" if internal else "") + " FROM attachments a "
"WHERE a.id=%s AND EXISTS (SELECT 1 FROM chat_members m "
"WHERE m.chat_id=a.chat_id AND m.user_id=%s)", (attachment_id, user_id),
).fetchone()
if not row:
raise not_found()
return row if internal else public(row)
+53
View File
@@ -0,0 +1,53 @@
import logging
import time
from io import BytesIO
from botocore.exceptions import BotoCoreError, ClientError
from .objects import Objects
from .errors import ApiError
def main():
admin, app = Objects(admin=True), Objects()
try:
deadline = time.monotonic() + 90
while True:
try:
admin.client.list_buckets()
break
except (ClientError, BotoCoreError):
if time.monotonic() >= deadline:
raise RuntimeError("S3 startup timeout") from None
time.sleep(1)
try:
admin.client.head_bucket(Bucket=admin.bucket)
except ClientError as error:
if error.response["ResponseMetadata"]["HTTPStatusCode"] != 404:
raise RuntimeError("Bucket is not accessible") from None
admin.client.create_bucket(Bucket=admin.bucket)
acl = admin.client.get_bucket_acl(Bucket=admin.bucket)
if any("URI" in grant["Grantee"] for grant in acl["Grants"]):
raise RuntimeError("Bucket must be private")
while True:
try:
probe = b"messenger-ready"
app.put("system/readiness", BytesIO(probe), len(probe), "application/octet-stream")
app.ready()
break
except ApiError:
if time.monotonic() >= deadline:
raise RuntimeError("S3 object path is not ready") from None
time.sleep(0.5)
print("PASS: private bucket ready; application credentials work", flush=True)
finally:
admin.close()
app.close()
if __name__ == "__main__":
try:
main()
except Exception as error:
logging.error("S3 bootstrap failed: %s", type(error).__name__)
raise SystemExit(1)
+63
View File
@@ -0,0 +1,63 @@
import argparse
import json
import logging
import os
import time
from .errors import ApiError
from .objects import Objects
from .storage import PostgresStore
def clean_once(store, objects, grace=600):
result = {"deleted": 0, "protected": 0}
for _ in range(100):
with store.connection() as connection:
intent = connection.execute(
"SELECT id,object_key FROM upload_intents "
"WHERE created_at < clock_timestamp() - %s * INTERVAL '1 second' "
"ORDER BY created_at,id LIMIT 1 FOR UPDATE SKIP LOCKED", (grace,),
).fetchone()
if not intent:
break
ready = connection.execute(
"SELECT id FROM attachments WHERE object_key=%s", (intent["object_key"],),
).fetchone()
if ready:
result["protected"] += 1
else:
objects.delete(intent["object_key"])
result["deleted"] += 1
connection.execute("DELETE FROM upload_intents WHERE id=%s", (intent["id"],))
return result
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--once", action="store_true")
args = parser.parse_args()
store, objects = PostgresStore(), Objects()
store.open()
try:
while True:
try:
result = clean_once(store, objects, int(os.environ.get("CLEANUP_GRACE_SECONDS", "600")))
print(json.dumps(result), flush=True)
except ApiError:
if args.once:
raise
logging.warning("cleanup: storage unavailable, retry later")
if args.once:
return
time.sleep(int(os.environ.get("CLEANUP_INTERVAL_SECONDS", "30")))
finally:
objects.close()
store.close()
if __name__ == "__main__":
try:
main()
except Exception as error:
logging.error("Cleanup failed: %s", type(error).__name__)
raise SystemExit(1)
+23
View File
@@ -0,0 +1,23 @@
import os
from pathlib import Path
def secret(name):
value = Path(os.environ[name]).read_text(encoding="utf-8").strip()
if not value:
raise ValueError(f"Empty secret file: {name}")
return value
def session_ttl():
value = int(os.environ.get("SESSION_TTL_SECONDS", "3600"))
if not 1 <= value <= 86400:
raise ValueError("SESSION_TTL_SECONDS must be between 1 and 86400")
return value
def idempotency_ttl():
value = int(os.environ.get("IDEMPOTENCY_TTL_SECONDS", "86400"))
if value < 86400:
raise ValueError("IDEMPOTENCY_TTL_SECONDS must be at least 86400")
return value
+15
View File
@@ -0,0 +1,15 @@
class ApiError(Exception):
def __init__(self, status, code, message, headers=None):
super().__init__(message)
self.status = status
self.code = code
self.message = message
self.headers = headers or {}
def invalid(message="Неверный запрос"):
return ApiError(400, "invalid_request", message)
def not_found():
return ApiError(404, "not_found", "Объект не найден")
+50
View File
@@ -0,0 +1,50 @@
import hashlib
import logging
from pathlib import Path
import psycopg
from .config import secret
def migrate():
files = sorted((Path(__file__).resolve().parents[1] / "migrations").glob("*.sql"))
if not files:
raise RuntimeError("No migrations found")
with psycopg.connect(secret("DATABASE_URL_FILE"), connect_timeout=5) as connection:
with connection.transaction():
connection.execute("SELECT pg_advisory_xact_lock(230402)")
connection.execute("""
CREATE TABLE IF NOT EXISTS schema_migrations (
version text PRIMARY KEY,
checksum char(64) NOT NULL,
applied_at timestamptz NOT NULL DEFAULT clock_timestamp()
)
""")
for file in files:
content = file.read_text(encoding="utf-8")
checksum = hashlib.sha256(content.encode("utf-8")).hexdigest()
with connection.transaction():
connection.execute("SELECT pg_advisory_xact_lock(230402)")
old = connection.execute(
"SELECT checksum FROM schema_migrations WHERE version = %s", (file.name,)
).fetchone()
if old:
if old[0] != checksum:
raise RuntimeError("Applied migration was changed")
print("Already applied: " + file.name, flush=True)
continue
connection.execute(content)
connection.execute(
"INSERT INTO schema_migrations(version, checksum) VALUES (%s, %s)",
(file.name, checksum),
)
print("Applied: " + file.name, flush=True)
if __name__ == "__main__":
try:
migrate()
except Exception as error:
logging.error("Migration failed: %s", type(error).__name__)
raise SystemExit(1)
+61
View File
@@ -0,0 +1,61 @@
import re
from typing import Annotated
from uuid import UUID
from pydantic import AfterValidator, BaseModel, ConfigDict, Field, StringConstraints, field_validator, model_validator
def nonblank(value: str) -> str:
if not value.strip() or "\x00" in value:
raise ValueError("Пустая строка или NUL")
value.encode("utf-8")
return value
Name = Annotated[str, StringConstraints(min_length=1, max_length=100), AfterValidator(nonblank)]
Text = Annotated[str, StringConstraints(min_length=1, max_length=4000), AfterValidator(nonblank)]
Password = Annotated[str, StringConstraints(min_length=12, max_length=128), AfterValidator(nonblank)]
UserId = Annotated[UUID, Field(strict=False)]
class RequestBody(BaseModel):
model_config = ConfigDict(extra="forbid", strict=True)
class CreateUser(RequestBody):
username: str
display_name: Name
password: Password = Field(repr=False)
@field_validator("username")
@classmethod
def check_username(cls, value):
if re.fullmatch(r"[a-z0-9_]{3,32}", value) is None:
raise ValueError("Неверный username")
return value
class CreateChat(RequestBody):
title: Name
member_ids: list[UserId] = Field(min_length=1, max_length=100)
@field_validator("member_ids")
@classmethod
def unique_members(cls, value):
if len(set(value)) != len(value):
raise ValueError("Повтор участника")
return value
class CreateMessage(RequestBody):
text: str = Field(max_length=4000)
attachment_ids: list[UserId] = Field(default_factory=list, max_length=10)
@model_validator(mode="after")
def check_message(self):
if "\x00" in self.text or (not self.text.strip() and not self.attachment_ids):
raise ValueError("Нужен текст или вложение")
self.text.encode("utf-8")
if len(set(self.attachment_ids)) != len(self.attachment_ids):
raise ValueError("Повтор вложения")
return self
+57
View File
@@ -0,0 +1,57 @@
import os
import boto3
from botocore.config import Config
from botocore.exceptions import BotoCoreError, ClientError
from .config import secret
from .errors import ApiError
class Objects:
def __init__(self, admin=False):
prefix = "S3_ADMIN_" if admin else "S3_"
self.bucket = os.environ.get("S3_BUCKET", "messenger-attachments")
self.client = boto3.client(
"s3", endpoint_url=os.environ.get("S3_ENDPOINT", "http://s3:9000"),
region_name=os.environ.get("S3_REGION", "us-east-1"),
aws_access_key_id=secret(prefix + "ACCESS_KEY_ID_FILE"),
aws_secret_access_key=secret(prefix + "SECRET_ACCESS_KEY_FILE"),
config=Config(
signature_version="s3v4", connect_timeout=2, read_timeout=5,
max_pool_connections=8, retries={"total_max_attempts": 1}, proxies={},
s3={"addressing_style": "path"},
request_checksum_calculation="when_required",
response_checksum_validation="when_required",
),
)
def call(self, operation, **values):
try:
return getattr(self.client, operation)(Bucket=self.bucket, **values)
except (BotoCoreError, ClientError):
raise ApiError(503, "unavailable", "Файловое хранилище недоступно") from None
def ready(self):
self.call("head_bucket")
body = self.content("system/readiness")
try:
if body.read(32) != b"messenger-ready":
raise ApiError(503, "unavailable", "Файловое хранилище не готово")
except BotoCoreError:
raise ApiError(503, "unavailable", "Файловое хранилище недоступно") from None
finally:
body.close()
def put(self, key, source, size, content_type):
source.seek(0)
self.call("put_object", Key=key, Body=source, ContentLength=size, ContentType=content_type)
def delete(self, key):
self.call("delete_object", Key=key)
def content(self, key):
return self.call("get_object", Key=key)["Body"]
def close(self):
self.client.close()
+44
View File
@@ -0,0 +1,44 @@
import base64
import hashlib
import hmac
import json
from datetime import datetime, timezone
from uuid import UUID
from .errors import invalid
class Pagination:
def __init__(self, secret):
self.secret = secret
def encode(self, scope, item):
data = json.dumps([1, scope, item["created_at"], item["id"]], separators=(",", ":"))
payload = base64.urlsafe_b64encode(data.encode()).rstrip(b"=").decode()
signature = hmac.new(self.secret, payload.encode(), hashlib.sha256).hexdigest()
return payload + "." + signature
def decode(self, scope, cursor):
try:
if not isinstance(cursor, str) or not cursor or len(cursor) > 2048:
raise ValueError()
payload, signature = cursor.split(".")
expected = hmac.new(self.secret, payload.encode("ascii"), hashlib.sha256).hexdigest()
if not hmac.compare_digest(signature, expected):
raise ValueError()
data = json.loads(base64.b64decode(payload + "=" * (-len(payload) % 4), altchars=b"-_", validate=True))
if not isinstance(data, list) or len(data) != 4 or data[:2] != [1, scope]:
raise ValueError()
if not isinstance(data[2], str) or not data[2].endswith("Z"):
raise ValueError()
created_at = datetime.fromisoformat(data[2].replace("Z", "+00:00"))
if created_at.utcoffset() != timezone.utc.utcoffset(created_at):
raise ValueError()
return created_at, UUID(data[3])
except (ValueError, TypeError, UnicodeError, KeyError, OverflowError):
raise invalid("Неверный cursor") from None
def page(self, rows, scope, limit):
items = rows[:limit]
cursor = self.encode(scope, items[-1]) if len(rows) > limit else None
return {"items": items, "next_cursor": cursor}
+63
View File
@@ -0,0 +1,63 @@
import base64
import binascii
import hashlib
import re
import secrets
from threading import BoundedSemaphore
from argon2 import PasswordHasher
from argon2.exceptions import VerificationError
from argon2.low_level import Type
from .errors import ApiError
HASHER = PasswordHasher(time_cost=2, memory_cost=19456, parallelism=1, type=Type.ID)
HASH_SLOTS = BoundedSemaphore(4)
DUMMY_HASH = HASHER.hash(secrets.token_urlsafe(32))
def unauthorized():
return ApiError(401, "unauthorized", "Неверные данные авторизации")
def hash_password(password):
with HASH_SLOTS:
return HASHER.hash(password)
def verify_password(password_hash, password):
try:
with HASH_SLOTS:
return HASHER.verify(password_hash or DUMMY_HASH, password)
except (VerificationError, ValueError):
return False
def basic_credentials(header):
try:
scheme, encoded = header.split(" ", 1)
if scheme.lower() != "basic" or len(encoded) > 2048:
raise ValueError()
decoded = base64.b64decode(encoded, validate=True).decode("utf-8")
username, separator, password = decoded.partition(":")
if not separator or re.fullmatch(r"[a-z0-9_]{3,32}", username) is None:
raise ValueError()
if not 12 <= len(password) <= 128 or not password.strip() or "\x00" in password:
raise ValueError()
return username, password
except (ValueError, UnicodeError, AttributeError, binascii.Error):
raise unauthorized() from None
def bearer_token(header):
try:
scheme, token = header.split(" ", 1)
if scheme.lower() != "bearer" or not re.fullmatch(r"[A-Za-z0-9_-]{43}", token):
raise ValueError()
return token
except (ValueError, TypeError, AttributeError):
raise unauthorized() from None
def token_hash(token):
return hashlib.sha256(token.encode("ascii")).hexdigest()
+240
View File
@@ -0,0 +1,240 @@
from contextlib import contextmanager
from datetime import datetime
import logging
import hashlib
import json
import secrets
from uuid import UUID, uuid4
import psycopg
from psycopg.rows import dict_row
from psycopg.types.json import Jsonb
from psycopg_pool import ConnectionPool, PoolTimeout, TooManyRequests
from .config import idempotency_ttl, secret, session_ttl
from .errors import ApiError, invalid, not_found
from .pagination import Pagination
from .security import hash_password, token_hash, unauthorized, verify_password
USER_FIELDS = "id, username, display_name, created_at"
CHAT_FIELDS = """
c.id, c.title, c.created_at,
ARRAY(SELECT cm.user_id FROM chat_members cm WHERE cm.chat_id=c.id ORDER BY cm.user_id) AS member_ids
"""
def serialize(value):
if isinstance(value, UUID):
return str(value)
if isinstance(value, datetime):
return value.isoformat(timespec="microseconds").replace("+00:00", "Z")
if isinstance(value, dict):
return {key: serialize(item) for key, item in value.items()}
if isinstance(value, list):
return [serialize(item) for item in value]
return value
class PostgresStore:
def __init__(self):
logging.getLogger("psycopg.pool").setLevel(logging.CRITICAL)
self.pool = ConnectionPool(
secret("DATABASE_URL_FILE"), min_size=1, max_size=10, timeout=3, max_waiting=40,
open=False, check=ConnectionPool.check_connection,
kwargs={"row_factory": dict_row, "connect_timeout": 3,
"options": "-c timezone=UTC -c statement_timeout=5000 -c lock_timeout=2000"},
)
self.pagination = Pagination(secret("CURSOR_SECRET_FILE").encode("ascii"))
self.ttl = session_ttl()
self.idempotency_ttl = idempotency_ttl()
def open(self):
self.pool.open()
def close(self):
self.pool.close()
@contextmanager
def connection(self):
try:
with self.pool.connection() as connection:
yield connection
except (psycopg.OperationalError, psycopg.InterfaceError, PoolTimeout, TooManyRequests,
psycopg.errors.QueryCanceled, psycopg.errors.LockNotAvailable):
raise ApiError(503, "unavailable", "Хранилище временно недоступно") from None
def ready(self):
try:
with self.connection() as connection:
row = connection.execute(
"SELECT version FROM schema_migrations WHERE version=%s", ("003_attachments.sql",)
).fetchone()
if not row:
raise ApiError(503, "unavailable", "Схема ещё не готова")
except psycopg.errors.UndefinedTable:
raise ApiError(503, "unavailable", "Схема ещё не готова") from None
return {"status": "ok"}
def create_user(self, body):
password_hash = hash_password(body.password)
try:
with self.connection() as connection:
row = connection.execute(
"INSERT INTO users(id,username,display_name,password_hash) VALUES (%s,%s,%s,%s) RETURNING "
+ USER_FIELDS,
(uuid4(), body.username, body.display_name, password_hash),
).fetchone()
return serialize(row)
except psycopg.errors.UniqueViolation:
raise ApiError(409, "conflict", "Username уже занят") from None
def create_session(self, username, password):
with self.connection() as connection:
row = connection.execute(
"SELECT " + USER_FIELDS + ", password_hash FROM users WHERE username=%s", (username,)
).fetchone()
if not verify_password(row["password_hash"] if row else None, password) or row is None:
raise unauthorized()
token = secrets.token_urlsafe(32)
with self.connection() as connection:
session = connection.execute("""
INSERT INTO sessions(token_hash,user_id,created_at,expires_at)
VALUES (%s,%s,CURRENT_TIMESTAMP,CURRENT_TIMESTAMP + %s * INTERVAL '1 second')
RETURNING expires_at
""", (token_hash(token), row["id"], self.ttl)).fetchone()
user = {key: value for key, value in row.items() if key != "password_hash"}
return {"token": token, "token_type": "Bearer", "expires_at": serialize(session["expires_at"]),
"user": serialize(user)}
def authenticate(self, token):
with self.connection() as connection:
row = connection.execute("""
SELECT user_id FROM sessions
WHERE token_hash=%s AND expires_at > CURRENT_TIMESTAMP
""", (token_hash(token),)).fetchone()
if row is None:
raise unauthorized()
return row["user_id"]
def logout(self, token):
with self.connection() as connection:
result = connection.execute("DELETE FROM sessions WHERE token_hash=%s", (token_hash(token),))
if result.rowcount != 1:
raise unauthorized()
def user(self, user_id):
with self.connection() as connection:
row = connection.execute("SELECT " + USER_FIELDS + " FROM users WHERE id=%s", (user_id,)).fetchone()
if not row:
raise unauthorized()
return serialize(row)
def create_chat(self, user_id, body):
members = body.member_ids
if user_id not in members:
raise invalid("Неверный список участников")
with self.connection() as connection:
found = connection.execute("SELECT id FROM users WHERE id=ANY(%s)", (members,)).fetchall()
if len(found) != len(members):
raise invalid("Неверный список участников")
chat_id = uuid4()
# чат и участники коммитятся вместе
connection.execute("INSERT INTO chats(id,title) VALUES (%s,%s)", (chat_id, body.title))
with connection.cursor() as cursor:
cursor.executemany(
"INSERT INTO chat_members(chat_id,user_id) VALUES (%s,%s)",
[(chat_id, member) for member in members],
)
return serialize(self.require_chat(connection, user_id, chat_id))
def require_chat(self, connection, user_id, chat_id):
row = connection.execute(
"SELECT " + CHAT_FIELDS + """ FROM chats c WHERE c.id=%s
AND EXISTS (SELECT 1 FROM chat_members m WHERE m.chat_id=c.id AND m.user_id=%s)
""", (chat_id, user_id)
).fetchone()
if row is None:
raise not_found()
return row
def chat(self, user_id, chat_id):
with self.connection() as connection:
return serialize(self.require_chat(connection, user_id, chat_id))
def list_chats(self, user_id, limit, cursor):
scope = "chats:" + str(user_id)
after = self.pagination.decode(scope, cursor) if cursor is not None else None
condition = " AND (c.created_at,c.id) > (%s,%s)" if after else ""
with self.connection() as connection:
rows = connection.execute(
"SELECT " + CHAT_FIELDS + """ FROM chats c
WHERE EXISTS (SELECT 1 FROM chat_members m WHERE m.chat_id=c.id AND m.user_id=%s)"""
+ condition + " ORDER BY c.created_at,c.id LIMIT %s",
[user_id, *(after or ()), limit + 1],
).fetchall()
return self.pagination.page(serialize(rows), scope, limit)
def create_message(self, user_id, chat_id, body, key):
path = f"/api/v1/chats/{chat_id}/messages"
scope = (user_id, "POST", path, key)
canonical = json.dumps(body.model_dump(mode="json"), sort_keys=True, ensure_ascii=False, separators=(",", ":"))
request_hash = hashlib.sha256(canonical.encode("utf-8")).hexdigest()
lock_data = json.dumps([str(user_id), "POST", path, key], separators=(",", ":")).encode()
lock_id = int.from_bytes(hashlib.sha256(lock_data).digest()[:8], "big", signed=True)
with self.connection() as connection:
self.require_chat(connection, user_id, chat_id)
# один ключ блокируется сразу для обеих реплик
connection.execute("SELECT pg_advisory_xact_lock(%s)", (lock_id,))
saved = connection.execute("""
SELECT request_hash,response_body,expires_at > clock_timestamp() AS active
FROM idempotency_keys WHERE user_id=%s AND method=%s AND path=%s AND request_key=%s
""", scope).fetchone()
if saved and saved["active"]:
if saved["request_hash"] != request_hash:
raise ApiError(409, "conflict", "С этим ключом уже отправлен другой запрос")
return saved["response_body"]
if saved:
connection.execute("""
DELETE FROM idempotency_keys WHERE user_id=%s AND method=%s AND path=%s AND request_key=%s
""", scope)
if body.attachment_ids:
found = connection.execute(
"SELECT id FROM attachments WHERE chat_id=%s AND id=ANY(%s)",
(chat_id, body.attachment_ids),
).fetchall()
if len(found) != len(body.attachment_ids):
raise not_found()
row = connection.execute("""
INSERT INTO messages(id,chat_id,sender_id,text) VALUES (%s,%s,%s,%s)
RETURNING id,chat_id,sender_id,text,created_at
""", (uuid4(), chat_id, user_id, body.text)).fetchone()
for position, attachment_id in enumerate(body.attachment_ids):
connection.execute(
"INSERT INTO message_attachments(message_id,attachment_id,chat_id,position) VALUES (%s,%s,%s,%s)",
(row["id"], attachment_id, chat_id, position),
)
row["attachment_ids"] = body.attachment_ids
message = serialize(row)
connection.execute("""
INSERT INTO idempotency_keys
(user_id,method,path,request_key,request_hash,message_id,response_status,response_body,expires_at)
VALUES (%s,%s,%s,%s,%s,%s,201,%s,clock_timestamp() + %s * INTERVAL '1 second')
""", (*scope, request_hash, row["id"], Jsonb(message), self.idempotency_ttl))
return message
def list_messages(self, user_id, chat_id, limit, cursor):
scope = "messages:" + str(chat_id)
after = self.pagination.decode(scope, cursor) if cursor is not None else None
condition = " AND (created_at,id) > (%s,%s)" if after else ""
with self.connection() as connection:
self.require_chat(connection, user_id, chat_id)
rows = connection.execute(
"""SELECT m.id,m.chat_id,m.sender_id,m.text,m.created_at,
ARRAY(SELECT ma.attachment_id FROM message_attachments ma
WHERE ma.message_id=m.id ORDER BY ma.position) AS attachment_ids
FROM messages m WHERE chat_id=%s"""
+ condition + " ORDER BY created_at,id LIMIT %s",
[chat_id, *(after or ()), limit + 1],
).fetchall()
return self.pagination.page(serialize(rows), scope, limit)
+232
View File
@@ -0,0 +1,232 @@
from concurrent.futures import ThreadPoolExecutor
from contextlib import contextmanager
import json
import subprocess
from urllib.parse import urlencode
from uuid import uuid4
from helpers import Client, ROOT, arguments, cleanup_once, image_bytes, lab, object_exists, s3, sql, wait
@contextmanager
def fault(chat_id, action):
name = "fault_" + uuid4().hex
sql("CREATE FUNCTION " + name + "() RETURNS trigger LANGUAGE plpgsql AS $body$ BEGIN "
"IF NEW.chat_id='" + chat_id + "'::uuid THEN " + action + "; END IF; RETURN NEW; END; $body$; "
"CREATE TRIGGER " + name + " BEFORE INSERT ON attachments FOR EACH ROW EXECUTE FUNCTION " + name + "()")
try:
yield
finally:
sql("DROP TRIGGER IF EXISTS " + name + " ON attachments; DROP FUNCTION IF EXISTS " + name + "()")
def intent(chat, actor, key=None):
identity = str(uuid4())
key = key or "originals/" + uuid4().hex
sql("INSERT INTO upload_intents(id,chat_id,owner_id,object_key) VALUES ('"
+ identity + "','" + chat["id"] + "','" + actor["id"] + "','" + key + "')")
return identity, key
def age(identity):
sql("UPDATE upload_intents SET created_at=clock_timestamp()-INTERVAL '11 minutes' WHERE id='" + identity + "'")
def present(identity):
return sql("SELECT count(*) FROM upload_intents WHERE id='" + identity + "'") == "1"
def put(key):
s3("c.put_object(Bucket=b,Key=" + repr(key) + ",Body=b'cleanup-test'); print('true')")
def privacy(client, actor, chat):
args = arguments()
current = lab.states(args)
networks = {}
for name in ("api1", "s3", "s3-storage"):
networks[name] = set(json.loads(lab.run(args, ["docker", "inspect", "--format",
"{{json .NetworkSettings.Networks}}", current[name]["id"]], quiet=True)))
assert not networks["api1"] & networks["s3-storage"], "Storage interfaces share the application network"
assert networks["api1"] & networks["s3"] and networks["s3-storage"] & networks["s3"]
for name in ("api1", "api2", "db", "s3", "s3-storage"):
ports = json.loads(lab.run(args, ["docker", "inspect", "--format",
"{{json .HostConfig.PortBindings}}", current[name]["id"]], quiet=True))
assert not ports, "Internal service has public ports"
att, raw = client.attachment(actor, chat)
key = sql("SELECT object_key FROM attachments WHERE id='" + att["id"] + "'")
anonymous = s3(
"import boto3, os; from botocore import UNSIGNED; from botocore.config import Config; "
"from botocore.exceptions import ClientError\n"
"u=boto3.client('s3',endpoint_url=os.environ['S3_ENDPOINT'],config=Config(signature_version=UNSIGNED,proxies={}))\n"
"try:\n u.get_object(Bucket=b,Key=" + repr(key) + "); status=200\n"
"except ClientError as e:\n status=e.response['ResponseMetadata']['HTTPStatusCode']\n"
"print(json.dumps(status))"
)
assert anonymous == 403, "Anonymous S3 access is allowed"
other = "other-" + uuid4().hex
forbidden = "forbidden-" + uuid4().hex
s3("c.create_bucket(Bucket=" + repr(other) + "); "
"c.put_object(Bucket=" + repr(other) + ",Key='private',Body=b'x'); print('true')")
try:
denied = s3(
"from botocore.exceptions import ClientError\n"
"checks=[('get_object',dict(Bucket=" + repr(other) + ",Key='private')),"
"('create_bucket',dict(Bucket=" + repr(forbidden) + ")),"
"('put_bucket_acl',dict(Bucket=b,ACL='public-read'))]\n"
"result=[]\n"
"for operation,values in checks:\n"
" try:\n getattr(c,operation)(**values); result.append(200)\n"
" except ClientError as e:\n result.append(e.response['ResponseMetadata']['HTTPStatusCode'])\n"
"print(json.dumps(result))", admin=False,
)
assert denied == [403, 403, 403], "Application credentials are too broad"
finally:
s3("c.delete_object(Bucket=" + repr(other) + ",Key='private'); "
"c.delete_bucket(Bucket=" + repr(other) + "); print('true')")
lab.compose(arguments(), "run", "--rm", "--no-deps", "bootstrap")
downloaded = client.call("GET", "/api/v1/attachments/" + att["id"] + "/content", actor=actor)["bytes"]
assert downloaded == raw
return {"anonymous_get": anonymous, "other_bucket_get": denied[0],
"create_bucket": denied[1], "change_acl": denied[2], "bootstrap_replay": "PASS",
"storage_network_isolated": True, "internal_ports_not_published": True}, att, key
def main():
args = arguments()
client = lab.client(args)
alice, bob, eve, chat = client.bootstrap()
result = {}
lab.compose(args, "stop", "--timeout", "3", "cleanup")
try:
result["privacy"], good, good_key = privacy(client, alice, chat)
fresh_id, fresh_key = intent(chat, alice)
put(fresh_key)
cleanup_once()
assert present(fresh_id) and object_exists(fresh_key)
result["fresh_intent_protected"] = "PASS"
age(fresh_id)
lock_name = "lab4_lock_" + uuid4().hex
command = ["docker", "compose", "--project-name", args.project, "--env-file", ".env",
"exec", "-T", "db", "psql", "-U", "messenger", "-d", "messenger", "-At", "-c",
"SET application_name='" + lock_name + "'; BEGIN; SELECT id FROM upload_intents WHERE id='"
+ fresh_id + "' FOR UPDATE; SELECT pg_sleep(60); COMMIT"]
with subprocess.Popen(command, cwd=ROOT, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) as process:
try:
wait(lambda: sql("SELECT count(*) FROM pg_stat_activity WHERE application_name='"
+ lock_name + "' AND wait_event='PgSleep'") == "1", 10)
cleanup_once()
assert present(fresh_id) and object_exists(fresh_key)
result["locked_intent_skipped"] = "PASS"
finally:
sql("SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE application_name='" + lock_name + "'")
process.wait(timeout=10)
cleanup_once()
assert not present(fresh_id) and not object_exists(fresh_key)
cleanup_once()
result["orphan_removed_and_repeat_safe"] = "PASS"
protected_id, _ = intent(chat, alice, good_key)
age(protected_id)
cleaned = cleanup_once()
assert cleaned["protected"] >= 1 and not present(protected_id) and object_exists(good_key)
import hashlib
protected = client.call("GET", "/api/v1/attachments/" + good["id"] + "/content", actor=bob)["bytes"]
assert hashlib.sha256(protected).hexdigest() == good["sha256"]
result["ready_unlinked_attachment_protected"] = "PASS"
path = "/api/v1/chats/" + chat["id"] + "/attachments?" + urlencode({"filename": "fault.png"})
with fault(chat["id"], "RAISE EXCEPTION 'test failure after S3 write'"):
client.call("POST", path, 500, actor=alice, raw=image_bytes(), headers={"Content-Type": "image/png"})
row = json.loads(sql("SELECT row_to_json(x) FROM (SELECT id,object_key FROM upload_intents WHERE chat_id='"
+ chat["id"] + "' ORDER BY created_at DESC LIMIT 1) x"))
assert object_exists(row["object_key"])
age(row["id"])
cleanup_once()
assert not present(row["id"]) and not object_exists(row["object_key"])
result["sql_failure_after_put_compensated"] = "PASS"
mapping = lab.instance_map(args)
addresses = {}
for service, item in mapping.items():
networks = json.loads(lab.run(args, ["docker", "inspect", "--format",
"{{json .NetworkSettings.Networks}}", item["container_id"]], quiet=True))
for network in networks.values():
addresses[network["IPAddress"]] = service
def interrupted_upload():
try:
Client(lab.base_url(args), timeout=20).call(
"POST", path, 201, actor=alice, raw=image_bytes(), headers={"Content-Type": "image/png"},
)
except (AssertionError, OSError):
return
with fault(chat["id"], "PERFORM pg_sleep(30)"):
with ThreadPoolExecutor(max_workers=1) as pool:
pending = pool.submit(interrupted_upload)
def session():
return sql("SELECT json_build_object('address',host(client_addr)) FROM pg_stat_activity "
"WHERE wait_event='PgSleep' AND query LIKE 'INSERT INTO attachments%' LIMIT 1")
wait(lambda: bool(session()), 10)
service = addresses[json.loads(session())["address"]]
row = json.loads(sql("SELECT row_to_json(x) FROM (SELECT id,object_key FROM upload_intents WHERE chat_id='"
+ chat["id"] + "' ORDER BY created_at DESC LIMIT 1) x"))
lab.run(args, ["docker", "kill", mapping[service]["container_id"]])
pending.result(timeout=25)
assert object_exists(row["object_key"]), "Fault was not injected after S3 PUT"
lab.restore(args)
assert present(row["id"])
assert sql("SELECT count(*) FROM attachments WHERE id='" + row["id"] + "'") == "0"
age(row["id"])
cleanup_once()
assert not present(row["id"]) and not object_exists(row["object_key"])
result["api_killed_after_put_compensated"] = {"status": "PASS", "service": service}
retry_id, retry_key = intent(chat, alice)
put(retry_key)
age(retry_id)
try:
lab.compose(args, "stop", "--timeout", "3", "s3")
cleanup_once(expected=False)
assert present(retry_id)
client.call("GET", "/health/live", schema="Health")
lab.outage_response(args, "/health/ready")
finally:
lab.compose(args, "start", "s3")
lab.wait_ready(args)
cleanup_once()
assert not present(retry_id) and not object_exists(retry_key)
result["s3_outage_keeps_intent_then_recovers"] = "PASS"
import socket
from urllib.parse import urlsplit
endpoint = urlsplit(client.base)
before = sql("SELECT count(*) FROM attachments WHERE chat_id='" + chat["id"] + "'")
with socket.create_connection((endpoint.hostname, endpoint.port), timeout=5) as connection:
request = ("POST " + path + " HTTP/1.1\r\nHost: localhost\r\nAuthorization: Bearer "
+ alice["token"] + "\r\nContent-Type: image/png\r\nContent-Length: 1000000\r\n\r\n")
connection.sendall(request.encode("ascii") + image_bytes())
import time
time.sleep(0.5)
assert sql("SELECT count(*) FROM attachments WHERE chat_id='" + chat["id"] + "'") == before
assert sql("SELECT count(*) FROM upload_intents WHERE chat_id='" + chat["id"] + "'") == "0"
result["client_disconnect_has_no_publication"] = "PASS"
worker_id, worker_key = intent(chat, alice)
put(worker_key)
age(worker_id)
lab.compose(args, "start", "cleanup")
wait(lambda: not present(worker_id), 45)
assert not object_exists(worker_key)
result["periodic_cleanup_worker"] = "PASS"
lab.evidence("cleanup.json", result)
print("PASS: private S3, restricted credentials, real faults and safe cleanup")
finally:
lab.compose(args, "start", "s3", "cleanup")
lab.restore(args)
if __name__ == "__main__":
main()
+129
View File
@@ -0,0 +1,129 @@
from concurrent.futures import ThreadPoolExecutor
import argparse
import hashlib
import json
from pathlib import Path
import subprocess
import threading
import time
from urllib.parse import urlencode
from uuid import uuid4
from helpers import Client, ROOT, image_bytes, lab, padded_png
RUNTIME = ("lb", "api1", "api2", "cleanup", "s3", "s3-storage", "db")
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--project", required=True)
parser.add_argument("--port", required=True, type=int)
args = parser.parse_args()
args.ttl, args.test_port = 3600, args.port
samplers, records, readers = {}, {"api1": [], "api2": []}, []
result = {"clients": 10, "upload_slots_per_replica": 2, "sampling_seconds": 0.05, "profiles": []}
flag = "/tmp/memory-stop-" + uuid4().hex
code = (
"import json,time; from pathlib import Path\n"
"root=Path('/sys/fs/cgroup')\n"
"while not Path(" + repr(flag) + ").exists():\n"
" status=dict(line.split(':',1) for line in Path('/proc/1/status').read_text().splitlines())\n"
" events=dict(line.split() for line in (root/'memory.events').read_text().splitlines())\n"
" print(json.dumps({'current':int((root/'memory.current').read_text()),"
"'peak':int((root/'memory.peak').read_text()),'limit':int((root/'memory.max').read_text()),"
"'rss':int(status['VmRSS'].split()[0])*1024,'hwm':int(status['VmHWM'].split()[0])*1024,"
"'oom_kill':int(events['oom_kill'])}),flush=True)\n"
" time.sleep(0.05)"
)
try:
# свежие API дают отдельный cgroup peak для этого опыта
lab.compose(args, "stop", "--timeout", "3", *RUNTIME)
lab.compose(args, "rm", "-f", "api1", "api2")
lab.start(args)
client = Client(lab.base_url(args), timeout=60)
actor, bob, eve, chat = client.bootstrap()
initial = lab.states(args)
for service in records:
command = ["docker", "exec", initial[service]["id"], "python", "-u", "-c", code]
process = subprocess.Popen(command, cwd=ROOT, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL,
text=True, encoding="utf-8")
samplers[service] = process
def collect(name=service, running=process):
for line in running.stdout:
item = json.loads(line)
item["received_at"] = time.monotonic()
records[name].append(item)
reader = threading.Thread(target=collect)
reader.start()
readers.append(reader)
deadline = time.monotonic() + 10
while not all(records.values()):
if time.monotonic() > deadline:
raise AssertionError("Memory sampler did not start")
time.sleep(0.05)
result["baseline"] = {name: values[-1] for name, values in records.items()}
profiles = [
("near_byte_limit", padded_png(10 * 1024 * 1024 - 1024), [32, 24]),
("near_pixel_limit", image_bytes(size=(8000, 5000), mode="RGBA"), [8000, 5000]),
]
for name, raw, dimensions in profiles:
started = time.monotonic()
barrier = threading.Barrier(10)
def upload(_):
c = Client(client.base, timeout=60)
barrier.wait(timeout=15)
response = c.call(
"POST", "/api/v1/chats/" + chat["id"] + "/attachments?"
+ urlencode({"filename": name + ".png"}), 201, actor=actor, raw=raw,
headers={"Content-Type": "image/png"}, schema="Attachment",
)
return response["json"], response["headers"]["X-Instance-Id"], response["elapsed"]
with ThreadPoolExecutor(max_workers=10) as pool:
uploads = list(pool.map(upload, range(10)))
for att, instance, elapsed in uploads:
downloaded = client.call("GET", "/api/v1/attachments/" + att["id"] + "/content", actor=bob)["bytes"]
assert hashlib.sha256(downloaded).hexdigest() == att["sha256"] == hashlib.sha256(raw).hexdigest()
assert downloaded == raw
peaks = {}
for service, values in records.items():
window = [item for item in values if item["received_at"] >= started]
assert window, "No memory samples during upload"
peaks[service] = {
"sampled_cgroup_peak_bytes": max(x["current"] for x in window),
"sampled_rss_peak_bytes": max(x["rss"] for x in window),
"cgroup_lifetime_peak_bytes": window[-1]["peak"],
"process_lifetime_hwm_bytes": window[-1]["hwm"],
"memory_limit_bytes": window[-1]["limit"],
"oom_kills": window[-1]["oom_kill"],
}
assert peaks[service]["oom_kills"] == 0
assert peaks[service]["cgroup_lifetime_peak_bytes"] < peaks[service]["memory_limit_bytes"]
assert len({item[0]["id"] for item in uploads}) == 10
assert len({item[1] for item in uploads}) == 2
result["profiles"].append({
"name": name, "file_bytes": len(raw), "dimensions": dimensions, "successes": len(uploads),
"errors": 0, "seconds": round(time.monotonic() - started, 3),
"slowest_upload_seconds": round(max(item[2] for item in uploads), 3),
"replicas_used": 2, "originals_verified": 10, "memory": peaks,
})
for service in records:
data = json.loads(lab.run(args, ["docker", "inspect", "--format",
'{"oom":{{json .State.OOMKilled}},"restarts":{{.RestartCount}}}',
initial[service]["id"]], quiet=True))
assert not data["oom"] and data["restarts"] == 0
result["status"] = "PASS"
lab.evidence("memory.json", result)
print("PASS: 10 concurrent uploads in both profiles; originals intact, no OOM or restarts")
finally:
for service, process in samplers.items():
lab.compose(args, "exec", "-T", service, "python", "-c", "from pathlib import Path; Path(" + repr(flag) + ").touch()",
quiet=True)
process.wait(timeout=10)
for reader in readers:
reader.join(timeout=5)
lab.compose(args, "stop", "--timeout", "10", *RUNTIME)
if __name__ == "__main__":
main()
+90
View File
@@ -0,0 +1,90 @@
import argparse
import io
import json
import os
from pathlib import Path
import struct
import subprocess
import sys
import time
import zlib
from PIL import Image
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT))
sys.path.insert(0, str(ROOT / "tests"))
from client import Client
from scripts import lab
def arguments():
return argparse.Namespace(
project=os.environ.get("LAB4_PROJECT", "mtusi-lab4"),
port=int(os.environ.get("LAB4_PORT", "8080")),
ttl=int(os.environ.get("LAB4_TTL", "3600")), test_port=18082,
)
def sql(statement):
args = arguments()
return lab.compose(args, "exec", "-T", "db", "psql", "-U", "messenger", "-d", "messenger",
"-v", "ON_ERROR_STOP=1", "-At", "-c", statement, quiet=True).strip()
def s3(code, admin=True):
args = arguments()
prelude = "import json; from src.objects import Objects; o=Objects(admin=" + str(admin) + "); c=o.client; b=o.bucket; "
if admin:
output = lab.compose(args, "run", "--rm", "--no-deps", "-T", "bootstrap",
"python", "-c", prelude + code, quiet=True)
else:
output = lab.compose(args, "exec", "-T", "api1", "python", "-c", prelude + code, quiet=True)
return json.loads(output.strip().splitlines()[-1])
def object_exists(key):
return s3("print(json.dumps(any(x['Key']==" + repr(key)
+ " for x in c.list_objects_v2(Bucket=b,Prefix=" + repr(key) + ").get('Contents',[]))))")
def wait(predicate, seconds=30):
deadline = time.monotonic() + seconds
while time.monotonic() < deadline:
if predicate():
return
time.sleep(0.1)
raise AssertionError("Test condition timed out")
def image_bytes(kind="PNG", size=(32, 24), mode="RGB"):
with Image.new(mode, size, (35, 120, 200, 255) if mode == "RGBA" else (35, 120, 200)) as image:
output = io.BytesIO()
image.save(output, format=kind)
return output.getvalue()
def chunk(kind, data):
return struct.pack(">I", len(data)) + kind + data + struct.pack(">I", zlib.crc32(kind + data) & 0xffffffff)
def padded_png(size):
base = image_bytes()
padding = b"p" * (size - len(base) - 12)
return base[:-12] + chunk(b"raNd", padding) + base[-12:]
def oversized_pixels():
header = struct.pack(">IIBBBBB", 8000, 5001, 8, 2, 0, 0, 0)
return b"\x89PNG\r\n\x1a\n" + chunk(b"IHDR", header) + chunk(b"IEND", b"")
def cleanup_once(expected=True):
args = arguments()
command = ["docker", "compose", "--project-name", args.project, "--env-file", ".env",
"run", "--rm", "--no-deps", "-T", "cleanup", "python", "-m", "src.cleanup", "--once"]
result = subprocess.run(command, cwd=ROOT, env=lab.environment(args), capture_output=True, text=True, encoding="utf-8")
if (result.returncode == 0) != expected:
raise AssertionError("Unexpected cleanup exit status")
return json.loads(result.stdout.strip().splitlines()[-1]) if expected else None
+181
View File
@@ -0,0 +1,181 @@
from concurrent.futures import ThreadPoolExecutor
import os
from pathlib import Path
import sys
import unittest
from urllib.parse import urlencode
from uuid import uuid4
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "tests"))
from client import Client, timestamp
class ApiTests(unittest.TestCase):
def setUp(self):
self.client = Client(os.environ.get("BASE_URL", "http://127.0.0.1:8080"))
self.alice, self.bob, self.eve, self.chat = self.client.bootstrap()
self.path = "/api/v1/chats/" + self.chat["id"]
def error(self, method, path, status=400, **kwargs):
if method == "POST" and path.endswith("/messages"):
kwargs.setdefault("headers", {}).setdefault("Idempotency-Key", uuid4().hex)
response = self.client.call(method, path, status, **kwargs)
code = {400: "invalid_request", 401: "unauthorized", 404: "not_found", 409: "conflict", 415: "unsupported_media_type"}[status]
self.assertEqual(response["json"]["error"]["code"], code)
def get_page(self, path, actor=None, **query):
schema = "MessagePage" if path.endswith("/messages") else "ChatPage"
return self.client.call("GET", path + "?" + urlencode(query), actor=actor or self.alice, schema=schema)["json"]
def new_chat(self):
return self.client.call("POST", "/api/v1/chats", 201, actor=self.alice,
body={"title": "Ещё чат", "member_ids": [self.alice["id"], self.bob["id"]]}, schema="Chat")["json"]
def test_user_fields(self):
name = "u_" + uuid4().hex[:20]
bodies = [
{"username": "ab", "display_name": "Имя"},
{"username": "ABC", "display_name": "Имя"},
{"username": "valid\n", "display_name": "Имя"},
{"username": 123, "display_name": "Имя"},
{"username": name, "display_name": " \t\n"},
{"username": name, "display_name": 123},
{"username": name, "display_name": None},
{"username": name, "display_name": "я" * 101},
{"username": name, "display_name": "Имя", "password": "extra"},
{"username": name},
]
for body in bodies:
body.setdefault("password", "T3st-password-123")
with self.subTest(fields=sorted(body)):
self.error("POST", "/api/v1/users", body=body)
user = self.client.call("POST", "/api/v1/users", 201,
body={"username": name, "display_name": "👋" * 100, "password": "T3st-password-123"}, schema="User")["json"]
self.assertEqual(user["display_name"], "👋" * 100)
self.error("POST", "/api/v1/users", 409, body={"username": name, "display_name": "Повтор", "password": "T3st-password-123"})
def test_invalid_json_and_content_type(self):
for raw in (b'{"username":', b"null", b"[]", b"true", b"\xff", '{}'.encode('utf-16')):
with self.subTest(raw=raw):
self.error("POST", "/api/v1/users", raw=raw, headers={"Content-Type": "application/json"})
self.error("POST", "/api/v1/users", 415, raw=b"text", headers={"Content-Type": "text/plain"})
self.error("POST", "/api/v1/users", 415, raw=b"{}")
def test_auth_and_access(self):
self.error("GET", "/api/v1/users/me", 401)
for value in (str(uuid4()), "bad-id"):
self.error("GET", "/api/v1/users/me", 401, headers={"X-User-Id": value})
self.error("GET", "/api/v1/chats/bad-id", 401)
self.error("GET", "/api/v1/chats/bad-id", actor=self.alice)
self.error("GET", "/api/v1/chats/" + str(uuid4()), 404, actor=self.alice)
self.error("GET", self.path, 404, actor=self.eve)
self.error("GET", self.path + "/messages", 404, actor=self.eve)
self.error("POST", self.path + "/messages", 404, actor=self.eve, body={"text": "Чужое"})
self.assertEqual(self.get_page("/api/v1/chats", self.eve)["items"], [])
def test_chat_validation_is_atomic(self):
before = self.get_page("/api/v1/chats")["items"]
valid = [self.alice["id"], self.bob["id"]]
for members in ([], [self.bob["id"]], [self.alice["id"], str(uuid4())],
[self.alice["id"], self.alice["id"].upper()], ["bad-id"], valid * 51):
self.error("POST", "/api/v1/chats", actor=self.alice, body={"title": "Чат", "member_ids": members})
for title in ("", " \t", "я" * 101, 123):
self.error("POST", "/api/v1/chats", actor=self.alice, body={"title": title, "member_ids": valid})
self.error("POST", "/api/v1/chats", actor=self.alice, body={"title": "Чат", "member_ids": valid, "extra": 1})
self.assertEqual(before, self.get_page("/api/v1/chats")["items"])
def test_message_fields_and_unicode(self):
bodies = [{"text": value} for value in ("", " \t\n", "я" * 4001, None, 123, True, [])]
bodies += [{}, {"text": "Hi", "sender_id": self.bob["id"]}, {"text": "Hi", "unexpected": []}]
for body in bodies:
self.error("POST", self.path + "/messages", actor=self.alice, body=body)
for text in (" Привет, мир 👋\n", "👋" * 4000):
message = self.client.message(self.alice, self.chat, text)["json"]
self.assertEqual(message["text"], text)
self.assertEqual(message["sender_id"], self.alice["id"])
def test_message_pagination(self):
self.assertEqual(self.get_page(self.path + "/messages"), {"items": [], "next_cursor": None})
self.assertEqual(self.get_page(self.path + "/messages", limit="0000000001")["items"], [])
sent = [self.client.message(self.alice if i % 2 else self.bob, self.chat, str(i))["json"] for i in range(6)]
received, cursor = [], None
for limit in (2, 3, 1):
query = {"limit": limit}
if cursor:
query["cursor"] = cursor
page = self.get_page(self.path + "/messages", self.bob, **query)
received.extend(page["items"])
cursor = page["next_cursor"]
self.assertIsNone(cursor)
self.assertEqual(received, sorted(sent, key=lambda item: (timestamp(item["created_at"]), item["id"])))
def test_chat_pagination(self):
chats = [self.chat] + [self.new_chat() for _ in range(4)]
received, cursor = [], None
while True:
query = {"limit": 2}
if cursor:
query["cursor"] = cursor
page = self.get_page("/api/v1/chats", **query)
received += page["items"]
cursor = page["next_cursor"]
if cursor is None:
break
self.assertEqual(received, sorted(chats, key=lambda item: (timestamp(item["created_at"]), item["id"])))
cursor = self.get_page("/api/v1/chats", limit=1)["next_cursor"]
self.error("GET", "/api/v1/chats?" + urlencode({"cursor": cursor}), actor=self.bob)
def test_bad_cursors_and_limits(self):
for i in range(2):
self.client.message(self.alice, self.chat, str(i))
cursor = self.get_page(self.path + "/messages", limit=1)["next_cursor"]
for value in ("", "bad", cursor[:-1] + ("0" if cursor[-1] != "0" else "1")):
self.error("GET", self.path + "/messages?" + urlencode({"cursor": value}), actor=self.alice)
other = self.new_chat()
self.error("GET", "/api/v1/chats/" + other["id"] + "/messages?" + urlencode({"cursor": cursor}), actor=self.alice)
self.error("GET", "/api/v1/chats?" + urlencode({"cursor": cursor}), actor=self.alice)
for path in ("/api/v1/chats", self.path + "/messages"):
for value in ("", "0", "-1", "101", "1.5", "true", "9" * 500):
self.error("GET", path + "?" + urlencode({"limit": value}), actor=self.alice)
def test_read_after_write(self):
first = self.client.message(self.alice, self.chat, "Первое")["json"]
second = self.client.message(self.bob, self.chat, "Второе")["json"]
page = self.get_page(self.path + "/messages", limit=1)
third = self.client.message(self.bob, self.chat, "Новое")["json"]
rest = self.get_page(self.path + "/messages", self.bob, cursor=page["next_cursor"])
self.assertEqual(page["items"] + rest["items"], [first, second, third])
self.assertIsNone(rest["next_cursor"])
def test_parallel_messages(self):
def send(worker):
client = Client(self.client.base)
return [client.message(self.alice, self.chat, f"{worker}:{i}")["json"]["id"] for i in range(10)]
with ThreadPoolExecutor(max_workers=10) as pool:
sent = [item for group in pool.map(send, range(10)) for item in group]
received, cursor = [], None
while True:
query = {"limit": 17}
if cursor:
query["cursor"] = cursor
page = self.get_page(self.path + "/messages", self.bob, **query)
received += page["items"]
cursor = page["next_cursor"]
if cursor is None:
break
self.assertEqual(len(received), 100)
self.assertEqual(len({item["text"] for item in received}), 100)
self.assertEqual({item["id"] for item in received}, set(sent))
def test_parallel_username(self):
body = {"username": "u_" + uuid4().hex[:20], "display_name": "Один", "password": "T3st-password-123"}
def create(_):
return Client(self.client.base).call("POST", "/api/v1/users", [201, 409], body=body, schema="User")["status"]
with ThreadPoolExecutor(max_workers=10) as pool:
statuses = list(pool.map(create, range(10)))
self.assertEqual(statuses.count(201), 1)
self.assertEqual(statuses.count(409), 9)
if __name__ == "__main__":
unittest.main(verbosity=2)
+117
View File
@@ -0,0 +1,117 @@
from concurrent.futures import ThreadPoolExecutor
import hashlib
from urllib.parse import urlencode
from uuid import uuid4
import unittest
from helpers import Client, ROOT, image_bytes, oversized_pixels, padded_png
class AttachmentTests(unittest.TestCase):
def setUp(self):
self.client = Client(timeout=60)
self.alice, self.bob, self.eve, self.chat = self.client.bootstrap()
self.path = "/api/v1/chats/" + self.chat["id"]
def upload(self, content=None, media="image/png", name="photo.png", expected=201, actor=None):
return self.client.call(
"POST", self.path + "/attachments?" + urlencode({"filename": name}), expected,
actor=actor or self.alice, raw=image_bytes() if content is None else content,
headers={"Content-Type": media}, schema="Attachment" if expected == 201 else None,
)
def test_original_png_jpeg_and_unicode_filename(self):
for kind, media in (("PNG", "image/png"), ("JPEG", "image/jpeg")):
raw = image_bytes(kind)
att = self.upload(raw, media, "фото 👋." + kind.lower())["json"]
self.assertEqual(att["owner_id"], self.alice["id"])
self.assertEqual(att["sha256"], hashlib.sha256(raw).hexdigest())
self.assertEqual(att["size_bytes"], len(raw))
self.assertEqual((att["status"], att["variants"], att["error_code"]), ("ready", [], None))
path = "/api/v1/attachments/" + att["id"]
metadata = self.client.call("GET", path, actor=self.bob, schema="Attachment")["json"]
self.assertEqual(metadata, att)
downloaded = self.client.call("GET", path + "/content", actor=self.bob)
self.assertEqual(downloaded["bytes"], raw)
self.assertEqual(downloaded["headers"].get_content_type(), media)
self.assertNotIn("object_key", metadata)
self.assertNotIn("Location", downloaded["headers"])
def test_filename_validation(self):
for name in ("", " \t", "../a.png", "a/b.png", "a\\b.png", "a\x00.png", "a\n.png", "a\x7f.png", "я" * 256):
self.upload(name=name, expected=400)
self.assertEqual(self.upload(name="я" * 255)["json"]["filename"], "я" * 255)
def test_corrupt_images_mime_and_multipart(self):
png, jpeg = image_bytes(), image_bytes("JPEG")
for content, media in ((b"", "image/png"), (b"not an image", "image/png"),
(png[:-8], "image/png"), (jpeg[:-8], "image/jpeg"),
(png, "image/jpeg"), (jpeg, "image/png"),
(image_bytes("GIF"), "image/png"), (png, "multipart/form-data")):
self.upload(content, media, expected=415)
def test_byte_limit_and_original_at_exact_boundary(self):
raw = padded_png(10485760)
att = self.upload(raw)["json"]
downloaded = self.client.call("GET", "/api/v1/attachments/" + att["id"] + "/content", actor=self.bob)
self.assertEqual(downloaded["bytes"], raw)
self.upload(raw + b"x", expected=413)
def test_chunked_body_cannot_bypass_size_limit(self):
def blocks():
for _ in range(161):
yield b"x" * 65536
self.upload(blocks(), expected=413)
def test_pixel_boundary_and_decompression_bomb(self):
self.upload(oversized_pixels(), expected=413)
self.assertEqual(self.upload(image_bytes(size=(8000, 5000)))["json"]["status"], "ready")
def test_attachment_access_and_unknown_ids(self):
att = self.upload()["json"]
for suffix in ("", "/content"):
self.client.call("GET", "/api/v1/attachments/" + att["id"] + suffix, 404, actor=self.eve)
self.client.call("GET", "/api/v1/attachments/" + str(uuid4()) + suffix, 404, actor=self.alice)
self.client.call("GET", "/api/v1/attachments/" + att["id"] + suffix, 401)
self.upload(actor=self.eve, expected=404)
def test_messages_keep_attachment_order_and_allow_empty_text(self):
ids = [self.upload()["json"]["id"] for _ in range(2)]
for text in ("", " \t\n", "с картинками"):
message = self.client.message(self.bob, self.chat, text, attachments=ids[::-1])["json"]
self.assertEqual(message["attachment_ids"], ids[::-1])
self.assertEqual(message["text"], text)
page = self.client.call("GET", self.path + "/messages", actor=self.alice, schema="MessagePage")["json"]
self.assertEqual([m["attachment_ids"] for m in page["items"]], [ids[::-1]] * 3)
def test_bad_attachment_list_and_other_chat_do_not_create_messages(self):
att = self.upload()["json"]["id"]
other = self.client.call("POST", "/api/v1/chats", 201, actor=self.alice,
body={"title": "other", "member_ids": [self.alice["id"]]}, schema="Chat")["json"]
foreign = self.client.attachment(self.alice, other)[0]["id"]
for ids, status in (([att, att], 400), ([str(uuid4()) for _ in range(11)], 400),
(["bad"], 400), ([str(uuid4())], 404), ([foreign], 404)):
self.client.call("POST", self.path + "/messages", status, actor=self.alice,
body={"text": "test", "attachment_ids": ids}, headers={"Idempotency-Key": uuid4().hex})
page = self.client.call("GET", self.path + "/messages", actor=self.alice, schema="MessagePage")["json"]
self.assertEqual(page["items"], [])
def test_idempotency_defaults_and_attachment_replay(self):
key = uuid4().hex
original = self.client.message(self.alice, self.chat, "text", key=key)["json"]
explicit = self.client.message(self.alice, self.chat, "text", key=key, attachments=[])["json"]
self.assertEqual(original, explicit)
att = self.upload()["json"]["id"]
key = uuid4().hex
def send(_):
return Client(timeout=30).message(self.alice, self.chat, "", key=key, attachments=[att])
with ThreadPoolExecutor(max_workers=10) as pool:
responses = list(pool.map(send, range(10)))
self.assertEqual(len({r["json"]["id"] for r in responses}), 1)
self.assertEqual(len({r["headers"]["X-Instance-Id"] for r in responses}), 2)
self.client.call("POST", self.path + "/messages", 409, actor=self.alice,
body={"text": "different", "attachment_ids": [att]}, headers={"Idempotency-Key": key})
if __name__ == "__main__":
unittest.main(verbosity=2)
+140
View File
@@ -0,0 +1,140 @@
from concurrent.futures import ThreadPoolExecutor
import base64
import json
import os
from pathlib import Path
import subprocess
import sys
import unittest
from uuid import uuid4
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "tests"))
from client import Client
class IdempotencyTests(unittest.TestCase):
def setUp(self):
self.client = Client(os.environ.get("BASE_URL", "http://127.0.0.1:8080"))
self.alice, self.bob, self.eve, self.chat = self.client.bootstrap()
self.path = "/api/v1/chats/" + self.chat["id"] + "/messages"
def history(self):
return self.client.call("GET", self.path + "?limit=100", actor=self.alice, schema="MessagePage")["json"]["items"]
def sql(self, statement):
project = os.environ.get("LAB4_PROJECT", "mtusi-lab4")
command = ["docker", "compose", "--project-name", project, "--env-file", ".env",
"exec", "-T", "db", "psql", "-U", "messenger", "-d", "messenger",
"-v", "ON_ERROR_STOP=1", "-At", "-c", statement]
result = subprocess.run(command, cwd=ROOT, capture_output=True, text=True, encoding="utf-8")
if result.returncode:
raise AssertionError("Database test command failed")
return result.stdout.strip()
def test_key_required_and_format(self):
for key in (None, "", "two words", "a/b", "é", "a" * 129):
headers = {} if key is None else {"Idempotency-Key": key}
self.client.call("POST", self.path, 400, actor=self.alice, body={"text": "test"}, headers=headers)
for key in ("a", ".:_-", "a" * 128):
self.client.message(self.alice, self.chat, key, key=key)
self.assertEqual(len(self.history()), 3)
def test_replay_normalizes_json(self):
key = uuid4().hex
text = "Привет 👋"
first = self.client.message(self.alice, self.chat, text, key=key)
raw = json.dumps({"text": text}, ensure_ascii=True, indent=4).encode("ascii")
second = self.client.call("POST", self.path, 201, actor=self.alice, raw=raw,
headers={"Idempotency-Key": key, "Content-Type": "application/json"}, schema="Message")
self.assertEqual(first["json"], second["json"])
self.assertNotEqual(first["headers"]["X-Request-Id"], second["headers"]["X-Request-Id"])
self.assertEqual(len(self.history()), 1)
def test_different_body_conflicts(self):
key = uuid4().hex
original = self.client.message(self.alice, self.chat, "first", key=key)["json"]
self.client.call("POST", self.path, 409, actor=self.alice,
body={"text": "second"}, headers={"Idempotency-Key": key})
self.assertEqual(self.client.message(self.alice, self.chat, "first", key=key)["json"], original)
self.assertEqual(self.history(), [original])
def test_twenty_concurrent_requests_use_both_replicas(self):
key = uuid4().hex
def send(_):
return Client(self.client.base).message(self.alice, self.chat, "one message", key=key)
with ThreadPoolExecutor(max_workers=20) as pool:
responses = list(pool.map(send, range(20)))
self.assertEqual(len({item["json"]["id"] for item in responses}), 1)
self.assertEqual(len({item["headers"]["X-Instance-Id"] for item in responses}), 2)
self.assertTrue(all(item["json"] == responses[0]["json"] for item in responses))
self.assertEqual(self.history(), [responses[0]["json"]])
def test_scope_is_user_and_chat_not_session(self):
key = uuid4().hex
first = self.client.message(self.alice, self.chat, "shared", key=key)["json"]
second = self.client.message(self.bob, self.chat, "shared", key=key)["json"]
other = self.client.call("POST", "/api/v1/chats", 201, actor=self.alice,
body={"title": "other", "member_ids": [self.alice["id"]]}, schema="Chat")["json"]
third = self.client.message(self.alice, other, "shared", key=key)["json"]
self.assertEqual(len({first["id"], second["id"], third["id"]}), 3)
source = self.alice["registration"]
value = base64.b64encode((source["username"] + ":" + source["password"]).encode()).decode()
session = self.client.call("POST", "/api/v1/auth/sessions", 201,
headers={"Authorization": "Basic " + value}, schema="Session")["json"]
actor = {"id": self.alice["id"], "token": session["token"]}
self.assertEqual(self.client.message(actor, self.chat, "shared", key=key)["json"], first)
def test_replay_still_checks_access_and_session(self):
key = uuid4().hex
self.client.message(self.alice, self.chat, "private", key=key)
self.client.call("POST", self.path, 404, actor=self.eve,
body={"text": "private"}, headers={"Idempotency-Key": key})
self.client.call("DELETE", "/api/v1/auth/sessions/current", 204, actor=self.alice)
self.client.call("POST", self.path, 401, actor=self.alice,
body={"text": "private"}, headers={"Idempotency-Key": key})
def test_failure_rolls_back_message_and_key(self):
key = "fault-" + uuid4().hex
constraint = "test_reject_" + uuid4().hex[:12]
self.sql("ALTER TABLE idempotency_keys ADD CONSTRAINT " + constraint
+ " CHECK(request_key <> '" + key + "') NOT VALID")
try:
self.client.call("POST", self.path, 500, actor=self.alice,
body={"text": "rollback"}, headers={"Idempotency-Key": key})
self.assertEqual(self.history(), [])
self.assertEqual(self.sql("SELECT count(*) FROM idempotency_keys WHERE request_key='" + key + "'"), "0")
finally:
self.sql("ALTER TABLE idempotency_keys DROP CONSTRAINT " + constraint)
result = self.client.message(self.alice, self.chat, "rollback", key=key)["json"]
self.assertEqual(self.client.message(self.alice, self.chat, "rollback", key=key)["json"], result)
self.assertEqual(self.history(), [result])
def test_retention_and_expired_key_reuse(self):
key = uuid4().hex
first = self.client.message(self.alice, self.chat, "old", key=key)["json"]
retained = self.sql("SELECT expires_at-created_at > INTERVAL '23 hours 59 minutes 59 seconds'"
+ " FROM idempotency_keys WHERE request_key='" + key + "'")
self.assertEqual(retained, "t")
self.sql("UPDATE idempotency_keys SET expires_at=created_at + INTERVAL '1 microsecond'"
+ " WHERE request_key='" + key + "'")
second = self.client.message(self.alice, self.chat, "new", key=key)["json"]
self.assertNotEqual(first["id"], second["id"])
self.assertEqual(self.history(), [first, second])
def test_same_session_on_both_instances(self):
responses = [self.client.call("GET", "/api/v1/users/me", actor=self.alice, schema="User")
for _ in range(12)]
self.assertEqual(len({item["headers"]["X-Instance-Id"] for item in responses}), 2)
self.assertTrue(all(item["json"] == self.alice["user"] for item in responses))
def test_invalid_body_does_not_reserve_key(self):
key = uuid4().hex
self.client.call("POST", self.path, 400, actor=self.alice,
body={"text": ""}, headers={"Idempotency-Key": key})
result = self.client.message(self.alice, self.chat, "fixed", key=key)["json"]
self.assertEqual(self.history(), [result])
if __name__ == "__main__":
unittest.main(verbosity=2)
+96
View File
@@ -0,0 +1,96 @@
import base64
import json
import os
from pathlib import Path
import re
import sys
import unittest
from uuid import uuid4
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "tests"))
from client import Client
def basic(username, password):
value = base64.b64encode((username + ":" + password).encode("utf-8")).decode("ascii")
return {"Authorization": "Basic " + value}
class SessionTests(unittest.TestCase):
def setUp(self):
self.client = Client(os.environ.get("BASE_URL", "http://127.0.0.1:8080"))
self.actor = self.client.user("auth")
def register(self, password):
body = {"username": "p_" + uuid4().hex[:20], "display_name": "Пароль", "password": password}
user = self.client.call("POST", "/api/v1/users", 201, body=body, schema="User")["json"]
session = self.client.call("POST", "/api/v1/auth/sessions", 201,
headers=basic(body["username"], password), schema="Session")["json"]
self.assertEqual(session["user"], user)
self.assertEqual(set(session), {"token", "token_type", "expires_at", "user"})
self.assertTrue(re.fullmatch(r"[A-Za-z0-9_-]{43}", session["token"]), "Invalid token format")
return body, session
def test_password_required_and_limits(self):
source = self.actor["registration"]
for password in ("x" * 11, "x" * 129, " " * 12, None, 123, True, "a" * 12 + "\x00"):
body = {**source, "username": "p_" + uuid4().hex[:20], "password": password}
self.client.call("POST", "/api/v1/users", 400, body=body)
missing = {key: value for key, value in source.items() if key != "password"}
self.client.call("POST", "/api/v1/users", 400, body=missing)
raw = json.dumps({**source, "username": "p_" + uuid4().hex[:20], "password": "\ud800" * 12}).encode()
self.client.call("POST", "/api/v1/users", 400, raw=raw, headers={"Content-Type": "application/json"})
def test_password_boundaries_unicode_colon_spaces(self):
for password in ("x" * 12, "🙂" * 127 + ":", " Привет:пароль🙂 "):
body, session = self.register(password)
actor = {"id": session["user"]["id"], "token": session["token"]}
self.client.call("GET", "/api/v1/users/me", actor=actor, schema="User")
changed = password.strip() if password != password.strip() else password + "!"
self.client.call("POST", "/api/v1/auth/sessions", 401,
headers=basic(body["username"], changed))
def test_wrong_and_unknown_credentials(self):
source = self.actor["registration"]
wrong = self.client.call("POST", "/api/v1/auth/sessions", 401,
headers=basic(source["username"], "wrong-password-123"))
unknown = self.client.call("POST", "/api/v1/auth/sessions", 401,
headers=basic("unknown_" + uuid4().hex[:12], source["password"]))
self.assertEqual(wrong["json"]["error"]["message"], unknown["json"]["error"]["message"])
self.assertEqual(wrong["headers"]["WWW-Authenticate"], 'Basic realm="messenger", charset="UTF-8"')
def test_malformed_basic_and_body(self):
for value in ("", "Basic", "Basic !!!", "Bearer " + self.actor["token"],
"Basic " + base64.b64encode(b"missing-colon").decode(),
"Basic " + base64.b64encode(b"\xff:some-password").decode()):
self.client.call("POST", "/api/v1/auth/sessions", 401, headers={"Authorization": value})
source = self.actor["registration"]
self.client.call("POST", "/api/v1/auth/sessions", 400, body={},
headers=basic(source["username"], source["password"]))
def test_only_bearer_identifies_user(self):
source = self.actor["registration"]
for value in ("", "Bearer", "Bearer bad", "Bearer " + "x" * 43,
basic(source["username"], source["password"])["Authorization"]):
self.client.call("GET", "/api/v1/users/me", 401, headers={"Authorization": value})
self.client.call("GET", "/api/v1/users/me", 401, headers={"X-User-Id": self.actor["id"]})
other = self.client.user("other")
result = self.client.call("GET", "/api/v1/users/me", actor=self.actor,
headers={"X-User-Id": other["id"]}, schema="User")
self.assertEqual(result["json"]["id"], self.actor["id"])
self.client.call("GET", "/api/v1/users/me", schema="User",
headers={"Authorization": "bEaReR " + self.actor["token"]})
def test_logout_only_revokes_current_session(self):
source = self.actor["registration"]
second = self.client.call("POST", "/api/v1/auth/sessions", 201,
headers=basic(source["username"], source["password"]), schema="Session")["json"]
other_session = {"id": self.actor["id"], "token": second["token"]}
self.client.call("DELETE", "/api/v1/auth/sessions/current", 204, actor=self.actor)
for method, path in (("GET", "/api/v1/users/me"), ("DELETE", "/api/v1/auth/sessions/current")):
self.client.call(method, path, 401, actor=self.actor)
self.client.call("GET", "/api/v1/users/me", actor=other_session, schema="User")
if __name__ == "__main__":
unittest.main(verbosity=2)