Bring the system changes of lab 3 from 61c0c28..72a1e57 onto the lab 4 code, resolving each conflict by meaning and keeping everything lab 4 added: - a message is posted with one SQL statement under the chat row lock, and the attachment links go into the same statement; a foreign key violation on the attachment link still answers 404 and leaves nothing behind. Text and the attachment list are checked before the write, an outsider gets 404 for both, as before the carry over; - the request pipeline counts the body bytes actually received against the limit of the route, so a chunked body over 1 MiB gets 413 and the upload route keeps its MAX_UPLOAD_BYTES limit; - a request cancelled at shutdown gets 503 unavailable in the contract format, the worker waits a second less than the rest of the shutdown budget, SHUTDOWN_TIMEOUT_SECONDS becomes 6 s and the stop hooks 7 s; - readiness sits behind a Readiness port with the draining flag in shared infrastructure, and the drain window is logged through Logger; - logout revokes through SessionRevocations and withdraws the mark when PostgreSQL rejects the delete; an empty cursor is 400; - EventBus.project is gone, the sweep test subscribes and waits on a queue; domain tests check attachment_list instead of Chat.post; - make up waits for every replica in the balancer, BASE_URL follows the lb port, the image is tagged by the compose project, retry-check and faults-evidence are added, detection runs three times, failover-each and rolling-update record the stand usage, CI checks numeric uids. down also takes the tools and reconcile profiles of lab 4. Track 1 tooling of lab 3 (scaling.py, its samplers, tools/ and the CPU pinning of api, lb, valkey and k6) stays out; stand-usage.sh queries the database size itself instead of the track 1 sampler. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01V1d3RLHNBBMA6UqBLLJv27
214 lines
7.0 KiB
Python
214 lines
7.0 KiB
Python
import asyncio
|
|
import re
|
|
import subprocess
|
|
import time
|
|
from base64 import b64encode
|
|
from collections.abc import AsyncIterator, Iterator
|
|
from contextlib import asynccontextmanager
|
|
from pathlib import Path
|
|
from typing import Any
|
|
from uuid import uuid4
|
|
|
|
import asyncpg
|
|
import pytest
|
|
from fastapi import FastAPI
|
|
from httpx import ASGITransport, AsyncClient
|
|
|
|
from fakes import MemoryStorageProvider
|
|
from messenger.attachments.config import AttachmentsSettings
|
|
from messenger.identity.config import IdentitySettings
|
|
from messenger.main import create_app
|
|
from messenger.messaging.config import MessagingSettings
|
|
from messenger.migrate import migrate
|
|
from messenger.shared.config import PlatformSettings
|
|
from messenger.shared.entrypoints.config import TransportSettings
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
POSTGRES_IMAGE = re.compile(r"image: (postgres:\S+)")
|
|
VALKEY_IMAGE = re.compile(r"image: (valkey/valkey:\S+)")
|
|
STARTUP_SECONDS = 60
|
|
PASSWORD = "Пароль-для-тестов-01"
|
|
APPS: dict[int, FastAPI] = {}
|
|
TOKENS: dict[str, str] = {}
|
|
DATABASE: dict[str, str] = {}
|
|
|
|
|
|
def docker(*args: str) -> str:
|
|
done = subprocess.run(["docker", *args], check=True, capture_output=True, text=True)
|
|
return done.stdout.strip()
|
|
|
|
|
|
async def migrated(dsn: str) -> None:
|
|
deadline = time.monotonic() + STARTUP_SECONDS
|
|
while True:
|
|
try:
|
|
await migrate(dsn)
|
|
return
|
|
except OSError, asyncpg.PostgresError, asyncpg.InterfaceError:
|
|
if time.monotonic() > deadline:
|
|
raise
|
|
await asyncio.sleep(0.5)
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def database_for_api(request: pytest.FixtureRequest) -> None:
|
|
if "api" in request.node.path.parts:
|
|
request.getfixturevalue("database")
|
|
|
|
|
|
def image(pattern: re.Pattern[str]) -> str:
|
|
found = pattern.search((ROOT / "compose.yaml").read_text())
|
|
assert found is not None
|
|
return found[1]
|
|
|
|
|
|
def published(name: str, port: int) -> int:
|
|
return int(docker("port", name, f"{port}/tcp").splitlines()[0].rsplit(":", 1)[1])
|
|
|
|
|
|
def container(name: str, *args: str) -> None:
|
|
docker("run", "-d", "--rm", "--name", name, *args)
|
|
|
|
|
|
def answering(name: str, *probe: str) -> bool:
|
|
done = subprocess.run(["docker", "exec", name, *probe], capture_output=True, check=False)
|
|
return done.returncode == 0 and b"PONG" in done.stdout
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
def valkey() -> Iterator[dict[str, str]]:
|
|
name = f"mtusi-lab4-test-valkey-{uuid4().hex[:8]}"
|
|
container(name, "-p", "127.0.0.1::6379", image(VALKEY_IMAGE))
|
|
try:
|
|
deadline = time.monotonic() + STARTUP_SECONDS
|
|
while not answering(name, "valkey-cli", "ping"):
|
|
assert time.monotonic() < deadline
|
|
time.sleep(0.2)
|
|
yield {"name": name, "url": f"redis://127.0.0.1:{published(name, 6379)}/0"}
|
|
finally:
|
|
subprocess.run(["docker", "rm", "-fv", name], capture_output=True, check=False)
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
def database() -> Iterator[str]:
|
|
name = f"mtusi-lab4-test-{uuid4().hex[:8]}"
|
|
tuning = ["-c", "fsync=off", "-c", "synchronous_commit=off", "-c", "full_page_writes=off"]
|
|
environment = ["-e", "POSTGRES_PASSWORD=test", "-e", "POSTGRES_DB=messenger"]
|
|
container(name, *environment, "-p", "127.0.0.1::5432", image(POSTGRES_IMAGE), *tuning)
|
|
try:
|
|
dsn = f"postgresql://postgres:test@127.0.0.1:{published(name, 5432)}/messenger"
|
|
asyncio.run(migrated(dsn))
|
|
DATABASE["url"] = dsn
|
|
yield dsn
|
|
finally:
|
|
subprocess.run(["docker", "rm", "-fv", name], capture_output=True, check=False)
|
|
|
|
|
|
def split_settings(**overrides: Any) -> dict[str, Any]:
|
|
base: dict[str, Any] = {
|
|
"log_level": "CRITICAL",
|
|
"cursor_secret": "test-secret-for-cursors",
|
|
"database_url": DATABASE.get("url", "postgresql://unused@127.0.0.1:1/unused"),
|
|
"s3_access_key_id": "test-key",
|
|
"s3_secret_access_key": "test-secret-key",
|
|
}
|
|
base.update(overrides)
|
|
groups = {
|
|
"platform": PlatformSettings,
|
|
"transport": TransportSettings,
|
|
"identity": IdentitySettings,
|
|
"messaging": MessagingSettings,
|
|
"attachments": AttachmentsSettings,
|
|
}
|
|
return {
|
|
name: kind.model_validate(
|
|
{key: value for key, value in base.items() if key in kind.model_fields}
|
|
)
|
|
for name, kind in groups.items()
|
|
}
|
|
|
|
|
|
@asynccontextmanager
|
|
async def running_app(**overrides: Any) -> AsyncIterator[AsyncClient]:
|
|
app = create_app(**split_settings(**overrides), overrides=(MemoryStorageProvider(),))
|
|
async with (
|
|
app.router.lifespan_context(app),
|
|
AsyncClient(transport=ASGITransport(app=app), base_url="http://testserver") as http,
|
|
):
|
|
APPS[id(http)] = app
|
|
try:
|
|
yield http
|
|
finally:
|
|
APPS.pop(id(http), None)
|
|
|
|
|
|
async def resolve[DependencyT](http: AsyncClient, dependency: type[DependencyT]) -> DependencyT:
|
|
return await APPS[id(http)].state.dishka_container.get(dependency)
|
|
|
|
|
|
@pytest.fixture
|
|
async def client() -> AsyncIterator[AsyncClient]:
|
|
async with running_app() as http:
|
|
yield http
|
|
|
|
|
|
async def scalar(sql: str, *args: Any) -> Any:
|
|
connection = await asyncpg.connect(DATABASE["url"])
|
|
try:
|
|
return await connection.fetchval(sql, *args)
|
|
finally:
|
|
await connection.close()
|
|
|
|
|
|
def basic(username: str, password: str) -> dict[str, str]:
|
|
encoded = b64encode(f"{username}:{password}".encode()).decode()
|
|
return {"Authorization": f"Basic {encoded}"}
|
|
|
|
|
|
def bearer(token: str) -> dict[str, str]:
|
|
return {"Authorization": f"Bearer {token}"}
|
|
|
|
|
|
def new_user(label: str, password: str = PASSWORD) -> dict[str, str]:
|
|
return {
|
|
"username": f"u_{label}_{uuid4().hex[:12]}",
|
|
"display_name": f"Студент {label}",
|
|
"password": password,
|
|
}
|
|
|
|
|
|
async def login(http: AsyncClient, username: str, password: str = PASSWORD) -> dict[str, Any]:
|
|
response = await http.post("/api/v1/auth/sessions", headers=basic(username, password))
|
|
assert response.status_code == 201, response.text
|
|
return response.json()
|
|
|
|
|
|
async def register(http: AsyncClient, label: str, password: str = PASSWORD) -> dict[str, Any]:
|
|
body = new_user(label, password)
|
|
response = await http.post("/api/v1/users", json=body)
|
|
assert response.status_code == 201, response.text
|
|
user = response.json()
|
|
TOKENS[user["id"]] = (await login(http, body["username"], password))["token"]
|
|
return user
|
|
|
|
|
|
async def make_chat(
|
|
http: AsyncClient, owner: dict[str, Any], *members: dict[str, Any]
|
|
) -> dict[str, Any]:
|
|
member_ids = [owner["id"], *[member["id"] for member in members]]
|
|
response = await http.post(
|
|
"/api/v1/chats",
|
|
json={"title": "Тестовый чат", "member_ids": member_ids},
|
|
headers=actor(owner),
|
|
)
|
|
assert response.status_code == 201, response.text
|
|
return response.json()
|
|
|
|
|
|
def actor(user: dict[str, Any]) -> dict[str, str]:
|
|
return bearer(TOKENS[user["id"]])
|
|
|
|
|
|
def keyed(user: dict[str, Any], key: str | None = None) -> dict[str, str]:
|
|
return {**actor(user), "Idempotency-Key": key or uuid4().hex}
|