From 8d001712fe40bed7b16b1d26683360299f13a8fb Mon Sep 17 00:00:00 2001 From: k00kzaAntonovaIra <148076030+k00kzaAntonovaIra@users.noreply.github.com> Date: Fri, 6 Nov 2026 13:57:29 +0300 Subject: [PATCH 1/2] solution --- .env.example | 30 +- Dockerfile | 41 ++ Makefile | 31 +- REPORT.md | 94 +++- app/__init__.py | 1 + app/__main__.py | 48 ++ app/attachments.py | 211 ++++++++ app/auth.py | 103 ++++ app/cache.py | 148 ++++++ app/cleanup.py | 80 +++ app/config.py | 122 +++++ app/crud.py | 262 ++++++++++ app/cursor.py | 51 ++ app/db.py | 27 ++ app/errors.py | 37 ++ app/explain.py | 139 ++++++ app/healthcheck.py | 20 + app/http_utils.py | 113 +++++ app/logging_setup.py | 25 + app/main.py | 128 +++++ app/middleware.py | 71 +++ app/migrate.py | 75 +++ app/migrations/0001_init.sql | 41 ++ app/migrations/0002_message_requests.sql | 10 + app/migrations/0003_attachments.sql | 26 + app/migrations/0004_attachment_timestamps.sql | 2 + app/models.py | 104 ++++ app/routes.py | 306 ++++++++++++ app/schemas.py | 188 ++++++++ app/security.py | 74 +++ app/storage.py | 85 ++++ app/timeutil.py | 23 + compose.example.yaml | 6 +- compose.yaml | 218 +++++++++ evidence/README.md | 9 +- .../lab2-history/01_check_contract.txt | 8 + evidence/archive/lab2-history/02_smoke.txt | 9 + .../archive/lab2-history/03_ps_before.txt | 11 + .../archive/lab2-history/03_volume_before.txt | 8 + .../archive/lab2-history/04_persistence.txt | 10 + evidence/archive/lab2-history/05_ps_after.txt | 11 + .../archive/lab2-history/05_volume_after.txt | 8 + evidence/archive/lab2-history/06_schema.txt | 83 ++++ .../archive/lab2-history/07_migrations.txt | 8 + .../lab2-history/07_migrations_first.txt | 16 + .../lab2-history/07_migrations_table.txt | 11 + evidence/archive/lab2-history/08_hashes.txt | 20 + .../archive/lab2-history/09_auth_tests.txt | 64 +++ .../lab2-history/10_database_tests.txt | 45 ++ .../archive/lab2-history/11_all_tests.txt | 179 +++++++ .../lab2-history/12_compose_no_secrets.txt | 10 + .../archive/lab2-history/12_git_files.txt | 113 +++++ .../archive/lab2-history/12_git_secrets.txt | 8 + evidence/archive/lab2-history/13_storage.txt | 194 ++++++++ .../archive/lab2-history/14_dependencies.txt | 323 +++++++++++++ evidence/archive/lab2-history/15_explain.txt | 89 ++++ .../archive/lab2-history/15_explain_tests.txt | 28 ++ .../archive/lab2-history/16_expiry_live.txt | 19 + .../lab2-history/17_mr_regressions.txt | 44 ++ .../archive/lab3-run2/00_environment.json | 8 + evidence/archive/lab3-run2/01_docker.txt | 30 ++ .../archive/lab3-run2/02_compose_config.txt | 3 + .../archive/lab3-run2/03_container_limits.txt | 8 + evidence/archive/lab3-run2/03_limits.txt | 8 + evidence/archive/lab3-run2/03_processes.json | 20 + evidence/archive/lab3-run2/04_smoke.txt | 4 + .../lab3-run2/05_concurrent_idempotency.json | 10 + .../archive/lab3-run2/06_failover_api-a.json | 108 +++++ .../archive/lab3-run2/06_failover_api-b.json | 108 +++++ .../archive/lab3-run2/07_public_failover.txt | 6 + .../archive/lab3-run2/08_rolling_api-a.json | 114 +++++ .../archive/lab3-run2/08_rolling_api-b.json | 114 +++++ evidence/archive/lab3-run2/09_persistence.txt | 5 + .../archive/lab3-run2/10_processes_after.json | 20 + evidence/archive/lab3-run2/11_nginx.txt | 47 ++ .../archive/lab3-run2/12_student_tests.txt | 303 ++++++++++++ evidence/archive/lab3-run2/result.json | 4 + evidence/archive/lab3/00_environment.json | 8 + evidence/archive/lab3/01_docker.txt | 30 ++ evidence/archive/lab3/02_compose_config.txt | 3 + evidence/archive/lab3/03_container_limits.txt | 8 + evidence/archive/lab3/03_limits.txt | 8 + evidence/archive/lab3/03_processes.json | 20 + evidence/archive/lab3/04_smoke.txt | 4 + .../lab3/05_concurrent_idempotency.json | 10 + evidence/archive/lab3/06_failover_api-a.json | 108 +++++ evidence/archive/lab3/06_failover_api-b.json | 108 +++++ evidence/archive/lab3/07_public_failover.txt | 6 + evidence/archive/lab3/08_rolling_api-a.json | 114 +++++ evidence/archive/lab3/08_rolling_api-b.json | 114 +++++ evidence/archive/lab3/09_persistence.txt | 5 + evidence/archive/lab3/10_processes_after.json | 20 + evidence/archive/lab3/11_nginx.txt | 47 ++ evidence/archive/lab3/12_student_tests.txt | 303 ++++++++++++ evidence/archive/lab3/result.json | 4 + evidence/archive/local/01_check.txt | 4 + evidence/archive/local/02_openapi.txt | 4 + evidence/archive/local/03_static_lab3.txt | 4 + evidence/archive/local/04_unit.txt | 11 + evidence/archive/local/05_collection.txt | 237 +++++++++ evidence/archive/local/06_dependencies.txt | 4 + evidence/archive/local/manifest.json | 72 +++ evidence/archive/local/runtime_status.json | 7 + evidence/lab4.zip | Bin 0 -> 30304 bytes evidence/lab4/00_compose_config.txt | 3 + evidence/lab4/00_docker.txt | 30 ++ evidence/lab4/00_environment.json | 9 + evidence/lab4/01_cluster.txt | 30 ++ evidence/lab4/01_public_smoke.txt | 4 + evidence/lab4/02_attachment_contract.json | 10 + evidence/lab4/03_uploader_failover.json | 10 + evidence/lab4/04_full_restart.json | 48 ++ evidence/lab4/04_restart_command.txt | 73 +++ evidence/lab4/05_s3_outage.json | 67 +++ evidence/lab4/06_bootstrap_repeat_1.txt | 4 + evidence/lab4/06_bootstrap_repeat_2.txt | 4 + evidence/lab4/06_private_bucket.json | 15 + evidence/lab4/07_concurrent_uploads.json | 206 ++++++++ evidence/lab4/08_cleanup_dry_run.txt | 4 + evidence/lab4/09_student_tests.txt | 455 +++++++++++++++++ evidence/lab4/10_final_processes.json | 20 + evidence/lab4/cluster/00_environment.json | 8 + evidence/lab4/cluster/01_docker.txt | 30 ++ evidence/lab4/cluster/02_compose_config.txt | 3 + evidence/lab4/cluster/03_container_limits.txt | 10 + evidence/lab4/cluster/03_limits.txt | 10 + evidence/lab4/cluster/03_processes.json | 20 + evidence/lab4/cluster/04_smoke.txt | 4 + .../cluster/05_concurrent_idempotency.json | 10 + evidence/lab4/cluster/06_failover_api-a.json | 108 +++++ evidence/lab4/cluster/06_failover_api-b.json | 108 +++++ evidence/lab4/cluster/07_public_failover.txt | 6 + evidence/lab4/cluster/08_rolling_api-a.json | 114 +++++ evidence/lab4/cluster/08_rolling_api-b.json | 114 +++++ evidence/lab4/cluster/09_persistence.txt | 5 + evidence/lab4/cluster/10_processes_after.json | 20 + evidence/lab4/cluster/11_nginx.txt | 76 +++ evidence/lab4/cluster/12_student_tests.txt | 456 ++++++++++++++++++ evidence/lab4/cluster/result.json | 4 + evidence/lab4/result.json | 6 + infra/README.md | 8 +- infra/minio/Dockerfile | 35 ++ infra/minio/bootstrap.py | 77 +++ infra/minio/healthcheck.py | 11 + infra/nginx/entrypoint.sh | 45 ++ infra/nginx/nginx.conf | 62 +++ lab.json | 2 +- pytest.ini | 4 + requirements-dev.txt | 1 + requirements-test.txt | 10 + requirements.txt | 31 ++ scripts/check_contract.py | 88 ++-- scripts/check_expiry.py | 90 ++++ scripts/check_stack.py | 59 +++ scripts/cluster_evidence.py | 428 ++++++++++++++++ scripts/collect_evidence.sh | 4 + scripts/compare_contracts.py | 55 +++ scripts/evidence_utils.py | 171 +++++++ scripts/init_env.py | 70 +++ scripts/lab4_evidence.py | 389 +++++++++++++++ scripts/local_evidence.py | 47 ++ scripts/manual_attachment_checks.py | 93 ++++ scripts/replicas.py | 173 +++++++ scripts/restart.py | 16 + scripts/restart.sh | 4 + scripts/run_and_save.py | 64 +++ scripts/show_instances.py | 30 ++ scripts/stop-one.sh | 4 + scripts/stop_one.py | 12 + student-tests/conftest.py | 274 +++++++++++ student-tests/test_attachment_failures.py | 121 +++++ student-tests/test_attachments.py | 240 +++++++++ student-tests/test_auth.py | 193 ++++++++ student-tests/test_cache.py | 258 ++++++++++ student-tests/test_concurrency.py | 61 +++ student-tests/test_contract_basics.py | 95 ++++ student-tests/test_database.py | 173 +++++++ student-tests/test_db_failure.py | 78 +++ student-tests/test_duplicate_username.py | 45 ++ student-tests/test_empty_message.py | 74 +++ student-tests/test_explain.py | 52 ++ student-tests/test_foreign_chat.py | 65 +++ student-tests/test_idempotency.py | 122 +++++ student-tests/test_invalid_json.py | 115 +++++ student-tests/test_pagination.py | 193 ++++++++ student-tests/test_session_expiry.py | 56 +++ student-tests/test_transactions.py | 69 +++ tests/README.md | 2 + tests/client.py | 372 +++++++++----- tests/resilience.py | 16 +- unit-tests/test_attachment_intent.py | 27 ++ unit-tests/test_message_transaction.py | 70 +++ unit-tests/test_mr_regressions.py | 151 ++++++ unit-tests/test_operator_scripts.py | 91 ++++ 194 files changed, 13448 insertions(+), 203 deletions(-) create mode 100644 Dockerfile create mode 100644 app/__init__.py create mode 100644 app/__main__.py create mode 100644 app/attachments.py create mode 100644 app/auth.py create mode 100644 app/cache.py create mode 100644 app/cleanup.py create mode 100644 app/config.py create mode 100644 app/crud.py create mode 100644 app/cursor.py create mode 100644 app/db.py create mode 100644 app/errors.py create mode 100644 app/explain.py create mode 100644 app/healthcheck.py create mode 100644 app/http_utils.py create mode 100644 app/logging_setup.py create mode 100644 app/main.py create mode 100644 app/middleware.py create mode 100644 app/migrate.py create mode 100644 app/migrations/0001_init.sql create mode 100644 app/migrations/0002_message_requests.sql create mode 100644 app/migrations/0003_attachments.sql create mode 100644 app/migrations/0004_attachment_timestamps.sql create mode 100644 app/models.py create mode 100644 app/routes.py create mode 100644 app/schemas.py create mode 100644 app/security.py create mode 100644 app/storage.py create mode 100644 app/timeutil.py create mode 100644 compose.yaml create mode 100644 evidence/archive/lab2-history/01_check_contract.txt create mode 100644 evidence/archive/lab2-history/02_smoke.txt create mode 100644 evidence/archive/lab2-history/03_ps_before.txt create mode 100644 evidence/archive/lab2-history/03_volume_before.txt create mode 100644 evidence/archive/lab2-history/04_persistence.txt create mode 100644 evidence/archive/lab2-history/05_ps_after.txt create mode 100644 evidence/archive/lab2-history/05_volume_after.txt create mode 100644 evidence/archive/lab2-history/06_schema.txt create mode 100644 evidence/archive/lab2-history/07_migrations.txt create mode 100644 evidence/archive/lab2-history/07_migrations_first.txt create mode 100644 evidence/archive/lab2-history/07_migrations_table.txt create mode 100644 evidence/archive/lab2-history/08_hashes.txt create mode 100644 evidence/archive/lab2-history/09_auth_tests.txt create mode 100644 evidence/archive/lab2-history/10_database_tests.txt create mode 100644 evidence/archive/lab2-history/11_all_tests.txt create mode 100644 evidence/archive/lab2-history/12_compose_no_secrets.txt create mode 100644 evidence/archive/lab2-history/12_git_files.txt create mode 100644 evidence/archive/lab2-history/12_git_secrets.txt create mode 100644 evidence/archive/lab2-history/13_storage.txt create mode 100644 evidence/archive/lab2-history/14_dependencies.txt create mode 100644 evidence/archive/lab2-history/15_explain.txt create mode 100644 evidence/archive/lab2-history/15_explain_tests.txt create mode 100644 evidence/archive/lab2-history/16_expiry_live.txt create mode 100644 evidence/archive/lab2-history/17_mr_regressions.txt create mode 100644 evidence/archive/lab3-run2/00_environment.json create mode 100644 evidence/archive/lab3-run2/01_docker.txt create mode 100644 evidence/archive/lab3-run2/02_compose_config.txt create mode 100644 evidence/archive/lab3-run2/03_container_limits.txt create mode 100644 evidence/archive/lab3-run2/03_limits.txt create mode 100644 evidence/archive/lab3-run2/03_processes.json create mode 100644 evidence/archive/lab3-run2/04_smoke.txt create mode 100644 evidence/archive/lab3-run2/05_concurrent_idempotency.json create mode 100644 evidence/archive/lab3-run2/06_failover_api-a.json create mode 100644 evidence/archive/lab3-run2/06_failover_api-b.json create mode 100644 evidence/archive/lab3-run2/07_public_failover.txt create mode 100644 evidence/archive/lab3-run2/08_rolling_api-a.json create mode 100644 evidence/archive/lab3-run2/08_rolling_api-b.json create mode 100644 evidence/archive/lab3-run2/09_persistence.txt create mode 100644 evidence/archive/lab3-run2/10_processes_after.json create mode 100644 evidence/archive/lab3-run2/11_nginx.txt create mode 100644 evidence/archive/lab3-run2/12_student_tests.txt create mode 100644 evidence/archive/lab3-run2/result.json create mode 100644 evidence/archive/lab3/00_environment.json create mode 100644 evidence/archive/lab3/01_docker.txt create mode 100644 evidence/archive/lab3/02_compose_config.txt create mode 100644 evidence/archive/lab3/03_container_limits.txt create mode 100644 evidence/archive/lab3/03_limits.txt create mode 100644 evidence/archive/lab3/03_processes.json create mode 100644 evidence/archive/lab3/04_smoke.txt create mode 100644 evidence/archive/lab3/05_concurrent_idempotency.json create mode 100644 evidence/archive/lab3/06_failover_api-a.json create mode 100644 evidence/archive/lab3/06_failover_api-b.json create mode 100644 evidence/archive/lab3/07_public_failover.txt create mode 100644 evidence/archive/lab3/08_rolling_api-a.json create mode 100644 evidence/archive/lab3/08_rolling_api-b.json create mode 100644 evidence/archive/lab3/09_persistence.txt create mode 100644 evidence/archive/lab3/10_processes_after.json create mode 100644 evidence/archive/lab3/11_nginx.txt create mode 100644 evidence/archive/lab3/12_student_tests.txt create mode 100644 evidence/archive/lab3/result.json create mode 100644 evidence/archive/local/01_check.txt create mode 100644 evidence/archive/local/02_openapi.txt create mode 100644 evidence/archive/local/03_static_lab3.txt create mode 100644 evidence/archive/local/04_unit.txt create mode 100644 evidence/archive/local/05_collection.txt create mode 100644 evidence/archive/local/06_dependencies.txt create mode 100644 evidence/archive/local/manifest.json create mode 100644 evidence/archive/local/runtime_status.json create mode 100644 evidence/lab4.zip create mode 100644 evidence/lab4/00_compose_config.txt create mode 100644 evidence/lab4/00_docker.txt create mode 100644 evidence/lab4/00_environment.json create mode 100644 evidence/lab4/01_cluster.txt create mode 100644 evidence/lab4/01_public_smoke.txt create mode 100644 evidence/lab4/02_attachment_contract.json create mode 100644 evidence/lab4/03_uploader_failover.json create mode 100644 evidence/lab4/04_full_restart.json create mode 100644 evidence/lab4/04_restart_command.txt create mode 100644 evidence/lab4/05_s3_outage.json create mode 100644 evidence/lab4/06_bootstrap_repeat_1.txt create mode 100644 evidence/lab4/06_bootstrap_repeat_2.txt create mode 100644 evidence/lab4/06_private_bucket.json create mode 100644 evidence/lab4/07_concurrent_uploads.json create mode 100644 evidence/lab4/08_cleanup_dry_run.txt create mode 100644 evidence/lab4/09_student_tests.txt create mode 100644 evidence/lab4/10_final_processes.json create mode 100644 evidence/lab4/cluster/00_environment.json create mode 100644 evidence/lab4/cluster/01_docker.txt create mode 100644 evidence/lab4/cluster/02_compose_config.txt create mode 100644 evidence/lab4/cluster/03_container_limits.txt create mode 100644 evidence/lab4/cluster/03_limits.txt create mode 100644 evidence/lab4/cluster/03_processes.json create mode 100644 evidence/lab4/cluster/04_smoke.txt create mode 100644 evidence/lab4/cluster/05_concurrent_idempotency.json create mode 100644 evidence/lab4/cluster/06_failover_api-a.json create mode 100644 evidence/lab4/cluster/06_failover_api-b.json create mode 100644 evidence/lab4/cluster/07_public_failover.txt create mode 100644 evidence/lab4/cluster/08_rolling_api-a.json create mode 100644 evidence/lab4/cluster/08_rolling_api-b.json create mode 100644 evidence/lab4/cluster/09_persistence.txt create mode 100644 evidence/lab4/cluster/10_processes_after.json create mode 100644 evidence/lab4/cluster/11_nginx.txt create mode 100644 evidence/lab4/cluster/12_student_tests.txt create mode 100644 evidence/lab4/cluster/result.json create mode 100644 evidence/lab4/result.json create mode 100644 infra/minio/Dockerfile create mode 100644 infra/minio/bootstrap.py create mode 100644 infra/minio/healthcheck.py create mode 100644 infra/nginx/entrypoint.sh create mode 100644 infra/nginx/nginx.conf create mode 100644 pytest.ini create mode 100644 requirements-test.txt create mode 100644 requirements.txt create mode 100644 scripts/check_expiry.py create mode 100644 scripts/check_stack.py create mode 100644 scripts/cluster_evidence.py create mode 100644 scripts/collect_evidence.sh create mode 100644 scripts/compare_contracts.py create mode 100644 scripts/evidence_utils.py create mode 100644 scripts/init_env.py create mode 100644 scripts/lab4_evidence.py create mode 100644 scripts/local_evidence.py create mode 100644 scripts/manual_attachment_checks.py create mode 100644 scripts/replicas.py create mode 100644 scripts/restart.py create mode 100644 scripts/restart.sh create mode 100644 scripts/run_and_save.py create mode 100644 scripts/show_instances.py create mode 100644 scripts/stop-one.sh create mode 100644 scripts/stop_one.py create mode 100644 student-tests/conftest.py create mode 100644 student-tests/test_attachment_failures.py create mode 100644 student-tests/test_attachments.py create mode 100644 student-tests/test_auth.py create mode 100644 student-tests/test_cache.py create mode 100644 student-tests/test_concurrency.py create mode 100644 student-tests/test_contract_basics.py create mode 100644 student-tests/test_database.py create mode 100644 student-tests/test_db_failure.py create mode 100644 student-tests/test_duplicate_username.py create mode 100644 student-tests/test_empty_message.py create mode 100644 student-tests/test_explain.py create mode 100644 student-tests/test_foreign_chat.py create mode 100644 student-tests/test_idempotency.py create mode 100644 student-tests/test_invalid_json.py create mode 100644 student-tests/test_pagination.py create mode 100644 student-tests/test_session_expiry.py create mode 100644 student-tests/test_transactions.py create mode 100644 unit-tests/test_attachment_intent.py create mode 100644 unit-tests/test_message_transaction.py create mode 100644 unit-tests/test_mr_regressions.py create mode 100644 unit-tests/test_operator_scripts.py diff --git a/.env.example b/.env.example index d99d26a2..6ad832f6 100644 --- a/.env.example +++ b/.env.example @@ -1,20 +1,12 @@ -# Copy to .env for your LOCAL synthetic test environment; never commit .env. -# These are logical interface suggestions; map actual framework names in REPORT.md. -APP_HOST=0.0.0.0 -APP_PORT=8080 -APP_ENV=lab +POSTGRES_USER=messenger +POSTGRES_PASSWORD=CHANGE_ME_TO_RANDOM_ASCII_PASSWORD +POSTGRES_DB=messenger SESSION_TTL_SECONDS=3600 -# Choose credentials locally. File paths contain no secret values. -DATABASE_URL_FILE=/run/secrets/database_url -# Optional on 3, required use on 4 in lab2; Redis OR Valkey: -SESSION_STORE=postgres -CACHE_URL_FILE=/run/secrets/cache_url -API_REPLICAS=2 -IDEMPOTENCY_TTL_SECONDS=86400 -S3_ENDPOINT=http://s3:9000 -S3_REGION=us-east-1 -S3_BUCKET=messenger-attachments -S3_ACCESS_KEY_ID_FILE=/run/secrets/s3_access_key_id -S3_SECRET_ACCESS_KEY_FILE=/run/secrets/s3_secret_access_key -MAX_UPLOAD_BYTES=10485760 -MAX_IMAGE_PIXELS=40000000 +CACHE_URL=redis://redis:6379/0 +LOG_LEVEL=WARNING +API_IMAGE=mtusi-messenger:lab4 +MINIO_ROOT_USER=minio-root +MINIO_ROOT_PASSWORD=CHANGE_ME_TO_RANDOM_MINIO_ROOT_PASSWORD +S3_BUCKET=attachments +S3_ACCESS_KEY=CHANGE_ME_TO_SEPARATE_API_ACCESS_KEY +S3_SECRET_KEY=CHANGE_ME_TO_RANDOM_API_SECRET_KEY diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..864335d5 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,41 @@ +FROM python:3.12.8-slim-bookworm AS base + +ENV PYTHONDONTWRITEBYTECODE=1 \ + PYTHONUNBUFFERED=1 \ + PIP_NO_CACHE_DIR=1 \ + PIP_DISABLE_PIP_VERSION_CHECK=1 \ + PIP_ROOT_USER_ACTION=ignore \ + APP_HOST=0.0.0.0 \ + APP_PORT=8080 \ + APP_ENV=lab + +RUN groupadd --system --gid 10001 app \ + && useradd --system --uid 10001 --gid app --no-create-home --shell /usr/sbin/nologin app + +WORKDIR /srv + +COPY requirements.txt ./ +RUN pip install -r requirements.txt + +COPY app ./app + +FROM base AS test +COPY requirements-test.txt ./ +RUN pip install -r requirements-test.txt +COPY pytest.ini ./ +COPY student-tests ./student-tests +COPY unit-tests ./unit-tests +COPY scripts ./scripts +COPY tests ./tests +COPY lab.json ./ +COPY contracts ./contracts +USER app +CMD ["python", "-m", "pytest", "-v", "--no-header"] + +FROM base AS runtime +USER app +EXPOSE 8080 +HEALTHCHECK --interval=10s --timeout=3s --start-period=5s --retries=3 \ + CMD ["python", "-m", "app.healthcheck"] +STOPSIGNAL SIGTERM +CMD ["python", "-m", "app"] diff --git a/Makefile b/Makefile index 5d86d171..b28939ac 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 lab4-evidence help: @echo "check: offline structure; validate: full OpenAPI (requirements-dev.txt); test: running API; load: synthetic writes" @@ -14,6 +14,7 @@ check: validate: $(PYTHON) scripts/validate_openapi.py + $(PYTHON) scripts/check_stack.py test: $(PYTHON) tests/smoke.py --base-url "$(BASE_URL)" $(TLS_ARGS) @@ -25,6 +26,32 @@ test-persistence: @test -n "$(ACTION_SCRIPT)" || (echo "Set ACTION_SCRIPT to your implemented executable restart hook"; exit 2) $(PYTHON) tests/resilience.py --mode persistence --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS) +COMPOSE = docker compose --env-file .env + +up: + $(COMPOSE) up -d --build + +test-student: + $(COMPOSE) --profile test run --rm --build tests + +explain: + $(COMPOSE) exec -T api-a python -m app.explain + +evidence: + scripts/collect_evidence.sh + test-failover: - @test -n "$(ACTION_SCRIPT)" || (echo "Set ACTION_SCRIPT to your implemented executable stop-one hook"; exit 2) + @test -n "$(ACTION_SCRIPT)" || (echo "Set ACTION_SCRIPT=scripts/stop-one.sh"; exit 2) $(PYTHON) tests/resilience.py --mode failover --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS) + +replicas: + $(PYTHON) scripts/replicas.py status + +restore: + $(PYTHON) scripts/replicas.py restore + +rolling: + $(PYTHON) scripts/replicas.py rolling + +lab4-evidence: + $(PYTHON) scripts/lab4_evidence.py --base-url "$(BASE_URL)" diff --git a/REPORT.md b/REPORT.md index 9ad2b84f..e7debe05 100644 --- a/REPORT.md +++ b/REPORT.md @@ -1,37 +1,99 @@ # Отчёт — лабораторная 4 -- Автор, группа: TODO -- Целевая оценка; выбранный трек 5, если нужен: TODO -- Commit/PR: TODO после публикации -- Использованные источники, библиотеки и помощь ИИ: TODO +- Автор, группа: Антонова Ирина Андреевна, БПИ2301. +- Целевая оценка: 4; Compose + Nginx, две API-реплики, MinIO как S3-хранилище. Трек 5 не заявлен. +- Commit/PR: ⟦хеш коммита / ссылка на PR — заполнить после commit и push⟧ +- Использованные источники, библиотеки и помощь ИИ: FastAPI, SQLAlchemy async, asyncpg, Pydantic, Uvicorn, redis-py, minio-py, Pillow, pytest. lab2 подготовлена с Claude, перенос lab3 — с Codex; основной код lab4 написан с Codex; правки выполнены с Claude. Основа: lab3 без изменений бизнес-логики. ## Запуск из чистого clone -TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных. +Образы: `nginx:1.28.0-alpine`, Python 3.12.8, `postgres:16.6-bookworm`, `redis:7.4.1-bookworm`, MinIO `RELEASE.2025-10-15T17-29-55Z` и mc `RELEASE.2025-08-13T08-35-41Z` (собираются из исходников, `infra/minio/Dockerfile`). На хосте нужны Docker с Compose v2 и Python 3.9+ (прогоны — Windows 10, Docker Desktop, Python 3.11.9). + +```sh +python scripts/init_env.py # создаёт .env со случайными секретами, значения не выводит +docker compose --env-file .env config --quiet +docker compose --env-file .env up -d --build +python scripts/replicas.py restore # ждёт готовности обеих API через LB +make test # публичный smoke +docker compose --env-file .env --profile test run --rm --build tests +python scripts/lab4_evidence.py --base-url "http://127.0.0.1:8080" +``` + +На Windows клиентские запросы нужно слать на `127.0.0.1`, а не `localhost`: через `localhost` каждый запрос задерживается на ≈ 2 с из-за попытки IPv6 (замер `/health/live`: 2147 мс против 72 мс). Перед сбором: `$env:BASE_URL = "http://127.0.0.1:8080"`. + +Порядок старта: `db` → `migrate` (код 0); `minio` → `minio-init` (код 0) → `api-a`, `api-b`, `cleanup` → `nginx`. `.env` создаёт оператор через `init_env.py` (пароль БД, root-учётные данные MinIO и отдельные `S3_ACCESS_KEY`/`S3_SECRET_KEY` для API); файл в `.gitignore`. + +Безопасная остановка (тома `pgdata` и `miniodata` сохраняются): `docker compose --env-file .env stop`. Намеренное удаление данных: `docker compose --env-file .env down -v`. ## Архитектура и решения -TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab3, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации. +Клиент → Nginx (`127.0.0.1:8080`, единственный опубликованный порт) → `api-a`/`api-b` → PostgreSQL (метаданные) и приватный bucket MinIO (байты); Redis — необязательный кэш; `cleanup` — служба сверки. БД, Redis, MinIO и cleanup находятся в `internal`-сети; консоль MinIO выключена и порт не публикуется. + +- **Источник истины.** PostgreSQL: пользователи, сессии, чаты, сообщения и таблица `attachments` (миграции `0003`, `0004`): `owner_id`, `chat_id`, `filename` (только для отображения), `object_key` (UNIQUE), `content_type` (PNG/JPEG), `size_bytes` ≤ 10 MiB, `sha256`, `width`/`height` (CHECK ≤ 40 млн пикселей), `status` (`uploading`/`ready`). Байты — только в S3; у API нет томов, временный буфер загрузки — `TemporaryFile`. +- **Object key** назначает сервер (`originals/`); опасные `filename` отклоняются (400). +- **Upload.** Проверка сессии и членства → проверка `Content-Type` (415) и `Content-Length` до чтения (413) → слоты: 2 загрузки и 1 декодирование на реплику (ожидание 30 с, иначе 503) → потоковый приём в файл с лимитом 10 MiB и таймаутом 30 с → проверка Pillow: размеры до декодирования, формат совпадает с MIME (415), анимация отклоняется, `verify()` + `load()` для битых файлов (415). Пользовательские ошибки не дают 500. +- **Транзакционные границы.** COMMIT строки `uploading` («намерение») → `SELECT … FOR UPDATE` → единственный S3 PUT → `ready` + COMMIT → `201`. Если PUT прошёл, а COMMIT нет, объект остаётся учтённым строкой `uploading`. +- **Download.** Только через API, стрим блоками 64 KiB; участникам чата — файл, постороннему — 404 (метаданные и байты). Приватный bucket и случайный ключ не заменяют ACL приложения: права проверяет API. +- **Привязка.** `POST /messages` принимает `attachment_ids` (до 10): в одной транзакции проверяются существование, тот же чат и статус `ready`; идемпотентность lab3 сохранена (повтор с другим списком — 409). +- **Компенсация.** Служба `cleanup` (`python -m app.cleanup --loop`, период 60 с) удаляет только строки `uploading` старше grace period 3600 с (`FOR UPDATE SKIP LOCKED`, пачки по 100): сначала объект, затем строка; при недоступном S3 повтор в следующем цикле. Загрузчик держит блокировку строки на время PUT, поэтому активные загрузки не удаляются; `ready` не удаляется никогда. Есть `--dry-run` и `--once`. Обход bucket для поиска объектов без строки не нужен (намерение фиксируется до PUT) и не реализован. +- **Безопасность S3.** `minio-init` идемпотентен: `mb --ignore-existing`, `anonymous set none`, политика и отдельный пользователь API: `ListBucket` на bucket, `Get/Put/DeleteObject` только на `originals/*`. Root-ключи API не получают, секреты ≥ 32 символов проверяются. +- **Nginx.** Для маршрута вложений: `client_max_body_size 11m`, `proxy_request_buffering off`, `proxy_next_upstream off` (бинарный POST без ключа идемпотентности не повторяется). +- **Из lab3 перенесено:** две реплики, общие сессии в PostgreSQL, идемпотентность `message_requests`, службы `migrate`, скрипты failover/rolling/restart. **Изменено:** добавлены MinIO, `minio-init`, `cleanup`, `attachments`, `attachment_ids`, проверки изображений и слоты загрузки; readiness зависит от S3; лимит памяти API — 512 MiB. Все переменные `.env.example` используются в `compose.yaml` (`POSTGRES_*`, `SESSION_TTL_SECONDS`, `CACHE_URL`, `LOG_LEVEL`, `API_IMAGE`, `MINIO_ROOT_*`, `S3_BUCKET`, `S3_ACCESS_KEY`/`S3_SECRET_KEY`); `S3_ENDPOINT_URL` и параметры cleanup заданы в `compose.yaml`. ## Доказательства критериев +Прогон `python scripts/lab4_evidence.py --base-url http://127.0.0.1:8080`, 2026-11-05 21:25–21:34 UTC, Windows 10, Docker Desktop. `result.json`: `status`, `cluster_checks`, `student_tests` — PASS. Файлы — в `evidence/lab4/`; проверки lab3 — в `evidence/lab4/cluster/`. + | Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства | | --- | --- | --- | --- | -| Публичный smoke | make test | TODO | TODO | -| Остальные пункты уровня 3 | TODO | TODO | TODO | -| Все пункты уровня 4, если заявлен | TODO | TODO | TODO | -| Выбранный трек 5, если заявлен | TODO | TODO | TODO | - -TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены. +| Публичный smoke | `make test` | PASS, 1.8 с | `01_public_smoke.txt` | +| Lab3 сохранён: тесты и идемпотентность | `docker compose … run --rm --build tests`; `cluster_evidence.py` | 279 passed; 12 параллельных запросов с одним ключом → 1 сообщение, 1 строка истории, 2 реплики, 409 при другом теле | `09_student_tests.txt`, `cluster/05_concurrent_idempotency.json` | +| Lab3 сохранён: failover, rolling, persistence | `cluster_evidence.py`, `tests/resilience.py` | отказ `api-a`/`api-b` по очереди: 618/618 и 572/572 без ошибок, дубликатов 0, чтение/запись после отказа 7.75/7.84 с и 8.53/8.42 с (≤ 10 с); публичный failover PASS (2 реплики, 92 записи, 0 ошибок, 6.67 с); rolling `api-a`/`api-b`: 1100/1100 и 1036/1036 без ошибок, дубликатов 0; persistence PASS | `cluster/06_failover_*.json`, `07_public_failover.txt`, `08_rolling_*.json`, `09_persistence.txt` | +| Метаданные в PostgreSQL, байты в приватном bucket, не на API | схема, `compose.yaml` | `attachments` без байтов; у API нет томов; один PUT в S3 | `app/migrations/0003_attachments.sql`, `compose.yaml` | +| 201 ready, побайтовое скачивание, Message с `attachment_ids`, чужой без доступа | `lab4_evidence.py` | 111 817 байт, SHA-256 совпал, сообщение видно участникам, посторонний: 404 для метаданных и байтов | `02_attachment_contract.json` | +| Только реальные PNG/JPEG ≤ 10 MiB и ≤ 40 млн пикселей, MIME по содержимому, опасные filename | pytest | файл ровно 10 MiB принят; лимит байт с `Content-Length` и без → 413; лимит пикселей до декодирования; 8+ опасных имён → 400; подмена MIME → 415 | `09_student_tests.txt` | +| Upload → остановка реплики → download через другую | `lab4_evidence.py` | загрузка через `api-b`, она `exited`/`pid=0`, скачивание через `api-a`, SHA-256 совпал, 6.97 с | `03_uploader_failover.json` | +| Restart с сохранением volumes | `scripts/restart.py` | все контейнеры пересозданы, прежний токен работает, сообщение, метаданные и байты равны | `04_full_restart.json` | +| **Ур. 4.** Большой файл, битое изображение, подмена MIME, чужое `attachment_id`, файл другого чата | pytest | 413 / 415 / 415 / 404 (сообщение не создаётся) / отклонено; ни одного 500 | `09_student_tests.txt` | +| Обрыв загрузки и сбой между S3 и БД | pytest с внедрением сбоев + `cleanup --dry-run` | строка `uploading` остаётся учтённой, свежая не удаляется, после grace (имитация 2 ч) удаляется вместе с объектом; потерянный COMMIT → файл остаётся `ready`; обрыв тела не создаёт ни строки, ни объекта; живой dry-run — 0 кандидатов | `09_student_tests.txt`, `08_cleanup_dry_run.txt` | +| Лимиты, потоковая обработка, ≥ 10 одновременных загрузок с пиком памяти | `lab4_evidence.py` | 10/10 успешно, прирост памяти 0.9 и 14.1 MiB из лимита 512 MiB (ниже) | `07_concurrent_uploads.json` | +| Идемпотентный bootstrap, отдельные ограниченные credentials, консоль закрыта, private bucket | `minio-init` дважды; `private_bucket` | оба запуска код 0; анонимный GET → 403; `AccessDenied` на чужой bucket, создание bucket и PUT вне `originals/`; root-ключей в API нет | `06_bootstrap_repeat_*.txt`, `06_private_bucket.json` | +| Трек 5 | Не заявлен | — | — | ## Эксперименты и отказы -TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения. +Окружение: один локальный хост (Windows 10, Docker Desktop, 16 CPU). Лимиты: API 0.5 CPU / 512 MiB, PostgreSQL и MinIO 1 CPU / 512 MiB. Данные синтетические. -| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных | +**Эксперимент 1. Одновременные загрузки и память API.** Гипотеза: потоковый приём в файл и слоты загрузки ограничивают память API независимо от числа клиентов. Методика: 10 клиентов стартуют по барьеру и загружают PNG 1024×683 (2 099 087 байт); память по cgroup и RSS снимается каждые 0.2 с. Результат: 10/10 успешно, 0.72 с, 13.9 загрузки/с; задержка 0.39 / 0.56 (медиана) / 0.72 с; все скачивания совпали с оригиналом. + +| Реплика | cgroup до | Пик | Прирост | RSS, пик | Лимит | +| --- | --- | --- | --- | --- | --- | +| api-a | 89.5 MiB | 90.4 MiB | 0.9 MiB | 96.7 MiB | 512 MiB | +| api-b | 78.2 MiB | 92.3 MiB | 14.1 MiB | 96.7 MiB | 512 MiB | + +| Воздействие | Что ожидаем | Что наблюдали | Восстановление / потеря данных | | --- | --- | --- | --- | -| TODO | TODO | TODO | TODO | +| Остановка реплики, принявшей загрузку | Файл скачивается через вторую | SHA-256 совпал | 6.97 с; потерь нет | +| Недоступность S3 | Контролируемая ошибка, не 500 | `ready`, upload и content → 503 `unavailable` с `request_id`; публичный `/health/ready` — 503; после возврата MinIO файл равен оригиналу | время восстановления не измерялось; потерь нет | +| Полный restart с сохранением томов | Токен и данные доступны | Сессия жива, метаданные и байты равны | restart ≈ 29 с; потерь нет | +| Сбой S3 / БД между PUT и COMMIT, потерянный COMMIT, обрыв клиента | Нет вечных orphan, `ready` и свежие не удаляются | Намерение удаляется вместе с объектом после grace; `ready` не трогается; при обрыве нет ни строки, ни объекта | grace ≥ 1 ч; пользовательские данные целы | +| Отказ реплики и rolling под нагрузкой (lab3) | Без ошибок клиента | 0 ошибок во всех прогонах (числа в таблице критериев); p50/p95 ≈ 47/172–188 мс; замена реплики 34.5 и 36.8 с | потерь нет | + +*Ограничения измерения:* один прогон каждого эксперимента; в нагрузочном тесте файл ≈ 2 MiB, а не 10 MiB; прогон длился 0.7 с, поэтому в выборку памяти попало мало точек, а cgroup включает файловый кэш; значения отражают локальный Docker. Время восстановления отсчитывается от начала действия и включает hook-скрипт (поиск контейнера и `docker compose stop --timeout 6`, ≈ 7.7–8.1 с), запас до порога 10 с небольшой. Таймер Windows ≈ 15.6 мс, поэтому задержки кратны этому шагу. + +**Изменения инструментов сбора.** Лимит ожидания команды остановки реплики в `scripts/cluster_evidence.py` увеличен с 10 до 60 с (на Docker Desktop она иногда не укладывалась); публичные тесты и пороги не менялись. Клиентские замеры идут через `127.0.0.1`. Ранние неудачные прогоны в доказательства не входят. ## Ограничения и следующие шаги -TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза. +**Выдерживается:** остановка одной API (файл доступен через другую); замена обеих реплик под нагрузкой; полный restart с прежним токеном; недоступность S3 (503, затем восстановление без потери данных); обрыв загрузки и сбой между S3 и БД без вечных неучтённых объектов. + +**Не выдерживается / упрощения:** отказ хоста; PostgreSQL и MinIO — единые точки отказа без репликации и backup; неудачная загрузка занимает место до истечения grace (≥ 1 ч); нет обхода bucket (потерянный файл при строке `ready` не обнаружится — трек 5.2); бинарный POST вложения автоматически не повторяется; память проверена на файле ≈ 2 MiB в одном прогоне, на 10 MiB — гипотеза; cleanup подтверждён тестами с внедрением сбоев, а живой dry-run нашёл 0 кандидатов; rolling проверен без смены версии образа, смешанный rolling со старым контрактом lab3 не поддерживается (изменён контракт сообщений); время восстановления после возврата S3 не измерялось. + +**Следующие шаги:** отказоустойчивый PostgreSQL и распределённый MinIO; reconciler с dry-run для объектов без метаданных и наоборот; presigned upload; метрики по возрасту записей `uploading`; нагрузочная проверка на файлах 10 MiB с повторами. Подтверждено экспериментами: ограничение памяти при 10 загрузках ≈ 2 MiB, поведение при недоступности S3, переключение при остановке реплики-загрузчика, failover, rolling и persistence; остальное — гипотезы. + +## Первичные источники + +- [S3 object uploads](https://docs.aws.amazon.com/AmazonS3/latest/userguide/upload-objects.html) +- [MinIO](https://github.com/minio/minio) +- [Nginx proxy_next_upstream](https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_next_upstream) +- [PostgreSQL: явные блокировки](https://www.postgresql.org/docs/16/explicit-locking.html) \ 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/attachments.py b/app/attachments.py new file mode 100644 index 00000000..8076d787 --- /dev/null +++ b/app/attachments.py @@ -0,0 +1,211 @@ +"""Synchronous binary uploads with bounded staging and durable database intents.""" + +import asyncio +import hashlib +import tempfile +import unicodedata +import uuid +import warnings +from urllib.parse import quote + +import anyio +from fastapi import APIRouter, Depends, Request +from fastapi.responses import StreamingResponse +from PIL import Image, UnidentifiedImageError +from sqlalchemy import select, func +from sqlalchemy.ext.asyncio import AsyncSession +from starlette.requests import ClientDisconnect + +from .auth import current_user, get_session +from .errors import ApiError +from .http_utils import bad_request, parse_uuid +from .models import Attachment, ChatMember, User +from .routes import open_chat +from .schemas import AttachmentOut +from .storage import MAX_BYTES, StorageUnavailable, unavailable +from .timeutil import format_ts + +router = APIRouter(prefix="/api/v1") +MAX_PIXELS = 40_000_000 +Image.MAX_IMAGE_PIXELS = MAX_PIXELS + + +def attachment_out(row): + return { + "id": str(row.id), "chat_id": str(row.chat_id), "owner_id": str(row.owner_id), + "filename": row.filename, "content_type": row.content_type, + "size_bytes": row.size_bytes, "sha256": row.sha256, + "status": row.status, + "variants": [], "error_code": None, "created_at": format_ts(row.created_at), + "updated_at": format_ts(row.updated_at), + } + + +def validate_filename(value): + if value is None or not 1 <= len(value) <= 255 or not value.strip(): + raise bad_request("filename: от 1 до 255 символов") + if any(char in "/\\" or unicodedata.category(char) in ("Cc", "Cs") for char in value): + raise bad_request("filename содержит путь или управляющие символы") + return value + + +def validate_image(stream, declared): + """Check dimensions before decode; verify CRC and actually decode pixel data.""" + try: + stream.seek(0) + with warnings.catch_warnings(): + warnings.simplefilter("error", Image.DecompressionBombWarning) + with Image.open(stream, formats=["PNG", "JPEG"]) as image: + width, height = image.size + if width * height > MAX_PIXELS: + raise ApiError(413, "payload_too_large", "Изображение больше 40 млн пикселей") + actual = {"PNG": "image/png", "JPEG": "image/jpeg"}[image.format] + if actual != declared: + raise ApiError(415, "unsupported_media_type", "MIME не соответствует содержимому") + # Animated images are not part of the single-image upload contract. + if getattr(image, "n_frames", 1) != 1: + raise ApiError(415, "unsupported_media_type", "Нужно одно изображение") + image.verify() + stream.seek(0) + with Image.open(stream, formats=["PNG", "JPEG"]) as image: + image.load() + return width, height + except (Image.DecompressionBombWarning, Image.DecompressionBombError): + raise ApiError(413, "payload_too_large", "Изображение больше 40 млн пикселей") from None + except (UnidentifiedImageError, OSError, SyntaxError, ValueError, EOFError): + raise ApiError(415, "unsupported_media_type", "Повреждённый PNG/JPEG") from None + finally: + stream.seek(0) + + +async def save_attachment(session, storage, row, stream): + """Commit intent BEFORE PUT; lock it until final ready commit. + + On failure keep the intent. A cleanup worker rechecks its state after grace; + never delete blindly after an ambiguous COMMIT acknowledgement. + """ + session.add(row) + await session.commit() + try: + locked = (await session.execute( + select(Attachment.id).where(Attachment.id == row.id).with_for_update() + )).scalar_one_or_none() + if locked is None: + raise ApiError(409, "conflict", "Срок незавершённой загрузки истёк") + await anyio.to_thread.run_sync(storage.put, row.object_key, stream, row.size_bytes, row.content_type) + row.status = "ready" + row.updated_at = (await session.execute(select(func.clock_timestamp()))).scalar_one() + await session.commit() + except BaseException: + await session.rollback() + raise + return row + + +@router.post("/chats/{chat_id}/attachments", status_code=201, response_model=AttachmentOut) +async def upload(chat_id: str, request: Request, user: User = Depends(current_user), + session: AsyncSession = Depends(get_session)): + chat = await open_chat(session, user, chat_id) + filename = validate_filename(request.query_params.get("filename")) + declared = request.headers.get("content-type", "").split(";", 1)[0].strip().lower() + if declared not in ("image/png", "image/jpeg"): + raise ApiError(415, "unsupported_media_type", "Нужен Content-Type image/png или image/jpeg") + length = request.headers.get("content-length") + if length is not None and (not length.isascii() or not length.isdigit()): + raise bad_request("Некорректный Content-Length") + if length is not None and (len(length) > 20 or int(length) > MAX_BYTES): + raise ApiError(413, "payload_too_large", "Файл больше 10 MiB") + chat_uuid, owner_uuid = chat.id, user.id + # Do not hold a DB connection during the client's network upload or slot wait. + await session.rollback() + storage = request.app.state.storage + if storage is None: + raise unavailable() + try: + await asyncio.wait_for(request.app.state.upload_slots.acquire(), timeout=30) + except TimeoutError: + raise ApiError(503, "unavailable", "Все слоты загрузки заняты") from None + try: + with tempfile.TemporaryFile(mode="w+b") as stream: + size, checksum = 0, hashlib.sha256() + try: + async with asyncio.timeout(30): + async for chunk in request.stream(): + size += len(chunk) + if size > MAX_BYTES: + raise ApiError(413, "payload_too_large", "Файл больше 10 MiB") + checksum.update(chunk) + stream.write(chunk) + except TimeoutError: + raise ApiError(408, "invalid_request", "Истекло время приёма файла") from None + except ClientDisconnect: + raise bad_request("Передача файла прервана") from None + if length is not None and int(length) != size: + raise bad_request("Неполное тело запроса") + async with request.app.state.decode_slot: + width, height = await anyio.to_thread.run_sync(validate_image, stream, declared) + row = Attachment( + id=uuid.uuid4(), chat_id=chat_uuid, owner_id=owner_uuid, + filename=filename, object_key="originals/" + uuid.uuid4().hex, + content_type=declared, size_bytes=size, sha256=checksum.hexdigest(), + width=width, height=height, status="uploading", + ) + await save_attachment(session, storage, row, stream) + return attachment_out(row) + except StorageUnavailable: + raise unavailable() from None + finally: + request.app.state.upload_slots.release() + + +async def accessible_attachment(session, user, attachment_id): + ident = uuid.UUID(parse_uuid(attachment_id, "attachment_id")) + row = (await session.execute( + select(Attachment).join(ChatMember, ChatMember.chat_id == Attachment.chat_id) + .where(Attachment.id == ident, ChatMember.user_id == user.id) + )).scalar_one_or_none() + if row is None: + raise ApiError(404, "not_found", "Объект не найден") + if row.status != "ready": + raise ApiError(409, "conflict", "Вложение ещё не готово") + return row + + +@router.get("/attachments/{attachment_id}", response_model=AttachmentOut) +async def metadata(attachment_id: str, user: User = Depends(current_user), + session: AsyncSession = Depends(get_session)): + return attachment_out(await accessible_attachment(session, user, attachment_id)) + + +@router.get("/attachments/{attachment_id}/content") +async def content(attachment_id: str, request: Request, user: User = Depends(current_user), + session: AsyncSession = Depends(get_session)): + row = await accessible_attachment(session, user, attachment_id) + if request.query_params.get("variant", "original") != "original": + raise ApiError(404, "not_found", "Вариант не найден") + key, mime, size, name = row.object_key, row.content_type, row.size_bytes, row.filename + await session.rollback() + storage = request.app.state.storage + if storage is None: + raise unavailable() + try: + response = await anyio.to_thread.run_sync(storage.get, key) + except StorageUnavailable: + raise unavailable() from None + + async def chunks(): + try: + while True: + chunk = await anyio.to_thread.run_sync(response.read, 64 * 1024) + if not chunk: + break + yield chunk + finally: + response.close() + response.release_conn() + + return StreamingResponse(chunks(), media_type=mime, headers={ + "Content-Length": str(size), "X-Content-Type-Options": "nosniff", + "Cache-Control": "private, no-store", + "Content-Disposition": "attachment; filename*=UTF-8''" + quote(name, safe=""), + }) 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/cleanup.py b/app/cleanup.py new file mode 100644 index 00000000..e9931475 --- /dev/null +++ b/app/cleanup.py @@ -0,0 +1,80 @@ +"""Retryable compensation of unfinished uploads after a safe grace period.""" + +import argparse +import asyncio +from datetime import timedelta +import json + +import anyio +from sqlalchemy import delete, func, select + +from .config import Settings +from .db import create_engine, create_sessionmaker +from .models import Attachment +from .storage import MinioStorage, StorageUnavailable + + +async def cleanup_once(sessionmaker, storage, grace_seconds=3600, dry_run=False): + """Only old uploading intents, never ready files, with row locks and a batch cap. + + A successful delete followed by failed COMMIT is retried safely: DeleteObject + is idempotent. An uploader holds the same row lock throughout PUT/ready. + """ + if grace_seconds < 3600: + raise ValueError("Grace period must be at least one hour") + result = {"candidates": 0, "deleted": 0, "failed": 0, "dry_run": dry_run} + async with sessionmaker() as session: + rows = list((await session.execute( + select(Attachment).where( + Attachment.status == "uploading", + Attachment.created_at < func.clock_timestamp() - timedelta(seconds=grace_seconds), + ).order_by(Attachment.created_at).limit(100).with_for_update(skip_locked=True) + )).scalars()) + result["candidates"] = len(rows) + for row in rows: + if dry_run: + continue + try: + await anyio.to_thread.run_sync(storage.delete, row.object_key) + except StorageUnavailable: + result["failed"] += 1 + continue + await session.execute(delete(Attachment).where(Attachment.id == row.id)) + result["deleted"] += 1 + await session.commit() + return result + + +async def main(): + parser = argparse.ArgumentParser(description=__doc__) + mode = parser.add_mutually_exclusive_group() + mode.add_argument("--loop", action="store_true") + mode.add_argument("--once", action="store_true") + parser.add_argument("--dry-run", action="store_true") + args = parser.parse_args() + settings = Settings.from_env() + engine = create_engine(settings) + storage = MinioStorage(settings) + try: + while True: + try: + result = await cleanup_once(create_sessionmaker(engine), storage, + settings.cleanup_grace_seconds, args.dry_run) + print(json.dumps(result), flush=True) + if result["failed"] and not args.loop: + return 1 + except Exception as error: + # Never log SQL, connection strings or exception details. + print(json.dumps({"status": "error", "type": type(error).__name__}), flush=True) + if not args.loop: + return 1 + if not args.loop: + return 0 + await asyncio.sleep(settings.cleanup_interval_seconds) + finally: + storage.close() + await engine.dispose() + + +if __name__ == "__main__": + raise SystemExit(asyncio.run(main())) diff --git a/app/config.py b/app/config.py new file mode 100644 index 00000000..28016e39 --- /dev/null +++ b/app/config.py @@ -0,0 +1,122 @@ +"""Настройки приложения из переменных окружения.""" + +import os +from dataclasses import dataclass +from typing import Mapping, Optional + +LOG_LEVELS = ("DEBUG", "INFO", "WARNING", "ERROR") + + +@dataclass(frozen=True) +class Settings: + """Неизменяемый набор настроек.""" + + host: str + port: int + env: str + log_level: str + database_url: str + session_ttl_seconds: int + session_store: str + db_pool_size: int + db_max_overflow: int + db_pool_timeout: int + db_startup_wait_seconds: int + cache_url: str + cache_ttl_seconds: int + cache_timeout_ms: int + cache_retry_seconds: int + + s3_endpoint_url: str = "" + s3_region: str = "us-east-1" + s3_bucket: str = "attachments" + s3_access_key: str = "" + s3_secret_key: str = "" + cleanup_grace_seconds: int = 3600 + cleanup_interval_seconds: int = 60 + + @classmethod + def from_env(cls, environ: Optional[Mapping[str, str]] = None) -> "Settings": + """Собрать и проверить настройки; при ошибке ValueError с описанием.""" + source = os.environ if environ is None else environ + + def get(name: str, default: str) -> str: + value = source.get(name) + if value is None or value.strip() == "": + return default + return value.strip() + + def get_int(name: str, default: int, low: int, high: int) -> int: + text = get(name, str(default)) + if not text.isdigit() or not (low <= int(text) <= high): + raise ValueError( + "%s должен быть числом от %d до %d" % (name, low, high) + ) + return int(text) + + log_level = get("LOG_LEVEL", "INFO").upper() + if log_level not in LOG_LEVELS: + raise ValueError( + "LOG_LEVEL должен быть одним из %s" % ", ".join(LOG_LEVELS) + ) + + session_store = get("SESSION_STORE", "postgres") + if session_store != "postgres": + raise ValueError( + "SESSION_STORE: источник истины сессий -- только postgres " + "(Redis -- кэш, задаётся CACHE_URL)" + ) + + database_url = _read_database_url(source) + if not database_url.startswith("postgresql+asyncpg://"): + raise ValueError("DATABASE_URL должен начинаться с postgresql+asyncpg://") + + return cls( + host=get("APP_HOST", "127.0.0.1"), + port=get_int("APP_PORT", 8080, 1, 65535), + env=get("APP_ENV", "lab"), + log_level=log_level, + database_url=database_url, + session_ttl_seconds=get_int("SESSION_TTL_SECONDS", 3600, 1, 30 * 24 * 3600), + session_store=session_store, + db_pool_size=get_int("DB_POOL_SIZE", 5, 1, 50), + db_max_overflow=get_int("DB_MAX_OVERFLOW", 5, 0, 50), + db_pool_timeout=get_int("DB_POOL_TIMEOUT", 5, 1, 60), + db_startup_wait_seconds=get_int("DB_STARTUP_WAIT_SECONDS", 30, 0, 300), + cache_url=_read_cache_url(source), + cache_ttl_seconds=get_int("CACHE_TTL_SECONDS", 60, 1, 3600), + cache_timeout_ms=get_int("CACHE_TIMEOUT_MS", 300, 10, 5000), + cache_retry_seconds=get_int("CACHE_RETRY_SECONDS", 5, 0, 300), + s3_endpoint_url=get("S3_ENDPOINT_URL", ""), + s3_region=get("S3_REGION", "us-east-1"), + s3_bucket=get("S3_BUCKET", "attachments"), + s3_access_key=get("S3_ACCESS_KEY", ""), + s3_secret_key=get("S3_SECRET_KEY", ""), + cleanup_grace_seconds=get_int("CLEANUP_GRACE_SECONDS", 3600, 3600, 604800), + cleanup_interval_seconds=get_int("CLEANUP_INTERVAL_SECONDS", 60, 10, 3600), + ) + + +def _read_database_url(source: Mapping[str, str]) -> str: + """Адрес БД из DATABASE_URL_FILE или DATABASE_URL.""" + path = source.get("DATABASE_URL_FILE", "").strip() + if path: + with open(path, encoding="utf-8") as handle: + return handle.read().strip() + value = source.get("DATABASE_URL", "").strip() + if not value: + raise ValueError("Не задан DATABASE_URL (или DATABASE_URL_FILE)") + return value + + +def _read_cache_url(source: Mapping[str, str]) -> str: + """Адрес Redis из CACHE_URL_FILE или CACHE_URL; пустая строка -- кэш выключен.""" + path = source.get("CACHE_URL_FILE", "").strip() + if path: + with open(path, encoding="utf-8") as handle: + value = handle.read().strip() + else: + value = source.get("CACHE_URL", "").strip() + if value and not value.startswith(("redis://", "rediss://")): + raise ValueError("CACHE_URL должен начинаться с redis:// или rediss://") + return value diff --git a/app/crud.py b/app/crud.py new file mode 100644 index 00000000..6718c898 --- /dev/null +++ b/app/crud.py @@ -0,0 +1,262 @@ +"""Запросы к PostgreSQL через SQLAlchemy с параметрами. + +Изменяющие функции -- одна транзакция: commit в конце, rollback при ошибке. +""" + +import hashlib +import uuid +from datetime import datetime, timedelta +from typing import Dict, List, Optional, Tuple + +from sqlalchemy import Select, delete, func, insert, select, tuple_ +from sqlalchemy.exc import IntegrityError +from sqlalchemy.ext.asyncio import AsyncSession + +from .models import Attachment, Chat, ChatMember, Message, MessageRequest, SessionRow, User +from .errors import ApiError + +UNIQUE_VIOLATION = "23505" +FOREIGN_KEY_VIOLATION = "23503" + +Position = Tuple[datetime, uuid.UUID] + + +class UsernameTaken(Exception): + """Username уже занят.""" + + +class IdempotencyConflict(Exception): + """Ключ уже использован с другим телом запроса.""" + + +class UnknownMember(Exception): + """Среди участников чата есть несуществующий пользователь.""" + + +def sqlstate(error: IntegrityError) -> Optional[str]: + """Код SQLSTATE из исключения SQLAlchemy.""" + original = error.orig + code = getattr(original, "sqlstate", None) + if code is None: + code = getattr(getattr(original, "__cause__", None), "sqlstate", None) + return code + + +async def create_user( + session: AsyncSession, username: str, display_name: str, password_hash: str +) -> User: + """Создать пользователя; дубликат username -> UsernameTaken.""" + statement = ( + insert(User) + .values( + id=uuid.uuid4(), + username=username, + display_name=display_name, + password_hash=password_hash, + ) + .returning(User) + ) + try: + user = (await session.execute(statement)).scalar_one() + await session.commit() + except IntegrityError as error: + await session.rollback() + if sqlstate(error) == UNIQUE_VIOLATION: + raise UsernameTaken(username) + raise + return user + + +async def get_user_by_username(session: AsyncSession, username: str) -> Optional[User]: + """Пользователь по username или None.""" + return ( + await session.execute(select(User).where(User.username == username)) + ).scalar_one_or_none() + + +async def create_session( + session: AsyncSession, user_id: uuid.UUID, token_hash: str, ttl_seconds: int +) -> datetime: + """Удалить истёкшие сессии пользователя, создать новую, вернуть expires_at.""" + await session.execute( + delete(SessionRow).where( + SessionRow.user_id == user_id, SessionRow.expires_at < func.now() + ) + ) + statement = ( + insert(SessionRow) + .values( + token_hash=token_hash, + user_id=user_id, + expires_at=func.now() + timedelta(seconds=ttl_seconds), + ) + .returning(SessionRow.expires_at) + ) + expires_at = (await session.execute(statement)).scalar_one() + await session.commit() + return expires_at + + +async def get_user_by_token( + session: AsyncSession, token_hash: str +) -> Optional[Tuple[User, float]]: + """(User, секунд до истечения) для действующей сессии или None.""" + statement = ( + select(User, func.extract("epoch", SessionRow.expires_at - func.now())) + .join(SessionRow, SessionRow.user_id == User.id) + .where(SessionRow.token_hash == token_hash, SessionRow.expires_at > func.now()) + ) + row = (await session.execute(statement)).one_or_none() + if row is None: + return None + return row[0], float(row[1]) + + +async def session_is_active(session: AsyncSession, token_hash: str) -> bool: + """Проверить наличие неистёкшей сессии без загрузки пользователя.""" + statement = select( + select(SessionRow.token_hash) + .where(SessionRow.token_hash == token_hash, SessionRow.expires_at > func.now()) + .exists() + ) + return (await session.execute(statement)).scalar_one() + + +async def delete_session(session: AsyncSession, token_hash: str) -> None: + """Удалить сессию по хешу токена.""" + await session.execute(delete(SessionRow).where(SessionRow.token_hash == token_hash)) + await session.commit() + + +async def create_chat( + session: AsyncSession, title: str, member_ids: List[uuid.UUID] +) -> Tuple[Chat, List[uuid.UUID]]: + """Атомарно создать чат и участников; неизвестный участник -> UnknownMember.""" + chat_statement = insert(Chat).values(id=uuid.uuid4(), title=title).returning(Chat) + try: + chat = (await session.execute(chat_statement)).scalar_one() + await session.execute( + insert(ChatMember), + [{"chat_id": chat.id, "user_id": user_id} for user_id in member_ids], + ) + await session.commit() + except IntegrityError as error: + await session.rollback() + if sqlstate(error) == FOREIGN_KEY_VIOLATION: + raise UnknownMember() + raise + return chat, member_ids + + +async def get_chat_for_member( + session: AsyncSession, chat_id: uuid.UUID, user_id: uuid.UUID +) -> Optional[Chat]: + """Чат, если пользователь в нём состоит, иначе None.""" + statement = ( + select(Chat) + .join(ChatMember, ChatMember.chat_id == Chat.id) + .where(Chat.id == chat_id, ChatMember.user_id == user_id) + ) + return (await session.execute(statement)).scalar_one_or_none() + + +async def get_member_ids( + session: AsyncSession, chat_ids: List[uuid.UUID] +) -> Dict[uuid.UUID, List[uuid.UUID]]: + """Участники нескольких чатов одним запросом: {chat_id: [user_id]}.""" + result: Dict[uuid.UUID, List[uuid.UUID]] = {chat_id: [] for chat_id in chat_ids} + if chat_ids: + statement = ( + select(ChatMember.chat_id, ChatMember.user_id) + .where(ChatMember.chat_id.in_(chat_ids)) + .order_by(ChatMember.chat_id, ChatMember.user_id) + ) + for chat_id, user_id in (await session.execute(statement)).all(): + result[chat_id].append(user_id) + return result + + +async def list_chats( + session: AsyncSession, user_id: uuid.UUID, after: Optional[Position], limit: int +) -> Tuple[List[Chat], bool]: + """Страница чатов пользователя по (created_at, id) и признак следующей страницы.""" + statement = ( + select(Chat) + .join(ChatMember, ChatMember.chat_id == Chat.id) + .where(ChatMember.user_id == user_id) + ) + if after is not None: + statement = statement.where(tuple_(Chat.created_at, Chat.id) > tuple_(*after)) + statement = statement.order_by(Chat.created_at, Chat.id).limit(limit + 1) + rows = list((await session.execute(statement)).scalars()) + return rows[:limit], len(rows) > limit + + +async def create_message_idempotent( + session: AsyncSession, chat_id: uuid.UUID, sender_id: uuid.UUID, text: str, key: str, + attachment_ids: Optional[List[uuid.UUID]] = None, +) -> Message: + """Атомарно сохранить сообщение и результат на 24 часа во всех API-репликах.""" + attachment_ids = attachment_ids or [] + scope = f"{sender_id}:{chat_id}:{key}".encode("utf-8") + lock_id = int.from_bytes(hashlib.sha256(scope).digest()[:8], "big", signed=True) + match = ( + MessageRequest.user_id == sender_id, + MessageRequest.chat_id == chat_id, + MessageRequest.key == key, + ) + try: + await session.execute(select(func.pg_advisory_xact_lock(lock_id))) + previous = (await session.execute( + select(MessageRequest).where(*match, MessageRequest.expires_at > func.clock_timestamp()) + )).scalar_one_or_none() + if previous is not None: + if previous.request_text != text or previous.request_attachment_ids != attachment_ids: + raise IdempotencyConflict() + message = (await session.execute( + select(Message).where(Message.id == previous.message_id) + )).scalar_one() + else: + if attachment_ids: + attachments = list((await session.execute(select(Attachment).where( + Attachment.id.in_(attachment_ids), Attachment.chat_id == chat_id, + ))).scalars()) + if len(attachments) != len(attachment_ids): + raise ApiError(404, "not_found", "Объект не найден") + if any(row.status != "ready" for row in attachments): + raise ApiError(409, "conflict", "Вложение ещё не готово") + await session.execute(delete(MessageRequest).where(*match)) + message = (await session.execute( + insert(Message).values( + id=uuid.uuid4(), chat_id=chat_id, sender_id=sender_id, text=text, + attachment_ids=attachment_ids, + ).returning(Message) + )).scalar_one() + await session.execute(insert(MessageRequest).values( + user_id=sender_id, chat_id=chat_id, key=key, request_text=text, + request_attachment_ids=attachment_ids, + message_id=message.id, expires_at=func.clock_timestamp() + timedelta(hours=24), + )) + await session.commit() + return message + except BaseException: + await session.rollback() + raise + + +def history_query(chat_id: uuid.UUID, after: Optional[Position], limit: int) -> Select: + """SELECT страницы истории по курсору (created_at, id); идёт по индексу чата.""" + statement = select(Message).where(Message.chat_id == chat_id) + if after is not None: + statement = statement.where( + tuple_(Message.created_at, Message.id) > tuple_(*after) + ) + return statement.order_by(Message.created_at, Message.id).limit(limit + 1) + + +async def list_messages( + session: AsyncSession, chat_id: uuid.UUID, after: Optional[Position], limit: int +) -> Tuple[List[Message], bool]: + """Страница истории чата и признак следующей страницы.""" + rows = list((await session.execute(history_query(chat_id, after, limit))).scalars()) + return rows[:limit], len(rows) > limit 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..1c6e775f --- /dev/null +++ b/app/main.py @@ -0,0 +1,128 @@ +"""Сборка приложения FastAPI: зависимости, миграции, обработчики ошибок.""" + +import asyncio +import uuid +from contextlib import asynccontextmanager +from typing import Optional + +from fastapi import FastAPI, Request +from fastapi.exceptions import RequestValidationError +from sqlalchemy.exc import InterfaceError, OperationalError +from sqlalchemy.exc import TimeoutError as PoolTimeoutError +from starlette.exceptions import HTTPException as StarletteHTTPException + +from .cache import create_cache +from .config import Settings +from .db import create_engine, create_sessionmaker +from .errors import ApiError, error_response +from .logging_setup import get_logger, setup_logging +from .middleware import RequestContextMiddleware +from .migrate import check_schema +from .routes import api, router +from .attachments import router as attachment_router +from .storage import MinioStorage + +DB_UNAVAILABLE = ( + OperationalError, + InterfaceError, + PoolTimeoutError, + OSError, + asyncio.TimeoutError, +) + + +def create_app(settings: Optional[Settings] = None) -> FastAPI: + """Создать приложение FastAPI с заданными настройками.""" + settings = settings or Settings.from_env() + setup_logging(settings.log_level) + + @asynccontextmanager + async def lifespan(app: FastAPI): + """Старт: кэш, движок, проверка версии схемы. Остановка: закрыть соединения.""" + get_logger().info( + "config env=%s pool=%d+%d ttl=%ds store=%s cache=%s", + settings.env, + settings.db_pool_size, + settings.db_max_overflow, + settings.session_ttl_seconds, + settings.session_store, + "redis" if settings.cache_url else "off", + ) + app.state.cache = create_cache(settings) + engine = create_engine(settings) + app.state.engine = engine + app.state.sessionmaker = create_sessionmaker(engine) + app.state.storage = MinioStorage(settings) if settings.s3_endpoint_url else None + app.state.upload_slots = asyncio.Semaphore(2) + app.state.decode_slot = asyncio.Semaphore(1) + try: + await check_schema(engine) + app.state.ready = True + get_logger().info("startup: приложение готово") + yield + finally: + app.state.ready = False + await app.state.cache.close() + if app.state.storage is not None: + app.state.storage.close() + await engine.dispose() + get_logger().info("shutdown: соединения с БД закрыты") + + app = FastAPI(title="Messenger API", version="1.4.0", lifespan=lifespan) + app.state.settings = settings + app.state.ready = False + app.router.redirect_slashes = False + app.state.instance_id = uuid.uuid4().hex + app.add_middleware(RequestContextMiddleware, instance_id=app.state.instance_id) + + @app.exception_handler(ApiError) + async def handle_api_error(request: Request, exc: ApiError): + """ApiError -> ответ в общем формате.""" + return error_response( + request.state.request_id, exc.status, exc.code, exc.message, exc.headers + ) + + @app.exception_handler(StarletteHTTPException) + async def handle_http_error(request: Request, exc: StarletteHTTPException): + """404 и 405 -> ответ в общем формате.""" + if exc.status_code == 404: + return error_response( + request.state.request_id, 404, "not_found", "Объект не найден" + ) + headers = dict(exc.headers) if exc.headers else None + return error_response( + request.state.request_id, + exc.status_code, + "invalid_request", + "Запрос не поддерживается", + headers, + ) + + @app.exception_handler(RequestValidationError) + async def handle_validation_error(request: Request, exc: RequestValidationError): + """Ошибки разбора параметров -> 400.""" + return error_response( + request.state.request_id, + 400, + "invalid_request", + "Неверные параметры запроса", + ) + + async def handle_db_unavailable(request: Request, exc: Exception): + """Недоступная БД или пул -> 503 без деталей.""" + get_logger().error( + "db_unavailable request_id=%s type=%s", + request.state.request_id, + type(exc).__name__, + ) + return error_response( + request.state.request_id, 503, "unavailable", "Сервис временно недоступен" + ) + + for error_type in DB_UNAVAILABLE: + app.add_exception_handler(error_type, handle_db_unavailable) + + app.include_router(router) + app.include_router(api) + app.include_router(attachment_router) + return app 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/migrations/0003_attachments.sql b/app/migrations/0003_attachments.sql new file mode 100644 index 00000000..d762f17c --- /dev/null +++ b/app/migrations/0003_attachments.sql @@ -0,0 +1,26 @@ +CREATE TABLE attachments ( + id uuid PRIMARY KEY, + chat_id uuid NOT NULL REFERENCES chats(id), + owner_id uuid NOT NULL REFERENCES users(id), + filename text NOT NULL CHECK (char_length(filename) BETWEEN 1 AND 255), + object_key text NOT NULL UNIQUE, + content_type text NOT NULL CHECK (content_type IN ('image/png', 'image/jpeg')), + size_bytes integer NOT NULL CHECK (size_bytes BETWEEN 1 AND 10485760), + sha256 text NOT NULL CHECK (sha256 ~ '^[0-9a-f]{64}$'), + width integer NOT NULL CHECK (width > 0), + height integer NOT NULL CHECK (height > 0), + status text NOT NULL CHECK (status IN ('uploading', 'ready')), + created_at timestamptz NOT NULL DEFAULT clock_timestamp(), + updated_at timestamptz NOT NULL DEFAULT clock_timestamp(), + CHECK (width::bigint * height <= 40000000) +); +CREATE INDEX attachments_chat_idx ON attachments(chat_id, id); +CREATE INDEX attachments_pending_idx ON attachments(created_at) WHERE status = 'uploading'; + +ALTER TABLE messages ADD COLUMN attachment_ids uuid[] NOT NULL DEFAULT '{}'; +ALTER TABLE messages DROP CONSTRAINT messages_text_check; +ALTER TABLE messages ADD CONSTRAINT messages_content_check CHECK ( + char_length(text) <= 4000 AND cardinality(attachment_ids) <= 10 + AND (char_length(btrim(text, E' \t\r\n')) > 0 OR cardinality(attachment_ids) > 0) +); +ALTER TABLE message_requests ADD COLUMN request_attachment_ids uuid[] NOT NULL DEFAULT '{}'; diff --git a/app/migrations/0004_attachment_timestamps.sql b/app/migrations/0004_attachment_timestamps.sql new file mode 100644 index 00000000..fed0d019 --- /dev/null +++ b/app/migrations/0004_attachment_timestamps.sql @@ -0,0 +1,2 @@ +-- Compatible with an already bootstrapped attachment table. +ALTER TABLE attachments ADD COLUMN IF NOT EXISTS updated_at timestamptz NOT NULL DEFAULT clock_timestamp(); diff --git a/app/models.py b/app/models.py new file mode 100644 index 00000000..55713102 --- /dev/null +++ b/app/models.py @@ -0,0 +1,104 @@ +"""ORM-модели таблиц; схема и ограничения задаются миграциями.""" + +import uuid +from datetime import datetime + +from sqlalchemy import DateTime, func +from sqlalchemy.dialects.postgresql import ARRAY, UUID +from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column + +NOW = func.clock_timestamp() + + +class Base(DeclarativeBase): + """Базовый класс моделей.""" + + +class User(Base): + """Пользователь; пароль хранится только как хеш.""" + + __tablename__ = "users" + id: Mapped[uuid.UUID] = mapped_column(primary_key=True) + username: Mapped[str] + display_name: Mapped[str] + password_hash: Mapped[str] + created_at: Mapped[datetime] = mapped_column( + DateTime(timezone=True), server_default=NOW + ) + + +class Chat(Base): + """Чат.""" + + __tablename__ = "chats" + id: Mapped[uuid.UUID] = mapped_column(primary_key=True) + title: Mapped[str] + created_at: Mapped[datetime] = mapped_column( + DateTime(timezone=True), server_default=NOW + ) + + +class ChatMember(Base): + """Участие пользователя в чате.""" + + __tablename__ = "chat_members" + chat_id: Mapped[uuid.UUID] = mapped_column(primary_key=True) + user_id: Mapped[uuid.UUID] = mapped_column(primary_key=True) + + +class Message(Base): + """Сообщение чата.""" + + __tablename__ = "messages" + id: Mapped[uuid.UUID] = mapped_column(primary_key=True) + chat_id: Mapped[uuid.UUID] + sender_id: Mapped[uuid.UUID] + text: Mapped[str] + attachment_ids: Mapped[list[uuid.UUID]] = mapped_column(ARRAY(UUID(as_uuid=True)), default=list) + created_at: Mapped[datetime] = mapped_column( + DateTime(timezone=True), server_default=NOW + ) + + +class SessionRow(Base): + """Сессия входа; хранится SHA-256 токена.""" + + __tablename__ = "sessions" + token_hash: Mapped[str] = mapped_column(primary_key=True) + user_id: Mapped[uuid.UUID] + created_at: Mapped[datetime] = mapped_column( + DateTime(timezone=True), server_default=NOW + ) + expires_at: Mapped[datetime] = mapped_column(DateTime(timezone=True)) + + +class MessageRequest(Base): + """Общий результат POST сообщения; область ключа — пользователь и чат.""" + + __tablename__ = "message_requests" + user_id: Mapped[uuid.UUID] = mapped_column(primary_key=True) + chat_id: Mapped[uuid.UUID] = mapped_column(primary_key=True) + key: Mapped[str] = mapped_column(primary_key=True) + request_text: Mapped[str] + request_attachment_ids: Mapped[list[uuid.UUID]] = mapped_column(ARRAY(UUID(as_uuid=True)), default=list) + message_id: Mapped[uuid.UUID] + expires_at: Mapped[datetime] = mapped_column(DateTime(timezone=True)) + + +class Attachment(Base): + """Метаданные и устойчивое намерение upload; бинарные данные только в S3.""" + + __tablename__ = "attachments" + id: Mapped[uuid.UUID] = mapped_column(primary_key=True) + chat_id: Mapped[uuid.UUID] + owner_id: Mapped[uuid.UUID] + filename: Mapped[str] + object_key: Mapped[str] + content_type: Mapped[str] + size_bytes: Mapped[int] + sha256: Mapped[str] + width: Mapped[int] + height: Mapped[int] + status: Mapped[str] + updated_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=NOW) + created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=NOW) diff --git a/app/routes.py b/app/routes.py new file mode 100644 index 00000000..cdbcd7b8 --- /dev/null +++ b/app/routes.py @@ -0,0 +1,306 @@ +"""HTTP-эндпоинты API. + +Порядок проверок: аутентификация (401), доступ к чату (404), тело запроса (400). +""" + +import asyncio +import uuid +import anyio +from typing import List + +from fastapi import APIRouter, Depends, Request, Response +from sqlalchemy import text +from sqlalchemy.ext.asyncio import AsyncSession + +from . import crud +from .auth import ( + BASIC_CHALLENGE, + current_user, + get_session, + parse_basic, + parse_bearer, + unauthorized, +) +from .cursor import encode_cursor +from .errors import ApiError +from .http_utils import ( + parse_cursor, + parse_idempotency_key, + parse_limit, + parse_uuid, + read_json_object, + validate_body, +) +from .models import Chat, Message, User +from .schemas import ( + ChatOut, + ChatPage, + CreateChatIn, + CreateMessageIn, + CreateUserIn, + MessageOut, + MessagePage, + SessionOut, + UserOut, +) +from .security import ( + DUMMY_HASH, + hash_password_async, + hash_token, + new_token, + verify_password_async, +) +from .timeutil import format_ts, from_micros, to_micros +from .storage import StorageUnavailable, unavailable + +router = APIRouter() +api = APIRouter(prefix="/api/v1") + + +def body_doc(model: type) -> dict: + """Описание тела запроса для OpenAPI-документации.""" + return { + "requestBody": { + "required": True, + "content": {"application/json": {"schema": model.model_json_schema()}}, + } + } + + +def user_out(user: User) -> dict: + """User -> словарь ответа.""" + return { + "id": str(user.id), + "username": user.username, + "display_name": user.display_name, + "created_at": format_ts(user.created_at), + } + + +def chat_out(chat: Chat, member_ids: List[uuid.UUID]) -> dict: + """Chat и участники -> словарь ответа.""" + return { + "id": str(chat.id), + "title": chat.title, + "member_ids": [str(member_id) for member_id in member_ids], + "created_at": format_ts(chat.created_at), + } + + +def message_out(message: Message) -> dict: + """Message -> словарь ответа.""" + return { + "id": str(message.id), + "chat_id": str(message.chat_id), + "sender_id": str(message.sender_id), + "text": message.text, + "attachment_ids": [str(ident) for ident in message.attachment_ids], + "created_at": format_ts(message.created_at), + } + + +def position(key): + """Позицию из курсора перевести в (datetime, UUID).""" + if key is None: + return None + return from_micros(key[0]), uuid.UUID(key[1]) + + +def cursor_for(scope: str, row) -> str: + """Курсор, указывающий на строку row.""" + return encode_cursor(scope, (to_micros(row.created_at), str(row.id))) + + +async def open_chat(session: AsyncSession, user: User, chat_id_text: str) -> Chat: + """Чат, доступный пользователю; неверный id -> 400, чужой или несуществующий -> 404.""" + chat_id = uuid.UUID(parse_uuid(chat_id_text, "chat_id")) + chat = await crud.get_chat_for_member(session, chat_id, user.id) + if chat is None: + raise ApiError(404, "not_found", "Объект не найден") + return chat + + +@router.get("/health/live") +async def live() -> dict: + """Процесс жив.""" + return {"status": "ok"} + + +@router.get("/health/ready") +async def ready(request: Request): + """Готовность: схема, PostgreSQL и приватный S3 bucket доступны.""" + if not getattr(request.app.state, "ready", False): + raise ApiError(503, "unavailable", "Сервис не готов") + try: + async with request.app.state.sessionmaker() as session: + await asyncio.wait_for(session.execute(text("SELECT 1")), timeout=2) + except Exception: + raise ApiError(503, "unavailable", "База данных недоступна") + if request.app.state.storage is not None: + try: + await anyio.to_thread.run_sync(request.app.state.storage.ready) + except StorageUnavailable: + raise unavailable() from None + return {"status": "ok"} + + +@api.post( + "/users", + status_code=201, + response_model=UserOut, + openapi_extra=body_doc(CreateUserIn), +) +async def register_user(request: Request, session: AsyncSession = Depends(get_session)): + """Регистрация пользователя.""" + body = validate_body(CreateUserIn, await read_json_object(request)) + password_hash = await hash_password_async(body.password) + try: + user = await crud.create_user( + session, body.username, body.display_name, password_hash + ) + except crud.UsernameTaken: + raise ApiError(409, "conflict", "Такое имя пользователя уже занято") + return user_out(user) + + +@api.post("/auth/sessions", status_code=201, response_model=SessionOut) +async def login(request: Request, session: AsyncSession = Depends(get_session)): + """Вход по Basic: создать сессию и вернуть Bearer-токен.""" + username, password = parse_basic(request) + user = await crud.get_user_by_username(session, username) + password_ok = await verify_password_async( + password, user.password_hash if user else DUMMY_HASH + ) + if user is None or not password_ok: + raise unauthorized(BASIC_CHALLENGE) + token = new_token() + expires_at = await crud.create_session( + session, + user.id, + hash_token(token), + request.app.state.settings.session_ttl_seconds, + ) + return { + "token": token, + "token_type": "Bearer", + "expires_at": format_ts(expires_at), + "user": user_out(user), + } + + +@api.delete("/auth/sessions/current", status_code=204) +async def logout( + request: Request, + user: User = Depends(current_user), + session: AsyncSession = Depends(get_session), +): + """Отозвать текущую сессию.""" + token_hash = hash_token(parse_bearer(request)) + await crud.delete_session(session, token_hash) + await request.app.state.cache.revoke(token_hash) + return Response(status_code=204) + + +@api.get("/users/me", response_model=UserOut) +async def read_me(user: User = Depends(current_user)): + """Текущий пользователь.""" + return user_out(user) + + +@api.post( + "/chats", + status_code=201, + response_model=ChatOut, + openapi_extra=body_doc(CreateChatIn), +) +async def create_chat( + request: Request, + user: User = Depends(current_user), + session: AsyncSession = Depends(get_session), +): + """Создать чат с участниками.""" + body = validate_body(CreateChatIn, await read_json_object(request)) + member_ids = [uuid.UUID(item) for item in body.member_ids] + if user.id not in member_ids: + raise ApiError(400, "invalid_request", "member_ids должен включать вас самих") + try: + chat, members = await crud.create_chat(session, body.title, member_ids) + except crud.UnknownMember: + raise ApiError( + 400, "invalid_request", "member_ids содержит несуществующего пользователя" + ) + return chat_out(chat, sorted(members)) + + +@api.get("/chats", response_model=ChatPage) +async def list_chats( + request: Request, + user: User = Depends(current_user), + session: AsyncSession = Depends(get_session), +): + """Страница чатов текущего пользователя.""" + limit = parse_limit(request.query_params.get("limit")) + scope = "chats:" + str(user.id) + after = position(parse_cursor(request.query_params.get("cursor"), scope)) + chats, has_more = await crud.list_chats(session, user.id, after, limit) + members = await crud.get_member_ids(session, [chat.id for chat in chats]) + next_cursor = cursor_for(scope, chats[-1]) if has_more and chats else None + return { + "items": [chat_out(c, members[c.id]) for c in chats], + "next_cursor": next_cursor, + } + + +@api.get("/chats/{chat_id}", response_model=ChatOut) +async def read_chat( + chat_id: str, + user: User = Depends(current_user), + session: AsyncSession = Depends(get_session), +): + """Один чат по id.""" + chat = await open_chat(session, user, chat_id) + members = await crud.get_member_ids(session, [chat.id]) + return chat_out(chat, members[chat.id]) + + +@api.post( + "/chats/{chat_id}/messages", + status_code=201, + response_model=MessageOut, + openapi_extra=body_doc(CreateMessageIn), +) +async def create_message( + chat_id: str, + request: Request, + user: User = Depends(current_user), + session: AsyncSession = Depends(get_session), +): + """Отправить сообщение в чат.""" + chat = await open_chat(session, user, chat_id) + body = validate_body(CreateMessageIn, await read_json_object(request)) + key = parse_idempotency_key(request) + try: + message = await crud.create_message_idempotent( + session, chat.id, user.id, body.text, key, + [uuid.UUID(value) for value in body.attachment_ids], + ) + except crud.IdempotencyConflict: + raise ApiError(409, "conflict", "Idempotency-Key уже использован с другим сообщением") + return message_out(message) + + +@api.get("/chats/{chat_id}/messages", response_model=MessagePage) +async def list_messages( + chat_id: str, + request: Request, + user: User = Depends(current_user), + session: AsyncSession = Depends(get_session), +): + """Страница истории чата.""" + chat = await open_chat(session, user, chat_id) + limit = parse_limit(request.query_params.get("limit")) + scope = "messages:" + str(chat.id) + after = position(parse_cursor(request.query_params.get("cursor"), scope)) + messages, has_more = await crud.list_messages(session, chat.id, after, limit) + next_cursor = cursor_for(scope, messages[-1]) if has_more and messages else None + return {"items": [message_out(m) for m in messages], "next_cursor": next_cursor} diff --git a/app/schemas.py b/app/schemas.py new file mode 100644 index 00000000..0be5d11f --- /dev/null +++ b/app/schemas.py @@ -0,0 +1,188 @@ +"""Pydantic-модели тел запросов и ответов.""" + +import re +from typing import List, Optional + +from pydantic import BaseModel, ConfigDict, Field, field_validator, model_validator + +from .cursor import UUID_RE + +USERNAME_RE = re.compile(r"[a-z0-9_]{3,32}") + + +def _check_text(value: str, name: str, max_len: int, allow_blank: bool = False) -> str: + """Проверить строку: тип, длина в символах, не только пробелы, без NUL.""" + try: + value.encode("utf-8") + except UnicodeEncodeError: + raise ValueError("%s содержит недопустимые символы" % name) + if "\x00" in value: + raise ValueError("%s не может содержать символ NUL" % name) + if not allow_blank and value.strip() == "": + raise ValueError("%s не может быть пустым или состоять из пробелов" % name) + if len(value) > max_len: + raise ValueError("%s длиннее %d символов" % (name, max_len)) + return value + + +class _Strict(BaseModel): + """База моделей запроса: лишние поля и приведение типов запрещены.""" + + model_config = ConfigDict(extra="forbid", strict=True) + + +class CreateUserIn(_Strict): + """Тело регистрации.""" + + username: str + display_name: str + password: str + + @field_validator("username") + @classmethod + def _username(cls, value: str) -> str: + """username: [a-z0-9_]{3,32}.""" + if not USERNAME_RE.fullmatch(value): + raise ValueError("username: только a-z, 0-9 и _, длина 3-32") + return value + + @field_validator("display_name") + @classmethod + def _display_name(cls, value: str) -> str: + """display_name: 1-100 символов.""" + return _check_text(value, "display_name", 100) + + @field_validator("password") + @classmethod + def _password(cls, value: str) -> str: + """password: 12-128 символов, без обрезки пробелов.""" + _check_text(value, "password", 128, allow_blank=True) + if len(value) < 12: + raise ValueError("password короче 12 символов") + return value + + +class CreateChatIn(_Strict): + """Тело создания чата.""" + + title: str + member_ids: List[str] + + @field_validator("title") + @classmethod + def _title(cls, value: str) -> str: + """title: 1-100 символов.""" + return _check_text(value, "title", 100) + + @field_validator("member_ids") + @classmethod + def _members(cls, value: List[str]) -> List[str]: + """member_ids: 1-100 уникальных UUID.""" + if not 1 <= len(value) <= 100: + raise ValueError("member_ids: от 1 до 100 участников") + normalized = [] + for item in value: + if not UUID_RE.fullmatch(item): + raise ValueError("member_ids: каждый элемент должен быть UUID") + normalized.append(item.lower()) + if len(set(normalized)) != len(normalized): + raise ValueError("member_ids: дубликаты не допускаются") + return normalized + + +class CreateMessageIn(_Strict): + """Тело сообщения; отправитель определяется по токену.""" + + text: str + attachment_ids: List[str] = Field(default_factory=list) + + @field_validator("attachment_ids") + @classmethod + def _attachments(cls, value: List[str]) -> List[str]: + """До десяти уникальных UUID; порядок вложений сохраняется.""" + if len(value) > 10: + raise ValueError("Не более 10 вложений") + if any(not UUID_RE.fullmatch(item) for item in value): + raise ValueError("attachment_ids: каждый элемент должен быть UUID") + normalized = [item.lower() for item in value] + if len(set(normalized)) != len(normalized): + raise ValueError("attachment_ids: дубликаты не допускаются") + return normalized + + @field_validator("text") + @classmethod + def _text(cls, value: str) -> str: + return _check_text(value, "text", 4000, allow_blank=True) + + @model_validator(mode="after") + def _not_empty(self): + if not self.text.strip() and not self.attachment_ids: + raise ValueError("Нужен текст или хотя бы одно вложение") + return self + + +class UserOut(BaseModel): + """Пользователь в ответе.""" + + id: str + username: str + display_name: str + created_at: str + + +class SessionOut(BaseModel): + """Ответ на вход.""" + + token: str + token_type: str + expires_at: str + user: UserOut + + +class ChatOut(BaseModel): + """Чат в ответе.""" + + id: str + title: str + member_ids: List[str] + created_at: str + + +class MessageOut(BaseModel): + """Сообщение в ответе.""" + + id: str + chat_id: str + sender_id: str + text: str + attachment_ids: List[str] + created_at: str + + +class ChatPage(BaseModel): + """Страница списка чатов.""" + + items: List[ChatOut] + next_cursor: Optional[str] + + +class MessagePage(BaseModel): + """Страница истории сообщений.""" + + items: List[MessageOut] + next_cursor: Optional[str] + + +class AttachmentOut(BaseModel): + id: str + chat_id: str + owner_id: str + filename: str + content_type: str + size_bytes: int + sha256: str + status: str + variants: list + error_code: Optional[str] + created_at: str + updated_at: str 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/storage.py b/app/storage.py new file mode 100644 index 00000000..7f4890df --- /dev/null +++ b/app/storage.py @@ -0,0 +1,85 @@ +"""S3 client: one bounded PUT, no implicit write retries, streaming GET.""" + +from urllib.parse import urlsplit + +from minio import Minio +from minio.error import MinioException +import urllib3 + +from .errors import ApiError + +STORAGE_ERRORS = (MinioException, urllib3.exceptions.HTTPError, OSError) +MAX_BYTES = 10 * 1024 * 1024 + + +class StorageUnavailable(Exception): + """Storage error without sensitive endpoint/credentials in its message.""" + + +class MinioStorage: + def __init__(self, settings): + endpoint = urlsplit(settings.s3_endpoint_url) + if endpoint.scheme not in ("http", "https") or not endpoint.netloc: + raise ValueError("S3_ENDPOINT_URL must be an http(s) endpoint") + if endpoint.path not in ("", "/") or endpoint.username or endpoint.password: + raise ValueError("S3 endpoint must not contain a path or credentials") + if not settings.s3_access_key or not settings.s3_secret_key: + raise ValueError("S3 credentials are required") + self.bucket = settings.s3_bucket + self.http = urllib3.PoolManager( + num_pools=2, maxsize=8, block=False, + timeout=urllib3.Timeout(connect=3, read=10), retries=False, + ) + # Readiness has its own short deadline, so S3 outage cannot accumulate probes. + self.probe_http = urllib3.PoolManager( + num_pools=1, maxsize=2, timeout=urllib3.Timeout(connect=0.5, read=0.5), retries=False, + ) + options = dict( + endpoint=endpoint.netloc, access_key=settings.s3_access_key, + secret_key=settings.s3_secret_key, secure=endpoint.scheme == "https", + region=settings.s3_region, + ) + self.client = Minio(**options, http_client=self.http) + self.probe = Minio(**options, http_client=self.probe_http) + + def put(self, key, stream, size, content_type): + try: + stream.seek(0) + # All accepted files fit ONE part: no abandoned multipart upload state. + self.client.put_object(self.bucket, key, stream, size, content_type, + part_size=MAX_BYTES, num_parallel_uploads=1) + except STORAGE_ERRORS: + raise StorageUnavailable() from None + + def get(self, key): + try: + return self.client.get_object(self.bucket, key) + except STORAGE_ERRORS: + raise StorageUnavailable() from None + + def stat(self, key): + try: + return self.client.stat_object(self.bucket, key) + except STORAGE_ERRORS: + raise StorageUnavailable() from None + + def delete(self, key): + try: + self.client.remove_object(self.bucket, key) + except STORAGE_ERRORS: + raise StorageUnavailable() from None + + def ready(self): + try: + if not self.probe.bucket_exists(self.bucket): + raise StorageUnavailable() + except STORAGE_ERRORS: + raise StorageUnavailable() from None + + def close(self): + self.http.clear() + self.probe_http.clear() + + +def unavailable(): + return ApiError(503, "unavailable", "Хранилище временно недоступно") 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.example.yaml b/compose.example.yaml index 76a785ff..b9904c18 100644 --- a/compose.example.yaml +++ b/compose.example.yaml @@ -1,12 +1,8 @@ -# Topology worksheet, deliberately NOT a runnable stack (no application supplied). -# Implement compose.yaml or stack.yaml; do not submit this empty file as deployment. -# Service names below are logical roles, not imposed container names. +# Учебный список ролей; запускаемый стенд находится в compose.yaml. x-lab-number: 4 x-required-roles: - api - postgres - load_balancer - s3 -# TODO: implement real images/builds, network wiring, volumes, healthchecks, -# credentials and bootstrap. Details: contracts/infrastructure.json and README.md. services: {} diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 00000000..35cf864a --- /dev/null +++ b/compose.yaml @@ -0,0 +1,218 @@ +name: mtusi-messenger-lab4 + +x-s3-env: &s3-env + S3_ENDPOINT_URL: http://minio:9000 + S3_REGION: us-east-1 + S3_BUCKET: ${S3_BUCKET:-attachments} + S3_ACCESS_KEY: ${S3_ACCESS_KEY:?run python scripts/init_env.py} + S3_SECRET_KEY: ${S3_SECRET_KEY:?run python scripts/init_env.py} + +x-api-env: &api-env + <<: *s3-env + APP_HOST: 0.0.0.0 + APP_PORT: "8080" + APP_ENV: lab4 + LOG_LEVEL: ${LOG_LEVEL:-INFO} + SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600} + SESSION_STORE: postgres + DATABASE_URL: postgresql+asyncpg://${POSTGRES_USER:?set POSTGRES_USER}:${POSTGRES_PASSWORD:?set POSTGRES_PASSWORD}@db:5432/${POSTGRES_DB:?set POSTGRES_DB} + CACHE_URL: ${CACHE_URL-redis://redis:6379/0} + CACHE_TTL_SECONDS: "60" + CACHE_TIMEOUT_MS: "300" + CACHE_RETRY_SECONDS: "5" + DB_POOL_SIZE: "5" + DB_MAX_OVERFLOW: "5" + DB_POOL_TIMEOUT: "1" + CLEANUP_GRACE_SECONDS: "3600" + CLEANUP_INTERVAL_SECONDS: "60" + +x-api: &api + build: . + image: ${API_IMAGE:-mtusi-messenger:lab4} + environment: *api-env + networks: [edge, data] + expose: ["8080"] + cpus: 0.50 + mem_limit: 512m + stop_signal: SIGTERM + stop_grace_period: 10s + healthcheck: + test: ["CMD", "python", "-m", "app.healthcheck"] + interval: 2s + timeout: 3s + start_period: 5s + retries: 2 + depends_on: + migrate: + condition: service_completed_successfully + minio-init: + condition: service_completed_successfully + restart: unless-stopped + +services: + db: + image: postgres:16.6-bookworm + environment: + POSTGRES_USER: ${POSTGRES_USER:?set POSTGRES_USER} + POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?set POSTGRES_PASSWORD} + POSTGRES_DB: ${POSTGRES_DB:?set POSTGRES_DB} + networks: [data] + volumes: + - pgdata:/var/lib/postgresql/data + cpus: 1.0 + mem_limit: 512m + healthcheck: + test: ["CMD-SHELL", "pg_isready -U $${POSTGRES_USER} -d $${POSTGRES_DB}"] + interval: 2s + timeout: 2s + retries: 15 + restart: unless-stopped + + redis: + image: redis:7.4.1-bookworm + command: ["redis-server", "--save", "", "--appendonly", "no", "--maxmemory", "64mb", "--maxmemory-policy", "noeviction"] + networks: [data] + cpus: 0.25 + mem_limit: 128m + healthcheck: + test: ["CMD", "redis-cli", "ping"] + interval: 2s + timeout: 1s + retries: 5 + restart: unless-stopped + + migrate: + build: . + image: ${API_IMAGE:-mtusi-messenger:lab4} + command: ["python", "-m", "app.migrate"] + environment: *api-env + networks: [data] + cpus: 0.50 + mem_limit: 256m + depends_on: + db: + condition: service_healthy + restart: "no" + + minio: + build: + context: . + dockerfile: infra/minio/Dockerfile + target: server + image: mtusi-minio:RELEASE.2025-10-15T17-29-55Z + environment: + MINIO_ROOT_USER: ${MINIO_ROOT_USER:?run python scripts/init_env.py} + MINIO_ROOT_PASSWORD: ${MINIO_ROOT_PASSWORD:?run python scripts/init_env.py} + MINIO_BROWSER: "off" + MINIO_SITE_REGION: us-east-1 + MINIO_API_REQUESTS_MAX: "32" + networks: [data] + volumes: + - miniodata:/data + cpus: 1.0 + mem_limit: 512m + healthcheck: + test: ["CMD", "python", "/opt/minio/healthcheck.py"] + interval: 2s + timeout: 3s + start_period: 10s + retries: 15 + restart: unless-stopped + + minio-init: + build: + context: . + dockerfile: infra/minio/Dockerfile + target: bootstrap + image: mtusi-minio-mc:RELEASE.2025-08-13T08-35-41Z + environment: + <<: *s3-env + MINIO_ROOT_USER: ${MINIO_ROOT_USER:?run python scripts/init_env.py} + MINIO_ROOT_PASSWORD: ${MINIO_ROOT_PASSWORD:?run python scripts/init_env.py} + networks: [data] + cpus: 0.50 + mem_limit: 128m + depends_on: + minio: + condition: service_healthy + restart: "no" + + cleanup: + build: . + image: ${API_IMAGE:-mtusi-messenger:lab4} + command: ["python", "-m", "app.cleanup", "--loop"] + environment: *api-env + networks: [data] + cpus: 0.25 + mem_limit: 256m + healthcheck: + disable: true + depends_on: + migrate: + condition: service_completed_successfully + minio-init: + condition: service_completed_successfully + restart: unless-stopped + + api-a: + <<: *api + + api-b: + <<: *api + + nginx: + image: nginx:1.28.0-alpine + entrypoint: ["/bin/sh", "/opt/lb/entrypoint.sh"] + volumes: + - ./infra/nginx:/opt/lb:ro + ports: ["127.0.0.1:8080:8080"] + networks: [edge] + cpus: 0.25 + mem_limit: 96m + stop_signal: SIGQUIT + stop_grace_period: 10s + depends_on: + api-a: + condition: service_started + api-b: + condition: service_started + healthcheck: + test: ["CMD", "wget", "-q", "-T", "2", "-O", "/dev/null", "http://127.0.0.1:8080/health/ready"] + interval: 2s + timeout: 3s + retries: 3 + restart: unless-stopped + + tests: + profiles: [test] + build: + context: . + target: test + image: mtusi-messenger:lab4-test + environment: + <<: *s3-env + TEST_DB_HOST: db + TEST_DB_PORT: "5432" + TEST_DB_USER: ${POSTGRES_USER:?set POSTGRES_USER} + TEST_DB_PASSWORD: ${POSTGRES_PASSWORD:?set POSTGRES_PASSWORD} + TEST_CACHE_URL: redis://redis:6379/15 + networks: [data] + cpus: 1.0 + mem_limit: 512m + depends_on: + db: + condition: service_healthy + redis: + condition: service_healthy + minio-init: + condition: service_completed_successfully + command: ["python", "-m", "pytest", "-v", "--no-header", "-p", "no:cacheprovider"] + +networks: + edge: + data: + internal: true + +volumes: + miniodata: + pgdata: diff --git a/evidence/README.md b/evidence/README.md index 8df70015..e318b0dd 100644 --- a/evidence/README.md +++ b/evidence/README.md @@ -1,5 +1,8 @@ -# Доказательства +# Доказательства лабораторной 4 -Храните небольшие обезличенные результаты: команды и stdout проверок, JSON/CSV агрегатов нагрузки, EXPLAIN, dashboard JSON, текст разбора отказа. Укажите дату, commit, аппаратные лимиты и профиль нагрузки. +`lab4/` — полный прогон текущей версии: `python scripts/lab4_evidence.py`. +Внутри: `cluster/` (проверки lab3: идемпотентность, failover обеих реплик, публичный failover, rolling-замена, persistence), файлы `00`–`10` (вложения, отказ загрузившей реплики, restart, отказ S3, private bucket, 10 одновременных загрузок, cleanup, собственные тесты) и `result.json`. Успешной приёмкой считается только `status: PASS` в `result.json`; FAIL/RUNNING — не приёмка. -Не добавляйте .env, session tokens, cookies, приватные ключи, credentials, реальные сообщения, raw dumps и signed URLs. Для скриншотов скрывайте секреты. Большие логи/бинарные traces сохраняйте отдельно по правилам преподавателя; одного скриншота без методики недостаточно. +`archive/` — материалы предыдущих лабораторных (lab2, lab3), оставлены как история; доказательством lab4 не являются. + +Для повторения: `python scripts/lab4_evidence.py --base-url "http://localhost:8080"` на работающем стенде. Подробности — [REPORT.md](../REPORT.md). \ No newline at end of file diff --git a/evidence/archive/lab2-history/01_check_contract.txt b/evidence/archive/lab2-history/01_check_contract.txt new file mode 100644 index 00000000..9cd80901 --- /dev/null +++ b/evidence/archive/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/archive/lab2-history/02_smoke.txt b/evidence/archive/lab2-history/02_smoke.txt new file mode 100644 index 00000000..ac067707 --- /dev/null +++ b/evidence/archive/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/archive/lab2-history/03_ps_before.txt b/evidence/archive/lab2-history/03_ps_before.txt new file mode 100644 index 00000000..5ab247ee --- /dev/null +++ b/evidence/archive/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/archive/lab2-history/03_volume_before.txt b/evidence/archive/lab2-history/03_volume_before.txt new file mode 100644 index 00000000..2a043dcf --- /dev/null +++ b/evidence/archive/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/archive/lab2-history/04_persistence.txt b/evidence/archive/lab2-history/04_persistence.txt new file mode 100644 index 00000000..eb7a745e --- /dev/null +++ b/evidence/archive/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/archive/lab2-history/05_ps_after.txt b/evidence/archive/lab2-history/05_ps_after.txt new file mode 100644 index 00000000..f812e4a2 --- /dev/null +++ b/evidence/archive/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/archive/lab2-history/05_volume_after.txt b/evidence/archive/lab2-history/05_volume_after.txt new file mode 100644 index 00000000..4077c2fd --- /dev/null +++ b/evidence/archive/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/archive/lab2-history/06_schema.txt b/evidence/archive/lab2-history/06_schema.txt new file mode 100644 index 00000000..c383852b --- /dev/null +++ b/evidence/archive/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/archive/lab2-history/07_migrations.txt b/evidence/archive/lab2-history/07_migrations.txt new file mode 100644 index 00000000..98fe6ad4 --- /dev/null +++ b/evidence/archive/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/archive/lab2-history/07_migrations_first.txt b/evidence/archive/lab2-history/07_migrations_first.txt new file mode 100644 index 00000000..9b7ff6ee --- /dev/null +++ b/evidence/archive/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/archive/lab2-history/07_migrations_table.txt b/evidence/archive/lab2-history/07_migrations_table.txt new file mode 100644 index 00000000..6c3e6a57 --- /dev/null +++ b/evidence/archive/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/archive/lab2-history/08_hashes.txt b/evidence/archive/lab2-history/08_hashes.txt new file mode 100644 index 00000000..c29f933d --- /dev/null +++ b/evidence/archive/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/archive/lab2-history/09_auth_tests.txt b/evidence/archive/lab2-history/09_auth_tests.txt new file mode 100644 index 00000000..53e77f15 --- /dev/null +++ b/evidence/archive/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/archive/lab2-history/10_database_tests.txt b/evidence/archive/lab2-history/10_database_tests.txt new file mode 100644 index 00000000..49be0ee1 --- /dev/null +++ b/evidence/archive/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/archive/lab2-history/11_all_tests.txt b/evidence/archive/lab2-history/11_all_tests.txt new file mode 100644 index 00000000..7f42c0ef --- /dev/null +++ b/evidence/archive/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/archive/lab2-history/12_compose_no_secrets.txt b/evidence/archive/lab2-history/12_compose_no_secrets.txt new file mode 100644 index 00000000..b3c25f2c --- /dev/null +++ b/evidence/archive/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/archive/lab2-history/12_git_files.txt b/evidence/archive/lab2-history/12_git_files.txt new file mode 100644 index 00000000..d027d2f7 --- /dev/null +++ b/evidence/archive/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/archive/lab2-history/12_git_secrets.txt b/evidence/archive/lab2-history/12_git_secrets.txt new file mode 100644 index 00000000..273f7eb1 --- /dev/null +++ b/evidence/archive/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/archive/lab2-history/13_storage.txt b/evidence/archive/lab2-history/13_storage.txt new file mode 100644 index 00000000..95934f9c --- /dev/null +++ b/evidence/archive/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/archive/lab2-history/14_dependencies.txt b/evidence/archive/lab2-history/14_dependencies.txt new file mode 100644 index 00000000..552c01bc --- /dev/null +++ b/evidence/archive/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/archive/lab2-history/15_explain.txt b/evidence/archive/lab2-history/15_explain.txt new file mode 100644 index 00000000..9c9b47c3 --- /dev/null +++ b/evidence/archive/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/archive/lab2-history/15_explain_tests.txt b/evidence/archive/lab2-history/15_explain_tests.txt new file mode 100644 index 00000000..e6ca2cff --- /dev/null +++ b/evidence/archive/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/archive/lab2-history/16_expiry_live.txt b/evidence/archive/lab2-history/16_expiry_live.txt new file mode 100644 index 00000000..4129e818 --- /dev/null +++ b/evidence/archive/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/archive/lab2-history/17_mr_regressions.txt b/evidence/archive/lab2-history/17_mr_regressions.txt new file mode 100644 index 00000000..b297dcb7 --- /dev/null +++ b/evidence/archive/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/archive/lab3-run2/00_environment.json b/evidence/archive/lab3-run2/00_environment.json new file mode 100644 index 00000000..2ac67756 --- /dev/null +++ b/evidence/archive/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/archive/lab3-run2/01_docker.txt b/evidence/archive/lab3-run2/01_docker.txt new file mode 100644 index 00000000..3da53c80 --- /dev/null +++ b/evidence/archive/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/archive/lab3-run2/02_compose_config.txt b/evidence/archive/lab3-run2/02_compose_config.txt new file mode 100644 index 00000000..82f18769 --- /dev/null +++ b/evidence/archive/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/archive/lab3-run2/03_container_limits.txt b/evidence/archive/lab3-run2/03_container_limits.txt new file mode 100644 index 00000000..0f716088 --- /dev/null +++ b/evidence/archive/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/archive/lab3-run2/03_limits.txt b/evidence/archive/lab3-run2/03_limits.txt new file mode 100644 index 00000000..99a2453c --- /dev/null +++ b/evidence/archive/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/archive/lab3-run2/03_processes.json b/evidence/archive/lab3-run2/03_processes.json new file mode 100644 index 00000000..52f26628 --- /dev/null +++ b/evidence/archive/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/archive/lab3-run2/04_smoke.txt b/evidence/archive/lab3-run2/04_smoke.txt new file mode 100644 index 00000000..2c8a074f --- /dev/null +++ b/evidence/archive/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/archive/lab3-run2/05_concurrent_idempotency.json b/evidence/archive/lab3-run2/05_concurrent_idempotency.json new file mode 100644 index 00000000..8b48d5e8 --- /dev/null +++ b/evidence/archive/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/archive/lab3-run2/06_failover_api-a.json b/evidence/archive/lab3-run2/06_failover_api-a.json new file mode 100644 index 00000000..eb8124b0 --- /dev/null +++ b/evidence/archive/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/archive/lab3-run2/06_failover_api-b.json b/evidence/archive/lab3-run2/06_failover_api-b.json new file mode 100644 index 00000000..29b21708 --- /dev/null +++ b/evidence/archive/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/archive/lab3-run2/07_public_failover.txt b/evidence/archive/lab3-run2/07_public_failover.txt new file mode 100644 index 00000000..4a5f8ea4 --- /dev/null +++ b/evidence/archive/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/archive/lab3-run2/08_rolling_api-a.json b/evidence/archive/lab3-run2/08_rolling_api-a.json new file mode 100644 index 00000000..4d3ac9c3 --- /dev/null +++ b/evidence/archive/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/archive/lab3-run2/08_rolling_api-b.json b/evidence/archive/lab3-run2/08_rolling_api-b.json new file mode 100644 index 00000000..5a295962 --- /dev/null +++ b/evidence/archive/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/archive/lab3-run2/09_persistence.txt b/evidence/archive/lab3-run2/09_persistence.txt new file mode 100644 index 00000000..8f3f3250 --- /dev/null +++ b/evidence/archive/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/archive/lab3-run2/10_processes_after.json b/evidence/archive/lab3-run2/10_processes_after.json new file mode 100644 index 00000000..ee308ba7 --- /dev/null +++ b/evidence/archive/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/archive/lab3-run2/11_nginx.txt b/evidence/archive/lab3-run2/11_nginx.txt new file mode 100644 index 00000000..7e06ee2c --- /dev/null +++ b/evidence/archive/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/archive/lab3-run2/12_student_tests.txt b/evidence/archive/lab3-run2/12_student_tests.txt new file mode 100644 index 00000000..6d9c9551 --- /dev/null +++ b/evidence/archive/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/archive/lab3-run2/result.json b/evidence/archive/lab3-run2/result.json new file mode 100644 index 00000000..5c61a6b5 --- /dev/null +++ b/evidence/archive/lab3-run2/result.json @@ -0,0 +1,4 @@ +{ + "status": "PASS", + "at_utc": "2026-11-02T14:38:48.559702+00:00" +} diff --git a/evidence/archive/lab3/00_environment.json b/evidence/archive/lab3/00_environment.json new file mode 100644 index 00000000..974f7c3a --- /dev/null +++ b/evidence/archive/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/archive/lab3/01_docker.txt b/evidence/archive/lab3/01_docker.txt new file mode 100644 index 00000000..f9e83429 --- /dev/null +++ b/evidence/archive/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/archive/lab3/02_compose_config.txt b/evidence/archive/lab3/02_compose_config.txt new file mode 100644 index 00000000..82f18769 --- /dev/null +++ b/evidence/archive/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/archive/lab3/03_container_limits.txt b/evidence/archive/lab3/03_container_limits.txt new file mode 100644 index 00000000..63b39049 --- /dev/null +++ b/evidence/archive/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/archive/lab3/03_limits.txt b/evidence/archive/lab3/03_limits.txt new file mode 100644 index 00000000..b7594e29 --- /dev/null +++ b/evidence/archive/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/archive/lab3/03_processes.json b/evidence/archive/lab3/03_processes.json new file mode 100644 index 00000000..21b5e944 --- /dev/null +++ b/evidence/archive/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/archive/lab3/04_smoke.txt b/evidence/archive/lab3/04_smoke.txt new file mode 100644 index 00000000..5bd059eb --- /dev/null +++ b/evidence/archive/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/archive/lab3/05_concurrent_idempotency.json b/evidence/archive/lab3/05_concurrent_idempotency.json new file mode 100644 index 00000000..6fe27a66 --- /dev/null +++ b/evidence/archive/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/archive/lab3/06_failover_api-a.json b/evidence/archive/lab3/06_failover_api-a.json new file mode 100644 index 00000000..288c5b0a --- /dev/null +++ b/evidence/archive/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/archive/lab3/06_failover_api-b.json b/evidence/archive/lab3/06_failover_api-b.json new file mode 100644 index 00000000..94d777e3 --- /dev/null +++ b/evidence/archive/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/archive/lab3/07_public_failover.txt b/evidence/archive/lab3/07_public_failover.txt new file mode 100644 index 00000000..4abdf54e --- /dev/null +++ b/evidence/archive/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/archive/lab3/08_rolling_api-a.json b/evidence/archive/lab3/08_rolling_api-a.json new file mode 100644 index 00000000..cf3e3d0e --- /dev/null +++ b/evidence/archive/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/archive/lab3/08_rolling_api-b.json b/evidence/archive/lab3/08_rolling_api-b.json new file mode 100644 index 00000000..fe932462 --- /dev/null +++ b/evidence/archive/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/archive/lab3/09_persistence.txt b/evidence/archive/lab3/09_persistence.txt new file mode 100644 index 00000000..efab0153 --- /dev/null +++ b/evidence/archive/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/archive/lab3/10_processes_after.json b/evidence/archive/lab3/10_processes_after.json new file mode 100644 index 00000000..52f26628 --- /dev/null +++ b/evidence/archive/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/archive/lab3/11_nginx.txt b/evidence/archive/lab3/11_nginx.txt new file mode 100644 index 00000000..acd07f29 --- /dev/null +++ b/evidence/archive/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/archive/lab3/12_student_tests.txt b/evidence/archive/lab3/12_student_tests.txt new file mode 100644 index 00000000..2010bfaa --- /dev/null +++ b/evidence/archive/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/archive/lab3/result.json b/evidence/archive/lab3/result.json new file mode 100644 index 00000000..c27bebd4 --- /dev/null +++ b/evidence/archive/lab3/result.json @@ -0,0 +1,4 @@ +{ + "status": "PASS", + "at_utc": "2026-11-02T14:15:07.979020+00:00" +} diff --git a/evidence/archive/local/01_check.txt b/evidence/archive/local/01_check.txt new file mode 100644 index 00000000..3f0ce0af --- /dev/null +++ b/evidence/archive/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/archive/local/02_openapi.txt b/evidence/archive/local/02_openapi.txt new file mode 100644 index 00000000..d9b61e43 --- /dev/null +++ b/evidence/archive/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/archive/local/03_static_lab3.txt b/evidence/archive/local/03_static_lab3.txt new file mode 100644 index 00000000..51ab911b --- /dev/null +++ b/evidence/archive/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/archive/local/04_unit.txt b/evidence/archive/local/04_unit.txt new file mode 100644 index 00000000..211c7d3f --- /dev/null +++ b/evidence/archive/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/archive/local/05_collection.txt b/evidence/archive/local/05_collection.txt new file mode 100644 index 00000000..68b17e53 --- /dev/null +++ b/evidence/archive/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/archive/local/06_dependencies.txt b/evidence/archive/local/06_dependencies.txt new file mode 100644 index 00000000..8f32567f --- /dev/null +++ b/evidence/archive/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/archive/local/manifest.json b/evidence/archive/local/manifest.json new file mode 100644 index 00000000..6ad0db03 --- /dev/null +++ b/evidence/archive/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/archive/local/runtime_status.json b/evidence/archive/local/runtime_status.json new file mode 100644 index 00000000..b1fec732 --- /dev/null +++ b/evidence/archive/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/evidence/lab4.zip b/evidence/lab4.zip new file mode 100644 index 0000000000000000000000000000000000000000..3e3a4f57c3ac7b12b253c38406aee8b7389190e8 GIT binary patch literal 30304 zcmcF~V|1n4(r#?qcG9t(bnK+#j&0kv?M^zjZ6_UeY}+>O>T~Y5_dehGbN}2m#vE&{ zG2S1uo~nAP-Z`IAkOl=q0|Ekq0-^&o(N-^M7ElKKQv)m4G2F*)^Rgh5VadR}&dc2;(5TvBBkw9aqG-8}tRaJz42 zd=5p2UZ-jZT#oDo}*2NrW`@o)6~wk;M+IAe^8VUA9{`KxL#V#|BdjKL>&(8N!BpS2>%3|>8Pcr`n| z@Rl6TJ-YmqZRL`RxE)CH_G`;Sri^sOdJdL+iVXkR1^306^O)bIbF!=f`ABrLa0HlC@5h=BHHm%8H;zYLyS2+HpI%H2s^hs;}64!F4N=YhcEnvdkMW z_q;h15?Bk zL5t+XA&sUg+PkP-SAeO1`Rtr}n?pBTOQ7C!n`%jUS)!UBd8kmSUe&Rz^_|!DT#{Ut z9@S;n$NA!|-&XmHTqJh0RndWqIQV2Q&zfJ(_e=(KD0vOGP~e$pg#}ew27dR>GjDKa~=GbXyP51rS#LU%Uhe#A|9plG%^+;gH42gzLXD4AJd3C>S8rT$f<2 z0*R0Qs%LLk7?b~cqF2FlseUYhH!3y2I@abe2SFMcX%&k+KkJoI`iR8^iw(Cb$7W+MxnoI0t|dF zA9v>-a$p~201<2|?=Z58hmay%NUgeAK&ZsYuR0q|m3KAQb_=!r<^82c{En%(6#W~F zm{eQ6$lkbE&#P^*kYEg2SmLy;ExDm*J4mh0em!I;(_K(Timze{`|c^GPj?QDzqj*N zK;w~c9rT{H@-@TFmJ!d#2-iF&K19`RqjltbHW zYS0R1H4bKC*LdgQYi@cW=2wW*7Bm^GnPW6>;3mVFql&mT*6*3WjJ$Im2C$6UtO9EX zOhXr#$=*>j4RT6lSxro(5po}4@!)hqn*aapKr|fUy5I*R?H&=>zU zjpBKj-g1<5%sCK-8RM*Z!g#$}!-({Sk>i+;x7R((g9#a z@E2Ary3Tf1w)#fK4!S1#=2o^YfRy}0Nyf3#HbIO?B99Vlz_oHDf$JZ8%p(`E0j$7~ zGR!knu88AgxOL}cYTC}_beuiDHCqKR(CKS9z?S+hM#yX|4Rf#e;Hw`D6`+LOg=+)Y{(}N_MWftN<)ODy(+LA%m>+o!4weeO>r_$(Kg72r( zl2@8j*ow#5`oqd5sX!7s3iLPD0IdKKwGJi?1B70j1|`*E|4sT2|EhOzeC(PK0}BWc z?oM#J#<~fO4C~;*bj?6p$s`KDSfV(k{h?)jqDRWM9SdH_Hic@Ly53g3)5zbFlM^DM z#)A+FhukVRO}~(#NV6jRF#3+thPpp=foO;TmNL*pzsr1iygfkbvvH77Z+y)-E#AZ z!-!DHyg}IXDSt=++8Yb;UiGTEYG`J7u{qCOX1o4dhnFbc#%R10q6stD~=IoBfBfnWA!F}9sD-v?s5wShN+BjobKgooF8rDsR zkg%eoUbJ|t{I|HRWRLGXHWYXd#Xx5+__aOy9Im#ZneUADk8!cbKWBY`95|R`DriM* z+X!sZ+E(>lryW!{LxR!L`d$$J&RrxxoErSXWa$IsZ5aX(5bi(X^#6wFpMKO@-^S<< z>360IS@tnvgF<4A`8dRs$VTq&ONWw(KaT|;*5S$&Jj11fDR5p9TJHFqN>0|q&Nwq-D zAWg%#1Jz`=IN0WQ21>K9ljW$px%Ze=Ms$I$8ACEf=2?*x<{sWL^s*C2ehyWq++~81 zx@PoD;%r!<78gOngyc!#woq;8&8!F>1aX1lahlo!M!c8Nc$IWrSU(2oxUjiO!G7%F zzD7xSa!!kaY{=pW>)<|dMpi4#PF)&=kz3=709C#65LdJ^+xAFTiQ_ORQ-G`gcMBmR z#g+~`waO{ecA}aEc)v1sP+pbB-7Se4|1E_H&;c$IkP=N)pdxO>D?v7}>9_a#F_zSJ z*IWrZjGq!#LJrCUsmf`j5;0H@{oBjC(_y(%S7Htq#XawbSIWZe?Xi8+VB#nzoJzP0 zK}&;1TZ^cvuI^_M=pSnLZK`^!)siZu3S=$8JC-Td;Wky6cVNwSa}Me_x`8MOqF{vF zW=UAfQW@|xJRH9wvOLaHgAQgme~HJ`2_IH1x?4D35@ts0{_T1afa}Nb`A*CMuCIp# z0>b#q^=!J1EV{PNPWq4Ug?b$ zR=K|9l!WC{5F|;?EhejPe;8j$T%RxvQXu}~pesaMzL0@~-$~ICtF8%D>^!x#RTD`WqD1Vdt1wz zARM|fOx^+SHsbC*71K)8G|ag#>Ea-_OV+Tk6oLEOIUE8WHx9-kcsNBf@UXn=SbQ&7 z6&vZWJQnvnv~yBw>&jtznWr>oM3Q8xM|us|vKEU5&8nETjz<3k69!N!RU#_G~4i@dCa&_)1>|mlI9L z=X!D^JQ;e5e8J0}GJxIqNBtlJI7eI8S~(WrlyboOyQZ@18ra%8IRe@aJ3tY!GuC&~ zW%^e?7A*wR$A~8K=)bnjuvS#kqImps>;i^QF#P!&bd%(hWW+Fs4MD$Q8W*zoqkmka zX9TtpT%W#bw|`rTgoT;hLkJQ)squ>U)o)g`$|C(7!lVJhCO@4{&9>A1s(Hk}Oop2?h{vV4nUTcA}ctm=vM`u&KvzK7NW$r$h$QJ7m}b0#Yn={dJzCF&D7J zn6YRqBUnMRRz8r3s?hpTCUOH8Q zc{c$bNdu(K-)zaQYv*9@qVHs^Yv2rcWBzwt4~`R(9uNdHgs|_3g@=4$9yb(E3?jnZ zvX)E7<8ek9sm7n}>5X+^0nKrb``0>?>s6PKGF;gUTId+e3SY~)dGBLg5ns#c3n=iS zC`N+8q8%-IT1%_MRRg(TuM_ktwgnGPuJAxgyA%vELf(g}+;TtDl;9oM_QP|bCf=@L zt@rk6*W;6ipR89LI&JvPLbkmN_g0%cUT=X&s?XNnrVD0Nxl99zSf7~;m3tJu!Q}jc zeRHK8$bgmaU(QAQU@4=Tk2qYMzJHQqAkhS!=WAAX9^mYbF2CBi9=rVb8y8vtE^bA; zm*arb14}@wL--e!aQtbJ4V@hv{&dU#ZU!Cy#VP0Mt1+uIXd#!h_xN*C90 zbZ{*~&Q_RbP0RW37lWZ>F)YQ(3*#adHUV3v#+8VmmV=Lk0%Ve9r&6vPt5TP)^ zAsbrmmlaceT{GWx)h+}qckE7icwjo-H$7J`wnlO|Xr=7b)f5nj) z;U~+aI0q4#eSKcy&1ZY6pde6Hpg$u57&1qMzu!K{2Q~ySc&^*=$%LLBP<4>MeZYuA z=sFAp@Q5y)9Vn1CbU(Z;3|6c}69R+`73kK)4+u0=9{=hgyX`(ggSempWzbG8h;X9V zhpNFIKP~8fl69Qub(oOk9Ppr@pjPfHUF<=IKHT|sW19BPmrK3t9En}5EH^3g>^H5RvhWP#UeX{H zcPkYuWmhU;!{5=z(z~=QXMflBd+=#~+p}Ci1CgTGjp+7aWlheh$-*A32G7&Lj8hq4Qjb`E6YTdD+R}uvL`O>qJL+T3Q7a}w4wtqk557MM zkNj@T`tS=aW^t+!YoUyChX&}^a&q`xd#~TMR5)t{u;RlIW&c+!go(!=!VkK$y7R_K zDjou3u*DsaR#ZH3VTarTjnqcPJ|eN!blZW}?43U9D_NGDNi4+`kBx+0>D`GZ|xj8)rLRBL{a~2WOi< zM9(%}`=@3FZ}Y{uwxEfB<5or*Y_?+{enbm2M@dlD9B_R^Xv?;Jn)2strG#m1*i#l7 zBI+7H11(7vUD_I18#S?|+wbI4BC8K`yach*s%O=5`L<9p-|om>XS%A;4ht`Sf&~o! zHfy#2f;dVsDpFbTWLAjIzDbda)x0eQ#jbXxysfn7!E=9TKX>wv^EFj~9p6Qr!5jc& zW)}L-*EcR*M<-{aKWyd%c!P8NW5u4fb>ga+!^Z{9C#3r5x_6l5PWGN^(o)?*SNjv0 zhN?qXg>nfnBUv6e6BNV}xp(qs&IL9Qo*!5$QTV+|u!`mv$t`z}jkX`@$Gso*7K(PlS{nj%c|Lsz&R@+=e^2T6yTs$^G+O(BFn$b)%LNxsj60>>F{gyhoa zOuy7tBI9mevkLP$r6ML^zeu^sCHPr@4_ittFh&xXJ?vuw>6y8nQiWq_rO(n4UsHZD zAxo;!l)$_)Ca4wpZe&&2iSd}Py|che3avX$x2nJE%8|j8GkK}oD<0l)b-&8doWZ}) zy2Z-FxyVXMJR07lrvc3ovqTp27G<<5**2j@*IGLi!j%*4hF2-hZ}=EX_Z@cn+Kq0j z@^&kgj$-s?2rHAlhjv8O_`^%#Hj~T+a#S5ed@RUVS!>ZO$gg)l6Him`V&UGl`DC-S zhHE01&}?Ynqrm&~<6QSe;~icXdDh-m#*m+~iYt?AfN`Rg0OGdix)dF0JsUf3UoN{; z`mMBky3!nK@o3Keywp71F)>FhLkzSd$+XcfA8 z$Z>wBUWg4hYuY5J#dD1H==BJzhzuHAL;??L?!_~t1)iaDFR1~^+`3D z=DhGMMsLNuFh$d}tL^4HJRT6jK`wHn50$F1dYELR!}-Lw_|(dQ|@j|YY>J%MOhGXjtimB zIhpU`x4jsPI1b*1>_1+^zi-Z_p~wlu?T{tn#WXT&*l(HORV<)GYS}nZop^tbOH##~i$+tQ;+{7UtlswGq9BwK>~F z1Jk92Ly!DzRw-(a#^I^$wX*4i!3BsBwmYA@q`|ix~V2&Jus!X^f1YVbBd{r#G~oSOahaG59_@L zh7gz^3z)nyWfhC}(8Ki3hkhT&S&jKQv%;p|f*);Skk9s0aT-drR|gMuFZ zJ0qTC2@&aZvINk zcN1pv^kY$JJi4r2_H+Kt{YA4Lknc`~Z?6tJ^bGHQrcqj6Lg{OS^<&vf?<-hvMyPC6+w9t*a>TjVx>@ zH6)S)UK=^%V?qzdxyeMpbQ#(O=*lX5#e>eEIcW zFDA{ogmPptOSS&IQsEyWsCyw#ghpGj0%OKk5JF5;`gj2TOIyxc4Vbiu{{3PiH59D@ zgs~F1%=2{h!KqHk1a|CgvH9|I_x{I*C29`ei1Zw81w?E(ue`F7_Wq3v`nZi>Yr6?z z9aSB9Bc{4YB|3q1B3O210N(hqYbIKvwVo-`ARWTfnk zCswH?su8NuW3$197KA}HG$|?&&f%r(nzS$ZU<|3MrR?mU_Z9RyGH=(W2tEokQ<1Jzb0(FSyD`RsiW5-JtE?7S0wOyu%Sh0=eoymlU66nc3lRfpSoyu z`qV;q3ok-H3?PIRI7;=OOc-ZP>#0Ws)HuGb+GEUR2?3E`?FWQRl4z;(1IwEAkJDwH z!hAqqCpgF@l26#=Dq8(kNq$9_f4W(cUciT>i?U&aVJ>OPckOt)iS!#}f){xn-mO*b zD$FyHJJba0RB9Ot8Ai=tB6XJwKz9MN75dpSLIug^=t$bNr+S|U33Bf55DNxO+k+iS z;9=JiPL_w+lPVeSfepSb@x1;>c5s*YaIyNsx68Y7-8|!?U4BaJgP4~7a7Zt(SA!MP7ZFA^U%9 z=zsUF(Wm(ZOnwWJK;#ntvkY||S_?#i1-MOb4+NBR!w)9K^elcEY*}I%SYJtIr>*u#kx%*UuUh%wmx;mA567bUScKwN zKB7I@!mNH>*|y)pplykuerShL{?3Dp8G-8l# z13$!^HpVd|167GoJGbB*Y`hW~oZHc72TjT&)pJB;<;LD)0~d9gpZIxLt#9edn=_)m z2-8$w&hka@YWmE5ncv5Yg8N4hAxGN=MFyg9lDAAwD3$GREP2aUM>F=>K^EkRtjMqh z)_!Dz-@uWs4jODv9#2=@VklhRgc+xy0^S0ds8}Qr z2#F`3{S}?c4~h%E`-sxk^)aX)H;CoD^0pL-!^6YZ`D`Z34hAP}{Qk5bjL*$8NN%?b zcF)U0L9AiKTOrem3ZR%v#u5Y-3TeYLBE|%B8QNMuq|)aa%34x!1cG6tMU_e=NK$0f z<7U0U0zwn+mj~Qc$x7^!7AB5NYsY7@L#I_CtID9QLsZY(Tv#Bgyu>s*v4 z9jWa!VVO}E8Ko#%wLo<&(yP(tbhtZ$X^OPffK!}hImku~wcZudNor3-W=*W`0#X~l z_lH{%Xd&h?@)Ry*rZ17N<~`1=tH8MtX^5PJAJc>3fz#b2b8CF>8yBpA+80f+L&R)4 zEjvIvXyFnV9Pclo-hM+aa@SEEIHyi$t^X#Q16C5C4IWpmo)OCV3`alj8bX?o}dxgKP-@! zc4-r4R70!U8kb9vasAM|M8n&n2nI4v^Hb|RGg$5AA-u-1p3>VoI47zxhJuR2u-hMR z(9vma+HZb@bx|-q^|Nt{_2mn`FzsHTcbUIH*MEb^fh+O8pI2+K2$X)Sg11dVu!DE= z<@-F#%!KrPzS&RCyC)R*yt_IGb+t`YEAR0zd|vo*>HB8a{ytVMfNiVklnry?)OMB1 zVSmMyDMLc~va0L*t6z7cdeg^K76(R?em0Cy3%(8?8mZ=62tHPj%E))c08-7L?2Pgd z72?vRfd(l_pp*eM@_bJhI@jnX#p4#aFFz;u^yncukbMjpz;a;1Bsq^<%}|Qyq;8Zz z95ia#&W`;Yw!3c-3wvMBLH)8dPn-moL+3sm*%T^EL8@Rnx!wzL>@qI6EqWl4Av6sy zU|K$OzGgwR#Uq0qOn`|&Rf1qxx>^G9$_AVReHGjHQLE`Q#zu?^VOZ)bXJ2w{%7K9A z2g&{_MwpA@&IM&JW6D}cX4@LsRdat3>Bo2GAvgQ=3y%hfV`lCMbIY;dvV1??W#A}W z6C&$8$~uyAh3PT39I`q}*+qr)3s|n~5rS%iiaQkS7~MmVFe2*fUg3=QONE5+6YtdS z2Of&Jjr(oZwvDowln@!l35}X}$a`fO#%9R5)KF1#rAt(g; zJi+iWTx83ZSpiu%c51#+;kZbn4uy2CN5gN~nAM0rPlB2{X{bg)Ch(|;9!9Wa;FGTo=unFujRu4LMLV!hy_IYu6sYEY3gbaI-60HN=;LT*0DU1!1Ws&=)A zzY@;dw7yiWm7S%)NTw34kh<$RjKZ#>t&PHjCJDoTs>x2(L6U9jF z2Il`xhZgEhlgp^aMCkDnsLXC2gikn|`+yQzL67c&;6`hIq-mtQR!MrV^=a`3b~4(58Tx>&ayQliPMHTfIMjJyd^6swv#q=xTE$MDHFlm z=&!;Q!-`oRLmWvbP)d{xPU~J&shS}>0pq9rQih5IIg9a~$rxIh{lY4+BLrD1+At0{ zqPO$9oGBcVIDLWe4BV z-mxU?IJ23V3OHRcARJCEIhnat$Tz0oCB!sW(g3EnlT5nzr`ZP|rk8Ajzn+UMy-5A( zVq~EVmS6(fJu6#K2H!3ngMY-J(Lk|Lc^*bB6QkfE)wj(SoILbEp6hp-?t4K#D07uo z6zMrP>0Rqy0i9q`dNRrRo66q=cP@nV4$np!2$>=}1Eae{LgtZnY|> zv3R28;KY)uR7GPS;>C-04+vrhNYJ*itig7)40?B$C^0tfp0b_Xx_rFbIfDlYmTT7QdLhwOW=XR zt55scnbpqe^%qcC79srCk-UNC`sSS3if1np^zmEfFPPoXsL7c$oPIz$zTO|^G3A7c zt@|~!v##v3F5FHp(a5Hno#rC@IjDVWnRo$15K~F&c7r*!x)TvI4JS1W*V&iOj@7iT zp1XbPH<-RiFwo?trVwA>U%5g43$T~Mk zZYjn%Jam{t<=alH4UmXqnjEAlN${&^o1;%()HvkTqh1h0>gO}oETU3j8G7ggZzNh# zz#v={WrWj9gg-g~e=mH8b9xM{`#)zRdh%;PF3!q|x#ip^vR%f3o-dD%tC zFsmvg7e3be58esS`8}yRaW!JZ02P51H@XGRXp(h3%9y6I9N2c6=c@wONZ;UtpuHw< z;2bz8;*9NI1tmuwOT-Tz1Zgik6(!M!G(v!dhE6%=$mJ!Fa z?xw)?GA-H7#jK&Tv(#?8K7pqCG7v0sEOR31v?5cK;c{H|HP^Y^5ieKYA8{^8JD`A? z9Gk#xu+aEu+?095`fNIJ^inb4vQ;Jmx|0NPGDvij#!`fP;2j5|hyL6N%1)A*Fw{)J zC`%<%_S}9NvIY!=I&B`bWADZlw2i@{lfC=3Ny7!`OzG<|uyo)UK9a5#Z1`R-D7I%O z6^(!@${zC8zEO4xo4s0ZtXLTiP6!BLhx=aPjcaLb{c^oxlTvE2_RcX0 zis2dD6cguH%U7cn^5RKi#y ziCnCc_#%c58@S2mIX~ub>sPR8x>H4wVxy8xLr~%o$#TAV^M~_dLWD?NizJ3Z6(#LZ z`|k)*biE|4YNT3>Ut?c0q6@LOE=`C(Nb#lEWt8H|Z|p|&jt3XAS{{{e7*QKC#fb02 z;S_Tf1>M#&9h->4#6)Q3j&&IDI)#i5!jvH;w-2{SOWeCh8pw6^C(FOUk4@P#-1+as z6ML0i@4^&e-_%y3{Q&BZY@~rebX0NFDa86!LiMO+Poi*MNPVv>&>aL#uY zjoIIb&4m2#7!Xk1+@*6PM?JAO#&Rb>w-@JA-O;sfy#+TFi8q`NNB8Hk&4e~8fkobM zG%5i#hQY8<6Y!@NCEH6wy-VZym*S0&T24K-^8;m?MzxdVTm`OWY-|j=#ue&Syk0&! z!wnAXxM}AlPy|`;PVWLO)L)$WGZDuZ=T7=(`6xA{ho|oKpAg0sLkqdN)enZ^=89vJ z^v})U6ZJG8z+Nw>_^f@$r z&jL;)_~x5WbDHgokw!(KEtcWig&FT~1 zttb_Fz^mGv{SX9x-v~{A5JeLhHI{>m*bKfC^e|+`*%_*QGqb#xF|1f`u|M>Rw;Ov~ zh&r&SPB-i{8G;R3ZDLl(9$AHO;(sRM<^D~!3;RSBY{bG@aoQE4*I2|4nN(ET{&W0- zfis}aLp3R$h={o&UXVKqjikCo0k&~S^wF6bPHi1&%mQ^uicZ0N4E zN02P_`7(EJDtHlmFj8KLS|bL6B&TE4I<4@TPEsr}wA0u3v{t;3SQ_sD$r2zWZaGt@e5d@S} z-dDHR(jbm#f=v1h{?T~p&9)Juk5KxV%im+KIs7GZ`Ortg^C6#vnhK4>PEG^}L!6Tx zpnqRDsRJ%*Sl>%F@Bp{*-vC-Wy#Kj*W@6MeF}KmT0^G~n8X7w~8vpy+xe{ zk`Mj}HJ+**H%b_l@eQREm7++G@2oIxzbE-{U0sn9LIZ=|yCS%Z_lg}8UF^(OUA%wg z?nKahm0cXOQJ3y+!+K$!WR7xPL2z-v&Hxq(R#1|N`}%eF0Hls650h|wUt$HMTx=%; z=deh!jEyq6E5h}#EAE))%rt!pQYO?MZ|Lx2DDA-Fd%8S5XPD*XV$4OaEr?amruzdj zi-A`Q{6y$G6VRk~qdsq0{=yh-A5Ep0yeJwid)|S=&oneBkOH0{l3p0H0(5xclbm_Ydk3ku^|(3 zO4A}>rMQBSZPZzZ>jo{aztkr{K&JpjCjiL5KJb8F|C&ztzq)FMza8*rkb!Gl9%vBo z$U9&W{qIKs0}cOMk*HaJupntJdfd=Je~fE}LS2Fa_5Khh9GKdoew3?;-FVhO?TNMTy47znur2Kw|qBT+-UPWlAwp=~0kug?2U;tzA`0o7r2+W)3zq+A_ zZl@F05eFS65d`OoczY|>eX;1PZu%z9qt+<-b#$g3fuma!$7++iJ4a>VKJ(a!+r1|Q z1k;WytObvgjILUW)8vA8r*Q*q!nDi4QdbSK zAw=P%GxnCR;b#zZEjqiw*;t<(rk3@>(ozP5pvqU3(lQKabLP^c=V&e>zxt%XYuBy) zO988(`1CFN^g)^wt@4T_l5eeax^VJFlBjWd^zz3}vpn8c5Sj*WA&E^KU(aD!INwTa%Yk#I4c2Y47Iy`lc?w=aOu zb)5fR_CGT?B>xq<%zw0x|6gZu9AUry$1DyDU{CAZ^@cnEE-L^70wVjbJ+b^z)Hvyz z+WR_4~`|GtB+Oq8`;6NC%7^nZh_zgvWdOXT{Ag1?(|QJ z`_!>s2~v=NCEty#RP(u$GSh*#PPuHu9Wq}oI}5#Un`)O`Mr>}T{P9cd@&aO6hr#sW z*+=K$@OExk`diWST3?Zk6U(K~Tb^$RVn$oG>udkiW{Nvbj)@oh_Bv;yeH*Vr0Buj7 z60h0_KYE!W6fHMM%-}b}1&|0n6@)a9P?D}jKWxr=ToA_qC@nrtwNh0`(FPZY0( zLB1kF1AEwK6#%m)(eES#4A4VZRQgqKdD2v-muFB%k~PUR92nWrC({gUm`vlc=3RJ6 z)e3bs&UN0^l=yEqX@skqwT7;3splsXsjKtbEmgOEF9So{Ew;bjoLrpN54x@l?LY7E6Cn``nD0DE*bco1kIPQ2tqN8Da{292 zTQdIb`{cO)*s!?3KjdwHf)5=t5 zyvEV@ZW`Qyp$@Z52E|QHMXL&<6qendN3?&A=%#M^@?T7K*fhVrIhH@hcIddc^5ra^ zpYn23MMPSn_Rkm9!J9IJh+(gAiYPIa7D*N_7nV1N$-x>!TY<-igAU?=U}XZA6(#O8Xg)m%OHku9A4vduDpL!NnYjxGx!llJr*FnN~O(s_R{`24W&@Om`*n6;Q;?MS! z>T9j$`o4GwAbxGKIId(}^4FI^zz=-!)2UomB8;xvrINO3VFb3O%as#yAUKTo-Sq8# z=DR`f(tXa0H62=IdH=wkQgbu^{CxXIkHZXbOLsvK1{uIDDgd|qr3L+~zOekSE~hYl z3Q%C-hAxAXe}U@b;b64pkJcKko(7V&1Pl&$NH~r9uK+DxhHDAtn%tlCZJZDv@)M-uN<8mvit3 zS1Am$1uGPic9b9SzWRy8VZPgCLII7PHt?w)@)$O=ayg^%Z8Yll-4B0g?g#u;{CMPk zGT&Xa6L{iM%szuw{_n8y&T?qAMAwwX_b2Y1FMoAAP(Z8zeBlLAQ)U5N3k<;cuUU=1 z@MZny*i-GF-otAw7nANbPEm&Ja^a>roRcMSJU_;A_V7+Hc)ERYDuwHRGUNCMYkA8_`0ox=;-{2g~ zWb3Ve?I8YL5!nDp;9}UzWdo2{1T5PBsy=N0)N`5}83U@Lld+AV`@b}uveD8AeMD$L zkFp-2A`>$EgzO3LdVy*lqV##>A7LzAN`s*z{=M) zG?+O+E~&u+0g?WfmF$0wE9vUnnSas$w_;Uw%w`p!f`=Rt0OO_v*+ldem#>8N|5+kZ z@&%e~z5(jyQd`R9OQF;bI~STLFlduW>e_lb@qXx7WO=#OrV9P4mNBu5w+o+e?{jrD zzTzwg#{rP|uB|p*2D`|toPHH!siJkfN|Z_hE^*6zVOf}=s)W1xas6-{IC`%c)lpB8eU}-hijqEaB8QrN# z&Je3bvDt=6OxffeIP8jwx{xvyOPVOU*5EcU$%>Y|I;CIOXA)ne2E+6su%*i*kfW1T z&6=DAE=D+u?!Z8@?X3n7tydeGmr3?WuS^UgP(=pdhd9D~iQEE@LyonGW8#ZWb{$mk zKq`o!vZwQHLQ8e_JvE=)-eDZ~rN^ns8HdZjMir>XQ1lJ8HNKjcO6N5;^nvu`p~JyL z^|pLP5Gpws_^q3^8}*9=C6ryumU9d`FTjDs=SOQj9Mt%Pm)Qwa0TwQ1&L_`Wm@1U> zCXU`E9ltNzj_+-p54_m`&E-$sTvM5 z#X?CV`nds(gkgk@_5~k#QUV=FZB`yPUZ||r?Bo8Wb$iGF$ML}_Pad)6OG6l_iWoC2 z!C|24K=QMWORuz6M`0M-+%d4jqfvzW8d@)blzFaRBk~T=dHo;*LOE;7P&=?GnFE%6 zj|t%Z1UjK?!9oYtZx+lRs_+C^%4rxK^}Oc{NI^NoVYE$R({MhhLNtpv=KhbQVMJ|W zNO0O5K8Rp$m@ie?VgaN~!IwBS&7mvPk*(T8WqF%y7#Te9Syd%SsJo%vD1+XI-34xU zftgCVT495oq3u?AqkVgJ%tdERms4*t@j8i?5Z$8m;|~4v zQ;w9+NttyY_y<%v`UuA?HUVR>PjCMqL2W=%pyy3PFaEzKg~9($3M@cUw0eczx!%pS zAxVTw1+63qD#}I@eQwP6*CtVgukOwK3;|xPAJe`VkGpx_A2dP5KUou9m7NP|3$KDG zasKd3hc7YzWy#aq`-F=*Q)dxaTEv(?U?e4-3yuk`t^#yL79TMa5Rj(2(8O?jb5{k| zY_8qf;B5VU0e#_V##9estipZ`Nz(5+ODR)&BxpJU88~aScJ3f5$9rS3w$@qne7$t+ z{ps3E%#kiOuc9h|n)Yy(`EoZ4tBg4ryNGUHCSxE1?iEC`1SN#LRcTTIGPofka4#Y= zYM%>IHd)n3wGEqwLX*WABz}_7YQR-RMU~?>$sXv9i7+7~$W6wKB?&+9dcc|zhNBQv zd`92le%*9q3m6nvp7?fA?FN~b)|1^mg!2R2Nm7Ecg0Uit0jec5TY7!)&p0#MMmgqm zVj?QEISDWO?J!a-8Zgd>DDGa38@YoK)uYNSCZI$aKjrR;H4Oxj0~0Y;Q9&gc42+^r z+u`oD7^7hJ$9$2PNdxP_2-*d%42rf*_-3cT#-cn|sLP>R*Wc(q+40Wu@=2uA0YJNXH4(5 zB$BSUNVrEL1_837+M8$BO+?AoE|Cu}krF+^=EsZ4QO1BsN%9o|TOahySI-q^h9aV; zOv-DSDKXdqGZ1No$Nk2rQJIq8iHaNUy!54|3Mc#azQKeyAOf+AYY9J4-{^ge=*VA@ ze3Tv71vniQUqB}>wO8lnIZ;aN3yzvZ96isXJ()Ph)Hm2(nfoWQAugsga6S5pKT(dg z&%5uhw|F>;^xD}ATnP~OgcE`XxnyhzZazjPa`!ScS)TYTwi{^#eWNr_S~cNLt4r7% z4zovfi{u!&DIU!~;T)r?7$FS?5BH!QQ!$@D!2d3ZR)D-vU#WihGbM!p$c_Il799UG z8vWOZD?k%03W$6_vFOBoBMg}~Jm)N$u<1P0KgRH|B)+3yj9^`cvnR{L_C%HX^b}uC z5zURI;Z+meO;(!gt?2*nGiSMArGvwu4GYP9N&|o`AnnTZr z4A)DQAfRX{?lX154qk10>5T9^Hxv?3(yPVs8|Jex(t@A%4MLxiW8xp5eaeuWrTHk- zLbEQ^K<2f?3T_WjGQnnPGdSfgQ-bT#vROUX{w%KY;XiljZ@=ETE<$`!7&TpYr^T?k z%1Bh-fMc=r{o=&{y*wC%gv1wkQJ0(sjCs@iaavZDM3p});C4m;M48zT@~K0>vHIXx z?EOzqMFBijd+BBd4mg6@0hD!;|0?U8x(>EhR^~RQ|E%gMt2S%wNFkS?d-zeEG2jSO zYlkB$5yD&gdr;caAcK|P!Q3BX3Y=Bz7K=GMsoRihdf>RSo38j}kX?Tq9ECkku}?w^G%S9g(erQWm9E;*b$y>9fry=9nP`cFCL){2SDJ(=YC(Hx zs7L%JDRZy=y=%cc^ge8N=>dQK$y(F?enqK40ejYLZ`zHZV9v}C-$Gemc86y7j4%$~A<=$NQ;3S2J~GSSpf_TW zw5=(sFH}kiYyQqDl~Ra~qPtl&heX?SkubwmmhGAA6v?pmx5mF5hwj;W*uMSXI&0ks zEGaOEGKYK{bNS8{(-6$!60-s8igPNuH8ynlLq`xZ=c{;t9|$fOBZdqV+=xkDZBO+7 z>Fg|k;#{&ojR%JS!3hu?LU4C?cL{;u?gR}E3GVI$5AG7&U4py2+hE~MZuTxL%iZ_t zLH$KBRDZwJbWhWLzH{Ja*ih}id4Mf=OrAx;V$q8;1!Ow<48lV0-EH-!^oi3CHd9W_+SbTE1aErHLEs=&kLH#$#ye65yE?0}sSAU?& z;HEMzU143Hpss%IGrLeRy>^>_`2$O|f>G)%^j8Ooh>yo{3=YSKUpr9Vx*hwELpvU| ziGCR(Auf*F;F+O>wDhMZ0r|W%7c3It`i*7On{{JulcFa0=d?nmLXER(2HWc$&VG~a zfCEpsZE!3fzg){M+ttWKw8R;vaq+d5ygZ_|YCe|O$yXX*o%!QJ;w4pXLd>e%7C613 zQ~2AX>c1Gha>sb16z~7ycGt&fCwrh;kYJVdY@R+szT{g|mF_k}veXYiqWL>Ke*v7l z+S3M(+dyW?Zka`f^Y^LuaVI4B)@uA+!C8%c<}JbG8kwd}?D|>&+Z5%Uj4YMq%Wk1X z?EbClovaSXAK!nZ#rjG(Q=idapTTpI43s&mUoPs9ErD^sgia6ePVRAkPTR$Q81C|b zorp?;Qv&M$u@n7k77)`HGRq1~F#>vmMLYpQ+=b$JtD$kjmJ~aD_ zbzS=cgXQA9fvE0xAKF*l>NR2_qd>o8)5W+?R_$*Y#c1M~1v|K*B)WBbpaoY^U4gmf=$s+S$t+q0nq$AxXF^8f@dEmh-HMm=EVMLOQ>5 z+;O3g1aqFqU>|d|GBN3%u^*Nir!H8M9P`zlF+9=S=dhW+u5t1TbBsdfQd;=c; z_96ehqQLgSO5euT7?`BSLiew9DoSB|rhg?50WRR;IY=cK=+4VKWA18KBlKGMt<3D0 zM9g%`H(V^rh{<$l2l_AG5OX!V?{E>DFmpCNQ@Cfn`x z*>WDcv`O-aVI@P45b3L8SJ=i*I4~kQYLo~ZTxW0l27aYvZ=o+>Htms=5zKpD#YQ0> zvkW+DWR10m2_@KLAY>a;)aZQ%-;e6GMs*l?@zmMH^qNeh{diU7T2F%9 zBg*6|ElN-L#!*m$Kxq^gMcct)L0m1$7t_g7k0H?jfpv@AZ9gk`iQ4@2>~*DDbO+DF zAk_eTynaf_uz0{ns{)*p{nsKnaP#lq+kPK(41Q+<`ki*9Fl;~^oDbRO(6gbfY*Vn# z#lX4l6gLG8X6%&6MXk+XRmk-d3*k%P<{yn)Zbm@Sq4kfQy6V&Ix?id}lZ2C>C(pNO z%@mqyv9I)##%L#ud7{d3U?M*Gh+(TCa0B8fOwGe=wFkqd<$|Gl`LXjcv1P5L!_xq_ zYOXbB6trVf?1RRQ4YbLf)|ja`G@Xo-WiqGJBm@g@EC}aN4@5snA1F@K<&U;a4!g3y z1O2@$R+Fn+G)af$I)F(M4YiB( z1*mJ^Bpz0erkmS_Pp8z|R0|_-EF`=;%IJ7#dqR{+8eNm8C3KnSm4CBX>-4 zC>tYlwXcx_Gql6zo*6Exb|V8io*7ibE8PY};gn+26Hwchhq51H%*`V|gZ%jUl{&z6 zt)G({(GWsY2q~9P-Z&e+)iKD1BSgrHHXVKGH4)~bww_V!5*>UL$Ivndm1uJvE(8NO&FVWHZ=P+C z&BL)iU_p1$>9mv5tKS_#cL85q%iy!U@_E%s4!XM98%$G76}@h z?$KpYdeHDxcbd5ZyQ3)@P#2Y*#RMte?lBj=hoo9_woxS)huAlbD|4%nZ-5R41R+Cg zufUh_b#fdp#Mz40@=J7A1UmBef9a*JT2+}}7LUc!AnNxrRc&R#qC)9l^(}7o*=D-P zw9HmHORmML)zctFsS>Z75)>Lg-VXWH^9etMt`BuoGMupFt%M*`&;2-gEo)~4y6L;6 zRX~oo*=jZMy0D|&fohqqFI#->$cB1E^`zC+Ry3XCsR)fc?vL8Zq5(y;1#d1g0ly_^>s$q;idVK6s%No%{Y`jV#-&uRwTix>Rr zMa3;*s%s34I}TvPPi^)7w&#AGDpjp7Lu%(_X)B7X_s5MFu`zrNG3(_#n2y^mhMVdO zxNQ#{L%KMK9|Yb5E#8MSK5!|QxPTWzQ9;aSH@GLO0GF(7u%+(zaKf)z_tzKJ4^6r& zomKLSJx>`O;WOLYH2Mn?KZg>+~ z1()|+I*5{c7XsU>3Q5~TIPg_GPgF;ge*i_9AoXlIvZh+cWgKsIPa*!k$?sRA-+>lE zI*s-(JjnBXr})}5VE+oKPu=OXAB*E^QZ%Ye7JR}wWz3E*y6kYKuIzLi$+N@ ztI9rVYIm(B#<|m!%=MFbH^C38%j{q$E}IujMXlXVEnXkwU>Z#i-|w46n1NM! zlC(i}vA;LZqmQ`XAp(aUjf^h9)$&lFAxrr5zb!iY=S*2spegHccR+au%n#=x>QN`iRb_s%78p(-=?KcF*&z}d}`_I`e6Pi)UH?hX#Th4 zF2_Jq7W*EL#B`sVl6j^ttZeqp0I79U6cSA(xelZlSbj8oah_@$XIFY~Y6UAvy2m(womZq#6zL45O!bWEXP%i3OKO{ll2-$#T+Au;qe0uQPB(?w zGWJkisMhDZ)s{t9gwkdg$d<-@{PM`=(HpNe7{v{(*EWe42g=@;ibYbHn#7HD_;ido ztmJI(yCqn%k(1G7XA_hT5~=0TiA|`0=z@RsvJ`WbSeoSmD0pHOMl$By)(Oi&QfU5? zlbwD@DM5fSV>!1~y`qM%G`<0=)=nyEcb)aioYaA~T8Ap|@@w39}bA_>r zeRJ_{5$^2IL@CFGH>#pwzY3K{Z|*;XkCQroz%`AOWT+Gi7nu#>CF>X>MpU6wD_5Z* zlT4g5jr>r-WVbY$>Ln3p-MqswJ9svUOT#y}#)$JT#Vs z$;9uz9>3YeFK)WJa-QLhkL!a;YgkjZUv+ic6V^gpbUU4Q*J|4EEiD2I&L&ClX zF%|m|wBV~nqvhEzq0zNJ5Hd9rty7Qd7mZs;f5=;=pgUgN%*5eO=ynJ} z)Xlcs;mp`5YEjVP7Ip2q$dfFMlfH`!KwXVx*zr~*KF!bG(|x-yYmkJ}7u2XtO8%>q zD52yfG#r#nf@k;ykE$L+QrFD{cVo-=Jd=TxxyZKA{t0aYWEq0+7poQ>$1x1lw(B&S zqPLs9%+9SER*Y3*U%swOf@IKCL4Lrmmx%J%>EGl+Xm5uag|_A+YJ_~zLXB)i3CMUc zm)|mutdeR8$$DPHtHc&VDe^&7{lfp7{wL8BmF&XenCss6KDJ%Vx#l$m(fSDm%{FPq z{-;-?Boh90plWh$$r{VO(6`a+>IXJ_xRCelVT{vIDqTC$e2Oz?Lc$WRYxdHt9Owgf z8r%N<;91gx5)jz$5%s>mj;Ok_doG2XXomQL`S-rru_x&Qdp=3?G?QV)Dlqqe08zxaJ?t3i$Ck7<)Rv)*3yP2>1* zS_qaQhwo{uc;zAdbcjok{<_{Morr~jz1MH7!L``qiMNTnDJZt|A}y?8znGIN5`?~j zZxf8@+-4VNl*7`FHp!ePSIvAkHP~C6GHOA%dQ*_4*2FZu&j%C%Jw{GDsZw@bz%r@EZ)A2s{MRJb#+)XA>|nx zU@LDuBdM_23#|07AOwpL1lp|fr~qU>O1+p;6O2t>@)b_ORF_ncS36T8QE$QD!h0=+ z*^vJPDznbVLSdbB2!PWzXo-T)qp}Q}64YL=+u@p)2^?3CMCHP?y{YFl!pYy+_VQk6 zzCVuak9fg_{Pk0m6EXBCimSs`dZvG(D2g*^l4NgxAH^4yUW-ODy3EvyPm8y=r{|{o z!eeLdS7&Bx_$MdZbGb0XmgDGQuY8zjsNiTmzLsVk^LhDpkt^I9@#XaONS&m_MkzgX z+MtSl5MVa<2n9Bx2aYebE$hgYc+Sa{{pM>@Svv%GO)VN6OK@9&uKTG%d<|xIAACW- z45&K51nMUNo~Pyx4br#p*z45v0m`I}%p_P9STHKE<=t#bKhH+mL~9DQdcbTt$&o+#`>5Ix4|vv2=mc5$t$Ub8XnXy zG%-7%;PskSU)5kN-;Gd$#W!|U`DwR72ZM?qKoaxq(F+`RC8Bb&-Qu6U_N4=jaHuNc zGZT+20F*$wa8n;pNHu5+z;5u(N;~$@yBf%S-$aS&F^^a%$krvCsoP(MhJLM8VQSU* z+7ASt$A#1d8dc08x)AWknT@u~jko0Hdhzw56cCpyIf$H{WPn^y+eFO9esm|)aA#xqXV=Ty1!SunWP~(xjC%NwtvJxFhCJ<=Tf<;vdS07~h?g0-pNQX4 zFm#cY43a{p4~c-Xe$pyE1J%WXzrt}N^-=d%HQB9erN?jQm(|j#@kzlW(Y8Zl=ETuv zVeecY>A620s>$L=9u)j;3OU=+5wA?(X!NVW44;P^HCd)L6bb0es)z7LxdWZqT%!@p z(BIDN>x}}#H|nf1Z)SwyH&th+P^1N#yX;*Ais5u?Pu7h5+B!18(GS--NFhtAr6P-yR4d9&hdOvGvtet<&)T-} z$34dECgs4nk7$Q?Y%!Y8ktkOfhhMzp)1H$|^Owac8FeU!Y91q=dQB^tr+Ns>%u2oL zIb!wZy_1cno8_+Y@DkB5+u9i-Y`9V~@{OG|gLptpJtnLMO{3M6#iVV*Ym@1CQraq* zUQYi+_LMuu{vB@2e(gJ6rNR*ez82@0qE%!HvwN^ z4=BOxC64J!(q>fFCdfl%l{QrEAQ2#f6icEXkzno>Rm9ON1Ic&6aU-`<3wIepq`L8u zg+nVpCP$Kda}OqgRaX=`FSRD5Z5zJdx~Qbgh~1s1$%X(*C79#4{lO8B;A26qY&aVH zj1yIB0NHY&vg#Vi>Ta5G*{pJT$7o@IL0;GyOa^#Fy`rR(PO=xrw05j~?(%)vj)Lnj z0i=RM+t(c|Of5$J*DCIypX<~q5N{NDEs8#JSDbOAo0UQ1aA))35u>ZQQSRX$DX$FY zO${t)F-i2x&XB#NP%zP|fc$~YcONn?f2g3eB==4d<1Ee&J&Ayvr8eNWOu>nc+KJ@@v}PNa;7rf+ z^0dt83>M|LzNeP}t=Svx+ZFEh5u9_#SNo7iJrh@GMu^IjCZ_xlAU8m3*7+M!{FYB# zO4zY5byPMum$-997`iNe{m%WCc+pQP5X$r5yqD^9H5iMrSA}@% z%~{cQBsRNy=wZQeekVH9kA1jJX_*<2q|p=S-v(=obm!7BS! ztcnu0(Yg3aY{>|VG34~G1dHx!ogrFmfzs^IU(zi1zoglWk3eY_?DXf#S<1#OW=K?O zw*pnJ@bdCEfy^yE<_b+`v#N0Jk97k9$!AV3(mbR+h5jiY#uH8Gljd{A{RmBncvm)0 zN`m{9>Sf{(+TT{lY2$z41~W0os&_}X1W{( z!9I8Jg&p>;laX0;woJwOuoZ7Tc-(-|GzbqAuy^`O|>|Kv` zPFkdf+UNc{p|TP^C`jTt&A^IU9boi*5bHvh+se36WL7YDNlB729@mG*!{}a%W>a5w~$E!!|h8Ip}6)f~k%jk2MAL@-Z@C zl7_qd4eMnRll2T`eNK2}zy2NZFLArIi_KPwrKK|D_5yDt!}wKVuv7=^@|Hb%m9ZCO zbCxDtx7t z1a-}%J~^bb8r@E?H+(7dorZkT5PIWT7d;IN^oD#1I>r!ma|&7dCpjjaQ+zdP=#F>+ zU0qHu;lbnHIFxvgY@>ciZnkNwmw27B2Awa0m&1 zGv*w}Imtd2b)!6#EK1~>%z0_7wSsw)B|e-3Hbm?pr{`+yh@z}pj}y+;el~*jvlgLR zg`V5D1vj|`JDD%J-^!(9O#w4$1Z!>tYlBjtxZee~?W3LF`WC!!royFo6PX<5ssdx3 zeKDddiZ2tlFBi8^rr~983z=Narh=k|=mDSr6M(#BbvVr{nj#&j=DnSOX5)Rd3{k)# zdH(fkgRyus&D(_Nb*fhgplGrjxIqPv0hsk-4I}YfGo($+KwApv{PK;>Cjlwa2C#w_ zuJ=8SQsK}L{B#+T2J~|b3tJ^x2#5iMKTw*L*C1{@mo!Xe*f?x(%2Wg`kvXvkJvkS* z1WqH#13T;-Ba|b7(yUGEW>Rz)KG-{E@sH&~_3?2hrt8Z6H)|>w+M+5D{>kTvk|LY- zO(aNwKG~ZV0`24GLZ|IcLxQvtWHJ~&iXHtU;-PJqP|#t@h5@zaGS}4vy!sGuQ!t2 zcpDukwf~tGq6BP(BJKoI>*IAjC>L5~O5$RwIX4H$MPB-5RkZ7G_Bt2W2i_thlYT?{rJU# z1EFy|?FAh!!D%5Q#A0TzKYo(?;-{Z=TJR%w;7&Wj%pa6ft-lKKh1}W|{u;Y7a{DM( z#C4>EqaU1Moz(kYK6qcon{K^V)?SMf%w7Nc6hqXJE5R@6{Zp~_Mp|0MFJuXGLbTu3 zEMeAY=#R&%=iXf_>UtJO_@_fB)OJ}kmZ@x&`iyA4cRV>@p$O6(4cb6(V~Tq#F@|}Q zS@t4$1WciIdOGe*M?l;!AR#lb;p5z2(kvxL^v$P0IMH*rKv&7eu2+`V2nDM(=*}Y6 zeaMaDfY0D<@W(UC0hS&*>&maZ=QVBm3po8osq&draG|>^8MAJ8SpZAo3z1*)l{)f6 z^!EdAO+kkcF_D{XP@I<>Yp^dhTyJo_2-o zO&Q(1KeYtGZ`-2oz_d3zg_Y=Z)|QL75QqpUjF{>cL*Lbmc=v;7^Jl}y$eeZO)#aO0 zXTRvcj&3s%p`)$6oZb5oDdW7;4jaA3Y-Rk^1GXM@_Ro2tZIg7uc(FT1hHBWl@~I07 zot2s6Qk^8G7vWiKl#}Ijl`dTbWrN{rA@1l-QB0$|u@jJl_K{-T(s1bl0wnq2 zlT_#^OV!olL*EqeRjgkZ0^hzmnE>@8J(lq%HqN$7L^o&d2^0GpEG!M+-8+T(6vd~n zSd~X~OXW_Y)-y!<%as_tA9mgKrG*w+4EBa|C8vWg(sC&o6yvpi!R#h0ay2jckr^t3 z|C}bpy2w&22nnSzXHM?|w76KZ*}Y6OCd+Zyd%kTPIq=?Z_7y67<*^ki%Fb7Jro4~2 zWUs}O)CpPDF+P1rx=vZ@hZ+{7^GW?kMBPlOeROb(CV4N)>zReJ zRevz{xr`tqn_xUt>cJew`MJax`E#$OE}EnvH+?mk?`BTk*pExGnm%My(JZfez_~Wv z1!YcQDPMzZ>xhUNA7USAbc;{8A&QB5-I{!5xY zCtM>0O0&0=qqp6yp^_-mjR3Zo4OYsskEAD>dJNt4YDwLzSQ=`gOADN$M~Tkq8z{mD zp!L;A{iJC8-!eX2syh-d$BP_mp?tk;xCsU?gCOlJq~*_F_JoZAK=C?x`-Q4mpCBkV zx7tZB2ZP@V$xFt4>xS%)z+Kv@%_KjJ&5ez;xa3{iM)9E}E%oIAJ5{5Ls!@_GNFuOq zu}$MijfT!1Y2y$Yd%(U;UzcMNx_(QZjB(29{75JHy>nwT zl{YN{+_8}W9{;W#^mlLqFz>&;*`Ey71sm(~va*!##NJX+E0@X3!dk&2DvyG~k;b&g z#t?f$1j7gQ+aVy>BS;Da!50Q06b8Yc!Ba~~4Fpvnz<-B_k8-RvajJc^ZS>pMjhH$7 zVgNj`5AZ++9)Fpo|NkeI`Yzdj7KDJ%A3X5qq-od@w&H;j@_{Rn4DylXBT`iFD5S&& zsJ_yOQNQEsKDHS@u>l2M6DSxO2paJG&sXCD{s9COgy8oN;MYIb=6cF`T1o47>pz^J zN1O*A^lAI2Wt|?{^8iB~{=SZ< zpvRQwO6WXL(jfn){BuE_$F%2)%skNgfhAV{+|)mnoO#T8t~kpBYw5+`tY=EKJjOg% zJL3WK1@-~+OeKxSpyw(vJb=7`AsBz|?P*8fj}+Q}k^Ua* zADaFd1)r+?zZ?D;y?UNDeManmN_%R>KhXMtSM%2+{`*4y!;yc?dCt;);P@~+PE8K3f=_pP52pK7xY#AxRKCO&xEk2%kIo)4V2tpCmVcaYm(;cbsu&pC|` ztRS|htVf39W6X0x-~%Rx{VC>u>Vc2J&pCJxU{{W(;Af4z$H3?Or3YX*_x}a>Cyx0s z@Hz400hq`46!@$L@)-D>pYQ-w5O@lF&Q^HLdoEP_ffpzIzj^oJr|<+0OJDAJO88i|3kp$W8!mBi4Vji$*06eF^Z2d&&4f0U|M9KVxEa&dWw5G tnSQ{zm^{Hfn&m#GJU1(TpaA}+{6}j@;FJdhgar8A2%KpIng0Im{{V#2{u}@R literal 0 HcmV?d00001 diff --git a/evidence/lab4/00_compose_config.txt b/evidence/lab4/00_compose_config.txt new file mode 100644 index 00000000..6363e4c5 --- /dev/null +++ b/evidence/lab4/00_compose_config.txt @@ -0,0 +1,3 @@ +$ docker compose --env-file .env config --quiet + +exit_code=0; elapsed_seconds=0.328 diff --git a/evidence/lab4/00_docker.txt b/evidence/lab4/00_docker.txt new file mode 100644 index 00000000..3da53c80 --- /dev/null +++ b/evidence/lab4/00_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/lab4/00_environment.json b/evidence/lab4/00_environment.json new file mode 100644 index 00000000..dd810a76 --- /dev/null +++ b/evidence/lab4/00_environment.json @@ -0,0 +1,9 @@ +{ + "at_utc": "2026-11-05T21:25:22.372954+00:00", + "platform": "Windows-10-10.0.19045-SP0", + "python": "3.11.9", + "host_cpu_count": 16, + "cluster_checks": "included", + "synthetic_data_only": true, + "host_dependencies": "Python standard library + Docker Compose" +} diff --git a/evidence/lab4/01_cluster.txt b/evidence/lab4/01_cluster.txt new file mode 100644 index 00000000..cb246b03 --- /dev/null +++ b/evidence/lab4/01_cluster.txt @@ -0,0 +1,30 @@ +$ C:\Users\ira\OneDrive\Desktop\work\vp\mtusi2026lab4-fork\.venv\Scripts\python.exe scripts/cluster_evidence.py --base-url http://127.0.0.1:8080 --output C:\Users\ira\OneDrive\Desktop\work\vp\mtusi2026lab4-fork\evidence\lab4\cluster +PASS: evidence saved to evidence\lab4\cluster + Container mtusi-messenger-lab4-api-a-1 Created + Container mtusi-messenger-lab4-api-a-1 Starting + Container mtusi-messenger-lab4-api-a-1 Started + Container mtusi-messenger-lab4-api-b-1 Created + Container mtusi-messenger-lab4-api-b-1 Starting + Container mtusi-messenger-lab4-api-b-1 Started + Container mtusi-messenger-lab4-api-a-1 Running + Container mtusi-messenger-lab4-api-b-1 Created + Container mtusi-messenger-lab4-api-b-1 Starting + Container mtusi-messenger-lab4-api-b-1 Started + Container mtusi-messenger-lab4-api-a-1 Recreate + Container mtusi-messenger-lab4-api-a-1 Recreated + Container mtusi-messenger-lab4-api-a-1 Starting + Container mtusi-messenger-lab4-api-a-1 Started + Container mtusi-messenger-lab4-api-b-1 Recreate + Container mtusi-messenger-lab4-api-b-1 Recreated + Container mtusi-messenger-lab4-api-b-1 Starting + Container mtusi-messenger-lab4-api-b-1 Started + Container mtusi-messenger-lab4-api-a-1 Recreate + Container mtusi-messenger-lab4-api-a-1 Recreated + Container mtusi-messenger-lab4-api-a-1 Starting + Container mtusi-messenger-lab4-api-a-1 Started + Container mtusi-messenger-lab4-api-b-1 Recreate + Container mtusi-messenger-lab4-api-b-1 Recreated + Container mtusi-messenger-lab4-api-b-1 Starting + Container mtusi-messenger-lab4-api-b-1 Started + +exit_code=0; elapsed_seconds=364.344 diff --git a/evidence/lab4/01_public_smoke.txt b/evidence/lab4/01_public_smoke.txt new file mode 100644 index 00000000..3ae2fe12 --- /dev/null +++ b/evidence/lab4/01_public_smoke.txt @@ -0,0 +1,4 @@ +$ C:\Users\ira\OneDrive\Desktop\work\vp\mtusi2026lab4-fork\.venv\Scripts\python.exe tests/smoke.py --base-url http://127.0.0.1:8080 +PASS: lab4 public API scenario; infrastructure and grading evidence remain separate. + +exit_code=0; elapsed_seconds=1.797 diff --git a/evidence/lab4/02_attachment_contract.json b/evidence/lab4/02_attachment_contract.json new file mode 100644 index 00000000..570deb60 --- /dev/null +++ b/evidence/lab4/02_attachment_contract.json @@ -0,0 +1,10 @@ +{ + "attachment_id": "5d078480-4f83-447e-b36c-c7d566e03da3", + "upload_instance": "4fa6b582e5b94397b9dc92a8653573cd", + "sha256": "99ea7533c441f79cd630d15ce4d58d1ec4e06f6f0efb3b77a5c088305cd1bd75", + "size_bytes": 111817, + "download_equal": true, + "attachment_only_message_visible": true, + "outsider_metadata_status": 404, + "outsider_content_status": 404 +} diff --git a/evidence/lab4/03_uploader_failover.json b/evidence/lab4/03_uploader_failover.json new file mode 100644 index 00000000..582a0920 --- /dev/null +++ b/evidence/lab4/03_uploader_failover.json @@ -0,0 +1,10 @@ +{ + "upload_instance": "4fa6b582e5b94397b9dc92a8653573cd", + "stopped_service": "api-b", + "stopped_state": "exited", + "stopped_pid": 0, + "download_instance": "46dfed50fce84f5283007f574410c1f8", + "sha256": "99ea7533c441f79cd630d15ce4d58d1ec4e06f6f0efb3b77a5c088305cd1bd75", + "download_equal": true, + "seconds_from_stop_start": 6.969 +} diff --git a/evidence/lab4/04_full_restart.json b/evidence/lab4/04_full_restart.json new file mode 100644 index 00000000..8e930a74 --- /dev/null +++ b/evidence/lab4/04_full_restart.json @@ -0,0 +1,48 @@ +{ + "before": [ + { + "service": "api-a", + "container_id": "acb7370e1017564b1d069792638ee393604b2005c2eb236ade1201a2df01073d", + "pid": 96679, + "state": "running", + "started_at": "2026-11-05T21:31:23.369770863Z", + "status": 200, + "instance_id": "46dfed50fce84f5283007f574410c1f8" + }, + { + "service": "api-b", + "container_id": "d0a10d56b0a4c20a44c899e177fb1b8ef209943b6f263aa35afa669ebe72c4d2", + "pid": 98295, + "state": "running", + "started_at": "2026-11-05T21:31:49.061422419Z", + "status": 200, + "instance_id": "f77b055741f14a63a061698f90d57e46" + } + ], + "after": [ + { + "service": "api-a", + "container_id": "72fd226c23b838293f4d7f0347a4541a13d0965cf37cc657e8f79fc8aedf0ae5", + "pid": 395, + "state": "running", + "started_at": "2026-11-05T21:32:21.171649446Z", + "status": 200, + "instance_id": "a7cafe615bac4e4fa5f098568c25444a" + }, + { + "service": "api-b", + "container_id": "a9f95a6b71d1029cf15151be7638e7c65aa7e4a3d2ca9bd9bad25c5fc3a1c8dc", + "pid": 394, + "state": "running", + "started_at": "2026-11-05T21:32:21.167275356Z", + "status": 200, + "instance_id": "d5ef73b927474fdb980ddc3283a8cd43" + } + ], + "existing_session_works": true, + "message_preserved": true, + "metadata_equal": true, + "download_equal": true, + "sha256": "99ea7533c441f79cd630d15ce4d58d1ec4e06f6f0efb3b77a5c088305cd1bd75", + "restart_scope": "all default Compose services, force recreated; named volumes retained" +} diff --git a/evidence/lab4/04_restart_command.txt b/evidence/lab4/04_restart_command.txt new file mode 100644 index 00000000..c22c6102 --- /dev/null +++ b/evidence/lab4/04_restart_command.txt @@ -0,0 +1,73 @@ +$ C:\Users\ira\OneDrive\Desktop\work\vp\mtusi2026lab4-fork\.venv\Scripts\python.exe scripts/restart.py + Container mtusi-messenger-lab4-nginx-1 Stopping + Container mtusi-messenger-lab4-redis-1 Stopping + Container mtusi-messenger-lab4-cleanup-1 Stopping + Container mtusi-messenger-lab4-redis-1 Stopped + Container mtusi-messenger-lab4-nginx-1 Stopped + Container mtusi-messenger-lab4-api-b-1 Stopping + Container mtusi-messenger-lab4-api-a-1 Stopping + Container mtusi-messenger-lab4-api-b-1 Stopped + Container mtusi-messenger-lab4-api-a-1 Stopped + Container mtusi-messenger-lab4-cleanup-1 Stopped + Container mtusi-messenger-lab4-minio-init-1 Stopping + Container mtusi-messenger-lab4-migrate-1 Stopping + Container mtusi-messenger-lab4-minio-init-1 Stopped + Container mtusi-messenger-lab4-minio-1 Stopping + Container mtusi-messenger-lab4-migrate-1 Stopped + Container mtusi-messenger-lab4-db-1 Stopping + Container mtusi-messenger-lab4-minio-1 Stopped + Container mtusi-messenger-lab4-db-1 Stopped + Container mtusi-messenger-lab4-redis-1 Recreate + Container mtusi-messenger-lab4-minio-1 Recreate + Container mtusi-messenger-lab4-db-1 Recreate + Container mtusi-messenger-lab4-db-1 Recreated + Container mtusi-messenger-lab4-migrate-1 Recreate + Container mtusi-messenger-lab4-minio-1 Recreated + Container mtusi-messenger-lab4-minio-init-1 Recreate + Container mtusi-messenger-lab4-redis-1 Recreated + Container mtusi-messenger-lab4-minio-init-1 Recreated + Container mtusi-messenger-lab4-migrate-1 Recreated + Container mtusi-messenger-lab4-api-a-1 Recreate + Container mtusi-messenger-lab4-api-b-1 Recreate + Container mtusi-messenger-lab4-cleanup-1 Recreate + Container mtusi-messenger-lab4-api-b-1 Recreated + Container mtusi-messenger-lab4-cleanup-1 Recreated + Container mtusi-messenger-lab4-api-a-1 Recreated + Container mtusi-messenger-lab4-nginx-1 Recreate + Container mtusi-messenger-lab4-nginx-1 Recreated + Container mtusi-messenger-lab4-db-1 Starting + Container mtusi-messenger-lab4-minio-1 Starting + Container mtusi-messenger-lab4-redis-1 Starting + Container mtusi-messenger-lab4-minio-1 Started + Container mtusi-messenger-lab4-minio-1 Waiting + Container mtusi-messenger-lab4-db-1 Started + Container mtusi-messenger-lab4-db-1 Waiting + Container mtusi-messenger-lab4-redis-1 Started + Container mtusi-messenger-lab4-minio-1 Healthy + Container mtusi-messenger-lab4-minio-init-1 Starting + Container mtusi-messenger-lab4-db-1 Healthy + Container mtusi-messenger-lab4-migrate-1 Starting + Container mtusi-messenger-lab4-minio-init-1 Started + Container mtusi-messenger-lab4-migrate-1 Started + Container mtusi-messenger-lab4-migrate-1 Waiting + Container mtusi-messenger-lab4-minio-init-1 Waiting + Container mtusi-messenger-lab4-minio-init-1 Waiting + Container mtusi-messenger-lab4-migrate-1 Waiting + Container mtusi-messenger-lab4-minio-init-1 Waiting + Container mtusi-messenger-lab4-migrate-1 Waiting + Container mtusi-messenger-lab4-minio-init-1 Exited + Container mtusi-messenger-lab4-minio-init-1 Exited + Container mtusi-messenger-lab4-minio-init-1 Exited + Container mtusi-messenger-lab4-migrate-1 Exited + Container mtusi-messenger-lab4-api-a-1 Starting + Container mtusi-messenger-lab4-migrate-1 Exited + Container mtusi-messenger-lab4-cleanup-1 Starting + Container mtusi-messenger-lab4-migrate-1 Exited + Container mtusi-messenger-lab4-api-b-1 Starting + Container mtusi-messenger-lab4-cleanup-1 Started + Container mtusi-messenger-lab4-api-b-1 Started + Container mtusi-messenger-lab4-api-a-1 Started + Container mtusi-messenger-lab4-nginx-1 Starting + Container mtusi-messenger-lab4-nginx-1 Started + +exit_code=0; elapsed_seconds=29.437 diff --git a/evidence/lab4/05_s3_outage.json b/evidence/lab4/05_s3_outage.json new file mode 100644 index 00000000..16d874a5 --- /dev/null +++ b/evidence/lab4/05_s3_outage.json @@ -0,0 +1,67 @@ +{ + "direct_api_probes": { + "api-a": { + "live": { + "status": 200, + "instance_id": "a7cafe615bac4e4fa5f098568c25444a", + "request_id_present": true + }, + "ready": { + "status": 503, + "instance_id": "a7cafe615bac4e4fa5f098568c25444a", + "request_id_present": true, + "error_code": "unavailable", + "request_id_matches": true + }, + "upload": { + "status": 503, + "instance_id": "a7cafe615bac4e4fa5f098568c25444a", + "request_id_present": true, + "error_code": "unavailable", + "request_id_matches": true + }, + "content": { + "status": 503, + "instance_id": "a7cafe615bac4e4fa5f098568c25444a", + "request_id_present": true, + "error_code": "unavailable", + "request_id_matches": true + } + }, + "api-b": { + "live": { + "status": 200, + "instance_id": "d5ef73b927474fdb980ddc3283a8cd43", + "request_id_present": true + }, + "ready": { + "status": 503, + "instance_id": "d5ef73b927474fdb980ddc3283a8cd43", + "request_id_present": true, + "error_code": "unavailable", + "request_id_matches": true + }, + "upload": { + "status": 503, + "instance_id": "d5ef73b927474fdb980ddc3283a8cd43", + "request_id_present": true, + "error_code": "unavailable", + "request_id_matches": true + }, + "content": { + "status": 503, + "instance_id": "d5ef73b927474fdb980ddc3283a8cd43", + "request_id_present": true, + "error_code": "unavailable", + "request_id_matches": true + } + } + }, + "public_readiness": { + "status": 503, + "error_code": "unavailable", + "request_id_matches": true + }, + "restored_download_equal": true, + "method": "probe each API directly inside its container because LB excludes unready backends" +} diff --git a/evidence/lab4/06_bootstrap_repeat_1.txt b/evidence/lab4/06_bootstrap_repeat_1.txt new file mode 100644 index 00000000..ce0f17c7 --- /dev/null +++ b/evidence/lab4/06_bootstrap_repeat_1.txt @@ -0,0 +1,4 @@ +$ docker compose --env-file .env run --rm --no-deps minio-init +PASS: private bucket and separate restricted API account configured + +exit_code=0; elapsed_seconds=4.219 diff --git a/evidence/lab4/06_bootstrap_repeat_2.txt b/evidence/lab4/06_bootstrap_repeat_2.txt new file mode 100644 index 00000000..c97c5603 --- /dev/null +++ b/evidence/lab4/06_bootstrap_repeat_2.txt @@ -0,0 +1,4 @@ +$ docker compose --env-file .env run --rm --no-deps minio-init +PASS: private bucket and separate restricted API account configured + +exit_code=0; elapsed_seconds=4.406 diff --git a/evidence/lab4/06_private_bucket.json b/evidence/lab4/06_private_bucket.json new file mode 100644 index 00000000..7d8e357a --- /dev/null +++ b/evidence/lab4/06_private_bucket.json @@ -0,0 +1,15 @@ +{ + "anonymous_get_status": 403, + "restricted_operations": { + "list_foreign_bucket": "AccessDenied", + "make_bucket": "AccessDenied", + "put_outside_prefix": "AccessDenied" + }, + "visible_buckets": [ + "attachments" + ], + "visible_buckets_limited": true, + "root_credentials_absent_in_api": true, + "server_assigned_original_prefix": true, + "bootstrap_repeated_successfully": 2 +} diff --git a/evidence/lab4/07_concurrent_uploads.json b/evidence/lab4/07_concurrent_uploads.json new file mode 100644 index 00000000..3d765e92 --- /dev/null +++ b/evidence/lab4/07_concurrent_uploads.json @@ -0,0 +1,206 @@ +{ + "concurrency": 10, + "barrier_synchronized": true, + "requests": 10, + "successes": 10, + "failures": 0, + "fixture_bytes": 2099087, + "fixture_dimensions": [ + 1024, + 683 + ], + "fixture_sha256": "f9c08c2477d17120713793c91f419cbea0de705de10c1123fdabc63d3733f00f", + "elapsed_seconds": 0.719, + "successful_uploads_per_second": 13.908, + "successful_mib_per_second": 27.842, + "instances": [ + "a7cafe615bac4e4fa5f098568c25444a", + "d5ef73b927474fdb980ddc3283a8cd43" + ], + "memory": [ + { + "service": "api-a", + "sampling_interval_seconds": 0.2, + "baseline_cgroup_bytes": 93847552, + "sampled_cgroup_peak_bytes": 94785536, + "sampled_growth_bytes": 937984, + "sampled_api_pid1_rss_peak_bytes": 101380096, + "container_lifetime_peak_bytes": 122769408, + "limit_bytes": 536870912, + "samples": [ + { + "seconds": 0.0, + "cgroup_version": 1, + "memory_current_bytes": 93847552, + "container_lifetime_peak_bytes": 122769408, + "limit_bytes": 536870912, + "api_pid1_rss_bytes": 91144192 + }, + { + "seconds": 0.201, + "cgroup_version": 1, + "memory_current_bytes": 94670848, + "container_lifetime_peak_bytes": 122769408, + "limit_bytes": 536870912, + "api_pid1_rss_bytes": 91144192 + }, + { + "seconds": 0.402, + "cgroup_version": 1, + "memory_current_bytes": 81805312, + "container_lifetime_peak_bytes": 122769408, + "limit_bytes": 536870912, + "api_pid1_rss_bytes": 91144192 + }, + { + "seconds": 0.603, + "cgroup_version": 1, + "memory_current_bytes": 81674240, + "container_lifetime_peak_bytes": 122769408, + "limit_bytes": 536870912, + "api_pid1_rss_bytes": 91144192 + }, + { + "seconds": 0.804, + "cgroup_version": 1, + "memory_current_bytes": 81674240, + "container_lifetime_peak_bytes": 122769408, + "limit_bytes": 536870912, + "api_pid1_rss_bytes": 91144192 + }, + { + "seconds": 1.005, + "cgroup_version": 1, + "memory_current_bytes": 81805312, + "container_lifetime_peak_bytes": 122769408, + "limit_bytes": 536870912, + "api_pid1_rss_bytes": 91144192 + }, + { + "seconds": 1.206, + "cgroup_version": 1, + "memory_current_bytes": 83456000, + "container_lifetime_peak_bytes": 122769408, + "limit_bytes": 536870912, + "api_pid1_rss_bytes": 92024832 + }, + { + "seconds": 1.408, + "cgroup_version": 1, + "memory_current_bytes": 94494720, + "container_lifetime_peak_bytes": 122769408, + "limit_bytes": 536870912, + "api_pid1_rss_bytes": 99217408 + }, + { + "seconds": 1.609, + "cgroup_version": 1, + "memory_current_bytes": 94785536, + "container_lifetime_peak_bytes": 122769408, + "limit_bytes": 536870912, + "api_pid1_rss_bytes": 99217408 + }, + { + "seconds": 1.8, + "cgroup_version": 1, + "memory_current_bytes": 92299264, + "container_lifetime_peak_bytes": 122769408, + "limit_bytes": 536870912, + "api_pid1_rss_bytes": 101380096 + } + ], + "limitations": "cgroup includes file cache and sampler overhead; 200 ms samples may miss short spikes; lifetime peak may predate this experiment" + }, + { + "service": "api-b", + "sampling_interval_seconds": 0.2, + "baseline_cgroup_bytes": 81969152, + "sampled_cgroup_peak_bytes": 96743424, + "sampled_growth_bytes": 14774272, + "sampled_api_pid1_rss_peak_bytes": 101416960, + "container_lifetime_peak_bytes": 99397632, + "limit_bytes": 536870912, + "samples": [ + { + "seconds": 0.0, + "cgroup_version": 1, + "memory_current_bytes": 81969152, + "container_lifetime_peak_bytes": 99397632, + "limit_bytes": 536870912, + "api_pid1_rss_bytes": 91119616 + }, + { + "seconds": 0.201, + "cgroup_version": 1, + "memory_current_bytes": 81969152, + "container_lifetime_peak_bytes": 99397632, + "limit_bytes": 536870912, + "api_pid1_rss_bytes": 91119616 + }, + { + "seconds": 0.402, + "cgroup_version": 1, + "memory_current_bytes": 81862656, + "container_lifetime_peak_bytes": 99397632, + "limit_bytes": 536870912, + "api_pid1_rss_bytes": 91119616 + }, + { + "seconds": 0.603, + "cgroup_version": 1, + "memory_current_bytes": 83652608, + "container_lifetime_peak_bytes": 99397632, + "limit_bytes": 536870912, + "api_pid1_rss_bytes": 92098560 + }, + { + "seconds": 0.804, + "cgroup_version": 1, + "memory_current_bytes": 94892032, + "container_lifetime_peak_bytes": 99397632, + "limit_bytes": 536870912, + "api_pid1_rss_bytes": 99246080 + }, + { + "seconds": 1.005, + "cgroup_version": 1, + "memory_current_bytes": 96743424, + "container_lifetime_peak_bytes": 99397632, + "limit_bytes": 536870912, + "api_pid1_rss_bytes": 101416960 + }, + { + "seconds": 1.206, + "cgroup_version": 1, + "memory_current_bytes": 92405760, + "container_lifetime_peak_bytes": 99397632, + "limit_bytes": 536870912, + "api_pid1_rss_bytes": 101416960 + }, + { + "seconds": 1.339, + "cgroup_version": 1, + "memory_current_bytes": 92397568, + "container_lifetime_peak_bytes": 99397632, + "limit_bytes": 536870912, + "api_pid1_rss_bytes": 101416960 + } + ], + "limitations": "cgroup includes file cache and sampler overhead; 200 ms samples may miss short spikes; lifetime peak may predate this experiment" + } + ], + "latency_seconds": [ + 0.562, + 0.672, + 0.516, + 0.625, + 0.391, + 0.391, + 0.687, + 0.562, + 0.391, + 0.719 + ], + "errors": [], + "all_downloads_equal": true +} diff --git a/evidence/lab4/08_cleanup_dry_run.txt b/evidence/lab4/08_cleanup_dry_run.txt new file mode 100644 index 00000000..b90da80c --- /dev/null +++ b/evidence/lab4/08_cleanup_dry_run.txt @@ -0,0 +1,4 @@ +$ docker compose --env-file .env exec -T api-a python -m app.cleanup --dry-run +{"candidates": 0, "deleted": 0, "failed": 0, "dry_run": true} + +exit_code=0; elapsed_seconds=5.844 diff --git a/evidence/lab4/09_student_tests.txt b/evidence/lab4/09_student_tests.txt new file mode 100644 index 00000000..1d18f055 --- /dev/null +++ b/evidence/lab4/09_student_tests.txt @@ -0,0 +1,455 @@ +$ docker compose --env-file .env --profile test run --rm --build tests +#0 building with "desktop-linux" instance using docker driver + +#1 [minio internal] load build definition from Dockerfile +#1 transferring dockerfile: 1.79kB done +#1 DONE 0.0s + +#2 [minio internal] load metadata for docker.io/library/python:3.12.8-slim-bookworm +#2 ... + +#3 [minio internal] load metadata for docker.io/library/golang:1.24.8-bookworm +#3 DONE 0.9s + +#2 [minio internal] load metadata for docker.io/library/python:3.12.8-slim-bookworm +#2 DONE 0.9s + +#4 [minio internal] load .dockerignore +#4 transferring context: 196B done +#4 DONE 0.0s + +#5 [minio server 1/4] FROM docker.io/library/python:3.12.8-slim-bookworm@sha256:2199a62885a12290dc9c5be3ca0681d367576ab7bf037da120e564723292a2f0 +#5 DONE 0.0s + +#6 [minio go-build 1/1] FROM docker.io/library/golang:1.24.8-bookworm@sha256:4ed690d6649d63c312b99a6120025ec79ce3b542968a37da53d6236c7c61a848 +#6 DONE 0.0s + +#7 [minio internal] load build context +#7 transferring context: 420B 0.0s done +#7 DONE 0.0s + +#8 [minio server 2/4] COPY --from=minio-build /out/minio /usr/local/bin/minio +#8 CACHED + +#9 [minio server 3/4] COPY infra/minio/healthcheck.py /opt/minio/healthcheck.py +#9 CACHED + +#10 [minio minio-build 1/1] RUN go install -p 2 -trimpath -ldflags="-s -w -X github.com/minio/minio/cmd.Version=2025-10-15T17:29:55Z -X github.com/minio/minio/cmd.ReleaseTag=RELEASE.2025-10-15T17-29-55Z" github.com/minio/minio@RELEASE.2025-10-15T17-29-55Z +#10 CACHED + +#11 [minio server 4/4] RUN mkdir /data && chown 10001:10001 /data +#11 CACHED + +#12 [minio] exporting to image +#12 exporting layers done +#12 writing image sha256:1a1494b9ad336bea42711376d400cb56f503ec86ad5a0c0442a3e86dea560afd done +#12 naming to docker.io/library/mtusi-minio:RELEASE.2025-10-15T17-29-55Z done +#12 DONE 0.0s + +#13 [minio-init internal] load build definition from Dockerfile +#13 transferring dockerfile: 1.79kB 0.0s done +#13 DONE 0.0s + +#2 [minio-init internal] load metadata for docker.io/library/python:3.12.8-slim-bookworm +#2 DONE 1.2s + +#3 [minio-init internal] load metadata for docker.io/library/golang:1.24.8-bookworm +#3 DONE 1.2s + +#14 [minio-init internal] load .dockerignore +#14 transferring context: 196B done +#14 DONE 0.0s + +#5 [minio-init bootstrap 1/3] FROM docker.io/library/python:3.12.8-slim-bookworm@sha256:2199a62885a12290dc9c5be3ca0681d367576ab7bf037da120e564723292a2f0 +#5 DONE 0.0s + +#6 [minio-init go-build 1/1] FROM docker.io/library/golang:1.24.8-bookworm@sha256:4ed690d6649d63c312b99a6120025ec79ce3b542968a37da53d6236c7c61a848 +#6 DONE 0.0s + +#15 [minio-init internal] load build context +#15 transferring context: 3.45kB done +#15 DONE 0.0s + +#16 [minio-init bootstrap 2/3] COPY --from=mc-build /out/mc /usr/local/bin/mc +#16 CACHED + +#17 [minio-init mc-build 1/1] RUN go install -p 2 -trimpath -ldflags="-s -w -X github.com/minio/mc/cmd.Version=2025-08-13T08:35:41Z -X github.com/minio/mc/cmd.ReleaseTag=RELEASE.2025-08-13T08-35-41Z" github.com/minio/mc@RELEASE.2025-08-13T08-35-41Z +#17 CACHED + +#18 [minio-init bootstrap 3/3] COPY infra/minio/bootstrap.py /opt/minio/bootstrap.py +#18 CACHED + +#19 [minio-init] exporting to image +#19 exporting layers done +#19 writing image sha256:bf240f90f68d7541e416ff322fc356abd7796a95e6cafe1673df1cbf363698a5 done +#19 naming to docker.io/library/mtusi-minio-mc:RELEASE.2025-08-13T08-35-41Z done +#19 DONE 0.0s +#0 building with "desktop-linux" instance using docker driver + +#1 [tests internal] load build definition from Dockerfile +#1 transferring dockerfile: 1.04kB done +#1 DONE 0.0s + +#2 [tests internal] load metadata for docker.io/library/python:3.12.8-slim-bookworm +#2 DONE 0.4s + +#3 [tests internal] load .dockerignore +#3 transferring context: 196B done +#3 DONE 0.0s + +#4 [tests base 1/6] FROM docker.io/library/python:3.12.8-slim-bookworm@sha256:2199a62885a12290dc9c5be3ca0681d367576ab7bf037da120e564723292a2f0 +#4 DONE 0.0s + +#5 [tests internal] load build context +#5 transferring context: 645.96kB 0.0s done +#5 DONE 0.1s + +#6 [tests test 4/9] COPY student-tests ./student-tests +#6 CACHED + +#7 [tests test 1/9] COPY requirements-test.txt ./ +#7 CACHED + +#8 [tests base 5/6] RUN pip install -r requirements.txt +#8 CACHED + +#9 [tests test 3/9] COPY pytest.ini ./ +#9 CACHED + +#10 [tests test 5/9] COPY unit-tests ./unit-tests +#10 CACHED + +#11 [tests test 7/9] COPY tests ./tests +#11 CACHED + +#12 [tests base 3/6] WORKDIR /srv +#12 CACHED + +#13 [tests test 2/9] RUN pip install -r requirements-test.txt +#13 CACHED + +#14 [tests test 6/9] COPY scripts ./scripts +#14 CACHED + +#15 [tests base 2/6] RUN groupadd --system --gid 10001 app && useradd --system --uid 10001 --gid app --no-create-home --shell /usr/sbin/nologin app +#15 CACHED + +#16 [tests test 8/9] COPY lab.json ./ +#16 CACHED + +#17 [tests base 4/6] COPY requirements.txt ./ +#17 CACHED + +#18 [tests base 6/6] COPY app ./app +#18 CACHED + +#19 [tests test 9/9] COPY contracts ./contracts +#19 CACHED + +#20 [tests] exporting to image +#20 exporting layers done +#20 writing image sha256:b0bdbf812b02e11b2c808878a3602d455fe28ff3ae11fed5fdf3ba2364d33d90 done +#20 naming to docker.io/library/mtusi-messenger:lab4-test done +#20 DONE 0.0s +============================= test session starts ============================== +collecting ... collected 279 items + +student-tests/test_attachment_failures.py::test_s3_failure_leaves_accounted_intent_and_cleanup_retries[False] PASSED [ 0%] +student-tests/test_attachment_failures.py::test_s3_failure_leaves_accounted_intent_and_cleanup_retries[True] PASSED [ 0%] +student-tests/test_attachment_failures.py::test_database_failure_after_put_is_compensated PASSED [ 1%] +student-tests/test_attachment_failures.py::test_ambiguous_successful_commit_never_deletes_ready_file PASSED [ 1%] +student-tests/test_attachment_failures.py::test_cleanup_never_removes_old_ready_attachment PASSED [ 1%] +student-tests/test_attachment_failures.py::test_interrupted_body_creates_neither_metadata_nor_object PASSED [ 2%] +student-tests/test_attachments.py::test_original_metadata_and_bytes_for_another_member[PNG-image/png-\u0441\u043d\u0438\u043c\u043e\u043a.png] PASSED [ 2%] +student-tests/test_attachments.py::test_original_metadata_and_bytes_for_another_member[JPEG-image/jpeg-photo.jpeg] PASSED [ 2%] +student-tests/test_attachments.py::test_filename_is_not_an_object_identifier PASSED [ 3%] +student-tests/test_attachments.py::test_outsider_cannot_upload_read_or_reference_an_attachment PASSED [ 3%] +student-tests/test_attachments.py::test_attachment_cannot_move_between_chats_even_for_same_member PASSED [ 3%] +student-tests/test_attachments.py::test_unknown_attachment_is_404_without_creating_message PASSED [ 4%] +student-tests/test_attachments.py::test_any_member_can_send_attachment_without_text[] PASSED [ 4%] +student-tests/test_attachments.py::test_any_member_can_send_attachment_without_text[ \t\n] PASSED [ 5%] +student-tests/test_attachments.py::test_replay_compares_attachments_and_preserves_order PASSED [ 5%] +student-tests/test_attachments.py::test_attachment_list_has_strict_types[None] PASSED [ 5%] +student-tests/test_attachments.py::test_attachment_list_has_strict_types[not-a-list] PASSED [ 6%] +student-tests/test_attachments.py::test_attachment_list_has_strict_types[values2] PASSED [ 6%] +student-tests/test_attachments.py::test_attachment_list_has_strict_types[values3] PASSED [ 6%] +student-tests/test_attachments.py::test_duplicate_and_excessive_attachment_ids_are_400 PASSED [ 7%] +student-tests/test_attachments.py::test_dangerous_or_invalid_filename_is_400[] PASSED [ 7%] +student-tests/test_attachments.py::test_dangerous_or_invalid_filename_is_400[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa] PASSED [ 7%] +student-tests/test_attachments.py::test_dangerous_or_invalid_filename_is_400[../photo.png] PASSED [ 8%] +student-tests/test_attachments.py::test_dangerous_or_invalid_filename_is_400[folder/photo.png] PASSED [ 8%] +student-tests/test_attachments.py::test_dangerous_or_invalid_filename_is_400[folder\\photo.png] PASSED [ 8%] +student-tests/test_attachments.py::test_dangerous_or_invalid_filename_is_400[bad\x00.png] PASSED [ 9%] +student-tests/test_attachments.py::test_dangerous_or_invalid_filename_is_400[bad\r\n.png] PASSED [ 9%] +student-tests/test_attachments.py::test_dangerous_or_invalid_filename_is_400[bad\t.png] PASSED [ 10%] +student-tests/test_attachments.py::test_dangerous_or_invalid_filename_is_400[bad\x7f.png] PASSED [ 10%] +student-tests/test_attachments.py::test_dangerous_or_invalid_filename_is_400[bad\x85.png] PASSED [ 10%] +student-tests/test_attachments.py::test_missing_filename_is_400 PASSED [ 11%] +student-tests/test_attachments.py::test_invalid_image_is_415_not_500[-image/png] PASSED [ 11%] +student-tests/test_attachments.py::test_invalid_image_is_415_not_500[not an image-image/png] PASSED [ 11%] +student-tests/test_attachments.py::test_invalid_image_is_415_not_500[GIF89a\x01\x00\x01\x00-image/png] PASSED [ 12%] +student-tests/test_attachments.py::test_invalid_image_is_415_not_500[-image/svg+xml] PASSED [ 12%] +student-tests/test_attachments.py::test_declared_mime_must_match_real_image[PNG-image/jpeg] PASSED [ 12%] +student-tests/test_attachments.py::test_declared_mime_must_match_real_image[JPEG-image/png] PASSED [ 13%] +student-tests/test_attachments.py::test_declared_mime_must_match_real_image[PNG-application/octet-stream] PASSED [ 13%] +student-tests/test_attachments.py::test_truncated_image_is_rejected[PNG-image/png] PASSED [ 13%] +student-tests/test_attachments.py::test_truncated_image_is_rejected[JPEG-image/jpeg] PASSED [ 14%] +student-tests/test_attachments.py::test_png_with_corrupted_pixel_stream_is_rejected PASSED [ 14%] +student-tests/test_attachments.py::test_byte_limit_with_content_length PASSED [ 15%] +student-tests/test_attachments.py::test_byte_limit_without_content_length PASSED [ 15%] +student-tests/test_attachments.py::test_image_exactly_at_byte_limit_is_accepted PASSED [ 15%] +student-tests/test_attachments.py::test_pixel_limit_is_checked_before_decoding PASSED [ 16%] +student-tests/test_attachments.py::test_missing_variant_is_404 PASSED [ 16%] +student-tests/test_auth.py::test_register_returns_user_without_secrets PASSED [ 16%] +student-tests/test_auth.py::test_register_password_rules[None] PASSED [ 17%] +student-tests/test_auth.py::test_register_password_rules[] PASSED [ 17%] +student-tests/test_auth.py::test_register_password_rules[short] PASSED [ 17%] +student-tests/test_auth.py::test_register_password_rules[aaaaaaaaaaa] PASSED [ 18%] +student-tests/test_auth.py::test_register_password_rules[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa] PASSED [ 18%] +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 [ 19%] +student-tests/test_auth.py::test_register_password_border_lengths PASSED [ 19%] +student-tests/test_auth.py::test_login_success_shape PASSED [ 20%] +student-tests/test_auth.py::test_each_login_gives_new_token PASSED [ 20%] +student-tests/test_auth.py::test_wrong_password_and_unknown_user_look_the_same PASSED [ 20%] +student-tests/test_auth.py::test_malformed_basic_is_401[Basic] PASSED [ 21%] +student-tests/test_auth.py::test_malformed_basic_is_401[Basic !!!not-base64!!!] PASSED [ 21%] +student-tests/test_auth.py::test_malformed_basic_is_401[Basic bm9jb2xvbg==] PASSED [ 21%] +student-tests/test_auth.py::test_malformed_basic_is_401[Basic 0LjQvNGPOnBhc3N3b3JkLTEyMzQ1Ng==] PASSED [ 22%] +student-tests/test_auth.py::test_malformed_basic_is_401[Basic dXNlcjr//i1iYWQtdXRmOA==] PASSED [ 22%] +student-tests/test_auth.py::test_malformed_basic_is_401[Bearer abc] PASSED [ 22%] +student-tests/test_auth.py::test_malformed_basic_is_401[] PASSED [ 23%] +student-tests/test_auth.py::test_login_without_header_is_401 PASSED [ 23%] +student-tests/test_auth.py::test_password_with_colon_and_unicode_works PASSED [ 24%] +student-tests/test_auth.py::test_bearer_token_gives_access PASSED [ 24%] +student-tests/test_auth.py::test_business_endpoint_rejects_basic PASSED [ 24%] +student-tests/test_auth.py::test_bad_or_unknown_bearer_is_401[] PASSED [ 25%] +student-tests/test_auth.py::test_bad_or_unknown_bearer_is_401[Bearer] PASSED [ 25%] +student-tests/test_auth.py::test_bad_or_unknown_bearer_is_401[Bearer short] PASSED [ 25%] +student-tests/test_auth.py::test_bad_or_unknown_bearer_is_401[Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx] PASSED [ 26%] +student-tests/test_auth.py::test_bad_or_unknown_bearer_is_401[Token abc] PASSED [ 26%] +student-tests/test_auth.py::test_bad_or_unknown_bearer_is_401[bearer] PASSED [ 26%] +student-tests/test_auth.py::test_x_user_id_alone_gives_no_access PASSED [ 27%] +student-tests/test_auth.py::test_x_user_id_does_not_change_owner_of_session PASSED [ 27%] +student-tests/test_auth.py::test_logout_revokes_token_and_repeat_is_401 PASSED [ 27%] +student-tests/test_auth.py::test_logout_without_token_is_401 PASSED [ 28%] +student-tests/test_auth.py::test_logout_revokes_only_current_session PASSED [ 28%] +student-tests/test_auth.py::test_session_lives_longer_than_300_seconds PASSED [ 29%] +student-tests/test_cache.py::test_user_profile_is_served_from_cache_with_bounded_ttl PASSED [ 29%] +student-tests/test_cache.py::test_cache_never_stores_raw_tokens_or_passwords PASSED [ 29%] +student-tests/test_cache.py::test_logout_revokes_cached_session PASSED [ 30%] +student-tests/test_cache.py::test_late_cache_fill_cannot_resurrect_revoked_session PASSED [ 30%] +student-tests/test_cache.py::test_flushed_cache_loses_nothing PASSED [ 30%] +student-tests/test_cache.py::test_cache_does_not_extend_short_session PASSED [ 31%] +student-tests/test_cache.py::test_staleness_after_out_of_band_delete_is_bounded_by_cache_ttl PASSED [ 31%] +student-tests/test_cache.py::test_redis_down_requests_bypass_cache_and_stay_fast PASSED [ 31%] +student-tests/test_cache.py::test_redis_down_never_trusts_anonymous_or_unknown_tokens PASSED [ 32%] +student-tests/test_cache.py::test_revocation_lost_while_redis_down_is_flushed_on_recovery PASSED [ 32%] +student-tests/test_cache.py::test_without_cache_url_cache_is_off PASSED [ 32%] +student-tests/test_cache.py::test_lost_revocation_survives_api_restart[False] PASSED [ 33%] +student-tests/test_cache.py::test_lost_revocation_survives_api_restart[True] PASSED [ 33%] +student-tests/test_cache.py::test_cached_session_deleted_in_database_is_rejected_immediately PASSED [ 34%] +student-tests/test_concurrency.py::test_100_messages_from_10_clients_in_parallel PASSED [ 34%] +student-tests/test_concurrency.py::test_parallel_chat_creation_and_reading PASSED [ 34%] +student-tests/test_contract_basics.py::test_health_endpoints PASSED [ 35%] +student-tests/test_contract_basics.py::test_every_response_has_unique_request_id PASSED [ 35%] +student-tests/test_contract_basics.py::test_unknown_path_is_404_in_common_format PASSED [ 35%] +student-tests/test_contract_basics.py::test_trailing_slash_is_not_redirected PASSED [ 36%] +student-tests/test_contract_basics.py::test_wrong_method_is_405_in_common_format PASSED [ 36%] +student-tests/test_contract_basics.py::test_two_users_one_chat_two_messages PASSED [ 36%] +student-tests/test_contract_basics.py::test_chat_response_shape PASSED [ 37%] +student-tests/test_contract_basics.py::test_unexpected_exception_gives_500_without_details PASSED [ 37%] +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 [ 38%] +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 [ 39%] +student-tests/test_database.py::test_expected_tables_and_indexes_exist PASSED [ 40%] +student-tests/test_database.py::test_primary_and_foreign_keys_are_declared PASSED [ 40%] +student-tests/test_database.py::test_database_itself_rejects_bad_rows PASSED [ 40%] +student-tests/test_database.py::test_chat_creation_is_atomic PASSED [ 41%] +student-tests/test_database.py::test_successful_chat_has_all_members_in_database PASSED [ 41%] +student-tests/test_db_failure.py::test_database_down_gives_503_and_never_trusts_anyone PASSED [ 41%] +student-tests/test_db_failure.py::test_database_down_with_cache_up_still_needs_database_for_data PASSED [ 42%] +student-tests/test_duplicate_username.py::test_second_registration_with_same_username_is_409 PASSED [ 42%] +student-tests/test_duplicate_username.py::test_duplicate_does_not_replace_original_user PASSED [ 43%] +student-tests/test_duplicate_username.py::test_username_is_case_sensitive_lowercase_only PASSED [ 43%] +student-tests/test_duplicate_username.py::test_parallel_registration_of_same_username_creates_exactly_one PASSED [ 43%] +student-tests/test_empty_message.py::test_blank_text_is_rejected[] PASSED [ 44%] +student-tests/test_empty_message.py::test_blank_text_is_rejected[ ] PASSED [ 44%] +student-tests/test_empty_message.py::test_blank_text_is_rejected[ ] PASSED [ 44%] +student-tests/test_empty_message.py::test_blank_text_is_rejected[\n] PASSED [ 45%] +student-tests/test_empty_message.py::test_blank_text_is_rejected[\t \n] PASSED [ 45%] +student-tests/test_empty_message.py::test_blank_text_is_rejected[\xa0] PASSED [ 45%] +student-tests/test_empty_message.py::test_blank_text_is_rejected[\u2003] PASSED [ 46%] +student-tests/test_empty_message.py::test_missing_or_wrong_type_text_is_rejected[body0] PASSED [ 46%] +student-tests/test_empty_message.py::test_missing_or_wrong_type_text_is_rejected[body1] PASSED [ 46%] +student-tests/test_empty_message.py::test_missing_or_wrong_type_text_is_rejected[body2] PASSED [ 47%] +student-tests/test_empty_message.py::test_missing_or_wrong_type_text_is_rejected[body3] PASSED [ 47%] +student-tests/test_empty_message.py::test_missing_or_wrong_type_text_is_rejected[body4] PASSED [ 48%] +student-tests/test_empty_message.py::test_text_length_is_counted_in_characters PASSED [ 48%] +student-tests/test_empty_message.py::test_nul_character_is_400_not_500 PASSED [ 48%] +student-tests/test_empty_message.py::test_unicode_survives_the_database PASSED [ 49%] +student-tests/test_empty_message.py::test_message_has_correct_fields PASSED [ 49%] +student-tests/test_explain.py::test_history_page_uses_index_without_full_scan[0] PASSED [ 49%] +student-tests/test_explain.py::test_history_page_uses_index_without_full_scan[9000] PASSED [ 50%] +student-tests/test_foreign_chat.py::test_outsider_cannot_read_chat PASSED [ 50%] +student-tests/test_foreign_chat.py::test_outsider_cannot_read_history PASSED [ 50%] +student-tests/test_foreign_chat.py::test_outsider_cannot_write PASSED [ 51%] +student-tests/test_foreign_chat.py::test_outsider_gets_404_even_with_bad_body PASSED [ 51%] +student-tests/test_foreign_chat.py::test_unknown_chat_looks_the_same_as_foreign PASSED [ 51%] +student-tests/test_foreign_chat.py::test_chat_list_shows_only_own_chats PASSED [ 52%] +student-tests/test_foreign_chat.py::test_bad_chat_id_is_400 PASSED [ 52%] +student-tests/test_foreign_chat.py::test_no_token_is_401_everywhere PASSED [ 53%] +student-tests/test_foreign_chat.py::test_401_comes_before_404 PASSED [ 53%] +student-tests/test_foreign_chat.py::test_member_can_read_and_write PASSED [ 53%] +student-tests/test_idempotency.py::test_key_is_required_and_validated[None] PASSED [ 54%] +student-tests/test_idempotency.py::test_key_is_required_and_validated[] PASSED [ 54%] +student-tests/test_idempotency.py::test_key_is_required_and_validated[a b] PASSED [ 54%] +student-tests/test_idempotency.py::test_key_is_required_and_validated[a?b] PASSED [ 55%] +student-tests/test_idempotency.py::test_key_is_required_and_validated[xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx] PASSED [ 55%] +student-tests/test_idempotency.py::test_key_is_required_and_validated[a,b] PASSED [ 55%] +student-tests/test_idempotency.py::test_key_is_required_and_validated[a/b] PASSED [ 56%] +student-tests/test_idempotency.py::test_duplicate_headers_are_invalid PASSED [ 56%] +student-tests/test_idempotency.py::test_replay_and_conflict_preserve_original_result PASSED [ 56%] +student-tests/test_idempotency.py::test_parallel_same_key_across_two_applications PASSED [ 57%] +student-tests/test_idempotency.py::test_key_scope_includes_user_and_chat PASSED [ 57%] +student-tests/test_idempotency.py::test_expired_result_allows_new_message PASSED [ 58%] +student-tests/test_idempotency.py::test_failure_between_message_and_result_rolls_back PASSED [ 58%] +student-tests/test_idempotency.py::test_committed_but_lost_response_is_safe_to_retry PASSED [ 58%] +student-tests/test_idempotency.py::test_replay_rechecks_session_and_chat_access PASSED [ 59%] +student-tests/test_invalid_json.py::test_registration_rejects_broken_json[{] PASSED [ 59%] +student-tests/test_invalid_json.py::test_registration_rejects_broken_json[not json at all] PASSED [ 59%] +student-tests/test_invalid_json.py::test_registration_rejects_broken_json[] PASSED [ 60%] +student-tests/test_invalid_json.py::test_registration_rejects_broken_json[{"username": "abc",}] PASSED [ 60%] +student-tests/test_invalid_json.py::test_registration_rejects_broken_json[\xff\xfe\x00] PASSED [ 60%] +student-tests/test_invalid_json.py::test_registration_rejects_broken_json[{"username": NaN}] PASSED [ 61%] +student-tests/test_invalid_json.py::test_registration_rejects_broken_json[["username"]] PASSED [ 61%] +student-tests/test_invalid_json.py::test_registration_rejects_broken_json["text"] PASSED [ 62%] +student-tests/test_invalid_json.py::test_registration_rejects_broken_json[null] PASSED [ 62%] +student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes0] PASSED [ 62%] +student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes1] PASSED [ 63%] +student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes2] PASSED [ 63%] +student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes3] PASSED [ 63%] +student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes4] PASSED [ 64%] +student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes5] PASSED [ 64%] +student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes6] PASSED [ 64%] +student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes7] PASSED [ 65%] +student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes8] PASSED [ 65%] +student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes9] PASSED [ 65%] +student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes10] PASSED [ 66%] +student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes11] PASSED [ 66%] +student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes12] PASSED [ 67%] +student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes13] PASSED [ 67%] +student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes14] PASSED [ 67%] +student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes15] PASSED [ 68%] +student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes16] PASSED [ 68%] +student-tests/test_invalid_json.py::test_border_values_are_accepted PASSED [ 68%] +student-tests/test_invalid_json.py::test_lone_surrogate_is_400_not_500 PASSED [ 69%] +student-tests/test_invalid_json.py::test_wrong_or_missing_content_type_is_415 PASSED [ 69%] +student-tests/test_invalid_json.py::test_huge_body_is_413 PASSED [ 69%] +student-tests/test_invalid_json.py::test_broken_json_in_message_and_chat PASSED [ 70%] +student-tests/test_invalid_json.py::test_sender_id_in_message_is_rejected PASSED [ 70%] +student-tests/test_invalid_json.py::test_chat_body_validation PASSED [ 70%] +student-tests/test_pagination.py::test_walk_25_messages_by_7 PASSED [ 71%] +student-tests/test_pagination.py::test_exact_multiple_has_no_empty_extra_page PASSED [ 71%] +student-tests/test_pagination.py::test_limit_one_walks_everything PASSED [ 72%] +student-tests/test_pagination.py::test_default_limit_is_50_and_max_is_100 PASSED [ 72%] +student-tests/test_pagination.py::test_empty_history PASSED [ 72%] +student-tests/test_pagination.py::test_limit_can_change_between_pages PASSED [ 73%] +student-tests/test_pagination.py::test_messages_sent_during_walk_appear_at_the_end PASSED [ 73%] +student-tests/test_pagination.py::test_created_at_is_increasing_and_utc PASSED [ 73%] +student-tests/test_pagination.py::test_bad_limit_values PASSED [ 74%] +student-tests/test_pagination.py::test_garbage_cursor_is_400 PASSED [ 74%] +student-tests/test_pagination.py::test_cursor_from_another_chat_is_400 PASSED [ 74%] +student-tests/test_pagination.py::test_chat_list_pagination PASSED [ 75%] +student-tests/test_pagination.py::test_chat_list_cursor_cannot_be_used_for_messages 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-1000000000000000000000000000000] PASSED [ 76%] +student-tests/test_pagination.py::test_invalid_cursor_timestamp_is_400[messages-253402300800000000] PASSED [ 76%] +student-tests/test_pagination.py::test_invalid_cursor_timestamp_is_400[messages-True] PASSED [ 77%] +student-tests/test_pagination.py::test_invalid_cursor_timestamp_is_400[messages-1.5] PASSED [ 77%] +student-tests/test_pagination.py::test_invalid_cursor_timestamp_is_400[messages-1] PASSED [ 77%] +student-tests/test_pagination.py::test_invalid_cursor_timestamp_is_400[messages-None] 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-1000000000000000000000000000000] PASSED [ 78%] +student-tests/test_pagination.py::test_invalid_cursor_timestamp_is_400[chats-253402300800000000] PASSED [ 79%] +student-tests/test_pagination.py::test_invalid_cursor_timestamp_is_400[chats-True] PASSED [ 79%] +student-tests/test_pagination.py::test_invalid_cursor_timestamp_is_400[chats-1.5] PASSED [ 79%] +student-tests/test_pagination.py::test_invalid_cursor_timestamp_is_400[chats-1] PASSED [ 80%] +student-tests/test_pagination.py::test_invalid_cursor_timestamp_is_400[chats-None] PASSED [ 80%] +student-tests/test_pagination.py::test_cursor_timestamp_boundaries_are_valid[messages-0] PASSED [ 81%] +student-tests/test_pagination.py::test_cursor_timestamp_boundaries_are_valid[messages-253402300799999999] PASSED [ 81%] +student-tests/test_pagination.py::test_cursor_timestamp_boundaries_are_valid[chats-0] PASSED [ 81%] +student-tests/test_pagination.py::test_cursor_timestamp_boundaries_are_valid[chats-253402300799999999] PASSED [ 82%] +student-tests/test_session_expiry.py::test_session_expires_after_ttl PASSED [ 82%] +student-tests/test_session_expiry.py::test_expired_sessions_are_cleaned_on_next_login PASSED [ 82%] +student-tests/test_session_expiry.py::test_short_ttl_from_environment_variable PASSED [ 83%] +student-tests/test_transactions.py::test_failure_after_chat_insert_rolls_back_whole_operation PASSED [ 83%] +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 [ 84%] +unit-tests/test_attachment_intent.py::test_removed_intent_prevents_put_when_uploader_resumes PASSED [ 84%] +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 [ 85%] +unit-tests/test_message_transaction.py::test_replay_returns_original_message_and_releases_transaction PASSED [ 85%] +unit-tests/test_message_transaction.py::test_failed_commit_rolls_back_and_propagates_error PASSED [ 86%] +unit-tests/test_mr_regressions.py::test_stale_positive_cache_never_authorizes_revoked_token[/api/v1/users/me-GET] PASSED [ 86%] +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 [ 87%] +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 [ 89%] +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 [ 90%] +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 [ 91%] +unit-tests/test_mr_regressions.py::test_cursor_timestamp_is_rejected_through_http[chats-True] PASSED [ 91%] +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 [ 92%] +unit-tests/test_mr_regressions.py::test_cursor_timestamp_is_rejected_through_http[chats-None] PASSED [ 92%] +unit-tests/test_mr_regressions.py::test_timestamp_boundaries_roundtrip[0] PASSED [ 93%] +unit-tests/test_mr_regressions.py::test_timestamp_boundaries_roundtrip[253402300799999999] PASSED [ 93%] +unit-tests/test_mr_regressions.py::test_message_key_validation[None] PASSED [ 93%] +unit-tests/test_mr_regressions.py::test_message_key_validation[] PASSED [ 94%] +unit-tests/test_mr_regressions.py::test_message_key_validation[bad key] PASSED [ 94%] +unit-tests/test_mr_regressions.py::test_message_key_validation[xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx] PASSED [ 94%] +unit-tests/test_mr_regressions.py::test_message_key_validation[a?b] PASSED [ 95%] +unit-tests/test_mr_regressions.py::test_message_key_validation[a,b] PASSED [ 95%] +unit-tests/test_mr_regressions.py::test_message_normalization_and_conflict PASSED [ 96%] +unit-tests/test_mr_regressions.py::test_instance_header_matches_process_on_all_responses[/health/live] PASSED [ 96%] +unit-tests/test_mr_regressions.py::test_instance_header_matches_process_on_all_responses[/missing] PASSED [ 96%] +unit-tests/test_mr_regressions.py::test_instance_header_matches_process_on_all_responses[/api/v1/users/me] PASSED [ 97%] +unit-tests/test_mr_regressions.py::test_instance_alias_changes_between_applications PASSED [ 97%] +unit-tests/test_mr_regressions.py::test_sigterm_drops_readiness_and_requests_graceful_exit PASSED [ 97%] +unit-tests/test_operator_scripts.py::BalancedReplicasTests::test_old_process_alias_does_not_replace_current_replica PASSED [ 98%] +unit-tests/test_operator_scripts.py::BalancedReplicasTests::test_one_ready_replica_never_counts_as_balanced PASSED [ 98%] +unit-tests/test_operator_scripts.py::BalancedReplicasTests::test_waits_until_both_replicas_answer_through_lb PASSED [ 98%] +unit-tests/test_operator_scripts.py::HookCommandTests::test_missing_shell_reports_failure PASSED [ 99%] +unit-tests/test_operator_scripts.py::HookCommandTests::test_python_hook_uses_current_interpreter_and_preserves_spaces PASSED [ 99%] +unit-tests/test_operator_scripts.py::HookCommandTests::test_shell_is_resolved_from_path 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 +================== 279 passed, 1 warning in 64.67s (0:01:04) =================== +2026/11/06 00:33:29 http2: server: error reading preface from client //./pipe/dockerDesktopLinuxEngine: file has already been closed + Container mtusi-messenger-lab4-minio-1 Running + Container mtusi-messenger-lab4-redis-1 Running + Container mtusi-messenger-lab4-db-1 Running + Container mtusi-messenger-lab4-minio-init-1 Created + Container mtusi-messenger-lab4-minio-1 Waiting + Container mtusi-messenger-lab4-minio-1 Healthy + Container mtusi-messenger-lab4-minio-init-1 Starting + Container mtusi-messenger-lab4-minio-init-1 Started +2026/11/06 00:33:31 http2: server: error reading preface from client //./pipe/dockerDesktopLinuxEngine: file has already been closed + +exit_code=0; elapsed_seconds=75.391 diff --git a/evidence/lab4/10_final_processes.json b/evidence/lab4/10_final_processes.json new file mode 100644 index 00000000..0a8683f3 --- /dev/null +++ b/evidence/lab4/10_final_processes.json @@ -0,0 +1,20 @@ +[ + { + "service": "api-a", + "container_id": "72fd226c23b838293f4d7f0347a4541a13d0965cf37cc657e8f79fc8aedf0ae5", + "pid": 395, + "state": "running", + "started_at": "2026-11-05T21:32:21.171649446Z", + "status": 200, + "instance_id": "a7cafe615bac4e4fa5f098568c25444a" + }, + { + "service": "api-b", + "container_id": "a9f95a6b71d1029cf15151be7638e7c65aa7e4a3d2ca9bd9bad25c5fc3a1c8dc", + "pid": 394, + "state": "running", + "started_at": "2026-11-05T21:32:21.167275356Z", + "status": 200, + "instance_id": "d5ef73b927474fdb980ddc3283a8cd43" + } +] diff --git a/evidence/lab4/cluster/00_environment.json b/evidence/lab4/cluster/00_environment.json new file mode 100644 index 00000000..b95261c4 --- /dev/null +++ b/evidence/lab4/cluster/00_environment.json @@ -0,0 +1,8 @@ +{ + "at_utc": "2026-11-05T21:25:23.526473+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/lab4/cluster/01_docker.txt b/evidence/lab4/cluster/01_docker.txt new file mode 100644 index 00000000..a8178832 --- /dev/null +++ b/evidence/lab4/cluster/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.187 diff --git a/evidence/lab4/cluster/02_compose_config.txt b/evidence/lab4/cluster/02_compose_config.txt new file mode 100644 index 00000000..e95e7486 --- /dev/null +++ b/evidence/lab4/cluster/02_compose_config.txt @@ -0,0 +1,3 @@ +$ docker compose --env-file .env config --quiet + +exit_code=0; elapsed_seconds=0.781 diff --git a/evidence/lab4/cluster/03_container_limits.txt b/evidence/lab4/cluster/03_container_limits.txt new file mode 100644 index 00000000..f993f8bb --- /dev/null +++ b/evidence/lab4/cluster/03_container_limits.txt @@ -0,0 +1,10 @@ +$ docker inspect --format {{.Name}} cpus={{.HostConfig.NanoCpus}} memory={{.HostConfig.Memory}} ports={{json .HostConfig.PortBindings}} 40a18c8ca38b1936a7b5c72b207185a37d88ea1e2f6b8426484ac3dd42918980 48861518b3100b7bb01acebf81279b6997c6cceb512f1a666645e0e0b69f52e3 a689d4a722eee940c4f72fb8577beda47d8b25c70682fdacc4dd4a27cd88d8a1 f46211fa086cd11d2cfcc06e1c5fcfb8dda13b13f6add127b979cc681d2288ec 006abc78338bfd1dc7f03665a28df182737e1c2f78eacf91fc192d7237b73e49 aa24ebc2760f115b96d8ec51f43de68747582264d5097c52e8f63782b2873d44 b17bf3ec3fad7c070567da5524504f33b9ac532e415fbe73191e01b590242d28 +/mtusi-messenger-lab4-api-a-1 cpus=500000000 memory=536870912 ports={} +/mtusi-messenger-lab4-api-b-1 cpus=500000000 memory=536870912 ports={} +/mtusi-messenger-lab4-cleanup-1 cpus=250000000 memory=268435456 ports={} +/mtusi-messenger-lab4-db-1 cpus=1000000000 memory=536870912 ports={} +/mtusi-messenger-lab4-minio-1 cpus=1000000000 memory=536870912 ports={} +/mtusi-messenger-lab4-nginx-1 cpus=250000000 memory=100663296 ports={"8080/tcp":[{"HostIp":"127.0.0.1","HostPort":"8080"}]} +/mtusi-messenger-lab4-redis-1 cpus=250000000 memory=134217728 ports={} + +exit_code=0; elapsed_seconds=0.109 diff --git a/evidence/lab4/cluster/03_limits.txt b/evidence/lab4/cluster/03_limits.txt new file mode 100644 index 00000000..d18357bb --- /dev/null +++ b/evidence/lab4/cluster/03_limits.txt @@ -0,0 +1,10 @@ +$ docker compose --env-file .env ps -q +40a18c8ca38b1936a7b5c72b207185a37d88ea1e2f6b8426484ac3dd42918980 +48861518b3100b7bb01acebf81279b6997c6cceb512f1a666645e0e0b69f52e3 +a689d4a722eee940c4f72fb8577beda47d8b25c70682fdacc4dd4a27cd88d8a1 +f46211fa086cd11d2cfcc06e1c5fcfb8dda13b13f6add127b979cc681d2288ec +006abc78338bfd1dc7f03665a28df182737e1c2f78eacf91fc192d7237b73e49 +aa24ebc2760f115b96d8ec51f43de68747582264d5097c52e8f63782b2873d44 +b17bf3ec3fad7c070567da5524504f33b9ac532e415fbe73191e01b590242d28 + +exit_code=0; elapsed_seconds=0.344 diff --git a/evidence/lab4/cluster/03_processes.json b/evidence/lab4/cluster/03_processes.json new file mode 100644 index 00000000..f40292fb --- /dev/null +++ b/evidence/lab4/cluster/03_processes.json @@ -0,0 +1,20 @@ +[ + { + "service": "api-a", + "container_id": "40a18c8ca38b1936a7b5c72b207185a37d88ea1e2f6b8426484ac3dd42918980", + "pid": 45133, + "state": "running", + "started_at": "2026-11-05T21:13:49.80510938Z", + "status": 200, + "instance_id": "5812f0fc0c844d75a665c9b8d2e4c677" + }, + { + "service": "api-b", + "container_id": "48861518b3100b7bb01acebf81279b6997c6cceb512f1a666645e0e0b69f52e3", + "pid": 5437, + "state": "running", + "started_at": "2026-11-05T20:59:54.223635268Z", + "status": 200, + "instance_id": "a310ac89621f4572b8f224776811f125" + } +] diff --git a/evidence/lab4/cluster/04_smoke.txt b/evidence/lab4/cluster/04_smoke.txt new file mode 100644 index 00000000..16e4016d --- /dev/null +++ b/evidence/lab4/cluster/04_smoke.txt @@ -0,0 +1,4 @@ +$ C:\Users\ira\OneDrive\Desktop\work\vp\mtusi2026lab4-fork\.venv\Scripts\python.exe tests/smoke.py --base-url http://127.0.0.1:8080 +PASS: lab4 public API scenario; infrastructure and grading evidence remain separate. + +exit_code=0; elapsed_seconds=2.141 diff --git a/evidence/lab4/cluster/05_concurrent_idempotency.json b/evidence/lab4/cluster/05_concurrent_idempotency.json new file mode 100644 index 00000000..3ff91da7 --- /dev/null +++ b/evidence/lab4/cluster/05_concurrent_idempotency.json @@ -0,0 +1,10 @@ +{ + "parallel_requests": 12, + "unique_message_ids": 1, + "history_rows": 1, + "instances": [ + "5812f0fc0c844d75a665c9b8d2e4c677", + "a310ac89621f4572b8f224776811f125" + ], + "conflict_status": 409 +} diff --git a/evidence/lab4/cluster/06_failover_api-a.json b/evidence/lab4/cluster/06_failover_api-a.json new file mode 100644 index 00000000..f1273a42 --- /dev/null +++ b/evidence/lab4/cluster/06_failover_api-a.json @@ -0,0 +1,108 @@ +{ + "mode": "stop", + "service": "api-a", + "target_instance": "5812f0fc0c844d75a665c9b8d2e4c677", + "observed_with_one_session": [ + "5812f0fc0c844d75a665c9b8d2e4c677", + "a310ac89621f4572b8f224776811f125" + ], + "before": [ + { + "service": "api-a", + "container_id": "40a18c8ca38b1936a7b5c72b207185a37d88ea1e2f6b8426484ac3dd42918980", + "pid": 45133, + "state": "running", + "started_at": "2026-11-05T21:13:49.80510938Z", + "status": 200, + "instance_id": "5812f0fc0c844d75a665c9b8d2e4c677" + }, + { + "service": "api-b", + "container_id": "48861518b3100b7bb01acebf81279b6997c6cceb512f1a666645e0e0b69f52e3", + "pid": 5437, + "state": "running", + "started_at": "2026-11-05T20:59:54.223635268Z", + "status": 200, + "instance_id": "a310ac89621f4572b8f224776811f125" + } + ], + "after_action": [ + { + "service": "api-a", + "container_id": "40a18c8ca38b1936a7b5c72b207185a37d88ea1e2f6b8426484ac3dd42918980", + "pid": 0, + "state": "exited", + "started_at": "2026-11-05T21:13:49.80510938Z" + }, + { + "service": "api-b", + "container_id": "48861518b3100b7bb01acebf81279b6997c6cceb512f1a666645e0e0b69f52e3", + "pid": 5437, + "state": "running", + "started_at": "2026-11-05T20:59:54.223635268Z", + "status": 200, + "instance_id": "a310ac89621f4572b8f224776811f125" + } + ], + "action": "{\"stopped_service\": \"api-a\", \"instance_id\": \"5812f0fc0c844d75a665c9b8d2e4c677\"}\n", + "action_seconds": 7.687, + "concurrency": 2, + "total": { + "elapsed_seconds": 21.156, + "attempts": 618, + "successes": 618, + "errors": 0, + "error_ratio": 0.0, + "successful_rps": 29.212, + "successful_latency_ms": { + "p50": 47.0, + "p95": 172.0, + "p99": 188.0 + } + }, + "during_and_after_action": { + "elapsed_seconds": 20.156, + "attempts": 584, + "successes": 584, + "errors": 0, + "error_ratio": 0.0, + "successful_rps": 28.974, + "successful_latency_ms": { + "p50": 47.0, + "p95": 172.0, + "p99": 203.0 + } + }, + "confirmed_responses": 310, + "history_messages": 310, + "duplicates": 0, + "existing_session_works": true, + "first_write_after_action_seconds": 7.844, + "write": { + "elapsed_seconds": 21.156, + "attempts": 309, + "successes": 309, + "errors": 0, + "error_ratio": 0.0, + "successful_rps": 14.606, + "successful_latency_ms": { + "p50": 62.0, + "p95": 172.0, + "p99": 266.0 + } + }, + "first_read_after_action_seconds": 7.75, + "read": { + "elapsed_seconds": 21.156, + "attempts": 309, + "successes": 309, + "errors": 0, + "error_ratio": 0.0, + "successful_rps": 14.606, + "successful_latency_ms": { + "p50": 31.0, + "p95": 109.0, + "p99": 188.0 + } + } +} diff --git a/evidence/lab4/cluster/06_failover_api-b.json b/evidence/lab4/cluster/06_failover_api-b.json new file mode 100644 index 00000000..ad3b858a --- /dev/null +++ b/evidence/lab4/cluster/06_failover_api-b.json @@ -0,0 +1,108 @@ +{ + "mode": "stop", + "service": "api-b", + "target_instance": "a310ac89621f4572b8f224776811f125", + "observed_with_one_session": [ + "3cbae4a060234d11bd62c55983052436", + "a310ac89621f4572b8f224776811f125" + ], + "before": [ + { + "service": "api-a", + "container_id": "40a18c8ca38b1936a7b5c72b207185a37d88ea1e2f6b8426484ac3dd42918980", + "pid": 79938, + "state": "running", + "started_at": "2026-11-05T21:26:13.450006912Z", + "status": 200, + "instance_id": "3cbae4a060234d11bd62c55983052436" + }, + { + "service": "api-b", + "container_id": "48861518b3100b7bb01acebf81279b6997c6cceb512f1a666645e0e0b69f52e3", + "pid": 5437, + "state": "running", + "started_at": "2026-11-05T20:59:54.223635268Z", + "status": 200, + "instance_id": "a310ac89621f4572b8f224776811f125" + } + ], + "after_action": [ + { + "service": "api-a", + "container_id": "40a18c8ca38b1936a7b5c72b207185a37d88ea1e2f6b8426484ac3dd42918980", + "pid": 79938, + "state": "running", + "started_at": "2026-11-05T21:26:13.450006912Z", + "status": 200, + "instance_id": "3cbae4a060234d11bd62c55983052436" + }, + { + "service": "api-b", + "container_id": "48861518b3100b7bb01acebf81279b6997c6cceb512f1a666645e0e0b69f52e3", + "pid": 0, + "state": "exited", + "started_at": "2026-11-05T20:59:54.223635268Z" + } + ], + "action": "{\"stopped_service\": \"api-b\", \"instance_id\": \"a310ac89621f4572b8f224776811f125\"}\n", + "action_seconds": 8.062, + "concurrency": 2, + "total": { + "elapsed_seconds": 21.14, + "attempts": 572, + "successes": 572, + "errors": 0, + "error_ratio": 0.0, + "successful_rps": 27.058, + "successful_latency_ms": { + "p50": 47.0, + "p95": 172.0, + "p99": 296.0 + } + }, + "during_and_after_action": { + "elapsed_seconds": 20.14, + "attempts": 537, + "successes": 537, + "errors": 0, + "error_ratio": 0.0, + "successful_rps": 26.663, + "successful_latency_ms": { + "p50": 47.0, + "p95": 187.0, + "p99": 296.0 + } + }, + "confirmed_responses": 287, + "history_messages": 287, + "duplicates": 0, + "existing_session_works": true, + "first_write_after_action_seconds": 8.422, + "write": { + "elapsed_seconds": 21.14, + "attempts": 286, + "successes": 286, + "errors": 0, + "error_ratio": 0.0, + "successful_rps": 13.529, + "successful_latency_ms": { + "p50": 47.0, + "p95": 188.0, + "p99": 313.0 + } + }, + "first_read_after_action_seconds": 8.531, + "read": { + "elapsed_seconds": 21.14, + "attempts": 286, + "successes": 286, + "errors": 0, + "error_ratio": 0.0, + "successful_rps": 13.529, + "successful_latency_ms": { + "p50": 31.0, + "p95": 125.0, + "p99": 203.0 + } + } +} diff --git a/evidence/lab4/cluster/07_public_failover.txt b/evidence/lab4/cluster/07_public_failover.txt new file mode 100644 index 00000000..e020236c --- /dev/null +++ b/evidence/lab4/cluster/07_public_failover.txt @@ -0,0 +1,6 @@ +$ C:\Users\ira\OneDrive\Desktop\work\vp\mtusi2026lab4-fork\.venv\Scripts\python.exe tests/resilience.py --mode failover --action-script scripts/stop_one.py --base-url http://127.0.0.1:8080 +Executing explicit failover hook; hook output suppressed to avoid leaking secrets. +{"observed_instances_before": 2, "successful_writes": 92, "failed_attempts": 0, "first_recovery_seconds": 6.672} +PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence. + +exit_code=0; elapsed_seconds=16.500 diff --git a/evidence/lab4/cluster/08_rolling_api-a.json b/evidence/lab4/cluster/08_rolling_api-a.json new file mode 100644 index 00000000..8ff832df --- /dev/null +++ b/evidence/lab4/cluster/08_rolling_api-a.json @@ -0,0 +1,114 @@ +{ + "mode": "replace", + "service": "api-a", + "target_instance": "3cbae4a060234d11bd62c55983052436", + "observed_with_one_session": [ + "3cbae4a060234d11bd62c55983052436", + "577f6e21fa2b446e8cd18aa39fde8860" + ], + "before": [ + { + "service": "api-a", + "container_id": "40a18c8ca38b1936a7b5c72b207185a37d88ea1e2f6b8426484ac3dd42918980", + "pid": 79938, + "state": "running", + "started_at": "2026-11-05T21:26:13.450006912Z", + "status": 200, + "instance_id": "3cbae4a060234d11bd62c55983052436" + }, + { + "service": "api-b", + "container_id": "48861518b3100b7bb01acebf81279b6997c6cceb512f1a666645e0e0b69f52e3", + "pid": 83510, + "state": "running", + "started_at": "2026-11-05T21:27:24.402019502Z", + "status": 200, + "instance_id": "577f6e21fa2b446e8cd18aa39fde8860" + } + ], + "after_action": [ + { + "service": "api-a", + "container_id": "768ac85d5f339e30fc74b7e9901b541443d7c4fecb95e9a2a181229d2d8e32ed", + "pid": 85398, + "state": "running", + "started_at": "2026-11-05T21:27:58.904230152Z", + "status": 200, + "instance_id": "82bdf56c2b214ba8b334c0aabb146756" + }, + { + "service": "api-b", + "container_id": "48861518b3100b7bb01acebf81279b6997c6cceb512f1a666645e0e0b69f52e3", + "pid": 83510, + "state": "running", + "started_at": "2026-11-05T21:27:24.402019502Z", + "status": 200, + "instance_id": "577f6e21fa2b446e8cd18aa39fde8860" + } + ], + "action": { + "service": "api-a", + "before": "3cbae4a060234d11bd62c55983052436", + "after": "82bdf56c2b214ba8b334c0aabb146756" + }, + "action_seconds": 34.469, + "concurrency": 2, + "total": { + "elapsed_seconds": 42.062, + "attempts": 1100, + "successes": 1100, + "errors": 0, + "error_ratio": 0.0, + "successful_rps": 26.152, + "successful_latency_ms": { + "p50": 47.0, + "p95": 172.0, + "p99": 266.0 + } + }, + "during_and_after_action": { + "elapsed_seconds": 41.062, + "attempts": 1059, + "successes": 1059, + "errors": 0, + "error_ratio": 0.0, + "successful_rps": 25.79, + "successful_latency_ms": { + "p50": 47.0, + "p95": 172.0, + "p99": 266.0 + } + }, + "confirmed_responses": 551, + "history_messages": 551, + "duplicates": 0, + "existing_session_works": true, + "first_write_after_action_seconds": 34.641, + "write": { + "elapsed_seconds": 42.062, + "attempts": 550, + "successes": 550, + "errors": 0, + "error_ratio": 0.0, + "successful_rps": 13.076, + "successful_latency_ms": { + "p50": 63.0, + "p95": 188.0, + "p99": 281.0 + } + }, + "first_read_after_action_seconds": 34.547, + "read": { + "elapsed_seconds": 42.062, + "attempts": 550, + "successes": 550, + "errors": 0, + "error_ratio": 0.0, + "successful_rps": 13.076, + "successful_latency_ms": { + "p50": 31.0, + "p95": 141.0, + "p99": 188.0 + } + } +} diff --git a/evidence/lab4/cluster/08_rolling_api-b.json b/evidence/lab4/cluster/08_rolling_api-b.json new file mode 100644 index 00000000..10fc3a87 --- /dev/null +++ b/evidence/lab4/cluster/08_rolling_api-b.json @@ -0,0 +1,114 @@ +{ + "mode": "replace", + "service": "api-b", + "target_instance": "577f6e21fa2b446e8cd18aa39fde8860", + "observed_with_one_session": [ + "577f6e21fa2b446e8cd18aa39fde8860", + "82bdf56c2b214ba8b334c0aabb146756" + ], + "before": [ + { + "service": "api-a", + "container_id": "768ac85d5f339e30fc74b7e9901b541443d7c4fecb95e9a2a181229d2d8e32ed", + "pid": 85398, + "state": "running", + "started_at": "2026-11-05T21:27:58.904230152Z", + "status": 200, + "instance_id": "82bdf56c2b214ba8b334c0aabb146756" + }, + { + "service": "api-b", + "container_id": "48861518b3100b7bb01acebf81279b6997c6cceb512f1a666645e0e0b69f52e3", + "pid": 83510, + "state": "running", + "started_at": "2026-11-05T21:27:24.402019502Z", + "status": 200, + "instance_id": "577f6e21fa2b446e8cd18aa39fde8860" + } + ], + "after_action": [ + { + "service": "api-a", + "container_id": "768ac85d5f339e30fc74b7e9901b541443d7c4fecb95e9a2a181229d2d8e32ed", + "pid": 85398, + "state": "running", + "started_at": "2026-11-05T21:27:58.904230152Z", + "status": 200, + "instance_id": "82bdf56c2b214ba8b334c0aabb146756" + }, + { + "service": "api-b", + "container_id": "28b2b2eea08ad0205cb3fc9208d2050767d38d34d22576398b97cdfeeee07257", + "pid": 87979, + "state": "running", + "started_at": "2026-11-05T21:28:47.633035368Z", + "status": 200, + "instance_id": "3e3954ec716d41f1a5a6ba9f19860797" + } + ], + "action": { + "service": "api-b", + "before": "577f6e21fa2b446e8cd18aa39fde8860", + "after": "3e3954ec716d41f1a5a6ba9f19860797" + }, + "action_seconds": 36.765, + "concurrency": 2, + "total": { + "elapsed_seconds": 44.984, + "attempts": 1036, + "successes": 1036, + "errors": 0, + "error_ratio": 0.0, + "successful_rps": 23.03, + "successful_latency_ms": { + "p50": 63.0, + "p95": 188.0, + "p99": 281.0 + } + }, + "during_and_after_action": { + "elapsed_seconds": 43.984, + "attempts": 1006, + "successes": 1006, + "errors": 0, + "error_ratio": 0.0, + "successful_rps": 22.872, + "successful_latency_ms": { + "p50": 63.0, + "p95": 188.0, + "p99": 281.0 + } + }, + "confirmed_responses": 519, + "history_messages": 519, + "duplicates": 0, + "existing_session_works": true, + "first_write_after_action_seconds": 36.859, + "write": { + "elapsed_seconds": 44.984, + "attempts": 518, + "successes": 518, + "errors": 0, + "error_ratio": 0.0, + "successful_rps": 11.515, + "successful_latency_ms": { + "p50": 78.0, + "p95": 265.0, + "p99": 297.0 + } + }, + "first_read_after_action_seconds": 36.875, + "read": { + "elapsed_seconds": 44.984, + "attempts": 518, + "successes": 518, + "errors": 0, + "error_ratio": 0.0, + "successful_rps": 11.515, + "successful_latency_ms": { + "p50": 47.0, + "p95": 110.0, + "p99": 203.0 + } + } +} diff --git a/evidence/lab4/cluster/09_persistence.txt b/evidence/lab4/cluster/09_persistence.txt new file mode 100644 index 00000000..20589301 --- /dev/null +++ b/evidence/lab4/cluster/09_persistence.txt @@ -0,0 +1,5 @@ +$ C:\Users\ira\OneDrive\Desktop\work\vp\mtusi2026lab4-fork\.venv\Scripts\python.exe tests/resilience.py --mode persistence --action-script scripts/restart.py --base-url http://127.0.0.1:8080 +Executing explicit persistence hook; hook output suppressed to avoid leaking secrets. +PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence. + +exit_code=0; elapsed_seconds=32.907 diff --git a/evidence/lab4/cluster/10_processes_after.json b/evidence/lab4/cluster/10_processes_after.json new file mode 100644 index 00000000..d076e5b7 --- /dev/null +++ b/evidence/lab4/cluster/10_processes_after.json @@ -0,0 +1,20 @@ +[ + { + "service": "api-a", + "container_id": "720a4321a8f2be54547c2c9ef0cc566c0c632e7b0968295e8b1b02d339120d4c", + "pid": 90662, + "state": "running", + "started_at": "2026-11-05T21:29:38.996361992Z", + "status": 200, + "instance_id": "c04ec178adf04b37b77b8e5e0a2111ed" + }, + { + "service": "api-b", + "container_id": "fc5965f79bed2c38499dbb7aad7f3116a6b83fa8a906eb92f772520233308190", + "pid": 90656, + "state": "running", + "started_at": "2026-11-05T21:29:38.991817395Z", + "status": 200, + "instance_id": "663168b0dbf54bda9fd588b8c699678d" + } +] diff --git a/evidence/lab4/cluster/11_nginx.txt b/evidence/lab4/cluster/11_nginx.txt new file mode 100644 index 00000000..311fe944 --- /dev/null +++ b/evidence/lab4/cluster/11_nginx.txt @@ -0,0 +1,76 @@ +$ 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; + # The API enforces the exact 10 MiB file limit while streaming. + client_max_body_size 11m; + client_header_timeout 5s; + client_body_timeout 10s; + keepalive_timeout 2s; + error_page 413 = @payload_too_large; + error_page 502 504 = @unavailable; + location @unavailable { + default_type application/json; + add_header X-Request-Id $request_id always; + return 503 '{"error":{"code":"unavailable","message":"Service temporarily unavailable","request_id":"$request_id"}}'; + } + location @payload_too_large { + default_type application/json; + add_header X-Request-Id $request_id always; + return 413 '{"error":{"code":"payload_too_large","message":"Request body too large","request_id":"$request_id"}}'; + } + location ~ ^/api/v1/chats/[^/]+/attachments$ { + 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 75s; + proxy_send_timeout 75s; + proxy_request_buffering off; + proxy_buffering off; + # A binary POST has no idempotency key and must not be replayed. + proxy_next_upstream off; + } + 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_request_buffering off; + proxy_buffering off; + 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.469 diff --git a/evidence/lab4/cluster/12_student_tests.txt b/evidence/lab4/cluster/12_student_tests.txt new file mode 100644 index 00000000..1bcfbf4f --- /dev/null +++ b/evidence/lab4/cluster/12_student_tests.txt @@ -0,0 +1,456 @@ +$ docker compose --env-file .env --profile test run --rm --build tests +#0 building with "desktop-linux" instance using docker driver + +#1 [minio internal] load build definition from Dockerfile +#1 transferring dockerfile: 1.79kB 0.0s done +#1 DONE 0.0s + +#2 [minio internal] load metadata for docker.io/library/python:3.12.8-slim-bookworm +#2 DONE 1.2s + +#3 [minio internal] load metadata for docker.io/library/golang:1.24.8-bookworm +#3 DONE 1.3s + +#4 [minio internal] load .dockerignore +#4 transferring context: 196B done +#4 DONE 0.0s + +#5 [minio go-build 1/1] FROM docker.io/library/golang:1.24.8-bookworm@sha256:4ed690d6649d63c312b99a6120025ec79ce3b542968a37da53d6236c7c61a848 +#5 DONE 0.0s + +#6 [minio server 1/4] FROM docker.io/library/python:3.12.8-slim-bookworm@sha256:2199a62885a12290dc9c5be3ca0681d367576ab7bf037da120e564723292a2f0 +#6 DONE 0.0s + +#7 [minio internal] load build context +#7 transferring context: 420B 0.0s done +#7 DONE 0.0s + +#8 [minio minio-build 1/1] RUN go install -p 2 -trimpath -ldflags="-s -w -X github.com/minio/minio/cmd.Version=2025-10-15T17:29:55Z -X github.com/minio/minio/cmd.ReleaseTag=RELEASE.2025-10-15T17-29-55Z" github.com/minio/minio@RELEASE.2025-10-15T17-29-55Z +#8 CACHED + +#9 [minio server 2/4] COPY --from=minio-build /out/minio /usr/local/bin/minio +#9 CACHED + +#10 [minio server 3/4] COPY infra/minio/healthcheck.py /opt/minio/healthcheck.py +#10 CACHED + +#11 [minio server 4/4] RUN mkdir /data && chown 10001:10001 /data +#11 CACHED + +#12 [minio] exporting to image +#12 exporting layers done +#12 writing image sha256:1a1494b9ad336bea42711376d400cb56f503ec86ad5a0c0442a3e86dea560afd done +#12 naming to docker.io/library/mtusi-minio:RELEASE.2025-10-15T17-29-55Z done +#12 DONE 0.0s + +#13 [minio-init internal] load build definition from Dockerfile +#13 transferring dockerfile: 1.79kB done +#13 DONE 0.0s + +#3 [minio-init internal] load metadata for docker.io/library/golang:1.24.8-bookworm +#3 ... + +#2 [minio-init internal] load metadata for docker.io/library/python:3.12.8-slim-bookworm +#2 DONE 1.5s + +#3 [minio-init internal] load metadata for docker.io/library/golang:1.24.8-bookworm +#3 DONE 1.7s + +#14 [minio-init internal] load .dockerignore +#14 transferring context: 196B done +#14 DONE 0.0s + +#5 [minio-init go-build 1/1] FROM docker.io/library/golang:1.24.8-bookworm@sha256:4ed690d6649d63c312b99a6120025ec79ce3b542968a37da53d6236c7c61a848 +#5 DONE 0.0s + +#6 [minio-init bootstrap 1/3] FROM docker.io/library/python:3.12.8-slim-bookworm@sha256:2199a62885a12290dc9c5be3ca0681d367576ab7bf037da120e564723292a2f0 +#6 DONE 0.0s + +#15 [minio-init internal] load build context +#15 transferring context: 3.45kB done +#15 DONE 0.0s + +#16 [minio-init mc-build 1/1] RUN go install -p 2 -trimpath -ldflags="-s -w -X github.com/minio/mc/cmd.Version=2025-08-13T08:35:41Z -X github.com/minio/mc/cmd.ReleaseTag=RELEASE.2025-08-13T08-35-41Z" github.com/minio/mc@RELEASE.2025-08-13T08-35-41Z +#16 CACHED + +#17 [minio-init bootstrap 2/3] COPY --from=mc-build /out/mc /usr/local/bin/mc +#17 CACHED + +#18 [minio-init bootstrap 3/3] COPY infra/minio/bootstrap.py /opt/minio/bootstrap.py +#18 CACHED + +#19 [minio-init] exporting to image +#19 exporting layers done +#19 writing image sha256:bf240f90f68d7541e416ff322fc356abd7796a95e6cafe1673df1cbf363698a5 done +#19 naming to docker.io/library/mtusi-minio-mc:RELEASE.2025-08-13T08-35-41Z done +#19 DONE 0.0s +#0 building with "desktop-linux" instance using docker driver + +#1 [tests internal] load build definition from Dockerfile +#1 transferring dockerfile: 1.04kB done +#1 DONE 0.0s + +#2 [tests internal] load metadata for docker.io/library/python:3.12.8-slim-bookworm +#2 DONE 0.3s + +#3 [tests internal] load .dockerignore +#3 transferring context: 196B done +#3 DONE 0.0s + +#4 [tests base 1/6] FROM docker.io/library/python:3.12.8-slim-bookworm@sha256:2199a62885a12290dc9c5be3ca0681d367576ab7bf037da120e564723292a2f0 +#4 DONE 0.0s + +#5 [tests internal] load build context +#5 transferring context: 645.96kB 0.1s done +#5 DONE 0.1s + +#6 [tests base 5/6] RUN pip install -r requirements.txt +#6 CACHED + +#7 [tests test 2/9] RUN pip install -r requirements-test.txt +#7 CACHED + +#8 [tests base 6/6] COPY app ./app +#8 CACHED + +#9 [tests test 3/9] COPY pytest.ini ./ +#9 CACHED + +#10 [tests test 4/9] COPY student-tests ./student-tests +#10 CACHED + +#11 [tests base 3/6] WORKDIR /srv +#11 CACHED + +#12 [tests base 4/6] COPY requirements.txt ./ +#12 CACHED + +#13 [tests base 2/6] RUN groupadd --system --gid 10001 app && useradd --system --uid 10001 --gid app --no-create-home --shell /usr/sbin/nologin app +#13 CACHED + +#14 [tests test 1/9] COPY requirements-test.txt ./ +#14 CACHED + +#15 [tests test 5/9] COPY unit-tests ./unit-tests +#15 CACHED + +#16 [tests test 6/9] COPY scripts ./scripts +#16 DONE 0.0s + +#17 [tests test 7/9] COPY tests ./tests +#17 DONE 0.1s + +#18 [tests test 8/9] COPY lab.json ./ +#18 DONE 0.1s + +#19 [tests test 9/9] COPY contracts ./contracts +#19 DONE 0.0s + +#20 [tests] exporting to image +#20 exporting layers +#20 exporting layers 0.1s done +#20 writing image sha256:b0bdbf812b02e11b2c808878a3602d455fe28ff3ae11fed5fdf3ba2364d33d90 done +#20 naming to docker.io/library/mtusi-messenger:lab4-test done +#20 DONE 0.1s +============================= test session starts ============================== +collecting ... collected 279 items + +student-tests/test_attachment_failures.py::test_s3_failure_leaves_accounted_intent_and_cleanup_retries[False] PASSED [ 0%] +student-tests/test_attachment_failures.py::test_s3_failure_leaves_accounted_intent_and_cleanup_retries[True] PASSED [ 0%] +student-tests/test_attachment_failures.py::test_database_failure_after_put_is_compensated PASSED [ 1%] +student-tests/test_attachment_failures.py::test_ambiguous_successful_commit_never_deletes_ready_file PASSED [ 1%] +student-tests/test_attachment_failures.py::test_cleanup_never_removes_old_ready_attachment PASSED [ 1%] +student-tests/test_attachment_failures.py::test_interrupted_body_creates_neither_metadata_nor_object PASSED [ 2%] +student-tests/test_attachments.py::test_original_metadata_and_bytes_for_another_member[PNG-image/png-\u0441\u043d\u0438\u043c\u043e\u043a.png] PASSED [ 2%] +student-tests/test_attachments.py::test_original_metadata_and_bytes_for_another_member[JPEG-image/jpeg-photo.jpeg] PASSED [ 2%] +student-tests/test_attachments.py::test_filename_is_not_an_object_identifier PASSED [ 3%] +student-tests/test_attachments.py::test_outsider_cannot_upload_read_or_reference_an_attachment PASSED [ 3%] +student-tests/test_attachments.py::test_attachment_cannot_move_between_chats_even_for_same_member PASSED [ 3%] +student-tests/test_attachments.py::test_unknown_attachment_is_404_without_creating_message PASSED [ 4%] +student-tests/test_attachments.py::test_any_member_can_send_attachment_without_text[] PASSED [ 4%] +student-tests/test_attachments.py::test_any_member_can_send_attachment_without_text[ \t\n] PASSED [ 5%] +student-tests/test_attachments.py::test_replay_compares_attachments_and_preserves_order PASSED [ 5%] +student-tests/test_attachments.py::test_attachment_list_has_strict_types[None] PASSED [ 5%] +student-tests/test_attachments.py::test_attachment_list_has_strict_types[not-a-list] PASSED [ 6%] +student-tests/test_attachments.py::test_attachment_list_has_strict_types[values2] PASSED [ 6%] +student-tests/test_attachments.py::test_attachment_list_has_strict_types[values3] PASSED [ 6%] +student-tests/test_attachments.py::test_duplicate_and_excessive_attachment_ids_are_400 PASSED [ 7%] +student-tests/test_attachments.py::test_dangerous_or_invalid_filename_is_400[] PASSED [ 7%] +student-tests/test_attachments.py::test_dangerous_or_invalid_filename_is_400[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa] PASSED [ 7%] +student-tests/test_attachments.py::test_dangerous_or_invalid_filename_is_400[../photo.png] PASSED [ 8%] +student-tests/test_attachments.py::test_dangerous_or_invalid_filename_is_400[folder/photo.png] PASSED [ 8%] +student-tests/test_attachments.py::test_dangerous_or_invalid_filename_is_400[folder\\photo.png] PASSED [ 8%] +student-tests/test_attachments.py::test_dangerous_or_invalid_filename_is_400[bad\x00.png] PASSED [ 9%] +student-tests/test_attachments.py::test_dangerous_or_invalid_filename_is_400[bad\r\n.png] PASSED [ 9%] +student-tests/test_attachments.py::test_dangerous_or_invalid_filename_is_400[bad\t.png] PASSED [ 10%] +student-tests/test_attachments.py::test_dangerous_or_invalid_filename_is_400[bad\x7f.png] PASSED [ 10%] +student-tests/test_attachments.py::test_dangerous_or_invalid_filename_is_400[bad\x85.png] PASSED [ 10%] +student-tests/test_attachments.py::test_missing_filename_is_400 PASSED [ 11%] +student-tests/test_attachments.py::test_invalid_image_is_415_not_500[-image/png] PASSED [ 11%] +student-tests/test_attachments.py::test_invalid_image_is_415_not_500[not an image-image/png] PASSED [ 11%] +student-tests/test_attachments.py::test_invalid_image_is_415_not_500[GIF89a\x01\x00\x01\x00-image/png] PASSED [ 12%] +student-tests/test_attachments.py::test_invalid_image_is_415_not_500[-image/svg+xml] PASSED [ 12%] +student-tests/test_attachments.py::test_declared_mime_must_match_real_image[PNG-image/jpeg] PASSED [ 12%] +student-tests/test_attachments.py::test_declared_mime_must_match_real_image[JPEG-image/png] PASSED [ 13%] +student-tests/test_attachments.py::test_declared_mime_must_match_real_image[PNG-application/octet-stream] PASSED [ 13%] +student-tests/test_attachments.py::test_truncated_image_is_rejected[PNG-image/png] PASSED [ 13%] +student-tests/test_attachments.py::test_truncated_image_is_rejected[JPEG-image/jpeg] PASSED [ 14%] +student-tests/test_attachments.py::test_png_with_corrupted_pixel_stream_is_rejected PASSED [ 14%] +student-tests/test_attachments.py::test_byte_limit_with_content_length PASSED [ 15%] +student-tests/test_attachments.py::test_byte_limit_without_content_length PASSED [ 15%] +student-tests/test_attachments.py::test_image_exactly_at_byte_limit_is_accepted PASSED [ 15%] +student-tests/test_attachments.py::test_pixel_limit_is_checked_before_decoding PASSED [ 16%] +student-tests/test_attachments.py::test_missing_variant_is_404 PASSED [ 16%] +student-tests/test_auth.py::test_register_returns_user_without_secrets PASSED [ 16%] +student-tests/test_auth.py::test_register_password_rules[None] PASSED [ 17%] +student-tests/test_auth.py::test_register_password_rules[] PASSED [ 17%] +student-tests/test_auth.py::test_register_password_rules[short] PASSED [ 17%] +student-tests/test_auth.py::test_register_password_rules[aaaaaaaaaaa] PASSED [ 18%] +student-tests/test_auth.py::test_register_password_rules[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa] PASSED [ 18%] +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 [ 19%] +student-tests/test_auth.py::test_register_password_border_lengths PASSED [ 19%] +student-tests/test_auth.py::test_login_success_shape PASSED [ 20%] +student-tests/test_auth.py::test_each_login_gives_new_token PASSED [ 20%] +student-tests/test_auth.py::test_wrong_password_and_unknown_user_look_the_same PASSED [ 20%] +student-tests/test_auth.py::test_malformed_basic_is_401[Basic] PASSED [ 21%] +student-tests/test_auth.py::test_malformed_basic_is_401[Basic !!!not-base64!!!] PASSED [ 21%] +student-tests/test_auth.py::test_malformed_basic_is_401[Basic bm9jb2xvbg==] PASSED [ 21%] +student-tests/test_auth.py::test_malformed_basic_is_401[Basic 0LjQvNGPOnBhc3N3b3JkLTEyMzQ1Ng==] PASSED [ 22%] +student-tests/test_auth.py::test_malformed_basic_is_401[Basic dXNlcjr//i1iYWQtdXRmOA==] PASSED [ 22%] +student-tests/test_auth.py::test_malformed_basic_is_401[Bearer abc] PASSED [ 22%] +student-tests/test_auth.py::test_malformed_basic_is_401[] PASSED [ 23%] +student-tests/test_auth.py::test_login_without_header_is_401 PASSED [ 23%] +student-tests/test_auth.py::test_password_with_colon_and_unicode_works PASSED [ 24%] +student-tests/test_auth.py::test_bearer_token_gives_access PASSED [ 24%] +student-tests/test_auth.py::test_business_endpoint_rejects_basic PASSED [ 24%] +student-tests/test_auth.py::test_bad_or_unknown_bearer_is_401[] PASSED [ 25%] +student-tests/test_auth.py::test_bad_or_unknown_bearer_is_401[Bearer] PASSED [ 25%] +student-tests/test_auth.py::test_bad_or_unknown_bearer_is_401[Bearer short] PASSED [ 25%] +student-tests/test_auth.py::test_bad_or_unknown_bearer_is_401[Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx] PASSED [ 26%] +student-tests/test_auth.py::test_bad_or_unknown_bearer_is_401[Token abc] PASSED [ 26%] +student-tests/test_auth.py::test_bad_or_unknown_bearer_is_401[bearer] PASSED [ 26%] +student-tests/test_auth.py::test_x_user_id_alone_gives_no_access PASSED [ 27%] +student-tests/test_auth.py::test_x_user_id_does_not_change_owner_of_session PASSED [ 27%] +student-tests/test_auth.py::test_logout_revokes_token_and_repeat_is_401 PASSED [ 27%] +student-tests/test_auth.py::test_logout_without_token_is_401 PASSED [ 28%] +student-tests/test_auth.py::test_logout_revokes_only_current_session PASSED [ 28%] +student-tests/test_auth.py::test_session_lives_longer_than_300_seconds PASSED [ 29%] +student-tests/test_cache.py::test_user_profile_is_served_from_cache_with_bounded_ttl PASSED [ 29%] +student-tests/test_cache.py::test_cache_never_stores_raw_tokens_or_passwords PASSED [ 29%] +student-tests/test_cache.py::test_logout_revokes_cached_session PASSED [ 30%] +student-tests/test_cache.py::test_late_cache_fill_cannot_resurrect_revoked_session PASSED [ 30%] +student-tests/test_cache.py::test_flushed_cache_loses_nothing PASSED [ 30%] +student-tests/test_cache.py::test_cache_does_not_extend_short_session PASSED [ 31%] +student-tests/test_cache.py::test_staleness_after_out_of_band_delete_is_bounded_by_cache_ttl PASSED [ 31%] +student-tests/test_cache.py::test_redis_down_requests_bypass_cache_and_stay_fast PASSED [ 31%] +student-tests/test_cache.py::test_redis_down_never_trusts_anonymous_or_unknown_tokens PASSED [ 32%] +student-tests/test_cache.py::test_revocation_lost_while_redis_down_is_flushed_on_recovery PASSED [ 32%] +student-tests/test_cache.py::test_without_cache_url_cache_is_off PASSED [ 32%] +student-tests/test_cache.py::test_lost_revocation_survives_api_restart[False] PASSED [ 33%] +student-tests/test_cache.py::test_lost_revocation_survives_api_restart[True] PASSED [ 33%] +student-tests/test_cache.py::test_cached_session_deleted_in_database_is_rejected_immediately PASSED [ 34%] +student-tests/test_concurrency.py::test_100_messages_from_10_clients_in_parallel PASSED [ 34%] +student-tests/test_concurrency.py::test_parallel_chat_creation_and_reading PASSED [ 34%] +student-tests/test_contract_basics.py::test_health_endpoints PASSED [ 35%] +student-tests/test_contract_basics.py::test_every_response_has_unique_request_id PASSED [ 35%] +student-tests/test_contract_basics.py::test_unknown_path_is_404_in_common_format PASSED [ 35%] +student-tests/test_contract_basics.py::test_trailing_slash_is_not_redirected PASSED [ 36%] +student-tests/test_contract_basics.py::test_wrong_method_is_405_in_common_format PASSED [ 36%] +student-tests/test_contract_basics.py::test_two_users_one_chat_two_messages PASSED [ 36%] +student-tests/test_contract_basics.py::test_chat_response_shape PASSED [ 37%] +student-tests/test_contract_basics.py::test_unexpected_exception_gives_500_without_details PASSED [ 37%] +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 [ 38%] +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 [ 39%] +student-tests/test_database.py::test_expected_tables_and_indexes_exist PASSED [ 40%] +student-tests/test_database.py::test_primary_and_foreign_keys_are_declared PASSED [ 40%] +student-tests/test_database.py::test_database_itself_rejects_bad_rows PASSED [ 40%] +student-tests/test_database.py::test_chat_creation_is_atomic PASSED [ 41%] +student-tests/test_database.py::test_successful_chat_has_all_members_in_database PASSED [ 41%] +student-tests/test_db_failure.py::test_database_down_gives_503_and_never_trusts_anyone PASSED [ 41%] +student-tests/test_db_failure.py::test_database_down_with_cache_up_still_needs_database_for_data PASSED [ 42%] +student-tests/test_duplicate_username.py::test_second_registration_with_same_username_is_409 PASSED [ 42%] +student-tests/test_duplicate_username.py::test_duplicate_does_not_replace_original_user PASSED [ 43%] +student-tests/test_duplicate_username.py::test_username_is_case_sensitive_lowercase_only PASSED [ 43%] +student-tests/test_duplicate_username.py::test_parallel_registration_of_same_username_creates_exactly_one PASSED [ 43%] +student-tests/test_empty_message.py::test_blank_text_is_rejected[] PASSED [ 44%] +student-tests/test_empty_message.py::test_blank_text_is_rejected[ ] PASSED [ 44%] +student-tests/test_empty_message.py::test_blank_text_is_rejected[ ] PASSED [ 44%] +student-tests/test_empty_message.py::test_blank_text_is_rejected[\n] PASSED [ 45%] +student-tests/test_empty_message.py::test_blank_text_is_rejected[\t \n] PASSED [ 45%] +student-tests/test_empty_message.py::test_blank_text_is_rejected[\xa0] PASSED [ 45%] +student-tests/test_empty_message.py::test_blank_text_is_rejected[\u2003] PASSED [ 46%] +student-tests/test_empty_message.py::test_missing_or_wrong_type_text_is_rejected[body0] PASSED [ 46%] +student-tests/test_empty_message.py::test_missing_or_wrong_type_text_is_rejected[body1] PASSED [ 46%] +student-tests/test_empty_message.py::test_missing_or_wrong_type_text_is_rejected[body2] PASSED [ 47%] +student-tests/test_empty_message.py::test_missing_or_wrong_type_text_is_rejected[body3] PASSED [ 47%] +student-tests/test_empty_message.py::test_missing_or_wrong_type_text_is_rejected[body4] PASSED [ 48%] +student-tests/test_empty_message.py::test_text_length_is_counted_in_characters PASSED [ 48%] +student-tests/test_empty_message.py::test_nul_character_is_400_not_500 PASSED [ 48%] +student-tests/test_empty_message.py::test_unicode_survives_the_database PASSED [ 49%] +student-tests/test_empty_message.py::test_message_has_correct_fields PASSED [ 49%] +student-tests/test_explain.py::test_history_page_uses_index_without_full_scan[0] PASSED [ 49%] +student-tests/test_explain.py::test_history_page_uses_index_without_full_scan[9000] PASSED [ 50%] +student-tests/test_foreign_chat.py::test_outsider_cannot_read_chat PASSED [ 50%] +student-tests/test_foreign_chat.py::test_outsider_cannot_read_history PASSED [ 50%] +student-tests/test_foreign_chat.py::test_outsider_cannot_write PASSED [ 51%] +student-tests/test_foreign_chat.py::test_outsider_gets_404_even_with_bad_body PASSED [ 51%] +student-tests/test_foreign_chat.py::test_unknown_chat_looks_the_same_as_foreign PASSED [ 51%] +student-tests/test_foreign_chat.py::test_chat_list_shows_only_own_chats PASSED [ 52%] +student-tests/test_foreign_chat.py::test_bad_chat_id_is_400 PASSED [ 52%] +student-tests/test_foreign_chat.py::test_no_token_is_401_everywhere PASSED [ 53%] +student-tests/test_foreign_chat.py::test_401_comes_before_404 PASSED [ 53%] +student-tests/test_foreign_chat.py::test_member_can_read_and_write PASSED [ 53%] +student-tests/test_idempotency.py::test_key_is_required_and_validated[None] PASSED [ 54%] +student-tests/test_idempotency.py::test_key_is_required_and_validated[] PASSED [ 54%] +student-tests/test_idempotency.py::test_key_is_required_and_validated[a b] PASSED [ 54%] +student-tests/test_idempotency.py::test_key_is_required_and_validated[a?b] PASSED [ 55%] +student-tests/test_idempotency.py::test_key_is_required_and_validated[xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx] PASSED [ 55%] +student-tests/test_idempotency.py::test_key_is_required_and_validated[a,b] PASSED [ 55%] +student-tests/test_idempotency.py::test_key_is_required_and_validated[a/b] PASSED [ 56%] +student-tests/test_idempotency.py::test_duplicate_headers_are_invalid PASSED [ 56%] +student-tests/test_idempotency.py::test_replay_and_conflict_preserve_original_result PASSED [ 56%] +student-tests/test_idempotency.py::test_parallel_same_key_across_two_applications PASSED [ 57%] +student-tests/test_idempotency.py::test_key_scope_includes_user_and_chat PASSED [ 57%] +student-tests/test_idempotency.py::test_expired_result_allows_new_message PASSED [ 58%] +student-tests/test_idempotency.py::test_failure_between_message_and_result_rolls_back PASSED [ 58%] +student-tests/test_idempotency.py::test_committed_but_lost_response_is_safe_to_retry PASSED [ 58%] +student-tests/test_idempotency.py::test_replay_rechecks_session_and_chat_access PASSED [ 59%] +student-tests/test_invalid_json.py::test_registration_rejects_broken_json[{] PASSED [ 59%] +student-tests/test_invalid_json.py::test_registration_rejects_broken_json[not json at all] PASSED [ 59%] +student-tests/test_invalid_json.py::test_registration_rejects_broken_json[] PASSED [ 60%] +student-tests/test_invalid_json.py::test_registration_rejects_broken_json[{"username": "abc",}] PASSED [ 60%] +student-tests/test_invalid_json.py::test_registration_rejects_broken_json[\xff\xfe\x00] PASSED [ 60%] +student-tests/test_invalid_json.py::test_registration_rejects_broken_json[{"username": NaN}] PASSED [ 61%] +student-tests/test_invalid_json.py::test_registration_rejects_broken_json[["username"]] PASSED [ 61%] +student-tests/test_invalid_json.py::test_registration_rejects_broken_json["text"] PASSED [ 62%] +student-tests/test_invalid_json.py::test_registration_rejects_broken_json[null] PASSED [ 62%] +student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes0] PASSED [ 62%] +student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes1] PASSED [ 63%] +student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes2] PASSED [ 63%] +student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes3] PASSED [ 63%] +student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes4] PASSED [ 64%] +student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes5] PASSED [ 64%] +student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes6] PASSED [ 64%] +student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes7] PASSED [ 65%] +student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes8] PASSED [ 65%] +student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes9] PASSED [ 65%] +student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes10] PASSED [ 66%] +student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes11] PASSED [ 66%] +student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes12] PASSED [ 67%] +student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes13] PASSED [ 67%] +student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes14] PASSED [ 67%] +student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes15] PASSED [ 68%] +student-tests/test_invalid_json.py::test_registration_rejects_bad_fields[changes16] PASSED [ 68%] +student-tests/test_invalid_json.py::test_border_values_are_accepted PASSED [ 68%] +student-tests/test_invalid_json.py::test_lone_surrogate_is_400_not_500 PASSED [ 69%] +student-tests/test_invalid_json.py::test_wrong_or_missing_content_type_is_415 PASSED [ 69%] +student-tests/test_invalid_json.py::test_huge_body_is_413 PASSED [ 69%] +student-tests/test_invalid_json.py::test_broken_json_in_message_and_chat PASSED [ 70%] +student-tests/test_invalid_json.py::test_sender_id_in_message_is_rejected PASSED [ 70%] +student-tests/test_invalid_json.py::test_chat_body_validation PASSED [ 70%] +student-tests/test_pagination.py::test_walk_25_messages_by_7 PASSED [ 71%] +student-tests/test_pagination.py::test_exact_multiple_has_no_empty_extra_page PASSED [ 71%] +student-tests/test_pagination.py::test_limit_one_walks_everything PASSED [ 72%] +student-tests/test_pagination.py::test_default_limit_is_50_and_max_is_100 PASSED [ 72%] +student-tests/test_pagination.py::test_empty_history PASSED [ 72%] +student-tests/test_pagination.py::test_limit_can_change_between_pages PASSED [ 73%] +student-tests/test_pagination.py::test_messages_sent_during_walk_appear_at_the_end PASSED [ 73%] +student-tests/test_pagination.py::test_created_at_is_increasing_and_utc PASSED [ 73%] +student-tests/test_pagination.py::test_bad_limit_values PASSED [ 74%] +student-tests/test_pagination.py::test_garbage_cursor_is_400 PASSED [ 74%] +student-tests/test_pagination.py::test_cursor_from_another_chat_is_400 PASSED [ 74%] +student-tests/test_pagination.py::test_chat_list_pagination PASSED [ 75%] +student-tests/test_pagination.py::test_chat_list_cursor_cannot_be_used_for_messages 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-1000000000000000000000000000000] PASSED [ 76%] +student-tests/test_pagination.py::test_invalid_cursor_timestamp_is_400[messages-253402300800000000] PASSED [ 76%] +student-tests/test_pagination.py::test_invalid_cursor_timestamp_is_400[messages-True] PASSED [ 77%] +student-tests/test_pagination.py::test_invalid_cursor_timestamp_is_400[messages-1.5] PASSED [ 77%] +student-tests/test_pagination.py::test_invalid_cursor_timestamp_is_400[messages-1] PASSED [ 77%] +student-tests/test_pagination.py::test_invalid_cursor_timestamp_is_400[messages-None] 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-1000000000000000000000000000000] PASSED [ 78%] +student-tests/test_pagination.py::test_invalid_cursor_timestamp_is_400[chats-253402300800000000] PASSED [ 79%] +student-tests/test_pagination.py::test_invalid_cursor_timestamp_is_400[chats-True] PASSED [ 79%] +student-tests/test_pagination.py::test_invalid_cursor_timestamp_is_400[chats-1.5] PASSED [ 79%] +student-tests/test_pagination.py::test_invalid_cursor_timestamp_is_400[chats-1] PASSED [ 80%] +student-tests/test_pagination.py::test_invalid_cursor_timestamp_is_400[chats-None] PASSED [ 80%] +student-tests/test_pagination.py::test_cursor_timestamp_boundaries_are_valid[messages-0] PASSED [ 81%] +student-tests/test_pagination.py::test_cursor_timestamp_boundaries_are_valid[messages-253402300799999999] PASSED [ 81%] +student-tests/test_pagination.py::test_cursor_timestamp_boundaries_are_valid[chats-0] PASSED [ 81%] +student-tests/test_pagination.py::test_cursor_timestamp_boundaries_are_valid[chats-253402300799999999] PASSED [ 82%] +student-tests/test_session_expiry.py::test_session_expires_after_ttl PASSED [ 82%] +student-tests/test_session_expiry.py::test_expired_sessions_are_cleaned_on_next_login PASSED [ 82%] +student-tests/test_session_expiry.py::test_short_ttl_from_environment_variable PASSED [ 83%] +student-tests/test_transactions.py::test_failure_after_chat_insert_rolls_back_whole_operation PASSED [ 83%] +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 [ 84%] +unit-tests/test_attachment_intent.py::test_removed_intent_prevents_put_when_uploader_resumes PASSED [ 84%] +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 [ 85%] +unit-tests/test_message_transaction.py::test_replay_returns_original_message_and_releases_transaction PASSED [ 85%] +unit-tests/test_message_transaction.py::test_failed_commit_rolls_back_and_propagates_error PASSED [ 86%] +unit-tests/test_mr_regressions.py::test_stale_positive_cache_never_authorizes_revoked_token[/api/v1/users/me-GET] PASSED [ 86%] +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 [ 87%] +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 [ 89%] +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 [ 90%] +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 [ 91%] +unit-tests/test_mr_regressions.py::test_cursor_timestamp_is_rejected_through_http[chats-True] PASSED [ 91%] +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 [ 92%] +unit-tests/test_mr_regressions.py::test_cursor_timestamp_is_rejected_through_http[chats-None] PASSED [ 92%] +unit-tests/test_mr_regressions.py::test_timestamp_boundaries_roundtrip[0] PASSED [ 93%] +unit-tests/test_mr_regressions.py::test_timestamp_boundaries_roundtrip[253402300799999999] PASSED [ 93%] +unit-tests/test_mr_regressions.py::test_message_key_validation[None] PASSED [ 93%] +unit-tests/test_mr_regressions.py::test_message_key_validation[] PASSED [ 94%] +unit-tests/test_mr_regressions.py::test_message_key_validation[bad key] PASSED [ 94%] +unit-tests/test_mr_regressions.py::test_message_key_validation[xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx] PASSED [ 94%] +unit-tests/test_mr_regressions.py::test_message_key_validation[a?b] PASSED [ 95%] +unit-tests/test_mr_regressions.py::test_message_key_validation[a,b] PASSED [ 95%] +unit-tests/test_mr_regressions.py::test_message_normalization_and_conflict PASSED [ 96%] +unit-tests/test_mr_regressions.py::test_instance_header_matches_process_on_all_responses[/health/live] PASSED [ 96%] +unit-tests/test_mr_regressions.py::test_instance_header_matches_process_on_all_responses[/missing] PASSED [ 96%] +unit-tests/test_mr_regressions.py::test_instance_header_matches_process_on_all_responses[/api/v1/users/me] PASSED [ 97%] +unit-tests/test_mr_regressions.py::test_instance_alias_changes_between_applications PASSED [ 97%] +unit-tests/test_mr_regressions.py::test_sigterm_drops_readiness_and_requests_graceful_exit PASSED [ 97%] +unit-tests/test_operator_scripts.py::BalancedReplicasTests::test_old_process_alias_does_not_replace_current_replica PASSED [ 98%] +unit-tests/test_operator_scripts.py::BalancedReplicasTests::test_one_ready_replica_never_counts_as_balanced PASSED [ 98%] +unit-tests/test_operator_scripts.py::BalancedReplicasTests::test_waits_until_both_replicas_answer_through_lb PASSED [ 98%] +unit-tests/test_operator_scripts.py::HookCommandTests::test_missing_shell_reports_failure PASSED [ 99%] +unit-tests/test_operator_scripts.py::HookCommandTests::test_python_hook_uses_current_interpreter_and_preserves_spaces PASSED [ 99%] +unit-tests/test_operator_scripts.py::HookCommandTests::test_shell_is_resolved_from_path 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 +================== 279 passed, 1 warning in 72.13s (0:01:12) =================== +2026/11/06 00:29:56 http2: server: error reading preface from client //./pipe/dockerDesktopLinuxEngine: file has already been closed +2026/11/06 00:29:59 http2: server: error reading preface from client //./pipe/dockerDesktopLinuxEngine: file has already been closed + Container mtusi-messenger-lab4-redis-1 Running + Container mtusi-messenger-lab4-db-1 Running + Container mtusi-messenger-lab4-minio-1 Running + Container mtusi-messenger-lab4-minio-init-1 Created + Container mtusi-messenger-lab4-minio-1 Waiting + Container mtusi-messenger-lab4-minio-1 Healthy + Container mtusi-messenger-lab4-minio-init-1 Starting + Container mtusi-messenger-lab4-minio-init-1 Started + +exit_code=0; elapsed_seconds=83.828 diff --git a/evidence/lab4/cluster/result.json b/evidence/lab4/cluster/result.json new file mode 100644 index 00000000..20f7b8f6 --- /dev/null +++ b/evidence/lab4/cluster/result.json @@ -0,0 +1,4 @@ +{ + "status": "PASS", + "at_utc": "2026-11-05T21:31:20.195562+00:00" +} diff --git a/evidence/lab4/result.json b/evidence/lab4/result.json new file mode 100644 index 00000000..520fce62 --- /dev/null +++ b/evidence/lab4/result.json @@ -0,0 +1,6 @@ +{ + "status": "PASS", + "at_utc": "2026-11-05T21:34:46.310736+00:00", + "cluster_checks": "PASS", + "student_tests": "PASS" +} diff --git a/infra/README.md b/infra/README.md index 5a39e862..668bb634 100644 --- a/infra/README.md +++ b/infra/README.md @@ -1,7 +1,7 @@ -# Инфраструктура студента +# Инфраструктура лабораторной 4 -Здесь разместите конфигурации выбранных сервисов; основную топологию — в корневом compose.yaml/stack.yaml. `compose.example.yaml` — только worksheet ролей и намеренно не запускается. `contracts/infrastructure.json` задаёт минимальные роли, не готовые образы/пароли. +Compose объединяет Nginx, api-a/api-b, PostgreSQL, Redis, MinIO, однократные migrate/minio-init и периодический cleanup. Тома pgdata и miniodata сохраняются при restart. Исходники MinIO и mc закреплены в infra/minio/Dockerfile, зависимости Python — в requirements.txt. -В REPORT.md сопоставьте роль → service name → версия/digest → health/readiness → volume → сеть/порт → способ передачи credentials. Приведите bootstrap (schema, bucket, broker topology, а с lab6 provisioning) и безопасное повторение этих команд. +Bootstrap идемпотентно создаёт приватный bucket и ограниченную учётную запись. API разрешены Get/Put/Delete только originals/* и ListBucket/GetBucketLocation только выбранного bucket. Консоль и внутренние службы не публикуют host-порты. -Приложение строится из исходников. Не используйте заранее существующий образ преподавательского сервера вместо своей реализации. Restart hook перезапускает все соответствующие stores с volumes; failover hook останавливает только заданную API-реплику. +Команды Windows/Linux: [GUIDE.md](../GUIDE.md). Контрактные роли из чистого skeleton сохранены в contracts/infrastructure.json; фактический стенд задаётся compose.yaml. diff --git a/infra/minio/Dockerfile b/infra/minio/Dockerfile new file mode 100644 index 00000000..eb6c2bea --- /dev/null +++ b/infra/minio/Dockerfile @@ -0,0 +1,35 @@ +# MinIO distributes its security release as source only. Compile the official, +# pinned releases; no dependency on a removed Docker Hub / Quay image or make. +FROM golang:1.24.8-bookworm AS go-build +ENV CGO_ENABLED=0 GOTOOLCHAIN=local GOBIN=/out GOMAXPROCS=2 + +FROM go-build AS minio-build +RUN go install -p 2 -trimpath \ + -ldflags="-s -w -X github.com/minio/minio/cmd.Version=2025-10-15T17:29:55Z -X github.com/minio/minio/cmd.ReleaseTag=RELEASE.2025-10-15T17-29-55Z" \ + github.com/minio/minio@RELEASE.2025-10-15T17-29-55Z + +FROM go-build AS mc-build +RUN go install -p 2 -trimpath \ + -ldflags="-s -w -X github.com/minio/mc/cmd.Version=2025-08-13T08:35:41Z -X github.com/minio/mc/cmd.ReleaseTag=RELEASE.2025-08-13T08-35-41Z" \ + github.com/minio/mc@RELEASE.2025-08-13T08-35-41Z + +FROM python:3.12.8-slim-bookworm AS server +LABEL org.opencontainers.image.source="https://github.com/minio/minio" \ + org.opencontainers.image.version="RELEASE.2025-10-15T17-29-55Z" +ENV PYTHONDONTWRITEBYTECODE=1 PYTHONUNBUFFERED=1 +COPY --from=minio-build /out/minio /usr/local/bin/minio +COPY infra/minio/healthcheck.py /opt/minio/healthcheck.py +RUN mkdir /data && chown 10001:10001 /data +USER 10001:10001 +EXPOSE 9000 +ENTRYPOINT ["minio"] +CMD ["server", "/data", "--address", ":9000", "--console-address", "127.0.0.1:9001", "--certs-dir", "/tmp/minio-certs"] + +FROM python:3.12.8-slim-bookworm AS bootstrap +LABEL org.opencontainers.image.source="https://github.com/minio/mc" \ + org.opencontainers.image.version="RELEASE.2025-08-13T08-35-41Z" +ENV PYTHONDONTWRITEBYTECODE=1 PYTHONUNBUFFERED=1 +COPY --from=mc-build /out/mc /usr/local/bin/mc +COPY infra/minio/bootstrap.py /opt/minio/bootstrap.py +USER 10001:10001 +ENTRYPOINT ["python", "/opt/minio/bootstrap.py"] diff --git a/infra/minio/bootstrap.py b/infra/minio/bootstrap.py new file mode 100644 index 00000000..6c84cbf9 --- /dev/null +++ b/infra/minio/bootstrap.py @@ -0,0 +1,77 @@ +"""Idempotent private bucket and least-privilege application account setup.""" + +import json +import os +from pathlib import Path +import re +import subprocess +import tempfile +from urllib.parse import quote, urlsplit + + +def main(): + bucket = os.environ["S3_BUCKET"] + endpoint = urlsplit(os.environ["S3_ENDPOINT_URL"]) + if not re.fullmatch(r"[a-z0-9][a-z0-9.-]{1,61}[a-z0-9]", bucket): + raise ValueError("Invalid S3_BUCKET") + if endpoint.scheme not in ("http", "https") or not endpoint.netloc: + raise ValueError("Invalid S3_ENDPOINT_URL") + root_user = os.environ["MINIO_ROOT_USER"] + root_password = os.environ["MINIO_ROOT_PASSWORD"] + access_key = os.environ["S3_ACCESS_KEY"] + secret_key = os.environ["S3_SECRET_KEY"] + if access_key == root_user or secret_key == root_password: + raise ValueError("API credentials must differ from MinIO root credentials") + if len(secret_key) < 32 or len(root_password) < 32: + raise ValueError("S3 secret and root password must contain at least 32 characters") + + mc_env = { + **os.environ, + "MC_HOST_store": "%s://%s:%s@%s" % ( + endpoint.scheme, quote(root_user, safe=""), + quote(root_password, safe=""), endpoint.netloc, + ), + } + secrets = (root_user, root_password, access_key, secret_key, mc_env["MC_HOST_store"]) + + with tempfile.TemporaryDirectory(prefix="minio-bootstrap-") as temporary: + def run_mc(step, *arguments, stdin=None): + result = subprocess.run( + ["mc", "--config-dir", temporary, *arguments], + input=stdin, env=mc_env, text=True, capture_output=True, timeout=30, + ) + if result.returncode: + detail = result.stderr + for value in secrets: + detail = detail.replace(value, "[redacted]") + raise RuntimeError("%s: %s" % (step, detail[:1000])) + + run_mc("create bucket", "mb", "--ignore-existing", "store/" + bucket) + run_mc("disable anonymous access", "anonymous", "set", "none", "store/" + bucket) + policy = { + "Version": "2012-10-17", + "Statement": [ + { + "Effect": "Allow", + "Action": ["s3:GetBucketLocation", "s3:ListBucket"], + "Resource": ["arn:aws:s3:::" + bucket], + }, + { + "Effect": "Allow", + "Action": ["s3:GetObject", "s3:PutObject", "s3:DeleteObject"], + "Resource": ["arn:aws:s3:::" + bucket + "/originals/*"], + }, + ], + } + path = Path(temporary) / "api-policy.json" + path.write_text(json.dumps(policy), encoding="utf-8") + run_mc("create policy", "admin", "policy", "create", "store", "messenger-api", str(path)) + # stdin avoids placing keys in process arguments. AddUser is an upsert. + run_mc("create API user", "admin", "user", "add", "store", stdin=access_key + "\n" + secret_key + "\n") + # mc treats an already attached policy as success. + run_mc("attach policy", "admin", "policy", "attach", "store", "messenger-api", "--user", access_key) + print("PASS: private bucket and separate restricted API account configured") + + +if __name__ == "__main__": + main() diff --git a/infra/minio/healthcheck.py b/infra/minio/healthcheck.py new file mode 100644 index 00000000..0fc11a81 --- /dev/null +++ b/infra/minio/healthcheck.py @@ -0,0 +1,11 @@ +"""Liveness of the private MinIO service; no credentials in process arguments.""" + +import sys +from urllib.request import urlopen + + +try: + with urlopen("http://127.0.0.1:9000/minio/health/live", timeout=2) as response: + sys.exit(0 if response.status == 200 else 1) +except OSError: + sys.exit(1) 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..9ca39cd6 --- /dev/null +++ b/infra/nginx/nginx.conf @@ -0,0 +1,62 @@ +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; + # The API enforces the exact 10 MiB file limit while streaming. + client_max_body_size 11m; + client_header_timeout 5s; + client_body_timeout 10s; + keepalive_timeout 2s; + error_page 413 = @payload_too_large; + error_page 502 504 = @unavailable; + location @unavailable { + default_type application/json; + add_header X-Request-Id $request_id always; + return 503 '{"error":{"code":"unavailable","message":"Service temporarily unavailable","request_id":"$request_id"}}'; + } + location @payload_too_large { + default_type application/json; + add_header X-Request-Id $request_id always; + return 413 '{"error":{"code":"payload_too_large","message":"Request body too large","request_id":"$request_id"}}'; + } + location ~ ^/api/v1/chats/[^/]+/attachments$ { + 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 75s; + proxy_send_timeout 75s; + proxy_request_buffering off; + proxy_buffering off; + # A binary POST has no idempotency key and must not be replayed. + proxy_next_upstream off; + } + 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_request_buffering off; + proxy_buffering off; + 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 e9ffc9f7..cc86272e 100644 --- a/lab.json +++ b/lab.json @@ -4,5 +4,5 @@ "contract_version": "1.4.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..e779fad9 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,31 @@ +# Зависимости 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 + +# Binary image validation and S3 client; checked with Python 3.12. +Pillow==13.0.0 +minio==7.2.20 +argon2-cffi==25.1.0 +argon2-cffi-bindings==26.1.0 +certifi==2026.10.19 +cffi==2.1.1 +pycparser==3.12 +pycryptodome==4.3.0 +urllib3==2.8.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_stack.py b/scripts/check_stack.py new file mode 100644 index 00000000..4751e292 --- /dev/null +++ b/scripts/check_stack.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python3 +"""Статические ограничения стенда lab4; не заменяет docker compose config/nginx -t.""" + +import ast +import json +import os +from pathlib import Path +import shutil +import subprocess +import yaml + +ROOT = Path(__file__).resolve().parents[1] + + +def main(): + config = yaml.safe_load((ROOT / "compose.yaml").read_text(encoding="utf-8")) + services = config["services"] + assert {"api-a", "api-b", "migrate", "nginx", "db", "redis", "minio", "minio-init", "cleanup"} <= 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 {"pgdata", "miniodata"} <= config["volumes"].keys() + assert services["minio"]["environment"]["MINIO_BROWSER"] == "off" + assert "latest" not in services["minio"]["image"] + assert "MINIO_ROOT_PASSWORD" not in services["api-a"]["environment"] + assert "S3_SECRET_KEY" in services["api-a"]["environment"] + assert services["cleanup"]["command"] == ["python", "-m", "app.cleanup", "--loop"] + assert not any("docker.sock" in str(s) for s in services.values()) + spec = json.loads((ROOT / "contracts/openapi.json").read_text(encoding="utf-8")) + 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(encoding="utf-8"), filename=str(path)) + shell = shutil.which("sh") + if shell: + for path in list((ROOT / "scripts").glob("*.sh")) + list((ROOT / "infra").rglob("*.sh")): + subprocess.run([shell, "-n", str(path)], check=True) + else: + print("SKIP: shell syntax (sh unavailable); container entrypoint requires runtime verification.") + for filename in ("scripts/stop_one.py", "scripts/restart.py"): + path = ROOT / filename + assert path.is_file() + if os.name != "nt": + assert path.stat().st_mode & 0o111 + print("PASS: lab4 static structure, resource/port isolation, contract, Python syntax; runtime NOT tested.") + + +if __name__ == "__main__": + main() diff --git a/scripts/cluster_evidence.py b/scripts/cluster_evidence.py new file mode 100644 index 00000000..6c95f01a --- /dev/null +++ b/scripts/cluster_evidence.py @@ -0,0 +1,428 @@ +#!/usr/bin/env python3 +"""Проверки кластера: останавливает и восстанавливает 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", encoding="utf-8" + ) + + +def record_command(out, name, command, env=None): + """Записать реальный вывод команды; не запрашивать полный inspect или env.""" + started = time.monotonic() + result = subprocess.run( + command, + cwd=ROOT, + text=True, + encoding="utf-8", + errors="replace", + 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", + encoding="utf-8", + ) + 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, + encoding="utf-8", + errors="replace", + capture_output=True, + timeout=60, + ) + 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/cluster") + 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.py", + "--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.py", + "--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/collect_evidence.sh b/scripts/collect_evidence.sh new file mode 100644 index 00000000..feb22761 --- /dev/null +++ b/scripts/collect_evidence.sh @@ -0,0 +1,4 @@ +#!/bin/sh +set -eu +cd "$(dirname "$0")/.." +exec python3 scripts/lab4_evidence.py "$@" diff --git a/scripts/compare_contracts.py b/scripts/compare_contracts.py new file mode 100644 index 00000000..72b8c885 --- /dev/null +++ b/scripts/compare_contracts.py @@ -0,0 +1,55 @@ +#!/usr/bin/env python3 +"""Сверить нормативные файлы с чистым clone без изменения эталона.""" + +import argparse +import ast +from datetime import datetime, timezone +import hashlib +import json +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] +FILES = ( + 'contracts/openapi.json', 'contracts/examples.json', 'contracts/infrastructure.json', + 'contracts/README.md', 'tests/client.py', 'tests/smoke.py', 'tests/load.py', + 'tests/image_probe.py', 'tests/fixtures/sample.png', +) + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--reference', type=Path, required=True) + parser.add_argument('--output', type=Path, default=ROOT / 'evidence/lab4/contract_comparison.json') + args = parser.parse_args() + rows = [] + for filename in FILES: + reference = (args.reference / filename).read_bytes() + actual = (ROOT / filename).read_bytes() + rows.append({'file': filename, 'identical': reference == actual, + 'sha256': hashlib.sha256(actual).hexdigest()}) + original_tree = ast.parse((args.reference / 'tests/resilience.py').read_text(encoding='utf-8')) + actual_tree = ast.parse((ROOT / 'tests/resilience.py').read_text(encoding='utf-8')) + actual_tree.body = [node for node in actual_tree.body + if not (isinstance(node, ast.FunctionDef) and node.name == 'hook_command') + and not (isinstance(node, ast.Import) and len(node.names) == 1 + and node.names[0].name in ('shutil', 'sys'))] + for node in ast.walk(actual_tree): + if (isinstance(node, ast.Call) and isinstance(node.func, ast.Attribute) + and node.func.attr == 'Popen' and node.args + and isinstance(node.args[0], ast.Call) + and isinstance(node.args[0].func, ast.Name) + and node.args[0].func.id == 'hook_command'): + node.args[0] = ast.parse('[str(script)]', mode='eval').body + acceptance_identical = ast.dump(original_tree) == ast.dump(actual_tree) + result = {'resilience_acceptance_ast_identical': acceptance_identical, 'source': str(args.reference.resolve()), 'at_utc': datetime.now(timezone.utc).isoformat(), + 'files': rows, 'resilience_difference': 'Only portable Python/shell hook interpreter resolution; acceptance assertions preserved.'} + args.output.parent.mkdir(parents=True, exist_ok=True) + args.output.write_text(json.dumps(result, indent=2) + '\n', encoding='utf-8') + for row in rows: + print(('PASS ' if row['identical'] else 'FAIL ') + row['file']) + print(('PASS ' if acceptance_identical else 'FAIL ') + 'resilience acceptance unchanged after interpreter adapter') + return int(not acceptance_identical or any(not row['identical'] for row in rows)) + + +if __name__ == '__main__': + raise SystemExit(main()) diff --git a/scripts/evidence_utils.py b/scripts/evidence_utils.py new file mode 100644 index 00000000..bf96387d --- /dev/null +++ b/scripts/evidence_utils.py @@ -0,0 +1,171 @@ +"""Переносимые помощники экспериментов: stdin для секретов и cgroup RAM sampler.""" + +import json +import os +from pathlib import Path +import struct +import subprocess +import threading +import time +import zlib + +from replicas import COMPOSE, ROOT + + +def save_json(path, value): + path.parent.mkdir(parents=True, exist_ok=True) + path.write_text(json.dumps(value, ensure_ascii=False, indent=2) + "\n", encoding="utf-8") + + +def record_command(out, name, command, *, timeout=600): + """Записать только команды, не содержащие credentials или полные inspect/env.""" + started = time.monotonic() + env = dict(os.environ, PYTHONUTF8="1", PYTHONIOENCODING="utf-8") + result = subprocess.run(command, cwd=ROOT, capture_output=True, env=env, timeout=timeout) + output = (result.stdout + result.stderr).decode("utf-8", errors="replace") + (out / (name + ".txt")).write_text( + "$ " + " ".join(map(str, command)) + "\n" + output + + f"\nexit_code={result.returncode}; elapsed_seconds={time.monotonic() - started:.3f}\n", + encoding="utf-8", + ) + if result.returncode: + raise RuntimeError(name + " failed; inspect the evidence file") + + +def exec_json(service, code, payload=None, *, timeout=90): + """JSON передаётся через stdin, поэтому Bearer не появляется в argv или evidence.""" + result = subprocess.run( + COMPOSE + ["exec", "-T", service, "python", "-c", code], + cwd=ROOT, + input=json.dumps(payload or {}).encode("utf-8"), + capture_output=True, + timeout=timeout, + ) + if result.returncode: + # Не печатать stderr кода, который имеет доступ к credentials. + raise RuntimeError("in-container check failed in " + service) + try: + return json.loads(result.stdout) + except (ValueError, UnicodeError): + raise RuntimeError("in-container check returned invalid JSON") from None + + +def png_fixture(width=1024, height=683): + """Настоящий RGB PNG >2 MiB, без Pillow и случайного содержимого на хосте.""" + def chunk(kind, data): + return struct.pack(">I", len(data)) + kind + data + struct.pack( + ">I", zlib.crc32(kind + data) & 0xffffffff + ) + + row = bytes(range(256)) * ((width * 3 + 255) // 256) + pixels = b"".join(b"\x00" + row[:width * 3] for _ in range(height)) + return ( + b"\x89PNG\r\n\x1a\n" + + chunk(b"IHDR", struct.pack(">IIBBBBB", width, height, 8, 2, 0, 0, 0)) + + chunk(b"IDAT", zlib.compress(pixels, level=0)) + + chunk(b"IEND", b"") + ) + + +MEMORY_SAMPLER = r''' +import json, pathlib, sys, threading, time +stop = threading.Event() +threading.Thread(target=lambda: (sys.stdin.buffer.read(), stop.set()), daemon=True).start() +root = pathlib.Path('/sys/fs/cgroup') +if (root / 'memory.current').exists(): + names = ('memory.current', 'memory.peak', 'memory.max') + version = 2 +else: + root = root / 'memory' + names = ('memory.usage_in_bytes', 'memory.max_usage_in_bytes', 'memory.limit_in_bytes') + version = 1 +started = time.monotonic() +def number(name): + try: + value = (root / name).read_text().strip() + return int(value) if value != 'max' else None + except (OSError, ValueError): + return None +def sample(): + rss = None + try: + for line in pathlib.Path('/proc/1/status').read_text().splitlines(): + if line.startswith('VmRSS:'): + rss = int(line.split()[1]) * 1024 + except (OSError, ValueError): + pass + print(json.dumps({'seconds': round(time.monotonic() - started, 3), + 'cgroup_version': version, 'memory_current_bytes': number(names[0]), + 'container_lifetime_peak_bytes': number(names[1]), 'limit_bytes': number(names[2]), + 'api_pid1_rss_bytes': rss}), flush=True) +sample() +while not stop.wait(0.2): + sample() +sample() +''' + + +class MemorySampler: + """Один небольшой Python-процесс в API читает RAM каждые 200 мс.""" + + def __init__(self, service): + self.service = service + self.samples = [] + self.ready = threading.Event() + self.process = None + + def start(self): + self.process = subprocess.Popen( + COMPOSE + ["exec", "-T", self.service, "python", "-u", "-c", MEMORY_SAMPLER], + cwd=ROOT, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, + ) + + def read(): + for line in self.process.stdout: + try: + sample = json.loads(line) + self.samples.append(sample) + self.ready.set() + except (ValueError, UnicodeError): + pass + + self.reader = threading.Thread(target=read, daemon=True) + self.reader.start() + if not self.ready.wait(timeout=10): + self.stop() + raise RuntimeError("memory sampler did not start in " + self.service) + if self.samples[0]["memory_current_bytes"] is None: + self.stop() + raise RuntimeError("cgroup memory counters unavailable in " + self.service) + return self + + def stop(self): + if self.process is None: + return + self.process.stdin.close() + try: + self.process.wait(timeout=10) + except subprocess.TimeoutExpired: + self.process.kill() + self.process.wait(timeout=5) + self.reader.join(timeout=5) + self.process.stdout.close() + self.process.stderr.close() + self.process = None + + def report(self): + current = [s["memory_current_bytes"] for s in self.samples if s["memory_current_bytes"] is not None] + rss = [s["api_pid1_rss_bytes"] for s in self.samples if s["api_pid1_rss_bytes"] is not None] + lifetime = [s["container_lifetime_peak_bytes"] for s in self.samples if s["container_lifetime_peak_bytes"] is not None] + return { + "service": self.service, + "sampling_interval_seconds": 0.2, + "baseline_cgroup_bytes": current[0], + "sampled_cgroup_peak_bytes": max(current), + "sampled_growth_bytes": max(current) - current[0], + "sampled_api_pid1_rss_peak_bytes": max(rss) if rss else None, + "container_lifetime_peak_bytes": max(lifetime) if lifetime else None, + "limit_bytes": self.samples[0]["limit_bytes"], + "samples": self.samples, + "limitations": "cgroup includes file cache and sampler overhead; 200 ms samples may miss short spikes; lifetime peak may predate this experiment", + } diff --git a/scripts/init_env.py b/scripts/init_env.py new file mode 100644 index 00000000..866285ac --- /dev/null +++ b/scripts/init_env.py @@ -0,0 +1,70 @@ +#!/usr/bin/env python3 +"""Создать локальный .env для lab4 без вывода секретов; работает в PowerShell.""" + +import argparse +import os +from pathlib import Path +import secrets + + +ROOT = Path(__file__).resolve().parents[1] + + +def initialize(path: Path): + """Сохранить существующие значения, заполнить отсутствующие настройки.""" + lines = path.read_text(encoding="utf-8-sig").splitlines() if path.exists() else [] + values = {} + positions = {} + for index, line in enumerate(lines): + if line.strip() and not line.lstrip().startswith("#") and "=" in line: + key, value = line.split("=", 1) + values[key.strip()] = value.strip() + positions.setdefault(key.strip(), []).append(index) + + defaults = { + "POSTGRES_USER": "messenger", + "POSTGRES_PASSWORD": secrets.token_hex(24), + "POSTGRES_DB": "messenger", + "SESSION_TTL_SECONDS": "3600", + "CACHE_URL": "redis://redis:6379/0", + "LOG_LEVEL": "WARNING", + "API_IMAGE": "mtusi-messenger:lab4", + "MINIO_ROOT_USER": "minio-root", + "MINIO_ROOT_PASSWORD": secrets.token_hex(32), + "S3_BUCKET": "attachments", + "S3_ACCESS_KEY": "api_" + secrets.token_hex(8), + "S3_SECRET_KEY": secrets.token_hex(20), + } + changed = [] + for key, default in defaults.items(): + existing = values.get(key, "") + upgrade_image = key == "API_IMAGE" and existing == "mtusi-messenger:lab3" + if key in values and existing and not existing.startswith("CHANGE_ME") and not upgrade_image: + continue + # An explicitly empty CACHE_URL means the optional cache is disabled. + if key == "CACHE_URL" and key in values: + continue + if key in positions: + for index in positions[key]: + lines[index] = key + "=" + default + else: + lines.append(key + "=" + default) + changed.append(key) + if changed or not path.exists(): + path.write_text("\n".join(lines) + "\n", encoding="utf-8") + if os.name != "nt": + path.chmod(0o600) + return changed + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--output", type=Path, default=ROOT / ".env") + args = parser.parse_args() + changed = initialize(args.output) + print(".env: " + ("обновлены настройки " + ", ".join(changed) if changed else "уже настроен")) + print("Существующий пароль PostgreSQL сохранён; значения секретов не выводятся.") + + +if __name__ == "__main__": + main() diff --git a/scripts/lab4_evidence.py b/scripts/lab4_evidence.py new file mode 100644 index 00000000..dae27db7 --- /dev/null +++ b/scripts/lab4_evidence.py @@ -0,0 +1,389 @@ +#!/usr/bin/env python3 +"""Повторяемая проверка lab4: реальные отказы, приватный MinIO и 10 uploads с RAM.""" + +import argparse +import base64 +from concurrent.futures import ThreadPoolExecutor +from datetime import datetime, timezone +import hashlib +import json +import os +from pathlib import Path +import platform +import subprocess +import sys +import threading +import time +import urllib.error +import urllib.request +from urllib.parse import quote + +from evidence_utils import MemorySampler, exec_json, png_fixture, record_command, save_json +from replicas import COMPOSE, ROOT, SERVICES, compose, inventory, wait_balanced + +sys.path.insert(0, str(ROOT / "tests")) +from client import Client, Failure, check +from resilience import history + + +def upload(c, actor, chat, content, filename="sample.png"): + response = c.call( + "POST", "/api/v1/chats/" + chat["id"] + "/attachments?filename=" + quote(filename), + 201, actor=actor, raw=content, headers={"Content-Type": "image/png"}, schema="Attachment", + ) + att = response["json"] + check(att["status"] == "ready" and att["error_code"] is None, "upload must finish ready") + check(att["variants"] == [], "lab4 must return the original without variants") + check(att["size_bytes"] == len(content), "uploaded size mismatch") + check(att["sha256"] == hashlib.sha256(content).hexdigest(), "uploaded checksum mismatch") + check(att["chat_id"] == chat["id"] and att["owner_id"] == actor["id"], "upload ownership mismatch") + return response + + +def download(c, actor, att, content): + metadata = c.call("GET", "/api/v1/attachments/" + att["id"], actor=actor, schema="Attachment") + check(metadata["json"] == att, "attachment metadata changed") + response = c.call("GET", "/api/v1/attachments/" + att["id"] + "/content", actor=actor) + check(response["bytes"] == content, "download differs byte-for-byte from original") + check(response["headers"].get_content_type() == "image/png", "download Content-Type mismatch") + return response + + +def attachment_contract(c, alice, bob, eve, chat, content): + response = upload(c, alice, chat, content) + att = response["json"] + attached = c.message(alice, chat, "", attachments=[att["id"]])["json"] + check(attached["attachment_ids"] == [att["id"]], "attachment-only message lost attachment") + check(attached in history(c, bob, chat), "second member cannot read attached message") + download(c, bob, att, content) + for suffix in ("", "/content"): + c.call("GET", "/api/v1/attachments/" + att["id"] + suffix, 404, actor=eve) + return { + "attachment_id": att["id"], "upload_instance": response["headers"]["X-Instance-Id"], + "sha256": att["sha256"], "size_bytes": len(content), "download_equal": True, + "attachment_only_message_visible": True, "outsider_metadata_status": 404, + "outsider_content_status": 404, + }, att, response["headers"]["X-Instance-Id"] + + +def uploader_failover(c, bob, att, content, target): + """Остановить именно процесс, вернувший 201 на binary upload.""" + before = inventory() + matches = [row for row in before if row.get("instance_id") == target] + check(len(matches) == 1, "upload must identify one current API process") + service = matches[0]["service"] + started = time.monotonic() + try: + env = dict(os.environ, TARGET_INSTANCE_ID=target, PYTHONUTF8="1") + result = subprocess.run( + [sys.executable, str(ROOT / "scripts/stop_one.py")], cwd=ROOT, env=env, + capture_output=True, timeout=15, + ) + check(result.returncode == 0, "could not stop uploading API") + stopped = next(row for row in inventory() if row["service"] == service) + check(stopped["state"] == "exited" and stopped["pid"] == 0, "upload process still alive") + deadline = time.monotonic() + 10 + while True: + try: + response = download(c, bob, att, content) + break + except Failure: + check(time.monotonic() < deadline, "download failed to recover after uploader stop") + time.sleep(0.2) + survivor = response["headers"]["X-Instance-Id"] + check(survivor != target, "download came from the stopped uploader") + return { + "upload_instance": target, "stopped_service": service, + "stopped_state": stopped["state"], "stopped_pid": stopped["pid"], + "download_instance": survivor, "sha256": hashlib.sha256(response["bytes"]).hexdigest(), + "download_equal": True, "seconds_from_stop_start": round(time.monotonic() - started, 3), + } + finally: + compose("up", "-d", "--no-deps", service) + wait_balanced(timeout=60) + + +def persistence(c, bob, chat, att, content, out): + before = inventory() + record_command(out, "04_restart_command", [sys.executable, "scripts/restart.py"], timeout=120) + after = inventory() + check({r["instance_id"] for r in before}.isdisjoint(r["instance_id"] for r in after), + "restart must replace both API processes") + response = download(c, bob, att, content) + check(any(att["id"] in message["attachment_ids"] for message in history(c, bob, chat)), + "attached message lost after restart") + return { + "before": before, "after": after, "existing_session_works": True, + "message_preserved": True, "metadata_equal": True, "download_equal": True, + "sha256": hashlib.sha256(response["bytes"]).hexdigest(), + "restart_scope": "all default Compose services, force recreated; named volumes retained", + } + + +DIRECT_PROBES = r''' +import base64, json, urllib.error, urllib.request, sys +p = json.load(sys.stdin) +result = {} +for name, method, path, body in ( + ('live', 'GET', '/health/live', None), + ('ready', 'GET', '/health/ready', None), + ('upload', 'POST', '/api/v1/chats/' + p['chat_id'] + '/attachments?filename=outage.png', base64.b64decode(p['png'])), + ('content', 'GET', '/api/v1/attachments/' + p['attachment_id'] + '/content', None), +): + headers = {'Authorization': 'Bearer ' + p['token'], 'Content-Type': 'image/png'} + request = urllib.request.Request('http://127.0.0.1:8080' + path, method=method, headers=headers, data=body) + try: + response = urllib.request.urlopen(request, timeout=30) + except urllib.error.HTTPError as error: + response = error + with response: + data = response.read(16 * 1024 * 1024) + result[name] = {'status': response.status, 'instance_id': response.headers.get('X-Instance-Id'), + 'request_id_present': bool(response.headers.get('X-Request-Id'))} + if response.status >= 400: + error = json.loads(data)['error'] + result[name]['error_code'] = error['code'] + result[name]['request_id_matches'] = error['request_id'] == response.headers.get('X-Request-Id') +print(json.dumps(result)) +''' + + +def s3_outage(c, alice, bob, chat, att, content): + results = {} + public = None + try: + compose("stop", "--timeout", "10", "minio") + for service in SERVICES: + results[service] = exec_json(service, DIRECT_PROBES, { + "token": alice["token"], "chat_id": chat["id"], "attachment_id": att["id"], + "png": base64.b64encode(content).decode("ascii"), + }, timeout=120) + for name, status in (("live", 200), ("ready", 503), ("upload", 503), ("content", 503)): + actual = results[service][name] + check(actual["status"] == status, service + "/" + name + ": " + str(actual)) + check(actual["request_id_present"] and actual["instance_id"], "missing API trace headers") + if status == 503: + check(actual["error_code"] == "unavailable" and actual["request_id_matches"], + "S3 outage must return a structured 503") + try: + reply = urllib.request.urlopen(c.base + "/health/ready", timeout=10) + except urllib.error.HTTPError as error: + reply = error + with reply: + payload = json.loads(reply.read()) + public = {"status": reply.status, "error_code": payload["error"]["code"], + "request_id_matches": payload["error"]["request_id"] == reply.headers.get("X-Request-Id")} + check(public["status"] == 503 and public["request_id_matches"], "public readiness must return structured 503") + finally: + compose("start", "minio") + wait_balanced(timeout=60) + download(c, bob, att, content) + return {"direct_api_probes": results, "public_readiness": public, "restored_download_equal": True, + "method": "probe each API directly inside its container because LB excludes unready backends"} + + +PRIVATE_BUCKET = r''' +import asyncio, io, json, os, sys, uuid, urllib.error, urllib.request +import urllib.error +import urllib.request +from urllib.parse import quote, urlsplit +from minio import Minio +from minio.error import S3Error +from sqlalchemy import text +from app.config import Settings +from app.db import create_engine +p = json.load(sys.stdin) +async def key(): + engine = create_engine(Settings.from_env()) + try: + async with engine.connect() as connection: + return (await connection.execute(text('SELECT object_key FROM attachments WHERE id=:id'), + {'id': uuid.UUID(p['attachment_id'])})).scalar_one() + finally: + await engine.dispose() +object_key = asyncio.run(key()) +endpoint = os.environ['S3_ENDPOINT_URL'].rstrip('/') +bucket = os.environ['S3_BUCKET'] +url = endpoint + '/' + quote(bucket, safe='') + '/' + quote(object_key, safe='/') +try: + response = urllib.request.urlopen(url, timeout=5) +except urllib.error.HTTPError as error: + response = error +with response: + anonymous_status = response.status +parsed = urlsplit(endpoint) +client = Minio(parsed.netloc, access_key=os.environ['S3_ACCESS_KEY'], secret_key=os.environ['S3_SECRET_KEY'], + secure=parsed.scheme == 'https', region=os.environ.get('S3_REGION', 'us-east-1')) +new_bucket = 'forbidden-' + uuid.uuid4().hex +outside_key = 'forbidden/' + uuid.uuid4().hex +operations = { + 'list_foreign_bucket': lambda: list(client.list_objects(new_bucket, recursive=True)), + 'make_bucket': lambda: client.make_bucket(new_bucket), + 'put_outside_prefix': lambda: client.put_object(bucket, outside_key, io.BytesIO(b'x'), 1), +} +# MinIO deliberately filters ListBuckets to buckets permitted by bucket-level IAM. +# Verify the names rather than incorrectly requiring HTTP 403 for this API. +visible_buckets = [item.name for item in client.list_buckets()] +outcomes = {} +for name, operation in operations.items(): + try: + operation() + outcomes[name] = 'UNEXPECTED_SUCCESS' + except S3Error as error: + outcomes[name] = error.code + except Exception as error: + outcomes[name] = type(error).__name__ +# Clean synthetic probes if an incorrect policy unexpectedly allowed them. +for action in ( + (lambda: client.remove_bucket(new_bucket)) if outcomes['make_bucket'] == 'UNEXPECTED_SUCCESS' else None, + (lambda: client.remove_object(bucket, outside_key)) if outcomes['put_outside_prefix'] == 'UNEXPECTED_SUCCESS' else None, +): + if action: + try: action() + except Exception: pass +print(json.dumps({'anonymous_get_status': anonymous_status, 'restricted_operations': outcomes, + 'visible_buckets': visible_buckets, 'visible_buckets_limited': set(visible_buckets) <= {bucket}, + 'root_credentials_absent_in_api': not any(name in os.environ for name in ('MINIO_ROOT_USER', 'MINIO_ROOT_PASSWORD')), + 'server_assigned_original_prefix': object_key.startswith('originals/')})) +''' + + +def private_bucket(att, out): + # The same bootstrap is executed twice against an already populated bucket. + for number in (1, 2): + record_command(out, "06_bootstrap_repeat_" + str(number), + COMPOSE + ["run", "--rm", "--no-deps", "minio-init"], timeout=120) + result = exec_json("api-a", PRIVATE_BUCKET, {"attachment_id": att["id"]}) + check(result["anonymous_get_status"] == 403, "private bucket anonymous GET: " + str(result)) + check(all(value == "AccessDenied" for value in result["restricted_operations"].values()), + "API S3 privilege violation: " + str(result)) + check(result["visible_buckets_limited"], "API can see an unrelated bucket") + check(result["root_credentials_absent_in_api"], "API must not receive root S3 credentials") + check(result["server_assigned_original_prefix"], "unexpected server object key prefix") + result["bootstrap_repeated_successfully"] = 2 + return result + + +def concurrent_uploads(base, alice, bob, chat, concurrency): + content = png_fixture() + check(len(content) >= 2 * 1024 * 1024, "experiment fixture must be at least 2 MiB") + barrier = threading.Barrier(concurrency, timeout=30) + samplers = [] + + def send(number): + client = Client(base, timeout=90) + barrier.wait() + started = time.monotonic() + try: + response = upload(client, alice, chat, content, "parallel-%02d.png" % number) + return {"ok": True, "started": started, "finished": time.monotonic(), + "attachment": response["json"], "instance": response["headers"]["X-Instance-Id"]} + except Exception as error: + return {"ok": False, "started": started, "finished": time.monotonic(), + "error": str(error) if isinstance(error, Failure) else type(error).__name__} + + try: + for service in SERVICES: + samplers.append(MemorySampler(service).start()) + with ThreadPoolExecutor(max_workers=concurrency) as pool: + responses = list(pool.map(send, range(concurrency))) + finally: + for sampler in samplers: + sampler.stop() + good = [r for r in responses if r["ok"]] + elapsed = max(r["finished"] for r in responses) - min(r["started"] for r in responses) + memory = [sampler.report() for sampler in samplers] + result = { + "concurrency": concurrency, "barrier_synchronized": True, "requests": len(responses), + "successes": len(good), "failures": len(responses) - len(good), + "fixture_bytes": len(content), "fixture_dimensions": [1024, 683], + "fixture_sha256": hashlib.sha256(content).hexdigest(), + "elapsed_seconds": round(elapsed, 3), "successful_uploads_per_second": round(len(good) / elapsed, 3), + "successful_mib_per_second": round(len(good) * len(content) / (1024 * 1024 * elapsed), 3), + "instances": sorted({r["instance"] for r in good}), "memory": memory, + "latency_seconds": [round(r["finished"] - r["started"], 3) for r in responses], + "errors": [r["error"] for r in responses if not r["ok"]], + } + # Preserve measured results even when the experiment finds a failure. + result["all_downloads_equal"] = False + if len(good) != concurrency: + return result + check(len({r["attachment"]["id"] for r in good}) == concurrency, "uploads reused attachment IDs") + check(len(result["instances"]) == len(SERVICES), "concurrent uploads did not visit both API replicas") + for response in good: + download(Client(base, timeout=30), bob, response["attachment"], content) + result["all_downloads_equal"] = True + return result + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--base-url", default="http://localhost:8080") + parser.add_argument("--output", default="evidence/lab4") + parser.add_argument("--concurrency", type=int, default=10) + parser.add_argument("--skip-cluster", action="store_true", help="Run only lab4 experiments; record omitted cluster coverage") + parser.add_argument("--skip-student-tests", action="store_true", help="Repeat infrastructure experiments without rerunning the already completed suite; record NOT_RUN") + args = parser.parse_args() + check(10 <= args.concurrency <= 32, "concurrency must be 10..32") + out = ROOT / args.output + out.mkdir(parents=True, exist_ok=True) + save_json(out / "result.json", {"status": "RUNNING"}) + os.environ["BASE_URL"] = args.base_url + + def stage(name, action): + print("Running " + name, flush=True) + started = time.monotonic() + result = action() + save_json(out / (name + ".json"), result) + print("Completed " + name + " in %.2f seconds" % (time.monotonic() - started), flush=True) + return result + + try: + save_json(out / "00_environment.json", { + "at_utc": datetime.now(timezone.utc).isoformat(), "platform": platform.platform(), + "python": platform.python_version(), "host_cpu_count": os.cpu_count(), + "cluster_checks": "NOT_RUN" if args.skip_cluster else "included", + "synthetic_data_only": True, "host_dependencies": "Python standard library + Docker Compose", + }) + record_command(out, "00_docker", ["docker", "version"]) + record_command(out, "00_compose_config", COMPOSE + ["config", "--quiet"]) + if not args.skip_cluster: + print("Running preserved cluster checks", flush=True) + record_command(out, "01_cluster", [ + sys.executable, "scripts/cluster_evidence.py", "--base-url", args.base_url, + "--output", str(out / "cluster"), + ], timeout=900) + wait_balanced(timeout=60) + record_command(out, "01_public_smoke", [sys.executable, "tests/smoke.py", "--base-url", args.base_url]) + c = Client(args.base_url, timeout=15) + alice, bob, eve, chat = c.bootstrap() + content = (ROOT / "tests/fixtures/sample.png").read_bytes() + contract, att, target = attachment_contract(c, alice, bob, eve, chat, content) + save_json(out / "02_attachment_contract.json", contract) + stage("03_uploader_failover", lambda: uploader_failover(c, bob, att, content, target)) + stage("04_full_restart", lambda: persistence(c, bob, chat, att, content, out)) + stage("05_s3_outage", lambda: s3_outage(c, alice, bob, chat, att, content)) + stage("06_private_bucket", lambda: private_bucket(att, out)) + result = stage("07_concurrent_uploads", lambda: concurrent_uploads( + args.base_url, alice, bob, chat, args.concurrency + )) + check(result["failures"] == 0 and result["all_downloads_equal"], "concurrent uploads failed; see measured results") + record_command(out, "08_cleanup_dry_run", COMPOSE + ["exec", "-T", "api-a", "python", "-m", "app.cleanup", "--dry-run"]) + if args.skip_student_tests: + save_json(out / "09_student_tests.json", {"status": "NOT_RUN", "reason": "explicit --skip-student-tests; consult full run"}) + else: + record_command(out, "09_student_tests", COMPOSE + ["--profile", "test", "run", "--rm", "--build", "tests"], timeout=900) + save_json(out / "10_final_processes.json", inventory()) + save_json(out / "result.json", {"status": "PASS", "at_utc": datetime.now(timezone.utc).isoformat(), + "cluster_checks": "NOT_RUN" if args.skip_cluster else "PASS", + "student_tests": "NOT_RUN" if args.skip_student_tests else "PASS"}) + print("PASS: lab4 evidence saved to " + str(out), flush=True) + except BaseException as error: + save_json(out / "result.json", {"status": "FAIL", "at_utc": datetime.now(timezone.utc).isoformat(), + "error_type": type(error).__name__, + "note": "Inspect completed files; an incomplete run does not prove acceptance."}) + raise + + +if __name__ == "__main__": + main() diff --git a/scripts/local_evidence.py b/scripts/local_evidence.py new file mode 100644 index 00000000..9889e37d --- /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_stack", [sys.executable, "scripts/check_stack.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 python scripts/lab4_evidence.py separately", + "required_command_on_docker_host": "python scripts/lab4_evidence.py", + }, indent=2) + "\n") + return int(any(check["exit_code"] != 0 for check in manifest)) + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/scripts/manual_attachment_checks.py b/scripts/manual_attachment_checks.py new file mode 100644 index 00000000..c95c82be --- /dev/null +++ b/scripts/manual_attachment_checks.py @@ -0,0 +1,93 @@ +#!/usr/bin/env python3 +"""Live HTTP negative checks and an actual client disconnect, without pytest.""" + +import argparse +from datetime import datetime, timezone +import hashlib +import json +from pathlib import Path +import socket +import sys +import time +from urllib.parse import urlsplit +import uuid + +from evidence_utils import exec_json, save_json +from replicas import ROOT +sys.path.insert(0, str(ROOT / 'tests')) +from client import Client, check + +INVENTORY = r''' +import asyncio,json,sys +from sqlalchemy import text +from app.config import Settings +from app.db import create_engine +from app.storage import MinioStorage +p=json.load(sys.stdin) +s=Settings.from_env() +async def count(): + e=create_engine(s) + try: + async with e.connect() as c: + return (await c.execute(text('SELECT count(*) FROM attachments WHERE filename=:name'), {'name':p['filename']})).scalar_one() + finally: + await e.dispose() +storage=MinioStorage(s) +try: + print(json.dumps({'matching_metadata':asyncio.run(count()),'s3_objects':sum(1 for _ in storage.client.list_objects(storage.bucket,prefix='originals/',recursive=True))})) +finally: + storage.close() +''' + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--base-url', default='http://localhost:8080') + parser.add_argument('--output', type=Path, default=ROOT / 'evidence/lab4/manual_http.json') + args = parser.parse_args() + c = Client(args.base_url, timeout=30) + alice, bob, eve, chat = c.bootstrap() + data = (ROOT / 'tests/fixtures/sample.png').read_bytes() + path = '/api/v1/chats/' + chat['id'] + '/attachments?filename=manual.png' + cases = [ + ('corrupt', data[:48], 'image/png', 415), + ('mime_spoof', data, 'image/jpeg', 415), + ('too_large', data + b'x' * (10 * 1024 * 1024), 'image/png', 413), + ] + results = [] + for label, raw, mime, status in cases: + reply = c.call('POST', path, status, actor=alice, raw=raw, headers={'Content-Type':mime}) + results.append({'case':label, 'status':reply['status'], 'error_code':reply['json']['error']['code']}) + attachment, content = c.attachment(alice, chat) + actual = c.call('GET','/api/v1/attachments/' + attachment['id'] + '/content',actor=bob)['bytes'] + check(actual == content, 'manual download mismatch') + label = 'interrupted-' + uuid.uuid4().hex + '.png' + before = exec_json('api-a', INVENTORY, {'filename':label}) + endpoint = urlsplit(args.base_url) + check(endpoint.scheme == 'http', 'disconnect probe requires isolated local HTTP stand') + host, port = endpoint.hostname, endpoint.port or 80 + header = ( + 'POST /api/v1/chats/' + chat['id'] + '/attachments?filename=' + label + ' HTTP/1.1\r\n' + 'Host: ' + endpoint.netloc + '\r\n' + 'Authorization: Bearer ' + alice['token'] + '\r\n' + 'Content-Type: image/png\r\nContent-Length: ' + str(len(data)) + '\r\n' + 'Connection: close\r\n\r\n' + ).encode('ascii') + with socket.create_connection((host, port), timeout=5) as connection: + connection.sendall(header + data[:1024]) + time.sleep(0.2) + # Close before declared Content-Length. The server must not publish an object. + time.sleep(1) + after = exec_json('api-a', INVENTORY, {'filename':label}) + check(after['matching_metadata'] == 0, 'interrupted upload persisted metadata') + check(before['s3_objects'] == after['s3_objects'], 'interrupted upload left an S3 object') + result = {'status':'PASS','at_utc':datetime.now(timezone.utc).isoformat(), 'negative_cases':results, + 'original_sha256':hashlib.sha256(actual).hexdigest(), 'download_equal':True, + 'disconnect':{'actual_tcp_close':True,'metadata_rows_after':0, + 's3_objects_before':before['s3_objects'],'s3_objects_after':after['s3_objects']}} + save_json(args.output,result) + print('PASS: live upload/download, negative files and interrupted TCP upload') + + +if __name__ == '__main__': + main() diff --git a/scripts/replicas.py b/scripts/replicas.py new file mode 100644 index 00000000..311254b9 --- /dev/null +++ b/scripts/replicas.py @@ -0,0 +1,173 @@ +#!/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, + encoding="utf-8", + errors="replace", + stdout=subprocess.PIPE if capture else None, + stderr=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, + encoding="utf-8", + errors="replace", + 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): + """Дождаться обеих текущих ready-реплик через LB, включая новую после replace.""" + import urllib.request + + deadline = time.monotonic() + timeout + expected = { + wait_ready(service, timeout=max(0, deadline - time.monotonic()))["instance_id"] + for service in SERVICES + } + if len(expected) != len(SERVICES): + raise RuntimeError("API processes must have distinct instance IDs") + observed = set() + 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: + observed.add(reply.headers.get("X-Instance-Id")) + if expected <= observed: + return + except OSError: + pass + time.sleep(0.3) + raise RuntimeError("LB did not expose both current 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.py b/scripts/restart.py new file mode 100644 index 00000000..04841b1d --- /dev/null +++ b/scripts/restart.py @@ -0,0 +1,16 @@ +#!/usr/bin/env python3 +"""Пересоздать весь стенд с сохранением volumes; работает в Windows и Linux.""" + +from replicas import compose, wait_balanced + + +def main(): + # Compose повторно запускает migrate и bootstrap согласно depends_on. + # Именованные тома PostgreSQL/MinIO остаются подключёнными к новым контейнерам. + compose("stop") + compose("up", "-d", "--force-recreate") + wait_balanced(timeout=60) + + +if __name__ == "__main__": + main() diff --git a/scripts/restart.sh b/scripts/restart.sh new file mode 100644 index 00000000..2836c372 --- /dev/null +++ b/scripts/restart.sh @@ -0,0 +1,4 @@ +#!/bin/sh +set -eu +cd "$(dirname "$0")/.." +exec python3 scripts/restart.py "$@" 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/scripts/stop_one.py b/scripts/stop_one.py new file mode 100644 index 00000000..7b77202b --- /dev/null +++ b/scripts/stop_one.py @@ -0,0 +1,12 @@ +#!/usr/bin/env python3 +"""Остановить API по TARGET_INSTANCE_ID без зависимости от shell хоста.""" + +from pathlib import Path +import subprocess +import sys + + +if __name__ == "__main__": + raise SystemExit(subprocess.call([ + sys.executable, str(Path(__file__).with_name("replicas.py")), "stop-target" + ])) diff --git a/student-tests/conftest.py b/student-tests/conftest.py new file mode 100644 index 00000000..b70d3683 --- /dev/null +++ b/student-tests/conftest.py @@ -0,0 +1,274 @@ +"""Общие фикстуры тестов. + +Тесты работают с настоящим 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, + **{key: os.environ[key] for key in ("S3_ENDPOINT_URL", "S3_REGION", "S3_BUCKET", "S3_ACCESS_KEY", "S3_SECRET_KEY") if key in os.environ}, + } + ) + 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_attachment_failures.py b/student-tests/test_attachment_failures.py new file mode 100644 index 00000000..cc8478b8 --- /dev/null +++ b/student-tests/test_attachment_failures.py @@ -0,0 +1,121 @@ +"""Fault injection at real S3/DB boundaries and safe compensation.""" + +import uuid +from unittest.mock import patch + +import pytest +from sqlalchemy.exc import OperationalError +from sqlalchemy.ext.asyncio import AsyncSession +from starlette.requests import ClientDisconnect, Request + +from app.cleanup import cleanup_once +from app.storage import StorageUnavailable +from conftest import assert_error, run_sql +from test_attachments import upload, uploaded + + +def rows(chat): + return run_sql("SELECT id,object_key,status FROM attachments WHERE chat_id=$1", uuid.UUID(chat["id"])) + + +def clean(client, dry=False): + return client.portal.call(cleanup_once, client.app.state.sessionmaker, + client.app.state.storage, 3600, dry) + + +def age(ident): + run_sql("UPDATE attachments SET created_at=clock_timestamp()-interval '2 hours' WHERE id=$1", ident) + + +@pytest.mark.parametrize("after_put", [False, True]) +def test_s3_failure_leaves_accounted_intent_and_cleanup_retries(client, alice, chat, after_put): + storage = client.app.state.storage + original = storage.put + + def fail(key, stream, size, mime): + if after_put: + original(key, stream, size, mime) + raise StorageUnavailable() + + with patch.object(storage, "put", side_effect=fail): + assert_error(upload(client, alice, chat), 503, "unavailable") + pending = rows(chat) + assert len(pending) == 1 and pending[0]["status"] == "uploading" + row = pending[0] + assert clean(client)["deleted"] == 0 # Fresh intent protected even after a failure. + age(row["id"]) + assert clean(client, dry=True)["candidates"] >= 1 + assert rows(chat) + with patch.object(storage, "delete", side_effect=StorageUnavailable()): + assert clean(client)["failed"] >= 1 + assert rows(chat) + assert clean(client)["deleted"] >= 1 + assert not rows(chat) + with pytest.raises(StorageUnavailable): + storage.stat(row["object_key"]) + assert clean(client)["deleted"] == 0 + + +def test_database_failure_after_put_is_compensated(client, alice, chat): + original = AsyncSession.commit + count = 0 + + async def fail_final(session): + nonlocal count + count += 1 + if count == 2: + raise OperationalError("", {}, Exception("injected before final commit")) + return await original(session) + + with patch.object(AsyncSession, "commit", fail_final): + assert_error(upload(client, alice, chat), 503, "unavailable") + row = rows(chat)[0] + assert row["status"] == "uploading" + assert client.app.state.storage.stat(row["object_key"]).size > 0 + age(row["id"]) + assert clean(client)["deleted"] >= 1 + assert not rows(chat) + + +def test_ambiguous_successful_commit_never_deletes_ready_file(client, alice, bob, chat): + original = AsyncSession.commit + count = 0 + + async def lost_ack(session): + nonlocal count + count += 1 + await original(session) + if count == 2: + raise OperationalError("", {}, Exception("lost final commit acknowledgement")) + + with patch.object(AsyncSession, "commit", lost_ack): + assert_error(upload(client, alice, chat), 503, "unavailable") + row = rows(chat)[0] + assert row["status"] == "ready" + age(row["id"]) + clean(client) + assert rows(chat)[0]["status"] == "ready" + assert client.get("/api/v1/attachments/" + str(row["id"]) + "/content", headers=bob.headers).status_code == 200 + + +def test_cleanup_never_removes_old_ready_attachment(client, alice, bob, chat): + attachment = uploaded(client, alice, chat) + age(uuid.UUID(attachment["id"])) + clean(client) + assert client.get("/api/v1/attachments/" + attachment["id"] + "/content", headers=bob.headers).status_code == 200 + + +def test_interrupted_body_creates_neither_metadata_nor_object(client, alice, chat): + original = Request.stream + + async def interrupted(request): + if request.url.path.endswith("/attachments"): + yield b"\x89PNG\r\n\x1a\n" + raise ClientDisconnect() + async for chunk in original(request): + yield chunk + + with patch.object(Request, "stream", interrupted), patch.object(client.app.state.storage, "put") as put: + assert_error(upload(client, alice, chat), 400, "invalid_request") + put.assert_not_called() + assert not rows(chat) diff --git a/student-tests/test_attachments.py b/student-tests/test_attachments.py new file mode 100644 index 00000000..4670e147 --- /dev/null +++ b/student-tests/test_attachments.py @@ -0,0 +1,240 @@ +"""Вложения: настоящий MinIO, проверка файла, ACL и связь с сообщениями.""" + +import hashlib +from io import BytesIO +import struct +from urllib.parse import quote +import uuid +import zlib + +from PIL import Image +import pytest + +from conftest import assert_error, run_sql + +MAX_BYTES = 10 * 1024 * 1024 + + +def image_bytes(format="PNG"): + stream = BytesIO() + with Image.new("RGB", (32, 24), (35, 120, 205)) as image: + image.save(stream, format=format) + return stream.getvalue() + + +def upload(client, person, chat, data=None, filename="photo.png", content_type="image/png"): + path = "/api/v1/chats/" + chat["id"] + "/attachments" + return client.post( + path + "?filename=" + quote(filename, safe=""), + content=image_bytes() if data is None else data, + headers={**person.headers, "Content-Type": content_type}, + ) + + +def uploaded(client, person, chat): + reply = upload(client, person, chat) + assert reply.status_code == 201, reply.text + return reply.json() + + +def attachment_path(attachment): + return "/api/v1/attachments/" + attachment["id"] + + +def message_path(chat): + return "/api/v1/chats/" + chat["id"] + "/messages" + + +def png_chunk(kind, data): + return struct.pack(">I", len(data)) + kind + data + struct.pack(">I", zlib.crc32(kind + data)) + + +@pytest.mark.parametrize("format,mime,filename", [("PNG", "image/png", "снимок.png"), ("JPEG", "image/jpeg", "photo.jpeg")]) +def test_original_metadata_and_bytes_for_another_member(client, alice, bob, chat, format, mime, filename): + data = image_bytes(format) + reply = upload(client, alice, chat, data, filename, mime) + assert reply.status_code == 201, reply.text + attachment = reply.json() + assert attachment["chat_id"] == chat["id"] + assert attachment["owner_id"] == alice.id + assert attachment["filename"] == filename + assert attachment["status"] == "ready" + assert attachment["variants"] == [] and attachment["error_code"] is None + assert attachment["content_type"] == mime + assert attachment["size_bytes"] == len(data) + assert attachment["sha256"] == hashlib.sha256(data).hexdigest() + assert attachment["updated_at"].endswith("Z") + assert attachment["created_at"].endswith("Z") + assert not {"object_key", "bucket", "url", "access_key", "secret_key"} & attachment.keys() + metadata = client.get(attachment_path(attachment), headers=bob.headers) + assert metadata.status_code == 200 and metadata.json() == attachment + for suffix in ("/content", "/content?variant=original"): + response = client.get(attachment_path(attachment) + suffix, headers=bob.headers, follow_redirects=False) + assert response.status_code == 200 + assert response.content == data + assert response.headers["content-type"].split(";")[0] == mime + assert response.headers["X-Request-Id"] and response.headers["X-Instance-Id"] + assert "location" not in response.headers + + +def test_filename_is_not_an_object_identifier(client, alice, bob, chat): + first = upload(client, alice, chat, image_bytes("PNG"), filename="same.png") + second = upload(client, alice, chat, image_bytes("JPEG"), filename="same.png", content_type="image/jpeg") + assert first.status_code == second.status_code == 201 + assert first.json()["id"] != second.json()["id"] + for reply, data in ((first, image_bytes("PNG")), (second, image_bytes("JPEG"))): + actual = client.get(attachment_path(reply.json()) + "/content", headers=bob.headers) + assert actual.status_code == 200 and actual.content == data + + +def test_outsider_cannot_upload_read_or_reference_an_attachment(client, alice, eve, chat): + attachment = uploaded(client, alice, chat) + assert_error(upload(client, eve, chat), 404, "not_found") + for suffix in ("", "/content"): + assert_error(client.get(attachment_path(attachment) + suffix, headers=eve.headers), 404, "not_found") + assert_error(client.get(attachment_path(attachment) + suffix), 401, "unauthorized") + assert_error( + client.post(message_path(chat), json={"text": "", "attachment_ids": [attachment["id"]]}, headers=eve.headers), + 404, "not_found", + ) + + +def test_attachment_cannot_move_between_chats_even_for_same_member(client, alice, bob, chat, make_chat): + attachment = uploaded(client, alice, chat) + other = make_chat(alice, bob) + assert_error( + client.post(message_path(other), json={"text": "file", "attachment_ids": [attachment["id"]]}, headers=alice.headers), + 404, "not_found", + ) + assert client.get(message_path(other), headers=bob.headers).json()["items"] == [] + + +def test_unknown_attachment_is_404_without_creating_message(client, alice, chat): + absent = str(uuid.uuid4()) + assert_error(client.get("/api/v1/attachments/" + absent, headers=alice.headers), 404, "not_found") + assert_error( + client.post(message_path(chat), json={"text": "", "attachment_ids": [absent]}, headers=alice.headers), + 404, "not_found", + ) + assert client.get(message_path(chat), headers=alice.headers).json()["items"] == [] + + +@pytest.mark.parametrize("text", ["", " \t\n"]) +def test_any_member_can_send_attachment_without_text(client, alice, bob, chat, text): + attachment = uploaded(client, alice, chat) + response = client.post(message_path(chat), json={"text": text, "attachment_ids": [attachment["id"]]}, headers=bob.headers) + assert response.status_code == 201, response.text + message = response.json() + assert message["text"] == text and message["attachment_ids"] == [attachment["id"]] + assert message["sender_id"] == bob.id + assert client.get(message_path(chat), headers=alice.headers).json()["items"] == [message] + + +def test_replay_compares_attachments_and_preserves_order(client, alice, chat): + first = uploaded(client, alice, chat) + second = uploaded(client, alice, chat) + ids = [second["id"], first["id"]] + body = {"text": "files", "attachment_ids": ids} + headers = alice.headers + original = client.post(message_path(chat), json=body, headers=headers) + assert original.status_code == 201, original.text + replay = client.post(message_path(chat), json={"attachment_ids": ids, "text": "files"}, headers=headers) + assert replay.status_code == 201 and replay.json() == original.json() + assert replay.json()["attachment_ids"] == ids + assert_error( + client.post(message_path(chat), json={"text": "files", "attachment_ids": [first["id"]]}, headers=headers), + 409, "conflict", + ) + history = client.get(message_path(chat), headers=alice.headers).json()["items"] + assert history == [original.json()] + + +@pytest.mark.parametrize("values", [None, "not-a-list", ["bad-uuid"], [42]]) +def test_attachment_list_has_strict_types(client, alice, chat, values): + assert_error( + client.post(message_path(chat), json={"text": "message", "attachment_ids": values}, headers=alice.headers), + 400, "invalid_request", + ) + + +def test_duplicate_and_excessive_attachment_ids_are_400(client, alice, chat): + attachment = uploaded(client, alice, chat) + for ids in ([attachment["id"], attachment["id"]], [str(uuid.uuid4()) for _ in range(11)]): + assert_error( + client.post(message_path(chat), json={"text": "message", "attachment_ids": ids}, headers=alice.headers), + 400, "invalid_request", + ) + + +@pytest.mark.parametrize("filename", ["", "a" * 256, "../photo.png", "folder/photo.png", "folder\\photo.png", "bad\x00.png", "bad\r\n.png", "bad\t.png", "bad\x7f.png", "bad\x85.png"]) +def test_dangerous_or_invalid_filename_is_400(client, alice, chat, filename): + assert_error(upload(client, alice, chat, filename=filename), 400, "invalid_request") + + +def test_missing_filename_is_400(client, alice, chat): + response = client.post( + "/api/v1/chats/" + chat["id"] + "/attachments", + content=image_bytes(), headers={**alice.headers, "Content-Type": "image/png"}, + ) + assert_error(response, 400, "invalid_request") + + +@pytest.mark.parametrize("data,mime", [(b"", "image/png"), (b"not an image", "image/png"), (b"GIF89a\x01\x00\x01\x00", "image/png"), (b"", "image/svg+xml")]) +def test_invalid_image_is_415_not_500(client, alice, chat, data, mime): + assert_error(upload(client, alice, chat, data, content_type=mime), 415, "unsupported_media_type") + + +@pytest.mark.parametrize("format,mime", [("PNG", "image/jpeg"), ("JPEG", "image/png"), ("PNG", "application/octet-stream")]) +def test_declared_mime_must_match_real_image(client, alice, chat, format, mime): + assert_error(upload(client, alice, chat, image_bytes(format), content_type=mime), 415, "unsupported_media_type") + + +@pytest.mark.parametrize("format,mime", [("PNG", "image/png"), ("JPEG", "image/jpeg")]) +def test_truncated_image_is_rejected(client, alice, chat, format, mime): + data = image_bytes(format) + assert_error(upload(client, alice, chat, data[:len(data) * 2 // 3], content_type=mime), 415, "unsupported_media_type") + + +def test_png_with_corrupted_pixel_stream_is_rejected(client, alice, chat): + original = image_bytes() + start = original.index(b"IDAT") + length = struct.unpack(">I", original[start - 4:start])[0] + # Valid CRC with an invalid compressed stream: checking the signature/CRC alone is insufficient. + corrupt = original[:start - 4] + png_chunk(b"IDAT", b"x" * length) + original[start + 4 + length + 4:] + assert_error(upload(client, alice, chat, corrupt), 415, "unsupported_media_type") + + +def test_byte_limit_with_content_length(client, alice, chat): + assert_error(upload(client, alice, chat, image_bytes() + b"x" * MAX_BYTES), 413, "payload_too_large") + + +def test_byte_limit_without_content_length(client, alice, chat): + def chunks(): + for _ in range(161): + yield b"x" * (64 * 1024) + + assert_error(upload(client, alice, chat, chunks()), 413, "payload_too_large") + + +def test_image_exactly_at_byte_limit_is_accepted(client, alice, bob, chat): + original = image_bytes() + padding = b"\0" * (MAX_BYTES - len(original) - 12) + data = original[:-12] + png_chunk(b"npAD", padding) + original[-12:] + assert len(data) == MAX_BYTES + reply = upload(client, alice, chat, data) + assert reply.status_code == 201, reply.text + response = client.get(attachment_path(reply.json()) + "/content", headers=bob.headers) + assert response.status_code == 200 + assert hashlib.sha256(response.content).digest() == hashlib.sha256(data).digest() + + +def test_pixel_limit_is_checked_before_decoding(client, alice, chat): + header = struct.pack(">IIBBBBB", 8000, 5001, 8, 2, 0, 0, 0) + data = b"\x89PNG\r\n\x1a\n" + png_chunk(b"IHDR", header) + png_chunk(b"IDAT", zlib.compress(b"")) + png_chunk(b"IEND", b"") + assert_error(upload(client, alice, chat, data), 413, "payload_too_large") + assert not run_sql("SELECT id FROM attachments WHERE chat_id=$1", uuid.UUID(chat["id"])) + + +def test_missing_variant_is_404(client, alice, chat): + attachment = uploaded(client, alice, chat) + assert_error(client.get(attachment_path(attachment) + "/content?variant=thumbnail", headers=alice.headers), 404, "not_found") 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..c39e79dc --- /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", "0003_attachments.sql", "0004_attachment_timestamps.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..1414e581 --- /dev/null +++ b/student-tests/test_empty_message.py @@ -0,0 +1,74 @@ +"""Текст сообщения: пустые строки, длина и 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", "attachment_ids"} + assert data["chat_id"] == chat["id"] and data["sender_id"] == alice.id + assert data["created_at"].endswith("Z") + assert data["attachment_ids"] == [] 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/README.md b/tests/README.md index 0dfa3f89..ebe9bdd0 100644 --- a/tests/README.md +++ b/tests/README.md @@ -1,3 +1,5 @@ +> Для реализованного стенда lab4 на Windows/Linux используйте команды из [REPORT.md](../REPORT.md): `python tests/smoke.py`, `python tests/resilience.py --mode failover --action-script scripts/stop_one.py` и `python tests/resilience.py --mode persistence --action-script scripts/restart.py`. Make и shell на хосте не нужны. Ниже сохранена исходная памятка публичного skeleton. + # Публичные проверки Python **3.9+**, стандартная библиотека. Сервер уже должен быть запущен; проверки не создают готовое приложение и сами не управляют Docker. `make check` можно выполнить на исходном skeleton, `make test` — только после реализации API. Полная OpenAPI-валидация отдельно: `make validate` после установки `requirements-dev.txt` в venv. diff --git a/tests/client.py b/tests/client.py index f0499a29..7baccfd3 100644 --- a/tests/client.py +++ b/tests/client.py @@ -1,4 +1,5 @@ """HTTP helpers for public black-box tests; Python 3.9+, standard library only.""" + import base64 import datetime as dt import hashlib @@ -13,205 +14,336 @@ import urllib.request import uuid ROOT = Path(__file__).resolve().parents[1] -LAB = json.loads((ROOT / 'lab.json').read_text())['number'] -SPEC = json.loads((ROOT / 'contracts/openapi.json').read_text()) +LAB = json.loads((ROOT / "lab.json").read_text())["number"] +SPEC = json.loads((ROOT / "contracts/openapi.json").read_text(encoding="utf-8")) + class Failure(AssertionError): pass + def check(condition, message): if not condition: raise Failure(message) -def timestamp(value): - check(isinstance(value, str) and value.endswith('Z'), 'timestamp must be UTC ending in Z') - try: - return dt.datetime.fromisoformat(value[:-1] + '+00:00') - except ValueError as exc: - raise Failure('invalid RFC3339 timestamp') from exc -def validate(value, schema, where='response'): +def timestamp(value): + check( + isinstance(value, str) and value.endswith("Z"), + "timestamp must be UTC ending in Z", + ) + try: + return dt.datetime.fromisoformat(value[:-1] + "+00:00") + except ValueError as exc: + raise Failure("invalid RFC3339 timestamp") from exc + + +def validate(value, schema, where="response"): """Validate the JSON Schema subset used by the supplied contract, not arbitrary OpenAPI.""" - if '$ref' in schema: + if "$ref" in schema: target = SPEC - for part in schema['$ref'].split('/')[1:]: + for part in schema["$ref"].split("/")[1:]: target = target[part] return validate(value, target, where) - kinds = schema.get('type', []) + kinds = schema.get("type", []) if isinstance(kinds, str): kinds = [kinds] - matches = {'null':value is None, 'boolean':isinstance(value,bool), 'integer':isinstance(value,int) and not isinstance(value,bool), 'number':isinstance(value,(int,float)) and not isinstance(value,bool), 'string':isinstance(value,str), 'array':isinstance(value,list), 'object':isinstance(value,dict)} + matches = { + "null": value is None, + "boolean": isinstance(value, bool), + "integer": isinstance(value, int) and not isinstance(value, bool), + "number": isinstance(value, (int, float)) and not isinstance(value, bool), + "string": isinstance(value, str), + "array": isinstance(value, list), + "object": isinstance(value, dict), + } if kinds: - check(any(matches.get(k,False) for k in kinds), where + ': incorrect type') - if 'enum' in schema: - check(value in schema['enum'], where + ': invalid enum') - if 'const' in schema: - check(value == schema['const'], where + ': invalid const') - if isinstance(value,dict): - props = schema.get('properties',{}) - check(all(k in value for k in schema.get('required',[])), where + ': missing required fields') - if schema.get('additionalProperties') is False: - check(not set(value).difference(props), where + ': unexpected fields') + check(any(matches.get(k, False) for k in kinds), where + ": incorrect type") + if "enum" in schema: + check(value in schema["enum"], where + ": invalid enum") + if "const" in schema: + check(value == schema["const"], where + ": invalid const") + if isinstance(value, dict): + props = schema.get("properties", {}) + check( + all(k in value for k in schema.get("required", [])), + where + ": missing required fields", + ) + if schema.get("additionalProperties") is False: + check(not set(value).difference(props), where + ": unexpected fields") for key, sub in props.items(): if key in value: - validate(value[key], sub, where + '.' + key) - if isinstance(value,list): - check(len(value) >= schema.get('minItems',0), where + ': too few items') - check(len(value) <= schema.get('maxItems',float('inf')), where + ': too many items') - if schema.get('uniqueItems'): - encoded = [json.dumps(x,sort_keys=True) for x in value] - check(len(encoded)==len(set(encoded)), where + ': duplicate items') - if 'items' in schema: + validate(value[key], sub, where + "." + key) + if isinstance(value, list): + check(len(value) >= schema.get("minItems", 0), where + ": too few items") + check( + len(value) <= schema.get("maxItems", float("inf")), + where + ": too many items", + ) + if schema.get("uniqueItems"): + encoded = [json.dumps(x, sort_keys=True) for x in value] + check(len(encoded) == len(set(encoded)), where + ": duplicate items") + if "items" in schema: for item in value: - validate(item, schema['items'], where + '[]') - if isinstance(value,str): - check(len(value) >= schema.get('minLength',0), where + ': too short') - check(len(value) <= schema.get('maxLength',float('inf')), where + ': too long') - if 'pattern' in schema: - check(re.search(schema['pattern'], value) is not None, where + ': pattern mismatch') - if schema.get('format') == 'uuid': + validate(item, schema["items"], where + "[]") + if isinstance(value, str): + check(len(value) >= schema.get("minLength", 0), where + ": too short") + check(len(value) <= schema.get("maxLength", float("inf")), where + ": too long") + if "pattern" in schema: + check( + re.search(schema["pattern"], value) is not None, + where + ": pattern mismatch", + ) + if schema.get("format") == "uuid": try: uuid.UUID(value) except ValueError as exc: - raise Failure(where + ': invalid UUID') from exc - if schema.get('format') == 'date-time': + raise Failure(where + ": invalid UUID") from exc + if schema.get("format") == "date-time": timestamp(value) - if isinstance(value,(int,float)) and not isinstance(value,bool): - check(value >= schema.get('minimum',-float('inf')), where + ': below minimum') - check(value <= schema.get('maximum',float('inf')), where + ': above maximum') - if 'anyOf' in schema: - for alternative in schema['anyOf']: + if isinstance(value, (int, float)) and not isinstance(value, bool): + check(value >= schema.get("minimum", -float("inf")), where + ": below minimum") + check(value <= schema.get("maximum", float("inf")), where + ": above maximum") + if "anyOf" in schema: + for alternative in schema["anyOf"]: try: validate(value, alternative, where) break except Failure: pass else: - raise Failure(where + ': no anyOf branch matches') + raise Failure(where + ": no anyOf branch matches") + def shape(value, name): - validate(value, SPEC['components']['schemas'][name]) + validate(value, SPEC["components"]["schemas"][name]) return value + class NoRedirect(urllib.request.HTTPRedirectHandler): def redirect_request(self, req, fp, code, msg, headers, newurl): return None + class Client: def __init__(self, base_url=None, ca_file=None, timeout=10): - self.base = (base_url or os.environ.get('BASE_URL') or ('https://localhost:8443' if LAB==7 else 'http://localhost:8080')).rstrip('/') - check(self.base.startswith(('http://','https://')), 'BASE_URL must be HTTP(S)') - check(LAB != 7 or self.base.startswith('https://'), 'lab7 requires HTTPS') + self.base = ( + base_url + or os.environ.get("BASE_URL") + or ("https://localhost:8443" if LAB == 7 else "http://localhost:8080") + ).rstrip("/") + check(self.base.startswith(("http://", "https://")), "BASE_URL must be HTTP(S)") + check(LAB != 7 or self.base.startswith("https://"), "lab7 requires HTTPS") self.timeout = timeout - context = ssl.create_default_context(cafile=ca_file or os.environ.get('CA_FILE') or None) - self.opener = urllib.request.build_opener(NoRedirect(), urllib.request.HTTPSHandler(context=context)) + context = ssl.create_default_context( + cafile=ca_file or os.environ.get("CA_FILE") or None + ) + self.opener = urllib.request.build_opener( + NoRedirect(), urllib.request.HTTPSHandler(context=context) + ) - def call(self, method, path, expected=200, body=None, actor=None, headers=None, raw=None, schema=None): - h = {'Accept':'application/json'} + def call( + self, + method, + path, + expected=200, + body=None, + actor=None, + headers=None, + raw=None, + schema=None, + ): + h = {"Accept": "application/json"} if actor: - h['X-User-Id' if LAB==1 else 'Authorization'] = actor['id'] if LAB==1 else 'Bearer ' + actor['token'] + h["X-User-Id" if LAB == 1 else "Authorization"] = ( + actor["id"] if LAB == 1 else "Bearer " + actor["token"] + ) if body is not None: - raw = json.dumps(body,ensure_ascii=False).encode('utf-8') - h['Content-Type'] = 'application/json' + raw = json.dumps(body, ensure_ascii=False).encode("utf-8") + h["Content-Type"] = "application/json" h.update(headers or {}) - req = urllib.request.Request(self.base + path, data=raw, headers=h, method=method) + req = urllib.request.Request( + self.base + path, data=raw, headers=h, method=method + ) start = time.monotonic() try: response = self.opener.open(req, timeout=self.timeout) except urllib.error.HTTPError as error: response = error - except (urllib.error.URLError,TimeoutError,OSError) as exc: + except (urllib.error.URLError, TimeoutError, OSError) as exc: # Do not print URL query, headers, request body, or credentials. - raise Failure(method + ' ' + path.split('?')[0] + ': connection/TLS failure (' + type(exc).__name__ + ')') from None + raise Failure( + method + + " " + + path.split("?")[0] + + ": connection/TLS failure (" + + type(exc).__name__ + + ")" + ) from None with response: status = response.code rh = response.headers - data = response.read(16*1024*1024 + 1) - check(len(data) <= 16*1024*1024, 'response exceeds test safety limit') - allowed = [expected] if isinstance(expected,int) else expected - check(status in allowed, method + ' ' + path.split('?')[0] + ': expected ' + str(allowed) + ', received ' + str(status)) - check(bool(rh.get('X-Request-Id')), 'missing X-Request-Id') - if LAB>=3: - check(bool(rh.get('X-Instance-Id')), 'missing X-Instance-Id') + data = response.read(16 * 1024 * 1024 + 1) + check(len(data) <= 16 * 1024 * 1024, "response exceeds test safety limit") + allowed = [expected] if isinstance(expected, int) else expected + check( + status in allowed, + method + + " " + + path.split("?")[0] + + ": expected " + + str(allowed) + + ", received " + + str(status), + ) + check(bool(rh.get("X-Request-Id")), "missing X-Request-Id") + if LAB >= 3: + check(bool(rh.get("X-Instance-Id")), "missing X-Instance-Id") payload = None - if schema or status>=400: - check(rh.get_content_type()=='application/json', 'expected application/json') + if schema or status >= 400: + check( + rh.get_content_type() == "application/json", "expected application/json" + ) try: payload = json.loads(data) - except (ValueError,UnicodeError): - raise Failure('invalid response JSON') from None - shape(payload, 'Error' if status>=400 else schema) - if status>=400: - check(payload['error']['request_id']==rh.get('X-Request-Id'),'error request_id differs from header') - if status==401 and LAB>=2: - scheme = 'Basic' if path=='/api/v1/auth/sessions' else 'Bearer' - check(rh.get('WWW-Authenticate','').lower().startswith(scheme.lower()), 'incorrect WWW-Authenticate challenge') - if status==204: - check(not data, '204 must not contain body') - return {'status':status,'headers':rh,'json':payload,'bytes':data,'elapsed':time.monotonic()-start} + except (ValueError, UnicodeError): + raise Failure("invalid response JSON") from None + shape(payload, "Error" if status >= 400 else schema) + if status >= 400: + check( + payload["error"]["request_id"] == rh.get("X-Request-Id"), + "error request_id differs from header", + ) + if status == 401 and LAB >= 2: + scheme = "Basic" if path == "/api/v1/auth/sessions" else "Bearer" + check( + rh.get("WWW-Authenticate", "").lower().startswith(scheme.lower()), + "incorrect WWW-Authenticate challenge", + ) + if status == 204: + check(not data, "204 must not contain body") + return { + "status": status, + "headers": rh, + "json": payload, + "bytes": data, + "elapsed": time.monotonic() - start, + } def user(self, label): - username = 'u_' + label + '_' + uuid.uuid4().hex[:12] - password = 'T3st-' + uuid.uuid4().hex - body = {'username':username,'display_name':'Студент ' + label} - if LAB>=2: - body['password'] = password - user = self.call('POST','/api/v1/users',201,body=body,schema='User')['json'] - check(user['username']==username,'username changed') - actor = {'id':user['id'],'user':user,'registration':body} - if LAB>=2: - credential = base64.b64encode((username+':'+password).encode()).decode() - session = self.call('POST','/api/v1/auth/sessions',201,headers={'Authorization':'Basic '+credential},schema='Session')['json'] - check(session['user']==user,'session user mismatch') - check(timestamp(session['expires_at']) > dt.datetime.now(dt.timezone.utc),'session already expired') - actor['token'] = session['token'] + username = "u_" + label + "_" + uuid.uuid4().hex[:12] + password = "T3st-" + uuid.uuid4().hex + body = {"username": username, "display_name": "Студент " + label} + if LAB >= 2: + body["password"] = password + user = self.call("POST", "/api/v1/users", 201, body=body, schema="User")["json"] + check(user["username"] == username, "username changed") + actor = {"id": user["id"], "user": user, "registration": body} + if LAB >= 2: + credential = base64.b64encode((username + ":" + password).encode()).decode() + session = self.call( + "POST", + "/api/v1/auth/sessions", + 201, + headers={"Authorization": "Basic " + credential}, + schema="Session", + )["json"] + check(session["user"] == user, "session user mismatch") + check( + timestamp(session["expires_at"]) > dt.datetime.now(dt.timezone.utc), + "session already expired", + ) + actor["token"] = session["token"] return actor def bootstrap(self): - self.call('GET','/health/live',schema='Health') - self.call('GET','/health/ready',schema='Health') - alice, bob, eve = (self.user(label) for label in ('alice','bob','eve')) - chat = self.call('POST','/api/v1/chats',201,actor=alice,body={'title':'Контрактный тест','member_ids':[alice['id'],bob['id']]},schema='Chat')['json'] - check(set(chat['member_ids'])=={alice['id'],bob['id']},'chat member mismatch') - return alice,bob,eve,chat + self.call("GET", "/health/live", schema="Health") + self.call("GET", "/health/ready", schema="Health") + alice, bob, eve = (self.user(label) for label in ("alice", "bob", "eve")) + chat = self.call( + "POST", + "/api/v1/chats", + 201, + actor=alice, + body={"title": "Контрактный тест", "member_ids": [alice["id"], bob["id"]]}, + schema="Chat", + )["json"] + check( + set(chat["member_ids"]) == {alice["id"], bob["id"]}, "chat member mismatch" + ) + return alice, bob, eve, chat def message(self, actor, chat, text, key=None, attachments=None): - body = {'text':text} + body = {"text": text} if attachments is not None: - body['attachment_ids'] = attachments - headers = {'Idempotency-Key':key or uuid.uuid4().hex} if LAB>=3 else {} - return self.call('POST','/api/v1/chats/'+chat['id']+'/messages',201,actor=actor,body=body,headers=headers,schema='Message') + body["attachment_ids"] = attachments + headers = {"Idempotency-Key": key or uuid.uuid4().hex} if LAB >= 3 else {} + return self.call( + "POST", + "/api/v1/chats/" + chat["id"] + "/messages", + 201, + actor=actor, + body=body, + headers=headers, + schema="Message", + ) def attachment(self, actor, chat): - content = (ROOT / 'tests/fixtures/sample.png').read_bytes() - att = self.call('POST','/api/v1/chats/'+chat['id']+'/attachments?filename=sample.png',201 if LAB==4 else 202,actor=actor,raw=content,headers={'Content-Type':'image/png'},schema='Attachment')['json'] - check(att['status']==('ready' if LAB==4 else 'queued'),'incorrect upload status') - check(att['chat_id']==chat['id'] and att['owner_id']==actor['id'],'attachment ownership mismatch') - check(att['sha256']==hashlib.sha256(content).hexdigest() and att['size_bytes']==len(content),'original checksum/size mismatch') - return att,content + content = (ROOT / "tests/fixtures/sample.png").read_bytes() + att = self.call( + "POST", + "/api/v1/chats/" + chat["id"] + "/attachments?filename=sample.png", + 201 if LAB == 4 else 202, + actor=actor, + raw=content, + headers={"Content-Type": "image/png"}, + schema="Attachment", + )["json"] + check( + att["status"] == ("ready" if LAB == 4 else "queued"), + "incorrect upload status", + ) + check( + att["chat_id"] == chat["id"] and att["owner_id"] == actor["id"], + "attachment ownership mismatch", + ) + check( + att["sha256"] == hashlib.sha256(content).hexdigest() + and att["size_bytes"] == len(content), + "original checksum/size mismatch", + ) + return att, content def wait_ready(self, actor, att, wait_seconds=60): - deadline = time.monotonic()+wait_seconds - while time.monotonic()=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) + proc = subprocess.Popen(hook_command(script),env=env,stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL) started = time.monotonic() successes,failures,after_instances = [],0,[] try: diff --git a/unit-tests/test_attachment_intent.py b/unit-tests/test_attachment_intent.py new file mode 100644 index 00000000..1b19c5d3 --- /dev/null +++ b/unit-tests/test_attachment_intent.py @@ -0,0 +1,27 @@ +"""An expired upload must not PUT after cleanup has removed its intent.""" + +import asyncio +from types import SimpleNamespace +from unittest.mock import AsyncMock, MagicMock +import uuid + +import pytest + +from app.attachments import save_attachment +from app.errors import ApiError + + +def test_removed_intent_prevents_put_when_uploader_resumes(): + session = AsyncMock() + session.add = MagicMock() + query = MagicMock() + query.scalar_one_or_none.return_value = None + session.execute.return_value = query + storage = MagicMock() + row = SimpleNamespace(id=uuid.uuid4()) + with pytest.raises(ApiError) as error: + asyncio.run(save_attachment(session, storage, row, None)) + assert error.value.status == 409 + storage.put.assert_not_called() + session.rollback.assert_awaited_once() + session.commit.assert_awaited_once() diff --git a/unit-tests/test_message_transaction.py b/unit-tests/test_message_transaction.py new file mode 100644 index 00000000..9fb165cf --- /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", request_attachment_ids=[]))] + 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", request_attachment_ids=[], 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", request_attachment_ids=[], 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 diff --git a/unit-tests/test_operator_scripts.py b/unit-tests/test_operator_scripts.py new file mode 100644 index 00000000..b6de1d70 --- /dev/null +++ b/unit-tests/test_operator_scripts.py @@ -0,0 +1,91 @@ +"""Операторские сценарии: две реальные реплики и переносимый запуск hooks.""" + +from contextlib import nullcontext +import importlib +from pathlib import Path +import sys +from types import SimpleNamespace +import unittest +from unittest.mock import patch + +from scripts import replicas + + +class BalancedReplicasTests(unittest.TestCase): + def check_responses(self, responses, *, times_out=False): + clock = [0.0] + current_ids = {"api-a": "current-a", "api-b": "current-b"} + iterator = iter(responses) + + def reply(*args, **kwargs): + instance = next(iterator) + return nullcontext( + SimpleNamespace(status=200, headers={"X-Instance-Id": instance}) + ) + + def sleep(seconds): + clock[0] += seconds + + with ( + patch.object( + replicas, + "wait_ready", + side_effect=lambda name, **kw: {"instance_id": current_ids[name]}, + ) as readiness, + patch.object(replicas.time, "monotonic", side_effect=lambda: clock[0]), + patch.object(replicas.time, "sleep", side_effect=sleep), + patch("urllib.request.urlopen", side_effect=reply) as requests, + ): + if times_out: + with self.assertRaisesRegex(RuntimeError, "both current ready"): + replicas.wait_balanced(timeout=0.3) + else: + replicas.wait_balanced(timeout=1) + self.assertEqual( + {c.args[0] for c in readiness.call_args_list}, set(current_ids) + ) + return requests.call_count + + def test_one_ready_replica_never_counts_as_balanced(self): + self.assertEqual(self.check_responses(["current-a"] * 3, times_out=True), 1) + + def test_waits_until_both_replicas_answer_through_lb(self): + self.assertEqual( + self.check_responses(["current-a", "current-a", "current-b"]), 3 + ) + + def test_old_process_alias_does_not_replace_current_replica(self): + self.assertEqual( + self.check_responses(["current-a", "old-b", "current-a", "current-b"]), 4 + ) + + +class HookCommandTests(unittest.TestCase): + @classmethod + def setUpClass(cls): + # Public helpers use sibling imports when invoked as command-line scripts. + public_tests = str(Path(__file__).resolve().parents[1] / "tests") + with patch.object(sys, "path", [public_tests, *sys.path]): + cls.resilience = importlib.import_module("resilience") + + def test_python_hook_uses_current_interpreter_and_preserves_spaces(self): + script = Path("directory with spaces/restart.py") + self.assertEqual( + self.resilience.hook_command(script), [sys.executable, str(script)] + ) + + def test_shell_is_resolved_from_path(self): + with patch.object(self.resilience.shutil, "which", return_value="/custom/bash"): + self.assertEqual( + self.resilience.hook_command(Path("restart.sh")), + ["/custom/bash", "restart.sh"], + ) + + def test_missing_shell_reports_failure(self): + with patch.object(self.resilience.shutil, "which", return_value=None): + with self.assertRaisesRegex(self.resilience.Failure, "Python action hook"): + self.resilience.hook_command(Path("restart.sh")) + + +if __name__ == "__main__": + unittest.main() -- 2.54.0 From 20b776cceb4bd20da6c6619a3deb11b56d3e3f70 Mon Sep 17 00:00:00 2001 From: k00kza <102+k00kza@noreply.gitea.privcloud.tech> Date: Fri, 6 Nov 2026 13:58:56 +0300 Subject: [PATCH 2/2] =?UTF-8?q?=D0=9E=D0=B1=D0=BD=D0=BE=D0=B2=D0=B8=D1=82?= =?UTF-8?q?=D1=8C=20REPORT.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- REPORT.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/REPORT.md b/REPORT.md index e7debe05..446ed53e 100644 --- a/REPORT.md +++ b/REPORT.md @@ -2,7 +2,7 @@ - Автор, группа: Антонова Ирина Андреевна, БПИ2301. - Целевая оценка: 4; Compose + Nginx, две API-реплики, MinIO как S3-хранилище. Трек 5 не заявлен. -- Commit/PR: ⟦хеш коммита / ссылка на PR — заполнить после commit и push⟧ +- Commit/PR: 8d001712fe40bed7b16b1d26683360299f13a8fb - Использованные источники, библиотеки и помощь ИИ: FastAPI, SQLAlchemy async, asyncpg, Pydantic, Uvicorn, redis-py, minio-py, Pillow, pytest. lab2 подготовлена с Claude, перенос lab3 — с Codex; основной код lab4 написан с Codex; правки выполнены с Claude. Основа: lab3 без изменений бизнес-логики. ## Запуск из чистого clone -- 2.54.0