forked from mtusi2026/mtusi2026lab7
- restore a verified row by fact: three variants give ready, otherwise failed with a known code instead of ready without content - check trigger functions are not SECURITY DEFINER and pin search_path - record the working stand comparison, the permanent drill lock file and the remaining SeaweedFS details Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
143 lines
4.7 KiB
Python
143 lines
4.7 KiB
Python
"""Строки конвейера для тестов значений роли worker и сверки заданий (change
|
||
`track1-worker-compromise`, design, решения 6 и 8): вложение с заданием и
|
||
вариантами — мимо приложения, соединением администратора."""
|
||
|
||
import uuid
|
||
from typing import Any
|
||
|
||
import asyncpg
|
||
|
||
from conftest import ROLE_PASSWORDS, TEST_DB, settings
|
||
from src.infra.tls import build_client_context
|
||
|
||
PIPELINE_VERSION = 1
|
||
VARIANTS = ("thumbnail", "crop", "optimized")
|
||
|
||
|
||
async def connect_as(role: str) -> asyncpg.Connection:
|
||
"""Соединение роли процесса с временной базой."""
|
||
source = settings.database
|
||
return await asyncpg.connect(
|
||
host=source.host,
|
||
port=source.port,
|
||
user=role,
|
||
password=ROLE_PASSWORDS[role],
|
||
database=TEST_DB,
|
||
timeout=source.connect_timeout_seconds,
|
||
ssl=build_client_context(settings.client_tls.ca_file),
|
||
)
|
||
|
||
|
||
async def add_world(connection: asyncpg.Connection) -> tuple[uuid.UUID, ...]:
|
||
"""Пользователь, чат и участник: владелец вложений."""
|
||
user, chat = uuid.uuid4(), uuid.uuid4()
|
||
await connection.execute(
|
||
"INSERT INTO users (id, username, display_name, password_hash, "
|
||
"created_at) VALUES ($1, $2, 'u', '$argon2id$v=19$m=65536,t=3,p=4$"
|
||
"c29tZXNhbHQ$c29tZWhhc2g', now())",
|
||
user,
|
||
f"u{user.hex[:12]}",
|
||
)
|
||
await connection.execute(
|
||
"INSERT INTO chats (id, title, created_at) VALUES ($1, 't', now())",
|
||
chat,
|
||
)
|
||
await connection.execute(
|
||
"INSERT INTO chat_members (chat_id, user_id, position, created_at) "
|
||
"VALUES ($1, $2, 0, now())",
|
||
chat,
|
||
user,
|
||
)
|
||
return user, chat
|
||
|
||
|
||
def variant_key(
|
||
attachment: uuid.UUID,
|
||
name: str,
|
||
*,
|
||
namespace: str = TEST_DB,
|
||
version: int = PIPELINE_VERSION,
|
||
) -> str:
|
||
return f"{namespace}/attachments/{attachment}/v{version}-{name}"
|
||
|
||
|
||
async def add_variants(
|
||
connection: asyncpg.Connection,
|
||
attachment: uuid.UUID,
|
||
names: tuple[str, ...] = VARIANTS,
|
||
) -> None:
|
||
for name in names:
|
||
await connection.execute(
|
||
INSERT_VARIANT_SQL,
|
||
attachment,
|
||
name,
|
||
variant_key(attachment, name),
|
||
)
|
||
|
||
|
||
INSERT_VARIANT_SQL = (
|
||
"INSERT INTO attachment_variants (attachment_id, pipeline_version, "
|
||
"variant, object_key, content_type, size_bytes, width, height, sha256, "
|
||
"created_at) VALUES ($1, 1, $2, $3, 'image/jpeg', 1, 1, 1, "
|
||
"decode(repeat('ab', 32), 'hex'), now())"
|
||
)
|
||
|
||
|
||
async def add_attachment(
|
||
connection: asyncpg.Connection,
|
||
world: tuple[uuid.UUID, ...],
|
||
*,
|
||
status: str,
|
||
state: str | None = None,
|
||
staging: bool = True,
|
||
variants: tuple[str, ...] = (),
|
||
age_seconds: int = 3600,
|
||
**job: Any,
|
||
) -> uuid.UUID:
|
||
"""Вложение `status` с заданием `state` (`None` — без задания); `job` —
|
||
колонки задания выражениями SQL."""
|
||
user, chat = world
|
||
attachment = uuid.uuid4()
|
||
namespace = TEST_DB
|
||
await connection.execute(
|
||
"INSERT INTO attachments (id, chat_id, owner_id, filename, "
|
||
"content_type, size_bytes, sha256, object_key, staging_key, status, "
|
||
"error_code, updated_at, created_at) VALUES ($1, $2, $3, 'a.png', "
|
||
"'image/png', 1, decode(repeat('ab', 32), 'hex'), $4, $5, $6, $7, "
|
||
"now() - make_interval(secs => $8), "
|
||
"now() - make_interval(secs => $8))",
|
||
attachment,
|
||
chat,
|
||
user,
|
||
f"{namespace}/attachments/{attachment}/original",
|
||
f"{namespace}/staging/{attachment}/original" if staging else None,
|
||
status,
|
||
"processing_failed" if status == "failed" else None,
|
||
float(age_seconds),
|
||
)
|
||
if state is not None:
|
||
columns = {
|
||
"lease_expires_at": (
|
||
"now() + interval '60 s'" if state == "processing" else "NULL"
|
||
),
|
||
"published_at": "now() - interval '1 s'",
|
||
"next_attempt_at": "NULL",
|
||
"attempt": "1",
|
||
"attempt_base": "0",
|
||
"updated_at": "now()",
|
||
**job,
|
||
}
|
||
names = ", ".join(columns)
|
||
values = ", ".join(columns.values())
|
||
await connection.execute(
|
||
"INSERT INTO attachment_jobs (id, attachment_id, "
|
||
f"pipeline_version, state, created_at, {names}) VALUES "
|
||
"(gen_random_uuid(), $1, 1, $2, "
|
||
f"now() - make_interval(secs => $3), {values})",
|
||
attachment,
|
||
state,
|
||
float(age_seconds),
|
||
)
|
||
await add_variants(connection, attachment, variants)
|
||
return attachment
|