"""Проверки внутри Linux-контейнера с отдельным Docker daemon.""" from datetime import datetime, timezone import json import os from pathlib import Path import platform import subprocess import tarfile import time ROOT = Path("/workspace") EVIDENCE = ROOT / "evidence" COMPOSE = ["docker", "compose", "--env-file", ".env"] def step(name, command, timeout=1800): print(name, flush=True) started = time.monotonic() with (EVIDENCE / (name + ".txt")).open("w") as output: output.write("$ " + " ".join(command) + "\n") output.flush() result = subprocess.run( command, cwd=ROOT, stdout=output, stderr=subprocess.STDOUT, timeout=timeout ) output.write( f"\nexit_code={result.returncode}; duration_seconds={time.monotonic() - started:.3f}\n" ) result.check_returncode() def main(): with tarfile.open("/tmp/source.tar") as archive: archive.extractall(ROOT, filter="data") os.chdir(ROOT) EVIDENCE.mkdir(exist_ok=True) deadline = time.monotonic() + 120 while subprocess.run(["docker", "info"], capture_output=True).returncode: if time.monotonic() > deadline: raise RuntimeError("Docker daemon did not start") time.sleep(1) os.environ.update( COMPOSE_PROJECT_NAME="messenger-test", COMPOSE_FILE="compose.yaml:compose.test.yaml", LB_HOST_PORT="28080", DOCKER_TESTS="1", TEST_USE_MIGRATIONS="1", TEST_REDIS_HOST="127.0.0.1", TEST_REDIS_PORT="26379", TEST_REDIS_DB="15", ) result = { "status": "failed", "python": platform.python_version(), "platform": platform.platform(), "started_at": datetime.now(timezone.utc).isoformat(), } try: step("00_init", ["python3", "scripts/init_local.py"]) secret = (ROOT / "secrets/db_password").read_text() os.environ["TEST_DATABASE_URL"] = ( f"postgresql+asyncpg://messenger:{secret}@127.0.0.1:25432/messenger_test" ) os.environ["TEST_REDIS_PASSWORD"] = ( ROOT / "secrets/redis_password" ).read_text() step( "01_black", [ "python3", "-m", "black", "--check", "app", "infra", "migrations", "scripts", "student-tests", "tests", ], ) step("02_contract", ["python3", "scripts/check_contract.py"]) step("03_openapi", ["python3", "scripts/validate_openapi.py"]) step( "04_build_start", COMPOSE + ["up", "-d", "--build"], timeout=3600, ) step( "04_readiness", COMPOSE + ["up", "-d", "--wait", "--wait-timeout", "240", "nginx"], ) step( "05_create_test_database", COMPOSE + [ "exec", "-T", "postgres", "createdb", "-U", "messenger", "messenger_test", ], ) step( "06_pytest", ["python3", "-m", "pytest", "-q", "--junitxml=evidence/pytest.xml"], timeout=1800, ) base = ["--base-url", "http://127.0.0.1:28080"] step("07_smoke", ["python3", "tests/smoke.py", *base]) step( "08_storage", ["python3", "-m", "scripts.check_s3", *base, "--with-outage"] ) step("09_outbox", ["python3", "-m", "scripts.check_outbox_recovery", *base]) step( "10_jobs", [ "python3", "-m", "scripts.check_jobs", *base, "--output", "evidence/jobs.json", ], ) step( "11_persistence", [ "python3", "tests/resilience.py", "--mode", "persistence", "--action-script", "scripts/restart.sh", *base, ], ) step( "12_failover", [ "python3", "tests/resilience.py", "--mode", "failover", "--action-script", "scripts/stop-one.sh", *base, ], ) step( "13_environment", [ "docker", "info", "--format", "CPUs={{.NCPU}} Memory={{.MemTotal}} Server={{.ServerVersion}} Architecture={{.Architecture}}", ], ) step("13_dependencies", ["python3", "-m", "pip", "freeze"]) step("13_images", ["docker", "image", "ls", "--digests"]) step("14_resources", COMPOSE + ["ps", "--all", "--format", "json"]) result["status"] = "passed" finally: step( "15_cleanup", COMPOSE + ["down", "--volumes", "--remove-orphans"], timeout=180, ) result["finished_at"] = datetime.now(timezone.utc).isoformat() (EVIDENCE / "verification.json").write_text(json.dumps(result, indent=2) + "\n") if __name__ == "__main__": main()