64 lines
3.5 KiB
Python
64 lines
3.5 KiB
Python
"""Temporarily pause Redis / stop PostgreSQL in the local stack; always restore them."""
|
|
import argparse
|
|
import hashlib
|
|
|
|
from compose_helpers import compose, ready, redis
|
|
from client import Client, check, run
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("--base-url", default="http://localhost:8080")
|
|
args = parser.parse_args()
|
|
client = Client(args.base_url, timeout=15)
|
|
alice, bob, _, chat = client.bootstrap()
|
|
key = "messenger:session:" + hashlib.sha256(alice["token"].encode("ascii")).hexdigest()
|
|
check(redis("EXISTS", key) == "1", "session cache was not warmed")
|
|
cached_snapshot = redis("GET", key)
|
|
|
|
# Pause preserves the stale entry while all Redis commands time out.
|
|
try:
|
|
compose("pause", "redis")
|
|
response = client.call("GET", "/api/v1/users/me", actor=alice, schema="User")
|
|
check(response["json"] == alice["user"] and response["elapsed"] < 3,
|
|
"Redis outage did not fall back within 3 seconds")
|
|
client.call("GET", "/health/ready", schema="Health")
|
|
client.call("GET", "/api/v1/users/me", 401, headers={"X-User-Id": alice["id"]})
|
|
client.call("GET", "/api/v1/users/me", 401, headers={"Authorization": "Bearer " + "A" * 43})
|
|
message = client.message(bob, chat, "Redis недоступен: запись сохраняется")["json"]
|
|
client.call("DELETE", "/api/v1/auth/sessions/current", 204, actor=alice)
|
|
client.call("GET", "/api/v1/users/me", 401, actor=alice)
|
|
finally:
|
|
compose("unpause", "redis")
|
|
# A timed-out command may still execute after unpause. Recreate a stale entry
|
|
# deterministically to model a concurrent cache fill completing after logout.
|
|
redis("SET", key, cached_snapshot, "PX", "60000")
|
|
client.call("GET", "/api/v1/users/me", 401, actor=alice)
|
|
print("PASS: Redis timeout fallback <3s, writes work, anonymous access denied, stale entry cannot undo logout")
|
|
|
|
# Redis may still hold Bob's valid entry; it cannot authorize when PostgreSQL is down.
|
|
client.call("GET", "/api/v1/users/me", actor=bob, schema="User")
|
|
try:
|
|
compose("stop", "postgres")
|
|
client.call("GET", "/health/live", schema="Health")
|
|
unavailable = client.call("GET", "/health/ready", 503)
|
|
check(unavailable["json"]["error"]["code"] == "unavailable", "wrong readiness failure")
|
|
response = client.call("GET", "/api/v1/users/me", 503, actor=bob)
|
|
check(response["elapsed"] < 12 and response["json"]["error"]["code"] == "unavailable",
|
|
"PostgreSQL failure is unbounded or trusts cached identity")
|
|
client.call("POST", "/api/v1/chats/" + chat["id"] + "/messages", 503,
|
|
actor=bob, body={"text": "Не подтверждать при отказе БД"})
|
|
client.call("GET", "/api/v1/users/me", 401, headers={"X-User-Id": bob["id"]})
|
|
finally:
|
|
compose("start", "postgres")
|
|
ready(client)
|
|
client.call("GET", "/api/v1/users/me", actor=bob, schema="User")
|
|
page = client.call("GET", "/api/v1/chats/" + chat["id"] + "/messages", actor=bob, schema="MessagePage")["json"]
|
|
check(message in page["items"], "confirmed write during Redis outage was lost")
|
|
check(not any(item["text"] == "Не подтверждать при отказе БД" for item in page["items"]), "failed DB write was committed")
|
|
print("PASS: PostgreSQL outage -> bounded 503; cache cannot authorize; data and session recover")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
run(main)
|