Files
mtusi2026lab5/student-tests/tests_helpers.py
T
Vladislav Kunetskiy 01b885e846 lab5: очереди и асинхронная обработка изображений
Durable staging handoff, transactional outbox, at-least-once worker with lease fencing, idempotent versioned pipeline, three variants, operator tooling, contract tests and evidence. Includes the review fixes: reprocess source selection and failure settlement, replay guards, stale-job retirement, PNG structural/raster validation, migration rollback safety and audit events.
2026-10-31 14:34:24 +03:00

66 lines
2.4 KiB
Python

"""Обвязки для собственных проверок ЛР3: docker compose и карта реплик.
Собственные тесты лежат в student-tests/ (CONTRIBUTING.md). X-Instance-Id равен
hostname контейнера, поэтому по нему можно найти реально обслуживший контейнер.
"""
import os
import subprocess
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
def compose(*args, check=True, env=None):
command = [*os.environ.get('COMPOSE_BIN', 'docker compose').split(), '--env-file', '.env', '-f', 'compose.yaml', *args]
environment = {**os.environ, **(env or {})}
return subprocess.run(command, cwd=ROOT, capture_output=True, text=True, check=check, env=environment)
def jobctl(*args, check=True):
"""Run the operator CLI inside the worker image."""
return compose('run', '--rm', '--entrypoint', '/usr/local/bin/jobctl', 'worker', *args, check=check)
def psql(sql):
"""Read a scalar/list from the stand database (operator evidence only)."""
project = os.environ.get('COMPOSE_PROJECT_NAME', 'mtusi-messenger-lab5')
result = subprocess.run(
['docker', 'exec', project + '-postgres-1', 'psql', '-U', 'messenger', '-d', 'messenger', '-Atc', sql],
cwd=ROOT, capture_output=True, text=True, check=True,
)
return result.stdout.strip()
def containers_by_hostname(service='api'):
"""{hostname: {id, name, status}} для контейнеров сервиса."""
project = os.environ.get('COMPOSE_PROJECT_NAME', 'mtusi-messenger-lab5')
listed = subprocess.run(
[
'docker', 'ps', '-q',
'--filter', 'label=com.docker.compose.project=' + project,
'--filter', 'label=com.docker.compose.service=' + service,
],
capture_output=True,
text=True,
check=True,
).stdout.split()
result = {}
for container in listed:
info = subprocess.run(
['docker', 'inspect', '--format', '{{.Config.Hostname}} {{.Name}} {{.State.Status}}', container],
capture_output=True,
text=True,
check=True,
).stdout.split()
result[info[0]] = {'id': container[:12], 'name': info[1].lstrip('/'), 'status': info[2]}
return result
def messages_path(chat):
return '/api/v1/chats/' + chat['id'] + '/messages'