Files
mtusi2026lab5-fork/scripts/test.py
2026-11-16 14:16:01 +03:00

190 lines
6.2 KiB
Python

"""Запускает проверки в одноразовом Docker-окружении и сохраняет evidence."""
import argparse
from datetime import datetime, timezone
import io
import json
from pathlib import Path
import subprocess
import tarfile
import tempfile
from uuid import uuid4
ROOT = Path(__file__).resolve().parents[1]
EXCLUDED = {
".git",
".agents",
".aws",
".codex",
".env",
"secrets",
"evidence",
".venv",
"venv",
"__pycache__",
".pytest_cache",
".github",
"docs",
}
def docker(*args, **kwargs):
return subprocess.run(["docker", *args], cwd=ROOT, check=True, **kwargs)
def archive_source(destination):
with tarfile.open(destination, "w") as archive:
for path in sorted(ROOT.rglob("*")):
relative = path.relative_to(ROOT)
if not path.is_file() or any(part in EXCLUDED for part in relative.parts):
continue
content = path.read_bytes()
if path.suffix in {".py", ".sh", ".conf", ".yaml", ".txt", ".md", ".ini"}:
content = content.replace(b"\r\n", b"\n")
entry = tarfile.TarInfo(str(relative).replace("\\", "/"))
entry.size = len(content)
entry.mode = 0o755 if path.suffix == ".sh" else 0o644
archive.addfile(entry, io.BytesIO(content))
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--format", action="store_true", help="применить Black и выйти")
args = parser.parse_args()
project = "messenger-check-" + uuid4().hex[:12]
image = project + ":tools"
engine = project + "-engine"
runner = project + "-runner"
workspace = project + "-workspace"
data = project + "-data"
network = project + "-network"
output = ROOT / "evidence" / "lab5"
output.mkdir(parents=True, exist_ok=True)
summary = {
"status": "failed",
"project": project,
"started_at": datetime.now(timezone.utc).isoformat(),
}
try:
docker("build", "--tag", image, "--file", "infra/testing/Dockerfile", ".")
if args.format:
docker(
"run",
"--rm",
"--volume",
str(ROOT) + ":/workspace",
image,
"python3",
"-m",
"black",
"app",
"infra",
"migrations",
"scripts",
"student-tests",
"tests",
)
summary["status"] = "formatted"
return
docker("network", "create", network, stdout=subprocess.DEVNULL)
for volume in (workspace, data):
docker("volume", "create", volume, stdout=subprocess.DEVNULL)
docker(
"run",
"-d",
"--privileged",
"--name",
engine,
"--network",
network,
"--env",
"DOCKER_TLS_CERTDIR=",
"--volume",
workspace + ":/workspace",
"--volume",
data + ":/var/lib/docker",
"docker:29.1.3-dind",
"dockerd",
"--host=unix:///var/run/docker.sock",
"--host=tcp://127.0.0.1:2375",
"--tls=false",
stdout=subprocess.DEVNULL,
)
docker(
"create",
"--name",
runner,
"--network",
"container:" + engine,
"--env",
"DOCKER_HOST=tcp://127.0.0.1:2375",
"--volume",
workspace + ":/workspace",
image,
stdout=subprocess.DEVNULL,
)
with tempfile.TemporaryDirectory(prefix="messenger-check-") as directory:
archive = Path(directory) / "source.tar"
archive_source(archive)
docker("cp", str(archive), runner + ":/tmp/source.tar")
docker("start", runner, stdout=subprocess.DEVNULL)
result = subprocess.run(
["docker", "wait", runner],
cwd=ROOT,
text=True,
capture_output=True,
timeout=7200,
)
result.check_returncode()
docker("logs", runner)
docker("cp", runner + ":/workspace/evidence/.", str(output))
if int(result.stdout.strip()) != 0:
raise RuntimeError("Проверки завершились ошибкой; см. evidence/lab5")
summary["status"] = "passed"
finally:
cleanup = []
for command in (
["rm", "--force", runner, engine],
["volume", "rm", workspace, data],
["network", "rm", network],
["image", "rm", image],
):
result = subprocess.run(
["docker", *command], cwd=ROOT, text=True, capture_output=True
)
cleanup.append(
{
"command": ["docker", *command],
"returncode": result.returncode,
"output": result.stdout.strip() + result.stderr.strip(),
}
)
remaining = {}
for kind in ("container", "volume", "network", "image"):
result = subprocess.run(
["docker", kind, "ls", "--format", "{{json .}}"],
cwd=ROOT,
text=True,
capture_output=True,
)
remaining[kind] = [
line for line in result.stdout.splitlines() if project in line
]
if result.returncode:
remaining[kind].append("resource audit failed")
summary["cleanup"] = cleanup
summary["remaining_resources"] = remaining
summary["finished_at"] = datetime.now(timezone.utc).isoformat()
if any(remaining.values()):
summary["status"] = "failed"
(output / "run.json").write_text(
json.dumps(summary, indent=2, ensure_ascii=False) + "\n", encoding="utf-8"
)
if summary["status"] == "failed":
raise SystemExit(1)
print("PASS: проверки завершены, временное окружение удалено")
if __name__ == "__main__":
main()