import os import re import subprocess import sys import time from common import BASE_URL, CA_FILE, 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, "--ca-file", CA_FILE], 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) or re.match(r"^(.+?)(?:-\d+)?$", name) restarted.append(match.group(1)) # lab7: + openbao (рестарт и unseal оператором в hook) и otel-gate services = ("postgres", "redis", "s3", "rabbitmq", "openbao", "api", "janitor", "dispatcher", "worker", "lb", "otel-gate", "otel-collector", "prometheus", "loki", "tempo", "grafana") present = {re.sub(r"^.+?-lab\d+-(.+)-\d+$", r"\1", n) if re.match(r"^.+?-lab\d+-", n) else re.sub(r"-\d+$", "", n) for n in after} services = tuple(s for s in services if s in present) # песочница: без 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()