80 lines
3.2 KiB
Python
80 lines
3.2 KiB
Python
"""Sequential real image replacement under message traffic."""
|
|
import json
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
import threading
|
|
import time
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(ROOT / "tests"))
|
|
from client import Client, Failure
|
|
|
|
|
|
def snapshots():
|
|
ids = subprocess.check_output(["docker", "compose", "ps", "-q", "api"], cwd=ROOT, text=True).split()
|
|
rows = []
|
|
for cid in ids:
|
|
raw = json.loads(subprocess.check_output(["docker", "inspect", cid], text=True))[0]
|
|
rows.append({"id": cid, "image_id": raw["Image"], "started_at": raw["State"]["StartedAt"]})
|
|
return rows
|
|
|
|
|
|
def main():
|
|
subprocess.run(["docker", "build", "-t", "mtusi-vasilev:lab3-rolling", "-"],
|
|
input="FROM mtusi-vasilev:lab3\nLABEL org.mtusi.rolling-proof=revision-2\n", text=True, check=True)
|
|
before = snapshots()
|
|
c = Client("http://127.0.0.1:8083")
|
|
actor, _, _, chat = c.bootstrap()
|
|
stop = threading.Event()
|
|
successes, errors = [], []
|
|
def traffic():
|
|
while not stop.is_set():
|
|
try:
|
|
response = c.message(actor, chat, "rolling-proof")
|
|
successes.append({"id": response["json"]["id"], "at": time.monotonic(),
|
|
"instance": response["headers"]["X-Instance-Id"]})
|
|
except Failure:
|
|
errors.append(time.monotonic())
|
|
started = time.monotonic()
|
|
thread = threading.Thread(target=traffic)
|
|
thread.start()
|
|
try:
|
|
time.sleep(1)
|
|
subprocess.run(["scripts/rolling-update.sh"], cwd=ROOT,
|
|
env={**os.environ, "API_IMAGE": "mtusi-vasilev:lab3-rolling"}, check=True)
|
|
time.sleep(2)
|
|
finally:
|
|
stop.set()
|
|
thread.join()
|
|
elapsed = time.monotonic() - started
|
|
after = snapshots()
|
|
assert not {x["id"] for x in before} & {x["id"] for x in after}
|
|
assert {x["image_id"] for x in before} != {x["image_id"] for x in after}
|
|
items, cursor = [], None
|
|
while True:
|
|
path = f"/api/v1/chats/{chat['id']}/messages?limit=100"
|
|
if cursor:
|
|
path += "&cursor=" + cursor
|
|
page = c.call("GET", path, actor=actor, schema="MessagePage")["json"]
|
|
items.extend(page["items"])
|
|
cursor = page["next_cursor"]
|
|
if cursor is None:
|
|
break
|
|
assert {x["id"] for x in successes} <= {x["id"] for x in items}
|
|
times = [started, *[x["at"] for x in successes], started + elapsed]
|
|
result = {"before": before, "after": after, "successful_writes": len(successes),
|
|
"failed_attempts": len(errors), "elapsed_seconds": elapsed,
|
|
"successful_rps": len(successes) / elapsed,
|
|
"observed_instances": sorted({x["instance"] for x in successes}),
|
|
"largest_success_gap_seconds": max(b-a for a,b in zip(times,times[1:])),
|
|
"all_confirmed_present": True,
|
|
"note": "Target image differs by revision label; same API/schema. This verifies sequential replacement, not arbitrary schema compatibility."}
|
|
(ROOT / "evidence/rolling-update.json").write_text(json.dumps(result, indent=2) + "\n")
|
|
print(json.dumps(result, indent=2))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|