diff --git a/scripts/check_pipeline_restart.py b/scripts/check_pipeline_restart.py new file mode 100644 index 0000000..4a04b39 --- /dev/null +++ b/scripts/check_pipeline_restart.py @@ -0,0 +1,49 @@ +"""Check that a queued upload survives restarting API and RabbitMQ.""" + +import subprocess +import sys +import time +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT / "tests")) +from client import Client, Failure, arguments, check, run + + +def compose(*args): + subprocess.run(["docker", "compose", *args], cwd=ROOT, check=True) + + +def main(): + args = arguments(__doc__) + client = Client(args.base_url, args.ca_file) + owner = client.user("queue") + chat = client.call("POST", "/api/v1/chats", 201, actor=owner, body={ + "title": "Queue restart", "member_ids": [owner["id"]], + }, schema="Chat")["json"] + + compose("stop", "-t", "5", "worker") + try: + attachment, original = client.attachment(owner, chat) + content_path = "/api/v1/attachments/" + attachment["id"] + "/content" + client.call("GET", content_path, 409, actor=owner) + compose("restart", "-t", "5", "api", "rabbitmq") + finally: + compose("start", "worker") + + deadline = time.monotonic() + 60 + while True: + try: + client.call("GET", "/health/ready", schema="Health") + break + except Failure: + check(time.monotonic() < deadline, "API did not recover after restart") + time.sleep(0.5) + client.wait_ready(owner, attachment) + restored = client.call("GET", content_path, 200, actor=owner)["bytes"] + check(restored == original, "Recovered attachment differs from upload") + print("PASS: queued upload survived API and RabbitMQ restart with worker stopped") + + +if __name__ == "__main__": + run(main) diff --git a/scripts/compose_hook.py b/scripts/compose_hook.py new file mode 100644 index 0000000..ea37dac --- /dev/null +++ b/scripts/compose_hook.py @@ -0,0 +1,54 @@ +"""Compose operator actions used by the public resilience tests.""" + +import argparse +import os +import subprocess +import sys +from pathlib import Path +from uuid import UUID + +ROOT = Path(__file__).resolve().parents[1] +STORAGES = ("openbao", "postgres", "s3", "rabbitmq", "prometheus", "loki", "tempo", "grafana") +APP = ("api", "worker", "relay", "reconciler", "lb") +PROXIES = ("collector-scrape", "prometheus-tls", "loki-tls", "tempo-tls") + + +def run(*args, capture=False): + return subprocess.run(args, cwd=ROOT, check=True, text=True, + stdout=subprocess.PIPE if capture else None).stdout + + +def container_ids(*services): + return run("docker", "compose", "ps", "-q", *services, capture=True).splitlines() + + +def restart(): + ids = container_ids(*STORAGES, *APP, "otel-collector", *PROXIES) + if not ids: + raise RuntimeError("Start the Compose stand before restarting it") + inspect = ("docker", "inspect", "--format", "{{.Name}} {{.State.Status}} {{.State.StartedAt}}", *ids) + run(*inspect) + run("docker", "compose", "restart", "-t", "5", *STORAGES, "otel-collector") + port = run("docker", "compose", "port", "openbao", "8200", capture=True).strip().rsplit(":", 1)[1] + run(sys.executable, "scripts/bao_operator.py", "unseal", "--address", + "https://localhost:" + port) + run("docker", "compose", "restart", "-t", "5", *PROXIES, *APP) + run(*inspect) + + +def stop_one(): + target = str(UUID(os.environ.get("TARGET_INSTANCE_ID", ""))) + matches = [ + container for container in container_ids("api") + if run("docker", "exec", container, "cat", "/tmp/messenger-instance-id", capture=True).strip() == target + ] + if len(matches) != 1: + raise RuntimeError("Expected exactly one running Compose API replica with the supplied alias") + run("docker", "stop", "-t", "3", matches[0]) + run("docker", "inspect", "--format", "{{.Name}} {{.State.Status}} {{.State.FinishedAt}}", matches[0]) + + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("action", choices=("restart", "stop-one")) + {"restart": restart, "stop-one": stop_one}[parser.parse_args().action]() diff --git a/scripts/restart.cmd b/scripts/restart.cmd new file mode 100644 index 0000000..8f3a324 --- /dev/null +++ b/scripts/restart.cmd @@ -0,0 +1,2 @@ +@echo off +python -X utf8 "%~dp0compose_hook.py" restart diff --git a/scripts/restart.sh b/scripts/restart.sh new file mode 100644 index 0000000..0ee14d1 --- /dev/null +++ b/scripts/restart.sh @@ -0,0 +1,3 @@ +#!/bin/sh +set -eu +exec python3 "$(dirname "$0")/compose_hook.py" restart diff --git a/scripts/stop-one.cmd b/scripts/stop-one.cmd new file mode 100644 index 0000000..05c25dc --- /dev/null +++ b/scripts/stop-one.cmd @@ -0,0 +1,2 @@ +@echo off +python -X utf8 "%~dp0compose_hook.py" stop-one diff --git a/scripts/stop-one.sh b/scripts/stop-one.sh new file mode 100644 index 0000000..9b346fb --- /dev/null +++ b/scripts/stop-one.sh @@ -0,0 +1,3 @@ +#!/bin/sh +set -eu +exec python3 "$(dirname "$0")/compose_hook.py" stop-one