248 lines
8.3 KiB
Python
248 lines
8.3 KiB
Python
import argparse
|
|
import atexit
|
|
import json
|
|
import os
|
|
import socket
|
|
import subprocess
|
|
import tempfile
|
|
from datetime import UTC, datetime
|
|
from pathlib import Path
|
|
from uuid import uuid4
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
PROGRESS = {}
|
|
|
|
|
|
def save_incomplete():
|
|
receipt = PROGRESS.get("receipt")
|
|
if receipt and receipt.get("result") != "PASS":
|
|
target = PROGRESS["output"].with_name(
|
|
PROGRESS["output"].stem + "-failed.json"
|
|
)
|
|
if target.exists():
|
|
target = target.with_name(
|
|
target.stem + "-" + uuid4().hex[:8] + target.suffix
|
|
)
|
|
target.parent.mkdir(parents=True, exist_ok=True)
|
|
target.write_text(
|
|
json.dumps(
|
|
{**receipt, "result": "INCOMPLETE"},
|
|
ensure_ascii=False,
|
|
indent=2,
|
|
)
|
|
+ "\n"
|
|
)
|
|
|
|
|
|
atexit.register(save_incomplete)
|
|
|
|
|
|
def run(args, *, cwd, env, expected=0):
|
|
result = subprocess.run(
|
|
args, cwd=cwd, env=env, capture_output=True, text=True
|
|
)
|
|
|
|
def redact(text):
|
|
return (
|
|
text.replace(str(cwd), "<clean-clone>")
|
|
.replace(str(ROOT), "<repository>")
|
|
.replace(str(Path.home()), "<home>")
|
|
)
|
|
|
|
if result.returncode != expected:
|
|
if "receipt" in PROGRESS:
|
|
PROGRESS["receipt"]["failed_action"] = redact(" ".join(args))
|
|
raise RuntimeError(redact(result.stdout + result.stderr))
|
|
return redact(result.stdout.strip())
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("--output", default="evidence/clean-clone.json")
|
|
args = parser.parse_args()
|
|
output = ROOT / args.output
|
|
if output.exists():
|
|
raise SystemExit("Choose new output path")
|
|
assert not subprocess.check_output(
|
|
["git", "status", "--porcelain"], cwd=ROOT
|
|
)
|
|
sha = subprocess.check_output(
|
|
["git", "rev-parse", "HEAD"], cwd=ROOT, text=True
|
|
).strip()
|
|
identity = uuid4().hex[:10]
|
|
project = "mtusi-lab2-clean-" + identity
|
|
with socket.socket() as reservation:
|
|
reservation.bind(("127.0.0.1", 0))
|
|
public_port = str(reservation.getsockname()[1])
|
|
env = {
|
|
**os.environ,
|
|
"COMPOSE_PROJECT_NAME": project,
|
|
"API_IMAGE": "mtusi-messenger:lab2-clean-" + identity,
|
|
"PUBLIC_PORT": public_port,
|
|
"VCS_REF": sha,
|
|
"NO_PROXY": "localhost,127.0.0.1",
|
|
"no_proxy": "localhost,127.0.0.1",
|
|
}
|
|
receipt = {
|
|
"date": datetime.now(UTC).isoformat(),
|
|
"source_commit": sha,
|
|
"fresh_secrets_environment_and_volumes": True,
|
|
"command": "python scripts/verify_clean.py",
|
|
"fixed_host_port_for_restart": int(public_port),
|
|
}
|
|
PROGRESS.update(receipt=receipt, output=output)
|
|
with tempfile.TemporaryDirectory(prefix="mtusi-lab2-clean-") as temporary:
|
|
clone = Path(temporary) / "lab2"
|
|
run(
|
|
[
|
|
"git",
|
|
"clone",
|
|
"--no-hardlinks",
|
|
"--branch",
|
|
"solution/bvt2301-maslov",
|
|
str(ROOT),
|
|
str(clone),
|
|
],
|
|
cwd=ROOT,
|
|
env=env,
|
|
)
|
|
for name in [".env", ".venv", "secrets"]:
|
|
assert not (clone / name).exists()
|
|
run(["uv", "sync", "--locked"], cwd=clone, env=env)
|
|
receipt["static_checks"] = run(
|
|
["make", "check", "validate", "lint", "PYTHON=.venv/bin/python"],
|
|
cwd=clone,
|
|
env=env,
|
|
)
|
|
run(["make", "bootstrap"], cwd=clone, env=env)
|
|
try:
|
|
run(
|
|
["docker", "compose", "up", "-d", "--build", "--wait"],
|
|
cwd=clone,
|
|
env=env,
|
|
)
|
|
api = run(
|
|
["docker", "compose", "ps", "-q", "api"], cwd=clone, env=env
|
|
)
|
|
info = json.loads(
|
|
run(["docker", "inspect", api], cwd=clone, env=env)
|
|
)[0]
|
|
endpoint = info["NetworkSettings"]["Ports"]["8080/tcp"][0]
|
|
assert endpoint["HostIp"] == "127.0.0.1"
|
|
env["BASE_URL"] = "http://127.0.0.1:" + endpoint["HostPort"]
|
|
for role, port in [
|
|
("postgres", "5432/tcp"),
|
|
("cache", "6379/tcp"),
|
|
]:
|
|
cid = run(
|
|
["docker", "compose", "ps", "-q", role], cwd=clone, env=env
|
|
)
|
|
state = json.loads(
|
|
run(["docker", "inspect", cid], cwd=clone, env=env)
|
|
)[0]
|
|
assert not state["NetworkSettings"]["Ports"].get(port)
|
|
receipt["internal_store_ports_not_published"] = True
|
|
receipt["smoke"] = run(
|
|
["make", "test", "BASE_URL=" + env["BASE_URL"]],
|
|
cwd=clone,
|
|
env=env,
|
|
)
|
|
receipt["http_tests"] = run(
|
|
[".venv/bin/pytest", "student-tests", "-q"], cwd=clone, env=env
|
|
)
|
|
receipt["public_persistence"] = run(
|
|
[
|
|
"make",
|
|
"test-persistence",
|
|
"ACTION_SCRIPT=scripts/restart.sh",
|
|
"BASE_URL=" + env["BASE_URL"],
|
|
],
|
|
cwd=clone,
|
|
env=env,
|
|
)
|
|
for module, filename in [
|
|
("verify_lifecycle", "clean-lifecycle.json"),
|
|
("verify_pool", "clean-pool.json"),
|
|
]:
|
|
run(
|
|
[
|
|
".venv/bin/python",
|
|
"-m",
|
|
"scripts." + module,
|
|
"--output",
|
|
"evidence/" + filename,
|
|
],
|
|
cwd=clone,
|
|
env=env,
|
|
)
|
|
receipt[module] = json.loads(
|
|
(clone / "evidence" / filename).read_text()
|
|
)
|
|
# Подменяется только временная read-only копия миграции.
|
|
altered = clone / ".work/altered-migrations"
|
|
altered.mkdir(parents=True)
|
|
migration = clone / "migrations/001_core.sql"
|
|
(altered / migration.name).write_bytes(
|
|
migration.read_bytes() + b"\n-- synthetic checksum mismatch\n"
|
|
)
|
|
attempt = subprocess.run(
|
|
[
|
|
"docker",
|
|
"compose",
|
|
"run",
|
|
"--rm",
|
|
"--no-deps",
|
|
"-v",
|
|
str(altered) + ":/app/migrations:ro",
|
|
"migrate",
|
|
],
|
|
cwd=clone,
|
|
env=env,
|
|
capture_output=True,
|
|
text=True,
|
|
)
|
|
assert attempt.returncode != 0
|
|
assert "Applied migration checksum mismatch" in attempt.stderr
|
|
receipt["applied_migration_checksum_guard"] = "PASS"
|
|
run(
|
|
[
|
|
".venv/bin/python",
|
|
"-m",
|
|
"research.query_plan",
|
|
"--output",
|
|
"evidence/clean-query-plan.json",
|
|
],
|
|
cwd=clone,
|
|
env=env,
|
|
)
|
|
research = json.loads(
|
|
(clone / "evidence/clean-query-plan.json").read_text()
|
|
)
|
|
receipt["research_repeated"] = {
|
|
"result": research["result"],
|
|
"messages": research["dataset"]["messages"],
|
|
"plans": len(research["reads"]),
|
|
"restored_snapshot_equal": research["restore_verification"][
|
|
"snapshot_equal"
|
|
],
|
|
}
|
|
receipt["fsck"] = run(
|
|
["git", "fsck", "--full"], cwd=clone, env=env
|
|
)
|
|
receipt["checks_completed"] = True
|
|
finally:
|
|
assert project.startswith("mtusi-lab2-clean-")
|
|
run(
|
|
["docker", "compose", "down", "--volumes", "--remove-orphans"],
|
|
cwd=clone,
|
|
env=env,
|
|
)
|
|
receipt["result"] = "PASS"
|
|
output.parent.mkdir(parents=True, exist_ok=True)
|
|
output.write_text(json.dumps(receipt, ensure_ascii=False, indent=2) + "\n")
|
|
print(json.dumps(receipt, ensure_ascii=False, indent=2))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|