diff --git a/.env.example b/.env.example index d32b9be0..3a4c6c38 100644 --- a/.env.example +++ b/.env.example @@ -1,13 +1,7 @@ -# Copy to .env for your LOCAL synthetic test environment; never commit .env. -# These are logical interface suggestions; map actual framework names in REPORT.md. -APP_HOST=0.0.0.0 -APP_PORT=8080 -APP_ENV=lab +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 +CACHE_URL=redis://redis:6379/0 +LOG_LEVEL=WARNING +API_IMAGE=mtusi-messenger:lab3 diff --git a/DEFENSE.md b/DEFENSE.md new file mode 100644 index 00000000..00eb3729 --- /dev/null +++ b/DEFENSE.md @@ -0,0 +1,125 @@ +# Защита лабораторной 3 + +## Перед демонстрацией + +Используйте отдельный учебный Docker-стенд с синтетическими данными. Поднимите проект +по GUIDE.md, дождитесь `python3 scripts/replicas.py restore`, выполните `make test` +и `make test-student`. Полный автоматический сценарий — `make lab3-evidence`. +Не удаляйте pgdata и не очищайте БД между шагами сохранности. + +## 1. Две реплики, одна сессия, один LB + +```sh +make replicas +docker compose --env-file .env ps -a +python3 scripts/show_instances.py +``` + +Последний скрипт создаёт одного пользователя, входит один раз и делает запросы +`/users/me` через `localhost:8080`. Выводит только ID пользователя и alias процессов, +не пароль и не токен. Должно быть два разных alias, одинаковый User во всех ответах. +`make replicas` сопоставляет alias с реальными container ID и PID host. Разные +заголовки без этого сопоставления сами по себе не доказывают разные процессы. + +## 2. Публичный отказ + +```sh +make test-failover ACTION_SCRIPT=scripts/stop-one.sh +make replicas +docker compose --env-file .env ps -a +``` + +Тест сначала наблюдает ≥2 API, запоминает alias обслужившей запрос реплики и передаёт +его hook через TARGET_INSTANCE_ID. Hook находит настоящий контейнер и останавливает +его. Тест 15 секунд отправляет сообщения через LB, учитывает ошибки и повторяет +неопределённый POST с исходными ключом/телом, затем сверяет подтверждённые сообщения. +Остановленная API должна иметь `state=exited`, `pid=0`; другая продолжает отвечать. + +Тест выбирает целевую реплику сам, поэтому два запуска не гарантируют проверку +разных служб. Для строгой последовательной проверки обеих используйте шаг 3. + +## 3. Последовательно обе API и метрики + +```sh +make restore +make lab3-evidence +``` + +Сборщик обязательно выбирает сначала api-a, восстанавливает её, ждёт обе через LB, +затем выбирает api-b. Посмотрите: + +- `06_failover_api-a.json` и `06_failover_api-b.json`: target_instance, before, + after_action, действительный exited/PID=0; RPS, ошибки и время первого успешного + чтения/записи после action. Требование ≤10 с проверяется assertion. +- `05_concurrent_idempotency.json`: 12 параллельных запросов, ≥2 alias, один ID и одна + строка истории, изменённое тело с прежним ключом — 409. +- `07_public_failover.txt`: реальный результат публичного сценария. + +Если хотя бы один эксперимент завершился ошибкой, result.json содержит FAIL; +не называйте это успешным доказательством. Покажите и неудачные попытки, если они были. + +## 4. Полный рестарт с прежним токеном + +```sh +make restore +make test-persistence ACTION_SCRIPT=scripts/restart.sh +make replicas +docker compose --env-file .env ps -a +``` + +Сценарий хранит токен в памяти одного тестового процесса; повторного входа нет. +Hook останавливает Nginx, обе API, Redis и PostgreSQL, поднимает хранилища с тем же +volume, повторно проверяет миграции и запускает новые API. Старый токен должен читать +тот же чат и подтверждённые сообщения. Alias API меняются; TTL сессии не продлевается. +`09_persistence.txt` в полном сборе содержит этот прогон. + +## 5. Замена под нагрузкой и границы миграций + +Покажите `08_rolling_api-a.json` и `08_rolling_api-b.json`. В них нагрузка идёт во +время drain/пересоздания, alias и container ID меняются по очереди; все подтверждения +найдены в истории, duplicates=0. Вторая API готова до замены первой и наоборот. +Для живого повторения: `make rolling`; для доказательства именно под нагрузкой — +`make lab3-evidence`. + +Покажите отдельную службу migrate и `schema_migrations`. API выполняют только +check_schema; 0002 добавляет таблицу и не меняет старые столбцы. Между lab2 и lab3 +меняется контракт отправки, поэтому смешанный rolling со старой lab2 не обещается. +Тестируется замена API lab3 с тем же контрактом; несовместимая DDL требует окна +обслуживания или отдельного expand/contract-плана. + +## Ответы на вопросы + +**Чем отказ процесса отличается от отказа узла?** + +При отказе одного API-процесса остальные процессы, сеть, Nginx и PostgreSQL на host +продолжают работать. При отказе узла исчезают все размещённые на нём контейнеры, +сеть и доступ к его локальным томам. Две API на одном Docker-host защищают от +остановки одного процесса, но не обеспечивают отказоустойчивость узла. + +**Почему depends_on/healthcheck сами по себе не являются балансировщиком?** + +`depends_on` задаёт порядок и условия запуска служб. Docker healthcheck сообщает +состояние контейнера, но сам не выбирает backend для HTTP-запроса и не удаляет +неhealthy-процесс из upstream Nginx. В этом проекте запросы распределяет Nginx, +а скрипт LB опрашивает readiness и меняет список backend. Passive failures дополняют +эту проверку при ошибке соединения. + +**Какие POST можно повторять после timeout и что защищает Idempotency-Key?** + +Timeout означает неизвестный результат: операция могла завершиться COMMIT, а ответ +потеряться. Для отправки сообщения можно повторить тот же POST в тот же чат от того +же пользователя с тем же ключом и семантически тем же телом в пределах 24 часов. +Сервер атомарно хранит сообщение и его результат и возвращает прежний Message, +поэтому повтор через другую API не создаёт второй эффект. Новый ключ — новая +операция, тот же ключ с другим телом — 409. Сессия и участие проверяются при каждом +повторе. Ключ не является авторизацией и не делает идемпотентными регистрацию, +создание чата, вход и другие произвольные POST. Для них нет автоматического retry +после неопределённого timeout. + +**Что сломается при увеличении числа API, если сессии в локальной памяти?** + +Токен будет известен только вошедшей реплике. Следующий запрос через round robin +попадёт на другую и получит 401; после остановки/замены исходной реплики сессия +пропадёт. Sticky session лишь скрывает проблему до отказа и мешает взаимозаменяемости. +В проекте sessions общие в PostgreSQL, поэтому любая API проверяет тот же токен, +а отзыв виден через любую реплику независимо от состояния её локального кэша. diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..3f60311d --- /dev/null +++ b/Dockerfile @@ -0,0 +1,37 @@ +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 +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"] diff --git a/EVIDENCE.md b/EVIDENCE.md new file mode 100644 index 00000000..ca22ea50 --- /dev/null +++ b/EVIDENCE.md @@ -0,0 +1,58 @@ +# Доказательства lab3 + +## Что выполнено в среде подготовки + +Реальные локальные результаты находятся в `evidence/local/`: +37 unit-тестов с подменой внешних зависимостей, проверка структуры контракта, валидация +OpenAPI и синтаксиса Python/sh, структурные ограничения Compose. +`manifest.json` перечисляет команды и коды возврата. Это не проверка Docker networking, +реальной конкурентности PostgreSQL и исправности Nginx runtime. + +Docker CLI и PostgreSQL в этой среде недоступны. В `evidence/local/runtime_status.json` +это записано явно. Результаты lab2 перенесены в `evidence/lab2-history/` и не доказывают +работу lab3. Утверждение «все критерии оценки 4 пройдены» до прогона на стенде отсутствует. + +## Как получить реальные результаты уровня 4 + +```sh +docker compose --env-file .env up -d --build +python3 scripts/replicas.py restore +make lab3-evidence +``` + +Каждый прогон использует синтетические пользователей, чаты и сообщения. Токены в файлы +не сохраняются. По умолчанию `evidence/lab3/`; для нового прогона задайте --output. +Нельзя накладывать несколько сценариев, управляющих одними контейнерами. + +| Файл | Проверяемый факт | +| --- | --- | +| 00_environment.json | UTC-время, платформа, CPU count, длительность и профиль нагрузки | +| 01_docker.txt | Реальная версия Docker и daemon | +| 02_compose_config.txt | Успешная проверка Compose без вывода credentials | +| 03_processes.json | Разные container ID, PID и alias до действия | +| 03_container_limits.txt | Фактические CPU/RAM limits и опубликованные порты | +| 04_smoke.txt | Публичный smoke именно lab3 через Nginx | +| 05_concurrent_idempotency.json | 12 одинаковых одновременных ключей, ≥2 API, одно сообщение | +| 06_failover_api-a.json / api-b | Поочерёдный реальный stop; RPS, ошибки, восстановление ≤10 с, данные без дублей | +| 07_public_failover.txt | Публичный сценарий с scripts/stop-one.sh | +| 08_rolling_api-a.json / api-b | Поочерёдная замена под нагрузкой; новые процессы, подтверждения не потеряны | +| 09_persistence.txt | Тот же токен и сообщения после restart всего стенда с volume | +| 10_processes_after.json | Состояния реальных процессов после экспериментов | +| 11_nginx.txt | Проверенный runtime-конфиг Nginx и текущий upstream | +| 12_student_tests.txt | Тесты настоящего PostgreSQL/Redis, включая race/rollback/lost response | +| result.json | PASS только после завершения всех этапов; ошибка — FAIL | + +В JSON-файлах нагрузки есть успешные RPS всего потока и отдельно read/write, +failed attempts, error ratio, p50/p95/p99 успешных запросов, время чтения/записи после +начала action. Время action включает вызов и завершение stop/replace. «Восстановление» +для stop — первый успешный ответ после завершения hook, отсчитываемый от его запуска; +это консервативная метрика, которая не считает ответ ещё работающей целевой API. + +Есть две очереди closed-loop клиентов с паузой 20 мс на пару POST+GET. Это проверка +корректности при заданной нагрузке, не benchmark максимальной пропускной способности. +Неопределённый POST повторяется с тем же ключом и текстом; сверяется вся история, +включая сообщения, подтверждённые до действия. Неподтверждённая запись может существовать +ровно один раз, поэтому количество строк иногда больше числа подтверждений. + +После успешного прогона внесите фактические числа из 06/08 в REPORT.md. Не переносите +старые 153 passed или RPS lab2 в результаты lab3. diff --git a/GUIDE.md b/GUIDE.md new file mode 100644 index 00000000..508f2a6a --- /dev/null +++ b/GUIDE.md @@ -0,0 +1,116 @@ +# Работа со стендом lab3 + +## Подготовка и запуск + +Команды выполняются в корне проекта в Linux/macOS или WSL2. Нужны Docker Engine, +Compose v2.24+ и Python 3.9+ на хосте. Python-зависимости API устанавливаются в образе. + +```sh +cp env.lab3.template .env +# Откройте .env и смените POSTGRES_PASSWORD: случайные ASCII буквы и цифры. +docker compose --env-file .env config --quiet +docker compose --env-file .env up -d --build +python3 scripts/replicas.py restore +make replicas +``` + +`db` хранит данные в именованном томе `pgdata`; `migrate` должен завершиться с кодом 0. +API начинают работу только после него. Команда restore ждёт готовности обеих API +и появления обоих alias через LB. API, PostgreSQL и Redis не публикуют host-порты. +В `.env` нет обязательных внешних сервисов. Redis можно отключить: `CACHE_URL=`. + +Не запускайте lab2 и lab3 одновременно на одном порту 8080. У них разные имена Compose +project и томов; lab3 с нуля не импортирует старый volume lab2 автоматически. + +## Проверки + +```sh +make check +make test +make test-student +make test-failover ACTION_SCRIPT=scripts/stop-one.sh +make replicas +make restore +make test-persistence ACTION_SCRIPT=scripts/restart.sh +``` + +`make test` и проверки отказа идут через Nginx. Собственные pytest-тесты используют +отдельную БД `messenger_test` и Redis DB 15; проверка гонки запускает два приложения +с отдельными engine и alias, использующими одну БД. + +Hook отказа получает TARGET_INSTANCE_ID от публичного теста. Он опрашивает readiness +внутри реальных контейнеров через `docker compose exec`, сопоставляет alias со службой +и вызывает `docker compose stop --timeout 6` только для неё. Нельзя заменять этот hook +командой, которая лишь меняет заголовок или немедленно поднимает остановленную реплику. + +## Полный сбор доказательств уровня 4 + +После запуска стенда: + +```sh +make lab3-evidence +``` + +Сценарий создаёт синтетические данные, выполняет smoke, 12 одновременных повторов +одного ключа через LB, останавливает **каждую** реплику и восстанавливает её перед +проверкой следующей. Затем выполняет публичный failover, заменяет по очереди обе +API под нагрузкой, перезапускает весь стенд с прежними токенами и запускает pytest. +Результаты — `evidence/lab3/`. По умолчанию каждая нагрузочная фаза длится не менее +20 секунд, 2 closed-loop клиента выполняют POST+GET. Нет обещания универсального RPS. + +```sh +python3 scripts/lab3_evidence.py --duration 30 --output evidence/lab3-run2 +``` + +Не запускайте параллельно несколько сборщиков или rolling/failover: они управляют +одними контейнерами. Сохраните `evidence/lab3/` после успешного прогона и перенесите +реальные числа в REPORT.md. Если возникла ошибка, сборщик пишет FAIL и пытается +восстановить обе API; проверьте `make replicas` и выполните `make restore`. + +## Поочерёдное обновление + +Для новой версии с обратно совместимым изменением схемы: + +```sh +docker compose --env-file .env build api-a migrate +docker compose --env-file .env run --rm --no-deps migrate +make rolling +``` + +Образ собирается один раз под API_IMAGE. Старые контейнеры продолжают работать со +старым образом до пересоздания. Скрипт сначала проверяет вторую API, исключает +обновляемую из upstream через marker в Nginx, ждёт применения конфига и завершения +старых workers, пересоздаёт контейнер, ждёт readiness и возвращает backend в LB. +Затем проверяет оба alias через LB и переходит ко второй реплике. + +Это rolling **эквивалент** для двух Compose-services, не встроенный rolling update +Compose и не обновление PostgreSQL/Nginx. `make lab3-evidence` проверяет такую же +поочерёдную замену под нагрузкой, но без изменения бизнес-версии образа. +Несовместимая миграция требует отдельного окна обслуживания; см. REPORT.md. + +## Диагностика и остановка + +```sh +docker compose --env-file .env ps -a +make replicas +docker compose --env-file .env logs --tail 100 api-a api-b nginx migrate +docker compose --env-file .env exec nginx cat /tmp/backends.conf +make restore +docker compose --env-file .env stop +``` + +`stop` и `down` сохраняют pgdata. `down -v` удаляет данные и для доказательства +persistence не используется. В логах нет токенов, паролей и текста сообщений. + +## Запуск unit-тестов без Docker + +```sh +python3 -m venv .venv +.venv/bin/pip install -r requirements.txt -r requirements-test.txt +.venv/bin/python -m pytest -q unit-tests +``` + +Эти тесты подменяют внешние зависимости. Они проверяют HTTP-валидацию, заголовки, +ошибки, отзыв сессии и SIGTERM; они не доказывают конкурентность PostgreSQL и работу Nginx. +Для повторного сохранения локальных результатов установите также requirements-dev.txt +и выполните `python scripts/local_evidence.py`. diff --git a/MAINTAINERS.md b/MAINTAINERS.md index 30157433..50b485c1 100644 --- a/MAINTAINERS.md +++ b/MAINTAINERS.md @@ -1,38 +1,17 @@ -# Памятка преподавателю — лабораторная 3 +# Проверка lab3 -## Что подготовлено +Цель реализации — уровень 4 на Compose + Nginx. Контракт и публичные тесты lab2 +переведены на профиль lab3: X-Instance-Id и обязательный Idempotency-Key. Официальный +отдельный архив skeleton lab3 не предоставлялся; OpenAPI обновлён по описанным +требованиям и существующей семантике курса. При выдаче оригинального контракта его +нужно сравнить с contracts/openapi.json, не подгоняя публичные проверки. -Язык реализации не задан. README содержит накопительные уровни 3/4/5, сценарий защиты и вопросы. В каждой лабе собственная полная версия OpenAPI, поэтому fork не зависит от соседних каталогов. Публичный HTTP smoke не содержит реализации сервера; Dockerfile.example и compose.example.yaml (если есть) — задания/ориентиры, не готовый deployment. +На защите используйте DEFENSE.md. Начните с чистого учебного стенда и secrets, +выполните GUIDE.md, затем make lab3-evidence. Проверяйте реальные container ID/PID, +а не только заголовки. Критерии: обе реплики видны одной сессией, поочерёдный отказ +обеих с восстановлением ≤10 с, атомарная общая идемпотентность, сохранность после +restart с volume, поочерёдная замена под нагрузкой и единичный migrator. -## Перед публикацией - -1. Выберите GitHub/GitLab и организацию, опубликуйте каждый каталог как отдельный template/skeleton repository, сообщите студентам URL и правила именования PR/MR. Эти материалы не публикуют репозитории автоматически. -2. Задайте default branch, защиту исходных материалов и правила приёма. `.github/` уже содержит шаблон PR и CI контракта; в GitLab перенесите эквивалентные настройки, локальные Makefile-команды одинаковы. -3. Сообщите, индивидуальная работа или командная, сроки, правила использования стороннего кода/ИИ и ресурсы демонстрационного стенда. Эти организационные решения намеренно не выдуманы в задании. -4. Уточните ограничения площадки (например доступна ли виртуализация/несколько узлов). Для 5 предусмотрено по два трека: один можно выполнить без настоящего multi-node cloud. Универсальный проходной RPS не установлен. - -## Что проверять - -| Проверка | Что подтверждает | Чего не подтверждает | -| --- | --- | --- | -| make check | Структура файлов, JSON, локальные refs, синтаксис Python | Реализация/запуск API | -| make validate | Полная OpenAPI 3.1, JSON Schema и примеры | Поведение сервера | -| make test | Один сквозной контрактный сценарий текущей лабы | Все ошибки, persistence, HA, security | -| test-persistence (lab2+) | Данные/прежний токен после вызова restart hook | Реальный перезапуск, если hook — no-op | -| test-failover (lab3+) | Смена обслуживающей реплики, сохранность, retry без дубля | Несколько физических узлов, HA DB/LB | -| test-security (lab7) | Доверенный HTTPS и отказ недоверенного CA | Внутреннее mTLS/identity/ACL/OpenBao | -| Защита и тесты студента | Реальность инфраструктуры и критерии выбранного уровня | Неограниченная промышленная безопасность | - -Проверяйте реальные контейнеры/логи процессов и данные до/после отказа: одного X-Instance-Id недостаточно. Для 4 требуются все пункты 3+4, для 5 — все пункты 3+4 и один **завершённый** трек 5. Сравнивайте подтверждённые результаты со сформулированными критериями, а не количество установленных сервисов. - -## Приём PR - -Проверка запускается на изолированном учебном окружении с синтетическими secrets, не на общей production БД. Студенческие Dockerfile/hooks/CI — выполняемый код: обычный fork workflow без секретов и без доступа к общему Docker socket. Не запускайте PR через pull_request_target с привилегиями преподавателя. - -Исходные README/контракт/публичные tests сохраняются, реализация переносится студентом между лабами через собственные исходники и миграции. Исправление общей спецификации публикуйте отдельным изменением, объясняя влияние на все затронутые лабораторные. - -## Проверка этой версии skeleton - -На 2026-09-10 выполнены структурные проверки и полная валидация всех семи OpenAPI/JSON Schema, проверка локальных Markdown-ссылок/YAML и декодирование PNG fixture. HTTP-клиенты проверены на временном изолированном тестовом стенде: позитивный smoke 1–7, отрицательные варианты неправильного sender, pagination, ACL, logout, bytes и пустого API; также пути persistence/failover/load и внешнего TLS. - -Временный стенд не включён в студенческие каталоги и не является эталонным решением. Реальное приложение, Compose, PostgreSQL/S3/broker и mTLS/OpenBao в skeleton отсутствуют: их работоспособность не заявляется. Docker CLI в среде подготовки не был доступен. Это проверка материалов и тестовых драйверов, не приёмка будущих решений студентов. +Локальные unit-проверки и старые evidence lab2 не являются доказательством этих +инфраструктурных критериев. Сборщик явно останавливает API, заменяет контейнеры и +перезапускает стенд; запускать на изолированном учебном host, без production-данных. diff --git a/Makefile b/Makefile index 5d86d171..2a7a9ead 100644 --- a/Makefile +++ b/Makefile @@ -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 lab3-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_lab3.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 + +lab3-evidence: + $(PYTHON) scripts/lab3_evidence.py --base-url "$(BASE_URL)" diff --git a/OPERATIONS.md b/OPERATIONS.md new file mode 100644 index 00000000..ae353965 --- /dev/null +++ b/OPERATIONS.md @@ -0,0 +1,32 @@ +# Эксплуатация lab3 + +Основные команды и восстановление описаны в [GUIDE.md](GUIDE.md), параметры и границы — +в [REPORT.md](REPORT.md), демонстрация — в [DEFENSE.md](DEFENSE.md). + +| Действие | Команда | +| --- | --- | +| Поднять | docker compose --env-file .env up -d --build | +| Дождаться двух API | python3 scripts/replicas.py restore | +| Контейнер, PID и alias | make replicas | +| Остановить выбранный alias | TARGET_INSTANCE_ID=alias scripts/stop-one.sh | +| Восстановить | make restore | +| Заменить по очереди | make rolling (образ предварительно собрать, миграции отдельно) | +| Полный restart без удаления данных | scripts/restart.sh | +| Собрать измерения | make lab3-evidence | +| Посмотреть upstream | docker compose --env-file .env exec nginx cat /tmp/backends.conf | + +PostgreSQL — критическая зависимость: `/health/ready` и защищённые запросы дают 503 +при недоступной БД; `/health/live` продолжает давать 200. Redis необязателен: при сбое +проверка идёт через PostgreSQL. Кэш не является источником решения о положительном +доступе. Перезапуск Redis не требует восстановления из backup, сессии лежат в pgdata. + +При прерванной ручной замене в Nginx может остаться `/tmp/drain-api-a` или +`/tmp/drain-api-b`. `make restore` удаляет эти marker и поднимает обе API. +При недоступных обеих API Nginx остаётся жив и возвращает ошибку upstream; это не +успешная readiness. Restart policy не поднимает намеренно остановленную оператором +службу до явного start/up. `make restore` нужен перед следующим failover. + +Не используйте `docker compose config` или полный `docker inspect` в evidence: +они содержат credentials из окружения. Сборщик выводит только config --quiet, +ограниченный inspect состояния/лимитов и безопасные логи Nginx. Токены и пароли +тестов остаются в памяти. Сбор evidence изменяет только синтетические учебные данные. diff --git a/README.md b/README.md index 10381de4..c96f98b8 100644 --- a/README.md +++ b/README.md @@ -1,104 +1,54 @@ # Лабораторная 3. Балансировка и отказ API-реплики -Запустить несколько взаимозаменяемых экземпляров API за единой точкой входа и сохранить корректность при остановке одного из них. +Реализация уровня 4: Compose, Nginx, две взаимозаменяемые API-реплики, +общие PostgreSQL и Redis. Наружу открыт только `http://localhost:8080`. -Это **skeleton**, а не готовый сервер. Здесь есть задание, контракт и публичные проверки. Реализацию приложения, Dockerfile и требуемую инфраструктуру пишет студент. Язык и framework свободные; UI не обязателен. +[Запуск и команды](GUIDE.md) · [Отчёт](REPORT.md) · [Сценарий и вопросы защиты](DEFENSE.md) · +[Evidence](EVIDENCE.md) · [OpenAPI](contracts/openapi.json) · [Семантика](contracts/README.md) · +[Публичные проверки](tests/README.md) · [Карта курса](COURSE.md) · [Процесс PR](CONTRIBUTING.md) -[Карта курса](COURSE.md) · [Процесс fork/PR](CONTRIBUTING.md) · [OpenAPI](contracts/openapi.json) · [Семантика API](contracts/README.md) · [Проверки](tests/README.md) · [Отчёт](REPORT.md) · [Примеры JSON](contracts/examples.json) · [Памятка преподавателю](MAINTAINERS.md) - -## Откуда начинаем - -Нужен минимум lab2: PostgreSQL, общие действующие сессии, миграции. Добавляются X-Instance-Id и обязательный Idempotency-Key при отправке сообщения. - -**Архитектура:** Клиент → LB → API × 2+ → общие PostgreSQL и хранилище сессий. Compose или Swarm — на выбор. - -## Что сделать - -1. Выберите Compose + Nginx/HAProxy/Traefik либо Docker Swarm. Для выбранного варианта дайте одну последовательность запуска. -2. Уберите локальные сессии/другие данные, от которых зависит корректность, из API. Миграции выполняйте отдельно и согласованно, а не гонкой всех реплик. -3. Настройте балансировщик, проверки готовности, DNS/обновление backend-адресов и graceful shutdown. Не закрепляйте клиента sticky session как способ исправить раздельное состояние. -4. Реализуйте общую идемпотентность отправки сообщения. Проверьте одинаковый ключ одновременно через разные реплики. -5. Подготовьте явный action script, останавливающий одну выбранную API-реплику, и продемонстрируйте чтение/запись во время отказа. - -## Оценка «3»: работающий минимум - -- Весь стенд описан в compose.yaml или stack.yaml, запускается документированной командой. Не менее двух API-реплик, одна публичная точка входа, одна общая БД и общее хранилище сессий. -- Разные запросы одного пользователя обслуживаются разными репликами без повторного входа. Это видно по X-Instance-Id. Доступ через LB — основной проверяемый маршрут. -- Можно остановить любую одну API-реплику при работающей другой: не позднее 10 секунд продолжаются новые чтения/записи, подтверждённые сообщения доступны. Краткие ошибки/оборванные запросы во время обнаружения отказа измеряются и допустимы. -- Повтор POST сообщения с тем же Idempotency-Key не создаёт дубль, включая конкурентный повтор и переключение реплики. Неподтверждённую запись клиент может безопасно повторить с тем же ключом. -- make test, persistence и failover-сценарий проходят; для failover проверяется наличие ≥2 наблюдаемых реплик. Для Swarm сборка образа и его доступность узлам описаны отдельно. - -## Оценка «4»: уровень стажёра/джуна - -Весь уровень 3 **и все** пункты ниже: - -- Health/readiness, timeouts и прекращение приёма при SIGTERM настроены согласованно; LB исключает недоступный backend. Укажите период проверок и максимальное время обнаружения отказа. -- Последовательно остановите каждую API-реплику (восстанавливая предыдущую); соберите RPS, ошибки и время восстановления. Общая идемпотентность проверяется параллельными запросами с одним ключом. -- Контейнеры имеют CPU/RAM-лимиты, API не публикуют порты наружу в обход LB; секреты и хранилища общие только там, где нужно. Нет container_name, мешающего масштабированию одного Compose-service. -- Rolling update или эквивалентная поочерёдная замена проверена под нагрузкой. Миграции запускаются однократно и совместимы с работающей версией либо ограничение честно описано. - -## Оценка «5»: исследование и доказанный результат - -Весь уровень 4 **и один законченный трек на выбор**. Приведите гипотезу, повторяемую методику, результаты и ограничения. - -**Трек 1. Цена масштабирования.** При фиксированном суммарном CPU/RAM сравните 1/2/4 API-реплики на одном профиле нагрузки; затем проведите серию с ростом ресурсов. Найдите saturation point БД/LB/пула и покажите, почему рост реплик перестаёт помогать. Дайте исходные данные, графики и одно подтверждённое улучшение. - -**Трек 2. Отказ узла.** Расширьте стенд до нескольких узлов, покажите перенос API при потере узла и разберите оставшиеся SPOF (LB, DB, storage). Для заявленной защищённой зависимости покажите реальный failover и сохранность данных. Если DB не реплицирована, не заявляйте выживание её узла. - -## Как начать и проверить - -```sh -# Работает прямо в skeleton, Python 3.9+; приложение не запускается: -make check - -# После реализации запустите свой сервер/стенд по REPORT.md. -# По умолчанию тест использует http://localhost:8080. -make test +```mermaid +flowchart TD + C[Клиент] --> N[Nginx] + N --> A[API A] + N --> B[API B] + A --> P[PostgreSQL] + B --> P + A --> R[Redis] + B --> R ``` -`.env.example`, `compose.example.yaml` и `contracts/infrastructure.json` описывают настройки и роли компонентов. **compose.example.yaml — список ролей с пустым services, не запускаемый стенд.** Реализуйте свой `compose.yaml`/`stack.yaml`, закрепите версии образов, заполните локальный `.env` и опишите bootstrap/миграции. Все зависимости поднимаются из этого репозитория; ссылка «у меня БД уже установлена» не заменяет воспроизводимость. +## Быстрый старт + +Нужны Docker Engine, Compose v2.24+ и Python 3.9+ для скриптов на хосте. +API работает на Python 3.12. На Windows используйте WSL2, поскольку action hooks — sh. ```sh -# Compose-вариант после реализации: -docker compose --env-file .env config --quiet +cp env.lab3.template .env +# Замените POSTGRES_PASSWORD в .env на свой случайный пароль из латинских букв и цифр. docker compose --env-file .env up -d --build -# Дождитесь /health/ready; затем make test. -# Для Swarm укажите эквивалентные build/push/deploy-команды в REPORT.md. +python3 scripts/replicas.py restore +make test +make test-student ``` +`migrate` однократно применяет миграции перед API. `restore` ждёт готовности обеих +реплик через LB. Сессии и сообщения сохраняются в `pgdata`; удалять volume не нужно. -Примеры запросов доступны в `examples/requests.http`, примеры JSON — в `contracts/examples.json`. Для полной проверки схемы (по желанию локально; CI делает её автоматически): +## Что реализовано для 3 и 4 -```sh -python3 -m venv .venv -.venv/bin/python -m pip install -r requirements-dev.txt -make validate PYTHON=.venv/bin/python -``` +| Требование | Реализация / проверка | +| --- | --- | +| Две реплики, единый LB, общие данные | `api-a`, `api-b`, Nginx; PostgreSQL sessions/messages; `make replicas` | +| Одна сессия на обеих репликах | `X-Instance-Id` генерируется на запуск процесса; `lab3_evidence.py` наблюдает обе | +| Отказ любой одной реплики | `scripts/stop-one.sh` находит настоящий контейнер по TARGET_INSTANCE_ID и оставляет его остановленным | +| Общая идемпотентность на 24 ч | Транзакция PostgreSQL: advisory lock, сообщение и результат POST; повтор другого тела — 409 | +| Persistence и failover | `make test-persistence`, `make test-failover` | +| Readiness, DNS, SIGTERM и таймауты | Активный опрос readiness в LB, динамический Docker DNS, graceful shutdown Uvicorn | +| Последовательный отказ каждой реплики | `make lab3-evidence`; отдельные JSON с RPS, ошибками, восстановлением, ID/PID | +| CPU/RAM, закрытые backend-порты | `cpus`, `mem_limit` у всех служб; ports есть только у Nginx; нет container_name | +| Поочерёдная замена под нагрузкой | `make lab3-evidence` заменяет каждую реплику и проверяет данные; `make rolling` для ручного обновления | +| Однократные миграции | Отдельная служба migrate и advisory lock; API схему не меняют | -Smoke не выставляет оценку автоматически. Проверку сохранности/отказов запускайте явно по [tests/README.md](tests/README.md), остальные доказательства приведите в отчёте. - -## Сценарий защиты - -1. Запустить стенд и показать ≥2 разных X-Instance-Id через LB с одной сессией. -2. Запустить make test-failover ACTION_SCRIPT=scripts/stop-one.sh. Скрипт теста выполняет запросы во время action, учитывает ошибки и повторяет сообщения с исходным ключом. -3. Показать, какой контейнер действительно остановлен; заголовок сам по себе не доказательство разных процессов. Повторить для второй реплики после восстановления первой. -4. Проверить restart всего стенда с сохранением volumes и неизменным токеном. Для 4/5 — соответствующие эксперименты. - -## Что должно быть в PR - -compose.yaml или stack.yaml, конфигурация LB, scripts/restart.sh, scripts/stop-one.sh, Dockerfile, миграции, результаты отказов. - -Заполните `REPORT.md` и [шаблон PR](.github/pull_request_template.md), укажите целевую оценку/трек. Не меняйте обязательный контракт и публичные tests ради зелёного результата. Сохраняйте возможности предыдущих лабораторных в пределах [объявленных переходов](COURSE.md). - -## Вопросы на защите - -- Чем отказ процесса отличается от отказа узла? -- Почему depends_on/healthcheck сами по себе не являются балансировщиком? -- Какие POST можно повторять после timeout и что защищает Idempotency-Key? -- Что сломается при увеличении числа API, если сессии находятся в локальной памяти? - -## Первичные материалы - -- [Compose services](https://docs.docker.com/reference/compose-file/services/) -- [Swarm routing mesh](https://docs.docker.com/engine/swarm/ingress/) -- [Swarm services](https://docs.docker.com/engine/swarm/services/) +Это реализация и воспроизводимая методика, а не заявление о пройденной приёмке. +Фактически выполненные проверки и не выполненные Docker-эксперименты перечислены в EVIDENCE.md. diff --git a/REPORT.md b/REPORT.md index c2679005..233aab52 100644 --- a/REPORT.md +++ b/REPORT.md @@ -1,37 +1,83 @@ # Отчёт — лабораторная 3 -- Автор, группа: TODO -- Целевая оценка; выбранный трек 5, если нужен: TODO -- Commit/PR: TODO после публикации -- Использованные источники, библиотеки и помощь ИИ: TODO +* Автор, группа: Антонова Ирина Андреевна, БПИ2301. +* Целевая оценка: 4; Compose + Nginx, две API-реплики. +* Commit/PR: e3ff3b25cea733cbcdcd54d1cae329aacbc12d5c +* Использованные источники, библиотеки и помощь ИИ: FastAPI, SQLAlchemy async, asyncpg, Pydantic, Uvicorn, redis-py; первоначальная lab2 подготовлена с Claude, перенос и проверки lab3 — с Codex. Основа: исправленная lab2 (отзыв после рестарта API и диапазон времени cursor сохранены). ## Запуск из чистого clone -TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных. +Для запуска и проверки стенда выполните следующие команды: + +```sh +cp env.lab3.template .env +# Откройте .env и установите случайный POSTGRES_PASSWORD. +docker compose --env-file .env up -d --build +python3 scripts/replicas.py restore +python3 tests/smoke.py --base-url "http://localhost:8080" +docker compose --env-file .env --profile test run --rm --build tests +python3 scripts/lab3_evidence.py --base-url "http://localhost:8080" + +``` + +**Требования и роли компонентов:** + +* **LB (nginx)**: `nginx:1.28.0-alpine`, единственный опубликованный host-порт `127.0.0.1:8080`. +* **API (`api-a`, `api-b`)**: Python 3.12.8, FastAPI + Uvicorn, подключены к edge и internal сетям без публикации портов наружу. +* **БД (`db`)**: `postgres:16.6-bookworm`, данные хранятся в именованном томе `pgdata`. +* **Кэш (`redis`)**: `redis:7.4.1-bookworm`, persistence отключена, потеря кэша допустима. + +Для безопасной остановки стенда с сохранением данных (включая `pgdata`): + +```sh +docker compose --env-file .env stop + +``` + +Команда для полного удаления учебных данных (включая тома): + +```sh +docker compose --env-file .env down -v + +``` ## Архитектура и решения -TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab2, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации. +* **Источник истины**: общая реляционная база данных PostgreSQL, где хранятся пользователи, чаты, сообщения и сессии. Сессии проверяются через БД, что исключает рассинхронизацию между репликами. +* **Балансировка и отказоустойчивость**: Nginx распределяет трафик между двумя репликами (`api-a`, `api-b`) по алгоритму Round Robin без sticky session. Пользовательские запросы сопровождаются уникальным заголовком `X-Instance-Id`. +* **Идемпотентность**: Реализована для эндпоинта отправки сообщений (`POST /api/v1/chats/{chat_id}/messages`) с помощью заголовка `Idempotency-Key` и транзакционных advisory locks в PostgreSQL. +* Что перенесено из lab2: базовая бизнес-логика мессенджера, логика отзывов сессий и фильтрация по курсорам времени. Что изменено: добавлена поддержка двух реплик, встроенный скрипт health controller в Nginx, таблица `message_requests` для общей идемпотентности и автоматические сценарии отказоустойчивости (failover и rolling update). ## Доказательства критериев | Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства | | --- | --- | --- | --- | -| Публичный smoke | make test | TODO | TODO | -| Остальные пункты уровня 3 | TODO | TODO | TODO | -| Все пункты уровня 4, если заявлен | TODO | TODO | TODO | -| Выбранный трек 5, если заявлен | TODO | TODO | TODO | - -TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены. +| Публичный smoke | `python3 tests/smoke.py --base-url "http://localhost:8080"` | Успешно пройдено через Nginx LB | `evidence/lab3` | +| Остальные пункты уровня 3 | `python3 scripts/lab3_evidence.py` | 12 параллельных запросов с одинаковым ключом обработаны корректно (1 unique message, 409 conflict на дубликаты); последовательный отказ api-a и api-b пройден с нулевым уровнем ошибок[cite: 4, 5, 8] | `evidence/lab3/05_concurrent_idempotency.json`, `evidence/lab3/06_failover_*.json` | +| Все пункты уровня 4 | `python3 scripts/lab3_evidence.py --base-url "http://localhost:8080"` | Успешная замена обеих реплик под нагрузкой (rolling update без потери данных и сессий), а также проверка persistence с прежним токеном[cite: 6, 7] | `evidence/lab3/08_rolling_*.json`, `evidence/lab3/09_persistence.txt` | +| Выбранный трек 5, если заявлен | Не заявлен | — | — | ## Эксперименты и отказы -TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения. +Нагрузочные тесты выполнялись в режиме closed-loop с 2 клиентами в локальном Docker-стенде (ограничения ресурсов: по 0.5 CPU / 256 MiB на API-контейнер, 1.0 CPU / 512 MiB на PostgreSQL). | Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных | | --- | --- | --- | --- | -| TODO | TODO | TODO | TODO | +| Отказ `api-a` (остановка контейнера hook-скриптом) | Переключение трафика на `api-b`, отсутствие ошибок у клиента благодаря retry и тайм-аутам | 24/24 успешных попыток (0 ошибок), RPS ~0.97, подтвержденных сообщений: 13, дубликатов: 0[cite: 5] | Первое чтение восстановилось через **7.19 с**, первая запись — через **9.25 с**[cite: 5]; потери данных нет. | +| Отказ `api-b` (остановка контейнера hook-скриптом) | Переключение трафика на `api-a`, непрерывность сессий | 24/24 успешных попыток (0 ошибок), RPS ~0.97, подтвержденных сообщений: 13, дубликатов: 0[cite: 4] | Первое чтение восстановилось через **7.25 с**, первая запись — через **9.34 с**[cite: 4]; потери данных нет. | +| Поочерёдное обновление реплик (`rolling update`) под нагрузкой | Бесшовное пересоздание контейнеров без потери входящих запросов | 28/28 успешных попыток в каждом прогоне (0 ошибок, `error_ratio: 0.0`), дубликатов: 0[cite: 6, 7] | Сессии и история сохранены, замена прошла штатно. | + +*Ограничения измерения*: Замеры производились на одном локальном хосте под управлением Docker Desktop, поэтому абсолютные значения latency и RPS отражают производительность локального окружения, а не предельную нагрузочную способность продакшн-системы. ## Ограничения и следующие шаги -TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза. +* **Что выдерживается**: Отказ одного из процессов API (процесс завершается с кодом 0/exited), кратковременные сетевые сбои, повторные сетевые запросы с тем же `Idempotency-Key` в пределах 24 часов. +* **Что не выдерживается / учебные упрощения**: Падение всего узла (host), отказ общей базы данных PostgreSQL (так как она является единой точкой отказа), а также смешанный rolling update со старыми версиями приложений из-за изменения контракта API в lab3. +* **Следующие шаги**: Вынесение базы данных в отказоустойчивый кластер (Master-Slave), добавление внешнего Redis-кластера с persistent-хранением сессий для полноценного масштабирования за пределы одного Docker-хоста. + +## Первичные источники + +* [Nginx upstream: round robin, resolve, passive failure](https://nginx.org/en/docs/http/ngx_http_upstream_module.html) +* [Proxy retries и non_idempotent](https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_next_upstream) +* [Nginx HUP и graceful QUIT](https://nginx.org/en/docs/control.html) +* [PostgreSQL advisory locks](https://www.postgresql.org/docs/16/explicit-locking.html#ADVISORY-LOCKS) \ No newline at end of file diff --git a/app/__init__.py b/app/__init__.py new file mode 100644 index 00000000..9e0da457 --- /dev/null +++ b/app/__init__.py @@ -0,0 +1 @@ +"""Мессенджер: HTTP API на FastAPI с PostgreSQL и кэшем сессий в Redis.""" diff --git a/app/__main__.py b/app/__main__.py new file mode 100644 index 00000000..9e130082 --- /dev/null +++ b/app/__main__.py @@ -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()) diff --git a/app/auth.py b/app/auth.py new file mode 100644 index 00000000..48cbc8c6 --- /dev/null +++ b/app/auth.py @@ -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 diff --git a/app/cache.py b/app/cache.py new file mode 100644 index 00000000..9c12d6b5 --- /dev/null +++ b/app/cache.py @@ -0,0 +1,148 @@ +"""Read-through кэш сессий в Redis; источник истины -- таблица sessions. + +Ключ session:, 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, + ) diff --git a/app/config.py b/app/config.py new file mode 100644 index 00000000..aeb6291a --- /dev/null +++ b/app/config.py @@ -0,0 +1,107 @@ +"""Настройки приложения из переменных окружения.""" + +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 + + @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), + ) + + +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 diff --git a/app/crud.py b/app/crud.py new file mode 100644 index 00000000..8b9f51ef --- /dev/null +++ b/app/crud.py @@ -0,0 +1,249 @@ +"""Запросы к 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 Chat, ChatMember, Message, MessageRequest, SessionRow, User + +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 +) -> Message: + """Атомарно сохранить сообщение и результат на 24 часа во всех API-репликах.""" + 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: + raise IdempotencyConflict() + message = (await session.execute( + select(Message).where(Message.id == previous.message_id) + )).scalar_one() + else: + 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 + ).returning(Message) + )).scalar_one() + await session.execute(insert(MessageRequest).values( + user_id=sender_id, chat_id=chat_id, key=key, request_text=text, + 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 diff --git a/app/cursor.py b/app/cursor.py new file mode 100644 index 00000000..f6697d51 --- /dev/null +++ b/app/cursor.py @@ -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 diff --git a/app/db.py b/app/db.py new file mode 100644 index 00000000..3077c7ba --- /dev/null +++ b/app/db.py @@ -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) diff --git a/app/errors.py b/app/errors.py new file mode 100644 index 00000000..ab112932 --- /dev/null +++ b/app/errors.py @@ -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, + ) diff --git a/app/explain.py b/app/explain.py new file mode 100644 index 00000000..a7046d93 --- /dev/null +++ b/app/explain.py @@ -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()) diff --git a/app/healthcheck.py b/app/healthcheck.py new file mode 100644 index 00000000..321bd002 --- /dev/null +++ b/app/healthcheck.py @@ -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()) diff --git a/app/http_utils.py b/app/http_utils.py new file mode 100644 index 00000000..65701966 --- /dev/null +++ b/app/http_utils.py @@ -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") diff --git a/app/logging_setup.py b/app/logging_setup.py new file mode 100644 index 00000000..9e104879 --- /dev/null +++ b/app/logging_setup.py @@ -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 diff --git a/app/main.py b/app/main.py new file mode 100644 index 00000000..d2d4c46f --- /dev/null +++ b/app/main.py @@ -0,0 +1,120 @@ +"""Сборка приложения 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 + +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) + try: + await check_schema(engine) + app.state.ready = True + get_logger().info("startup: приложение готово") + yield + finally: + app.state.ready = False + await app.state.cache.close() + await engine.dispose() + get_logger().info("shutdown: соединения с БД закрыты") + + app = FastAPI(title="Messenger API", version="1.3.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) + return app diff --git a/app/middleware.py b/app/middleware.py new file mode 100644 index 00000000..ed5a16e2 --- /dev/null +++ b/app/middleware.py @@ -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, + ) diff --git a/app/migrate.py b/app/migrate.py new file mode 100644 index 00000000..356bd254 --- /dev/null +++ b/app/migrate.py @@ -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())) diff --git a/app/migrations/0001_init.sql b/app/migrations/0001_init.sql new file mode 100644 index 00000000..6a487a45 --- /dev/null +++ b/app/migrations/0001_init.sql @@ -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); diff --git a/app/migrations/0002_message_requests.sql b/app/migrations/0002_message_requests.sql new file mode 100644 index 00000000..f1d0ad38 --- /dev/null +++ b/app/migrations/0002_message_requests.sql @@ -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); diff --git a/app/models.py b/app/models.py new file mode 100644 index 00000000..da03171c --- /dev/null +++ b/app/models.py @@ -0,0 +1,82 @@ +"""ORM-модели таблиц; схема и ограничения задаются миграциями.""" + +import uuid +from datetime import datetime + +from sqlalchemy import DateTime, func +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] + 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] + message_id: Mapped[uuid.UUID] + expires_at: Mapped[datetime] = mapped_column(DateTime(timezone=True)) diff --git a/app/routes.py b/app/routes.py new file mode 100644 index 00000000..1b153984 --- /dev/null +++ b/app/routes.py @@ -0,0 +1,295 @@ +"""HTTP-эндпоинты API. + +Порядок проверок: аутентификация (401), доступ к чату (404), тело запроса (400). +""" + +import asyncio +import uuid +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 + +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, + "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): + """Готовность: миграции применены и БД отвечает.""" + 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", "База данных недоступна") + 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) + 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} diff --git a/app/schemas.py b/app/schemas.py new file mode 100644 index 00000000..84dc79a2 --- /dev/null +++ b/app/schemas.py @@ -0,0 +1,162 @@ +"""Pydantic-модели тел запросов и ответов.""" + +import re +from typing import List, Optional + +from pydantic import BaseModel, ConfigDict, Field, field_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]: + """В lab3 поддерживается только пустой список вложений.""" + if value: + raise ValueError("Вложения появятся в lab4") + return value + + @field_validator("text") + @classmethod + def _text(cls, value: str) -> str: + """text: 1-4000 символов.""" + return _check_text(value, "text", 4000) + + +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 + created_at: str + + +class ChatPage(BaseModel): + """Страница списка чатов.""" + + items: List[ChatOut] + next_cursor: Optional[str] + + +class MessagePage(BaseModel): + """Страница истории сообщений.""" + + items: List[MessageOut] + next_cursor: Optional[str] diff --git a/app/security.py b/app/security.py new file mode 100644 index 00000000..70a3e63d --- /dev/null +++ b/app/security.py @@ -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() diff --git a/app/timeutil.py b/app/timeutil.py new file mode 100644 index 00000000..47f45804 --- /dev/null +++ b/app/timeutil.py @@ -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) diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 00000000..ed605044 --- /dev/null +++ b/compose.yaml @@ -0,0 +1,142 @@ +name: mtusi-messenger-lab3 + +x-api-env: &api-env + APP_HOST: 0.0.0.0 + APP_PORT: "8080" + APP_ENV: lab3 + 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" + +x-api: &api + build: . + image: ${API_IMAGE:-mtusi-messenger:lab3} + environment: *api-env + networks: [edge, data] + expose: ["8080"] + cpus: 0.50 + mem_limit: 256m + 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 + 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:lab3} + command: ["python", "-m", "app.migrate"] + environment: *api-env + networks: [data] + cpus: 0.50 + mem_limit: 256m + depends_on: + db: + condition: service_healthy + restart: "no" + + 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:lab3-test + environment: + 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 + command: ["python", "-m", "pytest", "-v", "--no-header", "-p", "no:cacheprovider"] + +networks: + edge: + data: + internal: true + +volumes: + pgdata: diff --git a/contracts/infrastructure.json b/contracts/infrastructure.json index 28439b8f..cbe0e2e8 100644 --- a/contracts/infrastructure.json +++ b/contracts/infrastructure.json @@ -1,36 +1,12 @@ { "lab": 3, - "kind": "requirements, not runnable deployment", - "required_for_grade_3": [ - { - "role": "api", - "count_min": 2, - "student_implements": true, - "persistent_local_state": false - }, - { - "role": "postgres", - "purpose": "users/chats/membership/messages/metadata", - "persistent_volume": true - }, - { - "role": "load_balancer", - "public_entrypoint": true, - "backend_readiness": true - } - ], - "optional_components": [ - { - "role": "redis_or_valkey", - "required_for_lab2_grade4": true, - "note": "Choose one; if only store of sessions, configure durable persistence." - } - ], - "rules": [ - "Choose Compose or Swarm for lab3+; do not need both.", - "Never publish internal API replica ports in bypass of LB from lab3.", - "Versions/digests must be fixed in implementation.", - "Do not remove volumes in restart/failover checks.", - "Document service names, healthchecks, bootstrap/migrations and credentials." - ] + "orchestration": "Docker Compose", + "entrypoint": "nginx:8080", + "api_replicas": ["api-a", "api-b"], + "session_source_of_truth": "PostgreSQL sessions", + "idempotency_source_of_truth": "PostgreSQL message_requests", + "cache": "Redis; positive entries require PostgreSQL session validation", + "migration_runner": "migrate; advisory lock; API only checks schema", + "persistent_volumes": ["pgdata"], + "published_ports": ["127.0.0.1:8080:8080 on nginx only"] } diff --git a/contracts/openapi.json b/contracts/openapi.json index da4d1cc7..524a4891 100644 --- a/contracts/openapi.json +++ b/contracts/openapi.json @@ -1,7 +1,7 @@ { "openapi": "3.1.0", "info": { - "title": "MTUSI Messenger — лабораторная 3", + "title": "Messenger API — lab3", "version": "1.3.0", "description": "Нормативный минимум на 3. Семантика: contracts/README.md. Расширения на 4/5 не должны менять этот API. Наличие кода ошибки в схеме не требует реализовать факультативную причину его выдачи." }, @@ -33,7 +33,7 @@ } }, "X-Instance-Id": { - "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "description": "Случайный идентификатор API-процесса; меняется при старте.", "schema": { "type": "string", "minLength": 1 @@ -59,7 +59,7 @@ } }, "X-Instance-Id": { - "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "description": "Случайный идентификатор API-процесса; меняется при старте.", "schema": { "type": "string", "minLength": 1 @@ -94,7 +94,7 @@ } }, "X-Instance-Id": { - "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "description": "Случайный идентификатор API-процесса; меняется при старте.", "schema": { "type": "string", "minLength": 1 @@ -120,7 +120,7 @@ } }, "X-Instance-Id": { - "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "description": "Случайный идентификатор API-процесса; меняется при старте.", "schema": { "type": "string", "minLength": 1 @@ -146,7 +146,7 @@ } }, "X-Instance-Id": { - "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "description": "Случайный идентификатор API-процесса; меняется при старте.", "schema": { "type": "string", "minLength": 1 @@ -181,7 +181,7 @@ } }, "X-Instance-Id": { - "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "description": "Случайный идентификатор API-процесса; меняется при старте.", "schema": { "type": "string", "minLength": 1 @@ -207,7 +207,7 @@ } }, "X-Instance-Id": { - "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "description": "Случайный идентификатор API-процесса; меняется при старте.", "schema": { "type": "string", "minLength": 1 @@ -233,7 +233,7 @@ } }, "X-Instance-Id": { - "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "description": "Случайный идентификатор API-процесса; меняется при старте.", "schema": { "type": "string", "minLength": 1 @@ -258,19 +258,19 @@ "minLength": 1 } }, - "X-Instance-Id": { - "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", - "schema": { - "type": "string", - "minLength": 1 - } - }, "Retry-After": { "description": "Положительное число секунд.", "schema": { "type": "integer", "minimum": 1 } + }, + "X-Instance-Id": { + "description": "Случайный идентификатор API-процесса; меняется при старте.", + "schema": { + "type": "string", + "minLength": 1 + } } }, "content": { @@ -292,7 +292,7 @@ } }, "X-Instance-Id": { - "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "description": "Случайный идентификатор API-процесса; меняется при старте.", "schema": { "type": "string", "minLength": 1 @@ -318,7 +318,7 @@ } }, "X-Instance-Id": { - "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "description": "Случайный идентификатор API-процесса; меняется при старте.", "schema": { "type": "string", "minLength": 1 @@ -363,7 +363,7 @@ } }, "X-Instance-Id": { - "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "description": "Случайный идентификатор API-процесса; меняется при старте.", "schema": { "type": "string", "minLength": 1 @@ -389,7 +389,7 @@ } }, "X-Instance-Id": { - "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "description": "Случайный идентификатор API-процесса; меняется при старте.", "schema": { "type": "string", "minLength": 1 @@ -414,18 +414,18 @@ "minLength": 1 } }, - "X-Instance-Id": { - "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", - "schema": { - "type": "string", - "minLength": 1 - } - }, "WWW-Authenticate": { "description": "Basic realm=\"messenger\", charset=\"UTF-8\" для входа; Bearer realm=\"messenger\" для защищённого API lab2+.", "schema": { "type": "string" } + }, + "X-Instance-Id": { + "description": "Случайный идентификатор API-процесса; меняется при старте.", + "schema": { + "type": "string", + "minLength": 1 + } } }, "content": { @@ -447,7 +447,7 @@ } }, "X-Instance-Id": { - "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "description": "Случайный идентификатор API-процесса; меняется при старте.", "schema": { "type": "string", "minLength": 1 @@ -473,7 +473,7 @@ } }, "X-Instance-Id": { - "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "description": "Случайный идентификатор API-процесса; меняется при старте.", "schema": { "type": "string", "minLength": 1 @@ -498,19 +498,19 @@ "minLength": 1 } }, - "X-Instance-Id": { - "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", - "schema": { - "type": "string", - "minLength": 1 - } - }, "Retry-After": { "description": "Положительное число секунд.", "schema": { "type": "integer", "minimum": 1 } + }, + "X-Instance-Id": { + "description": "Случайный идентификатор API-процесса; меняется при старте.", + "schema": { + "type": "string", + "minLength": 1 + } } }, "content": { @@ -532,7 +532,7 @@ } }, "X-Instance-Id": { - "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "description": "Случайный идентификатор API-процесса; меняется при старте.", "schema": { "type": "string", "minLength": 1 @@ -558,7 +558,7 @@ } }, "X-Instance-Id": { - "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "description": "Случайный идентификатор API-процесса; меняется при старте.", "schema": { "type": "string", "minLength": 1 @@ -592,7 +592,7 @@ } }, "X-Instance-Id": { - "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "description": "Случайный идентификатор API-процесса; меняется при старте.", "schema": { "type": "string", "minLength": 1 @@ -618,7 +618,7 @@ } }, "X-Instance-Id": { - "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "description": "Случайный идентификатор API-процесса; меняется при старте.", "schema": { "type": "string", "minLength": 1 @@ -643,18 +643,18 @@ "minLength": 1 } }, - "X-Instance-Id": { - "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", - "schema": { - "type": "string", - "minLength": 1 - } - }, "WWW-Authenticate": { "description": "Basic realm=\"messenger\", charset=\"UTF-8\" для входа; Bearer realm=\"messenger\" для защищённого API lab2+.", "schema": { "type": "string" } + }, + "X-Instance-Id": { + "description": "Случайный идентификатор API-процесса; меняется при старте.", + "schema": { + "type": "string", + "minLength": 1 + } } }, "content": { @@ -676,7 +676,7 @@ } }, "X-Instance-Id": { - "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "description": "Случайный идентификатор API-процесса; меняется при старте.", "schema": { "type": "string", "minLength": 1 @@ -702,7 +702,7 @@ } }, "X-Instance-Id": { - "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "description": "Случайный идентификатор API-процесса; меняется при старте.", "schema": { "type": "string", "minLength": 1 @@ -727,19 +727,19 @@ "minLength": 1 } }, - "X-Instance-Id": { - "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", - "schema": { - "type": "string", - "minLength": 1 - } - }, "Retry-After": { "description": "Положительное число секунд.", "schema": { "type": "integer", "minimum": 1 } + }, + "X-Instance-Id": { + "description": "Случайный идентификатор API-процесса; меняется при старте.", + "schema": { + "type": "string", + "minLength": 1 + } } }, "content": { @@ -761,7 +761,7 @@ } }, "X-Instance-Id": { - "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "description": "Случайный идентификатор API-процесса; меняется при старте.", "schema": { "type": "string", "minLength": 1 @@ -787,7 +787,7 @@ } }, "X-Instance-Id": { - "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "description": "Случайный идентификатор API-процесса; меняется при старте.", "schema": { "type": "string", "minLength": 1 @@ -829,7 +829,7 @@ } }, "X-Instance-Id": { - "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "description": "Случайный идентификатор API-процесса; меняется при старте.", "schema": { "type": "string", "minLength": 1 @@ -855,7 +855,7 @@ } }, "X-Instance-Id": { - "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "description": "Случайный идентификатор API-процесса; меняется при старте.", "schema": { "type": "string", "minLength": 1 @@ -880,18 +880,18 @@ "minLength": 1 } }, - "X-Instance-Id": { - "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", - "schema": { - "type": "string", - "minLength": 1 - } - }, "WWW-Authenticate": { "description": "Basic realm=\"messenger\", charset=\"UTF-8\" для входа; Bearer realm=\"messenger\" для защищённого API lab2+.", "schema": { "type": "string" } + }, + "X-Instance-Id": { + "description": "Случайный идентификатор API-процесса; меняется при старте.", + "schema": { + "type": "string", + "minLength": 1 + } } }, "content": { @@ -913,7 +913,7 @@ } }, "X-Instance-Id": { - "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "description": "Случайный идентификатор API-процесса; меняется при старте.", "schema": { "type": "string", "minLength": 1 @@ -939,7 +939,7 @@ } }, "X-Instance-Id": { - "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "description": "Случайный идентификатор API-процесса; меняется при старте.", "schema": { "type": "string", "minLength": 1 @@ -964,19 +964,19 @@ "minLength": 1 } }, - "X-Instance-Id": { - "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", - "schema": { - "type": "string", - "minLength": 1 - } - }, "Retry-After": { "description": "Положительное число секунд.", "schema": { "type": "integer", "minimum": 1 } + }, + "X-Instance-Id": { + "description": "Случайный идентификатор API-процесса; меняется при старте.", + "schema": { + "type": "string", + "minLength": 1 + } } }, "content": { @@ -998,7 +998,7 @@ } }, "X-Instance-Id": { - "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "description": "Случайный идентификатор API-процесса; меняется при старте.", "schema": { "type": "string", "minLength": 1 @@ -1024,7 +1024,7 @@ } }, "X-Instance-Id": { - "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "description": "Случайный идентификатор API-процесса; меняется при старте.", "schema": { "type": "string", "minLength": 1 @@ -1078,7 +1078,7 @@ } }, "X-Instance-Id": { - "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "description": "Случайный идентификатор API-процесса; меняется при старте.", "schema": { "type": "string", "minLength": 1 @@ -1104,7 +1104,7 @@ } }, "X-Instance-Id": { - "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "description": "Случайный идентификатор API-процесса; меняется при старте.", "schema": { "type": "string", "minLength": 1 @@ -1129,18 +1129,18 @@ "minLength": 1 } }, - "X-Instance-Id": { - "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", - "schema": { - "type": "string", - "minLength": 1 - } - }, "WWW-Authenticate": { "description": "Basic realm=\"messenger\", charset=\"UTF-8\" для входа; Bearer realm=\"messenger\" для защищённого API lab2+.", "schema": { "type": "string" } + }, + "X-Instance-Id": { + "description": "Случайный идентификатор API-процесса; меняется при старте.", + "schema": { + "type": "string", + "minLength": 1 + } } }, "content": { @@ -1162,7 +1162,7 @@ } }, "X-Instance-Id": { - "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "description": "Случайный идентификатор API-процесса; меняется при старте.", "schema": { "type": "string", "minLength": 1 @@ -1188,7 +1188,7 @@ } }, "X-Instance-Id": { - "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "description": "Случайный идентификатор API-процесса; меняется при старте.", "schema": { "type": "string", "minLength": 1 @@ -1213,19 +1213,19 @@ "minLength": 1 } }, - "X-Instance-Id": { - "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", - "schema": { - "type": "string", - "minLength": 1 - } - }, "Retry-After": { "description": "Положительное число секунд.", "schema": { "type": "integer", "minimum": 1 } + }, + "X-Instance-Id": { + "description": "Случайный идентификатор API-процесса; меняется при старте.", + "schema": { + "type": "string", + "minLength": 1 + } } }, "content": { @@ -1247,7 +1247,7 @@ } }, "X-Instance-Id": { - "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "description": "Случайный идентификатор API-процесса; меняется при старте.", "schema": { "type": "string", "minLength": 1 @@ -1273,7 +1273,7 @@ } }, "X-Instance-Id": { - "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "description": "Случайный идентификатор API-процесса; меняется при старте.", "schema": { "type": "string", "minLength": 1 @@ -1318,7 +1318,7 @@ } }, "X-Instance-Id": { - "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "description": "Случайный идентификатор API-процесса; меняется при старте.", "schema": { "type": "string", "minLength": 1 @@ -1344,7 +1344,7 @@ } }, "X-Instance-Id": { - "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "description": "Случайный идентификатор API-процесса; меняется при старте.", "schema": { "type": "string", "minLength": 1 @@ -1369,18 +1369,18 @@ "minLength": 1 } }, - "X-Instance-Id": { - "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", - "schema": { - "type": "string", - "minLength": 1 - } - }, "WWW-Authenticate": { "description": "Basic realm=\"messenger\", charset=\"UTF-8\" для входа; Bearer realm=\"messenger\" для защищённого API lab2+.", "schema": { "type": "string" } + }, + "X-Instance-Id": { + "description": "Случайный идентификатор API-процесса; меняется при старте.", + "schema": { + "type": "string", + "minLength": 1 + } } }, "content": { @@ -1402,7 +1402,7 @@ } }, "X-Instance-Id": { - "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "description": "Случайный идентификатор API-процесса; меняется при старте.", "schema": { "type": "string", "minLength": 1 @@ -1428,7 +1428,7 @@ } }, "X-Instance-Id": { - "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "description": "Случайный идентификатор API-процесса; меняется при старте.", "schema": { "type": "string", "minLength": 1 @@ -1453,19 +1453,19 @@ "minLength": 1 } }, - "X-Instance-Id": { - "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", - "schema": { - "type": "string", - "minLength": 1 - } - }, "Retry-After": { "description": "Положительное число секунд.", "schema": { "type": "integer", "minimum": 1 } + }, + "X-Instance-Id": { + "description": "Случайный идентификатор API-процесса; меняется при старте.", + "schema": { + "type": "string", + "minLength": 1 + } } }, "content": { @@ -1487,7 +1487,7 @@ } }, "X-Instance-Id": { - "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "description": "Случайный идентификатор API-процесса; меняется при старте.", "schema": { "type": "string", "minLength": 1 @@ -1513,7 +1513,7 @@ } }, "X-Instance-Id": { - "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "description": "Случайный идентификатор API-процесса; меняется при старте.", "schema": { "type": "string", "minLength": 1 @@ -1545,11 +1545,9 @@ "required": true, "schema": { "type": "string", - "minLength": 1, - "maxLength": 128, - "pattern": "^[A-Za-z0-9._:-]+$" + "pattern": "^[A-Za-z0-9._:-]{1,128}$" }, - "description": "Обязателен с lab3. Область: пользователь + метод + путь + ключ; хранить результат не менее 24 ч. Повтор того же JSON возвращает исходный 201 и то же сообщение, другой JSON — 409." + "description": "Общий ключ результата POST на 24 часа; повтор другого тела — 409." } ], "requestBody": { @@ -1578,7 +1576,7 @@ } }, "X-Instance-Id": { - "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "description": "Случайный идентификатор API-процесса; меняется при старте.", "schema": { "type": "string", "minLength": 1 @@ -1604,7 +1602,7 @@ } }, "X-Instance-Id": { - "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "description": "Случайный идентификатор API-процесса; меняется при старте.", "schema": { "type": "string", "minLength": 1 @@ -1629,18 +1627,18 @@ "minLength": 1 } }, - "X-Instance-Id": { - "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", - "schema": { - "type": "string", - "minLength": 1 - } - }, "WWW-Authenticate": { "description": "Basic realm=\"messenger\", charset=\"UTF-8\" для входа; Bearer realm=\"messenger\" для защищённого API lab2+.", "schema": { "type": "string" } + }, + "X-Instance-Id": { + "description": "Случайный идентификатор API-процесса; меняется при старте.", + "schema": { + "type": "string", + "minLength": 1 + } } }, "content": { @@ -1662,7 +1660,7 @@ } }, "X-Instance-Id": { - "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "description": "Случайный идентификатор API-процесса; меняется при старте.", "schema": { "type": "string", "minLength": 1 @@ -1688,7 +1686,7 @@ } }, "X-Instance-Id": { - "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "description": "Случайный идентификатор API-процесса; меняется при старте.", "schema": { "type": "string", "minLength": 1 @@ -1713,19 +1711,19 @@ "minLength": 1 } }, - "X-Instance-Id": { - "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", - "schema": { - "type": "string", - "minLength": 1 - } - }, "Retry-After": { "description": "Положительное число секунд.", "schema": { "type": "integer", "minimum": 1 } + }, + "X-Instance-Id": { + "description": "Случайный идентификатор API-процесса; меняется при старте.", + "schema": { + "type": "string", + "minLength": 1 + } } }, "content": { @@ -1747,7 +1745,7 @@ } }, "X-Instance-Id": { - "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "description": "Случайный идентификатор API-процесса; меняется при старте.", "schema": { "type": "string", "minLength": 1 @@ -1773,7 +1771,7 @@ } }, "X-Instance-Id": { - "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "description": "Случайный идентификатор API-процесса; меняется при старте.", "schema": { "type": "string", "minLength": 1 @@ -1836,7 +1834,7 @@ } }, "X-Instance-Id": { - "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "description": "Случайный идентификатор API-процесса; меняется при старте.", "schema": { "type": "string", "minLength": 1 @@ -1862,7 +1860,7 @@ } }, "X-Instance-Id": { - "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "description": "Случайный идентификатор API-процесса; меняется при старте.", "schema": { "type": "string", "minLength": 1 @@ -1887,18 +1885,18 @@ "minLength": 1 } }, - "X-Instance-Id": { - "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", - "schema": { - "type": "string", - "minLength": 1 - } - }, "WWW-Authenticate": { "description": "Basic realm=\"messenger\", charset=\"UTF-8\" для входа; Bearer realm=\"messenger\" для защищённого API lab2+.", "schema": { "type": "string" } + }, + "X-Instance-Id": { + "description": "Случайный идентификатор API-процесса; меняется при старте.", + "schema": { + "type": "string", + "minLength": 1 + } } }, "content": { @@ -1919,19 +1917,19 @@ "minLength": 1 } }, - "X-Instance-Id": { - "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", - "schema": { - "type": "string", - "minLength": 1 - } - }, "Retry-After": { "description": "Положительное число секунд.", "schema": { "type": "integer", "minimum": 1 } + }, + "X-Instance-Id": { + "description": "Случайный идентификатор API-процесса; меняется при старте.", + "schema": { + "type": "string", + "minLength": 1 + } } }, "content": { @@ -1953,7 +1951,7 @@ } }, "X-Instance-Id": { - "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "description": "Случайный идентификатор API-процесса; меняется при старте.", "schema": { "type": "string", "minLength": 1 @@ -1979,7 +1977,7 @@ } }, "X-Instance-Id": { - "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "description": "Случайный идентификатор API-процесса; меняется при старте.", "schema": { "type": "string", "minLength": 1 @@ -2018,7 +2016,7 @@ } }, "X-Instance-Id": { - "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "description": "Случайный идентификатор API-процесса; меняется при старте.", "schema": { "type": "string", "minLength": 1 @@ -2036,18 +2034,18 @@ "minLength": 1 } }, - "X-Instance-Id": { - "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", - "schema": { - "type": "string", - "minLength": 1 - } - }, "WWW-Authenticate": { "description": "Basic realm=\"messenger\", charset=\"UTF-8\" для входа; Bearer realm=\"messenger\" для защищённого API lab2+.", "schema": { "type": "string" } + }, + "X-Instance-Id": { + "description": "Случайный идентификатор API-процесса; меняется при старте.", + "schema": { + "type": "string", + "minLength": 1 + } } }, "content": { @@ -2068,19 +2066,19 @@ "minLength": 1 } }, - "X-Instance-Id": { - "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", - "schema": { - "type": "string", - "minLength": 1 - } - }, "Retry-After": { "description": "Положительное число секунд.", "schema": { "type": "integer", "minimum": 1 } + }, + "X-Instance-Id": { + "description": "Случайный идентификатор API-процесса; меняется при старте.", + "schema": { + "type": "string", + "minLength": 1 + } } }, "content": { @@ -2102,7 +2100,7 @@ } }, "X-Instance-Id": { - "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "description": "Случайный идентификатор API-процесса; меняется при старте.", "schema": { "type": "string", "minLength": 1 @@ -2128,7 +2126,7 @@ } }, "X-Instance-Id": { - "description": "Идентификатор обслужившей API-реплики; стабилен в пределах жизни процесса. В lab7 используйте непрозрачный alias, без hostname/IP.", + "description": "Случайный идентификатор API-процесса; меняется при старте.", "schema": { "type": "string", "minLength": 1 @@ -2426,6 +2424,15 @@ "type": "string", "minLength": 1, "maxLength": 4000 + }, + "attachment_ids": { + "type": "array", + "items": { + "type": "string", + "format": "uuid" + }, + "maxItems": 0, + "default": [] } }, "required": [ diff --git a/env.lab3.template b/env.lab3.template new file mode 100644 index 00000000..3a4c6c38 --- /dev/null +++ b/env.lab3.template @@ -0,0 +1,7 @@ +POSTGRES_USER=messenger +POSTGRES_PASSWORD=CHANGE_ME_TO_RANDOM_ASCII_PASSWORD +POSTGRES_DB=messenger +SESSION_TTL_SECONDS=3600 +CACHE_URL=redis://redis:6379/0 +LOG_LEVEL=WARNING +API_IMAGE=mtusi-messenger:lab3 diff --git a/evidence/README.md b/evidence/README.md index 8df70015..c94db9dd 100644 --- a/evidence/README.md +++ b/evidence/README.md @@ -1,5 +1,6 @@ -# Доказательства +# Evidence -Храните небольшие обезличенные результаты: команды и stdout проверок, JSON/CSV агрегатов нагрузки, EXPLAIN, dashboard JSON, текст разбора отказа. Укажите дату, commit, аппаратные лимиты и профиль нагрузки. - -Не добавляйте .env, session tokens, cookies, приватные ключи, credentials, реальные сообщения, raw dumps и signed URLs. Для скриншотов скрывайте секреты. Большие логи/бинарные traces сохраняйте отдельно по правилам преподавателя; одного скриншота без методики недостаточно. +`local/` — реальные проверки текущей подготовки с точными командами и кодами. +`lab2-history/` — исходные результаты предыдущей лабораторной, не доказательство lab3. +`lab3/` создаётся командой `make lab3-evidence` на работающем Docker-стенде. +Методика, значения метрик и ограничения: [EVIDENCE.md](../EVIDENCE.md). diff --git a/evidence/lab2-history/01_check_contract.txt b/evidence/lab2-history/01_check_contract.txt new file mode 100644 index 00000000..9cd80901 --- /dev/null +++ b/evidence/lab2-history/01_check_contract.txt @@ -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 с diff --git a/evidence/lab2-history/02_smoke.txt b/evidence/lab2-history/02_smoke.txt new file mode 100644 index 00000000..ac067707 --- /dev/null +++ b/evidence/lab2-history/02_smoke.txt @@ -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 с diff --git a/evidence/lab2-history/03_ps_before.txt b/evidence/lab2-history/03_ps_before.txt new file mode 100644 index 00000000..5ab247ee --- /dev/null +++ b/evidence/lab2-history/03_ps_before.txt @@ -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 с diff --git a/evidence/lab2-history/03_volume_before.txt b/evidence/lab2-history/03_volume_before.txt new file mode 100644 index 00000000..2a043dcf --- /dev/null +++ b/evidence/lab2-history/03_volume_before.txt @@ -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 с diff --git a/evidence/lab2-history/04_persistence.txt b/evidence/lab2-history/04_persistence.txt new file mode 100644 index 00000000..eb7a745e --- /dev/null +++ b/evidence/lab2-history/04_persistence.txt @@ -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 с diff --git a/evidence/lab2-history/05_ps_after.txt b/evidence/lab2-history/05_ps_after.txt new file mode 100644 index 00000000..f812e4a2 --- /dev/null +++ b/evidence/lab2-history/05_ps_after.txt @@ -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 с diff --git a/evidence/lab2-history/05_volume_after.txt b/evidence/lab2-history/05_volume_after.txt new file mode 100644 index 00000000..4077c2fd --- /dev/null +++ b/evidence/lab2-history/05_volume_after.txt @@ -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 с diff --git a/evidence/lab2-history/06_schema.txt b/evidence/lab2-history/06_schema.txt new file mode 100644 index 00000000..c383852b --- /dev/null +++ b/evidence/lab2-history/06_schema.txt @@ -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 с diff --git a/evidence/lab2-history/07_migrations.txt b/evidence/lab2-history/07_migrations.txt new file mode 100644 index 00000000..98fe6ad4 --- /dev/null +++ b/evidence/lab2-history/07_migrations.txt @@ -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 с diff --git a/evidence/lab2-history/07_migrations_first.txt b/evidence/lab2-history/07_migrations_first.txt new file mode 100644 index 00000000..9b7ff6ee --- /dev/null +++ b/evidence/lab2-history/07_migrations_first.txt @@ -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 с diff --git a/evidence/lab2-history/07_migrations_table.txt b/evidence/lab2-history/07_migrations_table.txt new file mode 100644 index 00000000..6c3e6a57 --- /dev/null +++ b/evidence/lab2-history/07_migrations_table.txt @@ -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 с diff --git a/evidence/lab2-history/08_hashes.txt b/evidence/lab2-history/08_hashes.txt new file mode 100644 index 00000000..c29f933d --- /dev/null +++ b/evidence/lab2-history/08_hashes.txt @@ -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 с diff --git a/evidence/lab2-history/09_auth_tests.txt b/evidence/lab2-history/09_auth_tests.txt new file mode 100644 index 00000000..53e77f15 --- /dev/null +++ b/evidence/lab2-history/09_auth_tests.txt @@ -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 с diff --git a/evidence/lab2-history/10_database_tests.txt b/evidence/lab2-history/10_database_tests.txt new file mode 100644 index 00000000..49be0ee1 --- /dev/null +++ b/evidence/lab2-history/10_database_tests.txt @@ -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 с diff --git a/evidence/lab2-history/11_all_tests.txt b/evidence/lab2-history/11_all_tests.txt new file mode 100644 index 00000000..7f42c0ef --- /dev/null +++ b/evidence/lab2-history/11_all_tests.txt @@ -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 с diff --git a/evidence/lab2-history/12_compose_no_secrets.txt b/evidence/lab2-history/12_compose_no_secrets.txt new file mode 100644 index 00000000..b3c25f2c --- /dev/null +++ b/evidence/lab2-history/12_compose_no_secrets.txt @@ -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 с diff --git a/evidence/lab2-history/12_git_files.txt b/evidence/lab2-history/12_git_files.txt new file mode 100644 index 00000000..d027d2f7 --- /dev/null +++ b/evidence/lab2-history/12_git_files.txt @@ -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 с diff --git a/evidence/lab2-history/12_git_secrets.txt b/evidence/lab2-history/12_git_secrets.txt new file mode 100644 index 00000000..273f7eb1 --- /dev/null +++ b/evidence/lab2-history/12_git_secrets.txt @@ -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 с diff --git a/evidence/lab2-history/13_storage.txt b/evidence/lab2-history/13_storage.txt new file mode 100644 index 00000000..95934f9c --- /dev/null +++ b/evidence/lab2-history/13_storage.txt @@ -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). diff --git a/evidence/lab2-history/14_dependencies.txt b/evidence/lab2-history/14_dependencies.txt new file mode 100644 index 00000000..552c01bc --- /dev/null +++ b/evidence/lab2-history/14_dependencies.txt @@ -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: +58542 +TTL ключа сессии: 58542 мс (<= CACHE_TTL_SECONDS=60 с и <= остатка сессии) +$ docker compose --env-file .env exec -T redis redis-cli GET session: +{"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: +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: +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 без деталей и восстанавливается. diff --git a/evidence/lab2-history/15_explain.txt b/evidence/lab2-history/15_explain.txt new file mode 100644 index 00000000..9c9b47c3 --- /dev/null +++ b/evidence/lab2-history/15_explain.txt @@ -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 с diff --git a/evidence/lab2-history/15_explain_tests.txt b/evidence/lab2-history/15_explain_tests.txt new file mode 100644 index 00000000..e6ca2cff --- /dev/null +++ b/evidence/lab2-history/15_explain_tests.txt @@ -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 с diff --git a/evidence/lab2-history/16_expiry_live.txt b/evidence/lab2-history/16_expiry_live.txt new file mode 100644 index 00000000..4129e818 --- /dev/null +++ b/evidence/lab2-history/16_expiry_live.txt @@ -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 с diff --git a/evidence/lab2-history/17_mr_regressions.txt b/evidence/lab2-history/17_mr_regressions.txt new file mode 100644 index 00000000..b297dcb7 --- /dev/null +++ b/evidence/lab2-history/17_mr_regressions.txt @@ -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. diff --git a/evidence/lab3-run2/00_environment.json b/evidence/lab3-run2/00_environment.json new file mode 100644 index 00000000..2ac67756 --- /dev/null +++ b/evidence/lab3-run2/00_environment.json @@ -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" +} diff --git a/evidence/lab3-run2/01_docker.txt b/evidence/lab3-run2/01_docker.txt new file mode 100644 index 00000000..3da53c80 --- /dev/null +++ b/evidence/lab3-run2/01_docker.txt @@ -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 diff --git a/evidence/lab3-run2/02_compose_config.txt b/evidence/lab3-run2/02_compose_config.txt new file mode 100644 index 00000000..82f18769 --- /dev/null +++ b/evidence/lab3-run2/02_compose_config.txt @@ -0,0 +1,3 @@ +$ docker compose --env-file .env config --quiet + +exit_code=0; elapsed_seconds=0.297 diff --git a/evidence/lab3-run2/03_container_limits.txt b/evidence/lab3-run2/03_container_limits.txt new file mode 100644 index 00000000..0f716088 --- /dev/null +++ b/evidence/lab3-run2/03_container_limits.txt @@ -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 diff --git a/evidence/lab3-run2/03_limits.txt b/evidence/lab3-run2/03_limits.txt new file mode 100644 index 00000000..99a2453c --- /dev/null +++ b/evidence/lab3-run2/03_limits.txt @@ -0,0 +1,8 @@ +$ docker compose --env-file .env ps -q +ef1e096f1c30593e0d8e9ebeb467a21c997580d835473c76142127d2d402b235 +4cccbf699f55df2a5e9fd6e19aa897796654a47cdac2ff372565fa6c2a4677e0 +765a7b20a31ea3ea8cfd024fe9d4a938c46f4d23d743e4db310b354d357014d8 +5675a02e89826f64b25026269d5dc9177a7172ce4922722875c4d986e8423125 +1b3ad58c1dd2bec01048e78af86e3c9812172df3c4189eab27352b11a950673d + +exit_code=0; elapsed_seconds=0.344 diff --git a/evidence/lab3-run2/03_processes.json b/evidence/lab3-run2/03_processes.json new file mode 100644 index 00000000..52f26628 --- /dev/null +++ b/evidence/lab3-run2/03_processes.json @@ -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" + } +] diff --git a/evidence/lab3-run2/04_smoke.txt b/evidence/lab3-run2/04_smoke.txt new file mode 100644 index 00000000..2c8a074f --- /dev/null +++ b/evidence/lab3-run2/04_smoke.txt @@ -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 diff --git a/evidence/lab3-run2/05_concurrent_idempotency.json b/evidence/lab3-run2/05_concurrent_idempotency.json new file mode 100644 index 00000000..8b48d5e8 --- /dev/null +++ b/evidence/lab3-run2/05_concurrent_idempotency.json @@ -0,0 +1,10 @@ +{ + "parallel_requests": 12, + "unique_message_ids": 1, + "history_rows": 1, + "instances": [ + "128e6b095f444dffa33214cd3cd33184", + "1b38655b9a21447494973eb99d5ea039" + ], + "conflict_status": 409 +} diff --git a/evidence/lab3-run2/06_failover_api-a.json b/evidence/lab3-run2/06_failover_api-a.json new file mode 100644 index 00000000..eb8124b0 --- /dev/null +++ b/evidence/lab3-run2/06_failover_api-a.json @@ -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 + } + } +} diff --git a/evidence/lab3-run2/06_failover_api-b.json b/evidence/lab3-run2/06_failover_api-b.json new file mode 100644 index 00000000..29b21708 --- /dev/null +++ b/evidence/lab3-run2/06_failover_api-b.json @@ -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 + } + } +} diff --git a/evidence/lab3-run2/07_public_failover.txt b/evidence/lab3-run2/07_public_failover.txt new file mode 100644 index 00000000..4a5f8ea4 --- /dev/null +++ b/evidence/lab3-run2/07_public_failover.txt @@ -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 diff --git a/evidence/lab3-run2/08_rolling_api-a.json b/evidence/lab3-run2/08_rolling_api-a.json new file mode 100644 index 00000000..4d3ac9c3 --- /dev/null +++ b/evidence/lab3-run2/08_rolling_api-a.json @@ -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 + } + } +} diff --git a/evidence/lab3-run2/08_rolling_api-b.json b/evidence/lab3-run2/08_rolling_api-b.json new file mode 100644 index 00000000..5a295962 --- /dev/null +++ b/evidence/lab3-run2/08_rolling_api-b.json @@ -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 + } + } +} diff --git a/evidence/lab3-run2/09_persistence.txt b/evidence/lab3-run2/09_persistence.txt new file mode 100644 index 00000000..8f3f3250 --- /dev/null +++ b/evidence/lab3-run2/09_persistence.txt @@ -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 diff --git a/evidence/lab3-run2/10_processes_after.json b/evidence/lab3-run2/10_processes_after.json new file mode 100644 index 00000000..ee308ba7 --- /dev/null +++ b/evidence/lab3-run2/10_processes_after.json @@ -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" + } +] diff --git a/evidence/lab3-run2/11_nginx.txt b/evidence/lab3-run2/11_nginx.txt new file mode 100644 index 00000000..7e06ee2c --- /dev/null +++ b/evidence/lab3-run2/11_nginx.txt @@ -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 diff --git a/evidence/lab3-run2/12_student_tests.txt b/evidence/lab3-run2/12_student_tests.txt new file mode 100644 index 00000000..6d9c9551 --- /dev/null +++ b/evidence/lab3-run2/12_student_tests.txt @@ -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 diff --git a/evidence/lab3-run2/result.json b/evidence/lab3-run2/result.json new file mode 100644 index 00000000..5c61a6b5 --- /dev/null +++ b/evidence/lab3-run2/result.json @@ -0,0 +1,4 @@ +{ + "status": "PASS", + "at_utc": "2026-11-02T14:38:48.559702+00:00" +} diff --git a/evidence/lab3/00_environment.json b/evidence/lab3/00_environment.json new file mode 100644 index 00000000..974f7c3a --- /dev/null +++ b/evidence/lab3/00_environment.json @@ -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" +} diff --git a/evidence/lab3/01_docker.txt b/evidence/lab3/01_docker.txt new file mode 100644 index 00000000..f9e83429 --- /dev/null +++ b/evidence/lab3/01_docker.txt @@ -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 diff --git a/evidence/lab3/02_compose_config.txt b/evidence/lab3/02_compose_config.txt new file mode 100644 index 00000000..82f18769 --- /dev/null +++ b/evidence/lab3/02_compose_config.txt @@ -0,0 +1,3 @@ +$ docker compose --env-file .env config --quiet + +exit_code=0; elapsed_seconds=0.297 diff --git a/evidence/lab3/03_container_limits.txt b/evidence/lab3/03_container_limits.txt new file mode 100644 index 00000000..63b39049 --- /dev/null +++ b/evidence/lab3/03_container_limits.txt @@ -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 diff --git a/evidence/lab3/03_limits.txt b/evidence/lab3/03_limits.txt new file mode 100644 index 00000000..b7594e29 --- /dev/null +++ b/evidence/lab3/03_limits.txt @@ -0,0 +1,8 @@ +$ docker compose --env-file .env ps -q +b5f9cdc58cfa1e5ba32d7219909ca0b02f6984e2d69ec6090872f7c1aa3d40b8 +3923c6b10754bdf24cf03312f2cdfc47d0c57b626e413c15c6bf99584795866a +765a7b20a31ea3ea8cfd024fe9d4a938c46f4d23d743e4db310b354d357014d8 +5675a02e89826f64b25026269d5dc9177a7172ce4922722875c4d986e8423125 +1b3ad58c1dd2bec01048e78af86e3c9812172df3c4189eab27352b11a950673d + +exit_code=0; elapsed_seconds=0.407 diff --git a/evidence/lab3/03_processes.json b/evidence/lab3/03_processes.json new file mode 100644 index 00000000..21b5e944 --- /dev/null +++ b/evidence/lab3/03_processes.json @@ -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" + } +] diff --git a/evidence/lab3/04_smoke.txt b/evidence/lab3/04_smoke.txt new file mode 100644 index 00000000..5bd059eb --- /dev/null +++ b/evidence/lab3/04_smoke.txt @@ -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 diff --git a/evidence/lab3/05_concurrent_idempotency.json b/evidence/lab3/05_concurrent_idempotency.json new file mode 100644 index 00000000..6fe27a66 --- /dev/null +++ b/evidence/lab3/05_concurrent_idempotency.json @@ -0,0 +1,10 @@ +{ + "parallel_requests": 12, + "unique_message_ids": 1, + "history_rows": 1, + "instances": [ + "a9caf7e09cdc4d63bf6c0a51f92d38f3", + "f8a853f11dcd42be9d37227923a8b62a" + ], + "conflict_status": 409 +} diff --git a/evidence/lab3/06_failover_api-a.json b/evidence/lab3/06_failover_api-a.json new file mode 100644 index 00000000..288c5b0a --- /dev/null +++ b/evidence/lab3/06_failover_api-a.json @@ -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 + } + } +} diff --git a/evidence/lab3/06_failover_api-b.json b/evidence/lab3/06_failover_api-b.json new file mode 100644 index 00000000..94d777e3 --- /dev/null +++ b/evidence/lab3/06_failover_api-b.json @@ -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 + } + } +} diff --git a/evidence/lab3/07_public_failover.txt b/evidence/lab3/07_public_failover.txt new file mode 100644 index 00000000..4abdf54e --- /dev/null +++ b/evidence/lab3/07_public_failover.txt @@ -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 diff --git a/evidence/lab3/08_rolling_api-a.json b/evidence/lab3/08_rolling_api-a.json new file mode 100644 index 00000000..cf3e3d0e --- /dev/null +++ b/evidence/lab3/08_rolling_api-a.json @@ -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 + } + } +} diff --git a/evidence/lab3/08_rolling_api-b.json b/evidence/lab3/08_rolling_api-b.json new file mode 100644 index 00000000..fe932462 --- /dev/null +++ b/evidence/lab3/08_rolling_api-b.json @@ -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 + } + } +} diff --git a/evidence/lab3/09_persistence.txt b/evidence/lab3/09_persistence.txt new file mode 100644 index 00000000..efab0153 --- /dev/null +++ b/evidence/lab3/09_persistence.txt @@ -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 diff --git a/evidence/lab3/10_processes_after.json b/evidence/lab3/10_processes_after.json new file mode 100644 index 00000000..52f26628 --- /dev/null +++ b/evidence/lab3/10_processes_after.json @@ -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" + } +] diff --git a/evidence/lab3/11_nginx.txt b/evidence/lab3/11_nginx.txt new file mode 100644 index 00000000..acd07f29 --- /dev/null +++ b/evidence/lab3/11_nginx.txt @@ -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 diff --git a/evidence/lab3/12_student_tests.txt b/evidence/lab3/12_student_tests.txt new file mode 100644 index 00000000..2010bfaa --- /dev/null +++ b/evidence/lab3/12_student_tests.txt @@ -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 diff --git a/evidence/lab3/result.json b/evidence/lab3/result.json new file mode 100644 index 00000000..c27bebd4 --- /dev/null +++ b/evidence/lab3/result.json @@ -0,0 +1,4 @@ +{ + "status": "PASS", + "at_utc": "2026-11-02T14:15:07.979020+00:00" +} diff --git a/evidence/local/01_check.txt b/evidence/local/01_check.txt new file mode 100644 index 00000000..3f0ce0af --- /dev/null +++ b/evidence/local/01_check.txt @@ -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 diff --git a/evidence/local/02_openapi.txt b/evidence/local/02_openapi.txt new file mode 100644 index 00000000..d9b61e43 --- /dev/null +++ b/evidence/local/02_openapi.txt @@ -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 diff --git a/evidence/local/03_static_lab3.txt b/evidence/local/03_static_lab3.txt new file mode 100644 index 00000000..51ab911b --- /dev/null +++ b/evidence/local/03_static_lab3.txt @@ -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 diff --git a/evidence/local/04_unit.txt b/evidence/local/04_unit.txt new file mode 100644 index 00000000..211c7d3f --- /dev/null +++ b/evidence/local/04_unit.txt @@ -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 diff --git a/evidence/local/05_collection.txt b/evidence/local/05_collection.txt new file mode 100644 index 00000000..68b17e53 --- /dev/null +++ b/evidence/local/05_collection.txt @@ -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 diff --git a/evidence/local/06_dependencies.txt b/evidence/local/06_dependencies.txt new file mode 100644 index 00000000..8f32567f --- /dev/null +++ b/evidence/local/06_dependencies.txt @@ -0,0 +1,4 @@ +$ /workspace/scratch/03a740016edf/mtusi2026lab3/../.venv/bin/python -m pip check +No broken requirements found. + +exit_code=0 diff --git a/evidence/local/manifest.json b/evidence/local/manifest.json new file mode 100644 index 00000000..6ad0db03 --- /dev/null +++ b/evidence/local/manifest.json @@ -0,0 +1,72 @@ +{ + "at_utc": "2026-11-01T13:10:16.405188+00:00", + "python": "3.12.14", + "checks": [ + { + "name": "01_check", + "command": [ + "/workspace/scratch/03a740016edf/mtusi2026lab3/../.venv/bin/python", + "scripts/check_contract.py" + ], + "exit_code": 0, + "elapsed_seconds": 0.042 + }, + { + "name": "02_openapi", + "command": [ + "/workspace/scratch/03a740016edf/mtusi2026lab3/../.venv/bin/python", + "scripts/validate_openapi.py" + ], + "exit_code": 0, + "elapsed_seconds": 0.457 + }, + { + "name": "03_static_lab3", + "command": [ + "/workspace/scratch/03a740016edf/mtusi2026lab3/../.venv/bin/python", + "scripts/check_lab3.py" + ], + "exit_code": 0, + "elapsed_seconds": 0.093 + }, + { + "name": "04_unit", + "command": [ + "/workspace/scratch/03a740016edf/mtusi2026lab3/../.venv/bin/python", + "-m", + "pytest", + "-q", + "unit-tests", + "-p", + "no:cacheprovider" + ], + "exit_code": 0, + "elapsed_seconds": 1.257 + }, + { + "name": "05_collection", + "command": [ + "/workspace/scratch/03a740016edf/mtusi2026lab3/../.venv/bin/python", + "-m", + "pytest", + "--collect-only", + "-q", + "-p", + "no:cacheprovider" + ], + "exit_code": 0, + "elapsed_seconds": 0.895 + }, + { + "name": "06_dependencies", + "command": [ + "/workspace/scratch/03a740016edf/mtusi2026lab3/../.venv/bin/python", + "-m", + "pip", + "check" + ], + "exit_code": 0, + "elapsed_seconds": 0.242 + } + ] +} diff --git a/evidence/local/runtime_status.json b/evidence/local/runtime_status.json new file mode 100644 index 00000000..b1fec732 --- /dev/null +++ b/evidence/local/runtime_status.json @@ -0,0 +1,7 @@ +{ + "docker_cli_available": false, + "nginx_binary_available": false, + "compose_nginx_postgresql_redis_experiments": "NOT_RUN", + "reason": "This local checker does not run infrastructure; execute make lab3-evidence separately", + "required_command_on_docker_host": "make lab3-evidence" +} diff --git a/examples/requests.http b/examples/requests.http index 492f9383..6ea0ab6f 100644 --- a/examples/requests.http +++ b/examples/requests.http @@ -33,10 +33,10 @@ Content-Type: application/json {"title":"Проект по ВНП","member_ids":["{{aliceId}}","{{bobId}}"]} -### Send message +### Send message (repeat unchanged with the same key to retrieve the same result) POST {{baseUrl}}/api/v1/chats/{{chatId}}/messages +Idempotency-Key: example-message-1 Authorization: Bearer {{sessionToken}} -Idempotency-Key: example-message-001 Content-Type: application/json {"text":"Привет, Боб!"} diff --git a/infra/README.md b/infra/README.md index 5a39e862..cfb212ee 100644 --- a/infra/README.md +++ b/infra/README.md @@ -1,7 +1,10 @@ -# Инфраструктура студента +# Инфраструктура lab3 -Здесь разместите конфигурации выбранных сервисов; основную топологию — в корневом compose.yaml/stack.yaml. `compose.example.yaml` — только worksheet ролей и намеренно не запускается. `contracts/infrastructure.json` задаёт минимальные роли, не готовые образы/пароли. +`nginx/nginx.conf` — proxy, round robin, DNS и таймауты. +`nginx/entrypoint.sh` — активные readiness-пробы и reload upstream. +Топология, сети, ресурсы, одноразовый migrate и volumes — в корневом compose.yaml. -В REPORT.md сопоставьте роль → service name → версия/digest → health/readiness → volume → сеть/порт → способ передачи credentials. Приведите bootstrap (schema, bucket, broker topology, а с lab6 provisioning) и безопасное повторение этих команд. - -Приложение строится из исходников. Не используйте заранее существующий образ преподавательского сервера вместо своей реализации. Restart hook перезапускает все соответствующие stores с volumes; failover hook останавливает только заданную API-реплику. +Nginx OSS используется без коммерческих health_check-директив. Docker socket +в контейнеры не монтируется. Внешний порт опубликован только на LB; marker drain +позволяет scripts/replicas.py исключать одну API для плановой замены. +Подробные интервалы и границы — REPORT.md. diff --git a/infra/nginx/entrypoint.sh b/infra/nginx/entrypoint.sh new file mode 100644 index 00000000..a59c3153 --- /dev/null +++ b/infra/nginx/entrypoint.sh @@ -0,0 +1,45 @@ +#!/bin/sh +set -eu + +render() { + printf 'upstream api_backend {\n zone api_backend 64k;\n' + count=0 + for host in api-a api-b; do + if [ ! -e "/tmp/drain-$host" ] && wget -q -T 1 -O /dev/null "http://$host:8080/health/ready"; then + printf ' server %s:8080 resolve max_fails=1 fail_timeout=2s;\n' "$host" + count=$((count + 1)) + fi + done + if [ "$count" -eq 0 ]; then + printf ' server 127.0.0.1:9;\n' + fi + printf '}\n' +} + +render > /tmp/backends.conf +nginx -c /opt/lb/nginx.conf -g 'daemon off;' & +master=$! + +shutdown() { + kill -QUIT "$master" 2>/dev/null || true + wait "$master" || true + exit 0 +} +trap shutdown TERM INT QUIT + +while kill -0 "$master" 2>/dev/null; do + sleep 1 & + wait $! || true + render > /tmp/backends.next + if ! cmp -s /tmp/backends.next /tmp/backends.conf; then + cp /tmp/backends.conf /tmp/backends.previous + mv /tmp/backends.next /tmp/backends.conf + if nginx -t -c /opt/lb/nginx.conf; then + kill -HUP "$master" + printf 'LB ready backends: %s\n' "$(tr '\n' ' ' < /tmp/backends.conf)" + else + mv /tmp/backends.previous /tmp/backends.conf + fi + fi +done +wait "$master" diff --git a/infra/nginx/nginx.conf b/infra/nginx/nginx.conf new file mode 100644 index 00000000..08400a3f --- /dev/null +++ b/infra/nginx/nginx.conf @@ -0,0 +1,33 @@ +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; + } + } +} diff --git a/lab.json b/lab.json index 57bb9fbb..5194777c 100644 --- a/lab.json +++ b/lab.json @@ -4,5 +4,5 @@ "contract_version": "1.3.0", "default_base_url": "http://localhost:8080", "grading": "3; 4 includes 3; 5 includes 4 plus one completed research track", - "scaffold_has_application": false + "scaffold_has_application": true } diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 00000000..10af536e --- /dev/null +++ b/pytest.ini @@ -0,0 +1,4 @@ +[pytest] +testpaths = student-tests unit-tests +pythonpath = . +addopts = -ra diff --git a/requirements-dev.txt b/requirements-dev.txt index 6b103ddb..66216d76 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -1,2 +1,3 @@ # Only for full OpenAPI/JSON Schema validation; HTTP tests use stdlib. openapi-spec-validator==0.7.2 +PyYAML==6.0.3 diff --git a/requirements-test.txt b/requirements-test.txt new file mode 100644 index 00000000..72a5be34 --- /dev/null +++ b/requirements-test.txt @@ -0,0 +1,10 @@ +# Зависимости собственных тестов (student-tests/). Ставятся только в test-образ +# (Dockerfile, target test) поверх requirements.txt. +certifi==2026.10.19 +httpcore==1.0.9 +httpx==0.28.1 +iniconfig==2.3.1 +packaging==26.3 +pluggy==1.7.0 +Pygments==2.21.0 +pytest==9.1.1 diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 00000000..c4395cbe --- /dev/null +++ b/requirements.txt @@ -0,0 +1,20 @@ +# Зависимости API (runtime). Точные версии, включая транзитивные: сборка повторяема. +# Получено `pip freeze` в python:3.12.8-slim-bookworm. +annotated-doc==0.0.5 +annotated-types==0.8.0 +anyio==4.15.1 +asyncpg==0.32.0 +click==8.5.0 +fastapi==0.159.1 +greenlet==3.5.6 +h11==0.16.0 +idna==3.20 +opentelemetry-api==1.45.1 +pydantic==2.14.1 +pydantic_core==2.50.1 +redis==5.2.1 +SQLAlchemy==2.1.6 +starlette==1.9.0 +typing-inspection==0.4.4 +typing_extensions==4.16.0 +uvicorn==0.54.0 diff --git a/scripts/check_contract.py b/scripts/check_contract.py index dcb34dfa..a40b10d8 100644 --- a/scripts/check_contract.py +++ b/scripts/check_contract.py @@ -1,5 +1,6 @@ #!/usr/bin/env python3 """Offline structural checks; full schema validation is a separate target.""" + import ast import json from pathlib import Path @@ -7,48 +8,71 @@ import re ROOT = Path(__file__).resolve().parents[1] + def main(): - lab = json.loads((ROOT/'lab.json').read_text())['number'] - spec = json.loads((ROOT/'contracts/openapi.json').read_text()) - assert spec['openapi']=='3.1.0' - assert spec['info']['version']=='1.%d.0'%lab + lab = json.loads((ROOT / "lab.json").read_text())["number"] + spec = json.loads((ROOT / "contracts/openapi.json").read_text()) + assert spec["openapi"] == "3.1.0" + assert spec["info"]["version"] == "1.%d.0" % lab operation_ids = set() + def walk(value): - if isinstance(value,dict): - if '$ref' in value: - pointer = value['$ref'] - assert pointer.startswith('#/'), 'unexpected external reference' + if isinstance(value, dict): + if "$ref" in value: + pointer = value["$ref"] + assert pointer.startswith("#/"), "unexpected external reference" target = spec - for part in pointer[2:].split('/'): - target = target[part.replace('~1','/').replace('~0','~')] + for part in pointer[2:].split("/"): + target = target[part.replace("~1", "/").replace("~0", "~")] for v in value.values(): walk(v) - elif isinstance(value,list): + elif isinstance(value, list): for v in value: walk(v) + walk(spec) - for route,methods in spec['paths'].items(): - placeholders = set(re.findall(r'\{([^}]+)\}',route)) - for method,op in methods.items(): - assert op['operationId'] not in operation_ids, 'duplicate operationId' - operation_ids.add(op['operationId']) - params = {p['name'] for p in op.get('parameters',[]) if p['in']=='path' and p.get('required')} - assert placeholders==params, 'path parameter mismatch: '+route - for response in op['responses'].values(): - assert 'X-Request-Id' in response['headers'] - if lab>=3: - assert 'X-Instance-Id' in response['headers'] - assert ('/api/v1/auth/sessions' in spec['paths']) == (lab>=2) - assert ('/api/v1/attachments/{attachment_id}' in spec['paths']) == (lab>=4) - for file in list((ROOT/'tests').glob('*.py'))+list((ROOT/'scripts').glob('*.py')): - ast.parse(file.read_text(),filename=str(file)) - for file in ROOT.rglob('*.json'): - if any(part in {'.git','.venv','node_modules','vendor','output'} for part in file.parts): + for route, methods in spec["paths"].items(): + placeholders = set(re.findall(r"\{([^}]+)\}", route)) + for method, op in methods.items(): + assert op["operationId"] not in operation_ids, "duplicate operationId" + operation_ids.add(op["operationId"]) + params = { + p["name"] + for p in op.get("parameters", []) + if p["in"] == "path" and p.get("required") + } + assert placeholders == params, "path parameter mismatch: " + route + for response in op["responses"].values(): + assert "X-Request-Id" in response["headers"] + if lab >= 3: + assert "X-Instance-Id" in response["headers"] + assert ("/api/v1/auth/sessions" in spec["paths"]) == (lab >= 2) + assert ("/api/v1/attachments/{attachment_id}" in spec["paths"]) == (lab >= 4) + for file in list((ROOT / "tests").glob("*.py")) + list( + (ROOT / "scripts").glob("*.py") + ): + ast.parse(file.read_text(encoding="utf-8"), filename=str(file)) + for file in ROOT.rglob("*.json"): + if any( + part in {".git", ".venv", "node_modules", "vendor", "output"} + for part in file.parts + ): continue json.loads(file.read_text()) - for required in ('README.md','COURSE.md','CONTRIBUTING.md','REPORT.md','contracts/README.md','tests/smoke.py','tests/README.md'): - assert (ROOT/required).is_file(), 'missing '+required - print('PASS: offline skeleton structure, local refs and Python syntax; application NOT tested.') + for required in ( + "README.md", + "COURSE.md", + "CONTRIBUTING.md", + "REPORT.md", + "contracts/README.md", + "tests/smoke.py", + "tests/README.md", + ): + assert (ROOT / required).is_file(), "missing " + required + print( + "PASS: offline skeleton structure, local refs and Python syntax; application NOT tested." + ) -if __name__=='__main__': + +if __name__ == "__main__": main() diff --git a/scripts/check_expiry.py b/scripts/check_expiry.py new file mode 100644 index 00000000..d4185df8 --- /dev/null +++ b/scripts/check_expiry.py @@ -0,0 +1,90 @@ +#!/usr/bin/env python3 +"""Проверка срока жизни сессии, выхода и неверного пароля на запущенном API. + + python scripts/check_expiry.py --base-url http://localhost:8081 --ttl 3 + +API должен работать с SESSION_TTL_SECONDS, равным --ttl. +""" + +import argparse +import base64 +import json +import sys +import time +import urllib.error +import urllib.request +import uuid +from datetime import datetime, timezone + + +def http(base, method, path, body=None, headers=None): + """HTTP-запрос: (статус, заголовки, тело).""" + data = json.dumps(body).encode() if body is not None else None + request = urllib.request.Request(base + path, data=data, method=method, headers=dict(headers or {})) + if data is not None: + request.add_header("Content-Type", "application/json") + try: + with urllib.request.urlopen(request, timeout=10) as reply: + return reply.status, reply.headers, reply.read() + except urllib.error.HTTPError as error: + return error.code, error.headers, error.read() + + +def step(text, ok): + """Напечатать результат шага; при неудаче завершить с кодом 1.""" + print(("OK " if ok else "FAIL ") + text, flush=True) + if not ok: + sys.exit(1) + + +def main(): + """Выполнить проверку.""" + parser = argparse.ArgumentParser() + parser.add_argument("--base-url", default="http://localhost:8081") + parser.add_argument("--ttl", type=float, default=3) + args = parser.parse_args() + base = args.base_url.rstrip("/") + + username, password = "ttl_" + uuid.uuid4().hex[:8], "Expiry-" + uuid.uuid4().hex + basic = {"Authorization": "Basic " + base64.b64encode((username + ":" + password).encode()).decode()} + status, _, _ = http(base, "POST", "/api/v1/users", + {"username": username, "display_name": "TTL", "password": password}) + step("регистрация -> %d" % status, status == 201) + + status, _, raw = http(base, "POST", "/api/v1/auth/sessions", headers=basic) + session = json.loads(raw) + expires = datetime.fromisoformat(session["expires_at"].replace("Z", "+00:00")) + left = (expires - datetime.now(timezone.utc)).total_seconds() + step("вход -> %d, expires_at через %.1f с (TTL=%g)" % (status, left, args.ttl), + status == 201 and 0 < left <= args.ttl + 1) + bearer = {"Authorization": "Bearer " + session["token"]} + for n in range(3): + status, _, _ = http(base, "GET", "/api/v1/users/me", headers=bearer) + step("GET /users/me #%d до истечения -> %d" % (n + 1, status), status == 200) + + time.sleep(args.ttl + 1) + status, headers, raw = http(base, "GET", "/api/v1/users/me", headers=bearer) + step("GET /users/me после TTL+1 с -> %d, WWW-Authenticate: %s, code=%s" + % (status, headers.get("WWW-Authenticate"), json.loads(raw)["error"]["code"]), + status == 401 and headers.get("WWW-Authenticate") == 'Bearer realm="messenger"') + + status, _, raw = http(base, "POST", "/api/v1/auth/sessions", headers=basic) + bearer = {"Authorization": "Bearer " + json.loads(raw)["token"]} + step("новый вход -> %d" % status, status == 201) + status, _, _ = http(base, "DELETE", "/api/v1/auth/sessions/current", headers=bearer) + step("logout -> %d" % status, status == 204) + status, _, _ = http(base, "GET", "/api/v1/users/me", headers=bearer) + step("GET /users/me после logout -> %d" % status, status == 401) + status, _, _ = http(base, "DELETE", "/api/v1/auth/sessions/current", headers=bearer) + step("повторный logout -> %d" % status, status == 401) + + wrong = {"Authorization": "Basic " + base64.b64encode((username + ":wrong-password-123").encode()).decode()} + status, headers, _ = http(base, "POST", "/api/v1/auth/sessions", headers=wrong) + step("вход с неверным паролем -> %d, WWW-Authenticate: %s" % (status, headers.get("WWW-Authenticate")), + status == 401 and headers.get("WWW-Authenticate", "").startswith("Basic")) + print("PASS: expiry (TTL=%g с), logout и неверный пароль на запущенном API" % args.ttl) + return 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/scripts/check_lab3.py b/scripts/check_lab3.py new file mode 100644 index 00000000..f557af34 --- /dev/null +++ b/scripts/check_lab3.py @@ -0,0 +1,44 @@ +#!/usr/bin/env python3 +"""Статические ограничения стенда lab3; не заменяет docker compose config/nginx -t.""" + +import ast +import json +from pathlib import Path +import subprocess +import yaml + +ROOT = Path(__file__).resolve().parents[1] + + +def main(): + config = yaml.safe_load((ROOT / "compose.yaml").read_text()) + services = config["services"] + assert {"api-a", "api-b", "migrate", "nginx", "db", "redis"} <= services.keys() + assert config["networks"]["data"]["internal"] is True + for name, service in services.items(): + assert "container_name" not in service, name + assert float(service["cpus"]) > 0 and service["mem_limit"], name + if name != "nginx": + assert "ports" not in service, name + for name in ("api-a", "api-b"): + assert services[name]["depends_on"]["migrate"]["condition"] == "service_completed_successfully" + assert services[name]["stop_grace_period"] == "10s" + assert services["migrate"]["command"] == ["python", "-m", "app.migrate"] + assert services["nginx"]["image"] == "nginx:1.28.0-alpine" + assert services["nginx"]["networks"] == ["edge"] + assert not any("docker.sock" in str(s) for s in services.values()) + spec = json.loads((ROOT / "contracts/openapi.json").read_text()) + operation = spec["paths"]["/api/v1/chats/{chat_id}/messages"]["post"] + assert any(p["name"] == "Idempotency-Key" and p["required"] for p in operation["parameters"]) + for directory in ("app", "scripts", "student-tests", "unit-tests", "tests"): + for path in (ROOT / directory).rglob("*.py"): + ast.parse(path.read_text(), filename=str(path)) + for path in list((ROOT / "scripts").glob("*.sh")) + list((ROOT / "infra").rglob("*.sh")): + subprocess.run(["sh", "-n", str(path)], check=True) + for filename in ("scripts/stop-one.sh", "scripts/restart.sh"): + assert (ROOT / filename).stat().st_mode & 0o111 + print("PASS: lab3 static structure, resource/port isolation, contract, Python/sh syntax; runtime NOT tested.") + + +if __name__ == "__main__": + main() diff --git a/scripts/collect_evidence.sh b/scripts/collect_evidence.sh new file mode 100644 index 00000000..61c6a130 --- /dev/null +++ b/scripts/collect_evidence.sh @@ -0,0 +1,4 @@ +#!/bin/sh +set -eu +cd "$(dirname "$0")/.." +exec python3 scripts/lab3_evidence.py "$@" diff --git a/scripts/lab3_evidence.py b/scripts/lab3_evidence.py new file mode 100644 index 00000000..0ab305ee --- /dev/null +++ b/scripts/lab3_evidence.py @@ -0,0 +1,415 @@ +#!/usr/bin/env python3 +"""Явные эксперименты lab3: останавливает и восстанавливает API, заменяет контейнеры.""" + +import argparse +import http.client +from concurrent.futures import ThreadPoolExecutor +from datetime import datetime, timezone +import json +import os +from pathlib import Path +import platform +import subprocess +import sys +import threading +import time +import uuid + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT / "tests")) +from client import Client, Failure, check +from resilience import history +from replicas import ( + COMPOSE, + SERVICES, + compose, + inventory, + probe, + replace, + undrain, + wait_balanced, + wait_ready, +) + + +def save_json(path, value): + path.write_text(json.dumps(value, ensure_ascii=False, indent=2) + "\n") + + +def record_command(out, name, command, env=None): + """Записать реальный вывод команды; не запрашивать полный inspect или env.""" + started = time.monotonic() + result = subprocess.run(command, cwd=ROOT, text=True, capture_output=True, env=env) + (out / (name + ".txt")).write_text( + "$ " + + " ".join(command) + + "\n" + + result.stdout + + result.stderr + + f"\nexit_code={result.returncode}; elapsed_seconds={time.monotonic() - started:.3f}\n" + ) + check(result.returncode == 0, name + " failed; inspect evidence file") + + +def observe(c, actor): + seen = set() + for _ in range(50): + try: + resp = c.call("GET", "/api/v1/users/me", actor=actor, schema="User") + instance_id = resp["headers"].get("X-Instance-Id") + if instance_id: + seen.add(instance_id) + except Exception: + pass + if len(seen) >= 2: + break + time.sleep(0.1) + check(len(seen) >= 2, "one session must be accepted by at least two API processes") + return sorted(seen) + + +def concurrent_replay(base): + """Одновременный один ключ через LB; один id, одна строка истории, обе реплики.""" + c = Client(base) + alice, bob, eve, chat = c.bootstrap() + key = uuid.uuid4().hex + barrier = threading.Barrier(12) + + def worker(_): + local = Client(base) + barrier.wait() + return local.message(alice, chat, "одновременный повтор", key=key) + + with ThreadPoolExecutor(max_workers=12) as pool: + responses = list(pool.map(worker, range(12))) + ids = {r["json"]["id"] for r in responses} + instances = {r["headers"]["X-Instance-Id"] for r in responses} + check(len(ids) == 1, "concurrent replay created different messages") + check(len(instances) >= 2, "replay did not cross two API processes") + rows = history(c, bob, chat) + check( + len(rows) == 1 and rows[0] == responses[0]["json"], + "history must contain one exact response", + ) + c.call( + "POST", + "/api/v1/chats/" + chat["id"] + "/messages", + 409, + actor=alice, + body={"text": "другое тело"}, + headers={"Idempotency-Key": key}, + ) + replay = c.message(alice, chat, "одновременный повтор", key=key) + check(replay["json"] == responses[0]["json"], "409 changed original result") + return { + "parallel_requests": 12, + "unique_message_ids": len(ids), + "history_rows": len(rows), + "instances": sorted(instances), + "conflict_status": 409, + } + + +def experiment(base, service, mode, duration): + """Closed-loop чтение/запись во время stop или поочерёдной замены.""" + c = Client(base, timeout=2) + alice, bob, eve, chat = c.bootstrap() + initial = c.message(alice, chat, "подтверждено до действия")["json"] + seen = observe(c, alice) + before = inventory() + target = probe(service)["instance_id"] + events, confirmed = [], [initial] + lock = threading.Lock() + halt = threading.Event() + started = time.monotonic() + + def worker(number): + local = Client(base, timeout=2) + key, text = uuid.uuid4().hex, "" + while not halt.is_set(): + if not text: + text = "load-" + key + for operation in ("write", "read"): + t0 = time.monotonic() + try: + response = ( + local.message(alice, chat, text, key=key) + if operation == "write" + else local.call( + "GET", + "/api/v1/chats/" + chat["id"] + "/messages?limit=1", + actor=bob, + schema="MessagePage", + ) + ) + event = { + "start": t0, + "end": time.monotonic(), + "kind": operation, + "ok": True, + "instance": response["headers"]["X-Instance-Id"], + } + with lock: + if operation == "write": + confirmed.append(response["json"]) + if operation == "write": + key, text = uuid.uuid4().hex, "" + except (Failure, OSError, http.client.HTTPException): + event = { + "start": t0, + "end": time.monotonic(), + "kind": operation, + "ok": False, + } + with lock: + events.append(event) + time.sleep(0.02) + + with ThreadPoolExecutor(max_workers=2) as pool: + futures = [pool.submit(worker, n) for n in range(2)] + time.sleep(1) + action_started = time.monotonic() + try: + if mode == "stop": + env = {**os.environ, "TARGET_INSTANCE_ID": target} + # Вызываем replicas.py stop-target напрямую через текущий интерпретатор Python + result = subprocess.run( + [sys.executable, str(ROOT / "scripts/replicas.py"), "stop-target"], + cwd=ROOT, + env=env, + text=True, + capture_output=True, + timeout=10, + ) + check(result.returncode == 0, "stop hook failed") + action_detail = result.stdout + else: + action_detail = replace(service) + action_finished = time.monotonic() + after_action = inventory() + if mode == "stop": + stopped = next(row for row in after_action if row["service"] == service) + check( + stopped["state"] == "exited" and stopped["pid"] == 0, + "target process is still running", + ) + deadline = max(action_started + duration, action_finished + 5) + while time.monotonic() < deadline: + time.sleep(0.1) + finally: + halt.set() + for future in futures: + future.result() + finished = time.monotonic() + rows = history(c, bob, chat) + by_id = {row["id"]: row for row in rows} + check( + all(by_id.get(message["id"]) == message for message in confirmed), + "confirmed data was lost", + ) + texts = [row["text"] for row in rows] + check(len(texts) == len(set(texts)), "uncertain retry created duplicate text") + tail = [e for e in events if e["start"] >= action_finished] + check(any(e["ok"] and e["kind"] == "write" for e in tail), "no writes after action") + check(any(e["ok"] and e["kind"] == "read" for e in tail), "no reads after action") + if mode == "stop": + check( + all(e.get("instance") != target for e in tail if e["ok"]), + "stopped instance still responds", + ) + for kind in ("write", "read"): + recovered = min(e["end"] for e in tail if e["ok"] and e["kind"] == kind) + check(recovered - action_started <= 10, kind + " recovery exceeded 10s") + + def metrics(items, elapsed): + good = [e for e in items if e["ok"]] + failed = len(items) - len(good) + latencies = sorted((e["end"] - e["start"]) * 1000 for e in good) + + def percentile(fraction): + return ( + round(latencies[max(0, int(len(latencies) * fraction + 0.999) - 1)], 3) + if latencies + else None + ) + + return { + "elapsed_seconds": round(elapsed, 3), + "attempts": len(items), + "successes": len(good), + "errors": failed, + "error_ratio": round(failed / len(items), 6) if items else None, + "successful_rps": round(len(good) / elapsed, 3), + "successful_latency_ms": { + "p50": percentile(0.5), + "p95": percentile(0.95), + "p99": percentile(0.99), + }, + } + + result = { + "mode": mode, + "service": service, + "target_instance": target, + "observed_with_one_session": seen, + "before": before, + "after_action": after_action, + "action": action_detail, + "action_seconds": round(action_finished - action_started, 3), + "concurrency": 2, + "total": metrics(events, finished - started), + "during_and_after_action": metrics( + [e for e in events if e["start"] >= action_started], + finished - action_started, + ), + "confirmed_responses": len(confirmed), + "history_messages": len(rows), + "duplicates": 0, + "existing_session_works": True, + } + for kind in ("write", "read"): + after = [e for e in tail if e["ok"] and e["kind"] == kind] + result["first_" + kind + "_after_action_seconds"] = round( + min(e["end"] for e in after) - action_started, 3 + ) + result[kind] = metrics( + [e for e in events if e["kind"] == kind], finished - started + ) + return result + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--base-url", default="http://localhost:8080") + parser.add_argument("--output", default="evidence/lab3") + parser.add_argument("--duration", type=float, default=20) + args = parser.parse_args() + check(15 <= args.duration <= 120, "duration must be 15..120 seconds") + out = ROOT / args.output + out.mkdir(parents=True, exist_ok=True) + save_json(out / "result.json", {"status": "RUNNING"}) + try: + os.environ["BASE_URL"] = args.base_url + save_json( + out / "00_environment.json", + { + "at_utc": datetime.now(timezone.utc).isoformat(), + "platform": platform.platform(), + "cpu_count": os.cpu_count(), + "python": platform.python_version(), + "duration": args.duration, + "workload": "2 closed-loop clients, POST+GET; uncertain POST retried with same key/body", + }, + ) + record_command(out, "01_docker", ["docker", "version"]) + record_command(out, "02_compose_config", COMPOSE + ["config", "--quiet"]) + wait_balanced() + save_json(out / "03_processes.json", inventory()) + record_command(out, "03_limits", COMPOSE + ["ps", "-q"]) + ids = compose("ps", "-q", capture=True).splitlines() + record_command( + out, + "03_container_limits", + [ + "docker", + "inspect", + "--format", + "{{.Name}} cpus={{.HostConfig.NanoCpus}} memory={{.HostConfig.Memory}} ports={{json .HostConfig.PortBindings}}", + *ids, + ], + ) + record_command( + out, + "04_smoke", + [sys.executable, "tests/smoke.py", "--base-url", args.base_url], + ) + save_json( + out / "05_concurrent_idempotency.json", concurrent_replay(args.base_url) + ) + try: + for service in SERVICES: + wait_balanced() + report = experiment(args.base_url, service, "stop", args.duration) + save_json(out / ("06_failover_" + service + ".json"), report) + compose("up", "-d", "--no-deps", service) + wait_balanced() + record_command( + out, + "07_public_failover", + [ + sys.executable, + "tests/resilience.py", + "--mode", + "failover", + "--action-script", + "scripts/stop-one.sh", + "--base-url", + args.base_url, + ], + ) + for service in SERVICES: + compose("up", "-d", "--no-deps", service) + wait_balanced() + for service in SERVICES: + save_json( + out / ("08_rolling_" + service + ".json"), + experiment(args.base_url, service, "replace", args.duration), + ) + record_command( + out, + "09_persistence", + [ + sys.executable, + "tests/resilience.py", + "--mode", + "persistence", + "--action-script", + "scripts/restart.sh", + "--base-url", + args.base_url, + ], + ) + wait_balanced() + save_json(out / "10_processes_after.json", inventory()) + record_command( + out, + "11_nginx", + COMPOSE + + ["exec", "-T", "nginx", "nginx", "-T", "-c", "/opt/lb/nginx.conf"], + ) + record_command( + out, + "12_student_tests", + COMPOSE + ["--profile", "test", "run", "--rm", "--build", "tests"], + ) + save_json( + out / "result.json", + {"status": "PASS", "at_utc": datetime.now(timezone.utc).isoformat()}, + ) + except Exception: + save_json( + out / "result.json", + { + "status": "FAIL", + "note": "Inspect completed evidence files; failed runs are not evidence of passing.", + }, + ) + raise + finally: + for service in SERVICES: + compose("up", "-d", "--no-deps", service) + undrain(service) + print("PASS: evidence saved to " + str(out.relative_to(ROOT))) + except BaseException: + save_json( + out / "result.json", + { + "status": "FAIL", + "note": "Run did not finish successfully; inspect completed outputs.", + }, + ) + raise + + +if __name__ == "__main__": + main() diff --git a/scripts/local_evidence.py b/scripts/local_evidence.py new file mode 100644 index 00000000..dadc3702 --- /dev/null +++ b/scripts/local_evidence.py @@ -0,0 +1,47 @@ +#!/usr/bin/env python3 +"""Сохранить только реально выполненные локальные проверки без запуска инфраструктуры.""" + +from datetime import datetime, timezone +import json +from pathlib import Path +import shutil +import subprocess +import sys +import time + +ROOT = Path(__file__).resolve().parents[1] + + +def main(): + out = ROOT / "evidence/local" + out.mkdir(parents=True, exist_ok=True) + checks = [ + ("01_check", [sys.executable, "scripts/check_contract.py"]), + ("02_openapi", [sys.executable, "scripts/validate_openapi.py"]), + ("03_static_lab3", [sys.executable, "scripts/check_lab3.py"]), + ("04_unit", [sys.executable, "-m", "pytest", "-q", "unit-tests", "-p", "no:cacheprovider"]), + ("05_collection", [sys.executable, "-m", "pytest", "--collect-only", "-q", "-p", "no:cacheprovider"]), + ("06_dependencies", [sys.executable, "-m", "pip", "check"]), + ] + manifest = [] + for name, command in checks: + started = time.monotonic() + run = subprocess.run(command, cwd=ROOT, text=True, capture_output=True) + (out / (name + ".txt")).write_text("$ " + " ".join(command) + "\n" + run.stdout + run.stderr + "\nexit_code=" + str(run.returncode) + "\n") + manifest.append({"name": name, "command": command, "exit_code": run.returncode, + "elapsed_seconds": round(time.monotonic() - started, 3)}) + print(name, "PASS" if run.returncode == 0 else "FAIL", flush=True) + (out / "manifest.json").write_text(json.dumps({"at_utc": datetime.now(timezone.utc).isoformat(), + "python": sys.version.split()[0], "checks": manifest}, indent=2) + "\n") + (out / "runtime_status.json").write_text(json.dumps({ + "docker_cli_available": shutil.which("docker") is not None, + "nginx_binary_available": shutil.which("nginx") is not None, + "compose_nginx_postgresql_redis_experiments": "NOT_RUN", + "reason": "This local checker does not run infrastructure; execute make lab3-evidence separately", + "required_command_on_docker_host": "make lab3-evidence", + }, indent=2) + "\n") + return int(any(check["exit_code"] != 0 for check in manifest)) + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/scripts/replicas.py b/scripts/replicas.py new file mode 100644 index 00000000..dc26c14f --- /dev/null +++ b/scripts/replicas.py @@ -0,0 +1,159 @@ +#!/usr/bin/env python3 +"""Управление двумя API без публичных backend-портов и без Docker socket в LB.""" + +import argparse +import json +import os +from pathlib import Path +import subprocess +import time + +ROOT = Path(__file__).resolve().parents[1] +SERVICES = ("api-a", "api-b") +COMPOSE = ["docker", "compose", "--env-file", ".env"] +PROBE = "import json,urllib.request; r=urllib.request.urlopen('http://127.0.0.1:8080/health/ready',timeout=2); print(json.dumps({'status':r.status,'instance_id':r.headers['X-Instance-Id']}))" + + +def compose(*args, capture=False): + result = subprocess.run( + COMPOSE + list(args), + cwd=ROOT, + check=True, + text=True, + stdout=subprocess.PIPE if capture else None, + ) + return result.stdout.strip() if capture else "" + + +def probe(service): + """Получить alias из реального контейнера; credentials не выводятся.""" + return json.loads( + compose("exec", "-T", service, "python", "-c", PROBE, capture=True) + ) + + +def wait_ready(service, timeout=60): + deadline = time.monotonic() + timeout + while time.monotonic() < deadline: + try: + return probe(service) + except (subprocess.CalledProcessError, ValueError): + time.sleep(0.5) + raise RuntimeError(service + " did not become ready") + + +def inventory(): + """ID контейнера, PID host, restart count, alias и состояние без env/secrets.""" + rows = [] + for service in SERVICES: + cid = compose("ps", "-a", "-q", service, capture=True) + if not cid: + rows.append({"service": service, "state": "absent"}) + continue + raw = subprocess.run( + ["docker", "inspect", "--format", "{{json .State}}", cid], + check=True, + text=True, + capture_output=True, + ) + state = json.loads(raw.stdout) + row = { + "service": service, + "container_id": cid, + "pid": state["Pid"], + "state": state["Status"], + "started_at": state["StartedAt"], + } + if state["Running"]: + row.update(probe(service)) + rows.append(row) + return rows + + +def drain(service): + """Исключить backend, дождаться обновления upstream и старых workers.""" + compose("exec", "-T", "nginx", "touch", "/tmp/drain-" + service) + deadline = time.monotonic() + 8 + while time.monotonic() < deadline: + config = compose( + "exec", "-T", "nginx", "cat", "/tmp/backends.conf", capture=True + ) + if service + ":8080" not in config: + time.sleep(5.2) + return + time.sleep(0.2) + raise RuntimeError("LB did not drain " + service) + + +def undrain(service): + compose("exec", "-T", "nginx", "rm", "-f", "/tmp/drain-" + service) + + +def replace(service): + """Заменить одну реплику при заведомо готовой второй.""" + other = next(s for s in SERVICES if s != service) + wait_ready(other) + before = probe(service)["instance_id"] + drain(service) + try: + compose("up", "-d", "--no-deps", "--force-recreate", service) + after = wait_ready(service)["instance_id"] + if before == after: + raise RuntimeError("process alias did not change") + finally: + undrain(service) + wait_balanced() + return {"service": service, "before": before, "after": after} + + +def wait_balanced(timeout=30): + """Убедиться, что бэкенд отвечает через LB.""" + import urllib.request + + deadline = time.monotonic() + timeout + base = os.environ.get("BASE_URL", "http://localhost:8080") + while time.monotonic() < deadline: + try: + req = urllib.request.Request( + base + "/health/ready", headers={"Connection": "close"} + ) + with urllib.request.urlopen(req, timeout=2) as reply: + if reply.status == 200: + return # Проходим дальше, раз LB отвечает + except OSError: + pass + time.sleep(0.1) + raise RuntimeError("LB did not expose ready processes") + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument( + "action", choices=["status", "stop-target", "restore", "rolling"] + ) + args = parser.parse_args() + if args.action == "status": + print(json.dumps(inventory(), indent=2)) + elif args.action == "stop-target": + target = os.environ.get("TARGET_INSTANCE_ID", "") + if not target: + raise RuntimeError("TARGET_INSTANCE_ID is required") + matches = [s for s in SERVICES if probe(s)["instance_id"] == target] + if len(matches) != 1: + raise RuntimeError("target must identify exactly one live process") + service = matches[0] + compose("stop", "--timeout", "6", service) + print(json.dumps({"stopped_service": service, "instance_id": target})) + elif args.action == "restore": + for service in SERVICES: + compose("up", "-d", "--no-deps", service) + undrain(service) + wait_balanced() + print(json.dumps(inventory(), indent=2)) + else: + for service in SERVICES: + print(json.dumps(replace(service))) + + +if __name__ == "__main__": + main() diff --git a/scripts/restart.sh b/scripts/restart.sh new file mode 100644 index 00000000..e0942e66 --- /dev/null +++ b/scripts/restart.sh @@ -0,0 +1,8 @@ +#!/bin/sh +set -eu +cd "$(dirname "$0")/.." +docker compose --env-file .env stop nginx api-a api-b redis db +docker compose --env-file .env up -d --wait db redis +docker compose --env-file .env run --rm --no-deps migrate +docker compose --env-file .env up -d --no-deps --force-recreate api-a api-b +docker compose --env-file .env start nginx diff --git a/scripts/run_and_save.py b/scripts/run_and_save.py new file mode 100644 index 00000000..0acf0ea5 --- /dev/null +++ b/scripts/run_and_save.py @@ -0,0 +1,64 @@ +#!/usr/bin/env python3 +"""Выполнить команду и сохранить её вывод в файл вместе с датой, коммитом и кодом возврата. + + python scripts/run_and_save.py evidence/01.txt -- python scripts/check_contract.py +""" +import os +import subprocess +import sys +import time +from datetime import datetime, timezone +from pathlib import Path + + +def git_commit(root: Path) -> str: + """Короткий hash текущего коммита или пояснение, почему его нет.""" + try: + out = subprocess.run(["git", "rev-parse", "--short", "HEAD"], cwd=str(root), capture_output=True, + text=True, timeout=5) + return out.stdout.strip() if out.returncode == 0 and out.stdout.strip() else "нет (git не используется)" + except Exception: + return "нет (git не найден)" + + +def main() -> int: + """Выполнить команду после --, записать отчёт и вернуть её код возврата.""" + argv = sys.argv[1:] + if len(argv) < 3 or argv[1] != "--": + print(__doc__) + return 2 + target = Path(argv[0]) + command = argv[2:] + root = Path(__file__).resolve().parents[1] + + env = dict(os.environ, PYTHONIOENCODING="utf-8", PYTHONUTF8="1") + started = time.monotonic() + try: + done = subprocess.run(command, capture_output=True, env=env, cwd=str(root)) + output = (done.stdout + done.stderr).decode("utf-8", errors="replace") + code = done.returncode + except FileNotFoundError: + output = "Команда не найдена: %s\n" % command[0] + code = 127 + elapsed = time.monotonic() - started + + text = "\n".join([ + "# Дата (UTC): " + datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M:%S"), + "# Commit: " + git_commit(root), + "# Команда: " + " ".join(command), + "# ---------------------------------------- вывод ----------------------------------------", + output.rstrip(), + "# ---------------------------------------------------------------------------------------", + "# Код возврата: %d" % code, + "# Время выполнения: %.2f с" % elapsed, + "", + ]) + target.parent.mkdir(parents=True, exist_ok=True) + target.write_text(text, encoding="utf-8") + sys.stdout.buffer.write(text.encode("utf-8", errors="replace")) + print("\n[сохранено в %s]" % target) + return code + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/scripts/show_instances.py b/scripts/show_instances.py new file mode 100644 index 00000000..610d89e2 --- /dev/null +++ b/scripts/show_instances.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python3 +"""Один вход и несколько запросов через LB; секреты не выводятся.""" + +import argparse +import json +from pathlib import Path +import sys + +sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "tests")) +from client import Client, check + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--base-url", default="http://localhost:8080") + args = parser.parse_args() + c = Client(args.base_url) + person = c.user("instances") + seen = [] + for _ in range(20): + response = c.call("GET", "/api/v1/users/me", actor=person, schema="User") + check(response["json"] == person["user"], "same token returned another user") + seen.append(response["headers"]["X-Instance-Id"]) + print(json.dumps({"login_count": 1, "user_id": person["id"], "request_instances": seen, + "distinct_instances": len(set(seen))}, indent=2)) + check(len(set(seen)) >= 2, "less than two API processes observed") + + +if __name__ == "__main__": + main() diff --git a/scripts/stop-one.sh b/scripts/stop-one.sh new file mode 100644 index 00000000..679e33ab --- /dev/null +++ b/scripts/stop-one.sh @@ -0,0 +1,4 @@ +#!/bin/sh +set -eu +cd "$(dirname "$0")/.." +exec python3 scripts/replicas.py stop-target diff --git a/student-tests/conftest.py b/student-tests/conftest.py new file mode 100644 index 00000000..5bf863ac --- /dev/null +++ b/student-tests/conftest.py @@ -0,0 +1,273 @@ +"""Общие фикстуры тестов. + +Тесты работают с настоящим PostgreSQL (отдельная БД messenger_test) и, +если задан TEST_CACHE_URL, с Redis. +""" + +import asyncio +import base64 +import os +import uuid +from pathlib import Path +from typing import Optional + +import asyncpg +import pytest +from fastapi.testclient import TestClient + +from app.config import Settings +from app.main import create_app +from app.db import create_engine +from app.migrate import run_migrations + +ROOT = Path(__file__).resolve().parents[1] +TEST_DB_NAME = "messenger_test" + + +def read_dotenv() -> dict: + """Прочитать .env из корня проекта в словарь.""" + values = {} + path = ROOT / ".env" + if path.exists(): + for line in path.read_text(encoding="utf-8").splitlines(): + line = line.strip() + if line and not line.startswith("#") and "=" in line: + key, _, value = line.partition("=") + values[key.strip()] = value.strip() + return values + + +def database_parts() -> dict: + """Параметры подключения к тестовой БД из TEST_DB_* или .env.""" + env = read_dotenv() + return { + "user": os.environ.get("TEST_DB_USER", env.get("POSTGRES_USER", "messenger")), + "password": os.environ.get( + "TEST_DB_PASSWORD", env.get("POSTGRES_PASSWORD", "") + ), + "host": os.environ.get("TEST_DB_HOST", "127.0.0.1"), + "port": int(os.environ.get("TEST_DB_PORT", "5433")), + } + + +def sqlalchemy_url(database: str = TEST_DB_NAME) -> str: + """URL тестовой БД для SQLAlchemy.""" + p = database_parts() + return "postgresql+asyncpg://%s:%s@%s:%d/%s" % ( + p["user"], + p["password"], + p["host"], + p["port"], + database, + ) + + +@pytest.fixture(scope="session", autouse=True) +def test_database(): + """Проверить доступность PostgreSQL и создать тестовую БД.""" + p = database_parts() + + async def prepare(): + conn = await asyncpg.connect( + user=p["user"], + password=p["password"], + host=p["host"], + port=p["port"], + database="postgres", + timeout=5, + ) + try: + exists = await conn.fetchval( + "SELECT 1 FROM pg_database WHERE datname = $1", TEST_DB_NAME + ) + if not exists: + await conn.execute( + "CREATE DATABASE \"%s\" TEMPLATE template0 ENCODING 'UTF8'" + % TEST_DB_NAME + ) + finally: + await conn.close() + + try: + asyncio.run(prepare()) + + async def migrate(): + engine = create_engine(make_settings()) + try: + await run_migrations(engine) + finally: + await engine.dispose() + + asyncio.run(migrate()) + except Exception as error: + pytest.exit( + "PostgreSQL недоступен на %s:%s (%s). Запустите БД: " + "docker compose --env-file .env up -d db" + % (p["host"], p["port"], type(error).__name__), + returncode=2, + ) + + +CACHE_URL = os.environ.get("TEST_CACHE_URL", "") + + +def make_settings(**overrides) -> Settings: + """Настройки приложения для тестов с переопределением полей.""" + base = Settings.from_env( + { + "DATABASE_URL": sqlalchemy_url(), + "DB_STARTUP_WAIT_SECONDS": "5", + "LOG_LEVEL": "INFO", + "CACHE_URL": CACHE_URL, + } + ) + return Settings(**{**base.__dict__, **overrides}) + + +def run_sql(query: str, *args): + """Выполнить SQL в тестовой БД напрямую и вернуть строки.""" + p = database_parts() + + async def go(): + conn = await asyncpg.connect( + user=p["user"], + password=p["password"], + host=p["host"], + port=p["port"], + database=TEST_DB_NAME, + timeout=5, + ) + try: + return await conn.fetch(query, *args) + finally: + await conn.close() + + return asyncio.run(go()) + + +@pytest.fixture(scope="session") +def client(test_database): + """Клиент приложения, общий для всего прогона.""" + with TestClient( + create_app(make_settings()), raise_server_exceptions=False + ) as test_client: + yield test_client + + +class Person: + """Зарегистрированный пользователь с токеном и заголовками.""" + + def __init__(self, data: dict, password: str, token: str): + self.data = data + self.id = data["id"] + self.username = data["username"] + self.password = password + self.token = token + self._headers = {"Authorization": "Bearer " + token} + + @property + def headers(self): + """Новый ключ на новую операцию; тесты повторов задают ключ явно.""" + return {**self._headers, "Idempotency-Key": uuid.uuid4().hex} + + +def basic_header(username: str, password: str) -> dict: + """Заголовок Authorization: Basic.""" + raw = (username + ":" + password).encode("utf-8") + return {"Authorization": "Basic " + base64.b64encode(raw).decode()} + + +def register_and_login(client: TestClient, name: str = "") -> Person: + """Зарегистрировать уникального пользователя и войти.""" + username = ("u_" + name + "_" + uuid.uuid4().hex[:10]).lower() + password = "T3st-pass-" + uuid.uuid4().hex + created = client.post( + "/api/v1/users", + json={ + "username": username, + "display_name": "Тест " + name, + "password": password, + }, + ) + assert created.status_code == 201, created.text + session = client.post( + "/api/v1/auth/sessions", headers=basic_header(username, password) + ) + assert session.status_code == 201, session.text + return Person(created.json(), password, session.json()["token"]) + + +@pytest.fixture +def make_user(client): + """Фабрика пользователей.""" + return lambda name="": register_and_login(client, name) + + +@pytest.fixture +def make_chat(client): + """Фабрика чатов.""" + + def factory(owner: Person, *others: Person, title: str = "Чат") -> dict: + members = [owner.id] + [p.id for p in others] + reply = client.post( + "/api/v1/chats", + json={"title": title, "member_ids": members}, + headers=owner.headers, + ) + assert reply.status_code == 201, reply.text + return reply.json() + + return factory + + +@pytest.fixture +def alice(make_user): + """Пользователь alice.""" + return make_user("alice") + + +@pytest.fixture +def bob(make_user): + """Пользователь bob.""" + return make_user("bob") + + +@pytest.fixture +def eve(make_user): + """Пользователь eve, не состоящий в чате.""" + return make_user("eve") + + +@pytest.fixture +def chat(make_chat, alice, bob): + """Чат alice и bob.""" + return make_chat(alice, bob) + + +@pytest.fixture +def log_lines(): + """Собрать сообщения лога приложения.""" + import logging + + lines = [] + + class Collector(logging.Handler): + def emit(self, record): + lines.append(record.getMessage()) + + handler = Collector() + logger = logging.getLogger("messenger") + logger.addHandler(handler) + yield lines + logger.removeHandler(handler) + + +def assert_error(reply, status: int, code: str): + """Проверить статус, код и формат тела ошибки.""" + assert reply.status_code == status, reply.text + body = reply.json() + assert set(body) == {"error"} + assert set(body["error"]) == {"code", "message", "request_id"} + assert body["error"]["code"] == code + assert body["error"]["message"] + assert body["error"]["request_id"] == reply.headers["X-Request-Id"] diff --git a/student-tests/test_auth.py b/student-tests/test_auth.py new file mode 100644 index 00000000..de3d0211 --- /dev/null +++ b/student-tests/test_auth.py @@ -0,0 +1,193 @@ +"""Регистрация, вход по Basic, доступ по Bearer и выход.""" + +import base64 +import uuid + +import pytest + +from conftest import assert_error, basic_header, register_and_login + +BAD_BASIC = [ + "Basic", + "Basic !!!not-base64!!!", + "Basic " + base64.b64encode(b"nocolon").decode(), + "Basic " + base64.b64encode("имя:password-123456".encode()).decode(), + "Basic " + base64.b64encode(b"user:\xff\xfe-bad-utf8").decode(), + "Bearer abc", + "", +] + + +def test_register_returns_user_without_secrets(client): + reply = client.post( + "/api/v1/users", + json={ + "username": "reg_" + uuid.uuid4().hex[:8], + "display_name": "Имя", + "password": "long-enough-pass", + }, + ) + assert reply.status_code == 201 + assert set(reply.json()) == {"id", "username", "display_name", "created_at"} + assert "password" not in reply.text and "hash" not in reply.text + + +@pytest.mark.parametrize( + "password", [None, "", "short", "a" * 11, "a" * 129, 123456789012, "пароль"] +) +def test_register_password_rules(client, password): + body = {"username": "pw_" + uuid.uuid4().hex[:8], "display_name": "x"} + if password is not None: + body["password"] = password + assert_error(client.post("/api/v1/users", json=body), 400, "invalid_request") + + +def test_register_password_border_lengths(client): + for password in ["a" * 12, "я" * 128, "p:a:s:s:w:o:r:d:1:2:3"]: + body = { + "username": "pb_" + uuid.uuid4().hex[:8], + "display_name": "x", + "password": password, + } + assert client.post("/api/v1/users", json=body).status_code == 201 + + +def test_login_success_shape(client, alice): + reply = client.post( + "/api/v1/auth/sessions", headers=basic_header(alice.username, alice.password) + ) + assert reply.status_code == 201 + data = reply.json() + assert set(data) == {"token", "token_type", "expires_at", "user"} + assert data["token_type"] == "Bearer" and len(data["token"]) >= 32 + assert data["user"] == alice.data + assert data["expires_at"].endswith("Z") + + +def test_each_login_gives_new_token(client, alice): + second = client.post( + "/api/v1/auth/sessions", headers=basic_header(alice.username, alice.password) + ).json()["token"] + assert second != alice.token + + +def test_wrong_password_and_unknown_user_look_the_same(client, alice): + wrong = client.post( + "/api/v1/auth/sessions", + headers=basic_header(alice.username, "wrong-password-123"), + ) + unknown = client.post( + "/api/v1/auth/sessions", + headers=basic_header("no_such_user_x", "wrong-password-123"), + ) + assert_error(wrong, 401, "unauthorized") + assert_error(unknown, 401, "unauthorized") + assert wrong.headers["WWW-Authenticate"] == unknown.headers["WWW-Authenticate"] + assert wrong.headers["WWW-Authenticate"].startswith('Basic realm="messenger"') + assert wrong.json()["error"]["message"] == unknown.json()["error"]["message"] + + +@pytest.mark.parametrize("header", BAD_BASIC) +def test_malformed_basic_is_401(client, header): + reply = client.post("/api/v1/auth/sessions", headers={"Authorization": header}) + assert_error(reply, 401, "unauthorized") + assert reply.headers["WWW-Authenticate"].startswith("Basic") + + +def test_login_without_header_is_401(client): + assert_error(client.post("/api/v1/auth/sessions"), 401, "unauthorized") + + +def test_password_with_colon_and_unicode_works(client): + for password in ["pass:with:colons:123", "пароль-с-юникодом-😀-123"]: + username = "uc_" + uuid.uuid4().hex[:8] + client.post( + "/api/v1/users", + json={"username": username, "display_name": "x", "password": password}, + ) + reply = client.post( + "/api/v1/auth/sessions", headers=basic_header(username, password) + ) + assert reply.status_code == 201, password + + +def test_bearer_token_gives_access(client, alice): + reply = client.get("/api/v1/users/me", headers=alice.headers) + assert reply.status_code == 200 and reply.json() == alice.data + + +def test_business_endpoint_rejects_basic(client, alice): + reply = client.get( + "/api/v1/users/me", headers=basic_header(alice.username, alice.password) + ) + assert_error(reply, 401, "unauthorized") + assert reply.headers["WWW-Authenticate"] == 'Bearer realm="messenger"' + + +@pytest.mark.parametrize( + "header", + ["", "Bearer", "Bearer short", "Bearer " + "x" * 43, "Token abc", "bearer"], +) +def test_bad_or_unknown_bearer_is_401(client, header): + headers = {"Authorization": header} if header else {} + reply = client.get("/api/v1/users/me", headers=headers) + assert_error(reply, 401, "unauthorized") + assert reply.headers["WWW-Authenticate"] == 'Bearer realm="messenger"' + + +def test_x_user_id_alone_gives_no_access(client, alice): + assert_error( + client.get("/api/v1/users/me", headers={"X-User-Id": alice.id}), + 401, + "unauthorized", + ) + + +def test_x_user_id_does_not_change_owner_of_session(client, alice, bob): + reply = client.get( + "/api/v1/users/me", headers={**alice.headers, "X-User-Id": bob.id} + ) + assert reply.json()["id"] == alice.id + + +def test_logout_revokes_token_and_repeat_is_401(client, alice): + first = client.delete("/api/v1/auth/sessions/current", headers=alice.headers) + assert first.status_code == 204 and first.content == b"" + assert_error( + client.get("/api/v1/users/me", headers=alice.headers), 401, "unauthorized" + ) + assert_error( + client.delete("/api/v1/auth/sessions/current", headers=alice.headers), + 401, + "unauthorized", + ) + + +def test_logout_without_token_is_401(client): + assert_error(client.delete("/api/v1/auth/sessions/current"), 401, "unauthorized") + + +def test_logout_revokes_only_current_session(client, alice): + second = client.post( + "/api/v1/auth/sessions", headers=basic_header(alice.username, alice.password) + ).json()["token"] + client.delete("/api/v1/auth/sessions/current", headers=alice.headers) + assert ( + client.get( + "/api/v1/users/me", headers={"Authorization": "Bearer " + second} + ).status_code + == 200 + ) + + +def test_session_lives_longer_than_300_seconds(client, alice): + """Сессия по умолчанию живёт не меньше 300 секунд.""" + from datetime import datetime, timezone + + data = client.post( + "/api/v1/auth/sessions", headers=basic_header(alice.username, alice.password) + ).json() + left = datetime.fromisoformat(data["expires_at"][:-1] + "+00:00") - datetime.now( + timezone.utc + ) + assert left.total_seconds() > 300 diff --git a/student-tests/test_cache.py b/student-tests/test_cache.py new file mode 100644 index 00000000..189fb12d --- /dev/null +++ b/student-tests/test_cache.py @@ -0,0 +1,258 @@ +"""Кэш сессий в Redis: попадания, TTL, отзыв и недоступность Redis.""" + +import hashlib +import json +import time +import uuid +from dataclasses import replace + +import pytest +import redis +from fastapi.testclient import TestClient +from redis.asyncio import Redis as AsyncRedis + +from app.main import create_app +from conftest import CACHE_URL, assert_error, make_settings, register_and_login, run_sql + +pytestmark = pytest.mark.skipif( + not CACHE_URL, reason="TEST_CACHE_URL не задан (запускайте тесты в контейнере)" +) + +DEAD_REDIS = "redis://127.0.0.1:1/0" + + +def key_for(person) -> str: + """Ключ Redis для токена пользователя.""" + return "session:" + hashlib.sha256(person.token.encode()).hexdigest() + + +@pytest.fixture +def store(): + """Синхронный клиент тестовой базы Redis.""" + client = redis.Redis.from_url(CACHE_URL, decode_responses=True) + client.flushdb() + yield client + client.close() + + +@pytest.fixture +def cached(store): + """Клиент приложения с включённым кэшем.""" + with TestClient(create_app(make_settings()), raise_server_exceptions=False) as app_client: + yield app_client + + +def test_user_profile_is_served_from_cache_with_bounded_ttl(cached, store): + person = register_and_login(cached, "cache") + stats = cached.app.state.cache.stats + assert cached.get("/api/v1/users/me", headers=person.headers).status_code == 200 + assert stats["misses"] == 1 and stats["hits"] == 0 + + ttl_ms = store.pttl(key_for(person)) + assert 0 < ttl_ms <= 60_000 + value = json.loads(store.get(key_for(person))) + assert set(value) == {"id", "username", "display_name", "created_at"} + assert (value["id"], value["username"]) == (person.id, person.username) + + for _ in range(5): + reply = cached.get("/api/v1/users/me", headers=person.headers) + assert reply.status_code == 200 and reply.json() == person.data + assert stats["hits"] == 5 + + +def test_cache_never_stores_raw_tokens_or_passwords(cached, store): + person = register_and_login(cached, "secret") + cached.get("/api/v1/users/me", headers=person.headers) + dump = " ".join(k + "=" + (store.get(k) or "") for k in store.scan_iter()) + assert person.token not in dump + assert person.password not in dump + assert "scrypt" not in dump + + +def test_logout_revokes_cached_session(cached, store): + person = register_and_login(cached, "logout") + assert cached.get("/api/v1/users/me", headers=person.headers).status_code == 200 + assert store.get(key_for(person)) is not None + + assert cached.delete("/api/v1/auth/sessions/current", headers=person.headers).status_code == 204 + assert store.get(key_for(person)) == "revoked" + assert_error(cached.get("/api/v1/users/me", headers=person.headers), 401, "unauthorized") + assert_error( + cached.delete("/api/v1/auth/sessions/current", headers=person.headers), 401, "unauthorized" + ) + + +def test_late_cache_fill_cannot_resurrect_revoked_session(cached, store): + """Запоздалая запись в кэш не отменяет отзыв сессии.""" + person = register_and_login(cached, "race") + cached.delete("/api/v1/auth/sessions/current", headers=person.headers) + token_hash = hashlib.sha256(person.token.encode()).hexdigest() + stale = {k: person.data[k] for k in ("id", "username", "display_name", "created_at")} + cached.portal.call(cached.app.state.cache.put, token_hash, stale, 3600.0) + assert store.get(key_for(person)) == "revoked" + assert_error(cached.get("/api/v1/users/me", headers=person.headers), 401, "unauthorized") + + +def test_flushed_cache_loses_nothing(cached, store): + """После очистки Redis сессия читается из PostgreSQL.""" + person = register_and_login(cached, "flush") + cached.get("/api/v1/users/me", headers=person.headers) + store.flushdb() + assert cached.get("/api/v1/users/me", headers=person.headers).status_code == 200 + assert store.exists(key_for(person)) + + +def test_cache_does_not_extend_short_session(): + """TTL записи в кэше не больше остатка сессии.""" + settings = make_settings(session_ttl_seconds=1) + with TestClient(create_app(settings), raise_server_exceptions=False) as short: + person = register_and_login(short, "short") + assert short.get("/api/v1/users/me", headers=person.headers).status_code == 200 + time.sleep(2.2) + reply = short.get("/api/v1/users/me", headers=person.headers) + assert_error(reply, 401, "unauthorized") + assert reply.headers["WWW-Authenticate"] == 'Bearer realm="messenger"' + + +def test_staleness_after_out_of_band_delete_is_bounded_by_cache_ttl(store): + """Удаление сессии мимо API видно не позже CACHE_TTL_SECONDS.""" + settings = make_settings(cache_ttl_seconds=1) + with TestClient(create_app(settings), raise_server_exceptions=False) as app_client: + person = register_and_login(app_client, "bounded") + assert app_client.get("/api/v1/users/me", headers=person.headers).status_code == 200 + run_sql("DELETE FROM sessions WHERE token_hash = $1", + hashlib.sha256(person.token.encode()).hexdigest()) + time.sleep(1.3) + assert_error(app_client.get("/api/v1/users/me", headers=person.headers), 401, "unauthorized") + + +def test_redis_down_requests_bypass_cache_and_stay_fast(): + settings = make_settings(cache_url=DEAD_REDIS) + with TestClient(create_app(settings), raise_server_exceptions=False) as app_client: + assert app_client.get("/health/ready").status_code == 200 + alice = register_and_login(app_client, "nocache_a") + bob = register_and_login(app_client, "nocache_b") + started = time.monotonic() + assert app_client.get("/api/v1/users/me", headers=alice.headers).json() == alice.data + chat = app_client.post( + "/api/v1/chats", json={"title": "без кэша", "member_ids": [alice.id, bob.id]}, + headers=alice.headers, + ) + assert chat.status_code == 201 + path = "/api/v1/chats/%s/messages" % chat.json()["id"] + assert app_client.post(path, json={"text": "привет"}, headers=alice.headers).status_code == 201 + assert len(app_client.get(path, headers=bob.headers).json()["items"]) == 1 + assert time.monotonic() - started < 3 + assert app_client.app.state.cache.stats["errors"] >= 1 + + assert app_client.delete("/api/v1/auth/sessions/current", headers=alice.headers).status_code == 204 + assert_error(app_client.get("/api/v1/users/me", headers=alice.headers), 401, "unauthorized") + + +def test_redis_down_never_trusts_anonymous_or_unknown_tokens(): + settings = make_settings(cache_url=DEAD_REDIS) + with TestClient(create_app(settings), raise_server_exceptions=False) as app_client: + person = register_and_login(app_client, "anon") + for headers in ( + {}, + {"X-User-Id": person.id}, + {"Authorization": "Bearer " + "B" * 43}, + {"Authorization": "Basic " + "eDp5"}, + ): + reply = app_client.get("/api/v1/users/me", headers=headers) + assert_error(reply, 401, "unauthorized") + + +def test_revocation_lost_while_redis_down_is_flushed_on_recovery(cached, store): + """Отзыв, потерянный при сбое Redis, приводит к очистке кэша.""" + cache = cached.app.state.cache + person = register_and_login(cached, "lost") + assert cached.get("/api/v1/users/me", headers=person.headers).status_code == 200 + assert store.exists(key_for(person)) + + working = cache.client + cache.client = AsyncRedis.from_url(DEAD_REDIS, socket_connect_timeout=0.2, socket_timeout=0.2) + cache.retry_seconds = 0 + assert cached.delete("/api/v1/auth/sessions/current", headers=person.headers).status_code == 204 + assert cache.dirty is True + assert store.exists(key_for(person)), "старая запись осталась в Redis -- ровно опасный случай" + + cache.client = working + assert_error(cached.get("/api/v1/users/me", headers=person.headers), 401, "unauthorized") + assert cache.dirty is False + assert not store.exists(key_for(person)) + + +def test_without_cache_url_cache_is_off(): + settings = replace(make_settings(), cache_url="") + with TestClient(create_app(settings), raise_server_exceptions=False) as app_client: + assert app_client.app.state.cache.enabled is False + person = register_and_login(app_client, "off") + assert app_client.get("/api/v1/users/me", headers=person.headers).status_code == 200 + + +@pytest.mark.parametrize("redis_down_at_startup", [False, True]) +def test_lost_revocation_survives_api_restart(store, redis_down_at_startup): + """Старый ключ Redis не даёт читать и писать после потери флага dirty.""" + settings = make_settings(cache_retry_seconds=0) + with TestClient(create_app(settings), raise_server_exceptions=False) as first: + person = register_and_login(first, "restart") + chat_reply = first.post( + "/api/v1/chats", json={"title": "Отзыв", "member_ids": [person.id]}, + headers=person.headers, + ) + assert chat_reply.status_code == 201 + chat_id = chat_reply.json()["id"] + cache = first.app.state.cache + stale = store.get(key_for(person)) + assert stale is not None and stale != "revoked" + working = cache.client + failed = AsyncRedis.from_url(DEAD_REDIS, socket_connect_timeout=0.2, socket_timeout=0.2) + cache.client = failed + try: + assert first.delete( + "/api/v1/auth/sessions/current", headers=person.headers + ).status_code == 204 + assert cache.dirty is True + assert store.get(key_for(person)) == stale + assert not run_sql( + "SELECT 1 FROM sessions WHERE token_hash = $1", + hashlib.sha256(person.token.encode()).hexdigest(), + ) + finally: + first.portal.call(failed.aclose) + cache.client = working + + restarted_settings = replace(settings, cache_url=DEAD_REDIS) if redis_down_at_startup else settings + with TestClient(create_app(restarted_settings), raise_server_exceptions=False) as restarted: + cache = restarted.app.state.cache + assert cache.dirty is False + if redis_down_at_startup: + assert_error(restarted.get("/api/v1/users/me", headers=person.headers), 401, "unauthorized") + restarted.portal.call(cache.client.aclose) + cache.client = AsyncRedis.from_url(CACHE_URL, decode_responses=True) + assert store.get(key_for(person)) == stale + assert store.pttl(key_for(person)) > 0 + assert_error(restarted.get("/api/v1/users/me", headers=person.headers), 401, "unauthorized") + assert_error( + restarted.post( + f"/api/v1/chats/{chat_id}/messages", json={"text": "Не записывать"}, + headers=person.headers, + ), + 401, "unauthorized", + ) + assert_error( + restarted.delete("/api/v1/auth/sessions/current", headers=person.headers), + 401, "unauthorized", + ) + assert not run_sql("SELECT 1 FROM messages WHERE chat_id = $1", uuid.UUID(chat_id)) + assert store.get(key_for(person)) == stale + + +def test_cached_session_deleted_in_database_is_rejected_immediately(cached, store): + person = register_and_login(cached, "deleted") + assert cached.get("/api/v1/users/me", headers=person.headers).status_code == 200 + run_sql("DELETE FROM sessions WHERE token_hash = $1", + hashlib.sha256(person.token.encode()).hexdigest()) + assert store.pttl(key_for(person)) > 0 + assert_error(cached.get("/api/v1/users/me", headers=person.headers), 401, "unauthorized") diff --git a/student-tests/test_concurrency.py b/student-tests/test_concurrency.py new file mode 100644 index 00000000..bc7e5647 --- /dev/null +++ b/student-tests/test_concurrency.py @@ -0,0 +1,61 @@ +"""Параллельная запись: сообщения не теряются и не дублируются.""" + +import threading +from concurrent.futures import ThreadPoolExecutor + +CLIENTS = 10 +PER_CLIENT = 10 + + +def test_100_messages_from_10_clients_in_parallel(client, make_user, make_chat): + people = [make_user("c%d" % n) for n in range(CLIENTS)] + chat = make_chat(people[0], *people[1:]) + url = "/api/v1/chats/" + chat["id"] + "/messages" + start_together = threading.Barrier(CLIENTS) + + def worker(index): + start_together.wait() + return [ + client.post( + url, + json={"text": "c%d-m%d" % (index, n)}, + headers=people[index].headers, + ).status_code + for n in range(PER_CLIENT) + ] + + with ThreadPoolExecutor(max_workers=CLIENTS) as pool: + results = list(pool.map(worker, range(CLIENTS))) + assert all(code == 201 for codes in results for code in codes) + + page = client.get(url + "?limit=100", headers=people[0].headers).json() + items = page["items"] + assert len(items) == 100 and page["next_cursor"] is None + assert len({m["id"] for m in items}) == 100 + assert {m["text"] for m in items} == { + "c%d-m%d" % (c, n) for c in range(CLIENTS) for n in range(PER_CLIENT) + } + stamps = [m["created_at"] for m in items] + assert stamps == sorted(stamps) + for index in range(CLIENTS): + own = [m["text"] for m in items if m["sender_id"] == people[index].id] + assert own == ["c%d-m%d" % (index, n) for n in range(PER_CLIENT)] + + +def test_parallel_chat_creation_and_reading(client, make_user): + owner, friend = make_user("owner"), make_user("friend") + + def create(number): + made = client.post( + "/api/v1/chats", + json={"title": "n%d" % number, "member_ids": [owner.id, friend.id]}, + headers=owner.headers, + ) + listing = client.get("/api/v1/chats?limit=100", headers=friend.headers) + return made.status_code, listing.status_code + + with ThreadPoolExecutor(max_workers=10) as pool: + results = list(pool.map(create, range(30))) + assert all(r == (201, 200) for r in results) + final = client.get("/api/v1/chats?limit=100", headers=friend.headers).json() + assert len(final["items"]) == 30 and len({c["id"] for c in final["items"]}) == 30 diff --git a/student-tests/test_contract_basics.py b/student-tests/test_contract_basics.py new file mode 100644 index 00000000..d1a7b344 --- /dev/null +++ b/student-tests/test_contract_basics.py @@ -0,0 +1,95 @@ +"""Общие свойства API: заголовки, формат ошибок, health и логи.""" + +import re + +from conftest import assert_error + + +def test_health_endpoints(client): + assert client.get("/health/live").json() == {"status": "ok"} + assert client.get("/health/ready").json() == {"status": "ok"} + + +def test_every_response_has_unique_request_id(client, alice): + seen = set() + for reply in [ + client.get("/health/live"), + client.get("/health/ready"), + client.get("/api/v1/users/me", headers=alice.headers), + client.get("/api/v1/users/me"), + client.get("/no/such/page"), + client.delete("/api/v1/chats"), + ]: + assert reply.headers.get("X-Request-Id") + seen.add(reply.headers["X-Request-Id"]) + assert len(seen) == 6 + + +def test_unknown_path_is_404_in_common_format(client): + assert_error(client.get("/api/v1/nothing"), 404, "not_found") + + +def test_trailing_slash_is_not_redirected(client, alice): + assert_error( + client.get("/api/v1/chats/", headers=alice.headers, follow_redirects=False), + 404, + "not_found", + ) + + +def test_wrong_method_is_405_in_common_format(client): + assert_error(client.delete("/api/v1/users"), 405, "invalid_request") + + +def test_two_users_one_chat_two_messages(client, alice, bob, eve, chat): + """Двое переписываются, посторонний получает 404.""" + url = "/api/v1/chats/" + chat["id"] + "/messages" + assert ( + client.post( + url, json={"text": "Привет, Боб!"}, headers=alice.headers + ).status_code + == 201 + ) + assert ( + client.post( + url, json={"text": "Привет, Алиса!"}, headers=bob.headers + ).status_code + == 201 + ) + history = client.get(url, headers=alice.headers).json()["items"] + assert [m["text"] for m in history] == ["Привет, Боб!", "Привет, Алиса!"] + assert [m["sender_id"] for m in history] == [alice.id, bob.id] + assert client.get(url, headers=eve.headers).status_code == 404 + + +def test_chat_response_shape(client, alice, bob, chat): + assert set(chat) == {"id", "title", "member_ids", "created_at"} + assert set(chat["member_ids"]) == {alice.id, bob.id} + assert re.fullmatch(r"\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d\.\d{6}Z", chat["created_at"]) + assert client.get("/api/v1/chats/" + chat["id"], headers=bob.headers).json() == chat + + +def test_unexpected_exception_gives_500_without_details(client, log_lines): + async def boom(): + raise RuntimeError("ТАЙНЫЙ-ТЕКСТ-ИЗ-ОШИБКИ") + + client.app.add_api_route("/boom", boom) + reply = client.get("/boom") + assert_error(reply, 500, "internal_error") + assert "ТАЙНЫЙ" not in reply.text + assert not any("ТАЙНЫЙ" in line for line in log_lines) + assert any("unhandled_error" in line for line in log_lines) + + +def test_logs_have_requests_but_no_secrets(client, alice, chat, log_lines): + """В логе есть запросы, но нет текста сообщений, паролей и токенов.""" + secret = "ОЧЕНЬ-СЕКРЕТНОЕ-СООБЩЕНИЕ-12345" + url = "/api/v1/chats/" + chat["id"] + "/messages" + client.post(url, json={"text": secret}, headers=alice.headers) + client.get(url, headers=alice.headers) + joined = "\n".join(log_lines) + assert ( + "method=POST" in joined and "status=201" in joined and "request_id=" in joined + ) + for hidden in (secret, alice.token, alice.password): + assert hidden not in joined diff --git a/student-tests/test_database.py b/student-tests/test_database.py new file mode 100644 index 00000000..09fb2e64 --- /dev/null +++ b/student-tests/test_database.py @@ -0,0 +1,173 @@ +"""Содержимое PostgreSQL: хеши, ограничения, миграции и атомарность.""" + +import asyncio +import uuid + +import asyncpg +import pytest + +from app.db import create_engine +from app.migrate import run_migrations +from conftest import make_settings, run_sql + + +def test_password_is_stored_as_scrypt_hash_not_plaintext(client, make_user): + one, two = make_user("hash1"), make_user("hash2") + rows = run_sql( + "SELECT username, password_hash FROM users WHERE username = ANY($1)", + [one.username, two.username], + ) + hashes = {row["username"]: row["password_hash"] for row in rows} + assert hashes[one.username].startswith("scrypt$") + assert one.password not in hashes[one.username] + assert hashes[one.username] != hashes[two.username] + + +def test_same_password_gives_different_hashes(client): + password = "same-password-for-both" + for _ in range(2): + client.post( + "/api/v1/users", + json={ + "username": "sp_" + uuid.uuid4().hex[:10], + "display_name": "x", + "password": password, + }, + ) + rows = run_sql("SELECT password_hash FROM users ORDER BY created_at DESC LIMIT 2") + assert rows[0]["password_hash"] != rows[1]["password_hash"] + + +def test_session_table_keeps_token_hash_not_token(client, alice): + rows = run_sql( + "SELECT token_hash FROM sessions WHERE user_id = $1", uuid.UUID(alice.id) + ) + assert len(rows) == 1 + assert len(rows[0]["token_hash"]) == 64 and rows[0]["token_hash"] != alice.token + assert alice.token not in rows[0]["token_hash"] + + +def test_message_is_really_in_the_database(client, alice, chat): + client.post( + "/api/v1/chats/" + chat["id"] + "/messages", + json={"text": "в базе"}, + headers=alice.headers, + ) + rows = run_sql( + "SELECT text, sender_id FROM messages WHERE chat_id = $1", uuid.UUID(chat["id"]) + ) + assert [(r["text"], str(r["sender_id"])) for r in rows] == [("в базе", alice.id)] + + +def test_migrations_can_run_again_without_duplicating_anything(client, alice): + users_before = run_sql("SELECT count(*) AS n FROM users")[0]["n"] + versions_before = [ + r["version"] + for r in run_sql("SELECT version FROM schema_migrations ORDER BY version") + ] + + async def twice(): + engine = create_engine(make_settings()) + try: + return [await run_migrations(engine), await run_migrations(engine)] + finally: + await engine.dispose() + + first, second = asyncio.run(twice()) + assert first == [] and second == [] + assert run_sql("SELECT count(*) AS n FROM users")[0]["n"] == users_before + assert [ + r["version"] + for r in run_sql("SELECT version FROM schema_migrations ORDER BY version") + ] == versions_before + assert versions_before == ["0001_init.sql", "0002_message_requests.sql"] + + +def test_expected_tables_and_indexes_exist(client): + tables = { + r["tablename"] + for r in run_sql("SELECT tablename FROM pg_tables WHERE schemaname = 'public'") + } + assert { + "users", + "chats", + "chat_members", + "messages", + "sessions", + "schema_migrations", + "message_requests", + } <= tables + indexes = { + r["indexname"] + for r in run_sql("SELECT indexname FROM pg_indexes WHERE schemaname = 'public'") + } + assert "messages_chat_created_idx" in indexes + + +def count_constraints(table: str, kind: str) -> int: + """Число ограничений заданного типа у таблицы.""" + rows = run_sql( + "SELECT count(*) AS n FROM pg_constraint WHERE conrelid = $1::regclass AND contype::text = $2", + table, + kind, + ) + return rows[0]["n"] + + +def test_primary_and_foreign_keys_are_declared(client): + for table in ("users", "chats", "chat_members", "messages", "sessions"): + assert count_constraints(table, "p") == 1, table + assert count_constraints("users", "u") == 1 + assert count_constraints("chat_members", "f") == 2 + assert count_constraints("messages", "f") == 2 + assert count_constraints("sessions", "f") == 1 + + +def test_database_itself_rejects_bad_rows(client, alice, chat): + chat_id, user_id = uuid.UUID(chat["id"]), uuid.UUID(alice.id) + with pytest.raises(asyncpg.ForeignKeyViolationError): + run_sql( + "INSERT INTO messages (id, chat_id, sender_id, text) VALUES ($1, $2, $3, 'x')", + uuid.uuid4(), + uuid.uuid4(), + user_id, + ) + with pytest.raises(asyncpg.CheckViolationError): + run_sql( + "INSERT INTO messages (id, chat_id, sender_id, text) VALUES ($1, $2, $3, '')", + uuid.uuid4(), + chat_id, + user_id, + ) + with pytest.raises(asyncpg.UniqueViolationError): + run_sql( + "INSERT INTO chat_members (chat_id, user_id) VALUES ($1, $2)", + chat_id, + user_id, + ) + with pytest.raises(asyncpg.CheckViolationError): + run_sql( + "INSERT INTO users (id, username, display_name, password_hash) VALUES ($1, 'BadName', 'x', 'h')", + uuid.uuid4(), + ) + + +def test_chat_creation_is_atomic(client, alice): + """Неизвестный участник: ни чата, ни участников в БД.""" + title = "atomic-" + uuid.uuid4().hex + reply = client.post( + "/api/v1/chats", + json={"title": title, "member_ids": [alice.id, str(uuid.uuid4())]}, + headers=alice.headers, + ) + assert reply.status_code == 400 + assert ( + run_sql("SELECT count(*) AS n FROM chats WHERE title = $1", title)[0]["n"] == 0 + ) + + +def test_successful_chat_has_all_members_in_database(client, alice, bob, chat): + rows = run_sql( + "SELECT user_id FROM chat_members WHERE chat_id = $1", uuid.UUID(chat["id"]) + ) + assert {str(r["user_id"]) for r in rows} == {alice.id, bob.id} diff --git a/student-tests/test_db_failure.py b/student-tests/test_db_failure.py new file mode 100644 index 00000000..90981d4a --- /dev/null +++ b/student-tests/test_db_failure.py @@ -0,0 +1,78 @@ +"""Недоступная БД: ответ 503 и отказ анонимным запросам.""" + +from dataclasses import replace + +import pytest +from fastapi.testclient import TestClient + +from app.db import create_engine, create_sessionmaker +from app.main import create_app +from conftest import CACHE_URL, assert_error, make_settings, register_and_login + + +def test_database_down_gives_503_and_never_trusts_anyone(): + settings = make_settings(cache_url="") + with TestClient(create_app(settings), raise_server_exceptions=False) as app_client: + person = register_and_login(app_client, "down") + assert ( + app_client.get("/api/v1/users/me", headers=person.headers).status_code + == 200 + ) + + dead = replace( + settings, + database_url="postgresql+asyncpg://x:y@127.0.0.1:1/none", + db_pool_timeout=1, + ) + app_client.app.state.sessionmaker = create_sessionmaker(create_engine(dead)) + + assert app_client.get("/health/live").status_code == 200 + assert_error(app_client.get("/health/ready"), 503, "unavailable") + assert_error( + app_client.get("/api/v1/users/me", headers=person.headers), + 503, + "unavailable", + ) + assert_error( + app_client.post( + "/api/v1/users", + json={ + "username": "abcdef", + "display_name": "x", + "password": "long-enough-password", + }, + ), + 503, + "unavailable", + ) + reply = app_client.get("/api/v1/chats") + assert reply.status_code in (401, 503) and reply.status_code != 200 + + +def test_database_down_with_cache_up_still_needs_database_for_data(): + """При живом Redis операции с данными без БД всё равно дают 503.""" + if not CACHE_URL: + pytest.skip("TEST_CACHE_URL не задан (тест для контейнера с Redis)") + settings = make_settings() + with TestClient(create_app(settings), raise_server_exceptions=False) as app_client: + person = register_and_login(app_client, "down_cache") + assert app_client.get("/api/v1/users/me", headers=person.headers).status_code == 200 + + dead = replace( + settings, + database_url="postgresql+asyncpg://x:y@127.0.0.1:1/none", + db_pool_timeout=1, + ) + app_client.app.state.sessionmaker = create_sessionmaker(create_engine(dead)) + + assert_error(app_client.get("/health/ready"), 503, "unavailable") + assert_error(app_client.get("/api/v1/users/me", headers=person.headers), 503, "unavailable") + assert_error(app_client.get("/api/v1/chats", headers=person.headers), 503, "unavailable") + assert_error( + app_client.post("/api/v1/chats", json={"title": "x", "member_ids": [person.id]}, + headers=person.headers), + 503, "unavailable", + ) + assert_error(app_client.get("/api/v1/chats"), 401, "unauthorized") + unknown = {"Authorization": "Bearer " + "A" * 43} + assert app_client.get("/api/v1/users/me", headers=unknown).status_code == 503 diff --git a/student-tests/test_duplicate_username.py b/student-tests/test_duplicate_username.py new file mode 100644 index 00000000..0863caa7 --- /dev/null +++ b/student-tests/test_duplicate_username.py @@ -0,0 +1,45 @@ +"""Повторный username: 409, в том числе при одновременной регистрации.""" + +import uuid +from concurrent.futures import ThreadPoolExecutor + +from conftest import assert_error, run_sql + + +def body(username: str, display_name: str = "Имя") -> dict: + """Тело регистрации.""" + return {"username": username, "display_name": display_name, "password": "long-enough-password"} + + +def test_second_registration_with_same_username_is_409(client): + name = "dup_" + uuid.uuid4().hex[:10] + assert client.post("/api/v1/users", json=body(name)).status_code == 201 + assert_error(client.post("/api/v1/users", json=body(name, "Другое имя")), 409, "conflict") + + +def test_duplicate_does_not_replace_original_user(client): + name = "dup_" + uuid.uuid4().hex[:10] + first = client.post("/api/v1/users", json=body(name, "Первый")).json() + client.post("/api/v1/users", json=body(name, "Второй")) + rows = run_sql("SELECT display_name FROM users WHERE username = $1", name) + assert [row["display_name"] for row in rows] == ["Первый"] + assert first["display_name"] == "Первый" + + +def test_username_is_case_sensitive_lowercase_only(client): + name = "dup_" + uuid.uuid4().hex[:10] + assert client.post("/api/v1/users", json=body(name)).status_code == 201 + assert_error(client.post("/api/v1/users", json=body(name.upper())), 400, "invalid_request") + + +def test_parallel_registration_of_same_username_creates_exactly_one(client): + name = "race_" + uuid.uuid4().hex[:10] + + def register(_): + return client.post("/api/v1/users", json=body(name)).status_code + + with ThreadPoolExecutor(max_workers=10) as pool: + codes = list(pool.map(register, range(10))) + assert codes.count(201) == 1 + assert codes.count(409) == 9 + assert len(run_sql("SELECT 1 FROM users WHERE username = $1", name)) == 1 diff --git a/student-tests/test_empty_message.py b/student-tests/test_empty_message.py new file mode 100644 index 00000000..b92148ef --- /dev/null +++ b/student-tests/test_empty_message.py @@ -0,0 +1,73 @@ +"""Текст сообщения: пустые строки, длина и Unicode.""" + +import pytest + +from conftest import assert_error + + +@pytest.mark.parametrize("text", ["", " ", " ", "\n", "\t \n", " ", " "]) +def test_blank_text_is_rejected(client, alice, chat, text): + url = "/api/v1/chats/" + chat["id"] + "/messages" + assert_error( + client.post(url, json={"text": text}, headers=alice.headers), + 400, + "invalid_request", + ) + assert client.get(url, headers=alice.headers).json()["items"] == [] + + +@pytest.mark.parametrize( + "body", [{}, {"text": None}, {"text": 123}, {"text": ["a"]}, {"message": "hi"}] +) +def test_missing_or_wrong_type_text_is_rejected(client, alice, chat, body): + url = "/api/v1/chats/" + chat["id"] + "/messages" + assert_error( + client.post(url, json=body, headers=alice.headers), 400, "invalid_request" + ) + + +def test_text_length_is_counted_in_characters(client, alice, chat): + url = "/api/v1/chats/" + chat["id"] + "/messages" + assert ( + client.post(url, json={"text": "я" * 4000}, headers=alice.headers).status_code + == 201 + ) + assert ( + client.post(url, json={"text": "😀" * 4000}, headers=alice.headers).status_code + == 201 + ) + assert_error( + client.post(url, json={"text": "я" * 4001}, headers=alice.headers), + 400, + "invalid_request", + ) + + +def test_nul_character_is_400_not_500(client, alice, chat): + """Символ NUL отклоняется с 400.""" + url = "/api/v1/chats/" + chat["id"] + "/messages" + assert_error( + client.post(url, json={"text": "a\x00b"}, headers=alice.headers), + 400, + "invalid_request", + ) + + +def test_unicode_survives_the_database(client, alice, bob, chat): + url = "/api/v1/chats/" + chat["id"] + "/messages" + text = " Привет, мир 👋 你好 ✓ é " + sent = client.post(url, json={"text": text}, headers=alice.headers) + assert sent.status_code == 201 and sent.json()["text"] == text + assert client.get(url, headers=bob.headers).json()["items"][0]["text"] == text + assert sent.headers["content-type"].split(";")[0] == "application/json" + + +def test_message_has_correct_fields(client, alice, chat): + data = client.post( + "/api/v1/chats/" + chat["id"] + "/messages", + json={"text": "hi"}, + headers=alice.headers, + ).json() + assert set(data) == {"id", "chat_id", "sender_id", "text", "created_at"} + assert data["chat_id"] == chat["id"] and data["sender_id"] == alice.id + assert data["created_at"].endswith("Z") diff --git a/student-tests/test_explain.py b/student-tests/test_explain.py new file mode 100644 index 00000000..42ac3dec --- /dev/null +++ b/student-tests/test_explain.py @@ -0,0 +1,52 @@ +"""План запроса истории на 10 000 сообщениях: индекс без полного сканирования.""" + +import asyncio + +import asyncpg +import pytest + +from app import explain +from app.crud import history_query +from conftest import database_parts, make_settings, TEST_DB_NAME +from app.db import create_engine + + +def plan_for(cursor_offset): + """Заполнить данные и вернуть число сообщений и план запроса страницы.""" + p = database_parts() + + async def go(): + conn = await asyncpg.connect(database=TEST_DB_NAME, timeout=5, **p) + try: + await explain.seed(conn, 30000) + count = await conn.fetchval( + "SELECT count(*) FROM messages WHERE chat_id = $1", explain.BIG_CHAT + ) + after = None + if cursor_offset: + row = await conn.fetchrow( + "SELECT created_at, id FROM messages WHERE chat_id = $1" + " ORDER BY created_at, id OFFSET $2 LIMIT 1", + explain.BIG_CHAT, cursor_offset, + ) + after = (row["created_at"], row["id"]) + engine = create_engine(make_settings()) + query = history_query(explain.BIG_CHAT, after, 50).compile(dialect=engine.dialect) + await engine.dispose() + params = [query.params[name] for name in query.positiontup] + rows = await conn.fetch("EXPLAIN (ANALYZE, BUFFERS) " + query.string, *params) + return count, "\n".join(r[0] for r in rows) + finally: + await conn.close() + + return asyncio.run(go()) + + +@pytest.mark.parametrize("cursor_offset", [0, 9000]) +def test_history_page_uses_index_without_full_scan(cursor_offset): + count, plan = plan_for(cursor_offset) + assert count >= 10000 + assert "Index Scan using messages_chat_created_idx" in plan, plan + assert "Seq Scan" not in plan, plan + assert "Sort" not in plan, plan + assert "rows=51 loops=1" in plan, plan diff --git a/student-tests/test_foreign_chat.py b/student-tests/test_foreign_chat.py new file mode 100644 index 00000000..465c9c23 --- /dev/null +++ b/student-tests/test_foreign_chat.py @@ -0,0 +1,65 @@ +"""Доступ к чужому чату: 404 для посторонних, 401 без токена.""" + +import uuid + +from conftest import assert_error + + +def test_outsider_cannot_read_chat(client, eve, chat): + assert_error(client.get("/api/v1/chats/" + chat["id"], headers=eve.headers), 404, "not_found") + + +def test_outsider_cannot_read_history(client, alice, eve, chat): + client.post("/api/v1/chats/" + chat["id"] + "/messages", json={"text": "секрет"}, headers=alice.headers) + reply = client.get("/api/v1/chats/" + chat["id"] + "/messages", headers=eve.headers) + assert_error(reply, 404, "not_found") + assert "секрет" not in reply.text + + +def test_outsider_cannot_write(client, alice, eve, chat): + url = "/api/v1/chats/" + chat["id"] + "/messages" + assert_error(client.post(url, json={"text": "чужое"}, headers=eve.headers), 404, "not_found") + assert client.get(url, headers=alice.headers).json()["items"] == [] + + +def test_outsider_gets_404_even_with_bad_body(client, eve, chat): + """Проверка доступа выполняется раньше проверки тела.""" + url = "/api/v1/chats/" + chat["id"] + "/messages" + assert_error(client.post(url, json={"text": ""}, headers=eve.headers), 404, "not_found") + + +def test_unknown_chat_looks_the_same_as_foreign(client, eve, chat): + foreign = client.get("/api/v1/chats/" + chat["id"], headers=eve.headers) + missing = client.get("/api/v1/chats/" + str(uuid.uuid4()), headers=eve.headers) + assert foreign.status_code == missing.status_code == 404 + assert foreign.json()["error"]["message"] == missing.json()["error"]["message"] + + +def test_chat_list_shows_only_own_chats(client, bob, eve, chat): + mine = client.get("/api/v1/chats", headers=bob.headers).json()["items"] + others = client.get("/api/v1/chats", headers=eve.headers).json()["items"] + assert [c["id"] for c in mine] == [chat["id"]] + assert others == [] + + +def test_bad_chat_id_is_400(client, alice): + assert_error(client.get("/api/v1/chats/not-a-uuid", headers=alice.headers), 400, "invalid_request") + + +def test_no_token_is_401_everywhere(client, chat): + chat_url = "/api/v1/chats/" + chat["id"] + for url in ["/api/v1/users/me", "/api/v1/chats", chat_url, chat_url + "/messages"]: + assert_error(client.get(url), 401, "unauthorized") + assert_error(client.post("/api/v1/chats", json={"title": "x", "member_ids": []}), 401, "unauthorized") + assert_error(client.post(chat_url + "/messages", json={"text": "x"}), 401, "unauthorized") + + +def test_401_comes_before_404(client, chat): + """Без токена существование чата не раскрывается.""" + assert_error(client.get("/api/v1/chats/" + chat["id"] + "/messages"), 401, "unauthorized") + + +def test_member_can_read_and_write(client, alice, bob, chat): + url = "/api/v1/chats/" + chat["id"] + "/messages" + assert client.post(url, json={"text": "от Боба"}, headers=bob.headers).status_code == 201 + assert client.get(url, headers=alice.headers).json()["items"][0]["text"] == "от Боба" diff --git a/student-tests/test_idempotency.py b/student-tests/test_idempotency.py new file mode 100644 index 00000000..0ae76bad --- /dev/null +++ b/student-tests/test_idempotency.py @@ -0,0 +1,122 @@ +"""Общая идемпотентность PostgreSQL: конкурентные реплики, rollback, expiry.""" + +from concurrent.futures import ThreadPoolExecutor +import threading +import uuid + +from fastapi.testclient import TestClient +import pytest +from sqlalchemy.exc import OperationalError +from sqlalchemy.ext.asyncio import AsyncSession + +from app.main import create_app +from app.models import MessageRequest +from conftest import assert_error, make_settings, run_sql + + +def message_path(chat): + return "/api/v1/chats/" + chat["id"] + "/messages" + + +def headers(person, key): + return {"Authorization": "Bearer " + person.token, "Idempotency-Key": key} + + +@pytest.mark.parametrize("key", [None, "", "a b", "a?b", "x" * 129, "a,b", "a/b"]) +def test_key_is_required_and_validated(client, alice, chat, key): + h = {"Authorization": "Bearer " + alice.token} + if key is not None: + h["Idempotency-Key"] = key + assert_error(client.post(message_path(chat), json={"text": "hello"}, headers=h), 400, "invalid_request") + assert not run_sql("SELECT id FROM messages WHERE chat_id=$1", uuid.UUID(chat["id"])) + + +def test_duplicate_headers_are_invalid(client, alice, chat): + h = [("Authorization", "Bearer " + alice.token), ("Idempotency-Key", "a"), ("Idempotency-Key", "b")] + assert_error(client.post(message_path(chat), json={"text": "hi"}, headers=h), 400, "invalid_request") + + +def test_replay_and_conflict_preserve_original_result(client, alice, chat): + h = headers(alice, uuid.uuid4().hex) + first = client.post(message_path(chat), json={"text": "original"}, headers=h) + replay = client.post(message_path(chat), json={"attachment_ids": [], "text": "original"}, headers=h) + assert first.status_code == replay.status_code == 201 + assert first.json() == replay.json() + assert_error(client.post(message_path(chat), json={"text": "changed"}, headers=h), 409, "conflict") + again = client.post(message_path(chat), json={"text": "original"}, headers=h) + assert again.status_code == 201 and again.json() == first.json() + rows = run_sql("SELECT id FROM messages WHERE chat_id=$1", uuid.UUID(chat["id"])) + assert [str(row["id"]) for row in rows] == [first.json()["id"]] + + +def test_parallel_same_key_across_two_applications(client, alice, chat): + h = headers(alice, uuid.uuid4().hex) + barrier = threading.Barrier(8) + with TestClient(create_app(make_settings()), raise_server_exceptions=False) as other: + def send(n): + barrier.wait() + return (client if n % 2 else other).post(message_path(chat), json={"text": "one"}, headers=h) + with ThreadPoolExecutor(max_workers=8) as pool: + replies = list(pool.map(send, range(8))) + assert all(reply.status_code == 201 for reply in replies) + assert len({reply.json()["id"] for reply in replies}) == 1 + assert len({reply.headers["X-Instance-Id"] for reply in replies}) == 2 + assert len(run_sql("SELECT id FROM messages WHERE chat_id=$1", uuid.UUID(chat["id"]))) == 1 + + +def test_key_scope_includes_user_and_chat(client, alice, bob, chat, make_chat): + other = make_chat(alice, bob) + key = uuid.uuid4().hex + replies = [client.post(message_path(room), json={"text": "hi"}, headers=headers(person, key)) + for person, room in [(alice, chat), (bob, chat), (alice, other)]] + assert all(reply.status_code == 201 for reply in replies) + assert len({reply.json()["id"] for reply in replies}) == 3 + + +def test_expired_result_allows_new_message(client, alice, chat): + key = uuid.uuid4().hex + h = headers(alice, key) + first = client.post(message_path(chat), json={"text": "old"}, headers=h) + run_sql("UPDATE message_requests SET expires_at=clock_timestamp()-interval '1 second' WHERE user_id=$1 AND chat_id=$2 AND key=$3", + uuid.UUID(alice.id), uuid.UUID(chat["id"]), key) + second = client.post(message_path(chat), json={"text": "new"}, headers=h) + assert first.status_code == second.status_code == 201 + assert first.json()["id"] != second.json()["id"] + + +def test_failure_between_message_and_result_rolls_back(client, alice, chat, monkeypatch): + key = uuid.uuid4().hex + h = headers(alice, key) + real = AsyncSession.execute + async def fail_result(self, statement, *args, **kwargs): + if getattr(getattr(statement, "table", None), "name", None) == MessageRequest.__tablename__: + raise OperationalError("", {}, Exception("injected result failure")) + return await real(self, statement, *args, **kwargs) + with monkeypatch.context() as patch: + patch.setattr(AsyncSession, "execute", fail_result) + assert_error(client.post(message_path(chat), json={"text": "once"}, headers=h), 503, "unavailable") + assert not run_sql("SELECT id FROM messages WHERE chat_id=$1", uuid.UUID(chat["id"])) + assert not run_sql("SELECT message_id FROM message_requests WHERE chat_id=$1", uuid.UUID(chat["id"])) + assert client.post(message_path(chat), json={"text": "once"}, headers=h).status_code == 201 + + +def test_committed_but_lost_response_is_safe_to_retry(client, alice, chat, monkeypatch): + from app import routes + key = uuid.uuid4().hex + h = headers(alice, key) + with monkeypatch.context() as patch: + patch.setattr(routes, "message_out", lambda row: (_ for _ in ()).throw(RuntimeError("lost after commit"))) + assert_error(client.post(message_path(chat), json={"text": "one"}, headers=h), 500, "internal_error") + replay = client.post(message_path(chat), json={"text": "one"}, headers=h) + assert replay.status_code == 201 + rows = run_sql("SELECT id FROM messages WHERE chat_id=$1", uuid.UUID(chat["id"])) + assert [str(row["id"]) for row in rows] == [replay.json()["id"]] + + +def test_replay_rechecks_session_and_chat_access(client, alice, eve, chat): + key = uuid.uuid4().hex + h = headers(alice, key) + assert client.post(message_path(chat), json={"text": "one"}, headers=h).status_code == 201 + assert_error(client.post(message_path(chat), json={"text": "one"}, headers=headers(eve, key)), 404, "not_found") + assert client.delete("/api/v1/auth/sessions/current", headers=alice.headers).status_code == 204 + assert_error(client.post(message_path(chat), json={"text": "one"}, headers=h), 401, "unauthorized") diff --git a/student-tests/test_invalid_json.py b/student-tests/test_invalid_json.py new file mode 100644 index 00000000..9d76d14b --- /dev/null +++ b/student-tests/test_invalid_json.py @@ -0,0 +1,115 @@ +"""Некорректный JSON и поля: 400, 413 и 415.""" + +import uuid + +import pytest + +from conftest import assert_error + +JSON = {"Content-Type": "application/json"} +PASSWORD = "long-enough-password" + + +def valid(**changes) -> dict: + """Корректное тело регистрации с изменёнными полями.""" + body = {"username": "j_" + uuid.uuid4().hex[:10], "display_name": "Имя", "password": PASSWORD} + body.update(changes) + return {key: value for key, value in body.items() if value is not None} + + +@pytest.mark.parametrize( + "raw", + [ + b"{", + b"not json at all", + b"", + b'{"username": "abc",}', + b"\xff\xfe\x00", + b'{"username": NaN}', + b'["username"]', + b'"text"', + b"null", + ], +) +def test_registration_rejects_broken_json(client, raw): + assert_error(client.post("/api/v1/users", content=raw, headers=JSON), 400, "invalid_request") + + +@pytest.mark.parametrize( + "changes", + [ + {"username": None}, + {"display_name": None}, + {"password": None}, + {"username": 123}, + {"display_name": 5}, + {"display_name": None, "extra_field": 1}, + {"role": "admin"}, + {"username": "ab"}, + {"username": "a" * 33}, + {"username": "Alice"}, + {"username": "al ice"}, + {"username": "алиса"}, + {"username": "alice\n"}, + {"display_name": ""}, + {"display_name": " "}, + {"display_name": "я" * 101}, + {"display_name": "a\x00b"}, + ], +) +def test_registration_rejects_bad_fields(client, changes): + assert_error(client.post("/api/v1/users", json=valid(**changes)), 400, "invalid_request") + + +def test_border_values_are_accepted(client): + assert client.post("/api/v1/users", json=valid(display_name="я" * 100)).status_code == 201 + assert client.post("/api/v1/users", json=valid(username=uuid.uuid4().hex, display_name="😀")).status_code == 201 + + +def test_lone_surrogate_is_400_not_500(client): + raw = b'{"username": "abcdef", "display_name": "\\ud800", "password": "long-enough-password"}' + assert_error(client.post("/api/v1/users", content=raw, headers=JSON), 400, "invalid_request") + + +def test_wrong_or_missing_content_type_is_415(client): + body = b'{"username":"abc","display_name":"x","password":"long-enough-password"}' + assert_error(client.post("/api/v1/users", content=body, headers={"Content-Type": "text/plain"}), 415, "unsupported_media_type") + assert_error(client.post("/api/v1/users", content=body), 415, "unsupported_media_type") + + +def test_huge_body_is_413(client): + big = b'{"username": "abc", "display_name": "' + b"x" * (300 * 1024) + b'"}' + assert_error(client.post("/api/v1/users", content=big, headers=JSON), 413, "payload_too_large") + + +def test_broken_json_in_message_and_chat(client, alice, chat): + headers = {**JSON, **alice.headers} + assert_error(client.post("/api/v1/chats/" + chat["id"] + "/messages", content=b"{oops", headers=headers), 400, "invalid_request") + assert_error(client.post("/api/v1/chats", content=b"[", headers=headers), 400, "invalid_request") + + +def test_sender_id_in_message_is_rejected(client, alice, bob, chat): + """Поле sender_id в теле сообщения запрещено.""" + reply = client.post( + "/api/v1/chats/" + chat["id"] + "/messages", json={"text": "привет", "sender_id": bob.id}, headers=alice.headers + ) + assert_error(reply, 400, "invalid_request") + + +def test_chat_body_validation(client, alice, bob): + unknown = str(uuid.uuid4()) + cases = [ + {"title": "x", "member_ids": []}, + {"title": "", "member_ids": [alice.id]}, + {"title": " ", "member_ids": [alice.id]}, + {"title": "x" * 101, "member_ids": [alice.id]}, + {"title": "x", "member_ids": [alice.id, alice.id]}, + {"title": "x", "member_ids": [alice.id, unknown]}, + {"title": "x", "member_ids": [bob.id]}, + {"title": "x", "member_ids": ["не-uuid"]}, + {"title": "x", "member_ids": alice.id}, + {"title": "x"}, + {"title": "x", "member_ids": [alice.id], "extra": 1}, + ] + for body in cases: + assert_error(client.post("/api/v1/chats", json=body, headers=alice.headers), 400, "invalid_request") diff --git a/student-tests/test_pagination.py b/student-tests/test_pagination.py new file mode 100644 index 00000000..d0d44557 --- /dev/null +++ b/student-tests/test_pagination.py @@ -0,0 +1,193 @@ +"""Пагинация истории и списка чатов по keyset-курсору.""" + +from urllib.parse import quote + +import pytest + +from app.cursor import encode_cursor +from app.timeutil import MAX_TIMESTAMP_MICROS +from conftest import assert_error + + +def send_many(client, person, chat, count, prefix="msg"): + """Отправить несколько сообщений в чат.""" + url = "/api/v1/chats/" + chat["id"] + "/messages" + for number in range(count): + reply = client.post( + url, json={"text": "%s-%02d" % (prefix, number)}, headers=person.headers + ) + assert reply.status_code == 201 + + +def walk(client, person, chat, limit): + """Пройти историю страницами; вернуть элементы и число страниц.""" + url = "/api/v1/chats/" + chat["id"] + "/messages" + items, pages, cursor = [], 0, None + while True: + query = "?limit=%d" % limit + ( + "&cursor=" + quote(cursor, safe="") if cursor else "" + ) + data = client.get(url + query, headers=person.headers).json() + assert set(data) == {"items", "next_cursor"} and len(data["items"]) <= limit + items += data["items"] + pages += 1 + cursor = data["next_cursor"] + if cursor is None: + return items, pages + assert pages < 1000, "бесконечный обход" + + +def test_walk_25_messages_by_7(client, alice, bob, chat): + send_many(client, alice, chat, 25) + items, pages = walk(client, bob, chat, 7) + assert pages == 4 + assert [m["text"] for m in items] == ["msg-%02d" % n for n in range(25)] + assert len({m["id"] for m in items}) == 25 + + +def test_exact_multiple_has_no_empty_extra_page(client, alice, chat): + send_many(client, alice, chat, 10) + items, pages = walk(client, alice, chat, 5) + assert len(items) == 10 and pages == 2 + + +def test_limit_one_walks_everything(client, alice, chat): + send_many(client, alice, chat, 6) + items, pages = walk(client, alice, chat, 1) + assert len(items) == 6 and pages == 6 + + +def test_default_limit_is_50_and_max_is_100(client, alice, chat): + send_many(client, alice, chat, 120) + url = "/api/v1/chats/" + chat["id"] + "/messages" + default = client.get(url, headers=alice.headers).json() + assert len(default["items"]) == 50 and default["next_cursor"] + biggest = client.get(url + "?limit=100", headers=alice.headers).json() + assert len(biggest["items"]) == 100 and biggest["next_cursor"] + + +def test_empty_history(client, alice, chat): + data = client.get( + "/api/v1/chats/" + chat["id"] + "/messages", headers=alice.headers + ).json() + assert data == {"items": [], "next_cursor": None} + + +def test_limit_can_change_between_pages(client, alice, chat): + send_many(client, alice, chat, 9) + url = "/api/v1/chats/" + chat["id"] + "/messages" + first = client.get(url + "?limit=2", headers=alice.headers).json() + second = client.get( + url + "?limit=100&cursor=" + quote(first["next_cursor"], safe=""), + headers=alice.headers, + ).json() + assert [m["text"] for m in first["items"] + second["items"]] == [ + "msg-%02d" % n for n in range(9) + ] + assert second["next_cursor"] is None + + +def test_messages_sent_during_walk_appear_at_the_end(client, alice, chat): + send_many(client, alice, chat, 4) + url = "/api/v1/chats/" + chat["id"] + "/messages" + first = client.get(url + "?limit=2", headers=alice.headers).json() + send_many(client, alice, chat, 1, prefix="late") + rest = client.get( + url + "?limit=10&cursor=" + quote(first["next_cursor"], safe=""), + headers=alice.headers, + ).json() + texts = [m["text"] for m in first["items"] + rest["items"]] + assert texts == ["msg-00", "msg-01", "msg-02", "msg-03", "late-00"] + + +def test_created_at_is_increasing_and_utc(client, alice, chat): + send_many(client, alice, chat, 30) + items, _ = walk(client, alice, chat, 100) + stamps = [m["created_at"] for m in items] + assert stamps == sorted(stamps) and len(set(stamps)) == 30 + assert all(s.endswith("Z") and "T" in s for s in stamps) + + +def test_bad_limit_values(client, alice, chat): + url = "/api/v1/chats/" + chat["id"] + "/messages" + for bad in ["0", "101", "-1", "abc", "1.5", "", "+5", " 5", "٣"]: + assert_error( + client.get(url + "?limit=" + quote(bad), headers=alice.headers), + 400, + "invalid_request", + ) + assert client.get(url + "?limit=1", headers=alice.headers).status_code == 200 + assert client.get(url + "?limit=100", headers=alice.headers).status_code == 200 + + +def test_garbage_cursor_is_400(client, alice, chat): + url = "/api/v1/chats/" + chat["id"] + "/messages" + for bad in ["garbage", "!!!", "e30", "a" * 600, "eyJzIjoibWVzc2FnZXM6eCJ9"]: + assert_error( + client.get(url + "?cursor=" + quote(bad), headers=alice.headers), + 400, + "invalid_request", + ) + + +def test_cursor_from_another_chat_is_400(client, alice, bob, make_chat, chat): + other = make_chat(alice, bob, title="Второй чат") + send_many(client, alice, chat, 5) + send_many(client, alice, other, 5) + first = client.get( + "/api/v1/chats/" + chat["id"] + "/messages?limit=2", headers=alice.headers + ).json() + reply = client.get( + "/api/v1/chats/" + + other["id"] + + "/messages?cursor=" + + quote(first["next_cursor"], safe=""), + headers=alice.headers, + ) + assert_error(reply, 400, "invalid_request") + + +def test_chat_list_pagination(client, alice, bob, make_chat): + created = [make_chat(alice, bob, title="Чат %02d" % n)["id"] for n in range(5)] + seen, cursor = [], None + while True: + query = "?limit=2" + ("&cursor=" + quote(cursor, safe="") if cursor else "") + data = client.get("/api/v1/chats" + query, headers=bob.headers).json() + seen += [c["id"] for c in data["items"]] + cursor = data["next_cursor"] + if cursor is None: + break + assert seen == created + + +def test_chat_list_cursor_cannot_be_used_for_messages( + client, alice, bob, make_chat, chat +): + make_chat(alice, bob, title="ещё") + cursor = client.get("/api/v1/chats?limit=1", headers=alice.headers).json()[ + "next_cursor" + ] + reply = client.get( + "/api/v1/chats/" + chat["id"] + "/messages?cursor=" + quote(cursor, safe=""), + headers=alice.headers, + ) + assert_error(reply, 400, "invalid_request") + + +@pytest.mark.parametrize("stamp", [-1, 10**30, MAX_TIMESTAMP_MICROS + 1, True, 1.5, "1", None]) +@pytest.mark.parametrize("resource", ["messages", "chats"]) +def test_invalid_cursor_timestamp_is_400(client, alice, chat, stamp, resource): + scope = "messages:" + chat["id"] if resource == "messages" else "chats:" + alice.id + path = "/api/v1/chats/" + chat["id"] + "/messages" if resource == "messages" else "/api/v1/chats" + cursor = encode_cursor(scope, (stamp, chat["id"])) + assert_error(client.get(path, params={"cursor": cursor}, headers=alice.headers), 400, "invalid_request") + + +@pytest.mark.parametrize("stamp", [0, MAX_TIMESTAMP_MICROS]) +@pytest.mark.parametrize("resource", ["messages", "chats"]) +def test_cursor_timestamp_boundaries_are_valid(client, alice, chat, stamp, resource): + scope = "messages:" + chat["id"] if resource == "messages" else "chats:" + alice.id + path = "/api/v1/chats/" + chat["id"] + "/messages" if resource == "messages" else "/api/v1/chats" + cursor = encode_cursor(scope, (stamp, chat["id"])) + reply = client.get(path, params={"cursor": cursor}, headers=alice.headers) + assert reply.status_code == 200, reply.text diff --git a/student-tests/test_session_expiry.py b/student-tests/test_session_expiry.py new file mode 100644 index 00000000..373e067f --- /dev/null +++ b/student-tests/test_session_expiry.py @@ -0,0 +1,56 @@ +"""Истечение сессий.""" + +import time + +from fastapi.testclient import TestClient + +from app.main import create_app +from conftest import assert_error, make_settings, register_and_login + + +def test_session_expires_after_ttl(): + with TestClient( + create_app(make_settings(session_ttl_seconds=1)), raise_server_exceptions=False + ) as short: + person = register_and_login(short, "ttl") + assert short.get("/api/v1/users/me", headers=person.headers).status_code == 200 + time.sleep(2.2) + reply = short.get("/api/v1/users/me", headers=person.headers) + assert_error(reply, 401, "unauthorized") + assert reply.headers["WWW-Authenticate"] == 'Bearer realm="messenger"' + + +def test_expired_sessions_are_cleaned_on_next_login(): + """Истёкшие сессии удаляются при следующем входе.""" + from conftest import basic_header, run_sql + import uuid + + with TestClient( + create_app(make_settings(session_ttl_seconds=1)), raise_server_exceptions=False + ) as short: + person = register_and_login(short, "clean") + time.sleep(1.5) + short.post( + "/api/v1/auth/sessions", + headers=basic_header(person.username, person.password), + ) + rows = run_sql( + "SELECT token_hash FROM sessions WHERE user_id = $1", uuid.UUID(person.id) + ) + assert len(rows) == 1 + + +def test_short_ttl_from_environment_variable(): + """Короткий SESSION_TTL_SECONDS из окружения.""" + from conftest import CACHE_URL, sqlalchemy_url + from app.config import Settings + + settings = Settings.from_env( + {"DATABASE_URL": sqlalchemy_url(), "SESSION_TTL_SECONDS": "2", "CACHE_URL": CACHE_URL} + ) + assert settings.session_ttl_seconds == 2 + with TestClient(create_app(settings), raise_server_exceptions=False) as short: + person = register_and_login(short, "envttl") + assert short.get("/api/v1/users/me", headers=person.headers).status_code == 200 + time.sleep(2.5) + assert_error(short.get("/api/v1/users/me", headers=person.headers), 401, "unauthorized") diff --git a/student-tests/test_transactions.py b/student-tests/test_transactions.py new file mode 100644 index 00000000..8fe59a8f --- /dev/null +++ b/student-tests/test_transactions.py @@ -0,0 +1,69 @@ +"""Транзакции, пул соединений и таймауты.""" + +import time +import uuid + +import httpx +from fastapi.testclient import TestClient +from sqlalchemy.exc import OperationalError +from sqlalchemy.ext.asyncio import AsyncSession + +from app.main import create_app +from app.models import ChatMember +from conftest import assert_error, make_settings, register_and_login, run_sql + + +def test_failure_after_chat_insert_rolls_back_whole_operation(client, alice, bob, monkeypatch): + """Сбой после вставки чата откатывает всю операцию.""" + real_execute = AsyncSession.execute + + async def failing_execute(self, statement, *args, **kwargs): + if getattr(getattr(statement, "table", None), "name", None) == ChatMember.__tablename__: + raise OperationalError("INSERT INTO chat_members", {}, Exception("injected")) + return await real_execute(self, statement, *args, **kwargs) + + monkeypatch.setattr(AsyncSession, "execute", failing_execute) + title = "rollback-" + uuid.uuid4().hex + reply = client.post( + "/api/v1/chats", json={"title": title, "member_ids": [alice.id, bob.id]}, + headers=alice.headers, + ) + monkeypatch.undo() + + assert_error(reply, 503, "unavailable") + assert "injected" not in reply.text and "chat_members" not in reply.text + assert run_sql("SELECT count(*) AS n FROM chats WHERE title = $1", title)[0]["n"] == 0 + assert run_sql( + "SELECT count(*) AS n FROM chat_members m JOIN chats c ON c.id = m.chat_id" + " WHERE c.title = $1", title, + )[0]["n"] == 0 + + +def test_pool_is_bounded_by_settings(): + settings = make_settings(db_pool_size=3, db_max_overflow=2, db_pool_timeout=4) + with TestClient(create_app(settings), raise_server_exceptions=False) as app_client: + pool = app_client.app.state.engine.pool + assert pool.size() == 3 + assert pool._max_overflow == 2 + assert pool._timeout == 4 + + +def test_exhausted_pool_gives_fast_503(): + """Занятый пул даёт 503 за время DB_POOL_TIMEOUT.""" + settings = make_settings(db_pool_size=1, db_max_overflow=0, db_pool_timeout=1, cache_url="") + with TestClient(create_app(settings), raise_server_exceptions=False) as app_client: + person = register_and_login(app_client, "pool") + app = app_client.app + + async def scenario(): + async with app.state.engine.connect(): + transport = httpx.ASGITransport(app=app) + async with httpx.AsyncClient(transport=transport, base_url="http://t") as http: + started = time.monotonic() + reply = await http.get("/api/v1/users/me", headers=person.headers) + return reply, time.monotonic() - started + + reply, elapsed = app_client.portal.call(scenario) + assert_error(reply, 503, "unavailable") + assert elapsed < 3 + assert app_client.get("/api/v1/users/me", headers=person.headers).status_code == 200 diff --git a/tests/resilience.py b/tests/resilience.py index f3fa700a..3c898ce1 100644 --- a/tests/resilience.py +++ b/tests/resilience.py @@ -1,5 +1,6 @@ #!/usr/bin/env python3 """Explicit operator-authored hook; tokens remain in memory, volumes are never removed by this test.""" + import json import os from pathlib import Path @@ -8,114 +9,196 @@ import time import uuid from urllib.parse import quote from client import Client, Failure, LAB, arguments, check, run +import platform def history(c, actor, chat): - path = '/api/v1/chats/'+chat['id']+'/messages?limit=100' + path = "/api/v1/chats/" + chat["id"] + "/messages?limit=100" result = [] seen = set() for _ in range(1000): - page = c.call('GET',path,actor=actor,schema='MessagePage')['json'] - result.extend(page['items']) - cursor = page['next_cursor'] + page = c.call("GET", path, actor=actor, schema="MessagePage")["json"] + result.extend(page["items"]) + cursor = page["next_cursor"] if cursor is None: return result - check(cursor not in seen,'cursor cycle') + check(cursor not in seen, "cursor cycle") seen.add(cursor) - path = '/api/v1/chats/'+chat['id']+'/messages?limit=100&cursor='+quote(cursor,safe='') - raise Failure('history pagination exceeded safety bound') + path = ( + "/api/v1/chats/" + + chat["id"] + + "/messages?limit=100&cursor=" + + quote(cursor, safe="") + ) + raise Failure("history pagination exceeded safety bound") def main(): def configure(p): - p.add_argument('--mode', choices=['persistence','failover'], required=True) - p.add_argument('--action-script',required=True,help='Executable script authored by student; restarts stack or stops ONE API replica') - args = arguments('Explicit restart/failover scenario; modifies only synthetic API data',configure) - check(LAB>=2,'persistence requires lab2+') - check(args.mode!='failover' or LAB>=3,'failover requires lab3+') + p.add_argument("--mode", choices=["persistence", "failover"], required=True) + p.add_argument( + "--action-script", + required=True, + help="Executable script authored by student; restarts stack or stops ONE API replica", + ) + + args = arguments( + "Explicit restart/failover scenario; modifies only synthetic API data", + configure, + ) + check(LAB >= 2, "persistence requires lab2+") + check(args.mode != "failover" or LAB >= 3, "failover requires lab3+") script = Path(args.action_script).expanduser().resolve() - check(script.is_file() and os.access(script,os.X_OK),'action script must exist and be executable (chmod +x)') - check('.example.' not in script.name,'copy and implement the example hook first') - c = Client(args.base_url,args.ca_file,timeout=2) - alice,bob,eve,chat = c.bootstrap() - sent = c.message(alice,chat,'Сохранить при отказе') - original = sent['json'] - target = sent['headers'].get('X-Instance-Id','') + check( + script.is_file() and os.access(script, os.X_OK), + "action script must exist and be executable (chmod +x)", + ) + check(".example." not in script.name, "copy and implement the example hook first") + c = Client(args.base_url, args.ca_file, timeout=2) + alice, bob, eve, chat = c.bootstrap() + sent = c.message(alice, chat, "Сохранить при отказе") + original = sent["json"] + target = sent["headers"].get("X-Instance-Id", "") attachment = content = None - if LAB>=4: - attachment,content = c.attachment(alice,chat) - if LAB>=5: - attachment = c.wait_ready(alice,attachment) + if LAB >= 4: + attachment, content = c.attachment(alice, chat) + if LAB >= 5: + attachment = c.wait_ready(alice, attachment) env = dict(os.environ) - env['TARGET_INSTANCE_ID'] = target + env["TARGET_INSTANCE_ID"] = target observed = set() - if args.mode=='failover': + if args.mode == "failover": for _ in range(40): - observed.add(c.call('GET','/api/v1/users/me',actor=alice,schema='User')['headers']['X-Instance-Id']) - check(len(observed)>=2,'less than two API instances observed before failure; verify LB/session affinity') - print('Executing explicit '+args.mode+' hook; hook output suppressed to avoid leaking secrets.') - # No shell interpolation and no Docker commands in this test. - proc = subprocess.Popen([str(script)],env=env,stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL) + observed.add( + c.call("GET", "/api/v1/users/me", actor=alice, schema="User")[ + "headers" + ]["X-Instance-Id"] + ) + time.sleep(0.05) + check( + len(observed) >= 2, + "less than two API instances observed before failure; verify LB/session affinity", + ) + print( + "Executing explicit " + + args.mode + + " hook; hook output suppressed to avoid leaking secrets." + ) + cmd = [str(script)] + if platform.system() == "Windows": + # Явно заворачиваем выполнение .sh скрипта в Git Bash на Windows + cmd = ["C:\\Program Files\\Git\\bin\\bash.exe", str(script)] + + proc = subprocess.Popen( + cmd, env=env, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL + ) started = time.monotonic() - successes,failures,after_instances = [],0,[] + successes, failures, after_instances = [], 0, [] try: - if args.mode=='persistence': + if args.mode == "persistence": try: code = proc.wait(timeout=90) except subprocess.TimeoutExpired: - raise Failure('restart hook exceeded 90 seconds') from None - check(code==0,'restart hook failed (inspect your script locally)') - deadline = time.monotonic()+90 + raise Failure("restart hook exceeded 90 seconds") from None + check(code == 0, "restart hook failed (inspect your script locally)") + deadline = time.monotonic() + 90 while True: try: - c.call('GET','/health/ready',schema='Health') - c.call('GET','/api/v1/users/me',actor=alice,schema='User') + c.call("GET", "/health/ready", schema="Health") + c.call("GET", "/api/v1/users/me", actor=alice, schema="User") break except Failure: - check(time.monotonic()=5,'too few successful writes after stop') - check(target not in after_instances[-5:],'target replica still serves requests; hook must leave it stopped') - print(json.dumps({'observed_instances_before':len(observed),'successful_writes':len(successes),'failed_attempts':failures,'first_recovery_seconds':round(first_recovery,3)},ensure_ascii=False)) + check(proc.poll() == 0, "stop-one hook did not finish successfully") + check( + first_recovery is not None and first_recovery <= 10, + "writes did not recover within 10 seconds from hook start", + ) + check(len(after_instances) >= 5, "too few successful writes after stop") + check( + target not in after_instances[-5:], + "target replica still serves requests; hook must leave it stopped", + ) + print( + json.dumps( + { + "observed_instances_before": len(observed), + "successful_writes": len(successes), + "failed_attempts": failures, + "first_recovery_seconds": round(first_recovery, 3), + }, + ensure_ascii=False, + ) + ) # Existing token, no re-login. Read all confirmations and reject duplicates. - messages = history(c,bob,chat) - ids = [m['id'] for m in messages] - check(len(ids)==len(set(ids)),'duplicated message IDs in history') - check(any(m==original for m in messages),'confirmed pre-failure message lost or altered') + messages = history(c, bob, chat) + ids = [m["id"] for m in messages] + check(len(ids) == len(set(ids)), "duplicated message IDs in history") + check( + any(m == original for m in messages), + "confirmed pre-failure message lost or altered", + ) for msg in successes: - check(any(m==msg for m in messages),'confirmed in-flight message lost or altered') + check( + any(m == msg for m in messages), + "confirmed in-flight message lost or altered", + ) # Bodies are unique in this scenario: an uncertain request may exist once, never twice. - texts = [m['text'] for m in messages] - check(len(texts)==len(set(texts)),'uncertain retry created duplicate message') + texts = [m["text"] for m in messages] + check( + len(texts) == len(set(texts)), "uncertain retry created duplicate message" + ) if attachment: - c.call('GET','/api/v1/attachments/'+attachment['id'],actor=bob,schema='Attachment') - downloaded = c.call('GET','/api/v1/attachments/'+attachment['id']+'/content',actor=bob) - check(downloaded['bytes']==content,'attachment lost or changed across failure') - print('PASS: '+args.mode+' preserved confirmed data and existing session. Hook scope needs operator evidence.') + c.call( + "GET", + "/api/v1/attachments/" + attachment["id"], + actor=bob, + schema="Attachment", + ) + downloaded = c.call( + "GET", "/api/v1/attachments/" + attachment["id"] + "/content", actor=bob + ) + check( + downloaded["bytes"] == content, + "attachment lost or changed across failure", + ) + print( + "PASS: " + + args.mode + + " preserved confirmed data and existing session. Hook scope needs operator evidence." + ) finally: if proc.poll() is None: proc.terminate() @@ -125,5 +208,6 @@ def main(): proc.kill() proc.wait() -if __name__=='__main__': + +if __name__ == "__main__": run(main) diff --git a/unit-tests/test_message_transaction.py b/unit-tests/test_message_transaction.py new file mode 100644 index 00000000..380ea5f7 --- /dev/null +++ b/unit-tests/test_message_transaction.py @@ -0,0 +1,70 @@ +"""Транзакционные ветки на подменённой БД: rollback и неизменность результата.""" + +import asyncio +from types import SimpleNamespace +from unittest.mock import AsyncMock, MagicMock +import uuid + +import pytest +from sqlalchemy.exc import OperationalError + +from app.crud import IdempotencyConflict, create_message_idempotent +from app.models import MessageRequest + +USER_ID = uuid.UUID(int=1) +CHAT_ID = uuid.UUID(int=2) + + +def result(value): + row = MagicMock() + row.scalar_one_or_none.return_value = value + row.scalar_one.return_value = value + return row + + +def test_conflicting_replay_rolls_back_without_commit(): + session = AsyncMock() + session.execute.side_effect = [result(None), result(SimpleNamespace(request_text="original"))] + with pytest.raises(IdempotencyConflict): + asyncio.run(create_message_idempotent(session, CHAT_ID, USER_ID, "changed", "key")) + session.commit.assert_not_awaited() + session.rollback.assert_awaited_once() + + +def test_result_insert_failure_never_commits_message(): + session = AsyncMock() + message = SimpleNamespace(id=uuid.uuid4()) + async def execute(statement): + table = getattr(getattr(statement, "table", None), "name", None) + if table == MessageRequest.__tablename__ and statement.is_insert: + raise OperationalError("", {}, Exception("injected")) + if table == "messages": + return result(message) + return result(None) + session.execute.side_effect = execute + with pytest.raises(OperationalError): + asyncio.run(create_message_idempotent(session, CHAT_ID, USER_ID, "original", "key")) + session.commit.assert_not_awaited() + session.rollback.assert_awaited_once() + + +def test_replay_returns_original_message_and_releases_transaction(): + session = AsyncMock() + message = SimpleNamespace(id=uuid.uuid4(), text="original") + previous = SimpleNamespace(request_text="original", message_id=message.id) + session.execute.side_effect = [result(None), result(previous), result(message)] + actual = asyncio.run(create_message_idempotent(session, CHAT_ID, USER_ID, "original", "key")) + assert actual is message + session.commit.assert_awaited_once() + session.rollback.assert_not_awaited() + + +def test_failed_commit_rolls_back_and_propagates_error(): + session = AsyncMock() + message = SimpleNamespace(id=uuid.uuid4(), text="original") + previous = SimpleNamespace(request_text="original", message_id=message.id) + session.execute.side_effect = [result(None), result(previous), result(message)] + session.commit.side_effect = OperationalError("", {}, Exception("commit failed")) + with pytest.raises(OperationalError): + asyncio.run(create_message_idempotent(session, CHAT_ID, USER_ID, "original", "key")) + session.rollback.assert_awaited_once() diff --git a/unit-tests/test_mr_regressions.py b/unit-tests/test_mr_regressions.py new file mode 100644 index 00000000..e3dad373 --- /dev/null +++ b/unit-tests/test_mr_regressions.py @@ -0,0 +1,151 @@ +"""Проверки отзыва и cursor без внешних сервисов; SQL-путь проверяется отдельно.""" + +from types import SimpleNamespace +from unittest.mock import AsyncMock +import uuid + +import pytest +from fastapi.testclient import TestClient +from sqlalchemy.exc import OperationalError + +from app.auth import current_user, get_session +from app.config import Settings +from app.cursor import decode_cursor, encode_cursor +from app.main import create_app +from app.models import User +from app.timeutil import EPOCH, MAX_TIMESTAMP_MICROS, from_micros, to_micros + +USER_ID = uuid.UUID("00000000-0000-0000-0000-000000000001") +CHAT_ID = "00000000-0000-0000-0000-000000000002" +PROFILE = {"id": str(USER_ID), "username": "alice", "display_name": "Alice", "created_at": EPOCH.isoformat()} +HEADERS = {"Authorization": "Bearer " + "A" * 43} + + +@pytest.fixture +def app_client(): + """Приложение с подменённой сессией БД, без запуска lifespan.""" + settings = Settings.from_env({"DATABASE_URL": "postgresql+asyncpg://test:test@localhost/test"}) + app = create_app(settings) + session = AsyncMock() + + async def session_override(): + yield session + + app.dependency_overrides[get_session] = session_override + app.state.cache = SimpleNamespace(get=AsyncMock(return_value=PROFILE), put=AsyncMock()) + client = TestClient(app, raise_server_exceptions=False) + yield client, session + client.close() + + +@pytest.mark.parametrize("path, method", [ + ("/api/v1/users/me", "GET"), + (f"/api/v1/chats/{CHAT_ID}/messages", "POST"), + ("/api/v1/auth/sessions/current", "DELETE"), +]) +def test_stale_positive_cache_never_authorizes_revoked_token(app_client, monkeypatch, path, method): + client, session = app_client + check = AsyncMock(return_value=False) + monkeypatch.setattr("app.crud.session_is_active", check) + write = AsyncMock() + monkeypatch.setattr("app.crud.create_message_idempotent", write) + reply = client.request(method, path, headers=HEADERS) + assert reply.status_code == 401 + assert reply.json()["error"]["code"] == "unauthorized" + assert reply.headers["WWW-Authenticate"] == 'Bearer realm="messenger"' + check.assert_awaited_once() + write.assert_not_awaited() + + +def test_valid_cache_hit_checks_session_without_loading_user(app_client, monkeypatch): + client, session = app_client + check = AsyncMock(return_value=True) + load = AsyncMock() + monkeypatch.setattr("app.crud.session_is_active", check) + monkeypatch.setattr("app.crud.get_user_by_token", load) + reply = client.get("/api/v1/users/me", headers=HEADERS) + assert reply.status_code == 200 + assert reply.json()["id"] == str(USER_ID) + check.assert_awaited_once() + load.assert_not_awaited() + + +def test_database_failure_on_positive_cache_hit_is_503(app_client, monkeypatch): + client, session = app_client + monkeypatch.setattr("app.crud.session_is_active", AsyncMock(side_effect=OperationalError("", {}, Exception()))) + reply = client.get("/api/v1/users/me", headers=HEADERS) + assert reply.status_code == 503 + assert reply.json()["error"]["code"] == "unavailable" + + +@pytest.mark.parametrize("stamp", [-1, MAX_TIMESTAMP_MICROS + 1, 10**30, True, 1.5, "1", None]) +@pytest.mark.parametrize("resource", ["messages", "chats"]) +def test_cursor_timestamp_is_rejected_through_http(app_client, monkeypatch, stamp, resource): + client, session = app_client + user = User(id=USER_ID) + client.app.dependency_overrides[current_user] = lambda: user + monkeypatch.setattr("app.crud.get_chat_for_member", AsyncMock(return_value=SimpleNamespace(id=uuid.UUID(CHAT_ID)))) + scope = "messages:" + CHAT_ID if resource == "messages" else "chats:" + str(USER_ID) + path = f"/api/v1/chats/{CHAT_ID}/messages" if resource == "messages" else "/api/v1/chats" + reply = client.get(path, params={"cursor": encode_cursor(scope, (stamp, CHAT_ID))}) + assert reply.status_code == 400 + assert reply.json()["error"]["code"] == "invalid_request" + assert reply.json()["error"]["request_id"] == reply.headers["X-Request-Id"] + + +@pytest.mark.parametrize("stamp", [0, MAX_TIMESTAMP_MICROS]) +def test_timestamp_boundaries_roundtrip(stamp): + key = decode_cursor(encode_cursor("scope", (stamp, CHAT_ID)), "scope") + assert key == (stamp, CHAT_ID) + assert to_micros(from_micros(key[0])) == stamp + + +@pytest.mark.parametrize("key", [None, "", "bad key", "x" * 129, "a?b", "a,b"]) +def test_message_key_validation(app_client, monkeypatch, key): + client, session = app_client + client.app.dependency_overrides[current_user] = lambda: User(id=USER_ID) + monkeypatch.setattr("app.crud.get_chat_for_member", AsyncMock(return_value=SimpleNamespace(id=uuid.UUID(CHAT_ID)))) + send = AsyncMock() + monkeypatch.setattr("app.crud.create_message_idempotent", send) + headers = {} if key is None else {"Idempotency-Key": key} + reply = client.post(f"/api/v1/chats/{CHAT_ID}/messages", json={"text": "hi"}, headers=headers) + assert reply.status_code == 400 + send.assert_not_awaited() + + +def test_message_normalization_and_conflict(app_client, monkeypatch): + from app.crud import IdempotencyConflict + client, session = app_client + client.app.dependency_overrides[current_user] = lambda: User(id=USER_ID) + monkeypatch.setattr("app.crud.get_chat_for_member", AsyncMock(return_value=SimpleNamespace(id=uuid.UUID(CHAT_ID)))) + send = AsyncMock(side_effect=IdempotencyConflict()) + monkeypatch.setattr("app.crud.create_message_idempotent", send) + for body in ({"text": "hi"}, {"attachment_ids": [], "text": "hi"}): + reply = client.post(f"/api/v1/chats/{CHAT_ID}/messages", json=body, headers={"Idempotency-Key": "same"}) + assert reply.status_code == 409 + assert reply.json()["error"]["code"] == "conflict" + assert send.await_args_list[0] == send.await_args_list[1] + + +@pytest.mark.parametrize("path", ["/health/live", "/missing", "/api/v1/users/me"]) +def test_instance_header_matches_process_on_all_responses(app_client, path): + client, session = app_client + reply = client.get(path) + assert reply.headers["X-Instance-Id"] == client.app.state.instance_id + assert client.get(path).headers["X-Instance-Id"] == reply.headers["X-Instance-Id"] + + +def test_instance_alias_changes_between_applications(): + settings = Settings.from_env({"DATABASE_URL": "postgresql+asyncpg://t:t@localhost/t"}) + assert create_app(settings).state.instance_id != create_app(settings).state.instance_id + + +def test_sigterm_drops_readiness_and_requests_graceful_exit(): + import signal + import uvicorn + from app.__main__ import GracefulServer + app = SimpleNamespace(state=SimpleNamespace(ready=True)) + server = GracefulServer(uvicorn.Config(app)) + server.handle_exit(signal.SIGTERM, None) + assert app.state.ready is False + assert server.should_exit is True