forked from mtusi2026/mtusi2026lab2
87 lines
4.0 KiB
Python
Executable File
87 lines
4.0 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""Valkey's role and failures of each dependency. STOPS AND STARTS containers of this stack.
|
|
|
|
- Valkey really caches sessions and holds logout tombstones.
|
|
- Valkey down: API keeps working (cache bypassed), readiness stays 200.
|
|
- PostgreSQL down: bounded 503 quickly, readiness 503, liveness 200; an unverifiable
|
|
token is never accepted; no token is still 401. Recovery after start.
|
|
"""
|
|
import time
|
|
|
|
from stack import BASE_URL, Client, Failure, check, compose, main_wrapper, ok, valkey
|
|
|
|
|
|
def wait_ready(c, seconds=60):
|
|
deadline = time.monotonic() + seconds
|
|
while time.monotonic() < deadline:
|
|
try:
|
|
c.call("GET", "/health/ready", 200)
|
|
return
|
|
except Failure:
|
|
time.sleep(0.5)
|
|
raise Failure("API did not become ready")
|
|
|
|
|
|
def main():
|
|
c = Client(BASE_URL, timeout=10)
|
|
alice, bob, _eve, chat = c.bootstrap()
|
|
history = "/api/v1/chats/" + chat["id"] + "/messages"
|
|
|
|
# Role of Valkey: a session lookup fills the cache, logout leaves a tombstone.
|
|
before = int(valkey("DBSIZE"))
|
|
c.call("GET", "/api/v1/users/me", actor=alice, schema="User")
|
|
keys = valkey("--scan", "--pattern", "session:*").split()
|
|
check(len(keys) >= 1 and int(valkey("DBSIZE")) >= before, "session lookups must be cached in Valkey")
|
|
ttl = int(valkey("TTL", keys[0]))
|
|
check(0 < ttl <= 300, "cache entries must expire (TTL %d)" % ttl)
|
|
temp = c.user("tomb")
|
|
c.call("GET", "/api/v1/users/me", actor=temp, schema="User")
|
|
c.call("DELETE", "/api/v1/auth/sessions/current", 204, actor=temp)
|
|
tombstones = [k for k in valkey("--scan", "--pattern", "session:*").split() if valkey("GET", k) == "revoked"]
|
|
check(tombstones, "logout must leave a 'revoked' tombstone in the cache")
|
|
ok("Valkey caches sessions with TTL<=300s and keeps logout tombstones")
|
|
|
|
# Valkey down -> the API bypasses the cache, PostgreSQL stays the source of truth.
|
|
try:
|
|
compose("stop", "valkey")
|
|
c.call("GET", "/health/ready", 200)
|
|
c.call("GET", "/api/v1/users/me", actor=alice, schema="User")
|
|
c.message(bob, chat, "без кэша")
|
|
c.call("GET", "/api/v1/users/me", 401, actor=temp) # revoked in PostgreSQL too
|
|
start = time.monotonic()
|
|
for _ in range(10):
|
|
c.call("GET", history + "?limit=1", actor=alice, schema="MessagePage")
|
|
per_request = (time.monotonic() - start) / 10
|
|
check(per_request < 0.5, "cache bypass must be fast (%.3fs per request)" % per_request)
|
|
ok("Valkey stopped: ready=200, reads/writes OK, revoked token 401, %.0f ms/request" % (per_request * 1000))
|
|
finally:
|
|
compose("start", "valkey")
|
|
|
|
# PostgreSQL down -> bounded 503, never anonymous access.
|
|
fresh = c.user("dbdown") # token never looked up yet: not in the cache
|
|
try:
|
|
compose("stop", "postgres")
|
|
c.call("GET", "/health/live", 200)
|
|
start = time.monotonic()
|
|
r = c.call("GET", "/health/ready", 503)
|
|
check(r["json"]["error"]["code"] == "unavailable", "readiness must report unavailable")
|
|
c.call("GET", "/api/v1/chats", 503, actor=alice)
|
|
c.call("POST", history, 503, actor=alice, body={"text": "в никуда"})
|
|
c.call("GET", "/api/v1/users/me", 503, actor=fresh) # cannot verify -> not trusted
|
|
c.call("GET", "/api/v1/users/me", 401) # no credentials at all
|
|
elapsed = time.monotonic() - start
|
|
check(elapsed < 20, "errors must be bounded in time (%.1fs)" % elapsed)
|
|
ok("PostgreSQL stopped: live=200, ready/business=503 in %.1fs total, unverified token not trusted" % elapsed)
|
|
finally:
|
|
compose("start", "postgres")
|
|
|
|
wait_ready(c)
|
|
c.call("GET", "/api/v1/users/me", actor=fresh, schema="User")
|
|
page = c.call("GET", history + "?limit=100", actor=alice, schema="MessagePage")["json"]
|
|
check(any(m["text"] == "без кэша" for m in page["items"]), "message written without cache must persist")
|
|
ok("PostgreSQL started again: API recovers without restart, data intact")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main_wrapper(main)
|