forked from mtusi2026/mtusi2026lab6
52 lines
2.3 KiB
Python
52 lines
2.3 KiB
Python
import os
|
|
import re
|
|
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:
|
|
match = re.match(r"^.+?-lab\d+-(.+)-\d+$", name)
|
|
restarted.append(match.group(1) if match else name)
|
|
services = ("postgres", "redis", "s3", "rabbitmq", "api", "janitor", "dispatcher", "worker", "lb",
|
|
"otel-collector", "prometheus", "loki", "tempo", "grafana")
|
|
for service in services:
|
|
check(service in restarted, f"{service} was not restarted by the hook")
|
|
print(f"PASS: the hook restarted {', '.join(services)} (all api replicas; same containers, new start time); "
|
|
f"data, the attachment bytes and the old token survived")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|