forked from mtusi2026/mtusi2026lab4
Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
20b776cceb | ||
|
|
8d001712fe |
+11
-19
@@ -1,20 +1,12 @@
|
||||
# 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
|
||||
POSTGRES_USER=messenger
|
||||
POSTGRES_PASSWORD=CHANGE_ME_TO_RANDOM_ASCII_PASSWORD
|
||||
POSTGRES_DB=messenger
|
||||
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
|
||||
CACHE_URL=redis://redis:6379/0
|
||||
LOG_LEVEL=WARNING
|
||||
API_IMAGE=mtusi-messenger:lab4
|
||||
MINIO_ROOT_USER=minio-root
|
||||
MINIO_ROOT_PASSWORD=CHANGE_ME_TO_RANDOM_MINIO_ROOT_PASSWORD
|
||||
S3_BUCKET=attachments
|
||||
S3_ACCESS_KEY=CHANGE_ME_TO_SEPARATE_API_ACCESS_KEY
|
||||
S3_SECRET_KEY=CHANGE_ME_TO_RANDOM_API_SECRET_KEY
|
||||
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
FROM python:3.12.8-slim-bookworm AS base
|
||||
|
||||
ENV PYTHONDONTWRITEBYTECODE=1 \
|
||||
PYTHONUNBUFFERED=1 \
|
||||
PIP_NO_CACHE_DIR=1 \
|
||||
PIP_DISABLE_PIP_VERSION_CHECK=1 \
|
||||
PIP_ROOT_USER_ACTION=ignore \
|
||||
APP_HOST=0.0.0.0 \
|
||||
APP_PORT=8080 \
|
||||
APP_ENV=lab
|
||||
|
||||
RUN groupadd --system --gid 10001 app \
|
||||
&& useradd --system --uid 10001 --gid app --no-create-home --shell /usr/sbin/nologin app
|
||||
|
||||
WORKDIR /srv
|
||||
|
||||
COPY requirements.txt ./
|
||||
RUN pip install -r requirements.txt
|
||||
|
||||
COPY app ./app
|
||||
|
||||
FROM base AS test
|
||||
COPY requirements-test.txt ./
|
||||
RUN pip install -r requirements-test.txt
|
||||
COPY pytest.ini ./
|
||||
COPY student-tests ./student-tests
|
||||
COPY unit-tests ./unit-tests
|
||||
COPY scripts ./scripts
|
||||
COPY tests ./tests
|
||||
COPY lab.json ./
|
||||
COPY contracts ./contracts
|
||||
USER app
|
||||
CMD ["python", "-m", "pytest", "-v", "--no-header"]
|
||||
|
||||
FROM base AS runtime
|
||||
USER app
|
||||
EXPOSE 8080
|
||||
HEALTHCHECK --interval=10s --timeout=3s --start-period=5s --retries=3 \
|
||||
CMD ["python", "-m", "app.healthcheck"]
|
||||
STOPSIGNAL SIGTERM
|
||||
CMD ["python", "-m", "app"]
|
||||
@@ -4,7 +4,7 @@ CA_FILE ?=
|
||||
ACTION_SCRIPT ?=
|
||||
TLS_ARGS = $(if $(CA_FILE),--ca-file "$(CA_FILE)",)
|
||||
|
||||
.PHONY: help check validate test load test-persistence test-failover
|
||||
.PHONY: help check validate test load test-persistence test-failover up test-student explain evidence replicas restore rolling lab4-evidence
|
||||
|
||||
help:
|
||||
@echo "check: offline structure; validate: full OpenAPI (requirements-dev.txt); test: running API; load: synthetic writes"
|
||||
@@ -14,6 +14,7 @@ check:
|
||||
|
||||
validate:
|
||||
$(PYTHON) scripts/validate_openapi.py
|
||||
$(PYTHON) scripts/check_stack.py
|
||||
|
||||
test:
|
||||
$(PYTHON) tests/smoke.py --base-url "$(BASE_URL)" $(TLS_ARGS)
|
||||
@@ -25,6 +26,32 @@ test-persistence:
|
||||
@test -n "$(ACTION_SCRIPT)" || (echo "Set ACTION_SCRIPT to your implemented executable restart hook"; exit 2)
|
||||
$(PYTHON) tests/resilience.py --mode persistence --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS)
|
||||
|
||||
COMPOSE = docker compose --env-file .env
|
||||
|
||||
up:
|
||||
$(COMPOSE) up -d --build
|
||||
|
||||
test-student:
|
||||
$(COMPOSE) --profile test run --rm --build tests
|
||||
|
||||
explain:
|
||||
$(COMPOSE) exec -T api-a python -m app.explain
|
||||
|
||||
evidence:
|
||||
scripts/collect_evidence.sh
|
||||
|
||||
test-failover:
|
||||
@test -n "$(ACTION_SCRIPT)" || (echo "Set ACTION_SCRIPT to your implemented executable stop-one hook"; exit 2)
|
||||
@test -n "$(ACTION_SCRIPT)" || (echo "Set ACTION_SCRIPT=scripts/stop-one.sh"; exit 2)
|
||||
$(PYTHON) tests/resilience.py --mode failover --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS)
|
||||
|
||||
replicas:
|
||||
$(PYTHON) scripts/replicas.py status
|
||||
|
||||
restore:
|
||||
$(PYTHON) scripts/replicas.py restore
|
||||
|
||||
rolling:
|
||||
$(PYTHON) scripts/replicas.py rolling
|
||||
|
||||
lab4-evidence:
|
||||
$(PYTHON) scripts/lab4_evidence.py --base-url "$(BASE_URL)"
|
||||
|
||||
@@ -1,37 +1,99 @@
|
||||
# Отчёт — лабораторная 4
|
||||
|
||||
- Автор, группа: TODO
|
||||
- Целевая оценка; выбранный трек 5, если нужен: TODO
|
||||
- Commit/PR: TODO после публикации
|
||||
- Использованные источники, библиотеки и помощь ИИ: TODO
|
||||
- Автор, группа: Антонова Ирина Андреевна, БПИ2301.
|
||||
- Целевая оценка: 4; Compose + Nginx, две API-реплики, MinIO как S3-хранилище. Трек 5 не заявлен.
|
||||
- Commit/PR: 8d001712fe40bed7b16b1d26683360299f13a8fb
|
||||
- Использованные источники, библиотеки и помощь ИИ: FastAPI, SQLAlchemy async, asyncpg, Pydantic, Uvicorn, redis-py, minio-py, Pillow, pytest. lab2 подготовлена с Claude, перенос lab3 — с Codex; основной код lab4 написан с Codex; правки выполнены с Claude. Основа: lab3 без изменений бизнес-логики.
|
||||
|
||||
## Запуск из чистого clone
|
||||
|
||||
TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных.
|
||||
Образы: `nginx:1.28.0-alpine`, Python 3.12.8, `postgres:16.6-bookworm`, `redis:7.4.1-bookworm`, MinIO `RELEASE.2025-10-15T17-29-55Z` и mc `RELEASE.2025-08-13T08-35-41Z` (собираются из исходников, `infra/minio/Dockerfile`). На хосте нужны Docker с Compose v2 и Python 3.9+ (прогоны — Windows 10, Docker Desktop, Python 3.11.9).
|
||||
|
||||
```sh
|
||||
python scripts/init_env.py # создаёт .env со случайными секретами, значения не выводит
|
||||
docker compose --env-file .env config --quiet
|
||||
docker compose --env-file .env up -d --build
|
||||
python scripts/replicas.py restore # ждёт готовности обеих API через LB
|
||||
make test # публичный smoke
|
||||
docker compose --env-file .env --profile test run --rm --build tests
|
||||
python scripts/lab4_evidence.py --base-url "http://127.0.0.1:8080"
|
||||
```
|
||||
|
||||
На Windows клиентские запросы нужно слать на `127.0.0.1`, а не `localhost`: через `localhost` каждый запрос задерживается на ≈ 2 с из-за попытки IPv6 (замер `/health/live`: 2147 мс против 72 мс). Перед сбором: `$env:BASE_URL = "http://127.0.0.1:8080"`.
|
||||
|
||||
Порядок старта: `db` → `migrate` (код 0); `minio` → `minio-init` (код 0) → `api-a`, `api-b`, `cleanup` → `nginx`. `.env` создаёт оператор через `init_env.py` (пароль БД, root-учётные данные MinIO и отдельные `S3_ACCESS_KEY`/`S3_SECRET_KEY` для API); файл в `.gitignore`.
|
||||
|
||||
Безопасная остановка (тома `pgdata` и `miniodata` сохраняются): `docker compose --env-file .env stop`. Намеренное удаление данных: `docker compose --env-file .env down -v`.
|
||||
|
||||
## Архитектура и решения
|
||||
|
||||
TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab3, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации.
|
||||
Клиент → Nginx (`127.0.0.1:8080`, единственный опубликованный порт) → `api-a`/`api-b` → PostgreSQL (метаданные) и приватный bucket MinIO (байты); Redis — необязательный кэш; `cleanup` — служба сверки. БД, Redis, MinIO и cleanup находятся в `internal`-сети; консоль MinIO выключена и порт не публикуется.
|
||||
|
||||
- **Источник истины.** PostgreSQL: пользователи, сессии, чаты, сообщения и таблица `attachments` (миграции `0003`, `0004`): `owner_id`, `chat_id`, `filename` (только для отображения), `object_key` (UNIQUE), `content_type` (PNG/JPEG), `size_bytes` ≤ 10 MiB, `sha256`, `width`/`height` (CHECK ≤ 40 млн пикселей), `status` (`uploading`/`ready`). Байты — только в S3; у API нет томов, временный буфер загрузки — `TemporaryFile`.
|
||||
- **Object key** назначает сервер (`originals/<uuid4.hex>`); опасные `filename` отклоняются (400).
|
||||
- **Upload.** Проверка сессии и членства → проверка `Content-Type` (415) и `Content-Length` до чтения (413) → слоты: 2 загрузки и 1 декодирование на реплику (ожидание 30 с, иначе 503) → потоковый приём в файл с лимитом 10 MiB и таймаутом 30 с → проверка Pillow: размеры до декодирования, формат совпадает с MIME (415), анимация отклоняется, `verify()` + `load()` для битых файлов (415). Пользовательские ошибки не дают 500.
|
||||
- **Транзакционные границы.** COMMIT строки `uploading` («намерение») → `SELECT … FOR UPDATE` → единственный S3 PUT → `ready` + COMMIT → `201`. Если PUT прошёл, а COMMIT нет, объект остаётся учтённым строкой `uploading`.
|
||||
- **Download.** Только через API, стрим блоками 64 KiB; участникам чата — файл, постороннему — 404 (метаданные и байты). Приватный bucket и случайный ключ не заменяют ACL приложения: права проверяет API.
|
||||
- **Привязка.** `POST /messages` принимает `attachment_ids` (до 10): в одной транзакции проверяются существование, тот же чат и статус `ready`; идемпотентность lab3 сохранена (повтор с другим списком — 409).
|
||||
- **Компенсация.** Служба `cleanup` (`python -m app.cleanup --loop`, период 60 с) удаляет только строки `uploading` старше grace period 3600 с (`FOR UPDATE SKIP LOCKED`, пачки по 100): сначала объект, затем строка; при недоступном S3 повтор в следующем цикле. Загрузчик держит блокировку строки на время PUT, поэтому активные загрузки не удаляются; `ready` не удаляется никогда. Есть `--dry-run` и `--once`. Обход bucket для поиска объектов без строки не нужен (намерение фиксируется до PUT) и не реализован.
|
||||
- **Безопасность S3.** `minio-init` идемпотентен: `mb --ignore-existing`, `anonymous set none`, политика и отдельный пользователь API: `ListBucket` на bucket, `Get/Put/DeleteObject` только на `originals/*`. Root-ключи API не получают, секреты ≥ 32 символов проверяются.
|
||||
- **Nginx.** Для маршрута вложений: `client_max_body_size 11m`, `proxy_request_buffering off`, `proxy_next_upstream off` (бинарный POST без ключа идемпотентности не повторяется).
|
||||
- **Из lab3 перенесено:** две реплики, общие сессии в PostgreSQL, идемпотентность `message_requests`, службы `migrate`, скрипты failover/rolling/restart. **Изменено:** добавлены MinIO, `minio-init`, `cleanup`, `attachments`, `attachment_ids`, проверки изображений и слоты загрузки; readiness зависит от S3; лимит памяти API — 512 MiB. Все переменные `.env.example` используются в `compose.yaml` (`POSTGRES_*`, `SESSION_TTL_SECONDS`, `CACHE_URL`, `LOG_LEVEL`, `API_IMAGE`, `MINIO_ROOT_*`, `S3_BUCKET`, `S3_ACCESS_KEY`/`S3_SECRET_KEY`); `S3_ENDPOINT_URL` и параметры cleanup заданы в `compose.yaml`.
|
||||
|
||||
## Доказательства критериев
|
||||
|
||||
Прогон `python scripts/lab4_evidence.py --base-url http://127.0.0.1:8080`, 2026-11-05 21:25–21:34 UTC, Windows 10, Docker Desktop. `result.json`: `status`, `cluster_checks`, `student_tests` — PASS. Файлы — в `evidence/lab4/`; проверки lab3 — в `evidence/lab4/cluster/`.
|
||||
|
||||
| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства |
|
||||
| --- | --- | --- | --- |
|
||||
| Публичный smoke | make test | TODO | TODO |
|
||||
| Остальные пункты уровня 3 | TODO | TODO | TODO |
|
||||
| Все пункты уровня 4, если заявлен | TODO | TODO | TODO |
|
||||
| Выбранный трек 5, если заявлен | TODO | TODO | TODO |
|
||||
|
||||
TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены.
|
||||
| Публичный smoke | `make test` | PASS, 1.8 с | `01_public_smoke.txt` |
|
||||
| Lab3 сохранён: тесты и идемпотентность | `docker compose … run --rm --build tests`; `cluster_evidence.py` | 279 passed; 12 параллельных запросов с одним ключом → 1 сообщение, 1 строка истории, 2 реплики, 409 при другом теле | `09_student_tests.txt`, `cluster/05_concurrent_idempotency.json` |
|
||||
| Lab3 сохранён: failover, rolling, persistence | `cluster_evidence.py`, `tests/resilience.py` | отказ `api-a`/`api-b` по очереди: 618/618 и 572/572 без ошибок, дубликатов 0, чтение/запись после отказа 7.75/7.84 с и 8.53/8.42 с (≤ 10 с); публичный failover PASS (2 реплики, 92 записи, 0 ошибок, 6.67 с); rolling `api-a`/`api-b`: 1100/1100 и 1036/1036 без ошибок, дубликатов 0; persistence PASS | `cluster/06_failover_*.json`, `07_public_failover.txt`, `08_rolling_*.json`, `09_persistence.txt` |
|
||||
| Метаданные в PostgreSQL, байты в приватном bucket, не на API | схема, `compose.yaml` | `attachments` без байтов; у API нет томов; один PUT в S3 | `app/migrations/0003_attachments.sql`, `compose.yaml` |
|
||||
| 201 ready, побайтовое скачивание, Message с `attachment_ids`, чужой без доступа | `lab4_evidence.py` | 111 817 байт, SHA-256 совпал, сообщение видно участникам, посторонний: 404 для метаданных и байтов | `02_attachment_contract.json` |
|
||||
| Только реальные PNG/JPEG ≤ 10 MiB и ≤ 40 млн пикселей, MIME по содержимому, опасные filename | pytest | файл ровно 10 MiB принят; лимит байт с `Content-Length` и без → 413; лимит пикселей до декодирования; 8+ опасных имён → 400; подмена MIME → 415 | `09_student_tests.txt` |
|
||||
| Upload → остановка реплики → download через другую | `lab4_evidence.py` | загрузка через `api-b`, она `exited`/`pid=0`, скачивание через `api-a`, SHA-256 совпал, 6.97 с | `03_uploader_failover.json` |
|
||||
| Restart с сохранением volumes | `scripts/restart.py` | все контейнеры пересозданы, прежний токен работает, сообщение, метаданные и байты равны | `04_full_restart.json` |
|
||||
| **Ур. 4.** Большой файл, битое изображение, подмена MIME, чужое `attachment_id`, файл другого чата | pytest | 413 / 415 / 415 / 404 (сообщение не создаётся) / отклонено; ни одного 500 | `09_student_tests.txt` |
|
||||
| Обрыв загрузки и сбой между S3 и БД | pytest с внедрением сбоев + `cleanup --dry-run` | строка `uploading` остаётся учтённой, свежая не удаляется, после grace (имитация 2 ч) удаляется вместе с объектом; потерянный COMMIT → файл остаётся `ready`; обрыв тела не создаёт ни строки, ни объекта; живой dry-run — 0 кандидатов | `09_student_tests.txt`, `08_cleanup_dry_run.txt` |
|
||||
| Лимиты, потоковая обработка, ≥ 10 одновременных загрузок с пиком памяти | `lab4_evidence.py` | 10/10 успешно, прирост памяти 0.9 и 14.1 MiB из лимита 512 MiB (ниже) | `07_concurrent_uploads.json` |
|
||||
| Идемпотентный bootstrap, отдельные ограниченные credentials, консоль закрыта, private bucket | `minio-init` дважды; `private_bucket` | оба запуска код 0; анонимный GET → 403; `AccessDenied` на чужой bucket, создание bucket и PUT вне `originals/`; root-ключей в API нет | `06_bootstrap_repeat_*.txt`, `06_private_bucket.json` |
|
||||
| Трек 5 | Не заявлен | — | — |
|
||||
|
||||
## Эксперименты и отказы
|
||||
|
||||
TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения.
|
||||
Окружение: один локальный хост (Windows 10, Docker Desktop, 16 CPU). Лимиты: API 0.5 CPU / 512 MiB, PostgreSQL и MinIO 1 CPU / 512 MiB. Данные синтетические.
|
||||
|
||||
| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных |
|
||||
**Эксперимент 1. Одновременные загрузки и память API.** Гипотеза: потоковый приём в файл и слоты загрузки ограничивают память API независимо от числа клиентов. Методика: 10 клиентов стартуют по барьеру и загружают PNG 1024×683 (2 099 087 байт); память по cgroup и RSS снимается каждые 0.2 с. Результат: 10/10 успешно, 0.72 с, 13.9 загрузки/с; задержка 0.39 / 0.56 (медиана) / 0.72 с; все скачивания совпали с оригиналом.
|
||||
|
||||
| Реплика | cgroup до | Пик | Прирост | RSS, пик | Лимит |
|
||||
| --- | --- | --- | --- | --- | --- |
|
||||
| api-a | 89.5 MiB | 90.4 MiB | 0.9 MiB | 96.7 MiB | 512 MiB |
|
||||
| api-b | 78.2 MiB | 92.3 MiB | 14.1 MiB | 96.7 MiB | 512 MiB |
|
||||
|
||||
| Воздействие | Что ожидаем | Что наблюдали | Восстановление / потеря данных |
|
||||
| --- | --- | --- | --- |
|
||||
| TODO | TODO | TODO | TODO |
|
||||
| Остановка реплики, принявшей загрузку | Файл скачивается через вторую | SHA-256 совпал | 6.97 с; потерь нет |
|
||||
| Недоступность S3 | Контролируемая ошибка, не 500 | `ready`, upload и content → 503 `unavailable` с `request_id`; публичный `/health/ready` — 503; после возврата MinIO файл равен оригиналу | время восстановления не измерялось; потерь нет |
|
||||
| Полный restart с сохранением томов | Токен и данные доступны | Сессия жива, метаданные и байты равны | restart ≈ 29 с; потерь нет |
|
||||
| Сбой S3 / БД между PUT и COMMIT, потерянный COMMIT, обрыв клиента | Нет вечных orphan, `ready` и свежие не удаляются | Намерение удаляется вместе с объектом после grace; `ready` не трогается; при обрыве нет ни строки, ни объекта | grace ≥ 1 ч; пользовательские данные целы |
|
||||
| Отказ реплики и rolling под нагрузкой (lab3) | Без ошибок клиента | 0 ошибок во всех прогонах (числа в таблице критериев); p50/p95 ≈ 47/172–188 мс; замена реплики 34.5 и 36.8 с | потерь нет |
|
||||
|
||||
*Ограничения измерения:* один прогон каждого эксперимента; в нагрузочном тесте файл ≈ 2 MiB, а не 10 MiB; прогон длился 0.7 с, поэтому в выборку памяти попало мало точек, а cgroup включает файловый кэш; значения отражают локальный Docker. Время восстановления отсчитывается от начала действия и включает hook-скрипт (поиск контейнера и `docker compose stop --timeout 6`, ≈ 7.7–8.1 с), запас до порога 10 с небольшой. Таймер Windows ≈ 15.6 мс, поэтому задержки кратны этому шагу.
|
||||
|
||||
**Изменения инструментов сбора.** Лимит ожидания команды остановки реплики в `scripts/cluster_evidence.py` увеличен с 10 до 60 с (на Docker Desktop она иногда не укладывалась); публичные тесты и пороги не менялись. Клиентские замеры идут через `127.0.0.1`. Ранние неудачные прогоны в доказательства не входят.
|
||||
|
||||
## Ограничения и следующие шаги
|
||||
|
||||
TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза.
|
||||
**Выдерживается:** остановка одной API (файл доступен через другую); замена обеих реплик под нагрузкой; полный restart с прежним токеном; недоступность S3 (503, затем восстановление без потери данных); обрыв загрузки и сбой между S3 и БД без вечных неучтённых объектов.
|
||||
|
||||
**Не выдерживается / упрощения:** отказ хоста; PostgreSQL и MinIO — единые точки отказа без репликации и backup; неудачная загрузка занимает место до истечения grace (≥ 1 ч); нет обхода bucket (потерянный файл при строке `ready` не обнаружится — трек 5.2); бинарный POST вложения автоматически не повторяется; память проверена на файле ≈ 2 MiB в одном прогоне, на 10 MiB — гипотеза; cleanup подтверждён тестами с внедрением сбоев, а живой dry-run нашёл 0 кандидатов; rolling проверен без смены версии образа, смешанный rolling со старым контрактом lab3 не поддерживается (изменён контракт сообщений); время восстановления после возврата S3 не измерялось.
|
||||
|
||||
**Следующие шаги:** отказоустойчивый PostgreSQL и распределённый MinIO; reconciler с dry-run для объектов без метаданных и наоборот; presigned upload; метрики по возрасту записей `uploading`; нагрузочная проверка на файлах 10 MiB с повторами. Подтверждено экспериментами: ограничение памяти при 10 загрузках ≈ 2 MiB, поведение при недоступности S3, переключение при остановке реплики-загрузчика, failover, rolling и persistence; остальное — гипотезы.
|
||||
|
||||
## Первичные источники
|
||||
|
||||
- [S3 object uploads](https://docs.aws.amazon.com/AmazonS3/latest/userguide/upload-objects.html)
|
||||
- [MinIO](https://github.com/minio/minio)
|
||||
- [Nginx proxy_next_upstream](https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_next_upstream)
|
||||
- [PostgreSQL: явные блокировки](https://www.postgresql.org/docs/16/explicit-locking.html)
|
||||
@@ -0,0 +1 @@
|
||||
"""Мессенджер: HTTP API на FastAPI с PostgreSQL и кэшем сессий в Redis."""
|
||||
@@ -0,0 +1,48 @@
|
||||
"""Точка входа: python -m app запускает HTTP-сервер."""
|
||||
|
||||
import signal
|
||||
import sys
|
||||
|
||||
import uvicorn
|
||||
|
||||
from .config import Settings
|
||||
from .logging_setup import get_logger
|
||||
from .main import create_app
|
||||
|
||||
|
||||
class GracefulServer(uvicorn.Server):
|
||||
"""Снять readiness по сигналу, затем завершить запросы средствами Uvicorn."""
|
||||
|
||||
def handle_exit(self, sig, frame):
|
||||
self.config.app.state.ready = False
|
||||
super().handle_exit(sig, frame)
|
||||
|
||||
|
||||
def main() -> int:
|
||||
"""Прочитать настройки и запустить uvicorn; вернуть код выхода."""
|
||||
try:
|
||||
settings = Settings.from_env()
|
||||
except ValueError as error:
|
||||
print("Ошибка настроек:", error, file=sys.stderr)
|
||||
return 2
|
||||
app = create_app(settings)
|
||||
get_logger().info("listen host=%s port=%d", settings.host, settings.port)
|
||||
config = uvicorn.Config(
|
||||
app,
|
||||
host=settings.host,
|
||||
port=settings.port,
|
||||
log_level=settings.log_level.lower(),
|
||||
access_log=False,
|
||||
timeout_graceful_shutdown=5,
|
||||
)
|
||||
server = GracefulServer(config)
|
||||
if sys.platform == "win32":
|
||||
signal.signal(
|
||||
signal.SIGBREAK, lambda signum, frame: setattr(server, "should_exit", True)
|
||||
)
|
||||
server.run()
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
@@ -0,0 +1,211 @@
|
||||
"""Synchronous binary uploads with bounded staging and durable database intents."""
|
||||
|
||||
import asyncio
|
||||
import hashlib
|
||||
import tempfile
|
||||
import unicodedata
|
||||
import uuid
|
||||
import warnings
|
||||
from urllib.parse import quote
|
||||
|
||||
import anyio
|
||||
from fastapi import APIRouter, Depends, Request
|
||||
from fastapi.responses import StreamingResponse
|
||||
from PIL import Image, UnidentifiedImageError
|
||||
from sqlalchemy import select, func
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from starlette.requests import ClientDisconnect
|
||||
|
||||
from .auth import current_user, get_session
|
||||
from .errors import ApiError
|
||||
from .http_utils import bad_request, parse_uuid
|
||||
from .models import Attachment, ChatMember, User
|
||||
from .routes import open_chat
|
||||
from .schemas import AttachmentOut
|
||||
from .storage import MAX_BYTES, StorageUnavailable, unavailable
|
||||
from .timeutil import format_ts
|
||||
|
||||
router = APIRouter(prefix="/api/v1")
|
||||
MAX_PIXELS = 40_000_000
|
||||
Image.MAX_IMAGE_PIXELS = MAX_PIXELS
|
||||
|
||||
|
||||
def attachment_out(row):
|
||||
return {
|
||||
"id": str(row.id), "chat_id": str(row.chat_id), "owner_id": str(row.owner_id),
|
||||
"filename": row.filename, "content_type": row.content_type,
|
||||
"size_bytes": row.size_bytes, "sha256": row.sha256,
|
||||
"status": row.status,
|
||||
"variants": [], "error_code": None, "created_at": format_ts(row.created_at),
|
||||
"updated_at": format_ts(row.updated_at),
|
||||
}
|
||||
|
||||
|
||||
def validate_filename(value):
|
||||
if value is None or not 1 <= len(value) <= 255 or not value.strip():
|
||||
raise bad_request("filename: от 1 до 255 символов")
|
||||
if any(char in "/\\" or unicodedata.category(char) in ("Cc", "Cs") for char in value):
|
||||
raise bad_request("filename содержит путь или управляющие символы")
|
||||
return value
|
||||
|
||||
|
||||
def validate_image(stream, declared):
|
||||
"""Check dimensions before decode; verify CRC and actually decode pixel data."""
|
||||
try:
|
||||
stream.seek(0)
|
||||
with warnings.catch_warnings():
|
||||
warnings.simplefilter("error", Image.DecompressionBombWarning)
|
||||
with Image.open(stream, formats=["PNG", "JPEG"]) as image:
|
||||
width, height = image.size
|
||||
if width * height > MAX_PIXELS:
|
||||
raise ApiError(413, "payload_too_large", "Изображение больше 40 млн пикселей")
|
||||
actual = {"PNG": "image/png", "JPEG": "image/jpeg"}[image.format]
|
||||
if actual != declared:
|
||||
raise ApiError(415, "unsupported_media_type", "MIME не соответствует содержимому")
|
||||
# Animated images are not part of the single-image upload contract.
|
||||
if getattr(image, "n_frames", 1) != 1:
|
||||
raise ApiError(415, "unsupported_media_type", "Нужно одно изображение")
|
||||
image.verify()
|
||||
stream.seek(0)
|
||||
with Image.open(stream, formats=["PNG", "JPEG"]) as image:
|
||||
image.load()
|
||||
return width, height
|
||||
except (Image.DecompressionBombWarning, Image.DecompressionBombError):
|
||||
raise ApiError(413, "payload_too_large", "Изображение больше 40 млн пикселей") from None
|
||||
except (UnidentifiedImageError, OSError, SyntaxError, ValueError, EOFError):
|
||||
raise ApiError(415, "unsupported_media_type", "Повреждённый PNG/JPEG") from None
|
||||
finally:
|
||||
stream.seek(0)
|
||||
|
||||
|
||||
async def save_attachment(session, storage, row, stream):
|
||||
"""Commit intent BEFORE PUT; lock it until final ready commit.
|
||||
|
||||
On failure keep the intent. A cleanup worker rechecks its state after grace;
|
||||
never delete blindly after an ambiguous COMMIT acknowledgement.
|
||||
"""
|
||||
session.add(row)
|
||||
await session.commit()
|
||||
try:
|
||||
locked = (await session.execute(
|
||||
select(Attachment.id).where(Attachment.id == row.id).with_for_update()
|
||||
)).scalar_one_or_none()
|
||||
if locked is None:
|
||||
raise ApiError(409, "conflict", "Срок незавершённой загрузки истёк")
|
||||
await anyio.to_thread.run_sync(storage.put, row.object_key, stream, row.size_bytes, row.content_type)
|
||||
row.status = "ready"
|
||||
row.updated_at = (await session.execute(select(func.clock_timestamp()))).scalar_one()
|
||||
await session.commit()
|
||||
except BaseException:
|
||||
await session.rollback()
|
||||
raise
|
||||
return row
|
||||
|
||||
|
||||
@router.post("/chats/{chat_id}/attachments", status_code=201, response_model=AttachmentOut)
|
||||
async def upload(chat_id: str, request: Request, user: User = Depends(current_user),
|
||||
session: AsyncSession = Depends(get_session)):
|
||||
chat = await open_chat(session, user, chat_id)
|
||||
filename = validate_filename(request.query_params.get("filename"))
|
||||
declared = request.headers.get("content-type", "").split(";", 1)[0].strip().lower()
|
||||
if declared not in ("image/png", "image/jpeg"):
|
||||
raise ApiError(415, "unsupported_media_type", "Нужен Content-Type image/png или image/jpeg")
|
||||
length = request.headers.get("content-length")
|
||||
if length is not None and (not length.isascii() or not length.isdigit()):
|
||||
raise bad_request("Некорректный Content-Length")
|
||||
if length is not None and (len(length) > 20 or int(length) > MAX_BYTES):
|
||||
raise ApiError(413, "payload_too_large", "Файл больше 10 MiB")
|
||||
chat_uuid, owner_uuid = chat.id, user.id
|
||||
# Do not hold a DB connection during the client's network upload or slot wait.
|
||||
await session.rollback()
|
||||
storage = request.app.state.storage
|
||||
if storage is None:
|
||||
raise unavailable()
|
||||
try:
|
||||
await asyncio.wait_for(request.app.state.upload_slots.acquire(), timeout=30)
|
||||
except TimeoutError:
|
||||
raise ApiError(503, "unavailable", "Все слоты загрузки заняты") from None
|
||||
try:
|
||||
with tempfile.TemporaryFile(mode="w+b") as stream:
|
||||
size, checksum = 0, hashlib.sha256()
|
||||
try:
|
||||
async with asyncio.timeout(30):
|
||||
async for chunk in request.stream():
|
||||
size += len(chunk)
|
||||
if size > MAX_BYTES:
|
||||
raise ApiError(413, "payload_too_large", "Файл больше 10 MiB")
|
||||
checksum.update(chunk)
|
||||
stream.write(chunk)
|
||||
except TimeoutError:
|
||||
raise ApiError(408, "invalid_request", "Истекло время приёма файла") from None
|
||||
except ClientDisconnect:
|
||||
raise bad_request("Передача файла прервана") from None
|
||||
if length is not None and int(length) != size:
|
||||
raise bad_request("Неполное тело запроса")
|
||||
async with request.app.state.decode_slot:
|
||||
width, height = await anyio.to_thread.run_sync(validate_image, stream, declared)
|
||||
row = Attachment(
|
||||
id=uuid.uuid4(), chat_id=chat_uuid, owner_id=owner_uuid,
|
||||
filename=filename, object_key="originals/" + uuid.uuid4().hex,
|
||||
content_type=declared, size_bytes=size, sha256=checksum.hexdigest(),
|
||||
width=width, height=height, status="uploading",
|
||||
)
|
||||
await save_attachment(session, storage, row, stream)
|
||||
return attachment_out(row)
|
||||
except StorageUnavailable:
|
||||
raise unavailable() from None
|
||||
finally:
|
||||
request.app.state.upload_slots.release()
|
||||
|
||||
|
||||
async def accessible_attachment(session, user, attachment_id):
|
||||
ident = uuid.UUID(parse_uuid(attachment_id, "attachment_id"))
|
||||
row = (await session.execute(
|
||||
select(Attachment).join(ChatMember, ChatMember.chat_id == Attachment.chat_id)
|
||||
.where(Attachment.id == ident, ChatMember.user_id == user.id)
|
||||
)).scalar_one_or_none()
|
||||
if row is None:
|
||||
raise ApiError(404, "not_found", "Объект не найден")
|
||||
if row.status != "ready":
|
||||
raise ApiError(409, "conflict", "Вложение ещё не готово")
|
||||
return row
|
||||
|
||||
|
||||
@router.get("/attachments/{attachment_id}", response_model=AttachmentOut)
|
||||
async def metadata(attachment_id: str, user: User = Depends(current_user),
|
||||
session: AsyncSession = Depends(get_session)):
|
||||
return attachment_out(await accessible_attachment(session, user, attachment_id))
|
||||
|
||||
|
||||
@router.get("/attachments/{attachment_id}/content")
|
||||
async def content(attachment_id: str, request: Request, user: User = Depends(current_user),
|
||||
session: AsyncSession = Depends(get_session)):
|
||||
row = await accessible_attachment(session, user, attachment_id)
|
||||
if request.query_params.get("variant", "original") != "original":
|
||||
raise ApiError(404, "not_found", "Вариант не найден")
|
||||
key, mime, size, name = row.object_key, row.content_type, row.size_bytes, row.filename
|
||||
await session.rollback()
|
||||
storage = request.app.state.storage
|
||||
if storage is None:
|
||||
raise unavailable()
|
||||
try:
|
||||
response = await anyio.to_thread.run_sync(storage.get, key)
|
||||
except StorageUnavailable:
|
||||
raise unavailable() from None
|
||||
|
||||
async def chunks():
|
||||
try:
|
||||
while True:
|
||||
chunk = await anyio.to_thread.run_sync(response.read, 64 * 1024)
|
||||
if not chunk:
|
||||
break
|
||||
yield chunk
|
||||
finally:
|
||||
response.close()
|
||||
response.release_conn()
|
||||
|
||||
return StreamingResponse(chunks(), media_type=mime, headers={
|
||||
"Content-Length": str(size), "X-Content-Type-Options": "nosniff",
|
||||
"Cache-Control": "private, no-store",
|
||||
"Content-Disposition": "attachment; filename*=UTF-8''" + quote(name, safe=""),
|
||||
})
|
||||
+103
@@ -0,0 +1,103 @@
|
||||
"""Аутентификация: разбор Authorization (Basic и Bearer) и определение пользователя."""
|
||||
|
||||
import base64
|
||||
import binascii
|
||||
import re
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
from typing import AsyncIterator, Tuple
|
||||
|
||||
from fastapi import Depends, Request
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from . import crud
|
||||
from .errors import ApiError
|
||||
from .models import User
|
||||
from .security import hash_token
|
||||
|
||||
BASIC_CHALLENGE = 'Basic realm="messenger", charset="UTF-8"'
|
||||
BEARER_CHALLENGE = 'Bearer realm="messenger"'
|
||||
TOKEN_RE = re.compile(r"[A-Za-z0-9_-]{20,200}")
|
||||
|
||||
|
||||
def unauthorized(challenge: str) -> ApiError:
|
||||
"""Ошибка 401 с заголовком WWW-Authenticate и одинаковым текстом."""
|
||||
return ApiError(
|
||||
401, "unauthorized", "Нет действительной аутентификации", {"WWW-Authenticate": challenge}
|
||||
)
|
||||
|
||||
|
||||
async def get_session(request: Request) -> AsyncIterator[AsyncSession]:
|
||||
"""Зависимость FastAPI: сессия БД на время запроса."""
|
||||
async with request.app.state.sessionmaker() as session:
|
||||
yield session
|
||||
|
||||
|
||||
def parse_basic(request: Request) -> Tuple[str, str]:
|
||||
"""Вернуть (username, password) из Basic; при любой ошибке формата 401."""
|
||||
header = request.headers.get("authorization", "")
|
||||
scheme, _, encoded = header.partition(" ")
|
||||
if scheme.lower() != "basic" or not encoded.strip():
|
||||
raise unauthorized(BASIC_CHALLENGE)
|
||||
try:
|
||||
raw = base64.b64decode(encoded.strip(), validate=True)
|
||||
username_bytes, separator, password_bytes = raw.partition(b":")
|
||||
if not separator:
|
||||
raise ValueError("нет двоеточия")
|
||||
return username_bytes.decode("ascii"), password_bytes.decode("utf-8")
|
||||
except (binascii.Error, ValueError):
|
||||
raise unauthorized(BASIC_CHALLENGE)
|
||||
|
||||
|
||||
def parse_bearer(request: Request) -> str:
|
||||
"""Вернуть токен из Bearer; при неверной схеме или формате 401."""
|
||||
header = request.headers.get("authorization", "")
|
||||
scheme, _, token = header.partition(" ")
|
||||
token = token.strip()
|
||||
if scheme.lower() != "bearer" or not TOKEN_RE.fullmatch(token):
|
||||
raise unauthorized(BEARER_CHALLENGE)
|
||||
return token
|
||||
|
||||
|
||||
def cached_user(data: dict) -> User:
|
||||
"""Собрать User из данных кэша (без привязки к сессии БД)."""
|
||||
return User(
|
||||
id=uuid.UUID(data["id"]),
|
||||
username=data["username"],
|
||||
display_name=data["display_name"],
|
||||
password_hash="",
|
||||
created_at=datetime.fromisoformat(data["created_at"]),
|
||||
)
|
||||
|
||||
|
||||
async def current_user(
|
||||
request: Request, session: AsyncSession = Depends(get_session)
|
||||
) -> User:
|
||||
"""Зависимость FastAPI: пользователь по Bearer-токену.
|
||||
|
||||
Кэш хранит данные пользователя; действительность сессии проверяет PostgreSQL.
|
||||
При промахе или недоступном Redis пользователь тоже загружается из БД.
|
||||
Ошибка БД превращается в 503, неизвестный токен -- в 401.
|
||||
"""
|
||||
token_hash = hash_token(parse_bearer(request))
|
||||
cache = request.app.state.cache
|
||||
data = await cache.get(token_hash)
|
||||
if data is not None:
|
||||
if data.get("revoked") or not await crud.session_is_active(session, token_hash):
|
||||
raise unauthorized(BEARER_CHALLENGE)
|
||||
return cached_user(data)
|
||||
found = await crud.get_user_by_token(session, token_hash)
|
||||
if found is None:
|
||||
raise unauthorized(BEARER_CHALLENGE)
|
||||
user, remaining_seconds = found
|
||||
await cache.put(
|
||||
token_hash,
|
||||
{
|
||||
"id": str(user.id),
|
||||
"username": user.username,
|
||||
"display_name": user.display_name,
|
||||
"created_at": user.created_at.isoformat(),
|
||||
},
|
||||
remaining_seconds,
|
||||
)
|
||||
return user
|
||||
+148
@@ -0,0 +1,148 @@
|
||||
"""Read-through кэш сессий в Redis; источник истины -- таблица sessions.
|
||||
|
||||
Ключ session:<sha256 токена>, TTL не больше остатка сессии. Заполнение
|
||||
через SET NX, logout записывает отметку отзыва. При недоступном Redis
|
||||
запросы идут в PostgreSQL; потерянная отметка отзыва ведёт к FLUSHDB
|
||||
перед следующим использованием кэша в текущем процессе. Положительное
|
||||
попадание дополнительно проверяется по sessions в PostgreSQL, поэтому
|
||||
потеря локального флага при рестарте не отменяет отзыв.
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
import time
|
||||
from typing import Awaitable, Callable, Dict, Optional, TypeVar
|
||||
|
||||
from redis.asyncio import Redis
|
||||
from redis.exceptions import RedisError
|
||||
|
||||
from .logging_setup import get_logger
|
||||
|
||||
KEY_PREFIX = "session:"
|
||||
REVOKED = "revoked"
|
||||
CACHE_ERRORS = (RedisError, OSError, asyncio.TimeoutError)
|
||||
|
||||
T = TypeVar("T")
|
||||
|
||||
|
||||
class SessionCache:
|
||||
"""Кэш сессий поверх Redis; ошибки Redis не выходят наружу."""
|
||||
|
||||
enabled = True
|
||||
|
||||
def __init__(self, url: str, ttl_cap: int, timeout_ms: int, retry_seconds: int):
|
||||
"""Создать клиент Redis с таймаутами; соединение открывается лениво."""
|
||||
timeout = timeout_ms / 1000
|
||||
self.client = Redis.from_url(
|
||||
url,
|
||||
socket_timeout=timeout,
|
||||
socket_connect_timeout=timeout,
|
||||
retry_on_timeout=False,
|
||||
decode_responses=True,
|
||||
)
|
||||
self.ttl_cap = ttl_cap
|
||||
self.timeout = timeout
|
||||
self.retry_seconds = retry_seconds
|
||||
self.down_until = 0.0
|
||||
self.dirty = False
|
||||
self.stats: Dict[str, int] = {
|
||||
"hits": 0, "misses": 0, "revoked_hits": 0, "errors": 0, "bypassed": 0
|
||||
}
|
||||
|
||||
async def _call(self, operation: Callable[[], Awaitable[T]]) -> T:
|
||||
"""Выполнить операцию с таймаутом, паузой после сбоя и очисткой грязного кэша."""
|
||||
if time.monotonic() < self.down_until:
|
||||
self.stats["bypassed"] += 1
|
||||
raise ConnectionError("cache is paused after a failure")
|
||||
try:
|
||||
if self.dirty:
|
||||
await asyncio.wait_for(self.client.flushdb(), self.timeout * 2)
|
||||
self.dirty = False
|
||||
get_logger().warning("cache: FLUSHDB after a lost revocation")
|
||||
return await asyncio.wait_for(operation(), self.timeout * 2)
|
||||
except CACHE_ERRORS as error:
|
||||
self.stats["errors"] += 1
|
||||
self.down_until = time.monotonic() + self.retry_seconds
|
||||
get_logger().warning("cache_unavailable type=%s", type(error).__name__)
|
||||
raise
|
||||
|
||||
async def get(self, token_hash: str) -> Optional[dict]:
|
||||
"""Данные пользователя, {"revoked": True} или None при промахе и сбое."""
|
||||
try:
|
||||
raw = await self._call(lambda: self.client.get(KEY_PREFIX + token_hash))
|
||||
except CACHE_ERRORS:
|
||||
return None
|
||||
if raw is None:
|
||||
self.stats["misses"] += 1
|
||||
return None
|
||||
if raw == REVOKED:
|
||||
self.stats["revoked_hits"] += 1
|
||||
return {"revoked": True}
|
||||
self.stats["hits"] += 1
|
||||
return json.loads(raw)
|
||||
|
||||
async def put(self, token_hash: str, user: dict, remaining_seconds: float) -> None:
|
||||
"""Сохранить сессию с TTL не больше её остатка, не затирая существующий ключ."""
|
||||
ttl_ms = int(min(self.ttl_cap, remaining_seconds) * 1000)
|
||||
if ttl_ms <= 0:
|
||||
return
|
||||
payload = json.dumps(user, separators=(",", ":"))
|
||||
try:
|
||||
await self._call(
|
||||
lambda: self.client.set(KEY_PREFIX + token_hash, payload, px=ttl_ms, nx=True)
|
||||
)
|
||||
except CACHE_ERRORS:
|
||||
pass
|
||||
|
||||
async def revoke(self, token_hash: str) -> None:
|
||||
"""Записать отметку отзыва; при сбое пометить кэш для очистки."""
|
||||
try:
|
||||
await self._call(
|
||||
lambda: self.client.set(
|
||||
KEY_PREFIX + token_hash, REVOKED, px=self.ttl_cap * 1000
|
||||
)
|
||||
)
|
||||
except CACHE_ERRORS:
|
||||
self.dirty = True
|
||||
|
||||
async def close(self) -> None:
|
||||
"""Закрыть соединения с Redis."""
|
||||
try:
|
||||
await self.client.aclose()
|
||||
except CACHE_ERRORS:
|
||||
pass
|
||||
|
||||
|
||||
class NoCache:
|
||||
"""Заглушка при выключенном кэше: всегда промах."""
|
||||
|
||||
enabled = False
|
||||
stats: Dict[str, int] = {}
|
||||
|
||||
async def get(self, token_hash: str) -> Optional[dict]:
|
||||
"""Всегда промах."""
|
||||
return None
|
||||
|
||||
async def put(self, token_hash: str, user: dict, remaining_seconds: float) -> None:
|
||||
"""Ничего не делает."""
|
||||
return None
|
||||
|
||||
async def revoke(self, token_hash: str) -> None:
|
||||
"""Ничего не делает."""
|
||||
return None
|
||||
|
||||
async def close(self) -> None:
|
||||
"""Ничего не делает."""
|
||||
return None
|
||||
|
||||
|
||||
def create_cache(settings) -> "SessionCache | NoCache":
|
||||
"""SessionCache, если задан CACHE_URL, иначе NoCache."""
|
||||
if not settings.cache_url:
|
||||
return NoCache()
|
||||
return SessionCache(
|
||||
settings.cache_url,
|
||||
settings.cache_ttl_seconds,
|
||||
settings.cache_timeout_ms,
|
||||
settings.cache_retry_seconds,
|
||||
)
|
||||
@@ -0,0 +1,80 @@
|
||||
"""Retryable compensation of unfinished uploads after a safe grace period."""
|
||||
|
||||
import argparse
|
||||
import asyncio
|
||||
from datetime import timedelta
|
||||
import json
|
||||
|
||||
import anyio
|
||||
from sqlalchemy import delete, func, select
|
||||
|
||||
from .config import Settings
|
||||
from .db import create_engine, create_sessionmaker
|
||||
from .models import Attachment
|
||||
from .storage import MinioStorage, StorageUnavailable
|
||||
|
||||
|
||||
async def cleanup_once(sessionmaker, storage, grace_seconds=3600, dry_run=False):
|
||||
"""Only old uploading intents, never ready files, with row locks and a batch cap.
|
||||
|
||||
A successful delete followed by failed COMMIT is retried safely: DeleteObject
|
||||
is idempotent. An uploader holds the same row lock throughout PUT/ready.
|
||||
"""
|
||||
if grace_seconds < 3600:
|
||||
raise ValueError("Grace period must be at least one hour")
|
||||
result = {"candidates": 0, "deleted": 0, "failed": 0, "dry_run": dry_run}
|
||||
async with sessionmaker() as session:
|
||||
rows = list((await session.execute(
|
||||
select(Attachment).where(
|
||||
Attachment.status == "uploading",
|
||||
Attachment.created_at < func.clock_timestamp() - timedelta(seconds=grace_seconds),
|
||||
).order_by(Attachment.created_at).limit(100).with_for_update(skip_locked=True)
|
||||
)).scalars())
|
||||
result["candidates"] = len(rows)
|
||||
for row in rows:
|
||||
if dry_run:
|
||||
continue
|
||||
try:
|
||||
await anyio.to_thread.run_sync(storage.delete, row.object_key)
|
||||
except StorageUnavailable:
|
||||
result["failed"] += 1
|
||||
continue
|
||||
await session.execute(delete(Attachment).where(Attachment.id == row.id))
|
||||
result["deleted"] += 1
|
||||
await session.commit()
|
||||
return result
|
||||
|
||||
|
||||
async def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
mode = parser.add_mutually_exclusive_group()
|
||||
mode.add_argument("--loop", action="store_true")
|
||||
mode.add_argument("--once", action="store_true")
|
||||
parser.add_argument("--dry-run", action="store_true")
|
||||
args = parser.parse_args()
|
||||
settings = Settings.from_env()
|
||||
engine = create_engine(settings)
|
||||
storage = MinioStorage(settings)
|
||||
try:
|
||||
while True:
|
||||
try:
|
||||
result = await cleanup_once(create_sessionmaker(engine), storage,
|
||||
settings.cleanup_grace_seconds, args.dry_run)
|
||||
print(json.dumps(result), flush=True)
|
||||
if result["failed"] and not args.loop:
|
||||
return 1
|
||||
except Exception as error:
|
||||
# Never log SQL, connection strings or exception details.
|
||||
print(json.dumps({"status": "error", "type": type(error).__name__}), flush=True)
|
||||
if not args.loop:
|
||||
return 1
|
||||
if not args.loop:
|
||||
return 0
|
||||
await asyncio.sleep(settings.cleanup_interval_seconds)
|
||||
finally:
|
||||
storage.close()
|
||||
await engine.dispose()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(asyncio.run(main()))
|
||||
+122
@@ -0,0 +1,122 @@
|
||||
"""Настройки приложения из переменных окружения."""
|
||||
|
||||
import os
|
||||
from dataclasses import dataclass
|
||||
from typing import Mapping, Optional
|
||||
|
||||
LOG_LEVELS = ("DEBUG", "INFO", "WARNING", "ERROR")
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class Settings:
|
||||
"""Неизменяемый набор настроек."""
|
||||
|
||||
host: str
|
||||
port: int
|
||||
env: str
|
||||
log_level: str
|
||||
database_url: str
|
||||
session_ttl_seconds: int
|
||||
session_store: str
|
||||
db_pool_size: int
|
||||
db_max_overflow: int
|
||||
db_pool_timeout: int
|
||||
db_startup_wait_seconds: int
|
||||
cache_url: str
|
||||
cache_ttl_seconds: int
|
||||
cache_timeout_ms: int
|
||||
cache_retry_seconds: int
|
||||
|
||||
s3_endpoint_url: str = ""
|
||||
s3_region: str = "us-east-1"
|
||||
s3_bucket: str = "attachments"
|
||||
s3_access_key: str = ""
|
||||
s3_secret_key: str = ""
|
||||
cleanup_grace_seconds: int = 3600
|
||||
cleanup_interval_seconds: int = 60
|
||||
|
||||
@classmethod
|
||||
def from_env(cls, environ: Optional[Mapping[str, str]] = None) -> "Settings":
|
||||
"""Собрать и проверить настройки; при ошибке ValueError с описанием."""
|
||||
source = os.environ if environ is None else environ
|
||||
|
||||
def get(name: str, default: str) -> str:
|
||||
value = source.get(name)
|
||||
if value is None or value.strip() == "":
|
||||
return default
|
||||
return value.strip()
|
||||
|
||||
def get_int(name: str, default: int, low: int, high: int) -> int:
|
||||
text = get(name, str(default))
|
||||
if not text.isdigit() or not (low <= int(text) <= high):
|
||||
raise ValueError(
|
||||
"%s должен быть числом от %d до %d" % (name, low, high)
|
||||
)
|
||||
return int(text)
|
||||
|
||||
log_level = get("LOG_LEVEL", "INFO").upper()
|
||||
if log_level not in LOG_LEVELS:
|
||||
raise ValueError(
|
||||
"LOG_LEVEL должен быть одним из %s" % ", ".join(LOG_LEVELS)
|
||||
)
|
||||
|
||||
session_store = get("SESSION_STORE", "postgres")
|
||||
if session_store != "postgres":
|
||||
raise ValueError(
|
||||
"SESSION_STORE: источник истины сессий -- только postgres "
|
||||
"(Redis -- кэш, задаётся CACHE_URL)"
|
||||
)
|
||||
|
||||
database_url = _read_database_url(source)
|
||||
if not database_url.startswith("postgresql+asyncpg://"):
|
||||
raise ValueError("DATABASE_URL должен начинаться с postgresql+asyncpg://")
|
||||
|
||||
return cls(
|
||||
host=get("APP_HOST", "127.0.0.1"),
|
||||
port=get_int("APP_PORT", 8080, 1, 65535),
|
||||
env=get("APP_ENV", "lab"),
|
||||
log_level=log_level,
|
||||
database_url=database_url,
|
||||
session_ttl_seconds=get_int("SESSION_TTL_SECONDS", 3600, 1, 30 * 24 * 3600),
|
||||
session_store=session_store,
|
||||
db_pool_size=get_int("DB_POOL_SIZE", 5, 1, 50),
|
||||
db_max_overflow=get_int("DB_MAX_OVERFLOW", 5, 0, 50),
|
||||
db_pool_timeout=get_int("DB_POOL_TIMEOUT", 5, 1, 60),
|
||||
db_startup_wait_seconds=get_int("DB_STARTUP_WAIT_SECONDS", 30, 0, 300),
|
||||
cache_url=_read_cache_url(source),
|
||||
cache_ttl_seconds=get_int("CACHE_TTL_SECONDS", 60, 1, 3600),
|
||||
cache_timeout_ms=get_int("CACHE_TIMEOUT_MS", 300, 10, 5000),
|
||||
cache_retry_seconds=get_int("CACHE_RETRY_SECONDS", 5, 0, 300),
|
||||
s3_endpoint_url=get("S3_ENDPOINT_URL", ""),
|
||||
s3_region=get("S3_REGION", "us-east-1"),
|
||||
s3_bucket=get("S3_BUCKET", "attachments"),
|
||||
s3_access_key=get("S3_ACCESS_KEY", ""),
|
||||
s3_secret_key=get("S3_SECRET_KEY", ""),
|
||||
cleanup_grace_seconds=get_int("CLEANUP_GRACE_SECONDS", 3600, 3600, 604800),
|
||||
cleanup_interval_seconds=get_int("CLEANUP_INTERVAL_SECONDS", 60, 10, 3600),
|
||||
)
|
||||
|
||||
|
||||
def _read_database_url(source: Mapping[str, str]) -> str:
|
||||
"""Адрес БД из DATABASE_URL_FILE или DATABASE_URL."""
|
||||
path = source.get("DATABASE_URL_FILE", "").strip()
|
||||
if path:
|
||||
with open(path, encoding="utf-8") as handle:
|
||||
return handle.read().strip()
|
||||
value = source.get("DATABASE_URL", "").strip()
|
||||
if not value:
|
||||
raise ValueError("Не задан DATABASE_URL (или DATABASE_URL_FILE)")
|
||||
return value
|
||||
|
||||
|
||||
def _read_cache_url(source: Mapping[str, str]) -> str:
|
||||
"""Адрес Redis из CACHE_URL_FILE или CACHE_URL; пустая строка -- кэш выключен."""
|
||||
path = source.get("CACHE_URL_FILE", "").strip()
|
||||
if path:
|
||||
with open(path, encoding="utf-8") as handle:
|
||||
value = handle.read().strip()
|
||||
else:
|
||||
value = source.get("CACHE_URL", "").strip()
|
||||
if value and not value.startswith(("redis://", "rediss://")):
|
||||
raise ValueError("CACHE_URL должен начинаться с redis:// или rediss://")
|
||||
return value
|
||||
+262
@@ -0,0 +1,262 @@
|
||||
"""Запросы к PostgreSQL через SQLAlchemy с параметрами.
|
||||
|
||||
Изменяющие функции -- одна транзакция: commit в конце, rollback при ошибке.
|
||||
"""
|
||||
|
||||
import hashlib
|
||||
import uuid
|
||||
from datetime import datetime, timedelta
|
||||
from typing import Dict, List, Optional, Tuple
|
||||
|
||||
from sqlalchemy import Select, delete, func, insert, select, tuple_
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from .models import Attachment, Chat, ChatMember, Message, MessageRequest, SessionRow, User
|
||||
from .errors import ApiError
|
||||
|
||||
UNIQUE_VIOLATION = "23505"
|
||||
FOREIGN_KEY_VIOLATION = "23503"
|
||||
|
||||
Position = Tuple[datetime, uuid.UUID]
|
||||
|
||||
|
||||
class UsernameTaken(Exception):
|
||||
"""Username уже занят."""
|
||||
|
||||
|
||||
class IdempotencyConflict(Exception):
|
||||
"""Ключ уже использован с другим телом запроса."""
|
||||
|
||||
|
||||
class UnknownMember(Exception):
|
||||
"""Среди участников чата есть несуществующий пользователь."""
|
||||
|
||||
|
||||
def sqlstate(error: IntegrityError) -> Optional[str]:
|
||||
"""Код SQLSTATE из исключения SQLAlchemy."""
|
||||
original = error.orig
|
||||
code = getattr(original, "sqlstate", None)
|
||||
if code is None:
|
||||
code = getattr(getattr(original, "__cause__", None), "sqlstate", None)
|
||||
return code
|
||||
|
||||
|
||||
async def create_user(
|
||||
session: AsyncSession, username: str, display_name: str, password_hash: str
|
||||
) -> User:
|
||||
"""Создать пользователя; дубликат username -> UsernameTaken."""
|
||||
statement = (
|
||||
insert(User)
|
||||
.values(
|
||||
id=uuid.uuid4(),
|
||||
username=username,
|
||||
display_name=display_name,
|
||||
password_hash=password_hash,
|
||||
)
|
||||
.returning(User)
|
||||
)
|
||||
try:
|
||||
user = (await session.execute(statement)).scalar_one()
|
||||
await session.commit()
|
||||
except IntegrityError as error:
|
||||
await session.rollback()
|
||||
if sqlstate(error) == UNIQUE_VIOLATION:
|
||||
raise UsernameTaken(username)
|
||||
raise
|
||||
return user
|
||||
|
||||
|
||||
async def get_user_by_username(session: AsyncSession, username: str) -> Optional[User]:
|
||||
"""Пользователь по username или None."""
|
||||
return (
|
||||
await session.execute(select(User).where(User.username == username))
|
||||
).scalar_one_or_none()
|
||||
|
||||
|
||||
async def create_session(
|
||||
session: AsyncSession, user_id: uuid.UUID, token_hash: str, ttl_seconds: int
|
||||
) -> datetime:
|
||||
"""Удалить истёкшие сессии пользователя, создать новую, вернуть expires_at."""
|
||||
await session.execute(
|
||||
delete(SessionRow).where(
|
||||
SessionRow.user_id == user_id, SessionRow.expires_at < func.now()
|
||||
)
|
||||
)
|
||||
statement = (
|
||||
insert(SessionRow)
|
||||
.values(
|
||||
token_hash=token_hash,
|
||||
user_id=user_id,
|
||||
expires_at=func.now() + timedelta(seconds=ttl_seconds),
|
||||
)
|
||||
.returning(SessionRow.expires_at)
|
||||
)
|
||||
expires_at = (await session.execute(statement)).scalar_one()
|
||||
await session.commit()
|
||||
return expires_at
|
||||
|
||||
|
||||
async def get_user_by_token(
|
||||
session: AsyncSession, token_hash: str
|
||||
) -> Optional[Tuple[User, float]]:
|
||||
"""(User, секунд до истечения) для действующей сессии или None."""
|
||||
statement = (
|
||||
select(User, func.extract("epoch", SessionRow.expires_at - func.now()))
|
||||
.join(SessionRow, SessionRow.user_id == User.id)
|
||||
.where(SessionRow.token_hash == token_hash, SessionRow.expires_at > func.now())
|
||||
)
|
||||
row = (await session.execute(statement)).one_or_none()
|
||||
if row is None:
|
||||
return None
|
||||
return row[0], float(row[1])
|
||||
|
||||
|
||||
async def session_is_active(session: AsyncSession, token_hash: str) -> bool:
|
||||
"""Проверить наличие неистёкшей сессии без загрузки пользователя."""
|
||||
statement = select(
|
||||
select(SessionRow.token_hash)
|
||||
.where(SessionRow.token_hash == token_hash, SessionRow.expires_at > func.now())
|
||||
.exists()
|
||||
)
|
||||
return (await session.execute(statement)).scalar_one()
|
||||
|
||||
|
||||
async def delete_session(session: AsyncSession, token_hash: str) -> None:
|
||||
"""Удалить сессию по хешу токена."""
|
||||
await session.execute(delete(SessionRow).where(SessionRow.token_hash == token_hash))
|
||||
await session.commit()
|
||||
|
||||
|
||||
async def create_chat(
|
||||
session: AsyncSession, title: str, member_ids: List[uuid.UUID]
|
||||
) -> Tuple[Chat, List[uuid.UUID]]:
|
||||
"""Атомарно создать чат и участников; неизвестный участник -> UnknownMember."""
|
||||
chat_statement = insert(Chat).values(id=uuid.uuid4(), title=title).returning(Chat)
|
||||
try:
|
||||
chat = (await session.execute(chat_statement)).scalar_one()
|
||||
await session.execute(
|
||||
insert(ChatMember),
|
||||
[{"chat_id": chat.id, "user_id": user_id} for user_id in member_ids],
|
||||
)
|
||||
await session.commit()
|
||||
except IntegrityError as error:
|
||||
await session.rollback()
|
||||
if sqlstate(error) == FOREIGN_KEY_VIOLATION:
|
||||
raise UnknownMember()
|
||||
raise
|
||||
return chat, member_ids
|
||||
|
||||
|
||||
async def get_chat_for_member(
|
||||
session: AsyncSession, chat_id: uuid.UUID, user_id: uuid.UUID
|
||||
) -> Optional[Chat]:
|
||||
"""Чат, если пользователь в нём состоит, иначе None."""
|
||||
statement = (
|
||||
select(Chat)
|
||||
.join(ChatMember, ChatMember.chat_id == Chat.id)
|
||||
.where(Chat.id == chat_id, ChatMember.user_id == user_id)
|
||||
)
|
||||
return (await session.execute(statement)).scalar_one_or_none()
|
||||
|
||||
|
||||
async def get_member_ids(
|
||||
session: AsyncSession, chat_ids: List[uuid.UUID]
|
||||
) -> Dict[uuid.UUID, List[uuid.UUID]]:
|
||||
"""Участники нескольких чатов одним запросом: {chat_id: [user_id]}."""
|
||||
result: Dict[uuid.UUID, List[uuid.UUID]] = {chat_id: [] for chat_id in chat_ids}
|
||||
if chat_ids:
|
||||
statement = (
|
||||
select(ChatMember.chat_id, ChatMember.user_id)
|
||||
.where(ChatMember.chat_id.in_(chat_ids))
|
||||
.order_by(ChatMember.chat_id, ChatMember.user_id)
|
||||
)
|
||||
for chat_id, user_id in (await session.execute(statement)).all():
|
||||
result[chat_id].append(user_id)
|
||||
return result
|
||||
|
||||
|
||||
async def list_chats(
|
||||
session: AsyncSession, user_id: uuid.UUID, after: Optional[Position], limit: int
|
||||
) -> Tuple[List[Chat], bool]:
|
||||
"""Страница чатов пользователя по (created_at, id) и признак следующей страницы."""
|
||||
statement = (
|
||||
select(Chat)
|
||||
.join(ChatMember, ChatMember.chat_id == Chat.id)
|
||||
.where(ChatMember.user_id == user_id)
|
||||
)
|
||||
if after is not None:
|
||||
statement = statement.where(tuple_(Chat.created_at, Chat.id) > tuple_(*after))
|
||||
statement = statement.order_by(Chat.created_at, Chat.id).limit(limit + 1)
|
||||
rows = list((await session.execute(statement)).scalars())
|
||||
return rows[:limit], len(rows) > limit
|
||||
|
||||
|
||||
async def create_message_idempotent(
|
||||
session: AsyncSession, chat_id: uuid.UUID, sender_id: uuid.UUID, text: str, key: str,
|
||||
attachment_ids: Optional[List[uuid.UUID]] = None,
|
||||
) -> Message:
|
||||
"""Атомарно сохранить сообщение и результат на 24 часа во всех API-репликах."""
|
||||
attachment_ids = attachment_ids or []
|
||||
scope = f"{sender_id}:{chat_id}:{key}".encode("utf-8")
|
||||
lock_id = int.from_bytes(hashlib.sha256(scope).digest()[:8], "big", signed=True)
|
||||
match = (
|
||||
MessageRequest.user_id == sender_id,
|
||||
MessageRequest.chat_id == chat_id,
|
||||
MessageRequest.key == key,
|
||||
)
|
||||
try:
|
||||
await session.execute(select(func.pg_advisory_xact_lock(lock_id)))
|
||||
previous = (await session.execute(
|
||||
select(MessageRequest).where(*match, MessageRequest.expires_at > func.clock_timestamp())
|
||||
)).scalar_one_or_none()
|
||||
if previous is not None:
|
||||
if previous.request_text != text or previous.request_attachment_ids != attachment_ids:
|
||||
raise IdempotencyConflict()
|
||||
message = (await session.execute(
|
||||
select(Message).where(Message.id == previous.message_id)
|
||||
)).scalar_one()
|
||||
else:
|
||||
if attachment_ids:
|
||||
attachments = list((await session.execute(select(Attachment).where(
|
||||
Attachment.id.in_(attachment_ids), Attachment.chat_id == chat_id,
|
||||
))).scalars())
|
||||
if len(attachments) != len(attachment_ids):
|
||||
raise ApiError(404, "not_found", "Объект не найден")
|
||||
if any(row.status != "ready" for row in attachments):
|
||||
raise ApiError(409, "conflict", "Вложение ещё не готово")
|
||||
await session.execute(delete(MessageRequest).where(*match))
|
||||
message = (await session.execute(
|
||||
insert(Message).values(
|
||||
id=uuid.uuid4(), chat_id=chat_id, sender_id=sender_id, text=text,
|
||||
attachment_ids=attachment_ids,
|
||||
).returning(Message)
|
||||
)).scalar_one()
|
||||
await session.execute(insert(MessageRequest).values(
|
||||
user_id=sender_id, chat_id=chat_id, key=key, request_text=text,
|
||||
request_attachment_ids=attachment_ids,
|
||||
message_id=message.id, expires_at=func.clock_timestamp() + timedelta(hours=24),
|
||||
))
|
||||
await session.commit()
|
||||
return message
|
||||
except BaseException:
|
||||
await session.rollback()
|
||||
raise
|
||||
|
||||
|
||||
def history_query(chat_id: uuid.UUID, after: Optional[Position], limit: int) -> Select:
|
||||
"""SELECT страницы истории по курсору (created_at, id); идёт по индексу чата."""
|
||||
statement = select(Message).where(Message.chat_id == chat_id)
|
||||
if after is not None:
|
||||
statement = statement.where(
|
||||
tuple_(Message.created_at, Message.id) > tuple_(*after)
|
||||
)
|
||||
return statement.order_by(Message.created_at, Message.id).limit(limit + 1)
|
||||
|
||||
|
||||
async def list_messages(
|
||||
session: AsyncSession, chat_id: uuid.UUID, after: Optional[Position], limit: int
|
||||
) -> Tuple[List[Message], bool]:
|
||||
"""Страница истории чата и признак следующей страницы."""
|
||||
rows = list((await session.execute(history_query(chat_id, after, limit))).scalars())
|
||||
return rows[:limit], len(rows) > limit
|
||||
@@ -0,0 +1,51 @@
|
||||
"""Непрозрачный keyset-курсор пагинации.
|
||||
|
||||
Хранит позицию (микросекунды, id) и область действия, поэтому курсор
|
||||
другого ресурса отвергается.
|
||||
"""
|
||||
|
||||
import base64
|
||||
import binascii
|
||||
import json
|
||||
import re
|
||||
from typing import Tuple
|
||||
|
||||
from .timeutil import MAX_TIMESTAMP_MICROS
|
||||
|
||||
Key = Tuple[int, str]
|
||||
|
||||
UUID_RE = re.compile(
|
||||
r"[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}"
|
||||
)
|
||||
MAX_CURSOR_LENGTH = 512
|
||||
|
||||
|
||||
def encode_cursor(scope: str, key: Key) -> str:
|
||||
"""Закодировать позицию и область в URL-безопасную строку."""
|
||||
payload = json.dumps({"s": scope, "t": key[0], "i": key[1]}, separators=(",", ":"))
|
||||
return base64.urlsafe_b64encode(payload.encode("ascii")).decode("ascii").rstrip("=")
|
||||
|
||||
|
||||
def decode_cursor(text: str, scope: str) -> Key:
|
||||
"""Раскодировать курсор; ValueError, если он испорчен или чужой."""
|
||||
if len(text) > MAX_CURSOR_LENGTH or not re.fullmatch(r"[A-Za-z0-9_-]+", text):
|
||||
raise ValueError("bad cursor")
|
||||
try:
|
||||
padded = text + "=" * (-len(text) % 4)
|
||||
data = json.loads(base64.urlsafe_b64decode(padded.encode("ascii")))
|
||||
except (binascii.Error, ValueError, UnicodeError):
|
||||
raise ValueError("bad cursor")
|
||||
if not isinstance(data, dict) or set(data) != {"s", "t", "i"}:
|
||||
raise ValueError("bad cursor")
|
||||
stamp, ident = data["t"], data["i"]
|
||||
if data["s"] != scope:
|
||||
raise ValueError("cursor belongs to another resource")
|
||||
if (
|
||||
not isinstance(stamp, int)
|
||||
or isinstance(stamp, bool)
|
||||
or not 0 <= stamp <= MAX_TIMESTAMP_MICROS
|
||||
):
|
||||
raise ValueError("bad cursor")
|
||||
if not isinstance(ident, str) or not UUID_RE.fullmatch(ident):
|
||||
raise ValueError("bad cursor")
|
||||
return stamp, ident
|
||||
@@ -0,0 +1,27 @@
|
||||
"""Подключение к PostgreSQL: движок с пулом и фабрика сессий."""
|
||||
|
||||
from sqlalchemy.ext.asyncio import AsyncEngine, async_sessionmaker, create_async_engine
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from .config import Settings
|
||||
|
||||
|
||||
def create_engine(settings: Settings) -> AsyncEngine:
|
||||
"""Async-движок с ограниченным пулом и таймаутами."""
|
||||
return create_async_engine(
|
||||
settings.database_url,
|
||||
pool_size=settings.db_pool_size,
|
||||
max_overflow=settings.db_max_overflow,
|
||||
pool_timeout=settings.db_pool_timeout,
|
||||
pool_pre_ping=True,
|
||||
connect_args={
|
||||
"timeout": 5,
|
||||
"command_timeout": 5,
|
||||
"server_settings": {"application_name": "messenger-api"},
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
def create_sessionmaker(engine: AsyncEngine) -> "async_sessionmaker[AsyncSession]":
|
||||
"""Фабрика сессий без истечения объектов после commit."""
|
||||
return async_sessionmaker(engine, expire_on_commit=False)
|
||||
@@ -0,0 +1,37 @@
|
||||
"""Единый формат ошибок API."""
|
||||
|
||||
from typing import Dict, Optional
|
||||
|
||||
from fastapi.responses import JSONResponse
|
||||
|
||||
|
||||
class ApiError(Exception):
|
||||
"""Исключение, которое превращается в ответ с кодом и сообщением."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
status: int,
|
||||
code: str,
|
||||
message: str,
|
||||
headers: Optional[Dict[str, str]] = None,
|
||||
):
|
||||
super().__init__(message)
|
||||
self.status = status
|
||||
self.code = code
|
||||
self.message = message
|
||||
self.headers = headers
|
||||
|
||||
|
||||
def error_response(
|
||||
request_id: str,
|
||||
status: int,
|
||||
code: str,
|
||||
message: str,
|
||||
headers: Optional[Dict[str, str]] = None,
|
||||
) -> JSONResponse:
|
||||
"""JSON-ответ вида {"error": {code, message, request_id}}."""
|
||||
return JSONResponse(
|
||||
status_code=status,
|
||||
content={"error": {"code": code, "message": message, "request_id": request_id}},
|
||||
headers=headers,
|
||||
)
|
||||
+139
@@ -0,0 +1,139 @@
|
||||
"""Анализ плана запроса истории: EXPLAIN (ANALYZE, BUFFERS) на синтетических данных.
|
||||
|
||||
Запуск: python -m app.explain [--messages N]. Создаёт данные один раз,
|
||||
затем сравнивает запрос приложения с OFFSET и с вариантом без индекса.
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import asyncio
|
||||
import sys
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
from typing import List, Optional, Tuple
|
||||
|
||||
from sqlalchemy.ext.asyncio import AsyncEngine
|
||||
|
||||
from .config import Settings
|
||||
from .crud import history_query
|
||||
from .db import create_engine
|
||||
|
||||
SEED_NAMESPACE = uuid.UUID("6f1c1a52-6a39-4c1e-9d0e-5ab0e0c0e2a1")
|
||||
BIG_CHAT = uuid.uuid5(SEED_NAMESPACE, "chat-big")
|
||||
OTHER_CHATS = [uuid.uuid5(SEED_NAMESPACE, "chat-%d" % n) for n in range(49)]
|
||||
SENDERS = [uuid.uuid5(SEED_NAMESPACE, "user-a"), uuid.uuid5(SEED_NAMESPACE, "user-b")]
|
||||
PAGE = 50
|
||||
DEEP_OFFSET = 15000
|
||||
|
||||
|
||||
async def seed(pg, total: int) -> None:
|
||||
"""Создать синтетических пользователей, чаты и сообщения, если их ещё нет."""
|
||||
if await pg.fetchval("SELECT 1 FROM chats WHERE id = $1", BIG_CHAT):
|
||||
return
|
||||
async with pg.transaction():
|
||||
await pg.executemany(
|
||||
"INSERT INTO users (id, username, display_name, password_hash)"
|
||||
" VALUES ($1, $2, $3, 'disabled')",
|
||||
[(SENDERS[0], "explain_seed_a", "Seed A"), (SENDERS[1], "explain_seed_b", "Seed B")],
|
||||
)
|
||||
chats = [BIG_CHAT] + OTHER_CHATS
|
||||
await pg.executemany(
|
||||
"INSERT INTO chats (id, title, created_at)"
|
||||
" VALUES ($1, $2, now() - interval '30 days')",
|
||||
[(chat, "explain seed %d" % n) for n, chat in enumerate(chats)],
|
||||
)
|
||||
await pg.executemany(
|
||||
"INSERT INTO chat_members (chat_id, user_id) VALUES ($1, $2)",
|
||||
[(chat, sender) for chat in chats for sender in SENDERS],
|
||||
)
|
||||
await pg.execute(
|
||||
"""
|
||||
INSERT INTO messages (id, chat_id, sender_id, text, created_at)
|
||||
SELECT gen_random_uuid(),
|
||||
CASE WHEN g % 3 = 0 THEN $1::uuid ELSE ($2::uuid[])[1 + g % 49] END,
|
||||
($3::uuid[])[1 + g % 2],
|
||||
'synthetic message #' || g,
|
||||
now() - interval '30 days' + g * interval '10 milliseconds'
|
||||
FROM generate_series(1, $4::int) AS g
|
||||
""",
|
||||
BIG_CHAT, OTHER_CHATS, SENDERS, total,
|
||||
)
|
||||
await pg.execute("ANALYZE messages")
|
||||
await pg.execute("ANALYZE chats")
|
||||
await pg.execute("ANALYZE chat_members")
|
||||
|
||||
|
||||
def compiled(engine: AsyncEngine, after: Optional[Tuple[datetime, uuid.UUID]]) -> Tuple[str, List]:
|
||||
"""Текст SQL запроса истории и значения его параметров."""
|
||||
query = history_query(BIG_CHAT, after, PAGE).compile(dialect=engine.dialect)
|
||||
return query.string, [query.params[name] for name in query.positiontup]
|
||||
|
||||
|
||||
async def explain(pg, title: str, sql: str, params: List) -> None:
|
||||
"""Напечатать запрос, параметры и план выполнения."""
|
||||
print("\n" + "=" * 78)
|
||||
print(title)
|
||||
print("=" * 78)
|
||||
print(sql)
|
||||
print("-- параметры:", ", ".join(str(p) for p in params))
|
||||
for row in await pg.fetch("EXPLAIN (ANALYZE, BUFFERS) " + sql, *params):
|
||||
print(row[0])
|
||||
|
||||
|
||||
async def main_async(total: int) -> int:
|
||||
"""Заполнить данные и вывести планы запросов."""
|
||||
engine = create_engine(Settings.from_env())
|
||||
try:
|
||||
async with engine.connect() as conn:
|
||||
pg = (await conn.get_raw_connection()).driver_connection
|
||||
await seed(pg, total)
|
||||
print("PostgreSQL:", await pg.fetchval("SHOW server_version"))
|
||||
print("Сообщений всего:", await pg.fetchval("SELECT count(*) FROM messages"))
|
||||
print("Сообщений в большом чате:",
|
||||
await pg.fetchval("SELECT count(*) FROM messages WHERE chat_id = $1", BIG_CHAT))
|
||||
print("Размер таблицы messages:",
|
||||
await pg.fetchval("SELECT pg_size_pretty(pg_relation_size('messages'))"))
|
||||
print("Размер индекса messages_chat_created_idx:",
|
||||
await pg.fetchval("SELECT pg_size_pretty(pg_relation_size('messages_chat_created_idx'))"))
|
||||
cursor = await pg.fetchrow(
|
||||
"SELECT created_at, id FROM messages WHERE chat_id = $1"
|
||||
" ORDER BY created_at, id OFFSET $2 LIMIT 1",
|
||||
BIG_CHAT, DEEP_OFFSET - 1,
|
||||
)
|
||||
after = (cursor["created_at"], cursor["id"])
|
||||
|
||||
sql, params = compiled(engine, None)
|
||||
await explain(pg, "1. Первая страница (без cursor), запрос приложения", sql, params)
|
||||
sql, params = compiled(engine, after)
|
||||
await explain(pg, "2. Глубокая страница: cursor после %d-го сообщения (keyset)"
|
||||
% DEEP_OFFSET, sql, params)
|
||||
await explain(
|
||||
pg, "3. Для сравнения: та же страница через OFFSET (так приложение НЕ делает)",
|
||||
"SELECT * FROM messages WHERE chat_id = $1 ORDER BY created_at, id"
|
||||
" OFFSET %d LIMIT %d" % (DEEP_OFFSET, PAGE + 1),
|
||||
[BIG_CHAT],
|
||||
)
|
||||
tx = pg.transaction()
|
||||
await tx.start()
|
||||
try:
|
||||
await pg.execute("DROP INDEX messages_chat_created_idx")
|
||||
await explain(pg, "4. Для сравнения: запрос приложения БЕЗ индекса"
|
||||
" (DROP INDEX в транзакции, затем ROLLBACK)", sql, params)
|
||||
finally:
|
||||
await tx.rollback()
|
||||
print("\nИндекс messages_chat_created_idx на месте:",
|
||||
bool(await pg.fetchval("SELECT to_regclass('messages_chat_created_idx')")))
|
||||
finally:
|
||||
await engine.dispose()
|
||||
return 0
|
||||
|
||||
|
||||
def main() -> int:
|
||||
"""Разобрать аргументы командной строки и запустить анализ."""
|
||||
parser = argparse.ArgumentParser(description=__doc__.splitlines()[0])
|
||||
parser.add_argument("--messages", type=int, default=60000,
|
||||
help="сколько синтетических сообщений создать при первом запуске")
|
||||
return asyncio.run(main_async(parser.parse_args().messages))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
@@ -0,0 +1,20 @@
|
||||
"""Проверка готовности для HEALTHCHECK контейнера."""
|
||||
|
||||
import os
|
||||
import sys
|
||||
import urllib.request
|
||||
|
||||
|
||||
def main() -> int:
|
||||
"""0, если /health/ready отвечает 200, иначе 1."""
|
||||
port = os.environ.get("APP_PORT", "8080").strip() or "8080"
|
||||
try:
|
||||
url = "http://127.0.0.1:%s/health/ready" % port
|
||||
with urllib.request.urlopen(url, timeout=2) as reply:
|
||||
return 0 if reply.status == 200 else 1
|
||||
except Exception:
|
||||
return 1
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
@@ -0,0 +1,113 @@
|
||||
"""Помощники HTTP: чтение JSON, проверка тела, разбор UUID, limit и cursor."""
|
||||
|
||||
import json
|
||||
import re
|
||||
from typing import Optional, Type, TypeVar
|
||||
|
||||
from fastapi import Request
|
||||
from pydantic import BaseModel, ValidationError
|
||||
|
||||
from .cursor import Key, UUID_RE, decode_cursor
|
||||
from .errors import ApiError
|
||||
|
||||
MAX_BODY_BYTES = 256 * 1024
|
||||
DEFAULT_LIMIT = 50
|
||||
MAX_LIMIT = 100
|
||||
|
||||
T = TypeVar("T", bound=BaseModel)
|
||||
|
||||
|
||||
def bad_request(message: str) -> ApiError:
|
||||
"""Ошибка 400 invalid_request."""
|
||||
return ApiError(400, "invalid_request", message)
|
||||
|
||||
|
||||
def _reject_constant(name: str):
|
||||
"""Запретить NaN и Infinity в JSON."""
|
||||
raise ValueError("недопустимое значение " + name)
|
||||
|
||||
|
||||
async def read_json_object(request: Request) -> dict:
|
||||
"""Прочитать тело запроса как JSON-объект."""
|
||||
content_type = request.headers.get("content-type", "")
|
||||
media_type, _, params = content_type.partition(";")
|
||||
if media_type.strip().lower() != "application/json":
|
||||
raise ApiError(
|
||||
415, "unsupported_media_type", "Нужен Content-Type: application/json"
|
||||
)
|
||||
charset = re.search(r"charset\s*=\s*\"?([^\";\s]+)", params, re.IGNORECASE)
|
||||
if charset and charset.group(1).lower() not in ("utf-8", "utf8"):
|
||||
raise ApiError(
|
||||
415, "unsupported_media_type", "Поддерживается только кодировка UTF-8"
|
||||
)
|
||||
|
||||
declared = request.headers.get("content-length", "")
|
||||
if declared.isdigit() and int(declared) > MAX_BODY_BYTES:
|
||||
raise ApiError(413, "payload_too_large", "Тело запроса слишком большое")
|
||||
|
||||
chunks = []
|
||||
size = 0
|
||||
async for chunk in request.stream():
|
||||
size += len(chunk)
|
||||
if size > MAX_BODY_BYTES:
|
||||
raise ApiError(413, "payload_too_large", "Тело запроса слишком большое")
|
||||
chunks.append(chunk)
|
||||
|
||||
try:
|
||||
data = json.loads(
|
||||
b"".join(chunks).decode("utf-8"), parse_constant=_reject_constant
|
||||
)
|
||||
except (UnicodeDecodeError, ValueError, RecursionError):
|
||||
raise bad_request("Тело запроса не является корректным JSON")
|
||||
if not isinstance(data, dict):
|
||||
raise bad_request("Тело запроса должно быть JSON-объектом")
|
||||
return data
|
||||
|
||||
|
||||
def validate_body(model: Type[T], data: dict) -> T:
|
||||
"""Проверить словарь моделью Pydantic; ошибки -> 400."""
|
||||
try:
|
||||
return model.model_validate(data)
|
||||
except ValidationError as exc:
|
||||
problems = []
|
||||
for err in exc.errors()[:3]:
|
||||
field = ".".join(str(part) for part in err["loc"]) or "body"
|
||||
problems.append("%s: %s" % (field, err["msg"].replace("Value error, ", "")))
|
||||
raise bad_request("Неверные данные: " + "; ".join(problems))
|
||||
|
||||
|
||||
def parse_uuid(value: str, what: str) -> str:
|
||||
"""Проверить строку как UUID и вернуть её в нижнем регистре."""
|
||||
if not UUID_RE.fullmatch(value):
|
||||
raise bad_request("%s должен быть UUID" % what)
|
||||
return value.lower()
|
||||
|
||||
|
||||
IDEMPOTENCY_KEY_RE = re.compile(r"[A-Za-z0-9._:-]{1,128}")
|
||||
|
||||
|
||||
def parse_idempotency_key(request: Request) -> str:
|
||||
"""Один обязательный ASCII Idempotency-Key длиной 1–128 символов."""
|
||||
values = request.headers.getlist("idempotency-key")
|
||||
if len(values) != 1 or not IDEMPOTENCY_KEY_RE.fullmatch(values[0]):
|
||||
raise bad_request("Нужен корректный Idempotency-Key")
|
||||
return values[0]
|
||||
|
||||
|
||||
def parse_limit(raw: Optional[str]) -> int:
|
||||
"""Размер страницы: по умолчанию 50, допустимо 1-100."""
|
||||
if raw is None:
|
||||
return DEFAULT_LIMIT
|
||||
if not re.fullmatch(r"[0-9]{1,6}", raw) or not 1 <= int(raw) <= MAX_LIMIT:
|
||||
raise bad_request("limit должен быть целым числом от 1 до %d" % MAX_LIMIT)
|
||||
return int(raw)
|
||||
|
||||
|
||||
def parse_cursor(raw: Optional[str], scope: str) -> Optional[Key]:
|
||||
"""Позиция из курсора или None; неверный курсор -> 400."""
|
||||
if raw is None or raw == "":
|
||||
return None
|
||||
try:
|
||||
return decode_cursor(raw, scope)
|
||||
except ValueError:
|
||||
raise bad_request("Некорректный cursor")
|
||||
@@ -0,0 +1,25 @@
|
||||
"""Настройка логирования в stdout."""
|
||||
|
||||
import logging
|
||||
import sys
|
||||
|
||||
LOGGER_NAME = "messenger"
|
||||
|
||||
|
||||
def get_logger() -> logging.Logger:
|
||||
"""Логгер приложения."""
|
||||
return logging.getLogger(LOGGER_NAME)
|
||||
|
||||
|
||||
def setup_logging(level: str = "INFO") -> logging.Logger:
|
||||
"""Задать уровень и формат: одна строка на событие."""
|
||||
logger = get_logger()
|
||||
logger.setLevel(level)
|
||||
if not logger.handlers:
|
||||
handler = logging.StreamHandler(sys.stdout)
|
||||
handler.setFormatter(
|
||||
logging.Formatter("%(asctime)s %(levelname)s %(message)s", "%Y-%m-%dT%H:%M:%S%z")
|
||||
)
|
||||
logger.addHandler(handler)
|
||||
logger.propagate = False
|
||||
return logger
|
||||
+128
@@ -0,0 +1,128 @@
|
||||
"""Сборка приложения FastAPI: зависимости, миграции, обработчики ошибок."""
|
||||
|
||||
import asyncio
|
||||
import uuid
|
||||
from contextlib import asynccontextmanager
|
||||
from typing import Optional
|
||||
|
||||
from fastapi import FastAPI, Request
|
||||
from fastapi.exceptions import RequestValidationError
|
||||
from sqlalchemy.exc import InterfaceError, OperationalError
|
||||
from sqlalchemy.exc import TimeoutError as PoolTimeoutError
|
||||
from starlette.exceptions import HTTPException as StarletteHTTPException
|
||||
|
||||
from .cache import create_cache
|
||||
from .config import Settings
|
||||
from .db import create_engine, create_sessionmaker
|
||||
from .errors import ApiError, error_response
|
||||
from .logging_setup import get_logger, setup_logging
|
||||
from .middleware import RequestContextMiddleware
|
||||
from .migrate import check_schema
|
||||
from .routes import api, router
|
||||
from .attachments import router as attachment_router
|
||||
from .storage import MinioStorage
|
||||
|
||||
DB_UNAVAILABLE = (
|
||||
OperationalError,
|
||||
InterfaceError,
|
||||
PoolTimeoutError,
|
||||
OSError,
|
||||
asyncio.TimeoutError,
|
||||
)
|
||||
|
||||
|
||||
def create_app(settings: Optional[Settings] = None) -> FastAPI:
|
||||
"""Создать приложение FastAPI с заданными настройками."""
|
||||
settings = settings or Settings.from_env()
|
||||
setup_logging(settings.log_level)
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
"""Старт: кэш, движок, проверка версии схемы. Остановка: закрыть соединения."""
|
||||
get_logger().info(
|
||||
"config env=%s pool=%d+%d ttl=%ds store=%s cache=%s",
|
||||
settings.env,
|
||||
settings.db_pool_size,
|
||||
settings.db_max_overflow,
|
||||
settings.session_ttl_seconds,
|
||||
settings.session_store,
|
||||
"redis" if settings.cache_url else "off",
|
||||
)
|
||||
app.state.cache = create_cache(settings)
|
||||
engine = create_engine(settings)
|
||||
app.state.engine = engine
|
||||
app.state.sessionmaker = create_sessionmaker(engine)
|
||||
app.state.storage = MinioStorage(settings) if settings.s3_endpoint_url else None
|
||||
app.state.upload_slots = asyncio.Semaphore(2)
|
||||
app.state.decode_slot = asyncio.Semaphore(1)
|
||||
try:
|
||||
await check_schema(engine)
|
||||
app.state.ready = True
|
||||
get_logger().info("startup: приложение готово")
|
||||
yield
|
||||
finally:
|
||||
app.state.ready = False
|
||||
await app.state.cache.close()
|
||||
if app.state.storage is not None:
|
||||
app.state.storage.close()
|
||||
await engine.dispose()
|
||||
get_logger().info("shutdown: соединения с БД закрыты")
|
||||
|
||||
app = FastAPI(title="Messenger API", version="1.4.0", lifespan=lifespan)
|
||||
app.state.settings = settings
|
||||
app.state.ready = False
|
||||
app.router.redirect_slashes = False
|
||||
app.state.instance_id = uuid.uuid4().hex
|
||||
app.add_middleware(RequestContextMiddleware, instance_id=app.state.instance_id)
|
||||
|
||||
@app.exception_handler(ApiError)
|
||||
async def handle_api_error(request: Request, exc: ApiError):
|
||||
"""ApiError -> ответ в общем формате."""
|
||||
return error_response(
|
||||
request.state.request_id, exc.status, exc.code, exc.message, exc.headers
|
||||
)
|
||||
|
||||
@app.exception_handler(StarletteHTTPException)
|
||||
async def handle_http_error(request: Request, exc: StarletteHTTPException):
|
||||
"""404 и 405 -> ответ в общем формате."""
|
||||
if exc.status_code == 404:
|
||||
return error_response(
|
||||
request.state.request_id, 404, "not_found", "Объект не найден"
|
||||
)
|
||||
headers = dict(exc.headers) if exc.headers else None
|
||||
return error_response(
|
||||
request.state.request_id,
|
||||
exc.status_code,
|
||||
"invalid_request",
|
||||
"Запрос не поддерживается",
|
||||
headers,
|
||||
)
|
||||
|
||||
@app.exception_handler(RequestValidationError)
|
||||
async def handle_validation_error(request: Request, exc: RequestValidationError):
|
||||
"""Ошибки разбора параметров -> 400."""
|
||||
return error_response(
|
||||
request.state.request_id,
|
||||
400,
|
||||
"invalid_request",
|
||||
"Неверные параметры запроса",
|
||||
)
|
||||
|
||||
async def handle_db_unavailable(request: Request, exc: Exception):
|
||||
"""Недоступная БД или пул -> 503 без деталей."""
|
||||
get_logger().error(
|
||||
"db_unavailable request_id=%s type=%s",
|
||||
request.state.request_id,
|
||||
type(exc).__name__,
|
||||
)
|
||||
return error_response(
|
||||
request.state.request_id, 503, "unavailable", "Сервис временно недоступен"
|
||||
)
|
||||
|
||||
for error_type in DB_UNAVAILABLE:
|
||||
app.add_exception_handler(error_type, handle_db_unavailable)
|
||||
|
||||
app.include_router(router)
|
||||
app.include_router(api)
|
||||
app.include_router(attachment_router)
|
||||
return app
|
||||
@@ -0,0 +1,71 @@
|
||||
"""ASGI-middleware: X-Request-Id, ответ 500 при сбое и строка лога на запрос."""
|
||||
|
||||
import time
|
||||
import traceback
|
||||
import uuid
|
||||
|
||||
from starlette.datastructures import MutableHeaders
|
||||
from starlette.types import ASGIApp, Message, Receive, Scope, Send
|
||||
|
||||
from .errors import error_response
|
||||
from .logging_setup import get_logger
|
||||
|
||||
|
||||
class RequestContextMiddleware:
|
||||
"""Request id, обработка необработанных исключений, журнал запросов."""
|
||||
|
||||
def __init__(self, app: ASGIApp, instance_id: str) -> None:
|
||||
"""Сохранить следующее приложение в цепочке."""
|
||||
self.app = app
|
||||
self.instance_id = instance_id
|
||||
|
||||
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
|
||||
"""Обработать один HTTP-запрос."""
|
||||
if scope["type"] != "http":
|
||||
await self.app(scope, receive, send)
|
||||
return
|
||||
|
||||
request_id = uuid.uuid4().hex
|
||||
scope.setdefault("state", {})["request_id"] = request_id
|
||||
started = time.perf_counter()
|
||||
state = {"status": 500, "response_started": False}
|
||||
|
||||
async def send_with_request_id(message: Message) -> None:
|
||||
if message["type"] == "http.response.start":
|
||||
state["status"] = message["status"]
|
||||
state["response_started"] = True
|
||||
headers = MutableHeaders(scope=message)
|
||||
headers["X-Request-Id"] = request_id
|
||||
headers["X-Instance-Id"] = self.instance_id
|
||||
await send(message)
|
||||
|
||||
try:
|
||||
await self.app(scope, receive, send_with_request_id)
|
||||
except Exception as exc:
|
||||
|
||||
frames = traceback.extract_tb(exc.__traceback__)
|
||||
where = " <- ".join(
|
||||
"%s:%s" % (f.filename.split("/")[-1], f.lineno)
|
||||
for f in reversed(frames[-3:])
|
||||
)
|
||||
get_logger().error(
|
||||
"unhandled_error request_id=%s type=%s where=%s",
|
||||
request_id,
|
||||
type(exc).__name__,
|
||||
where,
|
||||
)
|
||||
if state["response_started"]:
|
||||
raise
|
||||
response = error_response(
|
||||
request_id, 500, "internal_error", "Внутренняя ошибка сервера"
|
||||
)
|
||||
await response(scope, receive, send_with_request_id)
|
||||
finally:
|
||||
get_logger().info(
|
||||
"request request_id=%s method=%s path=%s status=%s duration_ms=%.1f",
|
||||
request_id,
|
||||
scope.get("method"),
|
||||
scope.get("path"),
|
||||
state["status"],
|
||||
(time.perf_counter() - started) * 1000,
|
||||
)
|
||||
@@ -0,0 +1,75 @@
|
||||
"""Применение SQL-миграций из app/migrations/.
|
||||
|
||||
Применённые файлы хранятся в schema_migrations; каждый файл выполняется
|
||||
в своей транзакции под advisory lock. Запуск: python -m app.migrate
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from typing import List
|
||||
|
||||
from sqlalchemy import text
|
||||
from sqlalchemy.ext.asyncio import AsyncEngine
|
||||
|
||||
from .config import Settings
|
||||
from .db import create_engine
|
||||
|
||||
MIGRATIONS_DIR = Path(__file__).parent / "migrations"
|
||||
LOCK_ID = 727001
|
||||
|
||||
|
||||
async def run_migrations(engine: AsyncEngine) -> List[str]:
|
||||
"""Применить новые миграции и вернуть их имена."""
|
||||
applied_now: List[str] = []
|
||||
async with engine.connect() as conn:
|
||||
raw = await conn.get_raw_connection()
|
||||
pg = raw.driver_connection
|
||||
await pg.execute("SELECT pg_advisory_lock(%d)" % LOCK_ID)
|
||||
try:
|
||||
await pg.execute(
|
||||
"CREATE TABLE IF NOT EXISTS schema_migrations ("
|
||||
" version text PRIMARY KEY,"
|
||||
" applied_at timestamptz NOT NULL DEFAULT now())"
|
||||
)
|
||||
done = {
|
||||
row["version"]
|
||||
for row in await pg.fetch("SELECT version FROM schema_migrations")
|
||||
}
|
||||
for path in sorted(MIGRATIONS_DIR.glob("*.sql")):
|
||||
if path.name in done:
|
||||
continue
|
||||
async with pg.transaction():
|
||||
await pg.execute(path.read_text(encoding="utf-8"))
|
||||
await pg.execute(
|
||||
"INSERT INTO schema_migrations (version) VALUES ($1)", path.name
|
||||
)
|
||||
applied_now.append(path.name)
|
||||
finally:
|
||||
await pg.execute("SELECT pg_advisory_unlock(%d)" % LOCK_ID)
|
||||
return applied_now
|
||||
|
||||
|
||||
async def check_schema(engine: AsyncEngine) -> None:
|
||||
"""Проверить миграции без изменения схемы при старте API."""
|
||||
required = {p.name for p in MIGRATIONS_DIR.glob("*.sql")}
|
||||
async with engine.connect() as conn:
|
||||
applied = set((await conn.execute(text("SELECT version FROM schema_migrations"))).scalars())
|
||||
missing = required - applied
|
||||
if missing:
|
||||
raise RuntimeError("Сначала выполните python -m app.migrate: " + ", ".join(sorted(missing)))
|
||||
|
||||
|
||||
async def _main() -> int:
|
||||
"""Применить миграции из командной строки и напечатать результат."""
|
||||
engine = create_engine(Settings.from_env())
|
||||
try:
|
||||
applied = await run_migrations(engine)
|
||||
finally:
|
||||
await engine.dispose()
|
||||
print("Применено: %s" % (", ".join(applied) if applied else "ничего нового"))
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(asyncio.run(_main()))
|
||||
@@ -0,0 +1,41 @@
|
||||
CREATE TABLE users (
|
||||
id uuid PRIMARY KEY,
|
||||
username text NOT NULL UNIQUE
|
||||
CHECK (username ~ '^[a-z0-9_]{3,32}$'),
|
||||
display_name text NOT NULL
|
||||
CHECK (char_length(display_name) BETWEEN 1 AND 100),
|
||||
password_hash text NOT NULL,
|
||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp()
|
||||
);
|
||||
|
||||
CREATE TABLE chats (
|
||||
id uuid PRIMARY KEY,
|
||||
title text NOT NULL CHECK (char_length(title) BETWEEN 1 AND 100),
|
||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp()
|
||||
);
|
||||
CREATE INDEX chats_created_idx ON chats (created_at, id);
|
||||
|
||||
CREATE TABLE chat_members (
|
||||
chat_id uuid NOT NULL REFERENCES chats (id) ON DELETE CASCADE,
|
||||
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 TABLE messages (
|
||||
id uuid PRIMARY KEY,
|
||||
chat_id uuid NOT NULL REFERENCES chats (id) ON DELETE CASCADE,
|
||||
sender_id uuid NOT NULL REFERENCES users (id),
|
||||
text text NOT NULL CHECK (char_length(text) BETWEEN 1 AND 4000),
|
||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp()
|
||||
);
|
||||
CREATE INDEX messages_chat_created_idx ON messages (chat_id, created_at, id);
|
||||
|
||||
CREATE TABLE sessions (
|
||||
token_hash text PRIMARY KEY,
|
||||
user_id uuid NOT NULL REFERENCES users (id) ON DELETE CASCADE,
|
||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
||||
expires_at timestamptz NOT NULL
|
||||
);
|
||||
CREATE INDEX sessions_user_idx ON sessions (user_id);
|
||||
CREATE INDEX sessions_expires_idx ON sessions (expires_at);
|
||||
@@ -0,0 +1,10 @@
|
||||
CREATE TABLE message_requests (
|
||||
user_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
chat_id uuid NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
|
||||
key text NOT NULL CHECK (key ~ '^[A-Za-z0-9._:-]{1,128}$'),
|
||||
request_text text NOT NULL,
|
||||
message_id uuid NOT NULL REFERENCES messages(id) ON DELETE CASCADE,
|
||||
expires_at timestamptz NOT NULL,
|
||||
PRIMARY KEY (user_id, chat_id, key)
|
||||
);
|
||||
CREATE INDEX message_requests_expires_idx ON message_requests(expires_at);
|
||||
@@ -0,0 +1,26 @@
|
||||
CREATE TABLE attachments (
|
||||
id uuid PRIMARY KEY,
|
||||
chat_id uuid NOT NULL REFERENCES chats(id),
|
||||
owner_id uuid NOT NULL REFERENCES users(id),
|
||||
filename text NOT NULL CHECK (char_length(filename) BETWEEN 1 AND 255),
|
||||
object_key text NOT NULL UNIQUE,
|
||||
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 text NOT NULL CHECK (sha256 ~ '^[0-9a-f]{64}$'),
|
||||
width integer NOT NULL CHECK (width > 0),
|
||||
height integer NOT NULL CHECK (height > 0),
|
||||
status text NOT NULL CHECK (status IN ('uploading', 'ready')),
|
||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
||||
updated_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
||||
CHECK (width::bigint * height <= 40000000)
|
||||
);
|
||||
CREATE INDEX attachments_chat_idx ON attachments(chat_id, id);
|
||||
CREATE INDEX attachments_pending_idx ON attachments(created_at) WHERE status = 'uploading';
|
||||
|
||||
ALTER TABLE messages ADD COLUMN attachment_ids uuid[] NOT NULL DEFAULT '{}';
|
||||
ALTER TABLE messages DROP CONSTRAINT messages_text_check;
|
||||
ALTER TABLE messages ADD CONSTRAINT messages_content_check CHECK (
|
||||
char_length(text) <= 4000 AND cardinality(attachment_ids) <= 10
|
||||
AND (char_length(btrim(text, E' \t\r\n')) > 0 OR cardinality(attachment_ids) > 0)
|
||||
);
|
||||
ALTER TABLE message_requests ADD COLUMN request_attachment_ids uuid[] NOT NULL DEFAULT '{}';
|
||||
@@ -0,0 +1,2 @@
|
||||
-- Compatible with an already bootstrapped attachment table.
|
||||
ALTER TABLE attachments ADD COLUMN IF NOT EXISTS updated_at timestamptz NOT NULL DEFAULT clock_timestamp();
|
||||
+104
@@ -0,0 +1,104 @@
|
||||
"""ORM-модели таблиц; схема и ограничения задаются миграциями."""
|
||||
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
|
||||
from sqlalchemy import DateTime, func
|
||||
from sqlalchemy.dialects.postgresql import ARRAY, UUID
|
||||
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
|
||||
|
||||
NOW = func.clock_timestamp()
|
||||
|
||||
|
||||
class Base(DeclarativeBase):
|
||||
"""Базовый класс моделей."""
|
||||
|
||||
|
||||
class User(Base):
|
||||
"""Пользователь; пароль хранится только как хеш."""
|
||||
|
||||
__tablename__ = "users"
|
||||
id: Mapped[uuid.UUID] = mapped_column(primary_key=True)
|
||||
username: Mapped[str]
|
||||
display_name: Mapped[str]
|
||||
password_hash: Mapped[str]
|
||||
created_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True), server_default=NOW
|
||||
)
|
||||
|
||||
|
||||
class Chat(Base):
|
||||
"""Чат."""
|
||||
|
||||
__tablename__ = "chats"
|
||||
id: Mapped[uuid.UUID] = mapped_column(primary_key=True)
|
||||
title: Mapped[str]
|
||||
created_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True), server_default=NOW
|
||||
)
|
||||
|
||||
|
||||
class ChatMember(Base):
|
||||
"""Участие пользователя в чате."""
|
||||
|
||||
__tablename__ = "chat_members"
|
||||
chat_id: Mapped[uuid.UUID] = mapped_column(primary_key=True)
|
||||
user_id: Mapped[uuid.UUID] = mapped_column(primary_key=True)
|
||||
|
||||
|
||||
class Message(Base):
|
||||
"""Сообщение чата."""
|
||||
|
||||
__tablename__ = "messages"
|
||||
id: Mapped[uuid.UUID] = mapped_column(primary_key=True)
|
||||
chat_id: Mapped[uuid.UUID]
|
||||
sender_id: Mapped[uuid.UUID]
|
||||
text: Mapped[str]
|
||||
attachment_ids: Mapped[list[uuid.UUID]] = mapped_column(ARRAY(UUID(as_uuid=True)), default=list)
|
||||
created_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True), server_default=NOW
|
||||
)
|
||||
|
||||
|
||||
class SessionRow(Base):
|
||||
"""Сессия входа; хранится SHA-256 токена."""
|
||||
|
||||
__tablename__ = "sessions"
|
||||
token_hash: Mapped[str] = mapped_column(primary_key=True)
|
||||
user_id: Mapped[uuid.UUID]
|
||||
created_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True), server_default=NOW
|
||||
)
|
||||
expires_at: Mapped[datetime] = mapped_column(DateTime(timezone=True))
|
||||
|
||||
|
||||
class MessageRequest(Base):
|
||||
"""Общий результат POST сообщения; область ключа — пользователь и чат."""
|
||||
|
||||
__tablename__ = "message_requests"
|
||||
user_id: Mapped[uuid.UUID] = mapped_column(primary_key=True)
|
||||
chat_id: Mapped[uuid.UUID] = mapped_column(primary_key=True)
|
||||
key: Mapped[str] = mapped_column(primary_key=True)
|
||||
request_text: Mapped[str]
|
||||
request_attachment_ids: Mapped[list[uuid.UUID]] = mapped_column(ARRAY(UUID(as_uuid=True)), default=list)
|
||||
message_id: Mapped[uuid.UUID]
|
||||
expires_at: Mapped[datetime] = mapped_column(DateTime(timezone=True))
|
||||
|
||||
|
||||
class Attachment(Base):
|
||||
"""Метаданные и устойчивое намерение upload; бинарные данные только в S3."""
|
||||
|
||||
__tablename__ = "attachments"
|
||||
id: Mapped[uuid.UUID] = mapped_column(primary_key=True)
|
||||
chat_id: Mapped[uuid.UUID]
|
||||
owner_id: Mapped[uuid.UUID]
|
||||
filename: Mapped[str]
|
||||
object_key: Mapped[str]
|
||||
content_type: Mapped[str]
|
||||
size_bytes: Mapped[int]
|
||||
sha256: Mapped[str]
|
||||
width: Mapped[int]
|
||||
height: Mapped[int]
|
||||
status: Mapped[str]
|
||||
updated_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=NOW)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=NOW)
|
||||
+306
@@ -0,0 +1,306 @@
|
||||
"""HTTP-эндпоинты API.
|
||||
|
||||
Порядок проверок: аутентификация (401), доступ к чату (404), тело запроса (400).
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import uuid
|
||||
import anyio
|
||||
from typing import List
|
||||
|
||||
from fastapi import APIRouter, Depends, Request, Response
|
||||
from sqlalchemy import text
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from . import crud
|
||||
from .auth import (
|
||||
BASIC_CHALLENGE,
|
||||
current_user,
|
||||
get_session,
|
||||
parse_basic,
|
||||
parse_bearer,
|
||||
unauthorized,
|
||||
)
|
||||
from .cursor import encode_cursor
|
||||
from .errors import ApiError
|
||||
from .http_utils import (
|
||||
parse_cursor,
|
||||
parse_idempotency_key,
|
||||
parse_limit,
|
||||
parse_uuid,
|
||||
read_json_object,
|
||||
validate_body,
|
||||
)
|
||||
from .models import Chat, Message, User
|
||||
from .schemas import (
|
||||
ChatOut,
|
||||
ChatPage,
|
||||
CreateChatIn,
|
||||
CreateMessageIn,
|
||||
CreateUserIn,
|
||||
MessageOut,
|
||||
MessagePage,
|
||||
SessionOut,
|
||||
UserOut,
|
||||
)
|
||||
from .security import (
|
||||
DUMMY_HASH,
|
||||
hash_password_async,
|
||||
hash_token,
|
||||
new_token,
|
||||
verify_password_async,
|
||||
)
|
||||
from .timeutil import format_ts, from_micros, to_micros
|
||||
from .storage import StorageUnavailable, unavailable
|
||||
|
||||
router = APIRouter()
|
||||
api = APIRouter(prefix="/api/v1")
|
||||
|
||||
|
||||
def body_doc(model: type) -> dict:
|
||||
"""Описание тела запроса для OpenAPI-документации."""
|
||||
return {
|
||||
"requestBody": {
|
||||
"required": True,
|
||||
"content": {"application/json": {"schema": model.model_json_schema()}},
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
def user_out(user: User) -> dict:
|
||||
"""User -> словарь ответа."""
|
||||
return {
|
||||
"id": str(user.id),
|
||||
"username": user.username,
|
||||
"display_name": user.display_name,
|
||||
"created_at": format_ts(user.created_at),
|
||||
}
|
||||
|
||||
|
||||
def chat_out(chat: Chat, member_ids: List[uuid.UUID]) -> dict:
|
||||
"""Chat и участники -> словарь ответа."""
|
||||
return {
|
||||
"id": str(chat.id),
|
||||
"title": chat.title,
|
||||
"member_ids": [str(member_id) for member_id in member_ids],
|
||||
"created_at": format_ts(chat.created_at),
|
||||
}
|
||||
|
||||
|
||||
def message_out(message: Message) -> dict:
|
||||
"""Message -> словарь ответа."""
|
||||
return {
|
||||
"id": str(message.id),
|
||||
"chat_id": str(message.chat_id),
|
||||
"sender_id": str(message.sender_id),
|
||||
"text": message.text,
|
||||
"attachment_ids": [str(ident) for ident in message.attachment_ids],
|
||||
"created_at": format_ts(message.created_at),
|
||||
}
|
||||
|
||||
|
||||
def position(key):
|
||||
"""Позицию из курсора перевести в (datetime, UUID)."""
|
||||
if key is None:
|
||||
return None
|
||||
return from_micros(key[0]), uuid.UUID(key[1])
|
||||
|
||||
|
||||
def cursor_for(scope: str, row) -> str:
|
||||
"""Курсор, указывающий на строку row."""
|
||||
return encode_cursor(scope, (to_micros(row.created_at), str(row.id)))
|
||||
|
||||
|
||||
async def open_chat(session: AsyncSession, user: User, chat_id_text: str) -> Chat:
|
||||
"""Чат, доступный пользователю; неверный id -> 400, чужой или несуществующий -> 404."""
|
||||
chat_id = uuid.UUID(parse_uuid(chat_id_text, "chat_id"))
|
||||
chat = await crud.get_chat_for_member(session, chat_id, user.id)
|
||||
if chat is None:
|
||||
raise ApiError(404, "not_found", "Объект не найден")
|
||||
return chat
|
||||
|
||||
|
||||
@router.get("/health/live")
|
||||
async def live() -> dict:
|
||||
"""Процесс жив."""
|
||||
return {"status": "ok"}
|
||||
|
||||
|
||||
@router.get("/health/ready")
|
||||
async def ready(request: Request):
|
||||
"""Готовность: схема, PostgreSQL и приватный S3 bucket доступны."""
|
||||
if not getattr(request.app.state, "ready", False):
|
||||
raise ApiError(503, "unavailable", "Сервис не готов")
|
||||
try:
|
||||
async with request.app.state.sessionmaker() as session:
|
||||
await asyncio.wait_for(session.execute(text("SELECT 1")), timeout=2)
|
||||
except Exception:
|
||||
raise ApiError(503, "unavailable", "База данных недоступна")
|
||||
if request.app.state.storage is not None:
|
||||
try:
|
||||
await anyio.to_thread.run_sync(request.app.state.storage.ready)
|
||||
except StorageUnavailable:
|
||||
raise unavailable() from None
|
||||
return {"status": "ok"}
|
||||
|
||||
|
||||
@api.post(
|
||||
"/users",
|
||||
status_code=201,
|
||||
response_model=UserOut,
|
||||
openapi_extra=body_doc(CreateUserIn),
|
||||
)
|
||||
async def register_user(request: Request, session: AsyncSession = Depends(get_session)):
|
||||
"""Регистрация пользователя."""
|
||||
body = validate_body(CreateUserIn, await read_json_object(request))
|
||||
password_hash = await hash_password_async(body.password)
|
||||
try:
|
||||
user = await crud.create_user(
|
||||
session, body.username, body.display_name, password_hash
|
||||
)
|
||||
except crud.UsernameTaken:
|
||||
raise ApiError(409, "conflict", "Такое имя пользователя уже занято")
|
||||
return user_out(user)
|
||||
|
||||
|
||||
@api.post("/auth/sessions", status_code=201, response_model=SessionOut)
|
||||
async def login(request: Request, session: AsyncSession = Depends(get_session)):
|
||||
"""Вход по Basic: создать сессию и вернуть Bearer-токен."""
|
||||
username, password = parse_basic(request)
|
||||
user = await crud.get_user_by_username(session, username)
|
||||
password_ok = await verify_password_async(
|
||||
password, user.password_hash if user else DUMMY_HASH
|
||||
)
|
||||
if user is None or not password_ok:
|
||||
raise unauthorized(BASIC_CHALLENGE)
|
||||
token = new_token()
|
||||
expires_at = await crud.create_session(
|
||||
session,
|
||||
user.id,
|
||||
hash_token(token),
|
||||
request.app.state.settings.session_ttl_seconds,
|
||||
)
|
||||
return {
|
||||
"token": token,
|
||||
"token_type": "Bearer",
|
||||
"expires_at": format_ts(expires_at),
|
||||
"user": user_out(user),
|
||||
}
|
||||
|
||||
|
||||
@api.delete("/auth/sessions/current", status_code=204)
|
||||
async def logout(
|
||||
request: Request,
|
||||
user: User = Depends(current_user),
|
||||
session: AsyncSession = Depends(get_session),
|
||||
):
|
||||
"""Отозвать текущую сессию."""
|
||||
token_hash = hash_token(parse_bearer(request))
|
||||
await crud.delete_session(session, token_hash)
|
||||
await request.app.state.cache.revoke(token_hash)
|
||||
return Response(status_code=204)
|
||||
|
||||
|
||||
@api.get("/users/me", response_model=UserOut)
|
||||
async def read_me(user: User = Depends(current_user)):
|
||||
"""Текущий пользователь."""
|
||||
return user_out(user)
|
||||
|
||||
|
||||
@api.post(
|
||||
"/chats",
|
||||
status_code=201,
|
||||
response_model=ChatOut,
|
||||
openapi_extra=body_doc(CreateChatIn),
|
||||
)
|
||||
async def create_chat(
|
||||
request: Request,
|
||||
user: User = Depends(current_user),
|
||||
session: AsyncSession = Depends(get_session),
|
||||
):
|
||||
"""Создать чат с участниками."""
|
||||
body = validate_body(CreateChatIn, await read_json_object(request))
|
||||
member_ids = [uuid.UUID(item) for item in body.member_ids]
|
||||
if user.id not in member_ids:
|
||||
raise ApiError(400, "invalid_request", "member_ids должен включать вас самих")
|
||||
try:
|
||||
chat, members = await crud.create_chat(session, body.title, member_ids)
|
||||
except crud.UnknownMember:
|
||||
raise ApiError(
|
||||
400, "invalid_request", "member_ids содержит несуществующего пользователя"
|
||||
)
|
||||
return chat_out(chat, sorted(members))
|
||||
|
||||
|
||||
@api.get("/chats", response_model=ChatPage)
|
||||
async def list_chats(
|
||||
request: Request,
|
||||
user: User = Depends(current_user),
|
||||
session: AsyncSession = Depends(get_session),
|
||||
):
|
||||
"""Страница чатов текущего пользователя."""
|
||||
limit = parse_limit(request.query_params.get("limit"))
|
||||
scope = "chats:" + str(user.id)
|
||||
after = position(parse_cursor(request.query_params.get("cursor"), scope))
|
||||
chats, has_more = await crud.list_chats(session, user.id, after, limit)
|
||||
members = await crud.get_member_ids(session, [chat.id for chat in chats])
|
||||
next_cursor = cursor_for(scope, chats[-1]) if has_more and chats else None
|
||||
return {
|
||||
"items": [chat_out(c, members[c.id]) for c in chats],
|
||||
"next_cursor": next_cursor,
|
||||
}
|
||||
|
||||
|
||||
@api.get("/chats/{chat_id}", response_model=ChatOut)
|
||||
async def read_chat(
|
||||
chat_id: str,
|
||||
user: User = Depends(current_user),
|
||||
session: AsyncSession = Depends(get_session),
|
||||
):
|
||||
"""Один чат по id."""
|
||||
chat = await open_chat(session, user, chat_id)
|
||||
members = await crud.get_member_ids(session, [chat.id])
|
||||
return chat_out(chat, members[chat.id])
|
||||
|
||||
|
||||
@api.post(
|
||||
"/chats/{chat_id}/messages",
|
||||
status_code=201,
|
||||
response_model=MessageOut,
|
||||
openapi_extra=body_doc(CreateMessageIn),
|
||||
)
|
||||
async def create_message(
|
||||
chat_id: str,
|
||||
request: Request,
|
||||
user: User = Depends(current_user),
|
||||
session: AsyncSession = Depends(get_session),
|
||||
):
|
||||
"""Отправить сообщение в чат."""
|
||||
chat = await open_chat(session, user, chat_id)
|
||||
body = validate_body(CreateMessageIn, await read_json_object(request))
|
||||
key = parse_idempotency_key(request)
|
||||
try:
|
||||
message = await crud.create_message_idempotent(
|
||||
session, chat.id, user.id, body.text, key,
|
||||
[uuid.UUID(value) for value in body.attachment_ids],
|
||||
)
|
||||
except crud.IdempotencyConflict:
|
||||
raise ApiError(409, "conflict", "Idempotency-Key уже использован с другим сообщением")
|
||||
return message_out(message)
|
||||
|
||||
|
||||
@api.get("/chats/{chat_id}/messages", response_model=MessagePage)
|
||||
async def list_messages(
|
||||
chat_id: str,
|
||||
request: Request,
|
||||
user: User = Depends(current_user),
|
||||
session: AsyncSession = Depends(get_session),
|
||||
):
|
||||
"""Страница истории чата."""
|
||||
chat = await open_chat(session, user, chat_id)
|
||||
limit = parse_limit(request.query_params.get("limit"))
|
||||
scope = "messages:" + str(chat.id)
|
||||
after = position(parse_cursor(request.query_params.get("cursor"), scope))
|
||||
messages, has_more = await crud.list_messages(session, chat.id, after, limit)
|
||||
next_cursor = cursor_for(scope, messages[-1]) if has_more and messages else None
|
||||
return {"items": [message_out(m) for m in messages], "next_cursor": next_cursor}
|
||||
+188
@@ -0,0 +1,188 @@
|
||||
"""Pydantic-модели тел запросов и ответов."""
|
||||
|
||||
import re
|
||||
from typing import List, Optional
|
||||
|
||||
from pydantic import BaseModel, ConfigDict, Field, field_validator, model_validator
|
||||
|
||||
from .cursor import UUID_RE
|
||||
|
||||
USERNAME_RE = re.compile(r"[a-z0-9_]{3,32}")
|
||||
|
||||
|
||||
def _check_text(value: str, name: str, max_len: int, allow_blank: bool = False) -> str:
|
||||
"""Проверить строку: тип, длина в символах, не только пробелы, без NUL."""
|
||||
try:
|
||||
value.encode("utf-8")
|
||||
except UnicodeEncodeError:
|
||||
raise ValueError("%s содержит недопустимые символы" % name)
|
||||
if "\x00" in value:
|
||||
raise ValueError("%s не может содержать символ NUL" % name)
|
||||
if not allow_blank and value.strip() == "":
|
||||
raise ValueError("%s не может быть пустым или состоять из пробелов" % name)
|
||||
if len(value) > max_len:
|
||||
raise ValueError("%s длиннее %d символов" % (name, max_len))
|
||||
return value
|
||||
|
||||
|
||||
class _Strict(BaseModel):
|
||||
"""База моделей запроса: лишние поля и приведение типов запрещены."""
|
||||
|
||||
model_config = ConfigDict(extra="forbid", strict=True)
|
||||
|
||||
|
||||
class CreateUserIn(_Strict):
|
||||
"""Тело регистрации."""
|
||||
|
||||
username: str
|
||||
display_name: str
|
||||
password: str
|
||||
|
||||
@field_validator("username")
|
||||
@classmethod
|
||||
def _username(cls, value: str) -> str:
|
||||
"""username: [a-z0-9_]{3,32}."""
|
||||
if not USERNAME_RE.fullmatch(value):
|
||||
raise ValueError("username: только a-z, 0-9 и _, длина 3-32")
|
||||
return value
|
||||
|
||||
@field_validator("display_name")
|
||||
@classmethod
|
||||
def _display_name(cls, value: str) -> str:
|
||||
"""display_name: 1-100 символов."""
|
||||
return _check_text(value, "display_name", 100)
|
||||
|
||||
@field_validator("password")
|
||||
@classmethod
|
||||
def _password(cls, value: str) -> str:
|
||||
"""password: 12-128 символов, без обрезки пробелов."""
|
||||
_check_text(value, "password", 128, allow_blank=True)
|
||||
if len(value) < 12:
|
||||
raise ValueError("password короче 12 символов")
|
||||
return value
|
||||
|
||||
|
||||
class CreateChatIn(_Strict):
|
||||
"""Тело создания чата."""
|
||||
|
||||
title: str
|
||||
member_ids: List[str]
|
||||
|
||||
@field_validator("title")
|
||||
@classmethod
|
||||
def _title(cls, value: str) -> str:
|
||||
"""title: 1-100 символов."""
|
||||
return _check_text(value, "title", 100)
|
||||
|
||||
@field_validator("member_ids")
|
||||
@classmethod
|
||||
def _members(cls, value: List[str]) -> List[str]:
|
||||
"""member_ids: 1-100 уникальных UUID."""
|
||||
if not 1 <= len(value) <= 100:
|
||||
raise ValueError("member_ids: от 1 до 100 участников")
|
||||
normalized = []
|
||||
for item in value:
|
||||
if not UUID_RE.fullmatch(item):
|
||||
raise ValueError("member_ids: каждый элемент должен быть UUID")
|
||||
normalized.append(item.lower())
|
||||
if len(set(normalized)) != len(normalized):
|
||||
raise ValueError("member_ids: дубликаты не допускаются")
|
||||
return normalized
|
||||
|
||||
|
||||
class CreateMessageIn(_Strict):
|
||||
"""Тело сообщения; отправитель определяется по токену."""
|
||||
|
||||
text: str
|
||||
attachment_ids: List[str] = Field(default_factory=list)
|
||||
|
||||
@field_validator("attachment_ids")
|
||||
@classmethod
|
||||
def _attachments(cls, value: List[str]) -> List[str]:
|
||||
"""До десяти уникальных UUID; порядок вложений сохраняется."""
|
||||
if len(value) > 10:
|
||||
raise ValueError("Не более 10 вложений")
|
||||
if any(not UUID_RE.fullmatch(item) for item in value):
|
||||
raise ValueError("attachment_ids: каждый элемент должен быть UUID")
|
||||
normalized = [item.lower() for item in value]
|
||||
if len(set(normalized)) != len(normalized):
|
||||
raise ValueError("attachment_ids: дубликаты не допускаются")
|
||||
return normalized
|
||||
|
||||
@field_validator("text")
|
||||
@classmethod
|
||||
def _text(cls, value: str) -> str:
|
||||
return _check_text(value, "text", 4000, allow_blank=True)
|
||||
|
||||
@model_validator(mode="after")
|
||||
def _not_empty(self):
|
||||
if not self.text.strip() and not self.attachment_ids:
|
||||
raise ValueError("Нужен текст или хотя бы одно вложение")
|
||||
return self
|
||||
|
||||
|
||||
class UserOut(BaseModel):
|
||||
"""Пользователь в ответе."""
|
||||
|
||||
id: str
|
||||
username: str
|
||||
display_name: str
|
||||
created_at: str
|
||||
|
||||
|
||||
class SessionOut(BaseModel):
|
||||
"""Ответ на вход."""
|
||||
|
||||
token: str
|
||||
token_type: str
|
||||
expires_at: str
|
||||
user: UserOut
|
||||
|
||||
|
||||
class ChatOut(BaseModel):
|
||||
"""Чат в ответе."""
|
||||
|
||||
id: str
|
||||
title: str
|
||||
member_ids: List[str]
|
||||
created_at: str
|
||||
|
||||
|
||||
class MessageOut(BaseModel):
|
||||
"""Сообщение в ответе."""
|
||||
|
||||
id: str
|
||||
chat_id: str
|
||||
sender_id: str
|
||||
text: str
|
||||
attachment_ids: List[str]
|
||||
created_at: str
|
||||
|
||||
|
||||
class ChatPage(BaseModel):
|
||||
"""Страница списка чатов."""
|
||||
|
||||
items: List[ChatOut]
|
||||
next_cursor: Optional[str]
|
||||
|
||||
|
||||
class MessagePage(BaseModel):
|
||||
"""Страница истории сообщений."""
|
||||
|
||||
items: List[MessageOut]
|
||||
next_cursor: Optional[str]
|
||||
|
||||
|
||||
class AttachmentOut(BaseModel):
|
||||
id: str
|
||||
chat_id: str
|
||||
owner_id: str
|
||||
filename: str
|
||||
content_type: str
|
||||
size_bytes: int
|
||||
sha256: str
|
||||
status: str
|
||||
variants: list
|
||||
error_code: Optional[str]
|
||||
created_at: str
|
||||
updated_at: str
|
||||
@@ -0,0 +1,74 @@
|
||||
"""Хеширование паролей (scrypt) и токены сессий."""
|
||||
|
||||
import asyncio
|
||||
import base64
|
||||
import hashlib
|
||||
import hmac
|
||||
import os
|
||||
import secrets
|
||||
|
||||
SCRYPT_N = 2**14
|
||||
SCRYPT_R = 8
|
||||
SCRYPT_P = 1
|
||||
SCRYPT_LEN = 32
|
||||
|
||||
|
||||
def hash_password(password: str) -> str:
|
||||
"""Хеш пароля scrypt со случайной солью в формате scrypt$N$r$p$соль$хеш."""
|
||||
salt = os.urandom(16)
|
||||
digest = hashlib.scrypt(
|
||||
password.encode("utf-8"),
|
||||
salt=salt,
|
||||
n=SCRYPT_N,
|
||||
r=SCRYPT_R,
|
||||
p=SCRYPT_P,
|
||||
dklen=SCRYPT_LEN,
|
||||
)
|
||||
return "scrypt$%d$%d$%d$%s$%s" % (
|
||||
SCRYPT_N,
|
||||
SCRYPT_R,
|
||||
SCRYPT_P,
|
||||
base64.b64encode(salt).decode(),
|
||||
base64.b64encode(digest).decode(),
|
||||
)
|
||||
|
||||
|
||||
def verify_password(password: str, stored: str) -> bool:
|
||||
"""Проверить пароль по сохранённому хешу за постоянное время."""
|
||||
try:
|
||||
_, n, r, p, salt_b64, hash_b64 = stored.split("$")
|
||||
expected = base64.b64decode(hash_b64)
|
||||
actual = hashlib.scrypt(
|
||||
password.encode("utf-8"),
|
||||
salt=base64.b64decode(salt_b64),
|
||||
n=int(n),
|
||||
r=int(r),
|
||||
p=int(p),
|
||||
dklen=len(expected),
|
||||
)
|
||||
except (ValueError, TypeError):
|
||||
return False
|
||||
return hmac.compare_digest(actual, expected)
|
||||
|
||||
|
||||
DUMMY_HASH = hash_password("dummy-password-for-timing-only")
|
||||
|
||||
|
||||
async def hash_password_async(password: str) -> str:
|
||||
"""hash_password в отдельном потоке."""
|
||||
return await asyncio.to_thread(hash_password, password)
|
||||
|
||||
|
||||
async def verify_password_async(password: str, stored: str) -> bool:
|
||||
"""verify_password в отдельном потоке."""
|
||||
return await asyncio.to_thread(verify_password, password, stored)
|
||||
|
||||
|
||||
def new_token() -> str:
|
||||
"""Случайный токен сессии (256 бит)."""
|
||||
return secrets.token_urlsafe(32)
|
||||
|
||||
|
||||
def hash_token(token: str) -> str:
|
||||
"""SHA-256 токена в hex."""
|
||||
return hashlib.sha256(token.encode("utf-8")).hexdigest()
|
||||
@@ -0,0 +1,85 @@
|
||||
"""S3 client: one bounded PUT, no implicit write retries, streaming GET."""
|
||||
|
||||
from urllib.parse import urlsplit
|
||||
|
||||
from minio import Minio
|
||||
from minio.error import MinioException
|
||||
import urllib3
|
||||
|
||||
from .errors import ApiError
|
||||
|
||||
STORAGE_ERRORS = (MinioException, urllib3.exceptions.HTTPError, OSError)
|
||||
MAX_BYTES = 10 * 1024 * 1024
|
||||
|
||||
|
||||
class StorageUnavailable(Exception):
|
||||
"""Storage error without sensitive endpoint/credentials in its message."""
|
||||
|
||||
|
||||
class MinioStorage:
|
||||
def __init__(self, settings):
|
||||
endpoint = urlsplit(settings.s3_endpoint_url)
|
||||
if endpoint.scheme not in ("http", "https") or not endpoint.netloc:
|
||||
raise ValueError("S3_ENDPOINT_URL must be an http(s) endpoint")
|
||||
if endpoint.path not in ("", "/") or endpoint.username or endpoint.password:
|
||||
raise ValueError("S3 endpoint must not contain a path or credentials")
|
||||
if not settings.s3_access_key or not settings.s3_secret_key:
|
||||
raise ValueError("S3 credentials are required")
|
||||
self.bucket = settings.s3_bucket
|
||||
self.http = urllib3.PoolManager(
|
||||
num_pools=2, maxsize=8, block=False,
|
||||
timeout=urllib3.Timeout(connect=3, read=10), retries=False,
|
||||
)
|
||||
# Readiness has its own short deadline, so S3 outage cannot accumulate probes.
|
||||
self.probe_http = urllib3.PoolManager(
|
||||
num_pools=1, maxsize=2, timeout=urllib3.Timeout(connect=0.5, read=0.5), retries=False,
|
||||
)
|
||||
options = dict(
|
||||
endpoint=endpoint.netloc, access_key=settings.s3_access_key,
|
||||
secret_key=settings.s3_secret_key, secure=endpoint.scheme == "https",
|
||||
region=settings.s3_region,
|
||||
)
|
||||
self.client = Minio(**options, http_client=self.http)
|
||||
self.probe = Minio(**options, http_client=self.probe_http)
|
||||
|
||||
def put(self, key, stream, size, content_type):
|
||||
try:
|
||||
stream.seek(0)
|
||||
# All accepted files fit ONE part: no abandoned multipart upload state.
|
||||
self.client.put_object(self.bucket, key, stream, size, content_type,
|
||||
part_size=MAX_BYTES, num_parallel_uploads=1)
|
||||
except STORAGE_ERRORS:
|
||||
raise StorageUnavailable() from None
|
||||
|
||||
def get(self, key):
|
||||
try:
|
||||
return self.client.get_object(self.bucket, key)
|
||||
except STORAGE_ERRORS:
|
||||
raise StorageUnavailable() from None
|
||||
|
||||
def stat(self, key):
|
||||
try:
|
||||
return self.client.stat_object(self.bucket, key)
|
||||
except STORAGE_ERRORS:
|
||||
raise StorageUnavailable() from None
|
||||
|
||||
def delete(self, key):
|
||||
try:
|
||||
self.client.remove_object(self.bucket, key)
|
||||
except STORAGE_ERRORS:
|
||||
raise StorageUnavailable() from None
|
||||
|
||||
def ready(self):
|
||||
try:
|
||||
if not self.probe.bucket_exists(self.bucket):
|
||||
raise StorageUnavailable()
|
||||
except STORAGE_ERRORS:
|
||||
raise StorageUnavailable() from None
|
||||
|
||||
def close(self):
|
||||
self.http.clear()
|
||||
self.probe_http.clear()
|
||||
|
||||
|
||||
def unavailable():
|
||||
return ApiError(503, "unavailable", "Хранилище временно недоступно")
|
||||
@@ -0,0 +1,23 @@
|
||||
"""Преобразования времени: RFC 3339 UTC и микросекунды."""
|
||||
|
||||
from datetime import datetime, timedelta, timezone
|
||||
|
||||
EPOCH = datetime(1970, 1, 1, tzinfo=timezone.utc)
|
||||
|
||||
|
||||
def format_ts(moment: datetime) -> str:
|
||||
"""datetime -> строка RFC 3339 в UTC с суффиксом Z."""
|
||||
return moment.astimezone(timezone.utc).strftime("%Y-%m-%dT%H:%M:%S.%fZ")
|
||||
|
||||
|
||||
def to_micros(moment: datetime) -> int:
|
||||
"""datetime -> микросекунды от эпохи."""
|
||||
return (moment - EPOCH) // timedelta(microseconds=1)
|
||||
|
||||
|
||||
MAX_TIMESTAMP_MICROS = to_micros(datetime.max.replace(tzinfo=timezone.utc))
|
||||
|
||||
|
||||
def from_micros(micros: int) -> datetime:
|
||||
"""Микросекунды от эпохи -> datetime в UTC."""
|
||||
return EPOCH + timedelta(microseconds=micros)
|
||||
@@ -1,12 +1,8 @@
|
||||
# Topology worksheet, deliberately NOT a runnable stack (no application supplied).
|
||||
# Implement compose.yaml or stack.yaml; do not submit this empty file as deployment.
|
||||
# Service names below are logical roles, not imposed container names.
|
||||
# Учебный список ролей; запускаемый стенд находится в compose.yaml.
|
||||
x-lab-number: 4
|
||||
x-required-roles:
|
||||
- api
|
||||
- postgres
|
||||
- load_balancer
|
||||
- s3
|
||||
# TODO: implement real images/builds, network wiring, volumes, healthchecks,
|
||||
# credentials and bootstrap. Details: contracts/infrastructure.json and README.md.
|
||||
services: {}
|
||||
|
||||
+218
@@ -0,0 +1,218 @@
|
||||
name: mtusi-messenger-lab4
|
||||
|
||||
x-s3-env: &s3-env
|
||||
S3_ENDPOINT_URL: http://minio:9000
|
||||
S3_REGION: us-east-1
|
||||
S3_BUCKET: ${S3_BUCKET:-attachments}
|
||||
S3_ACCESS_KEY: ${S3_ACCESS_KEY:?run python scripts/init_env.py}
|
||||
S3_SECRET_KEY: ${S3_SECRET_KEY:?run python scripts/init_env.py}
|
||||
|
||||
x-api-env: &api-env
|
||||
<<: *s3-env
|
||||
APP_HOST: 0.0.0.0
|
||||
APP_PORT: "8080"
|
||||
APP_ENV: lab4
|
||||
LOG_LEVEL: ${LOG_LEVEL:-INFO}
|
||||
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600}
|
||||
SESSION_STORE: postgres
|
||||
DATABASE_URL: postgresql+asyncpg://${POSTGRES_USER:?set POSTGRES_USER}:${POSTGRES_PASSWORD:?set POSTGRES_PASSWORD}@db:5432/${POSTGRES_DB:?set POSTGRES_DB}
|
||||
CACHE_URL: ${CACHE_URL-redis://redis:6379/0}
|
||||
CACHE_TTL_SECONDS: "60"
|
||||
CACHE_TIMEOUT_MS: "300"
|
||||
CACHE_RETRY_SECONDS: "5"
|
||||
DB_POOL_SIZE: "5"
|
||||
DB_MAX_OVERFLOW: "5"
|
||||
DB_POOL_TIMEOUT: "1"
|
||||
CLEANUP_GRACE_SECONDS: "3600"
|
||||
CLEANUP_INTERVAL_SECONDS: "60"
|
||||
|
||||
x-api: &api
|
||||
build: .
|
||||
image: ${API_IMAGE:-mtusi-messenger:lab4}
|
||||
environment: *api-env
|
||||
networks: [edge, data]
|
||||
expose: ["8080"]
|
||||
cpus: 0.50
|
||||
mem_limit: 512m
|
||||
stop_signal: SIGTERM
|
||||
stop_grace_period: 10s
|
||||
healthcheck:
|
||||
test: ["CMD", "python", "-m", "app.healthcheck"]
|
||||
interval: 2s
|
||||
timeout: 3s
|
||||
start_period: 5s
|
||||
retries: 2
|
||||
depends_on:
|
||||
migrate:
|
||||
condition: service_completed_successfully
|
||||
minio-init:
|
||||
condition: service_completed_successfully
|
||||
restart: unless-stopped
|
||||
|
||||
services:
|
||||
db:
|
||||
image: postgres:16.6-bookworm
|
||||
environment:
|
||||
POSTGRES_USER: ${POSTGRES_USER:?set POSTGRES_USER}
|
||||
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?set POSTGRES_PASSWORD}
|
||||
POSTGRES_DB: ${POSTGRES_DB:?set POSTGRES_DB}
|
||||
networks: [data]
|
||||
volumes:
|
||||
- pgdata:/var/lib/postgresql/data
|
||||
cpus: 1.0
|
||||
mem_limit: 512m
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "pg_isready -U $${POSTGRES_USER} -d $${POSTGRES_DB}"]
|
||||
interval: 2s
|
||||
timeout: 2s
|
||||
retries: 15
|
||||
restart: unless-stopped
|
||||
|
||||
redis:
|
||||
image: redis:7.4.1-bookworm
|
||||
command: ["redis-server", "--save", "", "--appendonly", "no", "--maxmemory", "64mb", "--maxmemory-policy", "noeviction"]
|
||||
networks: [data]
|
||||
cpus: 0.25
|
||||
mem_limit: 128m
|
||||
healthcheck:
|
||||
test: ["CMD", "redis-cli", "ping"]
|
||||
interval: 2s
|
||||
timeout: 1s
|
||||
retries: 5
|
||||
restart: unless-stopped
|
||||
|
||||
migrate:
|
||||
build: .
|
||||
image: ${API_IMAGE:-mtusi-messenger:lab4}
|
||||
command: ["python", "-m", "app.migrate"]
|
||||
environment: *api-env
|
||||
networks: [data]
|
||||
cpus: 0.50
|
||||
mem_limit: 256m
|
||||
depends_on:
|
||||
db:
|
||||
condition: service_healthy
|
||||
restart: "no"
|
||||
|
||||
minio:
|
||||
build:
|
||||
context: .
|
||||
dockerfile: infra/minio/Dockerfile
|
||||
target: server
|
||||
image: mtusi-minio:RELEASE.2025-10-15T17-29-55Z
|
||||
environment:
|
||||
MINIO_ROOT_USER: ${MINIO_ROOT_USER:?run python scripts/init_env.py}
|
||||
MINIO_ROOT_PASSWORD: ${MINIO_ROOT_PASSWORD:?run python scripts/init_env.py}
|
||||
MINIO_BROWSER: "off"
|
||||
MINIO_SITE_REGION: us-east-1
|
||||
MINIO_API_REQUESTS_MAX: "32"
|
||||
networks: [data]
|
||||
volumes:
|
||||
- miniodata:/data
|
||||
cpus: 1.0
|
||||
mem_limit: 512m
|
||||
healthcheck:
|
||||
test: ["CMD", "python", "/opt/minio/healthcheck.py"]
|
||||
interval: 2s
|
||||
timeout: 3s
|
||||
start_period: 10s
|
||||
retries: 15
|
||||
restart: unless-stopped
|
||||
|
||||
minio-init:
|
||||
build:
|
||||
context: .
|
||||
dockerfile: infra/minio/Dockerfile
|
||||
target: bootstrap
|
||||
image: mtusi-minio-mc:RELEASE.2025-08-13T08-35-41Z
|
||||
environment:
|
||||
<<: *s3-env
|
||||
MINIO_ROOT_USER: ${MINIO_ROOT_USER:?run python scripts/init_env.py}
|
||||
MINIO_ROOT_PASSWORD: ${MINIO_ROOT_PASSWORD:?run python scripts/init_env.py}
|
||||
networks: [data]
|
||||
cpus: 0.50
|
||||
mem_limit: 128m
|
||||
depends_on:
|
||||
minio:
|
||||
condition: service_healthy
|
||||
restart: "no"
|
||||
|
||||
cleanup:
|
||||
build: .
|
||||
image: ${API_IMAGE:-mtusi-messenger:lab4}
|
||||
command: ["python", "-m", "app.cleanup", "--loop"]
|
||||
environment: *api-env
|
||||
networks: [data]
|
||||
cpus: 0.25
|
||||
mem_limit: 256m
|
||||
healthcheck:
|
||||
disable: true
|
||||
depends_on:
|
||||
migrate:
|
||||
condition: service_completed_successfully
|
||||
minio-init:
|
||||
condition: service_completed_successfully
|
||||
restart: unless-stopped
|
||||
|
||||
api-a:
|
||||
<<: *api
|
||||
|
||||
api-b:
|
||||
<<: *api
|
||||
|
||||
nginx:
|
||||
image: nginx:1.28.0-alpine
|
||||
entrypoint: ["/bin/sh", "/opt/lb/entrypoint.sh"]
|
||||
volumes:
|
||||
- ./infra/nginx:/opt/lb:ro
|
||||
ports: ["127.0.0.1:8080:8080"]
|
||||
networks: [edge]
|
||||
cpus: 0.25
|
||||
mem_limit: 96m
|
||||
stop_signal: SIGQUIT
|
||||
stop_grace_period: 10s
|
||||
depends_on:
|
||||
api-a:
|
||||
condition: service_started
|
||||
api-b:
|
||||
condition: service_started
|
||||
healthcheck:
|
||||
test: ["CMD", "wget", "-q", "-T", "2", "-O", "/dev/null", "http://127.0.0.1:8080/health/ready"]
|
||||
interval: 2s
|
||||
timeout: 3s
|
||||
retries: 3
|
||||
restart: unless-stopped
|
||||
|
||||
tests:
|
||||
profiles: [test]
|
||||
build:
|
||||
context: .
|
||||
target: test
|
||||
image: mtusi-messenger:lab4-test
|
||||
environment:
|
||||
<<: *s3-env
|
||||
TEST_DB_HOST: db
|
||||
TEST_DB_PORT: "5432"
|
||||
TEST_DB_USER: ${POSTGRES_USER:?set POSTGRES_USER}
|
||||
TEST_DB_PASSWORD: ${POSTGRES_PASSWORD:?set POSTGRES_PASSWORD}
|
||||
TEST_CACHE_URL: redis://redis:6379/15
|
||||
networks: [data]
|
||||
cpus: 1.0
|
||||
mem_limit: 512m
|
||||
depends_on:
|
||||
db:
|
||||
condition: service_healthy
|
||||
redis:
|
||||
condition: service_healthy
|
||||
minio-init:
|
||||
condition: service_completed_successfully
|
||||
command: ["python", "-m", "pytest", "-v", "--no-header", "-p", "no:cacheprovider"]
|
||||
|
||||
networks:
|
||||
edge:
|
||||
data:
|
||||
internal: true
|
||||
|
||||
volumes:
|
||||
miniodata:
|
||||
pgdata:
|
||||
+6
-3
@@ -1,5 +1,8 @@
|
||||
# Доказательства
|
||||
# Доказательства лабораторной 4
|
||||
|
||||
Храните небольшие обезличенные результаты: команды и stdout проверок, JSON/CSV агрегатов нагрузки, EXPLAIN, dashboard JSON, текст разбора отказа. Укажите дату, commit, аппаратные лимиты и профиль нагрузки.
|
||||
`lab4/` — полный прогон текущей версии: `python scripts/lab4_evidence.py`.
|
||||
Внутри: `cluster/` (проверки lab3: идемпотентность, failover обеих реплик, публичный failover, rolling-замена, persistence), файлы `00`–`10` (вложения, отказ загрузившей реплики, restart, отказ S3, private bucket, 10 одновременных загрузок, cleanup, собственные тесты) и `result.json`. Успешной приёмкой считается только `status: PASS` в `result.json`; FAIL/RUNNING — не приёмка.
|
||||
|
||||
Не добавляйте .env, session tokens, cookies, приватные ключи, credentials, реальные сообщения, raw dumps и signed URLs. Для скриншотов скрывайте секреты. Большие логи/бинарные traces сохраняйте отдельно по правилам преподавателя; одного скриншота без методики недостаточно.
|
||||
`archive/` — материалы предыдущих лабораторных (lab2, lab3), оставлены как история; доказательством lab4 не являются.
|
||||
|
||||
Для повторения: `python scripts/lab4_evidence.py --base-url "http://localhost:8080"` на работающем стенде. Подробности — [REPORT.md](../REPORT.md).
|
||||
@@ -0,0 +1,8 @@
|
||||
# Дата (UTC): 2026-10-23 10:17:04
|
||||
# Commit: 65003b6
|
||||
# Команда: python3 scripts/check_contract.py
|
||||
# ---------------------------------------- вывод ----------------------------------------
|
||||
PASS: offline skeleton structure, local refs and Python syntax; application NOT tested.
|
||||
# ---------------------------------------------------------------------------------------
|
||||
# Код возврата: 0
|
||||
# Время выполнения: 0.06 с
|
||||
@@ -0,0 +1,9 @@
|
||||
# Дата (UTC): 2026-10-23 10:17:05
|
||||
# Commit: 65003b6
|
||||
# Команда: make test
|
||||
# ---------------------------------------- вывод ----------------------------------------
|
||||
python3 tests/smoke.py --base-url "http://localhost:8080"
|
||||
PASS: lab2 public API scenario; infrastructure and grading evidence remain separate.
|
||||
# ---------------------------------------------------------------------------------------
|
||||
# Код возврата: 0
|
||||
# Время выполнения: 0.58 с
|
||||
@@ -0,0 +1,11 @@
|
||||
# Дата (UTC): 2026-10-23 10:17:05
|
||||
# Commit: 65003b6
|
||||
# Команда: docker compose --env-file .env ps
|
||||
# ---------------------------------------- вывод ----------------------------------------
|
||||
NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
|
||||
messenger-api mtusi-messenger:lab2 "python -m app" api 24 seconds ago Up 18 seconds (healthy) 127.0.0.1:8080->8080/tcp
|
||||
messenger-db postgres:16.6-bookworm "docker-entrypoint.s…" db 24 seconds ago Up 23 seconds (healthy) 127.0.0.1:5433->5432/tcp
|
||||
messenger-redis redis:7.4.1-bookworm "docker-entrypoint.s…" redis 24 seconds ago Up 23 seconds (healthy) 6379/tcp
|
||||
# ---------------------------------------------------------------------------------------
|
||||
# Код возврата: 0
|
||||
# Время выполнения: 0.07 с
|
||||
@@ -0,0 +1,8 @@
|
||||
# Дата (UTC): 2026-10-23 10:17:05
|
||||
# Commit: 65003b6
|
||||
# Команда: docker volume inspect mtusi-messenger_pgdata --format {{.Name}} created={{.CreatedAt}}
|
||||
# ---------------------------------------- вывод ----------------------------------------
|
||||
mtusi-messenger_pgdata created=2026-10-23T13:16:41+03:00
|
||||
# ---------------------------------------------------------------------------------------
|
||||
# Код возврата: 0
|
||||
# Время выполнения: 0.01 с
|
||||
@@ -0,0 +1,10 @@
|
||||
# Дата (UTC): 2026-10-23 10:17:14
|
||||
# Commit: 65003b6
|
||||
# Команда: make test-persistence ACTION_SCRIPT=scripts/restart.sh
|
||||
# ---------------------------------------- вывод ----------------------------------------
|
||||
python3 tests/resilience.py --mode persistence --action-script "scripts/restart.sh" --base-url "http://localhost: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.
|
||||
# ---------------------------------------------------------------------------------------
|
||||
# Код возврата: 0
|
||||
# Время выполнения: 9.13 с
|
||||
@@ -0,0 +1,11 @@
|
||||
# Дата (UTC): 2026-10-23 10:17:14
|
||||
# Commit: 65003b6
|
||||
# Команда: docker compose --env-file .env ps
|
||||
# ---------------------------------------- вывод ----------------------------------------
|
||||
NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
|
||||
messenger-api mtusi-messenger:lab2 "python -m app" api 33 seconds ago Up 1 second (health: starting) 127.0.0.1:8080->8080/tcp
|
||||
messenger-db postgres:16.6-bookworm "docker-entrypoint.s…" db 33 seconds ago Up 7 seconds (healthy) 127.0.0.1:5433->5432/tcp
|
||||
messenger-redis redis:7.4.1-bookworm "docker-entrypoint.s…" redis 33 seconds ago Up 7 seconds (healthy) 6379/tcp
|
||||
# ---------------------------------------------------------------------------------------
|
||||
# Код возврата: 0
|
||||
# Время выполнения: 0.07 с
|
||||
@@ -0,0 +1,8 @@
|
||||
# Дата (UTC): 2026-10-23 10:17:14
|
||||
# Commit: 65003b6
|
||||
# Команда: docker volume inspect mtusi-messenger_pgdata --format {{.Name}} created={{.CreatedAt}}
|
||||
# ---------------------------------------- вывод ----------------------------------------
|
||||
mtusi-messenger_pgdata created=2026-10-23T13:16:41+03:00
|
||||
# ---------------------------------------------------------------------------------------
|
||||
# Код возврата: 0
|
||||
# Время выполнения: 0.01 с
|
||||
@@ -0,0 +1,83 @@
|
||||
# Дата (UTC): 2026-10-23 10:17:15
|
||||
# Commit: 65003b6
|
||||
# Команда: docker compose --env-file .env exec -T db psql -U messenger -d messenger -c \d users -c \d chats -c \d chat_members -c \d messages -c \d sessions
|
||||
# ---------------------------------------- вывод ----------------------------------------
|
||||
Table "public.users"
|
||||
Column | Type | Collation | Nullable | Default
|
||||
---------------+--------------------------+-----------+----------+-------------------
|
||||
id | uuid | | not null |
|
||||
username | text | | not null |
|
||||
display_name | text | | not null |
|
||||
password_hash | text | | not null |
|
||||
created_at | timestamp with time zone | | not null | clock_timestamp()
|
||||
Indexes:
|
||||
"users_pkey" PRIMARY KEY, btree (id)
|
||||
"users_username_key" UNIQUE CONSTRAINT, btree (username)
|
||||
Check constraints:
|
||||
"users_display_name_check" CHECK (char_length(display_name) >= 1 AND char_length(display_name) <= 100)
|
||||
"users_username_check" CHECK (username ~ '^[a-z0-9_]{3,32}$'::text)
|
||||
Referenced by:
|
||||
TABLE "chat_members" CONSTRAINT "chat_members_user_id_fkey" FOREIGN KEY (user_id) REFERENCES users(id)
|
||||
TABLE "messages" CONSTRAINT "messages_sender_id_fkey" FOREIGN KEY (sender_id) REFERENCES users(id)
|
||||
TABLE "sessions" CONSTRAINT "sessions_user_id_fkey" FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
|
||||
|
||||
Table "public.chats"
|
||||
Column | Type | Collation | Nullable | Default
|
||||
------------+--------------------------+-----------+----------+-------------------
|
||||
id | uuid | | not null |
|
||||
title | text | | not null |
|
||||
created_at | timestamp with time zone | | not null | clock_timestamp()
|
||||
Indexes:
|
||||
"chats_pkey" PRIMARY KEY, btree (id)
|
||||
"chats_created_idx" btree (created_at, id)
|
||||
Check constraints:
|
||||
"chats_title_check" CHECK (char_length(title) >= 1 AND char_length(title) <= 100)
|
||||
Referenced by:
|
||||
TABLE "chat_members" CONSTRAINT "chat_members_chat_id_fkey" FOREIGN KEY (chat_id) REFERENCES chats(id) ON DELETE CASCADE
|
||||
TABLE "messages" CONSTRAINT "messages_chat_id_fkey" FOREIGN KEY (chat_id) REFERENCES chats(id) ON DELETE CASCADE
|
||||
|
||||
Table "public.chat_members"
|
||||
Column | Type | Collation | Nullable | Default
|
||||
---------+------+-----------+----------+---------
|
||||
chat_id | uuid | | not null |
|
||||
user_id | uuid | | not null |
|
||||
Indexes:
|
||||
"chat_members_pkey" PRIMARY KEY, btree (chat_id, user_id)
|
||||
"chat_members_user_idx" btree (user_id, chat_id)
|
||||
Foreign-key constraints:
|
||||
"chat_members_chat_id_fkey" FOREIGN KEY (chat_id) REFERENCES chats(id) ON DELETE CASCADE
|
||||
"chat_members_user_id_fkey" FOREIGN KEY (user_id) REFERENCES users(id)
|
||||
|
||||
Table "public.messages"
|
||||
Column | Type | Collation | Nullable | Default
|
||||
------------+--------------------------+-----------+----------+-------------------
|
||||
id | uuid | | not null |
|
||||
chat_id | uuid | | not null |
|
||||
sender_id | uuid | | not null |
|
||||
text | text | | not null |
|
||||
created_at | timestamp with time zone | | not null | clock_timestamp()
|
||||
Indexes:
|
||||
"messages_pkey" PRIMARY KEY, btree (id)
|
||||
"messages_chat_created_idx" btree (chat_id, created_at, id)
|
||||
Check constraints:
|
||||
"messages_text_check" CHECK (char_length(text) >= 1 AND char_length(text) <= 4000)
|
||||
Foreign-key constraints:
|
||||
"messages_chat_id_fkey" FOREIGN KEY (chat_id) REFERENCES chats(id) ON DELETE CASCADE
|
||||
"messages_sender_id_fkey" FOREIGN KEY (sender_id) REFERENCES users(id)
|
||||
|
||||
Table "public.sessions"
|
||||
Column | Type | Collation | Nullable | Default
|
||||
------------+--------------------------+-----------+----------+-------------------
|
||||
token_hash | text | | not null |
|
||||
user_id | uuid | | not null |
|
||||
created_at | timestamp with time zone | | not null | clock_timestamp()
|
||||
expires_at | timestamp with time zone | | not null |
|
||||
Indexes:
|
||||
"sessions_pkey" PRIMARY KEY, btree (token_hash)
|
||||
"sessions_expires_idx" btree (expires_at)
|
||||
"sessions_user_idx" btree (user_id)
|
||||
Foreign-key constraints:
|
||||
"sessions_user_id_fkey" FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
|
||||
# ---------------------------------------------------------------------------------------
|
||||
# Код возврата: 0
|
||||
# Время выполнения: 0.16 с
|
||||
@@ -0,0 +1,8 @@
|
||||
# Дата (UTC): 2026-10-23 10:17:16
|
||||
# Commit: 65003b6
|
||||
# Команда: docker compose --env-file .env exec -T api python -m app.migrate
|
||||
# ---------------------------------------- вывод ----------------------------------------
|
||||
Применено: ничего нового
|
||||
# ---------------------------------------------------------------------------------------
|
||||
# Код возврата: 0
|
||||
# Время выполнения: 0.98 с
|
||||
@@ -0,0 +1,16 @@
|
||||
# Дата (UTC): 2026-10-23 10:16:49
|
||||
# Commit: 65003b6
|
||||
# Команда: docker compose --env-file .env logs api
|
||||
# ---------------------------------------- вывод ----------------------------------------
|
||||
messenger-api | 2026-10-23T10:16:48+0000 INFO listen host=0.0.0.0 port=8080
|
||||
messenger-api | INFO: Started server process [1]
|
||||
messenger-api | INFO: Waiting for application startup.
|
||||
messenger-api | 2026-10-23T10:16:48+0000 INFO config env=lab pool=5+5 ttl=3600s store=postgres cache=redis
|
||||
messenger-api | 2026-10-23T10:16:48+0000 INFO migrations: 0001_init.sql
|
||||
messenger-api | 2026-10-23T10:16:48+0000 INFO startup: приложение готово
|
||||
messenger-api | INFO: Application startup complete.
|
||||
messenger-api | INFO: Uvicorn running on http://0.0.0.0:8080 (Press CTRL+C to quit)
|
||||
messenger-api | 2026-10-23T10:16:49+0000 INFO request request_id=4933db77a1c84e3197e740dc2e11d0f1 method=GET path=/health/ready status=200 duration_ms=14.7
|
||||
# ---------------------------------------------------------------------------------------
|
||||
# Код возврата: 0
|
||||
# Время выполнения: 0.07 с
|
||||
@@ -0,0 +1,11 @@
|
||||
# Дата (UTC): 2026-10-23 10:17:16
|
||||
# Commit: 65003b6
|
||||
# Команда: docker compose --env-file .env exec -T db psql -U messenger -d messenger -c SELECT * FROM schema_migrations
|
||||
# ---------------------------------------- вывод ----------------------------------------
|
||||
version | applied_at
|
||||
---------------+-------------------------------
|
||||
0001_init.sql | 2026-10-23 10:16:48.774351+00
|
||||
(1 row)
|
||||
# ---------------------------------------------------------------------------------------
|
||||
# Код возврата: 0
|
||||
# Время выполнения: 0.13 с
|
||||
@@ -0,0 +1,20 @@
|
||||
# Дата (UTC): 2026-10-23 10:17:16
|
||||
# Commit: 65003b6
|
||||
# Команда: docker compose --env-file .env exec -T db psql -U messenger -d messenger -c SELECT username, left(password_hash, 7) AS password_prefix, length(password_hash) AS len FROM users WHERE password_hash LIKE 'scrypt$%' LIMIT 3 -c SELECT length(token_hash) AS token_hash_len, expires_at FROM sessions LIMIT 3
|
||||
# ---------------------------------------- вывод ----------------------------------------
|
||||
username | password_prefix | len
|
||||
----------------------+-----------------+-----
|
||||
u_alice_cbe058671fcd | scrypt$ | 86
|
||||
u_bob_b1685b095bab | scrypt$ | 86
|
||||
u_eve_1f0c444128f3 | scrypt$ | 86
|
||||
(3 rows)
|
||||
|
||||
token_hash_len | expires_at
|
||||
----------------+-------------------------------
|
||||
64 | 2026-10-23 11:17:05.128109+00
|
||||
64 | 2026-10-23 11:17:05.223716+00
|
||||
64 | 2026-10-23 11:17:05.789433+00
|
||||
(3 rows)
|
||||
# ---------------------------------------------------------------------------------------
|
||||
# Код возврата: 0
|
||||
# Время выполнения: 0.13 с
|
||||
@@ -0,0 +1,64 @@
|
||||
# Дата (UTC): 2026-10-23 10:17:28
|
||||
# Commit: 65003b6
|
||||
# Команда: docker compose --env-file .env --profile test run --rm tests python -m pytest -v --no-header -p no:cacheprovider student-tests/test_auth.py student-tests/test_session_expiry.py
|
||||
# ---------------------------------------- вывод ----------------------------------------
|
||||
============================= test session starts ==============================
|
||||
collecting ... collected 38 items
|
||||
|
||||
student-tests/test_auth.py::test_register_returns_user_without_secrets PASSED [ 2%]
|
||||
student-tests/test_auth.py::test_register_password_rules[None] PASSED [ 5%]
|
||||
student-tests/test_auth.py::test_register_password_rules[] PASSED [ 7%]
|
||||
student-tests/test_auth.py::test_register_password_rules[short] PASSED [ 10%]
|
||||
student-tests/test_auth.py::test_register_password_rules[aaaaaaaaaaa] PASSED [ 13%]
|
||||
student-tests/test_auth.py::test_register_password_rules[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa] PASSED [ 15%]
|
||||
student-tests/test_auth.py::test_register_password_rules[123456789012] PASSED [ 18%]
|
||||
student-tests/test_auth.py::test_register_password_rules[\u043f\u0430\u0440\u043e\u043b\u044c] PASSED [ 21%]
|
||||
student-tests/test_auth.py::test_register_password_border_lengths PASSED [ 23%]
|
||||
student-tests/test_auth.py::test_login_success_shape PASSED [ 26%]
|
||||
student-tests/test_auth.py::test_each_login_gives_new_token PASSED [ 28%]
|
||||
student-tests/test_auth.py::test_wrong_password_and_unknown_user_look_the_same PASSED [ 31%]
|
||||
student-tests/test_auth.py::test_malformed_basic_is_401[Basic] PASSED [ 34%]
|
||||
student-tests/test_auth.py::test_malformed_basic_is_401[Basic !!!not-base64!!!] PASSED [ 36%]
|
||||
student-tests/test_auth.py::test_malformed_basic_is_401[Basic bm9jb2xvbg==] PASSED [ 39%]
|
||||
student-tests/test_auth.py::test_malformed_basic_is_401[Basic 0LjQvNGPOnBhc3N3b3JkLTEyMzQ1Ng==] PASSED [ 42%]
|
||||
student-tests/test_auth.py::test_malformed_basic_is_401[Basic dXNlcjr//i1iYWQtdXRmOA==] PASSED [ 44%]
|
||||
student-tests/test_auth.py::test_malformed_basic_is_401[Bearer abc] PASSED [ 47%]
|
||||
student-tests/test_auth.py::test_malformed_basic_is_401[] PASSED [ 50%]
|
||||
student-tests/test_auth.py::test_login_without_header_is_401 PASSED [ 52%]
|
||||
student-tests/test_auth.py::test_password_with_colon_and_unicode_works PASSED [ 55%]
|
||||
student-tests/test_auth.py::test_bearer_token_gives_access PASSED [ 57%]
|
||||
student-tests/test_auth.py::test_business_endpoint_rejects_basic PASSED [ 60%]
|
||||
student-tests/test_auth.py::test_bad_or_unknown_bearer_is_401[] PASSED [ 63%]
|
||||
student-tests/test_auth.py::test_bad_or_unknown_bearer_is_401[Bearer] PASSED [ 65%]
|
||||
student-tests/test_auth.py::test_bad_or_unknown_bearer_is_401[Bearer short] PASSED [ 68%]
|
||||
student-tests/test_auth.py::test_bad_or_unknown_bearer_is_401[Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx] PASSED [ 71%]
|
||||
student-tests/test_auth.py::test_bad_or_unknown_bearer_is_401[Token abc] PASSED [ 73%]
|
||||
student-tests/test_auth.py::test_bad_or_unknown_bearer_is_401[bearer] PASSED [ 76%]
|
||||
student-tests/test_auth.py::test_x_user_id_alone_gives_no_access PASSED [ 78%]
|
||||
student-tests/test_auth.py::test_x_user_id_does_not_change_owner_of_session PASSED [ 81%]
|
||||
student-tests/test_auth.py::test_logout_revokes_token_and_repeat_is_401 PASSED [ 84%]
|
||||
student-tests/test_auth.py::test_logout_without_token_is_401 PASSED [ 86%]
|
||||
student-tests/test_auth.py::test_logout_revokes_only_current_session PASSED [ 89%]
|
||||
student-tests/test_auth.py::test_session_lives_longer_than_300_seconds PASSED [ 92%]
|
||||
student-tests/test_session_expiry.py::test_session_expires_after_ttl PASSED [ 94%]
|
||||
student-tests/test_session_expiry.py::test_expired_sessions_are_cleaned_on_next_login PASSED [ 97%]
|
||||
student-tests/test_session_expiry.py::test_short_ttl_from_environment_variable PASSED [100%]
|
||||
|
||||
=============================== warnings summary ===============================
|
||||
../usr/local/lib/python3.12/site-packages/fastapi/testclient.py:1
|
||||
/usr/local/lib/python3.12/site-packages/fastapi/testclient.py:1: StarletteDeprecationWarning: Using `httpx` with `starlette.testclient` is deprecated; install `httpx2` instead.
|
||||
from starlette.testclient import TestClient as TestClient # noqa
|
||||
|
||||
-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
|
||||
======================== 38 passed, 1 warning in 8.97s =========================
|
||||
Container messenger-db Running
|
||||
Container messenger-redis Running
|
||||
Container messenger-db Waiting
|
||||
Container messenger-redis Waiting
|
||||
Container messenger-redis Healthy
|
||||
Container messenger-db Healthy
|
||||
Container mtusi-messenger-tests-run-5a33a2ea2bbb Creating
|
||||
Container mtusi-messenger-tests-run-5a33a2ea2bbb Created
|
||||
# ---------------------------------------------------------------------------------------
|
||||
# Код возврата: 0
|
||||
# Время выполнения: 12.15 с
|
||||
@@ -0,0 +1,45 @@
|
||||
# Дата (UTC): 2026-10-23 10:17:35
|
||||
# Commit: 65003b6
|
||||
# Команда: docker compose --env-file .env --profile test run --rm tests python -m pytest -v --no-header -p no:cacheprovider student-tests/test_database.py student-tests/test_db_failure.py student-tests/test_transactions.py student-tests/test_duplicate_username.py
|
||||
# ---------------------------------------- вывод ----------------------------------------
|
||||
============================= test session starts ==============================
|
||||
collecting ... collected 19 items
|
||||
|
||||
student-tests/test_database.py::test_password_is_stored_as_scrypt_hash_not_plaintext PASSED [ 5%]
|
||||
student-tests/test_database.py::test_same_password_gives_different_hashes PASSED [ 10%]
|
||||
student-tests/test_database.py::test_session_table_keeps_token_hash_not_token PASSED [ 15%]
|
||||
student-tests/test_database.py::test_message_is_really_in_the_database PASSED [ 21%]
|
||||
student-tests/test_database.py::test_migrations_can_run_again_without_duplicating_anything PASSED [ 26%]
|
||||
student-tests/test_database.py::test_expected_tables_and_indexes_exist PASSED [ 31%]
|
||||
student-tests/test_database.py::test_primary_and_foreign_keys_are_declared PASSED [ 36%]
|
||||
student-tests/test_database.py::test_database_itself_rejects_bad_rows PASSED [ 42%]
|
||||
student-tests/test_database.py::test_chat_creation_is_atomic PASSED [ 47%]
|
||||
student-tests/test_database.py::test_successful_chat_has_all_members_in_database PASSED [ 52%]
|
||||
student-tests/test_db_failure.py::test_database_down_gives_503_and_never_trusts_anyone PASSED [ 57%]
|
||||
student-tests/test_db_failure.py::test_database_down_with_cache_up_still_needs_database_for_data PASSED [ 63%]
|
||||
student-tests/test_transactions.py::test_failure_after_chat_insert_rolls_back_whole_operation PASSED [ 68%]
|
||||
student-tests/test_transactions.py::test_pool_is_bounded_by_settings PASSED [ 73%]
|
||||
student-tests/test_transactions.py::test_exhausted_pool_gives_fast_503 PASSED [ 78%]
|
||||
student-tests/test_duplicate_username.py::test_second_registration_with_same_username_is_409 PASSED [ 84%]
|
||||
student-tests/test_duplicate_username.py::test_duplicate_does_not_replace_original_user PASSED [ 89%]
|
||||
student-tests/test_duplicate_username.py::test_username_is_case_sensitive_lowercase_only PASSED [ 94%]
|
||||
student-tests/test_duplicate_username.py::test_parallel_registration_of_same_username_creates_exactly_one PASSED [100%]
|
||||
|
||||
=============================== warnings summary ===============================
|
||||
../usr/local/lib/python3.12/site-packages/fastapi/testclient.py:1
|
||||
/usr/local/lib/python3.12/site-packages/fastapi/testclient.py:1: StarletteDeprecationWarning: Using `httpx` with `starlette.testclient` is deprecated; install `httpx2` instead.
|
||||
from starlette.testclient import TestClient as TestClient # noqa
|
||||
|
||||
-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
|
||||
======================== 19 passed, 1 warning in 3.91s =========================
|
||||
Container messenger-redis Running
|
||||
Container messenger-db Running
|
||||
Container messenger-db Waiting
|
||||
Container messenger-redis Waiting
|
||||
Container messenger-db Healthy
|
||||
Container messenger-redis Healthy
|
||||
Container mtusi-messenger-tests-run-28da14e927b9 Creating
|
||||
Container mtusi-messenger-tests-run-28da14e927b9 Created
|
||||
# ---------------------------------------------------------------------------------------
|
||||
# Код возврата: 0
|
||||
# Время выполнения: 7.04 с
|
||||
@@ -0,0 +1,179 @@
|
||||
# Дата (UTC): 2026-10-23 10:18:12
|
||||
# Commit: 65003b6
|
||||
# Команда: docker compose --env-file .env --profile test run --rm tests
|
||||
# ---------------------------------------- вывод ----------------------------------------
|
||||
============================= test session starts ==============================
|
||||
collecting ... collected 153 items
|
||||
|
||||
student-tests/test_auth.py::test_register_returns_user_without_secrets PASSED [ 0%]
|
||||
student-tests/test_auth.py::test_register_password_rules[None] PASSED [ 1%]
|
||||
student-tests/test_auth.py::test_register_password_rules[] PASSED [ 1%]
|
||||
student-tests/test_auth.py::test_register_password_rules[short] PASSED [ 2%]
|
||||
student-tests/test_auth.py::test_register_password_rules[aaaaaaaaaaa] PASSED [ 3%]
|
||||
student-tests/test_auth.py::test_register_password_rules[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa] PASSED [ 3%]
|
||||
student-tests/test_auth.py::test_register_password_rules[123456789012] PASSED [ 4%]
|
||||
student-tests/test_auth.py::test_register_password_rules[\u043f\u0430\u0440\u043e\u043b\u044c] PASSED [ 5%]
|
||||
student-tests/test_auth.py::test_register_password_border_lengths PASSED [ 5%]
|
||||
student-tests/test_auth.py::test_login_success_shape PASSED [ 6%]
|
||||
student-tests/test_auth.py::test_each_login_gives_new_token PASSED [ 7%]
|
||||
student-tests/test_auth.py::test_wrong_password_and_unknown_user_look_the_same PASSED [ 7%]
|
||||
student-tests/test_auth.py::test_malformed_basic_is_401[Basic] PASSED [ 8%]
|
||||
student-tests/test_auth.py::test_malformed_basic_is_401[Basic !!!not-base64!!!] PASSED [ 9%]
|
||||
student-tests/test_auth.py::test_malformed_basic_is_401[Basic bm9jb2xvbg==] PASSED [ 9%]
|
||||
student-tests/test_auth.py::test_malformed_basic_is_401[Basic 0LjQvNGPOnBhc3N3b3JkLTEyMzQ1Ng==] PASSED [ 10%]
|
||||
student-tests/test_auth.py::test_malformed_basic_is_401[Basic dXNlcjr//i1iYWQtdXRmOA==] PASSED [ 11%]
|
||||
student-tests/test_auth.py::test_malformed_basic_is_401[Bearer abc] PASSED [ 11%]
|
||||
student-tests/test_auth.py::test_malformed_basic_is_401[] PASSED [ 12%]
|
||||
student-tests/test_auth.py::test_login_without_header_is_401 PASSED [ 13%]
|
||||
student-tests/test_auth.py::test_password_with_colon_and_unicode_works PASSED [ 13%]
|
||||
student-tests/test_auth.py::test_bearer_token_gives_access PASSED [ 14%]
|
||||
student-tests/test_auth.py::test_business_endpoint_rejects_basic PASSED [ 15%]
|
||||
student-tests/test_auth.py::test_bad_or_unknown_bearer_is_401[] PASSED [ 15%]
|
||||
student-tests/test_auth.py::test_bad_or_unknown_bearer_is_401[Bearer] PASSED [ 16%]
|
||||
student-tests/test_auth.py::test_bad_or_unknown_bearer_is_401[Bearer short] PASSED [ 16%]
|
||||
student-tests/test_auth.py::test_bad_or_unknown_bearer_is_401[Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx] PASSED [ 17%]
|
||||
student-tests/test_auth.py::test_bad_or_unknown_bearer_is_401[Token abc] PASSED [ 18%]
|
||||
student-tests/test_auth.py::test_bad_or_unknown_bearer_is_401[bearer] PASSED [ 18%]
|
||||
student-tests/test_auth.py::test_x_user_id_alone_gives_no_access PASSED [ 19%]
|
||||
student-tests/test_auth.py::test_x_user_id_does_not_change_owner_of_session PASSED [ 20%]
|
||||
student-tests/test_auth.py::test_logout_revokes_token_and_repeat_is_401 PASSED [ 20%]
|
||||
student-tests/test_auth.py::test_logout_without_token_is_401 PASSED [ 21%]
|
||||
student-tests/test_auth.py::test_logout_revokes_only_current_session PASSED [ 22%]
|
||||
student-tests/test_auth.py::test_session_lives_longer_than_300_seconds PASSED [ 22%]
|
||||
student-tests/test_cache.py::test_token_check_is_served_from_cache_with_bounded_ttl PASSED [ 23%]
|
||||
student-tests/test_cache.py::test_cache_never_stores_raw_tokens_or_passwords PASSED [ 24%]
|
||||
student-tests/test_cache.py::test_logout_revokes_cached_session PASSED [ 24%]
|
||||
student-tests/test_cache.py::test_late_cache_fill_cannot_resurrect_revoked_session PASSED [ 25%]
|
||||
student-tests/test_cache.py::test_flushed_cache_loses_nothing PASSED [ 26%]
|
||||
student-tests/test_cache.py::test_cache_does_not_extend_short_session PASSED [ 26%]
|
||||
student-tests/test_cache.py::test_staleness_after_out_of_band_delete_is_bounded_by_cache_ttl PASSED [ 27%]
|
||||
student-tests/test_cache.py::test_redis_down_requests_bypass_cache_and_stay_fast PASSED [ 28%]
|
||||
student-tests/test_cache.py::test_redis_down_never_trusts_anonymous_or_unknown_tokens PASSED [ 28%]
|
||||
student-tests/test_cache.py::test_revocation_lost_while_redis_down_is_flushed_on_recovery PASSED [ 29%]
|
||||
student-tests/test_cache.py::test_without_cache_url_cache_is_off PASSED [ 30%]
|
||||
student-tests/test_concurrency.py::test_100_messages_from_10_clients_in_parallel PASSED [ 30%]
|
||||
student-tests/test_concurrency.py::test_parallel_chat_creation_and_reading PASSED [ 31%]
|
||||
student-tests/test_contract_basics.py::test_health_endpoints PASSED [ 32%]
|
||||
student-tests/test_contract_basics.py::test_every_response_has_unique_request_id PASSED [ 32%]
|
||||
student-tests/test_contract_basics.py::test_unknown_path_is_404_in_common_format PASSED [ 33%]
|
||||
student-tests/test_contract_basics.py::test_trailing_slash_is_not_redirected PASSED [ 33%]
|
||||
student-tests/test_contract_basics.py::test_wrong_method_is_405_in_common_format PASSED [ 34%]
|
||||
student-tests/test_contract_basics.py::test_two_users_one_chat_two_messages PASSED [ 35%]
|
||||
student-tests/test_contract_basics.py::test_chat_response_shape PASSED [ 35%]
|
||||
student-tests/test_contract_basics.py::test_unexpected_exception_gives_500_without_details PASSED [ 36%]
|
||||
student-tests/test_contract_basics.py::test_logs_have_requests_but_no_secrets PASSED [ 37%]
|
||||
student-tests/test_database.py::test_password_is_stored_as_scrypt_hash_not_plaintext PASSED [ 37%]
|
||||
student-tests/test_database.py::test_same_password_gives_different_hashes PASSED [ 38%]
|
||||
student-tests/test_database.py::test_session_table_keeps_token_hash_not_token PASSED [ 39%]
|
||||
student-tests/test_database.py::test_message_is_really_in_the_database PASSED [ 39%]
|
||||
student-tests/test_database.py::test_migrations_can_run_again_without_duplicating_anything PASSED [ 40%]
|
||||
student-tests/test_database.py::test_expected_tables_and_indexes_exist PASSED [ 41%]
|
||||
student-tests/test_database.py::test_primary_and_foreign_keys_are_declared PASSED [ 41%]
|
||||
student-tests/test_database.py::test_database_itself_rejects_bad_rows PASSED [ 42%]
|
||||
student-tests/test_database.py::test_chat_creation_is_atomic PASSED [ 43%]
|
||||
student-tests/test_database.py::test_successful_chat_has_all_members_in_database PASSED [ 43%]
|
||||
student-tests/test_db_failure.py::test_database_down_gives_503_and_never_trusts_anyone PASSED [ 44%]
|
||||
student-tests/test_db_failure.py::test_database_down_with_cache_up_still_needs_database_for_data PASSED [ 45%]
|
||||
student-tests/test_duplicate_username.py::test_second_registration_with_same_username_is_409 PASSED [ 45%]
|
||||
student-tests/test_duplicate_username.py::test_duplicate_does_not_replace_original_user PASSED [ 46%]
|
||||
student-tests/test_duplicate_username.py::test_username_is_case_sensitive_lowercase_only PASSED [ 47%]
|
||||
student-tests/test_duplicate_username.py::test_parallel_registration_of_same_username_creates_exactly_one PASSED [ 47%]
|
||||
student-tests/test_empty_message.py::test_blank_text_is_rejected[] PASSED [ 48%]
|
||||
student-tests/test_empty_message.py::test_blank_text_is_rejected[ ] PASSED [ 49%]
|
||||
student-tests/test_empty_message.py::test_blank_text_is_rejected[ ] PASSED [ 49%]
|
||||
student-tests/test_empty_message.py::test_blank_text_is_rejected[\n] PASSED [ 50%]
|
||||
student-tests/test_empty_message.py::test_blank_text_is_rejected[\t \n] PASSED [ 50%]
|
||||
student-tests/test_empty_message.py::test_blank_text_is_rejected[\xa0] PASSED [ 51%]
|
||||
student-tests/test_empty_message.py::test_blank_text_is_rejected[\u2003] PASSED [ 52%]
|
||||
student-tests/test_empty_message.py::test_missing_or_wrong_type_text_is_rejected[body0] PASSED [ 52%]
|
||||
student-tests/test_empty_message.py::test_missing_or_wrong_type_text_is_rejected[body1] PASSED [ 53%]
|
||||
student-tests/test_empty_message.py::test_missing_or_wrong_type_text_is_rejected[body2] PASSED [ 54%]
|
||||
student-tests/test_empty_message.py::test_missing_or_wrong_type_text_is_rejected[body3] PASSED [ 54%]
|
||||
student-tests/test_empty_message.py::test_missing_or_wrong_type_text_is_rejected[body4] PASSED [ 55%]
|
||||
student-tests/test_empty_message.py::test_text_length_is_counted_in_characters PASSED [ 56%]
|
||||
student-tests/test_empty_message.py::test_nul_character_is_400_not_500 PASSED [ 56%]
|
||||
student-tests/test_empty_message.py::test_unicode_survives_the_database PASSED [ 57%]
|
||||
student-tests/test_empty_message.py::test_message_has_correct_fields PASSED [ 58%]
|
||||
student-tests/test_explain.py::test_history_page_uses_index_without_full_scan[0] PASSED [ 58%]
|
||||
student-tests/test_explain.py::test_history_page_uses_index_without_full_scan[9000] PASSED [ 59%]
|
||||
student-tests/test_foreign_chat.py::test_outsider_cannot_read_chat PASSED [ 60%]
|
||||
student-tests/test_foreign_chat.py::test_outsider_cannot_read_history PASSED [ 60%]
|
||||
student-tests/test_foreign_chat.py::test_outsider_cannot_write PASSED [ 61%]
|
||||
student-tests/test_foreign_chat.py::test_outsider_gets_404_even_with_bad_body PASSED [ 62%]
|
||||
student-tests/test_foreign_chat.py::test_unknown_chat_looks_the_same_as_foreign PASSED [ 62%]
|
||||
student-tests/test_foreign_chat.py::test_chat_list_shows_only_own_chats PASSED [ 63%]
|
||||
student-tests/test_foreign_chat.py::test_bad_chat_id_is_400 PASSED [ 64%]
|
||||
student-tests/test_foreign_chat.py::test_no_token_is_401_everywhere PASSED [ 64%]
|
||||
student-tests/test_foreign_chat.py::test_401_comes_before_404 PASSED [ 65%]
|
||||
student-tests/test_foreign_chat.py::test_member_can_read_and_write PASSED [ 66%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_broken_json[{] PASSED [ 66%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_broken_json[not json at all] PASSED [ 67%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_broken_json[] PASSED [ 67%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_broken_json[{"username": "abc",}] PASSED [ 68%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_broken_json[\xff\xfe\x00] PASSED [ 69%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_broken_json[{"username": NaN}] PASSED [ 69%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_broken_json[["username"]] PASSED [ 70%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_broken_json["text"] PASSED [ 71%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_broken_json[null] PASSED [ 71%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes0] PASSED [ 72%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes1] PASSED [ 73%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes2] PASSED [ 73%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes3] PASSED [ 74%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes4] PASSED [ 75%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes5] PASSED [ 75%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes6] PASSED [ 76%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes7] PASSED [ 77%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes8] PASSED [ 77%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes9] PASSED [ 78%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes10] PASSED [ 79%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes11] PASSED [ 79%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes12] PASSED [ 80%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes13] PASSED [ 81%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes14] PASSED [ 81%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes15] PASSED [ 82%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes16] PASSED [ 83%]
|
||||
student-tests/test_invalid_json.py::test_border_values_are_accepted PASSED [ 83%]
|
||||
student-tests/test_invalid_json.py::test_lone_surrogate_is_400_not_500 PASSED [ 84%]
|
||||
student-tests/test_invalid_json.py::test_wrong_or_missing_content_type_is_415 PASSED [ 84%]
|
||||
student-tests/test_invalid_json.py::test_huge_body_is_413 PASSED [ 85%]
|
||||
student-tests/test_invalid_json.py::test_broken_json_in_message_and_chat PASSED [ 86%]
|
||||
student-tests/test_invalid_json.py::test_sender_id_in_message_is_rejected PASSED [ 86%]
|
||||
student-tests/test_invalid_json.py::test_chat_body_validation PASSED [ 87%]
|
||||
student-tests/test_pagination.py::test_walk_25_messages_by_7 PASSED [ 88%]
|
||||
student-tests/test_pagination.py::test_exact_multiple_has_no_empty_extra_page PASSED [ 88%]
|
||||
student-tests/test_pagination.py::test_limit_one_walks_everything PASSED [ 89%]
|
||||
student-tests/test_pagination.py::test_default_limit_is_50_and_max_is_100 PASSED [ 90%]
|
||||
student-tests/test_pagination.py::test_empty_history PASSED [ 90%]
|
||||
student-tests/test_pagination.py::test_limit_can_change_between_pages PASSED [ 91%]
|
||||
student-tests/test_pagination.py::test_messages_sent_during_walk_appear_at_the_end PASSED [ 92%]
|
||||
student-tests/test_pagination.py::test_created_at_is_increasing_and_utc PASSED [ 92%]
|
||||
student-tests/test_pagination.py::test_bad_limit_values PASSED [ 93%]
|
||||
student-tests/test_pagination.py::test_garbage_cursor_is_400 PASSED [ 94%]
|
||||
student-tests/test_pagination.py::test_cursor_from_another_chat_is_400 PASSED [ 94%]
|
||||
student-tests/test_pagination.py::test_chat_list_pagination PASSED [ 95%]
|
||||
student-tests/test_pagination.py::test_chat_list_cursor_cannot_be_used_for_messages PASSED [ 96%]
|
||||
student-tests/test_session_expiry.py::test_session_expires_after_ttl PASSED [ 96%]
|
||||
student-tests/test_session_expiry.py::test_expired_sessions_are_cleaned_on_next_login PASSED [ 97%]
|
||||
student-tests/test_session_expiry.py::test_short_ttl_from_environment_variable PASSED [ 98%]
|
||||
student-tests/test_transactions.py::test_failure_after_chat_insert_rolls_back_whole_operation PASSED [ 98%]
|
||||
student-tests/test_transactions.py::test_pool_is_bounded_by_settings PASSED [ 99%]
|
||||
student-tests/test_transactions.py::test_exhausted_pool_gives_fast_503 PASSED [100%]
|
||||
|
||||
=============================== warnings summary ===============================
|
||||
../usr/local/lib/python3.12/site-packages/fastapi/testclient.py:1
|
||||
/usr/local/lib/python3.12/site-packages/fastapi/testclient.py:1: StarletteDeprecationWarning: Using `httpx` with `starlette.testclient` is deprecated; install `httpx2` instead.
|
||||
from starlette.testclient import TestClient as TestClient # noqa
|
||||
|
||||
-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
|
||||
======================= 153 passed, 1 warning in 33.55s ========================
|
||||
Container messenger-db Running
|
||||
Container messenger-redis Running
|
||||
Container messenger-redis Waiting
|
||||
Container messenger-db Waiting
|
||||
Container messenger-redis Healthy
|
||||
Container messenger-db Healthy
|
||||
Container mtusi-messenger-tests-run-27b6c4db6282 Creating
|
||||
Container mtusi-messenger-tests-run-27b6c4db6282 Created
|
||||
# ---------------------------------------------------------------------------------------
|
||||
# Код возврата: 0
|
||||
# Время выполнения: 36.72 с
|
||||
@@ -0,0 +1,10 @@
|
||||
# Дата (UTC): 2026-10-23 10:18:12
|
||||
# Commit: 65003b6
|
||||
# Команда: grep -n -i password\|secret compose.yaml
|
||||
# ---------------------------------------- вывод ----------------------------------------
|
||||
13: DATABASE_URL: postgresql+asyncpg://${POSTGRES_USER:?задайте POSTGRES_USER в файле .env}:${POSTGRES_PASSWORD:?задайте POSTGRES_PASSWORD в файле .env}@db:5432/${POSTGRES_DB:?задайте POSTGRES_DB в файле .env}
|
||||
25: POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?задайте POSTGRES_PASSWORD в файле .env}
|
||||
102: TEST_DB_PASSWORD: ${POSTGRES_PASSWORD:?задайте POSTGRES_PASSWORD в файле .env}
|
||||
# ---------------------------------------------------------------------------------------
|
||||
# Код возврата: 0
|
||||
# Время выполнения: 0.00 с
|
||||
@@ -0,0 +1,113 @@
|
||||
# Дата (UTC): 2026-10-23 10:18:12
|
||||
# Commit: 65003b6
|
||||
# Команда: git ls-files --cached --others --exclude-standard
|
||||
# ---------------------------------------- вывод ----------------------------------------
|
||||
Dockerfile
|
||||
EVIDENCE.md
|
||||
GUIDE.md
|
||||
OPERATIONS.md
|
||||
app/__init__.py
|
||||
app/__main__.py
|
||||
app/auth.py
|
||||
app/cache.py
|
||||
app/config.py
|
||||
app/crud.py
|
||||
app/cursor.py
|
||||
app/db.py
|
||||
app/errors.py
|
||||
app/explain.py
|
||||
app/healthcheck.py
|
||||
app/http_utils.py
|
||||
app/logging_setup.py
|
||||
app/main.py
|
||||
app/middleware.py
|
||||
app/migrate.py
|
||||
app/migrations/0001_init.sql
|
||||
app/models.py
|
||||
app/routes.py
|
||||
app/schemas.py
|
||||
app/security.py
|
||||
app/timeutil.py
|
||||
compose.yaml
|
||||
env.lab2.template
|
||||
evidence/01_check_contract.txt
|
||||
evidence/02_smoke.txt
|
||||
evidence/03_ps_before.txt
|
||||
evidence/03_volume_before.txt
|
||||
evidence/04_persistence.txt
|
||||
evidence/05_ps_after.txt
|
||||
evidence/05_volume_after.txt
|
||||
evidence/06_schema.txt
|
||||
evidence/07_migrations.txt
|
||||
evidence/07_migrations_first.txt
|
||||
evidence/07_migrations_table.txt
|
||||
evidence/08_hashes.txt
|
||||
evidence/09_auth_tests.txt
|
||||
evidence/10_database_tests.txt
|
||||
evidence/11_all_tests.txt
|
||||
evidence/12_compose_no_secrets.txt
|
||||
evidence/12_git_files.txt
|
||||
evidence/12_git_secrets.txt
|
||||
evidence/13_storage.txt
|
||||
evidence/14_dependencies.txt
|
||||
evidence/15_explain.txt
|
||||
evidence/15_explain_tests.txt
|
||||
evidence/16_expiry_live.txt
|
||||
pytest.ini
|
||||
requirements-test.txt
|
||||
requirements.txt
|
||||
scripts/check_expiry.py
|
||||
scripts/collect_evidence.sh
|
||||
scripts/dependency_demo.py
|
||||
scripts/restart.bat
|
||||
scripts/restart.sh
|
||||
scripts/run_and_save.py
|
||||
scripts/storage_demo.py
|
||||
student-tests/conftest.py
|
||||
student-tests/test_auth.py
|
||||
student-tests/test_cache.py
|
||||
student-tests/test_concurrency.py
|
||||
student-tests/test_contract_basics.py
|
||||
student-tests/test_database.py
|
||||
student-tests/test_db_failure.py
|
||||
student-tests/test_duplicate_username.py
|
||||
student-tests/test_empty_message.py
|
||||
student-tests/test_explain.py
|
||||
student-tests/test_foreign_chat.py
|
||||
student-tests/test_invalid_json.py
|
||||
student-tests/test_pagination.py
|
||||
student-tests/test_session_expiry.py
|
||||
student-tests/test_transactions.py
|
||||
.dockerignore
|
||||
.env.example
|
||||
.github/pull_request_template.md
|
||||
.github/workflows/contracts.yml
|
||||
.gitignore
|
||||
CONTRIBUTING.md
|
||||
COURSE.md
|
||||
Dockerfile.example
|
||||
MAINTAINERS.md
|
||||
Makefile
|
||||
README.md
|
||||
REPORT.md
|
||||
compose.example.yaml
|
||||
contracts/README.md
|
||||
contracts/examples.json
|
||||
contracts/infrastructure.json
|
||||
contracts/openapi.json
|
||||
evidence/README.md
|
||||
examples/requests.http
|
||||
infra/README.md
|
||||
lab.json
|
||||
requirements-dev.txt
|
||||
scripts/check_contract.py
|
||||
scripts/restart.example.sh
|
||||
scripts/validate_openapi.py
|
||||
tests/README.md
|
||||
tests/client.py
|
||||
tests/load.py
|
||||
tests/resilience.py
|
||||
tests/smoke.py
|
||||
# ---------------------------------------------------------------------------------------
|
||||
# Код возврата: 0
|
||||
# Время выполнения: 0.00 с
|
||||
@@ -0,0 +1,8 @@
|
||||
# Дата (UTC): 2026-10-23 10:18:12
|
||||
# Commit: 65003b6
|
||||
# Команда: git check-ignore -v .env
|
||||
# ---------------------------------------- вывод ----------------------------------------
|
||||
.gitignore:2:.env .env
|
||||
# ---------------------------------------------------------------------------------------
|
||||
# Код возврата: 0
|
||||
# Время выполнения: 0.00 с
|
||||
@@ -0,0 +1,194 @@
|
||||
# Дата (UTC): 2026-10-23 10:18:23
|
||||
# Commit: 65003b6
|
||||
# Команда: python scripts/storage_demo.py --mode down
|
||||
|
||||
|
||||
==============================================================================
|
||||
1. Как устроено приложение
|
||||
==============================================================================
|
||||
Приложение -- мессенджер: HTTP API (FastAPI), PostgreSQL и Redis (кэш сессий).
|
||||
* Сам API состояния не хранит: пользователи, чаты, участники, сообщения и сессии
|
||||
лежат в PostgreSQL (таблицы users, chats, chat_members, messages, sessions).
|
||||
* Файлы PostgreSQL лежат не в контейнере, а в именованном томе Docker (pgdata).
|
||||
Контейнеры api и db временные, том постоянный.
|
||||
* Пароль хранится как scrypt-хеш, у сессии в БД лежит только SHA-256 токена,
|
||||
срок сессии считает сама БД -- перезапуск его не продлевает и не сбрасывает.
|
||||
* Redis только кэширует проверку токена (без persistence): после перезапуска
|
||||
кэш пуст, и сессии читаются из PostgreSQL.
|
||||
Ожидание: пока том не удалён (команда down -v здесь НЕ используется), после
|
||||
удаления и повторного создания контейнеров (docker compose down, затем up -d)
|
||||
те же пользователи, чат, сообщение и тот же токен продолжают работать.
|
||||
План: (1) создать данные через API, (2) показать их в БД, (3) выключить,
|
||||
(4) включить, (5) проверить, что данные и токены прежние.
|
||||
|
||||
|
||||
==============================================================================
|
||||
2. Состояние до начала
|
||||
==============================================================================
|
||||
$ docker compose --env-file .env ps
|
||||
NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
|
||||
messenger-api mtusi-messenger:lab2 "python -m app" api About a minute ago Up 59 seconds (healthy) 127.0.0.1:8080->8080/tcp
|
||||
messenger-db postgres:16.6-bookworm "docker-entrypoint.s…" db About a minute ago Up About a minute (healthy) 127.0.0.1:5433->5432/tcp
|
||||
messenger-redis redis:7.4.1-bookworm "docker-entrypoint.s…" redis About a minute ago Up About a minute (healthy) 6379/tcp
|
||||
$ docker volume ls --filter name=pgdata --format {{.Name}}
|
||||
mtusi-messenger_pgdata
|
||||
$ docker volume inspect mtusi-messenger_pgdata --format "{{.Name}} создан: {{.CreatedAt}}"
|
||||
mtusi-messenger_pgdata создан: 2026-10-23T13:16:41+03:00
|
||||
|
||||
==============================================================================
|
||||
3. Запись данных через API (создаём пользователей, чат, сообщение)
|
||||
==============================================================================
|
||||
$ POST /api/v1/users # регистрация Алисы
|
||||
тело запроса: {"username": "demo_a_7681083c", "display_name": "Алиса", "password": "***"}
|
||||
ответ: 201 {"id": "a0fce947-1cb5-4780-ba66-daddb74fadea", "username": "demo_a_7681083c", "display_name": "Алиса", "created_at": "2026-10-23T10:18:12.849116Z"}
|
||||
$ POST /api/v1/users # регистрация Боба
|
||||
тело запроса: {"username": "demo_b_7681083c", "display_name": "Боб", "password": "***"}
|
||||
ответ: 201 {"id": "2c34c828-f3c2-46ae-8055-bfaa49de3789", "username": "demo_b_7681083c", "display_name": "Боб", "created_at": "2026-10-23T10:18:12.903720Z"}
|
||||
$ POST /api/v1/auth/sessions # вход Алисы (Basic)
|
||||
ответ: 201 {"token": "<скрыт, 43 симв.>", "token_type": "Bearer", "expires_at": "2026-10-23T11:18:12.910850Z", "user": {"id": "a0fce947-1cb5-4780-ba66-daddb74fadea", "username": "demo_a_7681083c", "display_name": "Алиса", "created_at": "2026-10-23T10:18:12.849116Z"}}
|
||||
$ POST /api/v1/auth/sessions # вход Боба (Basic)
|
||||
ответ: 201 {"token": "<скрыт, 43 симв.>", "token_type": "Bearer", "expires_at": "2026-10-23T11:18:12.961660Z", "user": {"id": "2c34c828-f3c2-46ae-8055-bfaa49de3789", "username": "demo_b_7681083c", "display_name": "Боб", "created_at": "2026-10-23T10:18:12.903720Z"}}
|
||||
$ POST /api/v1/chats # чат Алисы и Боба (Bearer)
|
||||
тело запроса: {"title": "Проверка сохранности 7681083c", "member_ids": ["a0fce947-1cb5-4780-ba66-daddb74fadea", "2c34c828-f3c2-46ae-8055-bfaa49de3789"]}
|
||||
ответ: 201 {"id": "051a293a-c75d-45f5-b4ab-7e24ef0833f4", "title": "Проверка сохранности 7681083c", "member_ids": ["2c34c828-f3c2-46ae-8055-bfaa49de3789", "a0fce947-1cb5-4780-ba66-daddb74fadea"], "created_at": "2026-10-23T10:18:13.013118Z"}
|
||||
$ POST /api/v1/chats/051a293a-c75d-45f5-b4ab-7e24ef0833f4/messages # сообщение Алисы
|
||||
тело запроса: {"text": "Это сообщение должно пережить перезапуск 7681083c"}
|
||||
ответ: 201 {"id": "8d7c0e89-8043-413d-b3f0-d6bca0062b3e", "chat_id": "051a293a-c75d-45f5-b4ab-7e24ef0833f4", "sender_id": "a0fce947-1cb5-4780-ba66-daddb74fadea", "text": "Это сообщение должно пережить перезапуск 7681083c", "created_at": "2026-10-23T10:18:13.025076Z"}
|
||||
|
||||
==============================================================================
|
||||
4. Что лежит в PostgreSQL ДО выключения (SELECT внутри контейнера db)
|
||||
==============================================================================
|
||||
-- пользователи (пароль -- только префикс хеша)
|
||||
$ docker compose --env-file .env exec -T db psql -U messenger -d messenger -c "SELECT username, display_name, left(password_hash, 7) AS password_prefix, created_at FROM users WHERE username IN ('demo_a_7681083c', 'demo_b_7681083c') ORDER BY username"
|
||||
username | display_name | password_prefix | created_at
|
||||
-----------------+--------------+-----------------+-------------------------------
|
||||
demo_a_7681083c | Алиса | scrypt$ | 2026-10-23 10:18:12.849116+00
|
||||
demo_b_7681083c | Боб | scrypt$ | 2026-10-23 10:18:12.90372+00
|
||||
(2 rows)
|
||||
-- чат и число участников
|
||||
$ docker compose --env-file .env exec -T db psql -U messenger -d messenger -c "SELECT c.title, count(m.user_id) AS members FROM chats c JOIN chat_members m ON m.chat_id = c.id WHERE c.id = '051a293a-c75d-45f5-b4ab-7e24ef0833f4' GROUP BY c.title"
|
||||
title | members
|
||||
-------------------------------+---------
|
||||
Проверка сохранности 7681083c | 2
|
||||
(1 row)
|
||||
-- сообщение
|
||||
$ docker compose --env-file .env exec -T db psql -U messenger -d messenger -c "SELECT id, sender_id, text, created_at FROM messages WHERE chat_id = '051a293a-c75d-45f5-b4ab-7e24ef0833f4'"
|
||||
id | sender_id | text | created_at
|
||||
--------------------------------------+--------------------------------------+---------------------------------------------------+-------------------------------
|
||||
8d7c0e89-8043-413d-b3f0-d6bca0062b3e | a0fce947-1cb5-4780-ba66-daddb74fadea | Это сообщение должно пережить перезапуск 7681083c | 2026-10-23 10:18:13.025076+00
|
||||
(1 row)
|
||||
-- действующие сессии (токены не показываем)
|
||||
$ docker compose --env-file .env exec -T db psql -U messenger -d messenger -c "SELECT count(*) AS live_sessions FROM sessions s JOIN users u ON u.id = s.user_id WHERE u.username IN ('demo_a_7681083c', 'demo_b_7681083c') AND s.expires_at > now()"
|
||||
live_sessions
|
||||
---------------
|
||||
2
|
||||
(1 row)
|
||||
|
||||
==============================================================================
|
||||
5. Выключаем приложение (контейнеры УДАЛЯЮТСЯ, том остаётся)
|
||||
==============================================================================
|
||||
$ docker compose --env-file .env down
|
||||
Container messenger-api Stopping
|
||||
Container messenger-api Stopped
|
||||
Container messenger-api Removing
|
||||
Container messenger-api Removed
|
||||
Container messenger-db Stopping
|
||||
Container messenger-redis Stopping
|
||||
Container messenger-redis Stopped
|
||||
Container messenger-redis Removing
|
||||
Container messenger-redis Removed
|
||||
Container messenger-db Stopped
|
||||
Container messenger-db Removing
|
||||
Container messenger-db Removed
|
||||
Network mtusi-messenger_default Removing
|
||||
Network mtusi-messenger_default Removed
|
||||
$ docker compose --env-file .env ps -a
|
||||
NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
|
||||
API на http://localhost:8080 не отвечает -- приложение выключено (так и должно быть).
|
||||
-- том с данными на месте:
|
||||
$ docker volume ls --filter name=pgdata
|
||||
DRIVER VOLUME NAME
|
||||
local mtusi-messenger_pgdata
|
||||
|
||||
==============================================================================
|
||||
6. Включаем снова
|
||||
==============================================================================
|
||||
$ docker compose --env-file .env up -d
|
||||
Network mtusi-messenger_default Creating
|
||||
Network mtusi-messenger_default Creating
|
||||
Network mtusi-messenger_default Created
|
||||
Network mtusi-messenger_default Created
|
||||
Container messenger-db Creating
|
||||
Container messenger-redis Creating
|
||||
Container messenger-redis Created
|
||||
Container messenger-db Created
|
||||
Container messenger-api Creating
|
||||
Container messenger-api Created
|
||||
Container messenger-db Starting
|
||||
Container messenger-redis Starting
|
||||
Container messenger-db Started
|
||||
Container messenger-redis Started
|
||||
Container messenger-db Waiting
|
||||
Container messenger-db Healthy
|
||||
Container messenger-api Starting
|
||||
Container messenger-api Started
|
||||
API снова готов (/health/ready = 200) через 2 с.
|
||||
$ docker compose --env-file .env ps
|
||||
NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
|
||||
messenger-api mtusi-messenger:lab2 "python -m app" api 7 seconds ago Up 2 seconds (health: starting) 127.0.0.1:8080->8080/tcp
|
||||
messenger-db postgres:16.6-bookworm "docker-entrypoint.s…" db 7 seconds ago Up 7 seconds (healthy) 127.0.0.1:5433->5432/tcp
|
||||
messenger-redis redis:7.4.1-bookworm "docker-entrypoint.s…" redis 7 seconds ago Up 7 seconds (healthy) 6379/tcp
|
||||
|
||||
==============================================================================
|
||||
7. Проверка ПОСЛЕ перезапуска: те же данные и те же токены
|
||||
==============================================================================
|
||||
$ GET /api/v1/users/me # старый токен Алисы, повторный вход НЕ делали
|
||||
ответ: 200 {"id": "a0fce947-1cb5-4780-ba66-daddb74fadea", "username": "demo_a_7681083c", "display_name": "Алиса", "created_at": "2026-10-23T10:18:12.849116Z"}
|
||||
OK: тот же пользователь (id, username, created_at совпали).
|
||||
$ GET /api/v1/chats/051a293a-c75d-45f5-b4ab-7e24ef0833f4/messages # старый токен Боба читает историю чата
|
||||
ответ: 200 {"items": [{"id": "8d7c0e89-8043-413d-b3f0-d6bca0062b3e", "chat_id": "051a293a-c75d-45f5-b4ab-7e24ef0833f4", "sender_id": "a0fce947-1cb5-4780-ba66-daddb74fadea", "text": "Это сообщение должно пережить перезапуск 7681083c", "created_at": "2026-10-23T10:18:13.025076Z"}], "next_cursor": null}
|
||||
OK: то же сообщение (id, text, created_at совпали).
|
||||
$ GET /api/v1/chats?limit=100 # список чатов Алисы
|
||||
ответ: 200 {"items": [{"id": "051a293a-c75d-45f5-b4ab-7e24ef0833f4", "title": "Проверка сохранности 7681083c", "member_ids": ["2c34c828-f3c2-46ae-8055-bfaa49de3789", "a0fce947-1cb5-4780-ba66-daddb74fadea"], "created_at": "2026-10-23T10:18:13.013118Z"}], "next_cursor": null}
|
||||
OK: чат в списке.
|
||||
|
||||
Те же SELECT после перезапуска (сравниваем с выводом из пункта 4):
|
||||
-- пользователи (пароль -- только префикс хеша)
|
||||
$ docker compose --env-file .env exec -T db psql -U messenger -d messenger -c "SELECT username, display_name, left(password_hash, 7) AS password_prefix, created_at FROM users WHERE username IN ('demo_a_7681083c', 'demo_b_7681083c') ORDER BY username"
|
||||
username | display_name | password_prefix | created_at
|
||||
-----------------+--------------+-----------------+-------------------------------
|
||||
demo_a_7681083c | Алиса | scrypt$ | 2026-10-23 10:18:12.849116+00
|
||||
demo_b_7681083c | Боб | scrypt$ | 2026-10-23 10:18:12.90372+00
|
||||
(2 rows)
|
||||
OK: вывод идентичен выводу до перезапуска.
|
||||
-- чат и число участников
|
||||
$ docker compose --env-file .env exec -T db psql -U messenger -d messenger -c "SELECT c.title, count(m.user_id) AS members FROM chats c JOIN chat_members m ON m.chat_id = c.id WHERE c.id = '051a293a-c75d-45f5-b4ab-7e24ef0833f4' GROUP BY c.title"
|
||||
title | members
|
||||
-------------------------------+---------
|
||||
Проверка сохранности 7681083c | 2
|
||||
(1 row)
|
||||
OK: вывод идентичен выводу до перезапуска.
|
||||
-- сообщение
|
||||
$ docker compose --env-file .env exec -T db psql -U messenger -d messenger -c "SELECT id, sender_id, text, created_at FROM messages WHERE chat_id = '051a293a-c75d-45f5-b4ab-7e24ef0833f4'"
|
||||
id | sender_id | text | created_at
|
||||
--------------------------------------+--------------------------------------+---------------------------------------------------+-------------------------------
|
||||
8d7c0e89-8043-413d-b3f0-d6bca0062b3e | a0fce947-1cb5-4780-ba66-daddb74fadea | Это сообщение должно пережить перезапуск 7681083c | 2026-10-23 10:18:13.025076+00
|
||||
(1 row)
|
||||
OK: вывод идентичен выводу до перезапуска.
|
||||
-- действующие сессии (токены не показываем)
|
||||
$ docker compose --env-file .env exec -T db psql -U messenger -d messenger -c "SELECT count(*) AS live_sessions FROM sessions s JOIN users u ON u.id = s.user_id WHERE u.username IN ('demo_a_7681083c', 'demo_b_7681083c') AND s.expires_at > now()"
|
||||
live_sessions
|
||||
---------------
|
||||
2
|
||||
(1 row)
|
||||
OK: вывод идентичен выводу до перезапуска.
|
||||
|
||||
-- том тот же (дата создания не изменилась):
|
||||
$ docker volume inspect mtusi-messenger_pgdata --format "{{.Name}} создан: {{.CreatedAt}}"
|
||||
mtusi-messenger_pgdata создан: 2026-10-23T13:16:41+03:00
|
||||
OK: том не пересоздавался.
|
||||
|
||||
==============================================================================
|
||||
ИТОГ
|
||||
==============================================================================
|
||||
ИТОГ: PASS -- пользователи, чат, сообщение и действующие сессии сохранились после удаления и повторного создания контейнеров (docker compose down, затем up -d).
|
||||
@@ -0,0 +1,323 @@
|
||||
# Дата (UTC): 2026-10-23 10:18:23
|
||||
# Commit: 65003b6
|
||||
# Команда: python3 scripts/dependency_demo.py
|
||||
|
||||
==============================================================================
|
||||
0. Контейнеры
|
||||
==============================================================================
|
||||
$ docker compose --env-file .env ps
|
||||
NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
|
||||
messenger-api mtusi-messenger:lab2 "python -m app" api 8 seconds ago Up 2 seconds (health: starting) 127.0.0.1:8080->8080/tcp
|
||||
messenger-db postgres:16.6-bookworm "docker-entrypoint.s…" db 8 seconds ago Up 8 seconds (healthy) 127.0.0.1:5433->5432/tcp
|
||||
messenger-redis redis:7.4.1-bookworm "docker-entrypoint.s…" redis 8 seconds ago Up 8 seconds (healthy) 6379/tcp
|
||||
|
||||
==============================================================================
|
||||
1. Redis: версия и настройки (кэш, persistence выключена намеренно)
|
||||
==============================================================================
|
||||
$ docker compose --env-file .env exec -T redis redis-server --version
|
||||
Redis server v=7.4.1 sha=00000000:0 malloc=jemalloc-5.3.0 bits=64 build=9ce9f16f947e6690
|
||||
$ docker compose --env-file .env exec -T redis redis-cli CONFIG GET save
|
||||
save
|
||||
$ docker compose --env-file .env exec -T redis redis-cli CONFIG GET appendonly
|
||||
appendonly
|
||||
no
|
||||
$ docker compose --env-file .env exec -T redis redis-cli CONFIG GET maxmemory-policy
|
||||
maxmemory-policy
|
||||
noeviction
|
||||
|
||||
==============================================================================
|
||||
2. Роль кэша: проверка Bearer-токена без запроса к PostgreSQL
|
||||
==============================================================================
|
||||
POST /api/v1/users -> 201 (57 мс) # регистрация
|
||||
{"id": "2454e675-039f-49b3-92f0-e2e492e0cefc", "username": "alice_a3a3d232", "display_name": "alice", "created_at": "2026-10-23T10:18:24.048024Z"}
|
||||
POST /api/v1/auth/sessions -> 201 (52 мс) # вход (Basic)
|
||||
{"token": "<скрыт>", "token_type": "Bearer", "expires_at": "2026-10-23T11:18:24.063082Z", "user": {"id": "2454e675-039f-49b3-92f0-e2e492e0cefc", "username": "alice_a3a3d232", "display_name": "alice", "created_at": "2026-10-23T10:18:24.048024Z"}}
|
||||
POST /api/v1/users -> 201 (47 мс) # регистрация
|
||||
{"id": "5430982a-788d-47f8-a794-b135476578df", "username": "bob_e2ebd6fa", "display_name": "bob", "created_at": "2026-10-23T10:18:24.154704Z"}
|
||||
POST /api/v1/auth/sessions -> 201 (48 мс) # вход (Basic)
|
||||
{"token": "<скрыт>", "token_type": "Bearer", "expires_at": "2026-10-23T11:18:24.161175Z", "user": {"id": "5430982a-788d-47f8-a794-b135476578df", "username": "bob_e2ebd6fa", "display_name": "bob", "created_at": "2026-10-23T10:18:24.154704Z"}}
|
||||
$ docker compose --env-file .env exec -T redis redis-cli CONFIG RESETSTAT
|
||||
OK
|
||||
$ docker compose --env-file .env exec -T db psql -U messenger -d messenger -At -c "SELECT idx_scan FROM pg_stat_user_indexes WHERE indexrelname = 'sessions_pkey'"
|
||||
10
|
||||
50 x GET /api/v1/users/me -> 200; первый 4.1 мс, медиана остальных 1.7 мс
|
||||
$ docker compose --env-file .env exec -T db psql -U messenger -d messenger -At -c "SELECT idx_scan FROM pg_stat_user_indexes WHERE indexrelname = 'sessions_pkey'"
|
||||
10
|
||||
Чтений индекса sessions_pkey в PostgreSQL за 50 запросов: 0
|
||||
$ docker compose --env-file .env exec -T redis redis-cli INFO stats
|
||||
# Stats
|
||||
total_connections_received:2
|
||||
total_commands_processed:53
|
||||
instantaneous_ops_per_sec:32
|
||||
total_net_input_bytes:4910
|
||||
total_net_output_bytes:7470
|
||||
total_net_repl_input_bytes:0
|
||||
total_net_repl_output_bytes:0
|
||||
instantaneous_input_kbps:2.96
|
||||
instantaneous_output_kbps:4.53
|
||||
instantaneous_input_repl_kbps:0.00
|
||||
instantaneous_output_repl_kbps:0.00
|
||||
rejected_connections:0
|
||||
sync_full:0
|
||||
sync_partial_ok:0
|
||||
sync_partial_err:0
|
||||
expired_subkeys:0
|
||||
expired_keys:0
|
||||
expired_stale_perc:0.00
|
||||
expired_time_cap_reached_count:0
|
||||
expire_cycle_cpu_milliseconds:0
|
||||
evicted_keys:0
|
||||
evicted_clients:0
|
||||
evicted_scripts:0
|
||||
total_eviction_exceeded_time:0
|
||||
current_eviction_exceeded_time:0
|
||||
keyspace_hits:49
|
||||
keyspace_misses:1
|
||||
pubsub_channels:0
|
||||
pubsub_patterns:0
|
||||
pubsubshard_channels:0
|
||||
latest_fork_usec:0
|
||||
total_forks:0
|
||||
migrate_cached_sockets:0
|
||||
slave_expires_tracked_keys:0
|
||||
active_defrag_hits:0
|
||||
active_defrag_misses:0
|
||||
active_defrag_key_hits:0
|
||||
active_defrag_key_misses:0
|
||||
total_active_defrag_time:0
|
||||
current_active_defrag_time:0
|
||||
tracking_total_keys:0
|
||||
tracking_total_items:0
|
||||
tracking_total_prefixes:0
|
||||
unexpected_error_replies:0
|
||||
total_error_replies:0
|
||||
dump_payload_sanitizations:0
|
||||
total_reads_processed:55
|
||||
total_writes_processed:53
|
||||
io_threaded_reads_processed:0
|
||||
io_threaded_writes_processed:0
|
||||
client_query_buffer_limit_disconnections:0
|
||||
client_output_buffer_limit_disconnections:0
|
||||
reply_buffer_shrinks:0
|
||||
reply_buffer_expands:0
|
||||
eventloop_cycles:81
|
||||
eventloop_duration_sum:14650
|
||||
eventloop_duration_cmd_sum:128
|
||||
instantaneous_eventloop_cycles_per_sec:42
|
||||
instantaneous_eventloop_duration_usec:452
|
||||
acl_access_denied_auth:0
|
||||
acl_access_denied_cmd:0
|
||||
acl_access_denied_key:0
|
||||
acl_access_denied_channel:0
|
||||
Redis: keyspace_hits=49 keyspace_misses=1
|
||||
$ docker compose --env-file .env exec -T redis redis-cli PTTL session:<sha256 токена>
|
||||
58542
|
||||
TTL ключа сессии: 58542 мс (<= CACHE_TTL_SECONDS=60 с и <= остатка сессии)
|
||||
$ docker compose --env-file .env exec -T redis redis-cli GET session:<sha256 токена>
|
||||
{"id":"2454e675-039f-49b3-92f0-e2e492e0cefc","username":"alice_a3a3d232","display_name":"alice","created_at":"2026-10-23T10:18:24.048024+00:00"}
|
||||
|
||||
==============================================================================
|
||||
3. Logout: DELETE в PostgreSQL, затем отметка отзыва в Redis
|
||||
==============================================================================
|
||||
POST /api/v1/users -> 201 (54 мс) # регистрация
|
||||
{"id": "b3b307ec-a474-451c-a347-51450f51fec8", "username": "carol_e6a6b17a", "display_name": "carol", "created_at": "2026-10-23T10:18:27.081509Z"}
|
||||
POST /api/v1/auth/sessions -> 201 (49 мс) # вход (Basic)
|
||||
{"token": "<скрыт>", "token_type": "Bearer", "expires_at": "2026-10-23T11:18:27.095801Z", "user": {"id": "b3b307ec-a474-451c-a347-51450f51fec8", "username": "carol_e6a6b17a", "display_name": "carol", "created_at": "2026-10-23T10:18:27.081509Z"}}
|
||||
GET /api/v1/users/me -> 200 (4 мс) # кладёт сессию в кэш
|
||||
{"id": "b3b307ec-a474-451c-a347-51450f51fec8", "username": "carol_e6a6b17a", "display_name": "carol", "created_at": "2026-10-23T10:18:27.081509Z"}
|
||||
DELETE /api/v1/auth/sessions/current -> 204 (7 мс)
|
||||
(пусто)
|
||||
$ docker compose --env-file .env exec -T redis redis-cli GET session:<sha256 токена>
|
||||
revoked
|
||||
GET /api/v1/users/me -> 401 (2 мс) # отозванный токен
|
||||
{"error": {"code": "unauthorized", "message": "Нет действительной аутентификации", "request_id": "5137ca7dc5464437bdaa45e85bb17e84"}}
|
||||
|
||||
==============================================================================
|
||||
4. Рестарт Redis: кэш пуст, сессии живы (источник истины -- PostgreSQL)
|
||||
==============================================================================
|
||||
$ docker compose --env-file .env restart redis
|
||||
Container messenger-redis Restarting
|
||||
Container messenger-redis Started
|
||||
redis healthy через 5.4 с
|
||||
$ docker compose --env-file .env exec -T redis redis-cli DBSIZE
|
||||
0
|
||||
GET /api/v1/users/me -> 200 (5 мс) # тот же токен
|
||||
{"id": "2454e675-039f-49b3-92f0-e2e492e0cefc", "username": "alice_a3a3d232", "display_name": "alice", "created_at": "2026-10-23T10:18:24.048024Z"}
|
||||
$ docker compose --env-file .env exec -T redis redis-cli EXISTS session:<sha256 токена>
|
||||
1
|
||||
GET /api/v1/users/me -> 401 (4 мс) # отозванный токен после потери кэша всё равно 401 (решает PostgreSQL)
|
||||
{"error": {"code": "unauthorized", "message": "Нет действительной аутентификации", "request_id": "aed4a94de8cf4ebfb495e3b35ae51bc3"}}
|
||||
|
||||
==============================================================================
|
||||
5. Redis ОСТАНОВЛЕН: API обходит кэш
|
||||
==============================================================================
|
||||
$ docker compose --env-file .env stop redis
|
||||
Container messenger-redis Stopping
|
||||
Container messenger-redis Stopped
|
||||
GET /health/live -> 200 (1 мс)
|
||||
{"status": "ok"}
|
||||
GET /health/ready -> 200 (2 мс) # Redis не критичен для обязательного пути
|
||||
{"status": "ok"}
|
||||
GET /api/v1/users/me -> 200 (307 мс)
|
||||
{"id": "2454e675-039f-49b3-92f0-e2e492e0cefc", "username": "alice_a3a3d232", "display_name": "alice", "created_at": "2026-10-23T10:18:24.048024Z"}
|
||||
POST /api/v1/chats -> 201 (21 мс)
|
||||
{"id": "515465df-f215-49f0-9548-55d81bf82331", "title": "без Redis", "member_ids": ["2454e675-039f-49b3-92f0-e2e492e0cefc", "5430982a-788d-47f8-a794-b135476578df"], "created_at": "2026-10-23T10:18:33.810751Z"}
|
||||
POST /api/v1/chats/515465df-f215-49f0-9548-55d81bf82331/messages -> 201 (10 мс)
|
||||
{"id": "ebd92a19-9f52-41c2-a883-6ecf0fa880b1", "chat_id": "515465df-f215-49f0-9548-55d81bf82331", "sender_id": "2454e675-039f-49b3-92f0-e2e492e0cefc", "text": "сообщение при остановленном Redis", "created_at": "2026-10-23T10:18:33.830951Z"}
|
||||
GET /api/v1/chats/515465df-f215-49f0-9548-55d81bf82331/messages -> 200 (5 мс)
|
||||
{"items": [{"id": "ebd92a19-9f52-41c2-a883-6ecf0fa880b1", "chat_id": "515465df-f215-49f0-9548-55d81bf82331", "sender_id": "2454e675-039f-49b3-92f0-e2e492e0cefc", "text": "сообщение при остановленном Redis", "created_at": "2026-10-23T10:18:33.830951Z"}], "next_cursor": null}
|
||||
GET /api/v1/users/me -> 401 (2 мс) # без токена -- 401, не доверяем
|
||||
{"error": {"code": "unauthorized", "message": "Нет действительной аутентификации", "request_id": "9726e3049265430abcbbf1370d866d0e"}}
|
||||
GET /api/v1/users/me -> 401 (4 мс) # чужой токен -- 401
|
||||
{"error": {"code": "unauthorized", "message": "Нет действительной аутентификации", "request_id": "f187d7d2bd65451ba37797de881c940f"}}
|
||||
POST /api/v1/users -> 201 (51 мс) # регистрация
|
||||
{"id": "ff7f3a4f-e69f-465b-a2ae-3a38b9571bc8", "username": "dave_3786d2ec", "display_name": "dave", "created_at": "2026-10-23T10:18:33.890145Z"}
|
||||
POST /api/v1/auth/sessions -> 201 (49 мс) # вход (Basic)
|
||||
{"token": "<скрыт>", "token_type": "Bearer", "expires_at": "2026-10-23T11:18:33.898871Z", "user": {"id": "ff7f3a4f-e69f-465b-a2ae-3a38b9571bc8", "username": "dave_3786d2ec", "display_name": "dave", "created_at": "2026-10-23T10:18:33.890145Z"}}
|
||||
GET /api/v1/users/me -> 200 (3 мс)
|
||||
{"id": "ff7f3a4f-e69f-465b-a2ae-3a38b9571bc8", "username": "dave_3786d2ec", "display_name": "dave", "created_at": "2026-10-23T10:18:33.890145Z"}
|
||||
DELETE /api/v1/auth/sessions/current -> 204 (7 мс) # logout при недоступном Redis
|
||||
(пусто)
|
||||
GET /api/v1/users/me -> 401 (3 мс)
|
||||
{"error": {"code": "unauthorized", "message": "Нет действительной аутентификации", "request_id": "afc3e9b8832241068e49c15a5f255647"}}
|
||||
Максимальная задержка запросов без Redis: 307 мс
|
||||
$ docker compose --env-file .env start redis
|
||||
Container messenger-redis Starting
|
||||
Container messenger-redis Started
|
||||
redis healthy через 5.4 с
|
||||
GET /api/v1/users/me -> 200 (21 мс) # кэш снова в работе
|
||||
{"id": "2454e675-039f-49b3-92f0-e2e492e0cefc", "username": "alice_a3a3d232", "display_name": "alice", "created_at": "2026-10-23T10:18:24.048024Z"}
|
||||
GET /api/v1/users/me -> 401 (14 мс)
|
||||
{"error": {"code": "unauthorized", "message": "Нет действительной аутентификации", "request_id": "9b50d84c22434a8ea61d0e642afb75d7"}}
|
||||
$ docker compose --env-file .env logs --since 2m api
|
||||
messenger-api | 2026-10-23T10:18:21+0000 INFO listen host=0.0.0.0 port=8080
|
||||
messenger-api | INFO: Started server process [1]
|
||||
messenger-api | INFO: Waiting for application startup.
|
||||
messenger-api | 2026-10-23T10:18:21+0000 INFO config env=lab pool=5+5 ttl=3600s store=postgres cache=redis
|
||||
messenger-api | 2026-10-23T10:18:21+0000 INFO migrations: nothing to apply
|
||||
messenger-api | 2026-10-23T10:18:21+0000 INFO startup: приложение готово
|
||||
messenger-api | INFO: Application startup complete.
|
||||
messenger-api | INFO: Uvicorn running on http://0.0.0.0:8080 (Press CTRL+C to quit)
|
||||
messenger-api | 2026-10-23T10:18:22+0000 INFO request request_id=ab0d003663564a1b9dc29362448eba2a method=GET path=/health/ready status=200 duration_ms=7.1
|
||||
messenger-api | 2026-10-23T10:18:22+0000 INFO request request_id=5ef96f93ebaa48f492ed2cc18ae57cef method=GET path=/api/v1/users/me status=200 duration_ms=15.0
|
||||
messenger-api | 2026-10-23T10:18:22+0000 INFO request request_id=3c763fd0b51641a8b6e78e67b93713dc method=GET path=/api/v1/chats/051a293a-c75d-45f5-b4ab-7e24ef0833f4/messages status=200 duration_ms=8.5
|
||||
messenger-api | 2026-10-23T10:18:22+0000 INFO request request_id=028499cc5fea4ab6806c129125221538 method=GET path=/api/v1/chats status=200 duration_ms=5.5
|
||||
messenger-api | 2026-10-23T10:18:23+0000 INFO request request_id=fcded80bfe3747828ad334eb67bf452a method=GET path=/health/ready status=200 duration_ms=2.1
|
||||
messenger-api | 2026-10-23T10:18:23+0000 INFO request request_id=ce4a44fa17fa419385d393eab8eda558 method=GET path=/health/ready status=200 duration_ms=2.0
|
||||
messenger-api | 2026-10-23T10:18:24+0000 INFO request request_id=a327a13994254a5e8695f8065c0052c7 method=POST path=/api/v1/users status=201 duration_ms=55.9
|
||||
messenger-api | 2026-10-23T10:18:24+0000 INFO request request_id=1bac32dcbdc9426981c93b4b1fc2a6a7 method=POST path=/api/v1/auth/sessions status=201 duration_ms=50.6
|
||||
messenger-api | 2026-10-23T10:18:24+0000 INFO request request_id=2ad87a6b1d714522b6ce473c3d03a88e method=POST path=/api/v1/users status=201 duration_ms=46.3
|
||||
messenger-api | 2026-10-23T10:18:24+0000 INFO request request_id=b6f69c04a1894b06966dce197177dc74 method=POST path=/api/v1/auth/sessions status=201 duration_ms=47.1
|
||||
messenger-api | 2026-10-23T10:18:25+0000 INFO request request_id=0939be01468d46149145707bb6a13e4e method=GET path=/api/v1/users/me status=200 duration_ms=3.4
|
||||
messenger-api | 2026-10-23T10:18:25+0000 INFO request request_id=68668e6e03c143d8aba1bc765e46594f method=GET path=/api/v1/users/me status=200 duration_ms=0.8
|
||||
messenger-api | 2026-10-23T10:18:25+0000 INFO request request_id=061a41bf184b4a28ae09a236a100b954 method=GET path=/api/v1/users/me status=200 duration_ms=0.9
|
||||
messenger-api | 2026-10-23T10:18:25+0000 INFO request request_id=3ac82f390f1e4c9ebf62030403c6d5f3 method=GET path=/api/v1/users/me status=200 duration_ms=0.8
|
||||
messenger-api | 2026-10-23T10:18:25+0000 INFO request request_id=b5d7ef2a5af14a17a409261526158e73 method=GET path=/api/v1/users/me status=200 duration_ms=0.8
|
||||
messenger-api | 2026-10-23T10:18:25+0000 INFO request request_id=60d4a26bdac3438fa1169f6e45eed859 method=GET path=/api/v1/users/me status=200 duration_ms=0.8
|
||||
messenger-api | 2026-10-23T10:18:25+0000 INFO request request_id=0a795eda6f1b43cb83c37b5ca3633d2b method=GET path=/api/v1/users/me status=200 duration_ms=0.7
|
||||
messenger-api | 2026-10-23T10:18:25+0000 INFO request request_id=d33c6949fe41473c91ded47999a57451 method=GET path=/api/v1/users/me status=200 duration_ms=0.8
|
||||
messenger-api | 2026-10-23T10:18:25+0000 INFO request request_id=3da894a019194c80b4eb470af8067f9b method=GET path=/api/v1/users/me status=200 duration_ms=0.8
|
||||
messenger-api | 2026-10-23T10:18:25+0000 INFO request request_id=3d45d5bb28e7433c91bd92fdabcf6529 method=GET path=/api/v1/users/me status=200 duration_ms=1.1
|
||||
messenger-api | 2026-10-23T10:18:25+0000 INFO request request_id=4e6752620fa24ec1803dd970647f372a method=GET path=/api/v1/users/me status=200 duration_ms=1.1
|
||||
messenger-api | 2026-10-23T10:18:25+0000 INFO request request_id=bad3a45c841e4f619fb6f0d05847f2cb method=GET path=/api/v1/users/me status=200 duration_ms=1.1
|
||||
messenger-api | 2026-10-23T10:18:25+0000 INFO request request_id=0afb12feb0b34ef8a29835d7a33655be method=GET path=/api/v1/users/me status=200 duration_ms=1.0
|
||||
messenger-api | 2026-10-23T10:18:25+0000 INFO request request_id=f10d8b39b9b844c884e91d230ab6ee93 method=GET path=/api/v1/users/me status=200 duration_ms=1.1
|
||||
messenger-api | 2026-10-23T10:18:25+0000 INFO request request_id=1d69e3356cac44dbb424492f1b666d5f method=GET path=/api/v1/users/me status=200 duration_ms=1.0
|
||||
messenger-api | 2026-10-23T10:18:25+0000 INFO request request_id=b52dcff84fe7411eb185807f66a477db method=GET path=/api/v1/users/me status=200 duration_ms=0.9
|
||||
messenger-api | 2026-10-23T10:18:25+0000 INFO request request_id=1f87f056fcda4b2d87e277424a596a81 method=GET path=/api/v1/users/me status=200 duration_ms=0.9
|
||||
messenger-api | 2026-10-23T10:18:25+0000 INFO request request_id=7b7c2bbf8a8d49d9be2e163d12c2783a method=GET path=/api/v1/users/me status=200 duration_ms=0.9
|
||||
messenger-api | 2026-10-23T10:18:25+0000 INFO request request_id=e767c61290b240f190b673bbe0cb8320 method=GET path=/api/v1/users/me status=200 duration_ms=0.9
|
||||
messenger-api | 2026-10-23T10:18:25+0000 INFO request request_id=5a9a796e47a343d699a6d42e604c1a32 method=GET path=/api/v1/users/me status=200 duration_ms=0.9
|
||||
messenger-api | 2026-10-23T10:18:25+0000 INFO request request_id=5b8ef3ca395e4efbbd54c1e7fdd91781 method=GET path=/api/v1/users/me status=200 duration_ms=0.8
|
||||
messenger-api | 2026-10-23T10:18:25+0000 INFO request request_id=f786c6bd84f84722886bf6dfd35d1a32 method=GET path=/api/v1/users/me status=200 duration_ms=0.8
|
||||
messenger-api | 2026-10-23T10:18:25+0000 INFO request request_id=dabde023b7784d77b66993f95c3b0666 method=GET path=/api/v1/users/me status=200 duration_ms=0.8
|
||||
messenger-api | 2026-10-23T10:18:25+0000 INFO request request_id=49c5501d17fd46eb916ce33a0ed8b504 method=GET path=/api/v1/users/me status=200 duration_ms=0.9
|
||||
messenger-api | 2026-10-23T10:18:25+0000 INFO request request_id=6b46168eb0a44611a7ddbbe6138cf5a2 method=GET path=/api/v1/users/me status=200 duration_ms=0.9
|
||||
messenger-api | 2026-10-23T10:18:25+0000 INFO request request_id=3dde34ec48e146d7b5f0110f1d2fbdc1 method=GET path=/api/v1/users/me status=200 duration_ms=0.9
|
||||
messenger-api | 2026-10-23T10:18:25+0000 INFO request request_id=1f122734d4544a9b8b1dc6f12cad4f87 method=GET path=/api/v1/users/me status=200 duration_ms=0.8
|
||||
messenger-api | 2026-10-23T10:18:25+0000 INFO request request_id=ba06f9dbf16e4b538f68fc5ee1268f0f method=GET path=/api/v1/users/me status=200 duration_ms=0.8
|
||||
messenger-api | 2026-10-23T10:18:25+0000 INFO request request_id=75b39d4d183e46878ad7ce0e0946d4fe method=GET path=/api/v1/users/me status=200 duration_ms=0.8
|
||||
messenger-api | 2026-10-23T10:18:25+0000 INFO request request_id=344a490d3a37460fa08ea554600cf13d method=GET path=/api/v1/users/me status=200 duration_ms=0.9
|
||||
messenger-api | 2026-10-23T10:18:25+0000 INFO request request_id=d86193959efb469fa70484723c0479f7 method=GET path=/api/v1/users/me status=200 duration_ms=1.0
|
||||
messenger-api | 2026-10-23T10:18:25+0000 INFO request request_id=5c7d4a99cf0d42e290c00843c5c5c277 method=GET path=/api/v1/users/me status=200 duration_ms=1.3
|
||||
messenger-api | 2026-10-23T10:18:25+0000 INFO request request_id=01eeb99a98c2496eae093957542f779c method=GET path=/api/v1/users/me status=200 duration_ms=1.2
|
||||
messenger-api | 2026-10-23T10:18:25+0000 INFO request request_id=33cccf79cdcb4b86a66b8db765943207 method=GET path=/api/v1/users/me status=200 duration_ms=1.2
|
||||
messenger-api | 2026-10-23T10:18:25+0000 INFO request request_id=ccfc557d55cf4205b7010790f41f61e1 method=GET path=/api/v1/users/me status=200 duration_ms=1.2
|
||||
messenger-api | 2026-10-23T10:18:25+0000 INFO request request_id=beb012eb4b4344c6ba47b1c229888c24 method=GET path=/api/v1/users/me status=200 duration_ms=1.2
|
||||
messenger-api | 2026-10-23T10:18:25+0000 INFO request request_id=30a106dab9ad4ad9bde5a9c67dfa0fa8 method=GET path=/api/v1/users/me status=200 duration_ms=1.2
|
||||
messenger-api | 2026-10-23T10:18:25+0000 INFO request request_id=2ef1c83fa84141f7ba9125ee2bd32b94 method=GET path=/api/v1/users/me status=200 duration_ms=1.2
|
||||
messenger-api | 2026-10-23T10:18:25+0000 INFO request request_id=eb54e1442e49409dbe3952b1dc472a9d method=GET path=/api/v1/users/me status=200 duration_ms=1.2
|
||||
messenger-api | 2026-10-23T10:18:25+0000 INFO request request_id=add208ea268245af868c186f2a464580 method=GET path=/api/v1/users/me status=200 duration_ms=1.2
|
||||
messenger-api | 2026-10-23T10:18:25+0000 INFO request request_id=124ca2404be5494696aae09353d139c3 method=GET path=/api/v1/users/me status=200 duration_ms=1.1
|
||||
messenger-api | 2026-10-23T10:18:25+0000 INFO request request_id=5b3f9c939709426992d41d96c14d32ee method=GET path=/api/v1/users/me status=200 duration_ms=1.1
|
||||
messenger-api | 2026-10-23T10:18:25+0000 INFO request request_id=d1cbe63bd9a44638abe624657ba0c203 method=GET path=/api/v1/users/me status=200 duration_ms=1.5
|
||||
messenger-api | 2026-10-23T10:18:25+0000 INFO request request_id=945013d1534c4a94a924a12eec335baa method=GET path=/api/v1/users/me status=200 duration_ms=1.5
|
||||
messenger-api | 2026-10-23T10:18:25+0000 INFO request request_id=464c092f0f164f9db27d86e840466e2a method=GET path=/api/v1/users/me status=200 duration_ms=1.3
|
||||
messenger-api | 2026-10-23T10:18:25+0000 INFO request request_id=becf9277028c4408bbab35fbf3e6fad2 method=GET path=/api/v1/users/me status=200 duration_ms=1.2
|
||||
messenger-api | 2026-10-23T10:18:25+0000 INFO request request_id=4481eecaef30407e85ecf86762337d39 method=GET path=/api/v1/users/me status=200 duration_ms=1.2
|
||||
messenger-api | 2026-10-23T10:18:25+0000 INFO request request_id=e1d34ed15b144d3b9907b64025677d39 method=GET path=/api/v1/users/me status=200 duration_ms=1.2
|
||||
messenger-api | 2026-10-23T10:18:25+0000 INFO request request_id=3d43e46e55334ddf91274467d331b167 method=GET path=/api/v1/users/me status=200 duration_ms=1.1
|
||||
messenger-api | 2026-10-23T10:18:25+0000 INFO request request_id=8817c423a2da48529a4519cbb19791e6 method=GET path=/api/v1/users/me status=200 duration_ms=1.1
|
||||
messenger-api | 2026-10-23T10:18:25+0000 INFO request request_id=705142a4330249bd99beaa84bdbc6127 method=GET path=/health/ready status=200 duration_ms=2.0
|
||||
messenger-api | 2026-10-23T10:18:27+0000 INFO request request_id=f4934e6692bd473fad9c59a9927fa304 method=POST path=/api/v1/users status=201 duration_ms=53.4
|
||||
messenger-api | 2026-10-23T10:18:27+0000 INFO request request_id=335a26c51d784946882f57f5668946e5 method=POST path=/api/v1/auth/sessions status=201 duration_ms=47.8
|
||||
messenger-api | 2026-10-23T10:18:27+0000 INFO request request_id=ea1bddbde1d04083966b5b77278a1618 method=GET path=/api/v1/users/me status=200 duration_ms=3.1
|
||||
messenger-api | 2026-10-23T10:18:27+0000 INFO request request_id=a47b63e1b3214a67974a4c506590b905 method=DELETE path=/api/v1/auth/sessions/current status=204 duration_ms=6.2
|
||||
messenger-api | 2026-10-23T10:18:27+0000 INFO request request_id=5137ca7dc5464437bdaa45e85bb17e84 method=GET path=/api/v1/users/me status=401 duration_ms=0.9
|
||||
messenger-api | 2026-10-23T10:18:33+0000 INFO request request_id=05218a878a3044d598f8a5c8ccc17ab6 method=GET path=/api/v1/users/me status=200 duration_ms=3.8
|
||||
messenger-api | 2026-10-23T10:18:33+0000 INFO request request_id=aed4a94de8cf4ebfb495e3b35ae51bc3 method=GET path=/api/v1/users/me status=401 duration_ms=2.7
|
||||
messenger-api | 2026-10-23T10:18:33+0000 INFO request request_id=dc35c7f0f7da44daa96e7f9995fe8ec7 method=GET path=/health/live status=200 duration_ms=0.5
|
||||
messenger-api | 2026-10-23T10:18:33+0000 INFO request request_id=742e776579014925b3426c3e5f28ed1a method=GET path=/health/ready status=200 duration_ms=1.5
|
||||
messenger-api | 2026-10-23T10:18:33+0000 WARNING cache_unavailable type=TimeoutError
|
||||
messenger-api | 2026-10-23T10:18:33+0000 INFO request request_id=34d82defd3454b18a6b5e3b17fc31fd8 method=GET path=/api/v1/users/me status=200 duration_ms=306.3
|
||||
messenger-api | 2026-10-23T10:18:33+0000 INFO request request_id=41c38a52d56042a8b5346d7fb623d2d3 method=POST path=/api/v1/chats status=201 duration_ms=19.7
|
||||
messenger-api | 2026-10-23T10:18:33+0000 INFO request request_id=44445527e20f47ae847911b139196fde method=POST path=/api/v1/chats/515465df-f215-49f0-9548-55d81bf82331/messages status=201 duration_ms=8.9
|
||||
messenger-api | 2026-10-23T10:18:33+0000 INFO request request_id=97111a005b89485c8f2abac6c18275a1 method=GET path=/api/v1/chats/515465df-f215-49f0-9548-55d81bf82331/messages status=200 duration_ms=4.4
|
||||
messenger-api | 2026-10-23T10:18:33+0000 INFO request request_id=9726e3049265430abcbbf1370d866d0e method=GET path=/api/v1/users/me status=401 duration_ms=0.6
|
||||
messenger-api | 2026-10-23T10:18:33+0000 INFO request request_id=f187d7d2bd65451ba37797de881c940f method=GET path=/api/v1/users/me status=401 duration_ms=2.6
|
||||
messenger-api | 2026-10-23T10:18:33+0000 INFO request request_id=bd141144a65a4644887fe5fb2b511a27 method=POST path=/api/v1/users status=201 duration_ms=50.0
|
||||
messenger-api | 2026-10-23T10:18:33+0000 INFO request request_id=e52d5593aa6b459ca4d49cfc2b185b01 method=POST path=/api/v1/auth/sessions status=201 duration_ms=47.7
|
||||
messenger-api | 2026-10-23T10:18:33+0000 INFO request request_id=d0dbee871d0a44e3a8a0eb106e955ce0 method=GET path=/api/v1/users/me status=200 duration_ms=2.7
|
||||
messenger-api | 2026-10-23T10:18:33+0000 INFO request request_id=f4ce6d2b27f74affbbc32a88b7712765 method=DELETE path=/api/v1/auth/sessions/current status=204 duration_ms=6.4
|
||||
messenger-api | 2026-10-23T10:18:33+0000 INFO request request_id=afc3e9b8832241068e49c15a5f255647 method=GET path=/api/v1/users/me status=401 duration_ms=2.3
|
||||
messenger-api | 2026-10-23T10:18:36+0000 INFO request request_id=d549e191c0974a69bd6f4bf634ee50bb method=GET path=/health/ready status=200 duration_ms=2.0
|
||||
messenger-api | 2026-10-23T10:18:45+0000 WARNING cache: FLUSHDB after a lost revocation
|
||||
messenger-api | 2026-10-23T10:18:45+0000 INFO request request_id=2620a2039cbf4f4e8cf5879087f13ec4 method=GET path=/api/v1/users/me status=200 duration_ms=18.6
|
||||
messenger-api | 2026-10-23T10:18:45+0000 INFO request request_id=9b50d84c22434a8ea61d0e642afb75d7 method=GET path=/api/v1/users/me status=401 duration_ms=9.8
|
||||
|
||||
==============================================================================
|
||||
6. PostgreSQL ОСТАНОВЛЕН: ограниченная ошибка, никого не пускаем без проверки
|
||||
==============================================================================
|
||||
$ docker compose --env-file .env stop db
|
||||
Container messenger-db Stopping
|
||||
Container messenger-db Stopped
|
||||
GET /health/live -> 200 (1 мс) # процесс жив
|
||||
{"status": "ok"}
|
||||
GET /health/ready -> 503 (912 мс) # обязательная зависимость недоступна
|
||||
{"error": {"code": "unavailable", "message": "База данных недоступна", "request_id": "127dd693633743c2ae7c483fec38293f"}}
|
||||
GET /api/v1/chats -> 503 (6 мс) # данные только в БД
|
||||
{"error": {"code": "unavailable", "message": "Сервис временно недоступен", "request_id": "606963e195294eb1ac31c8195b368194"}}
|
||||
POST /api/v1/chats/515465df-f215-49f0-9548-55d81bf82331/messages -> 503 (6 мс)
|
||||
{"error": {"code": "unavailable", "message": "Сервис временно недоступен", "request_id": "27b4b397069446bca210fda788cc5f6d"}}
|
||||
POST /api/v1/users -> 503 (51 мс)
|
||||
{"error": {"code": "unavailable", "message": "Сервис временно недоступен", "request_id": "72a0730aad794645b2f27e12da18c230"}}
|
||||
GET /api/v1/users/me -> 401 (1 мс) # аноним -- 401
|
||||
{"error": {"code": "unauthorized", "message": "Нет действительной аутентификации", "request_id": "2e09c4aa32f044aebadbb9a58101de2c"}}
|
||||
GET /api/v1/users/me -> 503 (5 мс) # неизвестный токен: проверить нельзя -> 503, не 200
|
||||
{"error": {"code": "unavailable", "message": "Сервис временно недоступен", "request_id": "e8f919b332014c4398f452d6e7787f5e"}}
|
||||
$ docker compose --env-file .env start db
|
||||
Container messenger-db Starting
|
||||
Container messenger-db Started
|
||||
API снова готов через 0.5 с после start db
|
||||
GET /api/v1/chats/515465df-f215-49f0-9548-55d81bf82331/messages -> 200 (16 мс) # тот же токен, те же данные
|
||||
{"items": [{"id": "ebd92a19-9f52-41c2-a883-6ecf0fa880b1", "chat_id": "515465df-f215-49f0-9548-55d81bf82331", "sender_id": "2454e675-039f-49b3-92f0-e2e492e0cefc", "text": "сообщение при остановленном Redis", "created_at": "2026-10-23T10:18:33.830951Z"}], "next_cursor": null}
|
||||
$ docker compose --env-file .env ps
|
||||
NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
|
||||
messenger-api mtusi-messenger:lab2 "python -m app" api 32 seconds ago Up 26 seconds (healthy) 127.0.0.1:8080->8080/tcp
|
||||
messenger-db postgres:16.6-bookworm "docker-entrypoint.s…" db 32 seconds ago Up Less than a second (health: starting) 127.0.0.1:5433->5432/tcp
|
||||
messenger-redis redis:7.4.1-bookworm "docker-entrypoint.s…" redis 32 seconds ago Up 13 seconds (healthy) 6379/tcp
|
||||
|
||||
==============================================================================
|
||||
ИТОГ
|
||||
==============================================================================
|
||||
ИТОГ: PASS -- Redis кэширует проверку сессий; рестарт/остановка Redis не теряют сессий и не пропускают анонимов; остановка PostgreSQL даёт 503 без деталей и восстанавливается.
|
||||
@@ -0,0 +1,89 @@
|
||||
# Дата (UTC): 2026-10-23 10:18:50
|
||||
# Commit: 65003b6
|
||||
# Команда: docker compose --env-file .env exec -T api python -m app.explain
|
||||
# ---------------------------------------- вывод ----------------------------------------
|
||||
PostgreSQL: 16.6 (Debian 16.6-1.pgdg120+1)
|
||||
Сообщений всего: 60005
|
||||
Сообщений в большом чате: 20000
|
||||
Размер таблицы messages: 6784 kB
|
||||
Размер индекса messages_chat_created_idx: 5992 kB
|
||||
|
||||
==============================================================================
|
||||
1. Первая страница (без cursor), запрос приложения
|
||||
==============================================================================
|
||||
SELECT messages.id, messages.chat_id, messages.sender_id, messages.text, messages.created_at
|
||||
FROM messages
|
||||
WHERE messages.chat_id = $1::UUID ORDER BY messages.created_at, messages.id
|
||||
LIMIT $2::INTEGER
|
||||
-- параметры: b21f9b12-3f7c-5896-bc0d-5c48f78f3576, 51
|
||||
Limit (cost=0.41..12.38 rows=51 width=80) (actual time=0.008..0.022 rows=51 loops=1)
|
||||
Buffers: shared hit=6
|
||||
-> Index Scan using messages_chat_created_idx on messages (cost=0.41..4718.99 rows=20106 width=80) (actual time=0.007..0.018 rows=51 loops=1)
|
||||
Index Cond: (chat_id = 'b21f9b12-3f7c-5896-bc0d-5c48f78f3576'::uuid)
|
||||
Buffers: shared hit=6
|
||||
Planning:
|
||||
Buffers: shared hit=6
|
||||
Planning Time: 0.073 ms
|
||||
Execution Time: 0.032 ms
|
||||
|
||||
==============================================================================
|
||||
2. Глубокая страница: cursor после 15000-го сообщения (keyset)
|
||||
==============================================================================
|
||||
SELECT messages.id, messages.chat_id, messages.sender_id, messages.text, messages.created_at
|
||||
FROM messages
|
||||
WHERE messages.chat_id = $1::UUID AND (messages.created_at, messages.id) > ($2::TIMESTAMP WITH TIME ZONE, $3) ORDER BY messages.created_at, messages.id
|
||||
LIMIT $4::INTEGER
|
||||
-- параметры: b21f9b12-3f7c-5896-bc0d-5c48f78f3576, 2026-09-23 10:26:18.547526+00:00, c108c4dd-61dd-4321-8b6c-70b26623adf7, 51
|
||||
Limit (cost=0.41..38.34 rows=51 width=80) (actual time=0.009..0.022 rows=51 loops=1)
|
||||
Buffers: shared hit=6
|
||||
-> Index Scan using messages_chat_created_idx on messages (cost=0.41..3712.96 rows=4992 width=80) (actual time=0.008..0.019 rows=51 loops=1)
|
||||
Index Cond: ((chat_id = 'b21f9b12-3f7c-5896-bc0d-5c48f78f3576'::uuid) AND (ROW(created_at, id) > ROW('2026-09-23 10:26:18.547526+00'::timestamp with time zone, 'c108c4dd-61dd-4321-8b6c-70b26623adf7'::uuid)))
|
||||
Buffers: shared hit=6
|
||||
Planning Time: 0.052 ms
|
||||
Execution Time: 0.049 ms
|
||||
|
||||
==============================================================================
|
||||
3. Для сравнения: та же страница через OFFSET (так приложение НЕ делает)
|
||||
==============================================================================
|
||||
SELECT * FROM messages WHERE chat_id = $1 ORDER BY created_at, id OFFSET 15000 LIMIT 51
|
||||
-- параметры: b21f9b12-3f7c-5896-bc0d-5c48f78f3576
|
||||
Limit (cost=3072.67..3072.80 rows=51 width=80) (actual time=7.919..7.925 rows=51 loops=1)
|
||||
Buffers: shared hit=848
|
||||
-> Sort (cost=3035.17..3085.44 rows=20106 width=80) (actual time=6.788..7.505 rows=15051 loops=1)
|
||||
Sort Key: created_at, id
|
||||
Sort Method: quicksort Memory: 3271kB
|
||||
Buffers: shared hit=848
|
||||
-> Seq Scan on messages (cost=0.00..1598.06 rows=20106 width=80) (actual time=0.006..5.029 rows=20000 loops=1)
|
||||
Filter: (chat_id = 'b21f9b12-3f7c-5896-bc0d-5c48f78f3576'::uuid)
|
||||
Rows Removed by Filter: 40005
|
||||
Buffers: shared hit=848
|
||||
Planning Time: 0.035 ms
|
||||
Execution Time: 7.938 ms
|
||||
|
||||
==============================================================================
|
||||
4. Для сравнения: запрос приложения БЕЗ индекса (DROP INDEX в транзакции, затем ROLLBACK)
|
||||
==============================================================================
|
||||
SELECT messages.id, messages.chat_id, messages.sender_id, messages.text, messages.created_at
|
||||
FROM messages
|
||||
WHERE messages.chat_id = $1::UUID AND (messages.created_at, messages.id) > ($2::TIMESTAMP WITH TIME ZONE, $3) ORDER BY messages.created_at, messages.id
|
||||
LIMIT $4::INTEGER
|
||||
-- параметры: b21f9b12-3f7c-5896-bc0d-5c48f78f3576, 2026-09-23 10:26:18.547526+00:00, c108c4dd-61dd-4321-8b6c-70b26623adf7, 51
|
||||
Limit (cost=2064.63..2064.76 rows=51 width=80) (actual time=5.644..5.649 rows=51 loops=1)
|
||||
Buffers: shared hit=848
|
||||
-> Sort (cost=2064.63..2077.11 rows=4992 width=80) (actual time=5.643..5.645 rows=51 loops=1)
|
||||
Sort Key: created_at, id
|
||||
Sort Method: top-N heapsort Memory: 31kB
|
||||
Buffers: shared hit=848
|
||||
-> Seq Scan on messages (cost=0.00..1898.09 rows=4992 width=80) (actual time=3.769..5.197 rows=5000 loops=1)
|
||||
Filter: ((chat_id = 'b21f9b12-3f7c-5896-bc0d-5c48f78f3576'::uuid) AND (ROW(created_at, id) > ROW('2026-09-23 10:26:18.547526+00'::timestamp with time zone, 'c108c4dd-61dd-4321-8b6c-70b26623adf7'::uuid)))
|
||||
Rows Removed by Filter: 55005
|
||||
Buffers: shared hit=848
|
||||
Planning:
|
||||
Buffers: shared hit=4
|
||||
Planning Time: 0.067 ms
|
||||
Execution Time: 5.661 ms
|
||||
|
||||
Индекс messages_chat_created_idx на месте: True
|
||||
# ---------------------------------------------------------------------------------------
|
||||
# Код возврата: 0
|
||||
# Время выполнения: 2.44 с
|
||||
@@ -0,0 +1,28 @@
|
||||
# Дата (UTC): 2026-10-23 10:18:54
|
||||
# Commit: 65003b6
|
||||
# Команда: docker compose --env-file .env --profile test run --rm tests python -m pytest -v --no-header -p no:cacheprovider student-tests/test_explain.py
|
||||
# ---------------------------------------- вывод ----------------------------------------
|
||||
============================= test session starts ==============================
|
||||
collecting ... collected 2 items
|
||||
|
||||
student-tests/test_explain.py::test_history_page_uses_index_without_full_scan[0] PASSED [ 50%]
|
||||
student-tests/test_explain.py::test_history_page_uses_index_without_full_scan[9000] PASSED [100%]
|
||||
|
||||
=============================== warnings summary ===============================
|
||||
../usr/local/lib/python3.12/site-packages/fastapi/testclient.py:1
|
||||
/usr/local/lib/python3.12/site-packages/fastapi/testclient.py:1: StarletteDeprecationWarning: Using `httpx` with `starlette.testclient` is deprecated; install `httpx2` instead.
|
||||
from starlette.testclient import TestClient as TestClient # noqa
|
||||
|
||||
-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
|
||||
========================= 2 passed, 1 warning in 0.15s =========================
|
||||
Container messenger-redis Running
|
||||
Container messenger-db Running
|
||||
Container messenger-db Waiting
|
||||
Container messenger-redis Waiting
|
||||
Container messenger-redis Healthy
|
||||
Container messenger-db Healthy
|
||||
Container mtusi-messenger-tests-run-177db5fdb80c Creating
|
||||
Container mtusi-messenger-tests-run-177db5fdb80c Created
|
||||
# ---------------------------------------------------------------------------------------
|
||||
# Код возврата: 0
|
||||
# Время выполнения: 4.73 с
|
||||
@@ -0,0 +1,19 @@
|
||||
# Дата (UTC): 2026-10-23 10:19:02
|
||||
# Commit: 65003b6
|
||||
# Команда: python3 scripts/check_expiry.py --base-url http://localhost:8081 --ttl 3
|
||||
# ---------------------------------------- вывод ----------------------------------------
|
||||
OK регистрация -> 201
|
||||
OK вход -> 201, expires_at через 2.9 с (TTL=3)
|
||||
OK GET /users/me #1 до истечения -> 200
|
||||
OK GET /users/me #2 до истечения -> 200
|
||||
OK GET /users/me #3 до истечения -> 200
|
||||
OK GET /users/me после TTL+1 с -> 401, WWW-Authenticate: Bearer realm="messenger", code=unauthorized
|
||||
OK новый вход -> 201
|
||||
OK logout -> 204
|
||||
OK GET /users/me после logout -> 401
|
||||
OK повторный logout -> 401
|
||||
OK вход с неверным паролем -> 401, WWW-Authenticate: Basic realm="messenger", charset="UTF-8"
|
||||
PASS: expiry (TTL=3 с), logout и неверный пароль на запущенном API
|
||||
# ---------------------------------------------------------------------------------------
|
||||
# Код возврата: 0
|
||||
# Время выполнения: 4.34 с
|
||||
@@ -0,0 +1,44 @@
|
||||
2026-11-01
|
||||
Python 3.12; pinned runtime/test dependencies from requirements files.
|
||||
|
||||
Regression sensitivity: original auth.py and cursor.py, remaining code fixed.
|
||||
FFFFF.FF.....FF...... [100%]
|
||||
=============================== warnings summary ===============================
|
||||
../.venv/lib/python3.12/site-packages/fastapi/testclient.py:1
|
||||
/workspace/scratch/03a740016edf/.venv/lib/python3.12/site-packages/fastapi/testclient.py:1: StarletteDeprecationWarning: Using `httpx` with `starlette.testclient` is deprecated; install `httpx2` instead.
|
||||
from starlette.testclient import TestClient as TestClient # noqa
|
||||
|
||||
unit-tests/test_mr_regressions.py::test_stale_positive_cache_never_authorizes_revoked_token[/api/v1/chats/00000000-0000-0000-0000-000000000002/messages-POST]
|
||||
/workspace/scratch/03a740016edf/.venv/lib/python3.12/site-packages/starlette/_exception_handler.py:63: RuntimeWarning: coroutine 'AsyncMockMixin._execute_mock_call' was never awaited
|
||||
await response(scope, receive, sender)
|
||||
Enable tracemalloc to get traceback where the object was allocated.
|
||||
See https://docs.pytest.org/en/stable/how-to/capture-warnings.html#resource-warnings for more info.
|
||||
|
||||
-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
|
||||
=========================== short test summary info ============================
|
||||
FAILED unit-tests/test_mr_regressions.py::test_stale_positive_cache_never_authorizes_revoked_token[/api/v1/users/me-GET]
|
||||
FAILED unit-tests/test_mr_regressions.py::test_stale_positive_cache_never_authorizes_revoked_token[/api/v1/chats/00000000-0000-0000-0000-000000000002/messages-POST]
|
||||
FAILED unit-tests/test_mr_regressions.py::test_stale_positive_cache_never_authorizes_revoked_token[/api/v1/auth/sessions/current-DELETE]
|
||||
FAILED unit-tests/test_mr_regressions.py::test_valid_cache_hit_checks_session_without_loading_user
|
||||
FAILED unit-tests/test_mr_regressions.py::test_database_failure_on_positive_cache_hit_is_503
|
||||
FAILED unit-tests/test_mr_regressions.py::test_cursor_timestamp_is_rejected_through_http[messages-253402300800000000]
|
||||
FAILED unit-tests/test_mr_regressions.py::test_cursor_timestamp_is_rejected_through_http[messages-1000000000000000000000000000000]
|
||||
FAILED unit-tests/test_mr_regressions.py::test_cursor_timestamp_is_rejected_through_http[chats-253402300800000000]
|
||||
FAILED unit-tests/test_mr_regressions.py::test_cursor_timestamp_is_rejected_through_http[chats-1000000000000000000000000000000]
|
||||
9 failed, 12 passed, 2 warnings in 0.77s
|
||||
|
||||
Fixed implementation:
|
||||
..................... [100%]
|
||||
=============================== warnings summary ===============================
|
||||
../.venv/lib/python3.12/site-packages/fastapi/testclient.py:1
|
||||
/workspace/scratch/03a740016edf/.venv/lib/python3.12/site-packages/fastapi/testclient.py:1: StarletteDeprecationWarning: Using `httpx` with `starlette.testclient` is deprecated; install `httpx2` instead.
|
||||
from starlette.testclient import TestClient as TestClient # noqa
|
||||
|
||||
-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
|
||||
21 passed, 1 warning in 0.82s
|
||||
|
||||
python3 scripts/check_contract.py
|
||||
PASS: offline skeleton structure, local refs and Python syntax; application NOT tested.
|
||||
|
||||
Integration suite NOT RUN: Docker/PostgreSQL/Redis unavailable.
|
||||
New integration cases are in student-tests/test_cache.py and test_pagination.py.
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"at_utc": "2026-11-02T14:27:32.969212+00:00",
|
||||
"platform": "Windows-10-10.0.19045-SP0",
|
||||
"cpu_count": 16,
|
||||
"python": "3.11.9",
|
||||
"duration": 30.0,
|
||||
"workload": "2 closed-loop clients, POST+GET; uncertain POST retried with same key/body"
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
$ docker version
|
||||
Client:
|
||||
Version: 26.1.4
|
||||
API version: 1.45
|
||||
Go version: go1.21.11
|
||||
Git commit: 5650f9b
|
||||
Built: Wed Jun 5 11:29:54 2024
|
||||
OS/Arch: windows/amd64
|
||||
Context: desktop-linux
|
||||
|
||||
Server: Docker Desktop 4.31.1 (153621)
|
||||
Engine:
|
||||
Version: 26.1.4
|
||||
API version: 1.45 (minimum version 1.24)
|
||||
Go version: go1.21.11
|
||||
Git commit: de5c9cf
|
||||
Built: Wed Jun 5 11:29:22 2024
|
||||
OS/Arch: linux/amd64
|
||||
Experimental: false
|
||||
containerd:
|
||||
Version: 1.6.33
|
||||
GitCommit: d2d58213f83a351ca8f528a95fbd145f5654e957
|
||||
runc:
|
||||
Version: 1.1.12
|
||||
GitCommit: v1.1.12-0-g51d5e94
|
||||
docker-init:
|
||||
Version: 0.19.0
|
||||
GitCommit: de40ad0
|
||||
|
||||
exit_code=0; elapsed_seconds=0.109
|
||||
@@ -0,0 +1,3 @@
|
||||
$ docker compose --env-file .env config --quiet
|
||||
|
||||
exit_code=0; elapsed_seconds=0.297
|
||||
@@ -0,0 +1,8 @@
|
||||
$ docker inspect --format {{.Name}} cpus={{.HostConfig.NanoCpus}} memory={{.HostConfig.Memory}} ports={{json .HostConfig.PortBindings}} ef1e096f1c30593e0d8e9ebeb467a21c997580d835473c76142127d2d402b235 4cccbf699f55df2a5e9fd6e19aa897796654a47cdac2ff372565fa6c2a4677e0 765a7b20a31ea3ea8cfd024fe9d4a938c46f4d23d743e4db310b354d357014d8 5675a02e89826f64b25026269d5dc9177a7172ce4922722875c4d986e8423125 1b3ad58c1dd2bec01048e78af86e3c9812172df3c4189eab27352b11a950673d
|
||||
/mtusi-messenger-lab3-api-a-1 cpus=500000000 memory=268435456 ports={}
|
||||
/mtusi-messenger-lab3-api-b-1 cpus=500000000 memory=268435456 ports={}
|
||||
/mtusi-messenger-lab3-db-1 cpus=1000000000 memory=536870912 ports={}
|
||||
/mtusi-messenger-lab3-nginx-1 cpus=250000000 memory=100663296 ports={"8080/tcp":[{"HostIp":"127.0.0.1","HostPort":"8080"}]}
|
||||
/mtusi-messenger-lab3-redis-1 cpus=250000000 memory=134217728 ports={}
|
||||
|
||||
exit_code=0; elapsed_seconds=0.109
|
||||
@@ -0,0 +1,8 @@
|
||||
$ docker compose --env-file .env ps -q
|
||||
ef1e096f1c30593e0d8e9ebeb467a21c997580d835473c76142127d2d402b235
|
||||
4cccbf699f55df2a5e9fd6e19aa897796654a47cdac2ff372565fa6c2a4677e0
|
||||
765a7b20a31ea3ea8cfd024fe9d4a938c46f4d23d743e4db310b354d357014d8
|
||||
5675a02e89826f64b25026269d5dc9177a7172ce4922722875c4d986e8423125
|
||||
1b3ad58c1dd2bec01048e78af86e3c9812172df3c4189eab27352b11a950673d
|
||||
|
||||
exit_code=0; elapsed_seconds=0.344
|
||||
@@ -0,0 +1,20 @@
|
||||
[
|
||||
{
|
||||
"service": "api-a",
|
||||
"container_id": "ef1e096f1c30593e0d8e9ebeb467a21c997580d835473c76142127d2d402b235",
|
||||
"pid": 48570,
|
||||
"state": "running",
|
||||
"started_at": "2026-11-02T14:13:58.63201658Z",
|
||||
"status": 200,
|
||||
"instance_id": "1b38655b9a21447494973eb99d5ea039"
|
||||
},
|
||||
{
|
||||
"service": "api-b",
|
||||
"container_id": "4cccbf699f55df2a5e9fd6e19aa897796654a47cdac2ff372565fa6c2a4677e0",
|
||||
"pid": 48587,
|
||||
"state": "running",
|
||||
"started_at": "2026-11-02T14:13:58.715403308Z",
|
||||
"status": 200,
|
||||
"instance_id": "128e6b095f444dffa33214cd3cd33184"
|
||||
}
|
||||
]
|
||||
@@ -0,0 +1,4 @@
|
||||
$ C:\Users\ira\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\python.exe tests/smoke.py --base-url http://localhost:8080
|
||||
PASS: lab3 public API scenario; infrastructure and grading evidence remain separate.
|
||||
|
||||
exit_code=0; elapsed_seconds=67.125
|
||||
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"parallel_requests": 12,
|
||||
"unique_message_ids": 1,
|
||||
"history_rows": 1,
|
||||
"instances": [
|
||||
"128e6b095f444dffa33214cd3cd33184",
|
||||
"1b38655b9a21447494973eb99d5ea039"
|
||||
],
|
||||
"conflict_status": 409
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
{
|
||||
"mode": "stop",
|
||||
"service": "api-a",
|
||||
"target_instance": "1b38655b9a21447494973eb99d5ea039",
|
||||
"observed_with_one_session": [
|
||||
"128e6b095f444dffa33214cd3cd33184",
|
||||
"1b38655b9a21447494973eb99d5ea039"
|
||||
],
|
||||
"before": [
|
||||
{
|
||||
"service": "api-a",
|
||||
"container_id": "ef1e096f1c30593e0d8e9ebeb467a21c997580d835473c76142127d2d402b235",
|
||||
"pid": 48570,
|
||||
"state": "running",
|
||||
"started_at": "2026-11-02T14:13:58.63201658Z",
|
||||
"status": 200,
|
||||
"instance_id": "1b38655b9a21447494973eb99d5ea039"
|
||||
},
|
||||
{
|
||||
"service": "api-b",
|
||||
"container_id": "4cccbf699f55df2a5e9fd6e19aa897796654a47cdac2ff372565fa6c2a4677e0",
|
||||
"pid": 48587,
|
||||
"state": "running",
|
||||
"started_at": "2026-11-02T14:13:58.715403308Z",
|
||||
"status": 200,
|
||||
"instance_id": "128e6b095f444dffa33214cd3cd33184"
|
||||
}
|
||||
],
|
||||
"after_action": [
|
||||
{
|
||||
"service": "api-a",
|
||||
"container_id": "ef1e096f1c30593e0d8e9ebeb467a21c997580d835473c76142127d2d402b235",
|
||||
"pid": 0,
|
||||
"state": "exited",
|
||||
"started_at": "2026-11-02T14:13:58.63201658Z"
|
||||
},
|
||||
{
|
||||
"service": "api-b",
|
||||
"container_id": "4cccbf699f55df2a5e9fd6e19aa897796654a47cdac2ff372565fa6c2a4677e0",
|
||||
"pid": 48587,
|
||||
"state": "running",
|
||||
"started_at": "2026-11-02T14:13:58.715403308Z",
|
||||
"status": 200,
|
||||
"instance_id": "128e6b095f444dffa33214cd3cd33184"
|
||||
}
|
||||
],
|
||||
"action": "{\"stopped_service\": \"api-a\", \"instance_id\": \"1b38655b9a21447494973eb99d5ea039\"}\n",
|
||||
"action_seconds": 4.125,
|
||||
"concurrency": 2,
|
||||
"total": {
|
||||
"elapsed_seconds": 32.953,
|
||||
"attempts": 32,
|
||||
"successes": 32,
|
||||
"errors": 0,
|
||||
"error_ratio": 0.0,
|
||||
"successful_rps": 0.971,
|
||||
"successful_latency_ms": {
|
||||
"p50": 2032.0,
|
||||
"p95": 2125.0,
|
||||
"p99": 2141.0
|
||||
}
|
||||
},
|
||||
"during_and_after_action": {
|
||||
"elapsed_seconds": 31.953,
|
||||
"attempts": 30,
|
||||
"successes": 30,
|
||||
"errors": 0,
|
||||
"error_ratio": 0.0,
|
||||
"successful_rps": 0.939,
|
||||
"successful_latency_ms": {
|
||||
"p50": 2032.0,
|
||||
"p95": 2125.0,
|
||||
"p99": 2141.0
|
||||
}
|
||||
},
|
||||
"confirmed_responses": 17,
|
||||
"history_messages": 17,
|
||||
"duplicates": 0,
|
||||
"existing_session_works": true,
|
||||
"first_write_after_action_seconds": 9.36,
|
||||
"write": {
|
||||
"elapsed_seconds": 32.953,
|
||||
"attempts": 16,
|
||||
"successes": 16,
|
||||
"errors": 0,
|
||||
"error_ratio": 0.0,
|
||||
"successful_rps": 0.486,
|
||||
"successful_latency_ms": {
|
||||
"p50": 2047.0,
|
||||
"p95": 2141.0,
|
||||
"p99": 2141.0
|
||||
}
|
||||
},
|
||||
"first_read_after_action_seconds": 7.297,
|
||||
"read": {
|
||||
"elapsed_seconds": 32.953,
|
||||
"attempts": 16,
|
||||
"successes": 16,
|
||||
"errors": 0,
|
||||
"error_ratio": 0.0,
|
||||
"successful_rps": 0.486,
|
||||
"successful_latency_ms": {
|
||||
"p50": 2031.0,
|
||||
"p95": 2047.0,
|
||||
"p99": 2047.0
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
{
|
||||
"mode": "stop",
|
||||
"service": "api-b",
|
||||
"target_instance": "128e6b095f444dffa33214cd3cd33184",
|
||||
"observed_with_one_session": [
|
||||
"128e6b095f444dffa33214cd3cd33184",
|
||||
"816f731929a14492b542719bbfcff984"
|
||||
],
|
||||
"before": [
|
||||
{
|
||||
"service": "api-a",
|
||||
"container_id": "ef1e096f1c30593e0d8e9ebeb467a21c997580d835473c76142127d2d402b235",
|
||||
"pid": 90437,
|
||||
"state": "running",
|
||||
"started_at": "2026-11-02T14:30:54.841666808Z",
|
||||
"status": 200,
|
||||
"instance_id": "816f731929a14492b542719bbfcff984"
|
||||
},
|
||||
{
|
||||
"service": "api-b",
|
||||
"container_id": "4cccbf699f55df2a5e9fd6e19aa897796654a47cdac2ff372565fa6c2a4677e0",
|
||||
"pid": 48587,
|
||||
"state": "running",
|
||||
"started_at": "2026-11-02T14:13:58.715403308Z",
|
||||
"status": 200,
|
||||
"instance_id": "128e6b095f444dffa33214cd3cd33184"
|
||||
}
|
||||
],
|
||||
"after_action": [
|
||||
{
|
||||
"service": "api-a",
|
||||
"container_id": "ef1e096f1c30593e0d8e9ebeb467a21c997580d835473c76142127d2d402b235",
|
||||
"pid": 90437,
|
||||
"state": "running",
|
||||
"started_at": "2026-11-02T14:30:54.841666808Z",
|
||||
"status": 200,
|
||||
"instance_id": "816f731929a14492b542719bbfcff984"
|
||||
},
|
||||
{
|
||||
"service": "api-b",
|
||||
"container_id": "4cccbf699f55df2a5e9fd6e19aa897796654a47cdac2ff372565fa6c2a4677e0",
|
||||
"pid": 0,
|
||||
"state": "exited",
|
||||
"started_at": "2026-11-02T14:13:58.715403308Z"
|
||||
}
|
||||
],
|
||||
"action": "{\"stopped_service\": \"api-b\", \"instance_id\": \"128e6b095f444dffa33214cd3cd33184\"}\n",
|
||||
"action_seconds": 3.985,
|
||||
"concurrency": 2,
|
||||
"total": {
|
||||
"elapsed_seconds": 32.907,
|
||||
"attempts": 32,
|
||||
"successes": 32,
|
||||
"errors": 0,
|
||||
"error_ratio": 0.0,
|
||||
"successful_rps": 0.972,
|
||||
"successful_latency_ms": {
|
||||
"p50": 2031.0,
|
||||
"p95": 2125.0,
|
||||
"p99": 2125.0
|
||||
}
|
||||
},
|
||||
"during_and_after_action": {
|
||||
"elapsed_seconds": 31.907,
|
||||
"attempts": 30,
|
||||
"successes": 30,
|
||||
"errors": 0,
|
||||
"error_ratio": 0.0,
|
||||
"successful_rps": 0.94,
|
||||
"successful_latency_ms": {
|
||||
"p50": 2031.0,
|
||||
"p95": 2125.0,
|
||||
"p99": 2125.0
|
||||
}
|
||||
},
|
||||
"confirmed_responses": 17,
|
||||
"history_messages": 17,
|
||||
"duplicates": 0,
|
||||
"existing_session_works": true,
|
||||
"first_write_after_action_seconds": 9.36,
|
||||
"write": {
|
||||
"elapsed_seconds": 32.907,
|
||||
"attempts": 16,
|
||||
"successes": 16,
|
||||
"errors": 0,
|
||||
"error_ratio": 0.0,
|
||||
"successful_rps": 0.486,
|
||||
"successful_latency_ms": {
|
||||
"p50": 2047.0,
|
||||
"p95": 2125.0,
|
||||
"p99": 2125.0
|
||||
}
|
||||
},
|
||||
"first_read_after_action_seconds": 7.297,
|
||||
"read": {
|
||||
"elapsed_seconds": 32.907,
|
||||
"attempts": 16,
|
||||
"successes": 16,
|
||||
"errors": 0,
|
||||
"error_ratio": 0.0,
|
||||
"successful_rps": 0.486,
|
||||
"successful_latency_ms": {
|
||||
"p50": 2031.0,
|
||||
"p95": 2047.0,
|
||||
"p99": 2047.0
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
$ C:\Users\ira\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\python.exe tests/resilience.py --mode failover --action-script scripts/stop-one.sh --base-url http://localhost:8080
|
||||
Executing explicit failover hook; hook output suppressed to avoid leaking secrets.
|
||||
{"observed_instances_before": 2, "successful_writes": 7, "failed_attempts": 0, "first_recovery_seconds": 6.328}
|
||||
PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence.
|
||||
|
||||
exit_code=0; elapsed_seconds=121.422
|
||||
@@ -0,0 +1,114 @@
|
||||
{
|
||||
"mode": "replace",
|
||||
"service": "api-a",
|
||||
"target_instance": "9f683cfbf70746f898efeb1ef3da5512",
|
||||
"observed_with_one_session": [
|
||||
"9f683cfbf70746f898efeb1ef3da5512",
|
||||
"b7542c30b09c43bf941aaade921e6640"
|
||||
],
|
||||
"before": [
|
||||
{
|
||||
"service": "api-a",
|
||||
"container_id": "ef1e096f1c30593e0d8e9ebeb467a21c997580d835473c76142127d2d402b235",
|
||||
"pid": 98867,
|
||||
"state": "running",
|
||||
"started_at": "2026-11-02T14:34:24.451317583Z",
|
||||
"status": 200,
|
||||
"instance_id": "9f683cfbf70746f898efeb1ef3da5512"
|
||||
},
|
||||
{
|
||||
"service": "api-b",
|
||||
"container_id": "4cccbf699f55df2a5e9fd6e19aa897796654a47cdac2ff372565fa6c2a4677e0",
|
||||
"pid": 93836,
|
||||
"state": "running",
|
||||
"started_at": "2026-11-02T14:32:20.000834613Z",
|
||||
"status": 200,
|
||||
"instance_id": "b7542c30b09c43bf941aaade921e6640"
|
||||
}
|
||||
],
|
||||
"after_action": [
|
||||
{
|
||||
"service": "api-a",
|
||||
"container_id": "ea9d5889a8f0c6ff908a726f8fbe787eef209d33c30b1195090f822fb742bdbb",
|
||||
"pid": 1674,
|
||||
"state": "running",
|
||||
"started_at": "2026-11-02T14:35:20.182460357Z",
|
||||
"status": 200,
|
||||
"instance_id": "8e1cb6e2bcd143fea7f8b677409738f5"
|
||||
},
|
||||
{
|
||||
"service": "api-b",
|
||||
"container_id": "4cccbf699f55df2a5e9fd6e19aa897796654a47cdac2ff372565fa6c2a4677e0",
|
||||
"pid": 93836,
|
||||
"state": "running",
|
||||
"started_at": "2026-11-02T14:32:20.000834613Z",
|
||||
"status": 200,
|
||||
"instance_id": "b7542c30b09c43bf941aaade921e6640"
|
||||
}
|
||||
],
|
||||
"action": {
|
||||
"service": "api-a",
|
||||
"before": "9f683cfbf70746f898efeb1ef3da5512",
|
||||
"after": "8e1cb6e2bcd143fea7f8b677409738f5"
|
||||
},
|
||||
"action_seconds": 18.407,
|
||||
"concurrency": 2,
|
||||
"total": {
|
||||
"elapsed_seconds": 32.875,
|
||||
"attempts": 32,
|
||||
"successes": 32,
|
||||
"errors": 0,
|
||||
"error_ratio": 0.0,
|
||||
"successful_rps": 0.973,
|
||||
"successful_latency_ms": {
|
||||
"p50": 2032.0,
|
||||
"p95": 2093.0,
|
||||
"p99": 2094.0
|
||||
}
|
||||
},
|
||||
"during_and_after_action": {
|
||||
"elapsed_seconds": 31.875,
|
||||
"attempts": 30,
|
||||
"successes": 30,
|
||||
"errors": 0,
|
||||
"error_ratio": 0.0,
|
||||
"successful_rps": 0.941,
|
||||
"successful_latency_ms": {
|
||||
"p50": 2032.0,
|
||||
"p95": 2093.0,
|
||||
"p99": 2094.0
|
||||
}
|
||||
},
|
||||
"confirmed_responses": 17,
|
||||
"history_messages": 17,
|
||||
"duplicates": 0,
|
||||
"existing_session_works": true,
|
||||
"first_write_after_action_seconds": 21.547,
|
||||
"write": {
|
||||
"elapsed_seconds": 32.875,
|
||||
"attempts": 16,
|
||||
"successes": 16,
|
||||
"errors": 0,
|
||||
"error_ratio": 0.0,
|
||||
"successful_rps": 0.487,
|
||||
"successful_latency_ms": {
|
||||
"p50": 2046.0,
|
||||
"p95": 2094.0,
|
||||
"p99": 2094.0
|
||||
}
|
||||
},
|
||||
"first_read_after_action_seconds": 23.578,
|
||||
"read": {
|
||||
"elapsed_seconds": 32.875,
|
||||
"attempts": 16,
|
||||
"successes": 16,
|
||||
"errors": 0,
|
||||
"error_ratio": 0.0,
|
||||
"successful_rps": 0.487,
|
||||
"successful_latency_ms": {
|
||||
"p50": 2031.0,
|
||||
"p95": 2093.0,
|
||||
"p99": 2093.0
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
{
|
||||
"mode": "replace",
|
||||
"service": "api-b",
|
||||
"target_instance": "b7542c30b09c43bf941aaade921e6640",
|
||||
"observed_with_one_session": [
|
||||
"8e1cb6e2bcd143fea7f8b677409738f5",
|
||||
"b7542c30b09c43bf941aaade921e6640"
|
||||
],
|
||||
"before": [
|
||||
{
|
||||
"service": "api-a",
|
||||
"container_id": "ea9d5889a8f0c6ff908a726f8fbe787eef209d33c30b1195090f822fb742bdbb",
|
||||
"pid": 1674,
|
||||
"state": "running",
|
||||
"started_at": "2026-11-02T14:35:20.182460357Z",
|
||||
"status": 200,
|
||||
"instance_id": "8e1cb6e2bcd143fea7f8b677409738f5"
|
||||
},
|
||||
{
|
||||
"service": "api-b",
|
||||
"container_id": "4cccbf699f55df2a5e9fd6e19aa897796654a47cdac2ff372565fa6c2a4677e0",
|
||||
"pid": 93836,
|
||||
"state": "running",
|
||||
"started_at": "2026-11-02T14:32:20.000834613Z",
|
||||
"status": 200,
|
||||
"instance_id": "b7542c30b09c43bf941aaade921e6640"
|
||||
}
|
||||
],
|
||||
"after_action": [
|
||||
{
|
||||
"service": "api-a",
|
||||
"container_id": "ea9d5889a8f0c6ff908a726f8fbe787eef209d33c30b1195090f822fb742bdbb",
|
||||
"pid": 1674,
|
||||
"state": "running",
|
||||
"started_at": "2026-11-02T14:35:20.182460357Z",
|
||||
"status": 200,
|
||||
"instance_id": "8e1cb6e2bcd143fea7f8b677409738f5"
|
||||
},
|
||||
{
|
||||
"service": "api-b",
|
||||
"container_id": "08d347efa257b234f70797becec6fa40110201dc05ca6ac4ef27ade4c8af2fec",
|
||||
"pid": 5532,
|
||||
"state": "running",
|
||||
"started_at": "2026-11-02T14:36:45.127161791Z",
|
||||
"status": 200,
|
||||
"instance_id": "8cc241fbc3c64938bf061ae1cb6bf86a"
|
||||
}
|
||||
],
|
||||
"action": {
|
||||
"service": "api-b",
|
||||
"before": "b7542c30b09c43bf941aaade921e6640",
|
||||
"after": "8cc241fbc3c64938bf061ae1cb6bf86a"
|
||||
},
|
||||
"action_seconds": 18.453,
|
||||
"concurrency": 2,
|
||||
"total": {
|
||||
"elapsed_seconds": 32.906,
|
||||
"attempts": 32,
|
||||
"successes": 32,
|
||||
"errors": 0,
|
||||
"error_ratio": 0.0,
|
||||
"successful_rps": 0.972,
|
||||
"successful_latency_ms": {
|
||||
"p50": 2031.0,
|
||||
"p95": 2094.0,
|
||||
"p99": 2109.0
|
||||
}
|
||||
},
|
||||
"during_and_after_action": {
|
||||
"elapsed_seconds": 31.906,
|
||||
"attempts": 30,
|
||||
"successes": 30,
|
||||
"errors": 0,
|
||||
"error_ratio": 0.0,
|
||||
"successful_rps": 0.94,
|
||||
"successful_latency_ms": {
|
||||
"p50": 2031.0,
|
||||
"p95": 2094.0,
|
||||
"p99": 2109.0
|
||||
}
|
||||
},
|
||||
"confirmed_responses": 17,
|
||||
"history_messages": 17,
|
||||
"duplicates": 0,
|
||||
"existing_session_works": true,
|
||||
"first_write_after_action_seconds": 21.672,
|
||||
"write": {
|
||||
"elapsed_seconds": 32.906,
|
||||
"attempts": 16,
|
||||
"successes": 16,
|
||||
"errors": 0,
|
||||
"error_ratio": 0.0,
|
||||
"successful_rps": 0.486,
|
||||
"successful_latency_ms": {
|
||||
"p50": 2031.0,
|
||||
"p95": 2094.0,
|
||||
"p99": 2094.0
|
||||
}
|
||||
},
|
||||
"first_read_after_action_seconds": 23.688,
|
||||
"read": {
|
||||
"elapsed_seconds": 32.906,
|
||||
"attempts": 16,
|
||||
"successes": 16,
|
||||
"errors": 0,
|
||||
"error_ratio": 0.0,
|
||||
"successful_rps": 0.486,
|
||||
"successful_latency_ms": {
|
||||
"p50": 2031.0,
|
||||
"p95": 2109.0,
|
||||
"p99": 2109.0
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
$ C:\Users\ira\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\python.exe tests/resilience.py --mode persistence --action-script scripts/restart.sh --base-url http://localhost: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.
|
||||
|
||||
exit_code=0; elapsed_seconds=42.250
|
||||
@@ -0,0 +1,20 @@
|
||||
[
|
||||
{
|
||||
"service": "api-a",
|
||||
"container_id": "2fd96ffc2d8d68c26da16bf09245a0172173d8bd94ec6a20cd746efa7b080bda",
|
||||
"pid": 8131,
|
||||
"state": "running",
|
||||
"started_at": "2026-11-02T14:37:40.580307643Z",
|
||||
"status": 200,
|
||||
"instance_id": "1fe93f99807c40ad873ed0e55339418a"
|
||||
},
|
||||
{
|
||||
"service": "api-b",
|
||||
"container_id": "876768b9146eb58906607265215fa30d8e29a58b40b91d701982c0ce0f470d90",
|
||||
"pid": 8121,
|
||||
"state": "running",
|
||||
"started_at": "2026-11-02T14:37:40.480238226Z",
|
||||
"status": 200,
|
||||
"instance_id": "2fb27071a2b0448880987581ff45a8bf"
|
||||
}
|
||||
]
|
||||
@@ -0,0 +1,47 @@
|
||||
$ docker compose --env-file .env exec -T nginx nginx -T -c /opt/lb/nginx.conf
|
||||
# configuration file /opt/lb/nginx.conf:
|
||||
worker_processes 1;
|
||||
pid /tmp/nginx.pid;
|
||||
error_log /dev/stderr warn;
|
||||
worker_shutdown_timeout 5s;
|
||||
events { worker_connections 1024; }
|
||||
http {
|
||||
log_format upstream '$request_method $uri status=$status upstream=$upstream_addr upstream_status=$upstream_status time=$request_time';
|
||||
access_log /dev/stdout upstream;
|
||||
resolver 127.0.0.11 valid=1s ipv6=off;
|
||||
resolver_timeout 1s;
|
||||
include /tmp/backends.conf;
|
||||
server {
|
||||
listen 8080;
|
||||
server_tokens off;
|
||||
client_max_body_size 256k;
|
||||
client_header_timeout 5s;
|
||||
client_body_timeout 5s;
|
||||
keepalive_timeout 2s;
|
||||
location / {
|
||||
proxy_pass http://api_backend;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Connection "";
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_connect_timeout 1s;
|
||||
proxy_read_timeout 8s;
|
||||
proxy_send_timeout 8s;
|
||||
proxy_next_upstream error timeout http_502 http_503 http_504;
|
||||
proxy_next_upstream_tries 2;
|
||||
proxy_next_upstream_timeout 9s;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# configuration file /tmp/backends.conf:
|
||||
upstream api_backend {
|
||||
zone api_backend 64k;
|
||||
server api-a:8080 resolve max_fails=1 fail_timeout=2s;
|
||||
server api-b:8080 resolve max_fails=1 fail_timeout=2s;
|
||||
}
|
||||
|
||||
nginx: the configuration file /opt/lb/nginx.conf syntax is ok
|
||||
nginx: configuration file /opt/lb/nginx.conf test is successful
|
||||
|
||||
exit_code=0; elapsed_seconds=0.359
|
||||
@@ -0,0 +1,303 @@
|
||||
$ docker compose --env-file .env --profile test run --rm --build tests
|
||||
#0 building with "desktop-linux" instance using docker driver
|
||||
|
||||
#1 [tests internal] load build definition from Dockerfile
|
||||
#1 transferring dockerfile: 955B done
|
||||
#1 DONE 0.0s
|
||||
|
||||
#2 [tests internal] load metadata for docker.io/library/python:3.12.8-slim-bookworm
|
||||
#2 ...
|
||||
|
||||
#3 [tests auth] library/python:pull token for registry-1.docker.io
|
||||
#3 DONE 0.0s
|
||||
|
||||
#2 [tests internal] load metadata for docker.io/library/python:3.12.8-slim-bookworm
|
||||
#2 DONE 1.4s
|
||||
|
||||
#4 [tests internal] load .dockerignore
|
||||
#4 transferring context: 196B done
|
||||
#4 DONE 0.0s
|
||||
|
||||
#5 [tests base 1/6] FROM docker.io/library/python:3.12.8-slim-bookworm@sha256:2199a62885a12290dc9c5be3ca0681d367576ab7bf037da120e564723292a2f0
|
||||
#5 DONE 0.0s
|
||||
|
||||
#6 [tests internal] load build context
|
||||
#6 transferring context: 2.00kB done
|
||||
#6 DONE 0.0s
|
||||
|
||||
#7 [tests test 3/5] COPY pytest.ini ./
|
||||
#7 CACHED
|
||||
|
||||
#8 [tests test 2/5] RUN pip install -r requirements-test.txt
|
||||
#8 CACHED
|
||||
|
||||
#9 [tests base 2/6] RUN groupadd --system --gid 10001 app && useradd --system --uid 10001 --gid app --no-create-home --shell /usr/sbin/nologin app
|
||||
#9 CACHED
|
||||
|
||||
#10 [tests base 5/6] RUN pip install -r requirements.txt
|
||||
#10 CACHED
|
||||
|
||||
#11 [tests test 4/5] COPY student-tests ./student-tests
|
||||
#11 CACHED
|
||||
|
||||
#12 [tests base 4/6] COPY requirements.txt ./
|
||||
#12 CACHED
|
||||
|
||||
#13 [tests test 1/5] COPY requirements-test.txt ./
|
||||
#13 CACHED
|
||||
|
||||
#14 [tests base 6/6] COPY app ./app
|
||||
#14 CACHED
|
||||
|
||||
#15 [tests base 3/6] WORKDIR /srv
|
||||
#15 CACHED
|
||||
|
||||
#16 [tests test 5/5] COPY unit-tests ./unit-tests
|
||||
#16 CACHED
|
||||
|
||||
#17 [tests] exporting to image
|
||||
#17 exporting layers done
|
||||
#17 writing image sha256:c4492757aa5070ae7b09009339bfa52d41b2ef1a39beb1d7dde355a0c2fa257e done
|
||||
#17 naming to docker.io/library/mtusi-messenger:lab3-test done
|
||||
#17 DONE 0.0s
|
||||
============================= test session starts ==============================
|
||||
collecting ... collected 226 items
|
||||
|
||||
student-tests/test_auth.py::test_register_returns_user_without_secrets PASSED [ 0%]
|
||||
student-tests/test_auth.py::test_register_password_rules[None] PASSED [ 0%]
|
||||
student-tests/test_auth.py::test_register_password_rules[] PASSED [ 1%]
|
||||
student-tests/test_auth.py::test_register_password_rules[short] PASSED [ 1%]
|
||||
student-tests/test_auth.py::test_register_password_rules[aaaaaaaaaaa] PASSED [ 2%]
|
||||
student-tests/test_auth.py::test_register_password_rules[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa] PASSED [ 2%]
|
||||
student-tests/test_auth.py::test_register_password_rules[123456789012] PASSED [ 3%]
|
||||
student-tests/test_auth.py::test_register_password_rules[\u043f\u0430\u0440\u043e\u043b\u044c] PASSED [ 3%]
|
||||
student-tests/test_auth.py::test_register_password_border_lengths PASSED [ 3%]
|
||||
student-tests/test_auth.py::test_login_success_shape PASSED [ 4%]
|
||||
student-tests/test_auth.py::test_each_login_gives_new_token PASSED [ 4%]
|
||||
student-tests/test_auth.py::test_wrong_password_and_unknown_user_look_the_same PASSED [ 5%]
|
||||
student-tests/test_auth.py::test_malformed_basic_is_401[Basic] PASSED [ 5%]
|
||||
student-tests/test_auth.py::test_malformed_basic_is_401[Basic !!!not-base64!!!] PASSED [ 6%]
|
||||
student-tests/test_auth.py::test_malformed_basic_is_401[Basic bm9jb2xvbg==] PASSED [ 6%]
|
||||
student-tests/test_auth.py::test_malformed_basic_is_401[Basic 0LjQvNGPOnBhc3N3b3JkLTEyMzQ1Ng==] PASSED [ 7%]
|
||||
student-tests/test_auth.py::test_malformed_basic_is_401[Basic dXNlcjr//i1iYWQtdXRmOA==] PASSED [ 7%]
|
||||
student-tests/test_auth.py::test_malformed_basic_is_401[Bearer abc] PASSED [ 7%]
|
||||
student-tests/test_auth.py::test_malformed_basic_is_401[] PASSED [ 8%]
|
||||
student-tests/test_auth.py::test_login_without_header_is_401 PASSED [ 8%]
|
||||
student-tests/test_auth.py::test_password_with_colon_and_unicode_works PASSED [ 9%]
|
||||
student-tests/test_auth.py::test_bearer_token_gives_access PASSED [ 9%]
|
||||
student-tests/test_auth.py::test_business_endpoint_rejects_basic PASSED [ 10%]
|
||||
student-tests/test_auth.py::test_bad_or_unknown_bearer_is_401[] PASSED [ 10%]
|
||||
student-tests/test_auth.py::test_bad_or_unknown_bearer_is_401[Bearer] PASSED [ 11%]
|
||||
student-tests/test_auth.py::test_bad_or_unknown_bearer_is_401[Bearer short] PASSED [ 11%]
|
||||
student-tests/test_auth.py::test_bad_or_unknown_bearer_is_401[Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx] PASSED [ 11%]
|
||||
student-tests/test_auth.py::test_bad_or_unknown_bearer_is_401[Token abc] PASSED [ 12%]
|
||||
student-tests/test_auth.py::test_bad_or_unknown_bearer_is_401[bearer] PASSED [ 12%]
|
||||
student-tests/test_auth.py::test_x_user_id_alone_gives_no_access PASSED [ 13%]
|
||||
student-tests/test_auth.py::test_x_user_id_does_not_change_owner_of_session PASSED [ 13%]
|
||||
student-tests/test_auth.py::test_logout_revokes_token_and_repeat_is_401 PASSED [ 14%]
|
||||
student-tests/test_auth.py::test_logout_without_token_is_401 PASSED [ 14%]
|
||||
student-tests/test_auth.py::test_logout_revokes_only_current_session PASSED [ 15%]
|
||||
student-tests/test_auth.py::test_session_lives_longer_than_300_seconds PASSED [ 15%]
|
||||
student-tests/test_cache.py::test_user_profile_is_served_from_cache_with_bounded_ttl PASSED [ 15%]
|
||||
student-tests/test_cache.py::test_cache_never_stores_raw_tokens_or_passwords PASSED [ 16%]
|
||||
student-tests/test_cache.py::test_logout_revokes_cached_session PASSED [ 16%]
|
||||
student-tests/test_cache.py::test_late_cache_fill_cannot_resurrect_revoked_session PASSED [ 17%]
|
||||
student-tests/test_cache.py::test_flushed_cache_loses_nothing PASSED [ 17%]
|
||||
student-tests/test_cache.py::test_cache_does_not_extend_short_session PASSED [ 18%]
|
||||
student-tests/test_cache.py::test_staleness_after_out_of_band_delete_is_bounded_by_cache_ttl PASSED [ 18%]
|
||||
student-tests/test_cache.py::test_redis_down_requests_bypass_cache_and_stay_fast PASSED [ 19%]
|
||||
student-tests/test_cache.py::test_redis_down_never_trusts_anonymous_or_unknown_tokens PASSED [ 19%]
|
||||
student-tests/test_cache.py::test_revocation_lost_while_redis_down_is_flushed_on_recovery PASSED [ 19%]
|
||||
student-tests/test_cache.py::test_without_cache_url_cache_is_off PASSED [ 20%]
|
||||
student-tests/test_cache.py::test_lost_revocation_survives_api_restart[False] PASSED [ 20%]
|
||||
student-tests/test_cache.py::test_lost_revocation_survives_api_restart[True] PASSED [ 21%]
|
||||
student-tests/test_cache.py::test_cached_session_deleted_in_database_is_rejected_immediately PASSED [ 21%]
|
||||
student-tests/test_concurrency.py::test_100_messages_from_10_clients_in_parallel PASSED [ 22%]
|
||||
student-tests/test_concurrency.py::test_parallel_chat_creation_and_reading PASSED [ 22%]
|
||||
student-tests/test_contract_basics.py::test_health_endpoints PASSED [ 23%]
|
||||
student-tests/test_contract_basics.py::test_every_response_has_unique_request_id PASSED [ 23%]
|
||||
student-tests/test_contract_basics.py::test_unknown_path_is_404_in_common_format PASSED [ 23%]
|
||||
student-tests/test_contract_basics.py::test_trailing_slash_is_not_redirected PASSED [ 24%]
|
||||
student-tests/test_contract_basics.py::test_wrong_method_is_405_in_common_format PASSED [ 24%]
|
||||
student-tests/test_contract_basics.py::test_two_users_one_chat_two_messages PASSED [ 25%]
|
||||
student-tests/test_contract_basics.py::test_chat_response_shape PASSED [ 25%]
|
||||
student-tests/test_contract_basics.py::test_unexpected_exception_gives_500_without_details PASSED [ 26%]
|
||||
student-tests/test_contract_basics.py::test_logs_have_requests_but_no_secrets PASSED [ 26%]
|
||||
student-tests/test_database.py::test_password_is_stored_as_scrypt_hash_not_plaintext PASSED [ 26%]
|
||||
student-tests/test_database.py::test_same_password_gives_different_hashes PASSED [ 27%]
|
||||
student-tests/test_database.py::test_session_table_keeps_token_hash_not_token PASSED [ 27%]
|
||||
student-tests/test_database.py::test_message_is_really_in_the_database PASSED [ 28%]
|
||||
student-tests/test_database.py::test_migrations_can_run_again_without_duplicating_anything PASSED [ 28%]
|
||||
student-tests/test_database.py::test_expected_tables_and_indexes_exist PASSED [ 29%]
|
||||
student-tests/test_database.py::test_primary_and_foreign_keys_are_declared PASSED [ 29%]
|
||||
student-tests/test_database.py::test_database_itself_rejects_bad_rows PASSED [ 30%]
|
||||
student-tests/test_database.py::test_chat_creation_is_atomic PASSED [ 30%]
|
||||
student-tests/test_database.py::test_successful_chat_has_all_members_in_database PASSED [ 30%]
|
||||
student-tests/test_db_failure.py::test_database_down_gives_503_and_never_trusts_anyone PASSED [ 31%]
|
||||
student-tests/test_db_failure.py::test_database_down_with_cache_up_still_needs_database_for_data PASSED [ 31%]
|
||||
student-tests/test_duplicate_username.py::test_second_registration_with_same_username_is_409 PASSED [ 32%]
|
||||
student-tests/test_duplicate_username.py::test_duplicate_does_not_replace_original_user PASSED [ 32%]
|
||||
student-tests/test_duplicate_username.py::test_username_is_case_sensitive_lowercase_only PASSED [ 33%]
|
||||
student-tests/test_duplicate_username.py::test_parallel_registration_of_same_username_creates_exactly_one PASSED [ 33%]
|
||||
student-tests/test_empty_message.py::test_blank_text_is_rejected[] PASSED [ 34%]
|
||||
student-tests/test_empty_message.py::test_blank_text_is_rejected[ ] PASSED [ 34%]
|
||||
student-tests/test_empty_message.py::test_blank_text_is_rejected[ ] PASSED [ 34%]
|
||||
student-tests/test_empty_message.py::test_blank_text_is_rejected[\n] PASSED [ 35%]
|
||||
student-tests/test_empty_message.py::test_blank_text_is_rejected[\t \n] PASSED [ 35%]
|
||||
student-tests/test_empty_message.py::test_blank_text_is_rejected[\xa0] PASSED [ 36%]
|
||||
student-tests/test_empty_message.py::test_blank_text_is_rejected[\u2003] PASSED [ 36%]
|
||||
student-tests/test_empty_message.py::test_missing_or_wrong_type_text_is_rejected[body0] PASSED [ 37%]
|
||||
student-tests/test_empty_message.py::test_missing_or_wrong_type_text_is_rejected[body1] PASSED [ 37%]
|
||||
student-tests/test_empty_message.py::test_missing_or_wrong_type_text_is_rejected[body2] PASSED [ 38%]
|
||||
student-tests/test_empty_message.py::test_missing_or_wrong_type_text_is_rejected[body3] PASSED [ 38%]
|
||||
student-tests/test_empty_message.py::test_missing_or_wrong_type_text_is_rejected[body4] PASSED [ 38%]
|
||||
student-tests/test_empty_message.py::test_text_length_is_counted_in_characters PASSED [ 39%]
|
||||
student-tests/test_empty_message.py::test_nul_character_is_400_not_500 PASSED [ 39%]
|
||||
student-tests/test_empty_message.py::test_unicode_survives_the_database PASSED [ 40%]
|
||||
student-tests/test_empty_message.py::test_message_has_correct_fields PASSED [ 40%]
|
||||
student-tests/test_explain.py::test_history_page_uses_index_without_full_scan[0] PASSED [ 41%]
|
||||
student-tests/test_explain.py::test_history_page_uses_index_without_full_scan[9000] PASSED [ 41%]
|
||||
student-tests/test_foreign_chat.py::test_outsider_cannot_read_chat PASSED [ 42%]
|
||||
student-tests/test_foreign_chat.py::test_outsider_cannot_read_history PASSED [ 42%]
|
||||
student-tests/test_foreign_chat.py::test_outsider_cannot_write PASSED [ 42%]
|
||||
student-tests/test_foreign_chat.py::test_outsider_gets_404_even_with_bad_body PASSED [ 43%]
|
||||
student-tests/test_foreign_chat.py::test_unknown_chat_looks_the_same_as_foreign PASSED [ 43%]
|
||||
student-tests/test_foreign_chat.py::test_chat_list_shows_only_own_chats PASSED [ 44%]
|
||||
student-tests/test_foreign_chat.py::test_bad_chat_id_is_400 PASSED [ 44%]
|
||||
student-tests/test_foreign_chat.py::test_no_token_is_401_everywhere PASSED [ 45%]
|
||||
student-tests/test_foreign_chat.py::test_401_comes_before_404 PASSED [ 45%]
|
||||
student-tests/test_foreign_chat.py::test_member_can_read_and_write PASSED [ 46%]
|
||||
student-tests/test_idempotency.py::test_key_is_required_and_validated[None] PASSED [ 46%]
|
||||
student-tests/test_idempotency.py::test_key_is_required_and_validated[] PASSED [ 46%]
|
||||
student-tests/test_idempotency.py::test_key_is_required_and_validated[a b] PASSED [ 47%]
|
||||
student-tests/test_idempotency.py::test_key_is_required_and_validated[a?b] PASSED [ 47%]
|
||||
student-tests/test_idempotency.py::test_key_is_required_and_validated[xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx] PASSED [ 48%]
|
||||
student-tests/test_idempotency.py::test_key_is_required_and_validated[a,b] PASSED [ 48%]
|
||||
student-tests/test_idempotency.py::test_key_is_required_and_validated[a/b] PASSED [ 49%]
|
||||
student-tests/test_idempotency.py::test_duplicate_headers_are_invalid PASSED [ 49%]
|
||||
student-tests/test_idempotency.py::test_replay_and_conflict_preserve_original_result PASSED [ 50%]
|
||||
student-tests/test_idempotency.py::test_parallel_same_key_across_two_applications PASSED [ 50%]
|
||||
student-tests/test_idempotency.py::test_key_scope_includes_user_and_chat PASSED [ 50%]
|
||||
student-tests/test_idempotency.py::test_expired_result_allows_new_message PASSED [ 51%]
|
||||
student-tests/test_idempotency.py::test_failure_between_message_and_result_rolls_back PASSED [ 51%]
|
||||
student-tests/test_idempotency.py::test_committed_but_lost_response_is_safe_to_retry PASSED [ 52%]
|
||||
student-tests/test_idempotency.py::test_replay_rechecks_session_and_chat_access PASSED [ 52%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_broken_json[{] PASSED [ 53%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_broken_json[not json at all] PASSED [ 53%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_broken_json[] PASSED [ 53%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_broken_json[{"username": "abc",}] PASSED [ 54%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_broken_json[\xff\xfe\x00] PASSED [ 54%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_broken_json[{"username": NaN}] PASSED [ 55%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_broken_json[["username"]] PASSED [ 55%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_broken_json["text"] PASSED [ 56%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_broken_json[null] PASSED [ 56%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes0] PASSED [ 57%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes1] PASSED [ 57%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes2] PASSED [ 57%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes3] PASSED [ 58%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes4] PASSED [ 58%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes5] PASSED [ 59%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes6] PASSED [ 59%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes7] PASSED [ 60%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes8] PASSED [ 60%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes9] PASSED [ 61%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes10] PASSED [ 61%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes11] PASSED [ 61%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes12] PASSED [ 62%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes13] PASSED [ 62%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes14] PASSED [ 63%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes15] PASSED [ 63%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes16] PASSED [ 64%]
|
||||
student-tests/test_invalid_json.py::test_border_values_are_accepted PASSED [ 64%]
|
||||
student-tests/test_invalid_json.py::test_lone_surrogate_is_400_not_500 PASSED [ 65%]
|
||||
student-tests/test_invalid_json.py::test_wrong_or_missing_content_type_is_415 PASSED [ 65%]
|
||||
student-tests/test_invalid_json.py::test_huge_body_is_413 PASSED [ 65%]
|
||||
student-tests/test_invalid_json.py::test_broken_json_in_message_and_chat PASSED [ 66%]
|
||||
student-tests/test_invalid_json.py::test_sender_id_in_message_is_rejected PASSED [ 66%]
|
||||
student-tests/test_invalid_json.py::test_chat_body_validation PASSED [ 67%]
|
||||
student-tests/test_pagination.py::test_walk_25_messages_by_7 PASSED [ 67%]
|
||||
student-tests/test_pagination.py::test_exact_multiple_has_no_empty_extra_page PASSED [ 68%]
|
||||
student-tests/test_pagination.py::test_limit_one_walks_everything PASSED [ 68%]
|
||||
student-tests/test_pagination.py::test_default_limit_is_50_and_max_is_100 PASSED [ 69%]
|
||||
student-tests/test_pagination.py::test_empty_history PASSED [ 69%]
|
||||
student-tests/test_pagination.py::test_limit_can_change_between_pages PASSED [ 69%]
|
||||
student-tests/test_pagination.py::test_messages_sent_during_walk_appear_at_the_end PASSED [ 70%]
|
||||
student-tests/test_pagination.py::test_created_at_is_increasing_and_utc PASSED [ 70%]
|
||||
student-tests/test_pagination.py::test_bad_limit_values PASSED [ 71%]
|
||||
student-tests/test_pagination.py::test_garbage_cursor_is_400 PASSED [ 71%]
|
||||
student-tests/test_pagination.py::test_cursor_from_another_chat_is_400 PASSED [ 72%]
|
||||
student-tests/test_pagination.py::test_chat_list_pagination PASSED [ 72%]
|
||||
student-tests/test_pagination.py::test_chat_list_cursor_cannot_be_used_for_messages PASSED [ 73%]
|
||||
student-tests/test_pagination.py::test_invalid_cursor_timestamp_is_400[messages--1] PASSED [ 73%]
|
||||
student-tests/test_pagination.py::test_invalid_cursor_timestamp_is_400[messages-1000000000000000000000000000000] PASSED [ 73%]
|
||||
student-tests/test_pagination.py::test_invalid_cursor_timestamp_is_400[messages-253402300800000000] PASSED [ 74%]
|
||||
student-tests/test_pagination.py::test_invalid_cursor_timestamp_is_400[messages-True] PASSED [ 74%]
|
||||
student-tests/test_pagination.py::test_invalid_cursor_timestamp_is_400[messages-1.5] PASSED [ 75%]
|
||||
student-tests/test_pagination.py::test_invalid_cursor_timestamp_is_400[messages-1] PASSED [ 75%]
|
||||
student-tests/test_pagination.py::test_invalid_cursor_timestamp_is_400[messages-None] PASSED [ 76%]
|
||||
student-tests/test_pagination.py::test_invalid_cursor_timestamp_is_400[chats--1] PASSED [ 76%]
|
||||
student-tests/test_pagination.py::test_invalid_cursor_timestamp_is_400[chats-1000000000000000000000000000000] PASSED [ 76%]
|
||||
student-tests/test_pagination.py::test_invalid_cursor_timestamp_is_400[chats-253402300800000000] PASSED [ 77%]
|
||||
student-tests/test_pagination.py::test_invalid_cursor_timestamp_is_400[chats-True] PASSED [ 77%]
|
||||
student-tests/test_pagination.py::test_invalid_cursor_timestamp_is_400[chats-1.5] PASSED [ 78%]
|
||||
student-tests/test_pagination.py::test_invalid_cursor_timestamp_is_400[chats-1] PASSED [ 78%]
|
||||
student-tests/test_pagination.py::test_invalid_cursor_timestamp_is_400[chats-None] PASSED [ 79%]
|
||||
student-tests/test_pagination.py::test_cursor_timestamp_boundaries_are_valid[messages-0] PASSED [ 79%]
|
||||
student-tests/test_pagination.py::test_cursor_timestamp_boundaries_are_valid[messages-253402300799999999] PASSED [ 80%]
|
||||
student-tests/test_pagination.py::test_cursor_timestamp_boundaries_are_valid[chats-0] PASSED [ 80%]
|
||||
student-tests/test_pagination.py::test_cursor_timestamp_boundaries_are_valid[chats-253402300799999999] PASSED [ 80%]
|
||||
student-tests/test_session_expiry.py::test_session_expires_after_ttl PASSED [ 81%]
|
||||
student-tests/test_session_expiry.py::test_expired_sessions_are_cleaned_on_next_login PASSED [ 81%]
|
||||
student-tests/test_session_expiry.py::test_short_ttl_from_environment_variable PASSED [ 82%]
|
||||
student-tests/test_transactions.py::test_failure_after_chat_insert_rolls_back_whole_operation PASSED [ 82%]
|
||||
student-tests/test_transactions.py::test_pool_is_bounded_by_settings PASSED [ 83%]
|
||||
student-tests/test_transactions.py::test_exhausted_pool_gives_fast_503 PASSED [ 83%]
|
||||
unit-tests/test_message_transaction.py::test_conflicting_replay_rolls_back_without_commit PASSED [ 84%]
|
||||
unit-tests/test_message_transaction.py::test_result_insert_failure_never_commits_message PASSED [ 84%]
|
||||
unit-tests/test_message_transaction.py::test_replay_returns_original_message_and_releases_transaction PASSED [ 84%]
|
||||
unit-tests/test_message_transaction.py::test_failed_commit_rolls_back_and_propagates_error PASSED [ 85%]
|
||||
unit-tests/test_mr_regressions.py::test_stale_positive_cache_never_authorizes_revoked_token[/api/v1/users/me-GET] PASSED [ 85%]
|
||||
unit-tests/test_mr_regressions.py::test_stale_positive_cache_never_authorizes_revoked_token[/api/v1/chats/00000000-0000-0000-0000-000000000002/messages-POST] PASSED [ 86%]
|
||||
unit-tests/test_mr_regressions.py::test_stale_positive_cache_never_authorizes_revoked_token[/api/v1/auth/sessions/current-DELETE] PASSED [ 86%]
|
||||
unit-tests/test_mr_regressions.py::test_valid_cache_hit_checks_session_without_loading_user PASSED [ 87%]
|
||||
unit-tests/test_mr_regressions.py::test_database_failure_on_positive_cache_hit_is_503 PASSED [ 87%]
|
||||
unit-tests/test_mr_regressions.py::test_cursor_timestamp_is_rejected_through_http[messages--1] PASSED [ 88%]
|
||||
unit-tests/test_mr_regressions.py::test_cursor_timestamp_is_rejected_through_http[messages-253402300800000000] PASSED [ 88%]
|
||||
unit-tests/test_mr_regressions.py::test_cursor_timestamp_is_rejected_through_http[messages-1000000000000000000000000000000] PASSED [ 88%]
|
||||
unit-tests/test_mr_regressions.py::test_cursor_timestamp_is_rejected_through_http[messages-True] PASSED [ 89%]
|
||||
unit-tests/test_mr_regressions.py::test_cursor_timestamp_is_rejected_through_http[messages-1.5] PASSED [ 89%]
|
||||
unit-tests/test_mr_regressions.py::test_cursor_timestamp_is_rejected_through_http[messages-1] PASSED [ 90%]
|
||||
unit-tests/test_mr_regressions.py::test_cursor_timestamp_is_rejected_through_http[messages-None] PASSED [ 90%]
|
||||
unit-tests/test_mr_regressions.py::test_cursor_timestamp_is_rejected_through_http[chats--1] PASSED [ 91%]
|
||||
unit-tests/test_mr_regressions.py::test_cursor_timestamp_is_rejected_through_http[chats-253402300800000000] PASSED [ 91%]
|
||||
unit-tests/test_mr_regressions.py::test_cursor_timestamp_is_rejected_through_http[chats-1000000000000000000000000000000] PASSED [ 92%]
|
||||
unit-tests/test_mr_regressions.py::test_cursor_timestamp_is_rejected_through_http[chats-True] PASSED [ 92%]
|
||||
unit-tests/test_mr_regressions.py::test_cursor_timestamp_is_rejected_through_http[chats-1.5] PASSED [ 92%]
|
||||
unit-tests/test_mr_regressions.py::test_cursor_timestamp_is_rejected_through_http[chats-1] PASSED [ 93%]
|
||||
unit-tests/test_mr_regressions.py::test_cursor_timestamp_is_rejected_through_http[chats-None] PASSED [ 93%]
|
||||
unit-tests/test_mr_regressions.py::test_timestamp_boundaries_roundtrip[0] PASSED [ 94%]
|
||||
unit-tests/test_mr_regressions.py::test_timestamp_boundaries_roundtrip[253402300799999999] PASSED [ 94%]
|
||||
unit-tests/test_mr_regressions.py::test_message_key_validation[None] PASSED [ 95%]
|
||||
unit-tests/test_mr_regressions.py::test_message_key_validation[] PASSED [ 95%]
|
||||
unit-tests/test_mr_regressions.py::test_message_key_validation[bad key] PASSED [ 96%]
|
||||
unit-tests/test_mr_regressions.py::test_message_key_validation[xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx] PASSED [ 96%]
|
||||
unit-tests/test_mr_regressions.py::test_message_key_validation[a?b] PASSED [ 96%]
|
||||
unit-tests/test_mr_regressions.py::test_message_key_validation[a,b] PASSED [ 97%]
|
||||
unit-tests/test_mr_regressions.py::test_message_normalization_and_conflict PASSED [ 97%]
|
||||
unit-tests/test_mr_regressions.py::test_instance_header_matches_process_on_all_responses[/health/live] PASSED [ 98%]
|
||||
unit-tests/test_mr_regressions.py::test_instance_header_matches_process_on_all_responses[/missing] PASSED [ 98%]
|
||||
unit-tests/test_mr_regressions.py::test_instance_header_matches_process_on_all_responses[/api/v1/users/me] PASSED [ 99%]
|
||||
unit-tests/test_mr_regressions.py::test_instance_alias_changes_between_applications PASSED [ 99%]
|
||||
unit-tests/test_mr_regressions.py::test_sigterm_drops_readiness_and_requests_graceful_exit PASSED [100%]
|
||||
|
||||
=============================== warnings summary ===============================
|
||||
../usr/local/lib/python3.12/site-packages/fastapi/testclient.py:1
|
||||
/usr/local/lib/python3.12/site-packages/fastapi/testclient.py:1: StarletteDeprecationWarning: Using `httpx` with `starlette.testclient` is deprecated; install `httpx2` instead.
|
||||
from starlette.testclient import TestClient as TestClient # noqa
|
||||
|
||||
-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
|
||||
======================= 226 passed, 1 warning in 46.61s ========================
|
||||
Container mtusi-messenger-lab3-db-1 Running
|
||||
Container mtusi-messenger-lab3-redis-1 Running
|
||||
|
||||
exit_code=0; elapsed_seconds=52.328
|
||||
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"status": "PASS",
|
||||
"at_utc": "2026-11-02T14:38:48.559702+00:00"
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"at_utc": "2026-11-02T14:04:52.232537+00:00",
|
||||
"platform": "Windows-10-10.0.19045-SP0",
|
||||
"cpu_count": 16,
|
||||
"python": "3.11.9",
|
||||
"duration": 20,
|
||||
"workload": "2 closed-loop clients, POST+GET; uncertain POST retried with same key/body"
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
$ docker version
|
||||
Client:
|
||||
Version: 26.1.4
|
||||
API version: 1.45
|
||||
Go version: go1.21.11
|
||||
Git commit: 5650f9b
|
||||
Built: Wed Jun 5 11:29:54 2024
|
||||
OS/Arch: windows/amd64
|
||||
Context: desktop-linux
|
||||
|
||||
Server: Docker Desktop 4.31.1 (153621)
|
||||
Engine:
|
||||
Version: 26.1.4
|
||||
API version: 1.45 (minimum version 1.24)
|
||||
Go version: go1.21.11
|
||||
Git commit: de5c9cf
|
||||
Built: Wed Jun 5 11:29:22 2024
|
||||
OS/Arch: linux/amd64
|
||||
Experimental: false
|
||||
containerd:
|
||||
Version: 1.6.33
|
||||
GitCommit: d2d58213f83a351ca8f528a95fbd145f5654e957
|
||||
runc:
|
||||
Version: 1.1.12
|
||||
GitCommit: v1.1.12-0-g51d5e94
|
||||
docker-init:
|
||||
Version: 0.19.0
|
||||
GitCommit: de40ad0
|
||||
|
||||
exit_code=0; elapsed_seconds=0.094
|
||||
@@ -0,0 +1,3 @@
|
||||
$ docker compose --env-file .env config --quiet
|
||||
|
||||
exit_code=0; elapsed_seconds=0.297
|
||||
@@ -0,0 +1,8 @@
|
||||
$ docker inspect --format {{.Name}} cpus={{.HostConfig.NanoCpus}} memory={{.HostConfig.Memory}} ports={{json .HostConfig.PortBindings}} b5f9cdc58cfa1e5ba32d7219909ca0b02f6984e2d69ec6090872f7c1aa3d40b8 3923c6b10754bdf24cf03312f2cdfc47d0c57b626e413c15c6bf99584795866a 765a7b20a31ea3ea8cfd024fe9d4a938c46f4d23d743e4db310b354d357014d8 5675a02e89826f64b25026269d5dc9177a7172ce4922722875c4d986e8423125 1b3ad58c1dd2bec01048e78af86e3c9812172df3c4189eab27352b11a950673d
|
||||
/mtusi-messenger-lab3-api-a-1 cpus=500000000 memory=268435456 ports={}
|
||||
/mtusi-messenger-lab3-api-b-1 cpus=500000000 memory=268435456 ports={}
|
||||
/mtusi-messenger-lab3-db-1 cpus=1000000000 memory=536870912 ports={}
|
||||
/mtusi-messenger-lab3-nginx-1 cpus=250000000 memory=100663296 ports={"8080/tcp":[{"HostIp":"127.0.0.1","HostPort":"8080"}]}
|
||||
/mtusi-messenger-lab3-redis-1 cpus=250000000 memory=134217728 ports={}
|
||||
|
||||
exit_code=0; elapsed_seconds=0.062
|
||||
@@ -0,0 +1,8 @@
|
||||
$ docker compose --env-file .env ps -q
|
||||
b5f9cdc58cfa1e5ba32d7219909ca0b02f6984e2d69ec6090872f7c1aa3d40b8
|
||||
3923c6b10754bdf24cf03312f2cdfc47d0c57b626e413c15c6bf99584795866a
|
||||
765a7b20a31ea3ea8cfd024fe9d4a938c46f4d23d743e4db310b354d357014d8
|
||||
5675a02e89826f64b25026269d5dc9177a7172ce4922722875c4d986e8423125
|
||||
1b3ad58c1dd2bec01048e78af86e3c9812172df3c4189eab27352b11a950673d
|
||||
|
||||
exit_code=0; elapsed_seconds=0.407
|
||||
@@ -0,0 +1,20 @@
|
||||
[
|
||||
{
|
||||
"service": "api-a",
|
||||
"container_id": "b5f9cdc58cfa1e5ba32d7219909ca0b02f6984e2d69ec6090872f7c1aa3d40b8",
|
||||
"pid": 494,
|
||||
"state": "running",
|
||||
"started_at": "2026-11-02T13:54:44.930052336Z",
|
||||
"status": 200,
|
||||
"instance_id": "a9caf7e09cdc4d63bf6c0a51f92d38f3"
|
||||
},
|
||||
{
|
||||
"service": "api-b",
|
||||
"container_id": "3923c6b10754bdf24cf03312f2cdfc47d0c57b626e413c15c6bf99584795866a",
|
||||
"pid": 496,
|
||||
"state": "running",
|
||||
"started_at": "2026-11-02T13:54:44.929946678Z",
|
||||
"status": 200,
|
||||
"instance_id": "f8a853f11dcd42be9d37227923a8b62a"
|
||||
}
|
||||
]
|
||||
@@ -0,0 +1,4 @@
|
||||
$ C:\Users\ira\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\python.exe tests/smoke.py --base-url http://localhost:8080
|
||||
PASS: lab3 public API scenario; infrastructure and grading evidence remain separate.
|
||||
|
||||
exit_code=0; elapsed_seconds=67.235
|
||||
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"parallel_requests": 12,
|
||||
"unique_message_ids": 1,
|
||||
"history_rows": 1,
|
||||
"instances": [
|
||||
"a9caf7e09cdc4d63bf6c0a51f92d38f3",
|
||||
"f8a853f11dcd42be9d37227923a8b62a"
|
||||
],
|
||||
"conflict_status": 409
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
{
|
||||
"mode": "stop",
|
||||
"service": "api-a",
|
||||
"target_instance": "a9caf7e09cdc4d63bf6c0a51f92d38f3",
|
||||
"observed_with_one_session": [
|
||||
"a9caf7e09cdc4d63bf6c0a51f92d38f3",
|
||||
"f8a853f11dcd42be9d37227923a8b62a"
|
||||
],
|
||||
"before": [
|
||||
{
|
||||
"service": "api-a",
|
||||
"container_id": "b5f9cdc58cfa1e5ba32d7219909ca0b02f6984e2d69ec6090872f7c1aa3d40b8",
|
||||
"pid": 494,
|
||||
"state": "running",
|
||||
"started_at": "2026-11-02T13:54:44.930052336Z",
|
||||
"status": 200,
|
||||
"instance_id": "a9caf7e09cdc4d63bf6c0a51f92d38f3"
|
||||
},
|
||||
{
|
||||
"service": "api-b",
|
||||
"container_id": "3923c6b10754bdf24cf03312f2cdfc47d0c57b626e413c15c6bf99584795866a",
|
||||
"pid": 496,
|
||||
"state": "running",
|
||||
"started_at": "2026-11-02T13:54:44.929946678Z",
|
||||
"status": 200,
|
||||
"instance_id": "f8a853f11dcd42be9d37227923a8b62a"
|
||||
}
|
||||
],
|
||||
"after_action": [
|
||||
{
|
||||
"service": "api-a",
|
||||
"container_id": "b5f9cdc58cfa1e5ba32d7219909ca0b02f6984e2d69ec6090872f7c1aa3d40b8",
|
||||
"pid": 0,
|
||||
"state": "exited",
|
||||
"started_at": "2026-11-02T13:54:44.930052336Z"
|
||||
},
|
||||
{
|
||||
"service": "api-b",
|
||||
"container_id": "3923c6b10754bdf24cf03312f2cdfc47d0c57b626e413c15c6bf99584795866a",
|
||||
"pid": 496,
|
||||
"state": "running",
|
||||
"started_at": "2026-11-02T13:54:44.929946678Z",
|
||||
"status": 200,
|
||||
"instance_id": "f8a853f11dcd42be9d37227923a8b62a"
|
||||
}
|
||||
],
|
||||
"action": "{\"stopped_service\": \"api-a\", \"instance_id\": \"a9caf7e09cdc4d63bf6c0a51f92d38f3\"}\n",
|
||||
"action_seconds": 3.594,
|
||||
"concurrency": 2,
|
||||
"total": {
|
||||
"elapsed_seconds": 24.609,
|
||||
"attempts": 24,
|
||||
"successes": 24,
|
||||
"errors": 0,
|
||||
"error_ratio": 0.0,
|
||||
"successful_rps": 0.975,
|
||||
"successful_latency_ms": {
|
||||
"p50": 2031.0,
|
||||
"p95": 2047.0,
|
||||
"p99": 2078.0
|
||||
}
|
||||
},
|
||||
"during_and_after_action": {
|
||||
"elapsed_seconds": 23.609,
|
||||
"attempts": 22,
|
||||
"successes": 22,
|
||||
"errors": 0,
|
||||
"error_ratio": 0.0,
|
||||
"successful_rps": 0.932,
|
||||
"successful_latency_ms": {
|
||||
"p50": 2031.0,
|
||||
"p95": 2047.0,
|
||||
"p99": 2078.0
|
||||
}
|
||||
},
|
||||
"confirmed_responses": 13,
|
||||
"history_messages": 13,
|
||||
"duplicates": 0,
|
||||
"existing_session_works": true,
|
||||
"first_write_after_action_seconds": 9.25,
|
||||
"write": {
|
||||
"elapsed_seconds": 24.609,
|
||||
"attempts": 12,
|
||||
"successes": 12,
|
||||
"errors": 0,
|
||||
"error_ratio": 0.0,
|
||||
"successful_rps": 0.488,
|
||||
"successful_latency_ms": {
|
||||
"p50": 2031.0,
|
||||
"p95": 2078.0,
|
||||
"p99": 2078.0
|
||||
}
|
||||
},
|
||||
"first_read_after_action_seconds": 7.187,
|
||||
"read": {
|
||||
"elapsed_seconds": 24.609,
|
||||
"attempts": 12,
|
||||
"successes": 12,
|
||||
"errors": 0,
|
||||
"error_ratio": 0.0,
|
||||
"successful_rps": 0.488,
|
||||
"successful_latency_ms": {
|
||||
"p50": 2031.0,
|
||||
"p95": 2047.0,
|
||||
"p99": 2047.0
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
{
|
||||
"mode": "stop",
|
||||
"service": "api-b",
|
||||
"target_instance": "f8a853f11dcd42be9d37227923a8b62a",
|
||||
"observed_with_one_session": [
|
||||
"71f43777fc8842999fb52b8b37b54209",
|
||||
"f8a853f11dcd42be9d37227923a8b62a"
|
||||
],
|
||||
"before": [
|
||||
{
|
||||
"service": "api-a",
|
||||
"container_id": "b5f9cdc58cfa1e5ba32d7219909ca0b02f6984e2d69ec6090872f7c1aa3d40b8",
|
||||
"pid": 33274,
|
||||
"state": "running",
|
||||
"started_at": "2026-11-02T14:08:05.363422472Z",
|
||||
"status": 200,
|
||||
"instance_id": "71f43777fc8842999fb52b8b37b54209"
|
||||
},
|
||||
{
|
||||
"service": "api-b",
|
||||
"container_id": "3923c6b10754bdf24cf03312f2cdfc47d0c57b626e413c15c6bf99584795866a",
|
||||
"pid": 496,
|
||||
"state": "running",
|
||||
"started_at": "2026-11-02T13:54:44.929946678Z",
|
||||
"status": 200,
|
||||
"instance_id": "f8a853f11dcd42be9d37227923a8b62a"
|
||||
}
|
||||
],
|
||||
"after_action": [
|
||||
{
|
||||
"service": "api-a",
|
||||
"container_id": "b5f9cdc58cfa1e5ba32d7219909ca0b02f6984e2d69ec6090872f7c1aa3d40b8",
|
||||
"pid": 33274,
|
||||
"state": "running",
|
||||
"started_at": "2026-11-02T14:08:05.363422472Z",
|
||||
"status": 200,
|
||||
"instance_id": "71f43777fc8842999fb52b8b37b54209"
|
||||
},
|
||||
{
|
||||
"service": "api-b",
|
||||
"container_id": "3923c6b10754bdf24cf03312f2cdfc47d0c57b626e413c15c6bf99584795866a",
|
||||
"pid": 0,
|
||||
"state": "exited",
|
||||
"started_at": "2026-11-02T13:54:44.929946678Z"
|
||||
}
|
||||
],
|
||||
"action": "{\"stopped_service\": \"api-b\", \"instance_id\": \"f8a853f11dcd42be9d37227923a8b62a\"}\n",
|
||||
"action_seconds": 3.922,
|
||||
"concurrency": 2,
|
||||
"total": {
|
||||
"elapsed_seconds": 24.672,
|
||||
"attempts": 24,
|
||||
"successes": 24,
|
||||
"errors": 0,
|
||||
"error_ratio": 0.0,
|
||||
"successful_rps": 0.973,
|
||||
"successful_latency_ms": {
|
||||
"p50": 2032.0,
|
||||
"p95": 2094.0,
|
||||
"p99": 2094.0
|
||||
}
|
||||
},
|
||||
"during_and_after_action": {
|
||||
"elapsed_seconds": 23.672,
|
||||
"attempts": 22,
|
||||
"successes": 22,
|
||||
"errors": 0,
|
||||
"error_ratio": 0.0,
|
||||
"successful_rps": 0.929,
|
||||
"successful_latency_ms": {
|
||||
"p50": 2032.0,
|
||||
"p95": 2094.0,
|
||||
"p99": 2094.0
|
||||
}
|
||||
},
|
||||
"confirmed_responses": 13,
|
||||
"history_messages": 13,
|
||||
"duplicates": 0,
|
||||
"existing_session_works": true,
|
||||
"first_write_after_action_seconds": 9.343,
|
||||
"write": {
|
||||
"elapsed_seconds": 24.672,
|
||||
"attempts": 12,
|
||||
"successes": 12,
|
||||
"errors": 0,
|
||||
"error_ratio": 0.0,
|
||||
"successful_rps": 0.486,
|
||||
"successful_latency_ms": {
|
||||
"p50": 2032.0,
|
||||
"p95": 2063.0,
|
||||
"p99": 2063.0
|
||||
}
|
||||
},
|
||||
"first_read_after_action_seconds": 7.25,
|
||||
"read": {
|
||||
"elapsed_seconds": 24.672,
|
||||
"attempts": 12,
|
||||
"successes": 12,
|
||||
"errors": 0,
|
||||
"error_ratio": 0.0,
|
||||
"successful_rps": 0.486,
|
||||
"successful_latency_ms": {
|
||||
"p50": 2032.0,
|
||||
"p95": 2094.0,
|
||||
"p99": 2094.0
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
$ C:\Users\ira\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\python.exe tests/resilience.py --mode failover --action-script scripts/stop-one.sh --base-url http://localhost:8080
|
||||
Executing explicit failover hook; hook output suppressed to avoid leaking secrets.
|
||||
{"observed_instances_before": 2, "successful_writes": 7, "failed_attempts": 0, "first_recovery_seconds": 6.343}
|
||||
PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence.
|
||||
|
||||
exit_code=0; elapsed_seconds=121.531
|
||||
@@ -0,0 +1,114 @@
|
||||
{
|
||||
"mode": "replace",
|
||||
"service": "api-a",
|
||||
"target_instance": "71f43777fc8842999fb52b8b37b54209",
|
||||
"observed_with_one_session": [
|
||||
"71f43777fc8842999fb52b8b37b54209",
|
||||
"7755536d105945f7bc8fb8a5f1afc7db"
|
||||
],
|
||||
"before": [
|
||||
{
|
||||
"service": "api-a",
|
||||
"container_id": "b5f9cdc58cfa1e5ba32d7219909ca0b02f6984e2d69ec6090872f7c1aa3d40b8",
|
||||
"pid": 33274,
|
||||
"state": "running",
|
||||
"started_at": "2026-11-02T14:08:05.363422472Z",
|
||||
"status": 200,
|
||||
"instance_id": "71f43777fc8842999fb52b8b37b54209"
|
||||
},
|
||||
{
|
||||
"service": "api-b",
|
||||
"container_id": "3923c6b10754bdf24cf03312f2cdfc47d0c57b626e413c15c6bf99584795866a",
|
||||
"pid": 40829,
|
||||
"state": "running",
|
||||
"started_at": "2026-11-02T14:11:10.686011176Z",
|
||||
"status": 200,
|
||||
"instance_id": "7755536d105945f7bc8fb8a5f1afc7db"
|
||||
}
|
||||
],
|
||||
"after_action": [
|
||||
{
|
||||
"service": "api-a",
|
||||
"container_id": "23e09768ad0c70567c705b69a9383a1ba2ce89c5c2ccf58117662168c763599c",
|
||||
"pid": 43148,
|
||||
"state": "running",
|
||||
"started_at": "2026-11-02T14:12:02.136399326Z",
|
||||
"status": 200,
|
||||
"instance_id": "1b8747dfda534a5a9ed92482ccb43f43"
|
||||
},
|
||||
{
|
||||
"service": "api-b",
|
||||
"container_id": "3923c6b10754bdf24cf03312f2cdfc47d0c57b626e413c15c6bf99584795866a",
|
||||
"pid": 40829,
|
||||
"state": "running",
|
||||
"started_at": "2026-11-02T14:11:10.686011176Z",
|
||||
"status": 200,
|
||||
"instance_id": "7755536d105945f7bc8fb8a5f1afc7db"
|
||||
}
|
||||
],
|
||||
"action": {
|
||||
"service": "api-a",
|
||||
"before": "71f43777fc8842999fb52b8b37b54209",
|
||||
"after": "1b8747dfda534a5a9ed92482ccb43f43"
|
||||
},
|
||||
"action_seconds": 20.032,
|
||||
"concurrency": 2,
|
||||
"total": {
|
||||
"elapsed_seconds": 28.921,
|
||||
"attempts": 28,
|
||||
"successes": 28,
|
||||
"errors": 0,
|
||||
"error_ratio": 0.0,
|
||||
"successful_rps": 0.968,
|
||||
"successful_latency_ms": {
|
||||
"p50": 2032.0,
|
||||
"p95": 2110.0,
|
||||
"p99": 2141.0
|
||||
}
|
||||
},
|
||||
"during_and_after_action": {
|
||||
"elapsed_seconds": 27.875,
|
||||
"attempts": 26,
|
||||
"successes": 26,
|
||||
"errors": 0,
|
||||
"error_ratio": 0.0,
|
||||
"successful_rps": 0.933,
|
||||
"successful_latency_ms": {
|
||||
"p50": 2032.0,
|
||||
"p95": 2078.0,
|
||||
"p99": 2141.0
|
||||
}
|
||||
},
|
||||
"confirmed_responses": 15,
|
||||
"history_messages": 15,
|
||||
"duplicates": 0,
|
||||
"existing_session_works": true,
|
||||
"first_write_after_action_seconds": 25.75,
|
||||
"write": {
|
||||
"elapsed_seconds": 28.921,
|
||||
"attempts": 14,
|
||||
"successes": 14,
|
||||
"errors": 0,
|
||||
"error_ratio": 0.0,
|
||||
"successful_rps": 0.484,
|
||||
"successful_latency_ms": {
|
||||
"p50": 2062.0,
|
||||
"p95": 2141.0,
|
||||
"p99": 2141.0
|
||||
}
|
||||
},
|
||||
"first_read_after_action_seconds": 23.657,
|
||||
"read": {
|
||||
"elapsed_seconds": 28.921,
|
||||
"attempts": 14,
|
||||
"successes": 14,
|
||||
"errors": 0,
|
||||
"error_ratio": 0.0,
|
||||
"successful_rps": 0.484,
|
||||
"successful_latency_ms": {
|
||||
"p50": 2031.0,
|
||||
"p95": 2047.0,
|
||||
"p99": 2047.0
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
{
|
||||
"mode": "replace",
|
||||
"service": "api-b",
|
||||
"target_instance": "7755536d105945f7bc8fb8a5f1afc7db",
|
||||
"observed_with_one_session": [
|
||||
"1b8747dfda534a5a9ed92482ccb43f43",
|
||||
"7755536d105945f7bc8fb8a5f1afc7db"
|
||||
],
|
||||
"before": [
|
||||
{
|
||||
"service": "api-a",
|
||||
"container_id": "23e09768ad0c70567c705b69a9383a1ba2ce89c5c2ccf58117662168c763599c",
|
||||
"pid": 43148,
|
||||
"state": "running",
|
||||
"started_at": "2026-11-02T14:12:02.136399326Z",
|
||||
"status": 200,
|
||||
"instance_id": "1b8747dfda534a5a9ed92482ccb43f43"
|
||||
},
|
||||
{
|
||||
"service": "api-b",
|
||||
"container_id": "3923c6b10754bdf24cf03312f2cdfc47d0c57b626e413c15c6bf99584795866a",
|
||||
"pid": 40829,
|
||||
"state": "running",
|
||||
"started_at": "2026-11-02T14:11:10.686011176Z",
|
||||
"status": 200,
|
||||
"instance_id": "7755536d105945f7bc8fb8a5f1afc7db"
|
||||
}
|
||||
],
|
||||
"after_action": [
|
||||
{
|
||||
"service": "api-a",
|
||||
"container_id": "23e09768ad0c70567c705b69a9383a1ba2ce89c5c2ccf58117662168c763599c",
|
||||
"pid": 43148,
|
||||
"state": "running",
|
||||
"started_at": "2026-11-02T14:12:02.136399326Z",
|
||||
"status": 200,
|
||||
"instance_id": "1b8747dfda534a5a9ed92482ccb43f43"
|
||||
},
|
||||
{
|
||||
"service": "api-b",
|
||||
"container_id": "51a88aae9d0221d84f2483e06469327ee9fd457c4a95f50557907d930f9e64ed",
|
||||
"pid": 46165,
|
||||
"state": "running",
|
||||
"started_at": "2026-11-02T14:13:06.793037685Z",
|
||||
"status": 200,
|
||||
"instance_id": "acec6d43ecdd4b76a240c85f334d3152"
|
||||
}
|
||||
],
|
||||
"action": {
|
||||
"service": "api-b",
|
||||
"before": "7755536d105945f7bc8fb8a5f1afc7db",
|
||||
"after": "acec6d43ecdd4b76a240c85f334d3152"
|
||||
},
|
||||
"action_seconds": 18.875,
|
||||
"concurrency": 2,
|
||||
"total": {
|
||||
"elapsed_seconds": 28.797,
|
||||
"attempts": 28,
|
||||
"successes": 28,
|
||||
"errors": 0,
|
||||
"error_ratio": 0.0,
|
||||
"successful_rps": 0.972,
|
||||
"successful_latency_ms": {
|
||||
"p50": 2032.0,
|
||||
"p95": 2078.0,
|
||||
"p99": 2079.0
|
||||
}
|
||||
},
|
||||
"during_and_after_action": {
|
||||
"elapsed_seconds": 27.797,
|
||||
"attempts": 26,
|
||||
"successes": 26,
|
||||
"errors": 0,
|
||||
"error_ratio": 0.0,
|
||||
"successful_rps": 0.935,
|
||||
"successful_latency_ms": {
|
||||
"p50": 2032.0,
|
||||
"p95": 2078.0,
|
||||
"p99": 2079.0
|
||||
}
|
||||
},
|
||||
"confirmed_responses": 15,
|
||||
"history_messages": 15,
|
||||
"duplicates": 0,
|
||||
"existing_session_works": true,
|
||||
"first_write_after_action_seconds": 21.578,
|
||||
"write": {
|
||||
"elapsed_seconds": 28.797,
|
||||
"attempts": 14,
|
||||
"successes": 14,
|
||||
"errors": 0,
|
||||
"error_ratio": 0.0,
|
||||
"successful_rps": 0.486,
|
||||
"successful_latency_ms": {
|
||||
"p50": 2047.0,
|
||||
"p95": 2078.0,
|
||||
"p99": 2078.0
|
||||
}
|
||||
},
|
||||
"first_read_after_action_seconds": 23.594,
|
||||
"read": {
|
||||
"elapsed_seconds": 28.797,
|
||||
"attempts": 14,
|
||||
"successes": 14,
|
||||
"errors": 0,
|
||||
"error_ratio": 0.0,
|
||||
"successful_rps": 0.486,
|
||||
"successful_latency_ms": {
|
||||
"p50": 2031.0,
|
||||
"p95": 2079.0,
|
||||
"p99": 2079.0
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
$ C:\Users\ira\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\python.exe tests/resilience.py --mode persistence --action-script scripts/restart.sh --base-url http://localhost: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.
|
||||
|
||||
exit_code=0; elapsed_seconds=43.297
|
||||
@@ -0,0 +1,20 @@
|
||||
[
|
||||
{
|
||||
"service": "api-a",
|
||||
"container_id": "ef1e096f1c30593e0d8e9ebeb467a21c997580d835473c76142127d2d402b235",
|
||||
"pid": 48570,
|
||||
"state": "running",
|
||||
"started_at": "2026-11-02T14:13:58.63201658Z",
|
||||
"status": 200,
|
||||
"instance_id": "1b38655b9a21447494973eb99d5ea039"
|
||||
},
|
||||
{
|
||||
"service": "api-b",
|
||||
"container_id": "4cccbf699f55df2a5e9fd6e19aa897796654a47cdac2ff372565fa6c2a4677e0",
|
||||
"pid": 48587,
|
||||
"state": "running",
|
||||
"started_at": "2026-11-02T14:13:58.715403308Z",
|
||||
"status": 200,
|
||||
"instance_id": "128e6b095f444dffa33214cd3cd33184"
|
||||
}
|
||||
]
|
||||
@@ -0,0 +1,47 @@
|
||||
$ docker compose --env-file .env exec -T nginx nginx -T -c /opt/lb/nginx.conf
|
||||
# configuration file /opt/lb/nginx.conf:
|
||||
worker_processes 1;
|
||||
pid /tmp/nginx.pid;
|
||||
error_log /dev/stderr warn;
|
||||
worker_shutdown_timeout 5s;
|
||||
events { worker_connections 1024; }
|
||||
http {
|
||||
log_format upstream '$request_method $uri status=$status upstream=$upstream_addr upstream_status=$upstream_status time=$request_time';
|
||||
access_log /dev/stdout upstream;
|
||||
resolver 127.0.0.11 valid=1s ipv6=off;
|
||||
resolver_timeout 1s;
|
||||
include /tmp/backends.conf;
|
||||
server {
|
||||
listen 8080;
|
||||
server_tokens off;
|
||||
client_max_body_size 256k;
|
||||
client_header_timeout 5s;
|
||||
client_body_timeout 5s;
|
||||
keepalive_timeout 2s;
|
||||
location / {
|
||||
proxy_pass http://api_backend;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Connection "";
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_connect_timeout 1s;
|
||||
proxy_read_timeout 8s;
|
||||
proxy_send_timeout 8s;
|
||||
proxy_next_upstream error timeout http_502 http_503 http_504;
|
||||
proxy_next_upstream_tries 2;
|
||||
proxy_next_upstream_timeout 9s;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# configuration file /tmp/backends.conf:
|
||||
upstream api_backend {
|
||||
zone api_backend 64k;
|
||||
server api-a:8080 resolve max_fails=1 fail_timeout=2s;
|
||||
server api-b:8080 resolve max_fails=1 fail_timeout=2s;
|
||||
}
|
||||
|
||||
nginx: the configuration file /opt/lb/nginx.conf syntax is ok
|
||||
nginx: configuration file /opt/lb/nginx.conf test is successful
|
||||
|
||||
exit_code=0; elapsed_seconds=0.375
|
||||
@@ -0,0 +1,303 @@
|
||||
$ docker compose --env-file .env --profile test run --rm --build tests
|
||||
#0 building with "desktop-linux" instance using docker driver
|
||||
|
||||
#1 [tests internal] load build definition from Dockerfile
|
||||
#1 transferring dockerfile: 955B 0.0s done
|
||||
#1 DONE 0.0s
|
||||
|
||||
#2 [tests internal] load metadata for docker.io/library/python:3.12.8-slim-bookworm
|
||||
#2 ...
|
||||
|
||||
#3 [tests auth] library/python:pull token for registry-1.docker.io
|
||||
#3 DONE 0.0s
|
||||
|
||||
#2 [tests internal] load metadata for docker.io/library/python:3.12.8-slim-bookworm
|
||||
#2 DONE 1.7s
|
||||
|
||||
#4 [tests internal] load .dockerignore
|
||||
#4 transferring context: 196B done
|
||||
#4 DONE 0.0s
|
||||
|
||||
#5 [tests base 1/6] FROM docker.io/library/python:3.12.8-slim-bookworm@sha256:2199a62885a12290dc9c5be3ca0681d367576ab7bf037da120e564723292a2f0
|
||||
#5 DONE 0.0s
|
||||
|
||||
#6 [tests internal] load build context
|
||||
#6 transferring context: 87.79kB 0.0s done
|
||||
#6 DONE 0.0s
|
||||
|
||||
#7 [tests base 4/6] COPY requirements.txt ./
|
||||
#7 CACHED
|
||||
|
||||
#8 [tests test 2/5] RUN pip install -r requirements-test.txt
|
||||
#8 CACHED
|
||||
|
||||
#9 [tests test 1/5] COPY requirements-test.txt ./
|
||||
#9 CACHED
|
||||
|
||||
#10 [tests test 4/5] COPY student-tests ./student-tests
|
||||
#10 CACHED
|
||||
|
||||
#11 [tests test 3/5] COPY pytest.ini ./
|
||||
#11 CACHED
|
||||
|
||||
#12 [tests base 3/6] WORKDIR /srv
|
||||
#12 CACHED
|
||||
|
||||
#13 [tests base 6/6] COPY app ./app
|
||||
#13 CACHED
|
||||
|
||||
#14 [tests base 2/6] RUN groupadd --system --gid 10001 app && useradd --system --uid 10001 --gid app --no-create-home --shell /usr/sbin/nologin app
|
||||
#14 CACHED
|
||||
|
||||
#15 [tests base 5/6] RUN pip install -r requirements.txt
|
||||
#15 CACHED
|
||||
|
||||
#16 [tests test 5/5] COPY unit-tests ./unit-tests
|
||||
#16 CACHED
|
||||
|
||||
#17 [tests] exporting to image
|
||||
#17 exporting layers done
|
||||
#17 writing image sha256:c4492757aa5070ae7b09009339bfa52d41b2ef1a39beb1d7dde355a0c2fa257e done
|
||||
#17 naming to docker.io/library/mtusi-messenger:lab3-test done
|
||||
#17 DONE 0.0s
|
||||
============================= test session starts ==============================
|
||||
collecting ... collected 226 items
|
||||
|
||||
student-tests/test_auth.py::test_register_returns_user_without_secrets PASSED [ 0%]
|
||||
student-tests/test_auth.py::test_register_password_rules[None] PASSED [ 0%]
|
||||
student-tests/test_auth.py::test_register_password_rules[] PASSED [ 1%]
|
||||
student-tests/test_auth.py::test_register_password_rules[short] PASSED [ 1%]
|
||||
student-tests/test_auth.py::test_register_password_rules[aaaaaaaaaaa] PASSED [ 2%]
|
||||
student-tests/test_auth.py::test_register_password_rules[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa] PASSED [ 2%]
|
||||
student-tests/test_auth.py::test_register_password_rules[123456789012] PASSED [ 3%]
|
||||
student-tests/test_auth.py::test_register_password_rules[\u043f\u0430\u0440\u043e\u043b\u044c] PASSED [ 3%]
|
||||
student-tests/test_auth.py::test_register_password_border_lengths PASSED [ 3%]
|
||||
student-tests/test_auth.py::test_login_success_shape PASSED [ 4%]
|
||||
student-tests/test_auth.py::test_each_login_gives_new_token PASSED [ 4%]
|
||||
student-tests/test_auth.py::test_wrong_password_and_unknown_user_look_the_same PASSED [ 5%]
|
||||
student-tests/test_auth.py::test_malformed_basic_is_401[Basic] PASSED [ 5%]
|
||||
student-tests/test_auth.py::test_malformed_basic_is_401[Basic !!!not-base64!!!] PASSED [ 6%]
|
||||
student-tests/test_auth.py::test_malformed_basic_is_401[Basic bm9jb2xvbg==] PASSED [ 6%]
|
||||
student-tests/test_auth.py::test_malformed_basic_is_401[Basic 0LjQvNGPOnBhc3N3b3JkLTEyMzQ1Ng==] PASSED [ 7%]
|
||||
student-tests/test_auth.py::test_malformed_basic_is_401[Basic dXNlcjr//i1iYWQtdXRmOA==] PASSED [ 7%]
|
||||
student-tests/test_auth.py::test_malformed_basic_is_401[Bearer abc] PASSED [ 7%]
|
||||
student-tests/test_auth.py::test_malformed_basic_is_401[] PASSED [ 8%]
|
||||
student-tests/test_auth.py::test_login_without_header_is_401 PASSED [ 8%]
|
||||
student-tests/test_auth.py::test_password_with_colon_and_unicode_works PASSED [ 9%]
|
||||
student-tests/test_auth.py::test_bearer_token_gives_access PASSED [ 9%]
|
||||
student-tests/test_auth.py::test_business_endpoint_rejects_basic PASSED [ 10%]
|
||||
student-tests/test_auth.py::test_bad_or_unknown_bearer_is_401[] PASSED [ 10%]
|
||||
student-tests/test_auth.py::test_bad_or_unknown_bearer_is_401[Bearer] PASSED [ 11%]
|
||||
student-tests/test_auth.py::test_bad_or_unknown_bearer_is_401[Bearer short] PASSED [ 11%]
|
||||
student-tests/test_auth.py::test_bad_or_unknown_bearer_is_401[Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx] PASSED [ 11%]
|
||||
student-tests/test_auth.py::test_bad_or_unknown_bearer_is_401[Token abc] PASSED [ 12%]
|
||||
student-tests/test_auth.py::test_bad_or_unknown_bearer_is_401[bearer] PASSED [ 12%]
|
||||
student-tests/test_auth.py::test_x_user_id_alone_gives_no_access PASSED [ 13%]
|
||||
student-tests/test_auth.py::test_x_user_id_does_not_change_owner_of_session PASSED [ 13%]
|
||||
student-tests/test_auth.py::test_logout_revokes_token_and_repeat_is_401 PASSED [ 14%]
|
||||
student-tests/test_auth.py::test_logout_without_token_is_401 PASSED [ 14%]
|
||||
student-tests/test_auth.py::test_logout_revokes_only_current_session PASSED [ 15%]
|
||||
student-tests/test_auth.py::test_session_lives_longer_than_300_seconds PASSED [ 15%]
|
||||
student-tests/test_cache.py::test_user_profile_is_served_from_cache_with_bounded_ttl PASSED [ 15%]
|
||||
student-tests/test_cache.py::test_cache_never_stores_raw_tokens_or_passwords PASSED [ 16%]
|
||||
student-tests/test_cache.py::test_logout_revokes_cached_session PASSED [ 16%]
|
||||
student-tests/test_cache.py::test_late_cache_fill_cannot_resurrect_revoked_session PASSED [ 17%]
|
||||
student-tests/test_cache.py::test_flushed_cache_loses_nothing PASSED [ 17%]
|
||||
student-tests/test_cache.py::test_cache_does_not_extend_short_session PASSED [ 18%]
|
||||
student-tests/test_cache.py::test_staleness_after_out_of_band_delete_is_bounded_by_cache_ttl PASSED [ 18%]
|
||||
student-tests/test_cache.py::test_redis_down_requests_bypass_cache_and_stay_fast PASSED [ 19%]
|
||||
student-tests/test_cache.py::test_redis_down_never_trusts_anonymous_or_unknown_tokens PASSED [ 19%]
|
||||
student-tests/test_cache.py::test_revocation_lost_while_redis_down_is_flushed_on_recovery PASSED [ 19%]
|
||||
student-tests/test_cache.py::test_without_cache_url_cache_is_off PASSED [ 20%]
|
||||
student-tests/test_cache.py::test_lost_revocation_survives_api_restart[False] PASSED [ 20%]
|
||||
student-tests/test_cache.py::test_lost_revocation_survives_api_restart[True] PASSED [ 21%]
|
||||
student-tests/test_cache.py::test_cached_session_deleted_in_database_is_rejected_immediately PASSED [ 21%]
|
||||
student-tests/test_concurrency.py::test_100_messages_from_10_clients_in_parallel PASSED [ 22%]
|
||||
student-tests/test_concurrency.py::test_parallel_chat_creation_and_reading PASSED [ 22%]
|
||||
student-tests/test_contract_basics.py::test_health_endpoints PASSED [ 23%]
|
||||
student-tests/test_contract_basics.py::test_every_response_has_unique_request_id PASSED [ 23%]
|
||||
student-tests/test_contract_basics.py::test_unknown_path_is_404_in_common_format PASSED [ 23%]
|
||||
student-tests/test_contract_basics.py::test_trailing_slash_is_not_redirected PASSED [ 24%]
|
||||
student-tests/test_contract_basics.py::test_wrong_method_is_405_in_common_format PASSED [ 24%]
|
||||
student-tests/test_contract_basics.py::test_two_users_one_chat_two_messages PASSED [ 25%]
|
||||
student-tests/test_contract_basics.py::test_chat_response_shape PASSED [ 25%]
|
||||
student-tests/test_contract_basics.py::test_unexpected_exception_gives_500_without_details PASSED [ 26%]
|
||||
student-tests/test_contract_basics.py::test_logs_have_requests_but_no_secrets PASSED [ 26%]
|
||||
student-tests/test_database.py::test_password_is_stored_as_scrypt_hash_not_plaintext PASSED [ 26%]
|
||||
student-tests/test_database.py::test_same_password_gives_different_hashes PASSED [ 27%]
|
||||
student-tests/test_database.py::test_session_table_keeps_token_hash_not_token PASSED [ 27%]
|
||||
student-tests/test_database.py::test_message_is_really_in_the_database PASSED [ 28%]
|
||||
student-tests/test_database.py::test_migrations_can_run_again_without_duplicating_anything PASSED [ 28%]
|
||||
student-tests/test_database.py::test_expected_tables_and_indexes_exist PASSED [ 29%]
|
||||
student-tests/test_database.py::test_primary_and_foreign_keys_are_declared PASSED [ 29%]
|
||||
student-tests/test_database.py::test_database_itself_rejects_bad_rows PASSED [ 30%]
|
||||
student-tests/test_database.py::test_chat_creation_is_atomic PASSED [ 30%]
|
||||
student-tests/test_database.py::test_successful_chat_has_all_members_in_database PASSED [ 30%]
|
||||
student-tests/test_db_failure.py::test_database_down_gives_503_and_never_trusts_anyone PASSED [ 31%]
|
||||
student-tests/test_db_failure.py::test_database_down_with_cache_up_still_needs_database_for_data PASSED [ 31%]
|
||||
student-tests/test_duplicate_username.py::test_second_registration_with_same_username_is_409 PASSED [ 32%]
|
||||
student-tests/test_duplicate_username.py::test_duplicate_does_not_replace_original_user PASSED [ 32%]
|
||||
student-tests/test_duplicate_username.py::test_username_is_case_sensitive_lowercase_only PASSED [ 33%]
|
||||
student-tests/test_duplicate_username.py::test_parallel_registration_of_same_username_creates_exactly_one PASSED [ 33%]
|
||||
student-tests/test_empty_message.py::test_blank_text_is_rejected[] PASSED [ 34%]
|
||||
student-tests/test_empty_message.py::test_blank_text_is_rejected[ ] PASSED [ 34%]
|
||||
student-tests/test_empty_message.py::test_blank_text_is_rejected[ ] PASSED [ 34%]
|
||||
student-tests/test_empty_message.py::test_blank_text_is_rejected[\n] PASSED [ 35%]
|
||||
student-tests/test_empty_message.py::test_blank_text_is_rejected[\t \n] PASSED [ 35%]
|
||||
student-tests/test_empty_message.py::test_blank_text_is_rejected[\xa0] PASSED [ 36%]
|
||||
student-tests/test_empty_message.py::test_blank_text_is_rejected[\u2003] PASSED [ 36%]
|
||||
student-tests/test_empty_message.py::test_missing_or_wrong_type_text_is_rejected[body0] PASSED [ 37%]
|
||||
student-tests/test_empty_message.py::test_missing_or_wrong_type_text_is_rejected[body1] PASSED [ 37%]
|
||||
student-tests/test_empty_message.py::test_missing_or_wrong_type_text_is_rejected[body2] PASSED [ 38%]
|
||||
student-tests/test_empty_message.py::test_missing_or_wrong_type_text_is_rejected[body3] PASSED [ 38%]
|
||||
student-tests/test_empty_message.py::test_missing_or_wrong_type_text_is_rejected[body4] PASSED [ 38%]
|
||||
student-tests/test_empty_message.py::test_text_length_is_counted_in_characters PASSED [ 39%]
|
||||
student-tests/test_empty_message.py::test_nul_character_is_400_not_500 PASSED [ 39%]
|
||||
student-tests/test_empty_message.py::test_unicode_survives_the_database PASSED [ 40%]
|
||||
student-tests/test_empty_message.py::test_message_has_correct_fields PASSED [ 40%]
|
||||
student-tests/test_explain.py::test_history_page_uses_index_without_full_scan[0] PASSED [ 41%]
|
||||
student-tests/test_explain.py::test_history_page_uses_index_without_full_scan[9000] PASSED [ 41%]
|
||||
student-tests/test_foreign_chat.py::test_outsider_cannot_read_chat PASSED [ 42%]
|
||||
student-tests/test_foreign_chat.py::test_outsider_cannot_read_history PASSED [ 42%]
|
||||
student-tests/test_foreign_chat.py::test_outsider_cannot_write PASSED [ 42%]
|
||||
student-tests/test_foreign_chat.py::test_outsider_gets_404_even_with_bad_body PASSED [ 43%]
|
||||
student-tests/test_foreign_chat.py::test_unknown_chat_looks_the_same_as_foreign PASSED [ 43%]
|
||||
student-tests/test_foreign_chat.py::test_chat_list_shows_only_own_chats PASSED [ 44%]
|
||||
student-tests/test_foreign_chat.py::test_bad_chat_id_is_400 PASSED [ 44%]
|
||||
student-tests/test_foreign_chat.py::test_no_token_is_401_everywhere PASSED [ 45%]
|
||||
student-tests/test_foreign_chat.py::test_401_comes_before_404 PASSED [ 45%]
|
||||
student-tests/test_foreign_chat.py::test_member_can_read_and_write PASSED [ 46%]
|
||||
student-tests/test_idempotency.py::test_key_is_required_and_validated[None] PASSED [ 46%]
|
||||
student-tests/test_idempotency.py::test_key_is_required_and_validated[] PASSED [ 46%]
|
||||
student-tests/test_idempotency.py::test_key_is_required_and_validated[a b] PASSED [ 47%]
|
||||
student-tests/test_idempotency.py::test_key_is_required_and_validated[a?b] PASSED [ 47%]
|
||||
student-tests/test_idempotency.py::test_key_is_required_and_validated[xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx] PASSED [ 48%]
|
||||
student-tests/test_idempotency.py::test_key_is_required_and_validated[a,b] PASSED [ 48%]
|
||||
student-tests/test_idempotency.py::test_key_is_required_and_validated[a/b] PASSED [ 49%]
|
||||
student-tests/test_idempotency.py::test_duplicate_headers_are_invalid PASSED [ 49%]
|
||||
student-tests/test_idempotency.py::test_replay_and_conflict_preserve_original_result PASSED [ 50%]
|
||||
student-tests/test_idempotency.py::test_parallel_same_key_across_two_applications PASSED [ 50%]
|
||||
student-tests/test_idempotency.py::test_key_scope_includes_user_and_chat PASSED [ 50%]
|
||||
student-tests/test_idempotency.py::test_expired_result_allows_new_message PASSED [ 51%]
|
||||
student-tests/test_idempotency.py::test_failure_between_message_and_result_rolls_back PASSED [ 51%]
|
||||
student-tests/test_idempotency.py::test_committed_but_lost_response_is_safe_to_retry PASSED [ 52%]
|
||||
student-tests/test_idempotency.py::test_replay_rechecks_session_and_chat_access PASSED [ 52%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_broken_json[{] PASSED [ 53%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_broken_json[not json at all] PASSED [ 53%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_broken_json[] PASSED [ 53%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_broken_json[{"username": "abc",}] PASSED [ 54%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_broken_json[\xff\xfe\x00] PASSED [ 54%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_broken_json[{"username": NaN}] PASSED [ 55%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_broken_json[["username"]] PASSED [ 55%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_broken_json["text"] PASSED [ 56%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_broken_json[null] PASSED [ 56%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes0] PASSED [ 57%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes1] PASSED [ 57%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes2] PASSED [ 57%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes3] PASSED [ 58%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes4] PASSED [ 58%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes5] PASSED [ 59%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes6] PASSED [ 59%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes7] PASSED [ 60%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes8] PASSED [ 60%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes9] PASSED [ 61%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes10] PASSED [ 61%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes11] PASSED [ 61%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes12] PASSED [ 62%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes13] PASSED [ 62%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes14] PASSED [ 63%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes15] PASSED [ 63%]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes16] PASSED [ 64%]
|
||||
student-tests/test_invalid_json.py::test_border_values_are_accepted PASSED [ 64%]
|
||||
student-tests/test_invalid_json.py::test_lone_surrogate_is_400_not_500 PASSED [ 65%]
|
||||
student-tests/test_invalid_json.py::test_wrong_or_missing_content_type_is_415 PASSED [ 65%]
|
||||
student-tests/test_invalid_json.py::test_huge_body_is_413 PASSED [ 65%]
|
||||
student-tests/test_invalid_json.py::test_broken_json_in_message_and_chat PASSED [ 66%]
|
||||
student-tests/test_invalid_json.py::test_sender_id_in_message_is_rejected PASSED [ 66%]
|
||||
student-tests/test_invalid_json.py::test_chat_body_validation PASSED [ 67%]
|
||||
student-tests/test_pagination.py::test_walk_25_messages_by_7 PASSED [ 67%]
|
||||
student-tests/test_pagination.py::test_exact_multiple_has_no_empty_extra_page PASSED [ 68%]
|
||||
student-tests/test_pagination.py::test_limit_one_walks_everything PASSED [ 68%]
|
||||
student-tests/test_pagination.py::test_default_limit_is_50_and_max_is_100 PASSED [ 69%]
|
||||
student-tests/test_pagination.py::test_empty_history PASSED [ 69%]
|
||||
student-tests/test_pagination.py::test_limit_can_change_between_pages PASSED [ 69%]
|
||||
student-tests/test_pagination.py::test_messages_sent_during_walk_appear_at_the_end PASSED [ 70%]
|
||||
student-tests/test_pagination.py::test_created_at_is_increasing_and_utc PASSED [ 70%]
|
||||
student-tests/test_pagination.py::test_bad_limit_values PASSED [ 71%]
|
||||
student-tests/test_pagination.py::test_garbage_cursor_is_400 PASSED [ 71%]
|
||||
student-tests/test_pagination.py::test_cursor_from_another_chat_is_400 PASSED [ 72%]
|
||||
student-tests/test_pagination.py::test_chat_list_pagination PASSED [ 72%]
|
||||
student-tests/test_pagination.py::test_chat_list_cursor_cannot_be_used_for_messages PASSED [ 73%]
|
||||
student-tests/test_pagination.py::test_invalid_cursor_timestamp_is_400[messages--1] PASSED [ 73%]
|
||||
student-tests/test_pagination.py::test_invalid_cursor_timestamp_is_400[messages-1000000000000000000000000000000] PASSED [ 73%]
|
||||
student-tests/test_pagination.py::test_invalid_cursor_timestamp_is_400[messages-253402300800000000] PASSED [ 74%]
|
||||
student-tests/test_pagination.py::test_invalid_cursor_timestamp_is_400[messages-True] PASSED [ 74%]
|
||||
student-tests/test_pagination.py::test_invalid_cursor_timestamp_is_400[messages-1.5] PASSED [ 75%]
|
||||
student-tests/test_pagination.py::test_invalid_cursor_timestamp_is_400[messages-1] PASSED [ 75%]
|
||||
student-tests/test_pagination.py::test_invalid_cursor_timestamp_is_400[messages-None] PASSED [ 76%]
|
||||
student-tests/test_pagination.py::test_invalid_cursor_timestamp_is_400[chats--1] PASSED [ 76%]
|
||||
student-tests/test_pagination.py::test_invalid_cursor_timestamp_is_400[chats-1000000000000000000000000000000] PASSED [ 76%]
|
||||
student-tests/test_pagination.py::test_invalid_cursor_timestamp_is_400[chats-253402300800000000] PASSED [ 77%]
|
||||
student-tests/test_pagination.py::test_invalid_cursor_timestamp_is_400[chats-True] PASSED [ 77%]
|
||||
student-tests/test_pagination.py::test_invalid_cursor_timestamp_is_400[chats-1.5] PASSED [ 78%]
|
||||
student-tests/test_pagination.py::test_invalid_cursor_timestamp_is_400[chats-1] PASSED [ 78%]
|
||||
student-tests/test_pagination.py::test_invalid_cursor_timestamp_is_400[chats-None] PASSED [ 79%]
|
||||
student-tests/test_pagination.py::test_cursor_timestamp_boundaries_are_valid[messages-0] PASSED [ 79%]
|
||||
student-tests/test_pagination.py::test_cursor_timestamp_boundaries_are_valid[messages-253402300799999999] PASSED [ 80%]
|
||||
student-tests/test_pagination.py::test_cursor_timestamp_boundaries_are_valid[chats-0] PASSED [ 80%]
|
||||
student-tests/test_pagination.py::test_cursor_timestamp_boundaries_are_valid[chats-253402300799999999] PASSED [ 80%]
|
||||
student-tests/test_session_expiry.py::test_session_expires_after_ttl PASSED [ 81%]
|
||||
student-tests/test_session_expiry.py::test_expired_sessions_are_cleaned_on_next_login PASSED [ 81%]
|
||||
student-tests/test_session_expiry.py::test_short_ttl_from_environment_variable PASSED [ 82%]
|
||||
student-tests/test_transactions.py::test_failure_after_chat_insert_rolls_back_whole_operation PASSED [ 82%]
|
||||
student-tests/test_transactions.py::test_pool_is_bounded_by_settings PASSED [ 83%]
|
||||
student-tests/test_transactions.py::test_exhausted_pool_gives_fast_503 PASSED [ 83%]
|
||||
unit-tests/test_message_transaction.py::test_conflicting_replay_rolls_back_without_commit PASSED [ 84%]
|
||||
unit-tests/test_message_transaction.py::test_result_insert_failure_never_commits_message PASSED [ 84%]
|
||||
unit-tests/test_message_transaction.py::test_replay_returns_original_message_and_releases_transaction PASSED [ 84%]
|
||||
unit-tests/test_message_transaction.py::test_failed_commit_rolls_back_and_propagates_error PASSED [ 85%]
|
||||
unit-tests/test_mr_regressions.py::test_stale_positive_cache_never_authorizes_revoked_token[/api/v1/users/me-GET] PASSED [ 85%]
|
||||
unit-tests/test_mr_regressions.py::test_stale_positive_cache_never_authorizes_revoked_token[/api/v1/chats/00000000-0000-0000-0000-000000000002/messages-POST] PASSED [ 86%]
|
||||
unit-tests/test_mr_regressions.py::test_stale_positive_cache_never_authorizes_revoked_token[/api/v1/auth/sessions/current-DELETE] PASSED [ 86%]
|
||||
unit-tests/test_mr_regressions.py::test_valid_cache_hit_checks_session_without_loading_user PASSED [ 87%]
|
||||
unit-tests/test_mr_regressions.py::test_database_failure_on_positive_cache_hit_is_503 PASSED [ 87%]
|
||||
unit-tests/test_mr_regressions.py::test_cursor_timestamp_is_rejected_through_http[messages--1] PASSED [ 88%]
|
||||
unit-tests/test_mr_regressions.py::test_cursor_timestamp_is_rejected_through_http[messages-253402300800000000] PASSED [ 88%]
|
||||
unit-tests/test_mr_regressions.py::test_cursor_timestamp_is_rejected_through_http[messages-1000000000000000000000000000000] PASSED [ 88%]
|
||||
unit-tests/test_mr_regressions.py::test_cursor_timestamp_is_rejected_through_http[messages-True] PASSED [ 89%]
|
||||
unit-tests/test_mr_regressions.py::test_cursor_timestamp_is_rejected_through_http[messages-1.5] PASSED [ 89%]
|
||||
unit-tests/test_mr_regressions.py::test_cursor_timestamp_is_rejected_through_http[messages-1] PASSED [ 90%]
|
||||
unit-tests/test_mr_regressions.py::test_cursor_timestamp_is_rejected_through_http[messages-None] PASSED [ 90%]
|
||||
unit-tests/test_mr_regressions.py::test_cursor_timestamp_is_rejected_through_http[chats--1] PASSED [ 91%]
|
||||
unit-tests/test_mr_regressions.py::test_cursor_timestamp_is_rejected_through_http[chats-253402300800000000] PASSED [ 91%]
|
||||
unit-tests/test_mr_regressions.py::test_cursor_timestamp_is_rejected_through_http[chats-1000000000000000000000000000000] PASSED [ 92%]
|
||||
unit-tests/test_mr_regressions.py::test_cursor_timestamp_is_rejected_through_http[chats-True] PASSED [ 92%]
|
||||
unit-tests/test_mr_regressions.py::test_cursor_timestamp_is_rejected_through_http[chats-1.5] PASSED [ 92%]
|
||||
unit-tests/test_mr_regressions.py::test_cursor_timestamp_is_rejected_through_http[chats-1] PASSED [ 93%]
|
||||
unit-tests/test_mr_regressions.py::test_cursor_timestamp_is_rejected_through_http[chats-None] PASSED [ 93%]
|
||||
unit-tests/test_mr_regressions.py::test_timestamp_boundaries_roundtrip[0] PASSED [ 94%]
|
||||
unit-tests/test_mr_regressions.py::test_timestamp_boundaries_roundtrip[253402300799999999] PASSED [ 94%]
|
||||
unit-tests/test_mr_regressions.py::test_message_key_validation[None] PASSED [ 95%]
|
||||
unit-tests/test_mr_regressions.py::test_message_key_validation[] PASSED [ 95%]
|
||||
unit-tests/test_mr_regressions.py::test_message_key_validation[bad key] PASSED [ 96%]
|
||||
unit-tests/test_mr_regressions.py::test_message_key_validation[xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx] PASSED [ 96%]
|
||||
unit-tests/test_mr_regressions.py::test_message_key_validation[a?b] PASSED [ 96%]
|
||||
unit-tests/test_mr_regressions.py::test_message_key_validation[a,b] PASSED [ 97%]
|
||||
unit-tests/test_mr_regressions.py::test_message_normalization_and_conflict PASSED [ 97%]
|
||||
unit-tests/test_mr_regressions.py::test_instance_header_matches_process_on_all_responses[/health/live] PASSED [ 98%]
|
||||
unit-tests/test_mr_regressions.py::test_instance_header_matches_process_on_all_responses[/missing] PASSED [ 98%]
|
||||
unit-tests/test_mr_regressions.py::test_instance_header_matches_process_on_all_responses[/api/v1/users/me] PASSED [ 99%]
|
||||
unit-tests/test_mr_regressions.py::test_instance_alias_changes_between_applications PASSED [ 99%]
|
||||
unit-tests/test_mr_regressions.py::test_sigterm_drops_readiness_and_requests_graceful_exit PASSED [100%]
|
||||
|
||||
=============================== warnings summary ===============================
|
||||
../usr/local/lib/python3.12/site-packages/fastapi/testclient.py:1
|
||||
/usr/local/lib/python3.12/site-packages/fastapi/testclient.py:1: StarletteDeprecationWarning: Using `httpx` with `starlette.testclient` is deprecated; install `httpx2` instead.
|
||||
from starlette.testclient import TestClient as TestClient # noqa
|
||||
|
||||
-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
|
||||
======================= 226 passed, 1 warning in 46.79s ========================
|
||||
Container mtusi-messenger-lab3-db-1 Running
|
||||
Container mtusi-messenger-lab3-redis-1 Running
|
||||
|
||||
exit_code=0; elapsed_seconds=53.031
|
||||
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"status": "PASS",
|
||||
"at_utc": "2026-11-02T14:15:07.979020+00:00"
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
$ /workspace/scratch/03a740016edf/mtusi2026lab3/../.venv/bin/python scripts/check_contract.py
|
||||
PASS: offline skeleton structure, local refs and Python syntax; application NOT tested.
|
||||
|
||||
exit_code=0
|
||||
@@ -0,0 +1,4 @@
|
||||
$ /workspace/scratch/03a740016edf/mtusi2026lab3/../.venv/bin/python scripts/validate_openapi.py
|
||||
PASS: full OpenAPI 3.1 and JSON Schema validation; application NOT tested.
|
||||
|
||||
exit_code=0
|
||||
@@ -0,0 +1,4 @@
|
||||
$ /workspace/scratch/03a740016edf/mtusi2026lab3/../.venv/bin/python scripts/check_lab3.py
|
||||
PASS: lab3 static structure, resource/port isolation, contract, Python/sh syntax; runtime NOT tested.
|
||||
|
||||
exit_code=0
|
||||
@@ -0,0 +1,11 @@
|
||||
$ /workspace/scratch/03a740016edf/mtusi2026lab3/../.venv/bin/python -m pytest -q unit-tests -p no:cacheprovider
|
||||
..................................... [100%]
|
||||
=============================== warnings summary ===============================
|
||||
../.venv/lib/python3.12/site-packages/fastapi/testclient.py:1
|
||||
/workspace/scratch/03a740016edf/.venv/lib/python3.12/site-packages/fastapi/testclient.py:1: StarletteDeprecationWarning: Using `httpx` with `starlette.testclient` is deprecated; install `httpx2` instead.
|
||||
from starlette.testclient import TestClient as TestClient # noqa
|
||||
|
||||
-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
|
||||
37 passed, 1 warning in 0.83s
|
||||
|
||||
exit_code=0
|
||||
@@ -0,0 +1,237 @@
|
||||
$ /workspace/scratch/03a740016edf/mtusi2026lab3/../.venv/bin/python -m pytest --collect-only -q -p no:cacheprovider
|
||||
student-tests/test_auth.py::test_register_returns_user_without_secrets
|
||||
student-tests/test_auth.py::test_register_password_rules[None]
|
||||
student-tests/test_auth.py::test_register_password_rules[]
|
||||
student-tests/test_auth.py::test_register_password_rules[short]
|
||||
student-tests/test_auth.py::test_register_password_rules[aaaaaaaaaaa]
|
||||
student-tests/test_auth.py::test_register_password_rules[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]
|
||||
student-tests/test_auth.py::test_register_password_rules[123456789012]
|
||||
student-tests/test_auth.py::test_register_password_rules[\u043f\u0430\u0440\u043e\u043b\u044c]
|
||||
student-tests/test_auth.py::test_register_password_border_lengths
|
||||
student-tests/test_auth.py::test_login_success_shape
|
||||
student-tests/test_auth.py::test_each_login_gives_new_token
|
||||
student-tests/test_auth.py::test_wrong_password_and_unknown_user_look_the_same
|
||||
student-tests/test_auth.py::test_malformed_basic_is_401[Basic]
|
||||
student-tests/test_auth.py::test_malformed_basic_is_401[Basic !!!not-base64!!!]
|
||||
student-tests/test_auth.py::test_malformed_basic_is_401[Basic bm9jb2xvbg==]
|
||||
student-tests/test_auth.py::test_malformed_basic_is_401[Basic 0LjQvNGPOnBhc3N3b3JkLTEyMzQ1Ng==]
|
||||
student-tests/test_auth.py::test_malformed_basic_is_401[Basic dXNlcjr//i1iYWQtdXRmOA==]
|
||||
student-tests/test_auth.py::test_malformed_basic_is_401[Bearer abc]
|
||||
student-tests/test_auth.py::test_malformed_basic_is_401[]
|
||||
student-tests/test_auth.py::test_login_without_header_is_401
|
||||
student-tests/test_auth.py::test_password_with_colon_and_unicode_works
|
||||
student-tests/test_auth.py::test_bearer_token_gives_access
|
||||
student-tests/test_auth.py::test_business_endpoint_rejects_basic
|
||||
student-tests/test_auth.py::test_bad_or_unknown_bearer_is_401[]
|
||||
student-tests/test_auth.py::test_bad_or_unknown_bearer_is_401[Bearer]
|
||||
student-tests/test_auth.py::test_bad_or_unknown_bearer_is_401[Bearer short]
|
||||
student-tests/test_auth.py::test_bad_or_unknown_bearer_is_401[Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx]
|
||||
student-tests/test_auth.py::test_bad_or_unknown_bearer_is_401[Token abc]
|
||||
student-tests/test_auth.py::test_bad_or_unknown_bearer_is_401[bearer]
|
||||
student-tests/test_auth.py::test_x_user_id_alone_gives_no_access
|
||||
student-tests/test_auth.py::test_x_user_id_does_not_change_owner_of_session
|
||||
student-tests/test_auth.py::test_logout_revokes_token_and_repeat_is_401
|
||||
student-tests/test_auth.py::test_logout_without_token_is_401
|
||||
student-tests/test_auth.py::test_logout_revokes_only_current_session
|
||||
student-tests/test_auth.py::test_session_lives_longer_than_300_seconds
|
||||
student-tests/test_cache.py::test_user_profile_is_served_from_cache_with_bounded_ttl
|
||||
student-tests/test_cache.py::test_cache_never_stores_raw_tokens_or_passwords
|
||||
student-tests/test_cache.py::test_logout_revokes_cached_session
|
||||
student-tests/test_cache.py::test_late_cache_fill_cannot_resurrect_revoked_session
|
||||
student-tests/test_cache.py::test_flushed_cache_loses_nothing
|
||||
student-tests/test_cache.py::test_cache_does_not_extend_short_session
|
||||
student-tests/test_cache.py::test_staleness_after_out_of_band_delete_is_bounded_by_cache_ttl
|
||||
student-tests/test_cache.py::test_redis_down_requests_bypass_cache_and_stay_fast
|
||||
student-tests/test_cache.py::test_redis_down_never_trusts_anonymous_or_unknown_tokens
|
||||
student-tests/test_cache.py::test_revocation_lost_while_redis_down_is_flushed_on_recovery
|
||||
student-tests/test_cache.py::test_without_cache_url_cache_is_off
|
||||
student-tests/test_cache.py::test_lost_revocation_survives_api_restart[False]
|
||||
student-tests/test_cache.py::test_lost_revocation_survives_api_restart[True]
|
||||
student-tests/test_cache.py::test_cached_session_deleted_in_database_is_rejected_immediately
|
||||
student-tests/test_concurrency.py::test_100_messages_from_10_clients_in_parallel
|
||||
student-tests/test_concurrency.py::test_parallel_chat_creation_and_reading
|
||||
student-tests/test_contract_basics.py::test_health_endpoints
|
||||
student-tests/test_contract_basics.py::test_every_response_has_unique_request_id
|
||||
student-tests/test_contract_basics.py::test_unknown_path_is_404_in_common_format
|
||||
student-tests/test_contract_basics.py::test_trailing_slash_is_not_redirected
|
||||
student-tests/test_contract_basics.py::test_wrong_method_is_405_in_common_format
|
||||
student-tests/test_contract_basics.py::test_two_users_one_chat_two_messages
|
||||
student-tests/test_contract_basics.py::test_chat_response_shape
|
||||
student-tests/test_contract_basics.py::test_unexpected_exception_gives_500_without_details
|
||||
student-tests/test_contract_basics.py::test_logs_have_requests_but_no_secrets
|
||||
student-tests/test_database.py::test_password_is_stored_as_scrypt_hash_not_plaintext
|
||||
student-tests/test_database.py::test_same_password_gives_different_hashes
|
||||
student-tests/test_database.py::test_session_table_keeps_token_hash_not_token
|
||||
student-tests/test_database.py::test_message_is_really_in_the_database
|
||||
student-tests/test_database.py::test_migrations_can_run_again_without_duplicating_anything
|
||||
student-tests/test_database.py::test_expected_tables_and_indexes_exist
|
||||
student-tests/test_database.py::test_primary_and_foreign_keys_are_declared
|
||||
student-tests/test_database.py::test_database_itself_rejects_bad_rows
|
||||
student-tests/test_database.py::test_chat_creation_is_atomic
|
||||
student-tests/test_database.py::test_successful_chat_has_all_members_in_database
|
||||
student-tests/test_db_failure.py::test_database_down_gives_503_and_never_trusts_anyone
|
||||
student-tests/test_db_failure.py::test_database_down_with_cache_up_still_needs_database_for_data
|
||||
student-tests/test_duplicate_username.py::test_second_registration_with_same_username_is_409
|
||||
student-tests/test_duplicate_username.py::test_duplicate_does_not_replace_original_user
|
||||
student-tests/test_duplicate_username.py::test_username_is_case_sensitive_lowercase_only
|
||||
student-tests/test_duplicate_username.py::test_parallel_registration_of_same_username_creates_exactly_one
|
||||
student-tests/test_empty_message.py::test_blank_text_is_rejected[]
|
||||
student-tests/test_empty_message.py::test_blank_text_is_rejected[ ]
|
||||
student-tests/test_empty_message.py::test_blank_text_is_rejected[ ]
|
||||
student-tests/test_empty_message.py::test_blank_text_is_rejected[\n]
|
||||
student-tests/test_empty_message.py::test_blank_text_is_rejected[\t \n]
|
||||
student-tests/test_empty_message.py::test_blank_text_is_rejected[\xa0]
|
||||
student-tests/test_empty_message.py::test_blank_text_is_rejected[\u2003]
|
||||
student-tests/test_empty_message.py::test_missing_or_wrong_type_text_is_rejected[body0]
|
||||
student-tests/test_empty_message.py::test_missing_or_wrong_type_text_is_rejected[body1]
|
||||
student-tests/test_empty_message.py::test_missing_or_wrong_type_text_is_rejected[body2]
|
||||
student-tests/test_empty_message.py::test_missing_or_wrong_type_text_is_rejected[body3]
|
||||
student-tests/test_empty_message.py::test_missing_or_wrong_type_text_is_rejected[body4]
|
||||
student-tests/test_empty_message.py::test_text_length_is_counted_in_characters
|
||||
student-tests/test_empty_message.py::test_nul_character_is_400_not_500
|
||||
student-tests/test_empty_message.py::test_unicode_survives_the_database
|
||||
student-tests/test_empty_message.py::test_message_has_correct_fields
|
||||
student-tests/test_explain.py::test_history_page_uses_index_without_full_scan[0]
|
||||
student-tests/test_explain.py::test_history_page_uses_index_without_full_scan[9000]
|
||||
student-tests/test_foreign_chat.py::test_outsider_cannot_read_chat
|
||||
student-tests/test_foreign_chat.py::test_outsider_cannot_read_history
|
||||
student-tests/test_foreign_chat.py::test_outsider_cannot_write
|
||||
student-tests/test_foreign_chat.py::test_outsider_gets_404_even_with_bad_body
|
||||
student-tests/test_foreign_chat.py::test_unknown_chat_looks_the_same_as_foreign
|
||||
student-tests/test_foreign_chat.py::test_chat_list_shows_only_own_chats
|
||||
student-tests/test_foreign_chat.py::test_bad_chat_id_is_400
|
||||
student-tests/test_foreign_chat.py::test_no_token_is_401_everywhere
|
||||
student-tests/test_foreign_chat.py::test_401_comes_before_404
|
||||
student-tests/test_foreign_chat.py::test_member_can_read_and_write
|
||||
student-tests/test_idempotency.py::test_key_is_required_and_validated[None]
|
||||
student-tests/test_idempotency.py::test_key_is_required_and_validated[]
|
||||
student-tests/test_idempotency.py::test_key_is_required_and_validated[a b]
|
||||
student-tests/test_idempotency.py::test_key_is_required_and_validated[a?b]
|
||||
student-tests/test_idempotency.py::test_key_is_required_and_validated[xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx]
|
||||
student-tests/test_idempotency.py::test_key_is_required_and_validated[a,b]
|
||||
student-tests/test_idempotency.py::test_key_is_required_and_validated[a/b]
|
||||
student-tests/test_idempotency.py::test_duplicate_headers_are_invalid
|
||||
student-tests/test_idempotency.py::test_replay_and_conflict_preserve_original_result
|
||||
student-tests/test_idempotency.py::test_parallel_same_key_across_two_applications
|
||||
student-tests/test_idempotency.py::test_key_scope_includes_user_and_chat
|
||||
student-tests/test_idempotency.py::test_expired_result_allows_new_message
|
||||
student-tests/test_idempotency.py::test_failure_between_message_and_result_rolls_back
|
||||
student-tests/test_idempotency.py::test_committed_but_lost_response_is_safe_to_retry
|
||||
student-tests/test_idempotency.py::test_replay_rechecks_session_and_chat_access
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_broken_json[{]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_broken_json[not json at all]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_broken_json[]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_broken_json[{"username": "abc",}]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_broken_json[\xff\xfe\x00]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_broken_json[{"username": NaN}]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_broken_json[["username"]]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_broken_json["text"]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_broken_json[null]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes0]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes1]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes2]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes3]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes4]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes5]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes6]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes7]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes8]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes9]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes10]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes11]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes12]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes13]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes14]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes15]
|
||||
student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes16]
|
||||
student-tests/test_invalid_json.py::test_border_values_are_accepted
|
||||
student-tests/test_invalid_json.py::test_lone_surrogate_is_400_not_500
|
||||
student-tests/test_invalid_json.py::test_wrong_or_missing_content_type_is_415
|
||||
student-tests/test_invalid_json.py::test_huge_body_is_413
|
||||
student-tests/test_invalid_json.py::test_broken_json_in_message_and_chat
|
||||
student-tests/test_invalid_json.py::test_sender_id_in_message_is_rejected
|
||||
student-tests/test_invalid_json.py::test_chat_body_validation
|
||||
student-tests/test_pagination.py::test_walk_25_messages_by_7
|
||||
student-tests/test_pagination.py::test_exact_multiple_has_no_empty_extra_page
|
||||
student-tests/test_pagination.py::test_limit_one_walks_everything
|
||||
student-tests/test_pagination.py::test_default_limit_is_50_and_max_is_100
|
||||
student-tests/test_pagination.py::test_empty_history
|
||||
student-tests/test_pagination.py::test_limit_can_change_between_pages
|
||||
student-tests/test_pagination.py::test_messages_sent_during_walk_appear_at_the_end
|
||||
student-tests/test_pagination.py::test_created_at_is_increasing_and_utc
|
||||
student-tests/test_pagination.py::test_bad_limit_values
|
||||
student-tests/test_pagination.py::test_garbage_cursor_is_400
|
||||
student-tests/test_pagination.py::test_cursor_from_another_chat_is_400
|
||||
student-tests/test_pagination.py::test_chat_list_pagination
|
||||
student-tests/test_pagination.py::test_chat_list_cursor_cannot_be_used_for_messages
|
||||
student-tests/test_pagination.py::test_invalid_cursor_timestamp_is_400[messages--1]
|
||||
student-tests/test_pagination.py::test_invalid_cursor_timestamp_is_400[messages-1000000000000000000000000000000]
|
||||
student-tests/test_pagination.py::test_invalid_cursor_timestamp_is_400[messages-253402300800000000]
|
||||
student-tests/test_pagination.py::test_invalid_cursor_timestamp_is_400[messages-True]
|
||||
student-tests/test_pagination.py::test_invalid_cursor_timestamp_is_400[messages-1.5]
|
||||
student-tests/test_pagination.py::test_invalid_cursor_timestamp_is_400[messages-1]
|
||||
student-tests/test_pagination.py::test_invalid_cursor_timestamp_is_400[messages-None]
|
||||
student-tests/test_pagination.py::test_invalid_cursor_timestamp_is_400[chats--1]
|
||||
student-tests/test_pagination.py::test_invalid_cursor_timestamp_is_400[chats-1000000000000000000000000000000]
|
||||
student-tests/test_pagination.py::test_invalid_cursor_timestamp_is_400[chats-253402300800000000]
|
||||
student-tests/test_pagination.py::test_invalid_cursor_timestamp_is_400[chats-True]
|
||||
student-tests/test_pagination.py::test_invalid_cursor_timestamp_is_400[chats-1.5]
|
||||
student-tests/test_pagination.py::test_invalid_cursor_timestamp_is_400[chats-1]
|
||||
student-tests/test_pagination.py::test_invalid_cursor_timestamp_is_400[chats-None]
|
||||
student-tests/test_pagination.py::test_cursor_timestamp_boundaries_are_valid[messages-0]
|
||||
student-tests/test_pagination.py::test_cursor_timestamp_boundaries_are_valid[messages-253402300799999999]
|
||||
student-tests/test_pagination.py::test_cursor_timestamp_boundaries_are_valid[chats-0]
|
||||
student-tests/test_pagination.py::test_cursor_timestamp_boundaries_are_valid[chats-253402300799999999]
|
||||
student-tests/test_session_expiry.py::test_session_expires_after_ttl
|
||||
student-tests/test_session_expiry.py::test_expired_sessions_are_cleaned_on_next_login
|
||||
student-tests/test_session_expiry.py::test_short_ttl_from_environment_variable
|
||||
student-tests/test_transactions.py::test_failure_after_chat_insert_rolls_back_whole_operation
|
||||
student-tests/test_transactions.py::test_pool_is_bounded_by_settings
|
||||
student-tests/test_transactions.py::test_exhausted_pool_gives_fast_503
|
||||
unit-tests/test_message_transaction.py::test_conflicting_replay_rolls_back_without_commit
|
||||
unit-tests/test_message_transaction.py::test_result_insert_failure_never_commits_message
|
||||
unit-tests/test_message_transaction.py::test_replay_returns_original_message_and_releases_transaction
|
||||
unit-tests/test_message_transaction.py::test_failed_commit_rolls_back_and_propagates_error
|
||||
unit-tests/test_mr_regressions.py::test_stale_positive_cache_never_authorizes_revoked_token[/api/v1/users/me-GET]
|
||||
unit-tests/test_mr_regressions.py::test_stale_positive_cache_never_authorizes_revoked_token[/api/v1/chats/00000000-0000-0000-0000-000000000002/messages-POST]
|
||||
unit-tests/test_mr_regressions.py::test_stale_positive_cache_never_authorizes_revoked_token[/api/v1/auth/sessions/current-DELETE]
|
||||
unit-tests/test_mr_regressions.py::test_valid_cache_hit_checks_session_without_loading_user
|
||||
unit-tests/test_mr_regressions.py::test_database_failure_on_positive_cache_hit_is_503
|
||||
unit-tests/test_mr_regressions.py::test_cursor_timestamp_is_rejected_through_http[messages--1]
|
||||
unit-tests/test_mr_regressions.py::test_cursor_timestamp_is_rejected_through_http[messages-253402300800000000]
|
||||
unit-tests/test_mr_regressions.py::test_cursor_timestamp_is_rejected_through_http[messages-1000000000000000000000000000000]
|
||||
unit-tests/test_mr_regressions.py::test_cursor_timestamp_is_rejected_through_http[messages-True]
|
||||
unit-tests/test_mr_regressions.py::test_cursor_timestamp_is_rejected_through_http[messages-1.5]
|
||||
unit-tests/test_mr_regressions.py::test_cursor_timestamp_is_rejected_through_http[messages-1]
|
||||
unit-tests/test_mr_regressions.py::test_cursor_timestamp_is_rejected_through_http[messages-None]
|
||||
unit-tests/test_mr_regressions.py::test_cursor_timestamp_is_rejected_through_http[chats--1]
|
||||
unit-tests/test_mr_regressions.py::test_cursor_timestamp_is_rejected_through_http[chats-253402300800000000]
|
||||
unit-tests/test_mr_regressions.py::test_cursor_timestamp_is_rejected_through_http[chats-1000000000000000000000000000000]
|
||||
unit-tests/test_mr_regressions.py::test_cursor_timestamp_is_rejected_through_http[chats-True]
|
||||
unit-tests/test_mr_regressions.py::test_cursor_timestamp_is_rejected_through_http[chats-1.5]
|
||||
unit-tests/test_mr_regressions.py::test_cursor_timestamp_is_rejected_through_http[chats-1]
|
||||
unit-tests/test_mr_regressions.py::test_cursor_timestamp_is_rejected_through_http[chats-None]
|
||||
unit-tests/test_mr_regressions.py::test_timestamp_boundaries_roundtrip[0]
|
||||
unit-tests/test_mr_regressions.py::test_timestamp_boundaries_roundtrip[253402300799999999]
|
||||
unit-tests/test_mr_regressions.py::test_message_key_validation[None]
|
||||
unit-tests/test_mr_regressions.py::test_message_key_validation[]
|
||||
unit-tests/test_mr_regressions.py::test_message_key_validation[bad key]
|
||||
unit-tests/test_mr_regressions.py::test_message_key_validation[xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx]
|
||||
unit-tests/test_mr_regressions.py::test_message_key_validation[a?b]
|
||||
unit-tests/test_mr_regressions.py::test_message_key_validation[a,b]
|
||||
unit-tests/test_mr_regressions.py::test_message_normalization_and_conflict
|
||||
unit-tests/test_mr_regressions.py::test_instance_header_matches_process_on_all_responses[/health/live]
|
||||
unit-tests/test_mr_regressions.py::test_instance_header_matches_process_on_all_responses[/missing]
|
||||
unit-tests/test_mr_regressions.py::test_instance_header_matches_process_on_all_responses[/api/v1/users/me]
|
||||
unit-tests/test_mr_regressions.py::test_instance_alias_changes_between_applications
|
||||
unit-tests/test_mr_regressions.py::test_sigterm_drops_readiness_and_requests_graceful_exit
|
||||
|
||||
=============================== warnings summary ===============================
|
||||
../.venv/lib/python3.12/site-packages/fastapi/testclient.py:1
|
||||
/workspace/scratch/03a740016edf/.venv/lib/python3.12/site-packages/fastapi/testclient.py:1: StarletteDeprecationWarning: Using `httpx` with `starlette.testclient` is deprecated; install `httpx2` instead.
|
||||
from starlette.testclient import TestClient as TestClient # noqa
|
||||
|
||||
-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
|
||||
226 tests collected in 0.09s
|
||||
|
||||
exit_code=0
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user