Files
mtusi2026lab7/scripts/test-persistence.py
T
2026-11-20 15:30:33 +03:00

54 lines
1.5 KiB
Python

#!/usr/bin/env python3
"""Public persistence test plus safe proof of all actual storage restarts."""
import json
import subprocess
import sys
from securitylib import ROOT, compose
def snapshot():
ids = compose("ps", "-q", capture=True).split()
data = json.loads(subprocess.check_output(["docker", "inspect", *ids], text=True))
return {
x["Config"]["Labels"]["com.docker.compose.service"] + ":" + x["Id"][:12]: {
"started_at": x["State"]["StartedAt"],
"container": x["Id"][:12],
}
for x in data
}
before = snapshot()
result = subprocess.run(
[
sys.executable,
"tests/resilience.py",
"--mode",
"persistence",
"--action-script",
"scripts/restart.sh",
"--base-url",
"https://localhost:8443",
"--ca-file",
str(ROOT / ".local/trust/ca.crt"),
],
cwd=ROOT,
text=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
)
print(result.stdout, end="")
assert result.returncode == 0
changes = []
after = snapshot()
for key, old in before.items():
new = after[key]
assert old["started_at"] != new["started_at"], key + " not restarted"
changes.append({"service": key, "before": old["started_at"], "after": new["started_at"]})
(ROOT / "evidence/persistence-summary.json").write_text(
json.dumps({"result": "PASS", "restarted_containers": changes}, indent=2) + "\n"
)
(ROOT / "evidence/persistence-checks.txt").write_text(result.stdout)