forked from mtusi2026/mtusi2026lab3
48 lines
2.0 KiB
Python
48 lines
2.0 KiB
Python
import os
|
|
import subprocess
|
|
import sys
|
|
import time
|
|
|
|
from common import BASE_URL, ROOT, check, compose_output, docker
|
|
|
|
HOOK = ROOT / "scripts" / ("restart.cmd" if os.name == "nt" else "restart.sh")
|
|
|
|
|
|
def started():
|
|
containers = compose_output("ps", "-q").split()
|
|
lines = docker("inspect", "-f", "{{.Name}} {{.Id}} {{.State.StartedAt}}", *containers).splitlines()
|
|
return {name.lstrip("/"): (cid, at) for name, cid, at in (line.split() for line in lines)}
|
|
|
|
|
|
def main():
|
|
before = started()
|
|
print("before: container, ID, started at")
|
|
for name, (cid, at) in sorted(before.items()):
|
|
print(f" {name:<28} {cid[:12]} {at}")
|
|
env = dict(os.environ, PYTHONUTF8="1", PYTHONIOENCODING="utf-8")
|
|
begin = time.monotonic()
|
|
result = subprocess.run([sys.executable, "tests/resilience.py", "--mode", "persistence",
|
|
"--action-script", str(HOOK), "--base-url", BASE_URL],
|
|
cwd=ROOT, capture_output=True, env=env)
|
|
seconds = time.monotonic() - begin
|
|
print((result.stdout + result.stderr).decode("utf-8", "replace").strip())
|
|
check(result.returncode == 0, "public persistence test failed")
|
|
after = started()
|
|
print(f"after (hook + waiting for readiness took {seconds:.1f} s): container, ID, started at, restarted?")
|
|
restarted = []
|
|
for name, (cid, at) in sorted(after.items()):
|
|
same_id = before.get(name, ("", ""))[0] == cid
|
|
new_start = before.get(name, ("", ""))[1] != at
|
|
print(f" {name:<28} {cid[:12]} {at} same container: {same_id}, restarted: {new_start}")
|
|
check(same_id, f"{name}: container was recreated, volumes/IDs must stay")
|
|
if new_start:
|
|
restarted.append(name.split("-")[-2])
|
|
for service in ("postgres", "redis", "api", "lb"):
|
|
check(service in restarted, f"{service} was not restarted by the hook")
|
|
print("PASS: the hook restarted postgres, redis, all api replicas and lb (same containers, new start time); "
|
|
"data and the old token survived")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|