Files
mtusi2026lab7/tests/test_worker_retry.py
T
kirill_dolda 280c5a3300
Skeleton contracts / contracts (pull_request) Blocked by required conditions
ЛР 7: безопасность, mTLS и OpenBao на оценку 3
2026-12-06 15:19:24 +03:00

73 lines
2.6 KiB
Python

"""Verify retries with a synthetic missing object, using the local operator socket."""
import os
from pathlib import Path
import subprocess
import time
import uuid
from client import Client, arguments, check, run
ROOT = Path(__file__).resolve().parents[1]
DEFAULT_CA = Path(os.environ.get("LAB7_STATE", ROOT / "secrets/lab7")) / "ca/ca.crt"
def sql(statement, **values):
arguments = [f"--set={key}={value}" for key, value in values.items()]
subprocess.run(
["docker", "compose", "exec", "-T", "db", "psql", "-X", "-q",
"--set=ON_ERROR_STOP=1", "-U", "messenger_admin", "-d", "messenger_db", *arguments],
cwd=ROOT, input=statement, text=True, check=True,
)
def verify(base_url=None, ca_file=None):
client = Client(base_url, ca_file or str(DEFAULT_CA))
owner, _, _, chat = client.bootstrap()
fixture = {
"attachment_id": str(uuid.uuid4()),
"event_id": str(uuid.uuid4()),
"owner_id": str(uuid.UUID(owner["id"])),
"chat_id": str(uuid.UUID(chat["id"])),
}
sql(
"""
BEGIN;
INSERT INTO attachments (id, object_key, filename, mime_type, size_bytes, sha256,
chat_id, user_id, status, created_at, updated_at)
VALUES (:'attachment_id', 'originals/lab7-missing-' || :'attachment_id', 'missing.png',
'image/png', 1, repeat('0', 64), :'chat_id', :'owner_id', 'queued', now(), now());
INSERT INTO outbox_events (id, attachment_id) VALUES (:'event_id', :'attachment_id');
COMMIT;
""", **fixture,
)
try:
deadline = time.monotonic() + 60
while time.monotonic() < deadline:
response = client.call("GET", f"/api/v1/attachments/{fixture['attachment_id']}",
actor=owner, schema="Attachment")["json"]
if response["status"] == "failed":
check(response["error_code"] == "s3_download_error", "incorrect worker error code")
print("PASS HTTPS worker retry reached failed with s3_download_error")
return
time.sleep(0.5)
raise AssertionError("worker did not reach failed within 60 seconds")
finally:
sql(
"BEGIN; DELETE FROM outbox_events WHERE attachment_id = :'attachment_id';"
" DELETE FROM attachments WHERE id = :'attachment_id'; COMMIT;",
attachment_id=fixture["attachment_id"],
)
def test_worker_retry():
verify()
def main():
args = arguments("Verify worker retries on the local TLS-enabled compose stand")
verify(args.base_url, args.ca_file)
if __name__ == "__main__":
run(main)