59 lines
2.4 KiB
Python
59 lines
2.4 KiB
Python
#!/usr/bin/env python3
|
|
"""Stop the replica that accepted an upload, then read its bytes elsewhere."""
|
|
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import subprocess
|
|
import sys
|
|
import time
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(ROOT / "tests"))
|
|
from client import Client, Failure # noqa: E402
|
|
|
|
|
|
def main():
|
|
client = Client(timeout=2)
|
|
alice, bob, _, chat = client.bootstrap()
|
|
original = (ROOT / "tests/fixtures/sample.png").read_bytes()
|
|
observed = {client.call("GET", "/api/v1/users/me", actor=bob)["headers"]["X-Instance-Id"] for _ in range(30)}
|
|
if len(observed) != 2:
|
|
raise Failure("two API replicas were not observed before stop")
|
|
for target in sorted(observed):
|
|
for _ in range(10):
|
|
result = client.call(
|
|
"POST", f"/api/v1/chats/{chat['id']}/attachments?filename=sample.png", 201,
|
|
actor=alice, raw=original, headers={"Content-Type": "image/png"}, schema="Attachment",
|
|
)
|
|
if result["headers"]["X-Instance-Id"] == target:
|
|
break
|
|
else:
|
|
raise Failure("upload did not reach the selected replica")
|
|
attachment_id = result["json"]["id"]
|
|
env = {**os.environ, "TARGET_INSTANCE_ID": target}
|
|
try:
|
|
subprocess.run([str(ROOT / "scripts/stop-one.sh")], cwd=ROOT, env=env, check=True)
|
|
deadline = time.monotonic() + 10
|
|
while True:
|
|
try:
|
|
read = client.call("GET", f"/api/v1/attachments/{attachment_id}/content", actor=bob)
|
|
if read["headers"]["X-Instance-Id"] == target:
|
|
raise Failure("stopped replica still answered")
|
|
if read["bytes"] != original:
|
|
raise Failure("attachment bytes changed after replica stop")
|
|
break
|
|
except Failure:
|
|
if time.monotonic() >= deadline:
|
|
raise
|
|
time.sleep(0.2)
|
|
finally:
|
|
subprocess.run(["docker", "compose", "--env-file", ".env", "up", "-d", "--wait", "--scale", "api=2"],
|
|
cwd=ROOT, check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
|
print(json.dumps({"stopped_upload_replicas": len(observed), "download_on_other_replica": True,
|
|
"bytes_equal": True}, sort_keys=True))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|