test: verify persistence, replica failover and queued upload recovery

This commit is contained in:
2026-11-30 16:36:14 +03:00
parent c0576a2dee
commit 1743870de7
6 changed files with 113 additions and 0 deletions
+49
View File
@@ -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)
+54
View File
@@ -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]()
+2
View File
@@ -0,0 +1,2 @@
@echo off
python -X utf8 "%~dp0compose_hook.py" restart
+3
View File
@@ -0,0 +1,3 @@
#!/bin/sh
set -eu
exec python3 "$(dirname "$0")/compose_hook.py" restart
+2
View File
@@ -0,0 +1,2 @@
@echo off
python -X utf8 "%~dp0compose_hook.py" stop-one
+3
View File
@@ -0,0 +1,3 @@
#!/bin/sh
set -eu
exec python3 "$(dirname "$0")/compose_hook.py" stop-one