"""Обвязки для собственных проверок ЛР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): command = [*os.environ.get('COMPOSE_BIN', 'docker compose').split(), '--env-file', '.env', '-f', 'compose.yaml', *args] return subprocess.run(command, cwd=ROOT, capture_output=True, text=True, check=check) def containers_by_hostname(service='api'): """{hostname: {id, name, status}} для контейнеров сервиса.""" project = os.environ.get('COMPOSE_PROJECT_NAME', 'mtusi-messenger-lab3') 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'