forked from mtusi2026/mtusi2026lab2
ЛР 2 — БВТ2304 — Черников
PostgreSQL с версионируемыми миграциями, вход по Basic и Bearer-сессии в Valkey (AOF), Argon2id, compose-стенд с секретами в файлах. Собственные тесты в student-tests/, проверки инфраструктуры и EXPLAIN в scripts/, отчёт в REPORT.md, результаты в evidence/, команды для показа в DEMO.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
# Build and test from a fresh clone of the solution branch, separate compose project with new volumes.
|
||||
# Date: 2026-10-07 16:07 UTC; host Windows 11, Docker Desktop; clone made with core.autocrlf=true (as on the author machine).
|
||||
# COMPOSE_PROJECT_NAME=mtusi-lab2-clean, APP_PUBLISHED_PORT=8090, so the main stand on 8080 is not touched.
|
||||
|
||||
$ git clone --branch solution/bvt2304-chernikov <repo> clean2
|
||||
[exit code 0]
|
||||
|
||||
$ git log --oneline -1
|
||||
5b4fa0e ЛР 2 — БВТ2304 — Черников
|
||||
[exit code 0]
|
||||
|
||||
$ python scripts/init_secrets.py
|
||||
created secrets/postgres_password
|
||||
created secrets/valkey_password
|
||||
written secrets/database_url, secrets/valkey_auth, secrets/cache_url
|
||||
[exit code 0]
|
||||
|
||||
$ cp .env.example .env
|
||||
|
||||
$ docker compose up -d --build --wait
|
||||
Network mtusi-lab2-clean_default Created
|
||||
Network mtusi-lab2-clean_default Created
|
||||
Container mtusi-lab2-clean-valkey-1 Created
|
||||
Container mtusi-lab2-clean-postgres-1 Created
|
||||
Container mtusi-lab2-clean-migrate-1 Created
|
||||
Container mtusi-lab2-clean-api-1 Created
|
||||
Container mtusi-lab2-clean-postgres-1 Healthy
|
||||
Container mtusi-lab2-clean-postgres-1 Healthy
|
||||
Container mtusi-lab2-clean-valkey-1 Healthy
|
||||
Container mtusi-lab2-clean-migrate-1 Exited
|
||||
Container mtusi-lab2-clean-migrate-1 Exited
|
||||
Container mtusi-lab2-clean-valkey-1 Healthy
|
||||
Container mtusi-lab2-clean-postgres-1 Healthy
|
||||
Container mtusi-lab2-clean-api-1 Healthy
|
||||
[exit code 0]
|
||||
|
||||
$ docker compose logs --no-log-prefix migrate
|
||||
INFO event=migration_applied version=0001_schema duration_ms=6
|
||||
INFO event=migration_applied version=0002_messages_history_index duration_ms=3
|
||||
INFO event=migrations_done applied_now=2 total=2
|
||||
[exit code 0]
|
||||
|
||||
$ docker compose run --rm --no-deps migrate
|
||||
INFO event=migrations_done applied_now=0 total=2
|
||||
[exit code 0]
|
||||
|
||||
$ python tests/smoke.py --base-url http://127.0.0.1:8090
|
||||
PASS: lab2 public API scenario; infrastructure and grading evidence remain separate.
|
||||
[exit code 0]
|
||||
|
||||
$ BASE_URL=http://127.0.0.1:8090 python -m unittest discover -s student-tests
|
||||
Ran 73 tests in 10.872s
|
||||
OK
|
||||
[exit code 0]
|
||||
|
||||
$ python tests/resilience.py --mode persistence --action-script scripts/restart.cmd --base-url http://127.0.0.1:8090
|
||||
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]
|
||||
|
||||
$ docker compose down -v (removes only the mtusi-lab2-clean project and its volumes)
|
||||
Volume mtusi-lab2-clean_pgdata Removing
|
||||
Volume mtusi-lab2-clean_valkeydata Removing
|
||||
Volume mtusi-lab2-clean_pgdata Removed
|
||||
Volume mtusi-lab2-clean_valkeydata Removed
|
||||
[exit code 0]
|
||||
|
||||
# exit codes: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
||||
RESULT: PASS
|
||||
@@ -0,0 +1,96 @@
|
||||
# 2026-10-07 16:04:34 UTC, PostgreSQL 18.6 (Debian 18.6-1.pgdg13+2) on x86_64-pc-linux-gnu, compiled by gcc (Debian 14.2.0-19) 14.2.0, 64-bit
|
||||
# target chat 5c31bb3d-a9e4-4c7c-a268-d15c483a2d25: 10000 messages; noise chat 0d65d937-959b-44c6-a012-df0c5a5ad525: 20000 messages
|
||||
# inserted and analyzed in 0.6 s
|
||||
messages total: 30302, in target chat: 10000
|
||||
|
||||
== 1. First page, middle page and last page: index range scan, no sort ==
|
||||
EXPLAIN (ANALYZE, BUFFERS, COSTS OFF) SELECT id, chat_id, sender_id, text, created_at FROM messages WHERE chat_id = '5c31bb3d-a9e4-4c7c-a268-d15c483a2d25' ORDER BY created_at, id LIMIT 51;
|
||||
QUERY PLAN
|
||||
------------------------------------------------------------------------------------------------------------
|
||||
Limit (actual time=0.036..0.048 rows=51.00 loops=1)
|
||||
Buffers: shared hit=4
|
||||
-> Index Scan using messages_chat_history_idx on messages (actual time=0.035..0.041 rows=51.00 loops=1)
|
||||
Index Cond: (chat_id = '5c31bb3d-a9e4-4c7c-a268-d15c483a2d25'::uuid)
|
||||
Index Searches: 1
|
||||
Buffers: shared hit=4
|
||||
Planning:
|
||||
Buffers: shared hit=112
|
||||
Planning Time: 0.414 ms
|
||||
Execution Time: 0.069 ms
|
||||
(10 rows)
|
||||
|
||||
EXPLAIN (ANALYZE, BUFFERS, COSTS OFF) SELECT id, chat_id, sender_id, text, created_at FROM messages WHERE chat_id = '5c31bb3d-a9e4-4c7c-a268-d15c483a2d25' AND (created_at, id) > ('2026-10-07 16:04:39.873383+00', 'fa6a0ba6-382c-459f-bab6-feab2ba3c2d3') ORDER BY created_at, id LIMIT 51;
|
||||
QUERY PLAN
|
||||
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
Limit (actual time=0.014..0.021 rows=51.00 loops=1)
|
||||
Buffers: shared hit=5
|
||||
-> Index Scan using messages_chat_history_idx on messages (actual time=0.013..0.018 rows=51.00 loops=1)
|
||||
Index Cond: ((chat_id = '5c31bb3d-a9e4-4c7c-a268-d15c483a2d25'::uuid) AND (ROW(created_at, id) > ROW('2026-10-07 16:04:39.873383+00'::timestamp with time zone, 'fa6a0ba6-382c-459f-bab6-feab2ba3c2d3'::uuid)))
|
||||
Index Searches: 1
|
||||
Buffers: shared hit=5
|
||||
Planning Time: 0.048 ms
|
||||
Execution Time: 0.041 ms
|
||||
(8 rows)
|
||||
|
||||
EXPLAIN (ANALYZE, BUFFERS, COSTS OFF) SELECT id, chat_id, sender_id, text, created_at FROM messages WHERE chat_id = '5c31bb3d-a9e4-4c7c-a268-d15c483a2d25' AND (created_at, id) > ('2026-10-07 16:04:44.872196+00', '2db6f051-d2f0-4608-a584-347e2b610391') ORDER BY created_at, id LIMIT 51;
|
||||
QUERY PLAN
|
||||
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
Limit (actual time=0.021..0.026 rows=20.00 loops=1)
|
||||
Buffers: shared hit=4
|
||||
-> Index Scan using messages_chat_history_idx on messages (actual time=0.020..0.023 rows=20.00 loops=1)
|
||||
Index Cond: ((chat_id = '5c31bb3d-a9e4-4c7c-a268-d15c483a2d25'::uuid) AND (ROW(created_at, id) > ROW('2026-10-07 16:04:44.872196+00'::timestamp with time zone, '2db6f051-d2f0-4608-a584-347e2b610391'::uuid)))
|
||||
Index Searches: 1
|
||||
Buffers: shared hit=4
|
||||
Planning Time: 0.061 ms
|
||||
Execution Time: 0.038 ms
|
||||
(8 rows)
|
||||
|
||||
== 2. Generic plan of the parameterized query (how a prepared statement with $1..$4 runs) ==
|
||||
SET plan_cache_mode = force_generic_plan;
|
||||
SET
|
||||
PREPARE history_page(uuid, timestamptz, uuid, int) AS
|
||||
SELECT id, chat_id, sender_id, text, created_at FROM messages
|
||||
WHERE chat_id = $1 AND (created_at, id) > ($2, $3) ORDER BY created_at, id LIMIT $4;
|
||||
PREPARE
|
||||
EXPLAIN (ANALYZE, BUFFERS, COSTS OFF) EXECUTE history_page('5c31bb3d-a9e4-4c7c-a268-d15c483a2d25', '2026-10-07 16:04:39.873383+00', 'fa6a0ba6-382c-459f-bab6-feab2ba3c2d3', 51);
|
||||
QUERY PLAN
|
||||
------------------------------------------------------------------------------------------------------------
|
||||
Limit (actual time=0.021..0.030 rows=51.00 loops=1)
|
||||
Buffers: shared hit=5
|
||||
-> Index Scan using messages_chat_history_idx on messages (actual time=0.020..0.025 rows=51.00 loops=1)
|
||||
Index Cond: ((chat_id = $1) AND (ROW(created_at, id) > ROW($2, $3)))
|
||||
Index Searches: 1
|
||||
Buffers: shared hit=5
|
||||
Planning:
|
||||
Buffers: shared hit=111
|
||||
Planning Time: 0.356 ms
|
||||
Execution Time: 0.064 ms
|
||||
(10 rows)
|
||||
|
||||
== 3. For comparison: the same middle page without the index (rolled back) ==
|
||||
BEGIN;
|
||||
BEGIN
|
||||
DROP INDEX messages_chat_history_idx;
|
||||
DROP INDEX
|
||||
EXPLAIN (ANALYZE, BUFFERS, COSTS OFF) SELECT id, chat_id, sender_id, text, created_at FROM messages WHERE chat_id = '5c31bb3d-a9e4-4c7c-a268-d15c483a2d25' AND (created_at, id) > ('2026-10-07 16:04:39.873383+00', 'fa6a0ba6-382c-459f-bab6-feab2ba3c2d3') ORDER BY created_at, id LIMIT 51;
|
||||
QUERY PLAN
|
||||
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
Limit (actual time=1.944..1.949 rows=51.00 loops=1)
|
||||
Buffers: shared hit=416
|
||||
-> Sort (actual time=1.942..1.944 rows=51.00 loops=1)
|
||||
Sort Key: created_at, id
|
||||
Sort Method: top-N heapsort Memory: 31kB
|
||||
Buffers: shared hit=416
|
||||
-> Seq Scan on messages (actual time=0.315..1.677 rows=5000.00 loops=1)
|
||||
Filter: ((chat_id = '5c31bb3d-a9e4-4c7c-a268-d15c483a2d25'::uuid) AND (ROW(created_at, id) > ROW('2026-10-07 16:04:39.873383+00'::timestamp with time zone, 'fa6a0ba6-382c-459f-bab6-feab2ba3c2d3'::uuid)))
|
||||
Rows Removed by Filter: 25302
|
||||
Buffers: shared hit=410
|
||||
Planning:
|
||||
Buffers: shared hit=83
|
||||
Planning Time: 0.271 ms
|
||||
Execution Time: 1.976 ms
|
||||
(14 rows)
|
||||
|
||||
ROLLBACK;
|
||||
ROLLBACK
|
||||
index still exists: true
|
||||
@@ -0,0 +1,238 @@
|
||||
# 2026-10-07 16:03:13 UTC, python 3.14.7, win32
|
||||
$ docker version --format docker {{.Server.Version}}
|
||||
# docker 29.8.1
|
||||
== Stack: services, pinned images, health, non-root, Valkey persistence ==
|
||||
$ docker compose ps -a --format table {{.Service}} {{.Image}} {{.Status}}
|
||||
SERVICE IMAGE STATUS
|
||||
api mtusi-messenger:lab2 Up 58 seconds (healthy)
|
||||
migrate mtusi-messenger:lab2 Exited (0) 59 seconds ago
|
||||
postgres postgres:18.6-trixie@sha256:74935e72241653ca55e0414067e6d8763aceb8a810eb51b452253ec3dcfc4336 Up About a minute (healthy)
|
||||
valkey valkey/valkey:9.0.6-trixie@sha256:63346cb24a61221e76bdf41acce99b3968a9fa83d8122144deab45394b27b4f2 Up About a minute (healthy)
|
||||
$ docker compose config --format json
|
||||
=> PASS: third-party images are pinned by tag and digest (postgres:18.6-trixie@sha256:74935e72241653ca55e0414067e6d8763aceb8a810eb51b452253ec3dcfc4336, valkey/valkey:9.0.6-trixie@sha256:63346cb24a61221e76bdf41acce99b3968a9fa83d8122144deab45394b27b4f2)
|
||||
|
||||
health: api=healthy, postgres=healthy, valkey=healthy
|
||||
=> PASS: api, postgres and valkey are healthy
|
||||
|
||||
$ docker compose exec -T api id
|
||||
uid=10001(app) gid=10001(app) groups=10001(app)
|
||||
=> PASS: API process does not run as root
|
||||
|
||||
$ docker compose exec -T valkey sh -c valkey-cli --no-auth-warning -a "$(sed -n "s/^requirepass //p" /run/secrets/valkey_auth)" CONFIG GET appendonly
|
||||
appendonly
|
||||
yes
|
||||
$ docker compose exec -T valkey sh -c valkey-cli --no-auth-warning -a "$(sed -n "s/^requirepass //p" /run/secrets/valkey_auth)" CONFIG GET appendfsync
|
||||
appendfsync
|
||||
always
|
||||
$ docker compose exec -T valkey sh -c valkey-cli --no-auth-warning -a "$(sed -n "s/^requirepass //p" /run/secrets/valkey_auth)" CONFIG GET maxmemory-policy
|
||||
maxmemory-policy
|
||||
noeviction
|
||||
=> PASS: Valkey persistence is AOF with fsync always, no eviction
|
||||
|
||||
== Migrations: schema, constraints, idempotent re-run ==
|
||||
$ docker compose exec -T postgres psql -U messenger -d messenger -v ON_ERROR_STOP=1 -At -c SELECT format('%s %s %s', conrelid::regclass, contype, conname) FROM pg_constraint WHERE conrelid::regclass::text IN ('users', 'chats', 'chat_members', 'messages') ORDER BY 1
|
||||
chat_members f chat_members_chat_id_fkey
|
||||
chat_members f chat_members_user_id_fkey
|
||||
chat_members n chat_members_chat_id_not_null
|
||||
chat_members n chat_members_position_not_null
|
||||
chat_members n chat_members_user_id_not_null
|
||||
chat_members p chat_members_pkey
|
||||
chat_members u chat_members_position_key
|
||||
chats c chats_title_length
|
||||
chats n chats_created_at_not_null
|
||||
chats n chats_id_not_null
|
||||
chats n chats_title_not_null
|
||||
chats p chats_pkey
|
||||
messages c messages_text_length
|
||||
messages f messages_sender_is_member
|
||||
messages n messages_chat_id_not_null
|
||||
messages n messages_created_at_not_null
|
||||
messages n messages_id_not_null
|
||||
messages n messages_sender_id_not_null
|
||||
messages n messages_text_not_null
|
||||
messages p messages_pkey
|
||||
users c users_display_name_length
|
||||
users c users_username_format
|
||||
users n users_created_at_not_null
|
||||
users n users_display_name_not_null
|
||||
users n users_id_not_null
|
||||
users n users_password_hash_not_null
|
||||
users n users_username_not_null
|
||||
users p users_pkey
|
||||
users u users_username_key
|
||||
$ docker compose exec -T postgres psql -U messenger -d messenger -v ON_ERROR_STOP=1 -At -c SELECT indexrelid::regclass FROM pg_index WHERE indrelid::regclass::text IN ('chat_members', 'messages') ORDER BY 1
|
||||
chat_members_pkey
|
||||
chat_members_position_key
|
||||
chat_members_user_id_idx
|
||||
messages_pkey
|
||||
messages_chat_history_idx
|
||||
$ docker compose exec -T postgres psql -U messenger -d messenger -v ON_ERROR_STOP=1 -At -c SELECT (SELECT count(*) FROM schema_migrations) || ' ' || (SELECT count(*) FROM users)
|
||||
2 62
|
||||
$ docker compose run --rm --no-deps migrate
|
||||
Container mtusi-lab2-migrate-run-a18e9389d033 Creating
|
||||
Container mtusi-lab2-migrate-run-a18e9389d033 Created
|
||||
INFO event=migrations_done applied_now=0 total=2
|
||||
$ docker compose exec -T postgres psql -U messenger -d messenger -v ON_ERROR_STOP=1 -At -c SELECT (SELECT count(*) FROM schema_migrations) || ' ' || (SELECT count(*) FROM users)
|
||||
2 62
|
||||
schema_migrations and users rows before/after: 2 62 / 2 62
|
||||
=> PASS: re-running migrations applies nothing and duplicates nothing
|
||||
|
||||
== Rollback: chat creation fails on the second INSERT ==
|
||||
POST /api/v1/chats with one unknown member -> 400 invalid_request in 0.01 s
|
||||
$ docker compose exec -T postgres psql -U messenger -d messenger -v ON_ERROR_STOP=1 -At -c SELECT count(*) FROM chats WHERE title = 'rollback-probe-2654a5871a'
|
||||
0
|
||||
$ docker compose exec -T postgres psql -U messenger -d messenger -v ON_ERROR_STOP=1 -At -c SELECT count(*) FROM chats c WHERE NOT EXISTS (SELECT 1 FROM chat_members m WHERE m.chat_id = c.id)
|
||||
0
|
||||
$ docker compose logs --no-log-prefix --tail 20 api
|
||||
rows in chats with this title: 0; chats without members in the whole table: 0; rollback logged: True
|
||||
=> PASS: composite chat creation is rolled back as a whole
|
||||
|
||||
== Registration race: 20 parallel requests with one username ==
|
||||
statuses: 201 409 409 409 409 409 409 409 409 409 409 409 409 409 409 409 409 409 409 409
|
||||
$ docker compose exec -T postgres psql -U messenger -d messenger -v ON_ERROR_STOP=1 -At -c SELECT count(*) FROM users WHERE username = 'race_cf2989174348'
|
||||
1
|
||||
rows in users with this username: 1
|
||||
=> PASS: exactly one registration wins, the other 19 get 409
|
||||
|
||||
== Connection pool: 60 parallel clients, at most DB_POOL_SIZE=10 connections to PostgreSQL ==
|
||||
requests: 1019, statuses: {200: 1019}
|
||||
API connections in pg_stat_activity, 11 samples: max 10
|
||||
=> PASS: the pool never opens more than 10 connections and all requests succeed
|
||||
|
||||
== Timeouts: chat row locked for 6 s by another session, API answers 503 after lock_timeout=3s ==
|
||||
$ docker compose exec -T postgres psql -U messenger -d messenger -v ON_ERROR_STOP=1 -At -c "BEGIN; SELECT 1 FROM chats WHERE id = '39c32dc4-57b4-4e7d-bd72-0a66c79c4681' FOR UPDATE; SELECT pg_sleep(6); ROLLBACK;" (in the background)
|
||||
POST message while the chat row is locked -> 503 unavailable in 3.01 s
|
||||
POST message after the lock is released -> 201 in 0.01 s
|
||||
=> PASS: waiting for a lock is bounded: 503 after about 3 s, then writes work again (3.01 s)
|
||||
|
||||
== Logs: useful fields, no message texts, passwords or tokens ==
|
||||
$ docker compose logs --no-log-prefix api
|
||||
$ docker compose logs --no-log-prefix --tail 3 api
|
||||
2026-10-07T16:03:31.696Z INFO request_id=0bfad325574345cb84467a9bf688cd43 method=POST path=/api/v1/auth/sessions status=201 duration_ms=24.2 user_id=628d50fe-65dd-4d80-81ef-e1bc4b771b78
|
||||
2026-10-07T16:03:31.723Z INFO request_id=4e28cb5a57d14d9fb0b0839c99a4c452 method=POST path=/api/v1/chats status=201 duration_ms=4.3 user_id=628d50fe-65dd-4d80-81ef-e1bc4b771b78
|
||||
2026-10-07T16:03:31.754Z INFO request_id=52dc2bcc6bc248c5b5a581e8f405b0bf method=POST path=/api/v1/chats/87f1c2f5-e97b-4bfd-b4b7-26bb0a60d62c/messages status=201 duration_ms=5.3 user_id=628d50fe-65dd-4d80-81ef-e1bc4b771b78
|
||||
searched 1834 log lines; found: nothing
|
||||
=> PASS: logs contain request_id/status/duration and no secrets
|
||||
|
||||
== Session expiry: second API instance with SESSION_TTL_SECONDS=2 ==
|
||||
$ docker compose --profile expiry up -d api-short-ttl
|
||||
Container mtusi-lab2-postgres-1 Running
|
||||
Container mtusi-lab2-valkey-1 Running
|
||||
Container mtusi-lab2-api-short-ttl-1 Creating
|
||||
Container mtusi-lab2-api-short-ttl-1 Created
|
||||
Container mtusi-lab2-postgres-1 Waiting
|
||||
Container mtusi-lab2-postgres-1 Healthy
|
||||
Container mtusi-lab2-migrate-1 Starting
|
||||
Container mtusi-lab2-migrate-1 Started
|
||||
Container mtusi-lab2-migrate-1 Waiting
|
||||
Container mtusi-lab2-postgres-1 Waiting
|
||||
Container mtusi-lab2-valkey-1 Waiting
|
||||
Container mtusi-lab2-postgres-1 Healthy
|
||||
Container mtusi-lab2-valkey-1 Healthy
|
||||
Container mtusi-lab2-migrate-1 Exited
|
||||
Container mtusi-lab2-api-short-ttl-1 Starting
|
||||
Container mtusi-lab2-api-short-ttl-1 Started
|
||||
GET /users/me right after login -> 200 in 0.02 s
|
||||
$ docker compose exec -T valkey sh -c valkey-cli --no-auth-warning -a "$(sed -n "s/^requirepass //p" /run/secrets/valkey_auth)" PTTL session:abfd1b21ab7e148541270050cbe792d4a26a2b73815f9487c01330eb5080b958
|
||||
1767
|
||||
PTTL of the session key in Valkey (ms): 1767
|
||||
GET /users/me 3 s later -> 401 unauthorized in 0.02 s
|
||||
$ docker compose exec -T valkey sh -c valkey-cli --no-auth-warning -a "$(sed -n "s/^requirepass //p" /run/secrets/valkey_auth)" EXISTS session:abfd1b21ab7e148541270050cbe792d4a26a2b73815f9487c01330eb5080b958
|
||||
0
|
||||
session key still exists in Valkey: 0
|
||||
=> PASS: session expires after SESSION_TTL_SECONDS and the key disappears from Valkey
|
||||
|
||||
$ docker compose --profile expiry rm -sf api-short-ttl
|
||||
== Valkey is down: bounded 503, no anonymous access, recovery with the same token ==
|
||||
$ docker compose stop valkey
|
||||
Container mtusi-lab2-valkey-1 Stopping
|
||||
Container mtusi-lab2-valkey-1 Stopped
|
||||
GET /users/me with a valid token -> 503 unavailable in 1.02 s
|
||||
POST /auth/sessions with valid credentials -> 503 unavailable in 1.03 s
|
||||
GET /users/me without a token -> 401 unauthorized in 0.00 s
|
||||
POST /users (needs only PostgreSQL) -> 201 in 0.04 s
|
||||
GET /health/ready -> 503 unavailable in 1.03 s
|
||||
GET /health/live -> 200 in 0.00 s
|
||||
slowest of 3 more authenticated requests: 1.04 s
|
||||
$ docker compose start valkey
|
||||
Container mtusi-lab2-valkey-1 Starting
|
||||
Container mtusi-lab2-valkey-1 Started
|
||||
same token after Valkey is back: 200
|
||||
=> PASS: without Valkey: 503 for sessions, 401 for anonymous, registration still works
|
||||
|
||||
=> PASS: after Valkey restarts the session is still valid (AOF)
|
||||
|
||||
== PostgreSQL is down: bounded 503, recovery without restarting the API ==
|
||||
$ docker compose stop postgres
|
||||
Container mtusi-lab2-postgres-1 Stopping
|
||||
Container mtusi-lab2-postgres-1 Stopped
|
||||
GET /users/me with a valid token -> 503 unavailable in 3.01 s
|
||||
POST /chats -> 503 unavailable in 3.01 s
|
||||
GET /users/me without a token -> 401 unauthorized in 0.02 s
|
||||
GET /health/ready -> 503 unavailable in 3.00 s
|
||||
slowest of 3 more requests: 3.02 s
|
||||
$ docker compose start postgres
|
||||
Container mtusi-lab2-postgres-1 Starting
|
||||
Container mtusi-lab2-postgres-1 Started
|
||||
same token after PostgreSQL is back: 200
|
||||
=> PASS: without PostgreSQL: bounded 503, anonymous still 401
|
||||
|
||||
=> PASS: API recovers by itself when PostgreSQL is back
|
||||
|
||||
== Full restart: public tests/resilience.py with the restart hook ==
|
||||
$ docker volume inspect -f {{.Name}} {{.CreatedAt}} mtusi-lab2_pgdata mtusi-lab2_valkeydata
|
||||
mtusi-lab2_pgdata 2026-10-07T16:01:52Z
|
||||
mtusi-lab2_valkeydata 2026-10-07T16:01:52Z
|
||||
$ C:\Users\Dima Chernikov\AppData\Local\Programs\Python\Python314\python.exe tests/resilience.py --mode persistence --action-script scripts/restart.cmd --base-url http://127.0.0.1:8080
|
||||
Executing explicit persistence hook; hook output suppressed to avoid leaking secrets.␍
|
||||
PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence.
|
||||
$ docker volume inspect -f {{.Name}} {{.CreatedAt}} mtusi-lab2_pgdata mtusi-lab2_valkeydata
|
||||
mtusi-lab2_pgdata 2026-10-07T16:01:52Z
|
||||
mtusi-lab2_valkeydata 2026-10-07T16:01:52Z
|
||||
api StartedAt 2026-10-07T16:02:15.275449332Z -> 2026-10-07T16:04:16.634415601Z
|
||||
postgres StartedAt 2026-10-07T16:04:06.046215674Z -> 2026-10-07T16:04:10.037996963Z
|
||||
valkey StartedAt 2026-10-07T16:03:46.643248943Z -> 2026-10-07T16:04:10.103443189Z
|
||||
=> PASS: resilience test passes after restarting api, postgres and valkey with the same volumes (hook scripts/restart.cmd, 10.5 s)
|
||||
|
||||
== SIGTERM: docker compose stop api ==
|
||||
$ docker compose stop -t 10 api
|
||||
Container mtusi-lab2-api-1 Stopping
|
||||
Container mtusi-lab2-api-1 Stopped
|
||||
$ docker compose logs --no-log-prefix --tail 2 api
|
||||
2026-10-07T16:04:18.852Z INFO event=shutdown_started signal=SIGTERM
|
||||
2026-10-07T16:04:19.121Z INFO event=shutdown_finished clean=True duration_ms=269
|
||||
$ docker compose start api
|
||||
Container mtusi-lab2-postgres-1 Waiting
|
||||
Container mtusi-lab2-postgres-1 Healthy
|
||||
Container mtusi-lab2-migrate-1 Starting
|
||||
Container mtusi-lab2-migrate-1 Started
|
||||
Container mtusi-lab2-migrate-1 Waiting
|
||||
Container mtusi-lab2-postgres-1 Waiting
|
||||
Container mtusi-lab2-valkey-1 Waiting
|
||||
Container mtusi-lab2-postgres-1 Healthy
|
||||
Container mtusi-lab2-valkey-1 Healthy
|
||||
Container mtusi-lab2-migrate-1 Exited
|
||||
Container mtusi-lab2-api-1 Starting
|
||||
Container mtusi-lab2-api-1 Started
|
||||
stop took 0.80 s, exit code 0, new request refused: True, ready again: True
|
||||
=> PASS: SIGTERM stops the API cleanly within 10 s
|
||||
|
||||
== Summary ==
|
||||
PASS third-party images are pinned by tag and digest
|
||||
PASS api, postgres and valkey are healthy
|
||||
PASS API process does not run as root
|
||||
PASS Valkey persistence is AOF with fsync always, no eviction
|
||||
PASS re-running migrations applies nothing and duplicates nothing
|
||||
PASS composite chat creation is rolled back as a whole
|
||||
PASS exactly one registration wins, the other 19 get 409
|
||||
PASS the pool never opens more than 10 connections and all requests succeed
|
||||
PASS waiting for a lock is bounded: 503 after about 3 s, then writes work again
|
||||
PASS logs contain request_id/status/duration and no secrets
|
||||
PASS session expires after SESSION_TTL_SECONDS and the key disappears from Valkey
|
||||
PASS without Valkey: 503 for sessions, 401 for anonymous, registration still works
|
||||
PASS after Valkey restarts the session is still valid (AOF)
|
||||
PASS without PostgreSQL: bounded 503, anonymous still 401
|
||||
PASS API recovers by itself when PostgreSQL is back
|
||||
PASS resilience test passes after restarting api, postgres and valkey with the same volumes
|
||||
PASS SIGTERM stops the API cleanly within 10 s
|
||||
RESULT: PASS
|
||||
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"base_url": "http://127.0.0.1:8080",
|
||||
"clients": 10,
|
||||
"messages_per_client": 10,
|
||||
"sent": 100,
|
||||
"accepted_201": 100,
|
||||
"history_total": 100,
|
||||
"history_unique_ids": 100,
|
||||
"history_unique_texts": 100,
|
||||
"history_matches_accepted": true,
|
||||
"senders_in_history": 10,
|
||||
"min_messages_per_sender": 10,
|
||||
"max_messages_per_sender": 10,
|
||||
"ordered_by_created_at_and_id": true,
|
||||
"pages_read": 15,
|
||||
"page_size": 7,
|
||||
"send_elapsed_seconds": 0.407,
|
||||
"ok": true
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
# Tests against the compose stand (docker compose up -d --build --wait).
|
||||
# Date: 2026-10-07 16:03 UTC
|
||||
# Host: Windows 11, Docker Desktop engine 29.8.1, Compose 5.5.1, Python 3.14.7 for the test clients;
|
||||
# Intel Core Ultra 9 185H (16 cores, 22 threads), 31 GiB RAM, no CPU/RAM limits on containers.
|
||||
# Code: working tree of the solution branch (commit is given in REPORT.md).
|
||||
|
||||
$ python scripts/check_contract.py
|
||||
PASS: offline skeleton structure, local refs and Python syntax; application NOT tested.
|
||||
[exit code 0]
|
||||
|
||||
$ python tests/smoke.py --base-url http://127.0.0.1:8080
|
||||
PASS: lab2 public API scenario; infrastructure and grading evidence remain separate.
|
||||
[exit code 0]
|
||||
|
||||
$ BASE_URL=http://127.0.0.1:8080 python -m unittest discover -s student-tests -v
|
||||
test_authentication_is_checked_before_body_and_resource (test_access.AuthenticationOrderTest.test_authentication_is_checked_before_body_and_resource) ... ok
|
||||
test_registration_does_not_need_a_token (test_access.AuthenticationOrderTest.test_registration_does_not_need_a_token) ... ok
|
||||
test_chat_list_contains_only_own_chats (test_access.ForeignChatTest.test_chat_list_contains_only_own_chats) ... ok
|
||||
test_every_member_can_read_and_write (test_access.ForeignChatTest.test_every_member_can_read_and_write) ... ok
|
||||
test_foreign_chat_looks_like_missing_chat (test_access.ForeignChatTest.test_foreign_chat_looks_like_missing_chat) ... ok
|
||||
test_invalid_chat_id_is_400 (test_access.ForeignChatTest.test_invalid_chat_id_is_400) ... ok
|
||||
test_outsider_cannot_get_chat (test_access.ForeignChatTest.test_outsider_cannot_get_chat) ... ok
|
||||
test_outsider_cannot_read_history (test_access.ForeignChatTest.test_outsider_cannot_read_history) ... ok
|
||||
test_outsider_cannot_write (test_access.ForeignChatTest.test_outsider_cannot_write) ... ok
|
||||
test_unknown_route_is_404 (test_access.ForeignChatTest.test_unknown_route_is_404) ... ok
|
||||
test_x_user_id_does_not_open_a_foreign_chat (test_access.ForeignChatTest.test_x_user_id_does_not_open_a_foreign_chat) ... ok
|
||||
test_100_messages_from_10_clients (test_concurrency.ConcurrencyTest.test_100_messages_from_10_clients) ... ok
|
||||
test_chats_created_in_parallel_are_all_listed (test_concurrency.ConcurrencyTest.test_chats_created_in_parallel_are_all_listed) ... ok
|
||||
test_same_username_registered_in_parallel_wins_once (test_concurrency.ConcurrencyTest.test_same_username_registered_in_parallel_wins_once) ... ok
|
||||
test_chats_are_paged_in_creation_order (test_pagination.ChatListPaginationTest.test_chats_are_paged_in_creation_order) ... ok
|
||||
test_cursor_of_another_user_is_400 (test_pagination.ChatListPaginationTest.test_cursor_of_another_user_is_400) ... ok
|
||||
test_default_limit_is_50 (test_pagination.DefaultLimitTest.test_default_limit_is_50) ... ok
|
||||
test_empty_history (test_pagination.GrowingHistoryTest.test_empty_history) ... ok
|
||||
test_messages_sent_during_walk_are_not_lost (test_pagination.GrowingHistoryTest.test_messages_sent_during_walk_are_not_lost) ... ok
|
||||
test_cursor_from_another_chat_is_400 (test_pagination.MessagePaginationTest.test_cursor_from_another_chat_is_400) ... ok
|
||||
test_default_and_maximum_limit (test_pagination.MessagePaginationTest.test_default_and_maximum_limit) ... ok
|
||||
test_invalid_cursor_is_400 (test_pagination.MessagePaginationTest.test_invalid_cursor_is_400) ... ok
|
||||
test_invalid_limit_is_400 (test_pagination.MessagePaginationTest.test_invalid_limit_is_400) ... ok
|
||||
test_last_full_page_has_no_cursor (test_pagination.MessagePaginationTest.test_last_full_page_has_no_cursor) ... ok
|
||||
test_limit_can_change_between_pages (test_pagination.MessagePaginationTest.test_limit_can_change_between_pages) ... ok
|
||||
test_one_message_per_page (test_pagination.MessagePaginationTest.test_one_message_per_page) ... ok
|
||||
test_walk_over_several_pages (test_pagination.MessagePaginationTest.test_walk_over_several_pages) ... ok
|
||||
test_basic_is_rejected_on_business_endpoints (test_sessions.BearerTest.test_basic_is_rejected_on_business_endpoints) ... ok
|
||||
test_protected_endpoints_need_a_token (test_sessions.BearerTest.test_protected_endpoints_need_a_token) ... ok
|
||||
test_scheme_name_is_case_insensitive (test_sessions.BearerTest.test_scheme_name_is_case_insensitive) ... ok
|
||||
test_token_selects_the_user (test_sessions.BearerTest.test_token_selects_the_user) ... ok
|
||||
test_unknown_or_malformed_token (test_sessions.BearerTest.test_unknown_or_malformed_token) ... ok
|
||||
test_x_user_id_is_ignored (test_sessions.BearerTest.test_x_user_id_is_ignored) ... ok
|
||||
test_credentials_in_json_body_are_not_accepted (test_sessions.LoginTest.test_credentials_in_json_body_are_not_accepted) ... ok
|
||||
test_each_login_gives_a_new_token (test_sessions.LoginTest.test_each_login_gives_a_new_token) ... ok
|
||||
test_malformed_basic_header (test_sessions.LoginTest.test_malformed_basic_header) ... ok
|
||||
test_password_with_colons_and_unicode (test_sessions.LoginTest.test_password_with_colons_and_unicode) ... ok
|
||||
test_registration_never_returns_password (test_sessions.LoginTest.test_registration_never_returns_password) ... ok
|
||||
test_successful_login (test_sessions.LoginTest.test_successful_login) ... ok
|
||||
test_unknown_user_gets_the_same_answer (test_sessions.LoginTest.test_unknown_user_gets_the_same_answer) ... ok
|
||||
test_wrong_password (test_sessions.LoginTest.test_wrong_password) ... ok
|
||||
test_login_again_after_logout (test_sessions.LogoutTest.test_login_again_after_logout) ... ok
|
||||
test_logout_revokes_only_the_current_token (test_sessions.LogoutTest.test_logout_revokes_only_the_current_token) ... ok
|
||||
test_bad_member_ids (test_validation.ChatValidationTest.test_bad_member_ids) ... ok
|
||||
test_bad_titles (test_validation.ChatValidationTest.test_bad_titles) ... ok
|
||||
test_caller_must_be_a_member (test_validation.ChatValidationTest.test_caller_must_be_a_member) ... ok
|
||||
test_duplicate_members (test_validation.ChatValidationTest.test_duplicate_members) ... ok
|
||||
test_unknown_member_rolls_back_the_whole_chat (test_validation.ChatValidationTest.test_unknown_member_rolls_back_the_whole_chat) ... ok
|
||||
test_valid_chat_keeps_member_order (test_validation.ChatValidationTest.test_valid_chat_keeps_member_order) ... ok
|
||||
test_extra_fields (test_validation.InvalidJsonTest.test_extra_fields) ... ok
|
||||
test_invalid_utf8 (test_validation.InvalidJsonTest.test_invalid_utf8) ... ok
|
||||
test_json_that_is_not_an_object (test_validation.InvalidJsonTest.test_json_that_is_not_an_object) ... ok
|
||||
test_lone_surrogate_is_rejected_not_crashed (test_validation.InvalidJsonTest.test_lone_surrogate_is_rejected_not_crashed) ... ok
|
||||
test_malformed_json_on_message (test_validation.InvalidJsonTest.test_malformed_json_on_message) ... ok
|
||||
test_malformed_json_on_register (test_validation.InvalidJsonTest.test_malformed_json_on_register) ... ok
|
||||
test_nul_character_is_rejected_not_crashed (test_validation.InvalidJsonTest.test_nul_character_is_rejected_not_crashed) ... ok
|
||||
test_wrong_content_type (test_validation.InvalidJsonTest.test_wrong_content_type) ... ok
|
||||
test_wrong_types (test_validation.InvalidJsonTest.test_wrong_types) ... ok
|
||||
test_empty_message_is_400 (test_validation.MessageValidationTest.test_empty_message_is_400) ... ok
|
||||
test_missing_or_wrong_text (test_validation.MessageValidationTest.test_missing_or_wrong_text) ... ok
|
||||
test_nul_in_text_is_400 (test_validation.MessageValidationTest.test_nul_in_text_is_400) ... ok
|
||||
test_oversized_body_is_413 (test_validation.MessageValidationTest.test_oversized_body_is_413) ... ok
|
||||
test_sender_id_in_body_is_rejected (test_validation.MessageValidationTest.test_sender_id_in_body_is_rejected) ... ok
|
||||
test_text_length_limit (test_validation.MessageValidationTest.test_text_length_limit) ... ok
|
||||
test_unicode_text_is_kept (test_validation.MessageValidationTest.test_unicode_text_is_kept) ... ok
|
||||
test_bad_display_names (test_validation.UserValidationTest.test_bad_display_names) ... ok
|
||||
test_bad_passwords (test_validation.UserValidationTest.test_bad_passwords) ... ok
|
||||
test_bad_usernames (test_validation.UserValidationTest.test_bad_usernames) ... ok
|
||||
test_duplicate_username_is_409 (test_validation.UserValidationTest.test_duplicate_username_is_409) ... ok
|
||||
test_missing_fields (test_validation.UserValidationTest.test_missing_fields) ... ok
|
||||
test_password_length_limits_are_accepted (test_validation.UserValidationTest.test_password_length_limits_are_accepted) ... ok
|
||||
test_unicode_display_name_is_kept (test_validation.UserValidationTest.test_unicode_display_name_is_kept) ... ok
|
||||
test_username_length_limits_are_accepted (test_validation.UserValidationTest.test_username_length_limits_are_accepted) ... ok
|
||||
|
||||
----------------------------------------------------------------------
|
||||
Ran 73 tests in 8.689s
|
||||
|
||||
OK
|
||||
[exit code 0]
|
||||
|
||||
$ BASE_URL=http://127.0.0.1:8080 python student-tests/race.py --output evidence/race.json
|
||||
{
|
||||
"base_url": "http://127.0.0.1:8080",
|
||||
"clients": 10,
|
||||
"messages_per_client": 10,
|
||||
"sent": 100,
|
||||
"accepted_201": 100,
|
||||
"history_total": 100,
|
||||
"history_unique_ids": 100,
|
||||
"history_unique_texts": 100,
|
||||
"history_matches_accepted": true,
|
||||
"senders_in_history": 10,
|
||||
"min_messages_per_sender": 10,
|
||||
"max_messages_per_sender": 10,
|
||||
"ordered_by_created_at_and_id": true,
|
||||
"pages_read": 15,
|
||||
"page_size": 7,
|
||||
"send_elapsed_seconds": 0.407,
|
||||
"ok": true
|
||||
}
|
||||
PASS
|
||||
[exit code 0]
|
||||
|
||||
# exit codes: [0, 0, 0, 0]
|
||||
Reference in New Issue
Block a user