"""Общие операции с репликами текущего Compose-проекта.""" from dataclasses import dataclass import json import os from pathlib import Path import subprocess ROOT = Path(__file__).resolve().parents[1] @dataclass(frozen=True) class Replica: """Состояние контейнера API.""" id: str image: str instance_id: str running: bool health: str | None class ComposeProject: """Работает только с выбранным Compose-проектом.""" def __init__(self) -> None: self.command = ["docker", "compose", "--env-file", ".env"] self.config = json.loads(self.run("config", "--format", "json")) self.name = self.config["name"] def docker(self, *args: str, timeout: int = 90, env=None) -> str: """Выполняет Docker-команду с ограничением времени.""" result = subprocess.run( ["docker", *args], cwd=ROOT, text=True, capture_output=True, timeout=timeout, env={**os.environ, **(env or {})}, ) if result.returncode: raise RuntimeError(result.stderr.strip() or result.stdout.strip()) return result.stdout.strip() def run(self, *args: str, timeout: int = 90, env=None) -> str: """Выполняет команду Compose с текущими overrides.""" return self.docker(*self.command[1:], *args, timeout=timeout, env=env) def replicas(self) -> list[Replica]: """Читает состояние API без окружения контейнеров.""" ids = self.run("ps", "--all", "--quiet", "api").splitlines() if not ids: return [] template = ( '{"id":{{json .Id}},"image":{{json .Image}},' '"hostname":{{json .Config.Hostname}},"running":{{json .State.Running}},' '"health":{{if .State.Health}}{{json .State.Health.Status}}{{else}}null{{end}}}' ) rows = self.docker("inspect", "--format", template, *ids).splitlines() configured = str( self.config["services"]["api"].get("environment", {}).get("INSTANCE_ID") or "" ).strip() return [ Replica( row["id"], row["image"], configured or row["hostname"], row["running"], row["health"], ) for row in map(json.loads, rows) ] def cleanup_docker(*args, allow_failure=False): result = subprocess.run(['docker',*args],cwd=ROOT,text=True,capture_output=True,timeout=120) if result.returncode and not allow_failure: raise RuntimeError('Docker cleanup failed: ' + ' '.join(args[:2])) return result.stdout.strip() def inventory(projects): resources = {'containers':[], 'networks':[], 'volumes':[]} for project in projects: label = 'label=com.docker.compose.project=' + project resources['containers'].extend(cleanup_docker('ps','-aq','--filter',label).splitlines()) resources['networks'].extend(cleanup_docker('network','ls','-q','--filter',label).splitlines()) resources['volumes'].extend(cleanup_docker('volume','ls','-q','--filter',label).splitlines()) return resources def clean_stands(*, apply=False): config = json.loads(cleanup_docker('compose','--env-file','.env','config','--format','json')) projects = sorted({config['name'], os.getenv('MAIN_PROJECT', 'messenger'), os.getenv('TEST_PROJECT', 'messenger-test')}) resources = inventory(projects) names = {service['image'] for service in config['services'].values() if 'build' in service} image_ids = set() for name in names: image_ids.update(cleanup_docker('image','ls','-q',name).splitlines()) # У старых сборок API может уже не быть тега. Сверяем также команду и WORKDIR, # чтобы не затронуть сторонние образы с обычным OCI revision label. candidates = cleanup_docker('image','ls','-q','--filter','label=org.opencontainers.image.revision').splitlines() for candidate in candidates: image = json.loads(cleanup_docker('image','inspect',candidate))[0] settings = image['Config'] if settings.get('Cmd') == ['python','-m','app'] and settings.get('WorkingDir') == '/app': image_ids.add(candidate) plan = {'projects':projects, 'resources':resources, 'application_image_ids':sorted(image_ids), 'local_settings_preserved':True} if not apply: return plan if resources['containers']: cleanup_docker('rm','-f','-v',*resources['containers']) if resources['networks']: cleanup_docker('network','rm',*resources['networks']) if resources['volumes']: cleanup_docker('volume','rm',*resources['volumes']) if image_ids: cleanup_docker('image','rm',*sorted(image_ids)) remaining = inventory(projects) if any(remaining.values()): raise RuntimeError('Cleanup incomplete') plan['remaining'] = remaining plan['application_images_remaining'] = [name for name in sorted(names) if cleanup_docker('image','ls','-q',name)] if plan['application_images_remaining']: raise RuntimeError('Application images remain') plan['result'] = 'PASS' return plan def verify(): """Один последовательный прогон на одном стенде с обязательной очисткой.""" from datetime import datetime, timezone import re import sys import time project_name = os.getenv('TEST_PROJECT', 'messenger-test') os.environ.update(COMPOSE_PROJECT_NAME=project_name, COMPOSE_FILE='compose.yaml:compose.test.yaml', GRAFANA_HOST_PORT='23000', LB_HOST_PORT='28080', DOCKER_TESTS='1') report = {'date_utc': datetime.now(timezone.utc).isoformat(), 'checks': [], 'profile': {'single_stack': True, 'load_duration_seconds': 10, 'load_requests_per_second': 10, 'load_concurrency': 2}} destination = ROOT / 'evidence/verification.json' destination.parent.mkdir(exist_ok=True) def run(name, command, timeout=600): print('RUN: ' + name, flush=True) started = time.monotonic() result = subprocess.run(command, cwd=ROOT, text=True, capture_output=True, timeout=timeout) executable = Path(command[0]) display_command = [str(executable.relative_to(ROOT)) if executable.is_relative_to(ROOT) else command[0], *command[1:]] entry = {'name': name, 'command': display_command, 'exit_code': result.returncode, 'duration_seconds': round(time.monotonic()-started, 3), 'result': 'PASS' if result.returncode == 0 else 'FAIL'} if name == 'regression': summary = re.search(r'\d+ passed[^\n]*', result.stdout) if summary: entry['summary'] = summary.group(0) if result.returncode: # Полные диагностические логи локальны; в evidence нет путей/credentials. (ROOT / '.verification-error.log').write_text(result.stdout + result.stderr) entry['diagnostics'] = '.verification-error.log (local, not committed)' report['checks'].append(entry) print(entry['result'] + ': ' + name, flush=True) if result.returncode: raise RuntimeError(name + ' failed; inspect local diagnostic log') try: run('contract', [sys.executable, 'scripts/check_contract.py']) run('openapi', [sys.executable, 'scripts/validate_openapi.py']) run('compose', ['docker', 'compose', '--env-file', '.env', 'config', '--quiet']) run('startup', ['make', '--no-print-directory', 'test-stand-up'], timeout=600) run('regression', ['make', '--no-print-directory', 'test-unit']) run('api', [sys.executable, 'tests/smoke.py', '--base-url', 'http://localhost:28080']) run('observability', [sys.executable, '-m', 'scripts.check_observability', '--base-url', 'http://localhost:28080', '--grafana-url', 'http://localhost:23000', '--incidents', '--restart-telemetry']) run('persistence', [sys.executable, 'tests/resilience.py', '--mode', 'persistence', '--action-script', 'scripts/restart.sh', '--base-url', 'http://localhost:28080']) run('failover', [sys.executable, 'tests/resilience.py', '--mode', 'failover', '--action-script', 'scripts/stop-one.sh', '--base-url', 'http://localhost:28080']) report['result'] = 'PASS' except BaseException: report['result'] = 'FAIL' raise finally: try: print('RUN: cleanup', flush=True) report['cleanup'] = clean_stands(apply=True) print('PASS: cleanup', flush=True) finally: report['finished_at_utc'] = datetime.now(timezone.utc).isoformat() destination.write_text(json.dumps(report, ensure_ascii=False, indent=2)+'\n') def main(): import argparse parser = argparse.ArgumentParser(description='Проверка и очистка Compose-стендов') parser.add_argument('command', choices=['verify', 'clean']) parser.add_argument('--apply', action='store_true', help='Подтвердить удаление ресурсов') args = parser.parse_args() if args.command == 'verify': verify() else: print(json.dumps(clean_stands(apply=args.apply), indent=2)) if __name__ == '__main__': main()