forked from mtusi2026/mtusi2026lab6
202 lines
6.2 KiB
Python
202 lines
6.2 KiB
Python
"""Последовательная проверка лабораторной 6 в отдельном Docker-окружении."""
|
|
|
|
from datetime import datetime, timezone
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import subprocess
|
|
import tarfile
|
|
import time
|
|
|
|
ROOT = Path("/workspace")
|
|
EVIDENCE = ROOT / "evidence"
|
|
COMPOSE = ["docker", "compose", "--env-file", ".env"]
|
|
BASE = ["--base-url", "http://127.0.0.1:28080"]
|
|
|
|
|
|
def step(report, name, command, *, timeout=600, env=None, required=True):
|
|
print("Запуск " + name, flush=True)
|
|
started = time.monotonic()
|
|
log = name + ".txt"
|
|
with (EVIDENCE / log).open("w", encoding="utf-8", newline="\n") as output:
|
|
output.write("$ " + " ".join(command) + "\n")
|
|
output.flush()
|
|
result = subprocess.run(
|
|
command,
|
|
cwd=ROOT,
|
|
stdout=output,
|
|
stderr=subprocess.STDOUT,
|
|
timeout=timeout,
|
|
env={**os.environ, **(env or {})},
|
|
)
|
|
report["checks"].append(
|
|
{
|
|
"name": name,
|
|
"exit_code": result.returncode,
|
|
"duration_seconds": round(time.monotonic() - started, 3),
|
|
"log": log,
|
|
}
|
|
)
|
|
if result.returncode:
|
|
print("Ошибка " + name, flush=True)
|
|
if required:
|
|
result.check_returncode()
|
|
else:
|
|
print("Готово " + name, flush=True)
|
|
|
|
|
|
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 не запустился")
|
|
time.sleep(1)
|
|
|
|
os.environ.update(
|
|
COMPOSE_PROJECT_NAME="messenger-test",
|
|
COMPOSE_FILE="compose.yaml:compose.test.yaml",
|
|
LB_HOST_PORT="28080",
|
|
GRAFANA_HOST_PORT="23000",
|
|
DOCKER_TESTS="1",
|
|
TEST_USE_MIGRATIONS="1",
|
|
TEST_REDIS_HOST="127.0.0.1",
|
|
TEST_REDIS_PORT="26379",
|
|
TEST_REDIS_DB="15",
|
|
)
|
|
integration_only = os.environ.get("INTEGRATION_ONLY") == "1"
|
|
report = {
|
|
"status": "failed",
|
|
"profile": "integration" if integration_only else "full",
|
|
"started_at": datetime.now(timezone.utc).isoformat(),
|
|
"checks": [],
|
|
}
|
|
|
|
try:
|
|
step(report, "00_init", ["python", "scripts/init_local.py"])
|
|
step(report, "01_contract", ["python", "scripts/check_contract.py"])
|
|
step(report, "02_openapi", ["python", "scripts/validate_openapi.py"])
|
|
step(
|
|
report,
|
|
"03_import_images",
|
|
["docker", "image", "load", "--input", "/tmp/images.tar"],
|
|
)
|
|
step(
|
|
report,
|
|
"03_startup",
|
|
COMPOSE + ["up", "-d", "--no-build", "--pull", "never"],
|
|
timeout=3600,
|
|
)
|
|
step(
|
|
report,
|
|
"03_readiness",
|
|
COMPOSE
|
|
+ [
|
|
"up",
|
|
"-d",
|
|
"--no-deps",
|
|
"--wait",
|
|
"--wait-timeout",
|
|
"240",
|
|
"nginx",
|
|
"grafana",
|
|
],
|
|
)
|
|
if not integration_only:
|
|
password = (ROOT / "secrets/db_password").read_text(encoding="utf-8")
|
|
test_env = {
|
|
"OTEL_ENABLED": "false",
|
|
"TEST_DATABASE_URL": (
|
|
f"postgresql+asyncpg://messenger:{password}@127.0.0.1:25432/messenger_test"
|
|
),
|
|
"TEST_REDIS_PASSWORD": (ROOT / "secrets/redis_password").read_text(
|
|
encoding="utf-8"
|
|
),
|
|
}
|
|
step(
|
|
report,
|
|
"04_test_database",
|
|
COMPOSE
|
|
+ [
|
|
"exec",
|
|
"-T",
|
|
"postgres",
|
|
"createdb",
|
|
"-U",
|
|
"messenger",
|
|
"messenger_test",
|
|
],
|
|
)
|
|
step(
|
|
report,
|
|
"05_pytest",
|
|
["python", "-m", "pytest", "-q", "--junitxml=evidence/pytest.xml"],
|
|
timeout=1800,
|
|
env=test_env,
|
|
required=False,
|
|
)
|
|
step(report, "06_smoke", ["python", "tests/smoke.py", *BASE], required=False)
|
|
step(
|
|
report,
|
|
"07_observability",
|
|
[
|
|
"python",
|
|
"-m",
|
|
"scripts.check_observability",
|
|
*BASE,
|
|
"--grafana-url",
|
|
"http://127.0.0.1:23000",
|
|
"--incidents",
|
|
"--restart-telemetry",
|
|
],
|
|
timeout=900,
|
|
required=False,
|
|
)
|
|
for number, mode, hook in (
|
|
("08", "persistence", "restart"),
|
|
("09", "failover", "stop-one"),
|
|
):
|
|
step(
|
|
report,
|
|
f"{number}_{mode}",
|
|
[
|
|
"python",
|
|
"tests/resilience.py",
|
|
"--mode",
|
|
mode,
|
|
"--action-script",
|
|
f"scripts/{hook}.sh",
|
|
*BASE,
|
|
],
|
|
required=False,
|
|
)
|
|
if all(check["exit_code"] == 0 for check in report["checks"]):
|
|
report["status"] = "passed"
|
|
finally:
|
|
try:
|
|
step(
|
|
report,
|
|
"10_cleanup",
|
|
COMPOSE + ["down", "--volumes", "--remove-orphans"],
|
|
timeout=180,
|
|
)
|
|
except (subprocess.SubprocessError, OSError):
|
|
report["status"] = "failed"
|
|
raise
|
|
finally:
|
|
report["finished_at"] = datetime.now(timezone.utc).isoformat()
|
|
(EVIDENCE / "verification.json").write_text(
|
|
json.dumps(report, indent=2, ensure_ascii=False) + "\n",
|
|
encoding="utf-8",
|
|
)
|
|
|
|
if report["status"] != "passed":
|
|
raise SystemExit(1)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|