Skeleton contracts / contracts (pull_request) Canceled after 0s
Co-authored-by: Cursor <cursoragent@cursor.com>
151 lines
5.7 KiB
Python
151 lines
5.7 KiB
Python
#!/usr/bin/env python3
|
|
"""Отказ Redis/PostgreSQL и короткий TTL. В конце стенд возвращается к SESSION_TTL_SECONDS=3600."""
|
|
import os
|
|
import time
|
|
import uuid
|
|
from datetime import datetime, timezone
|
|
from pathlib import Path
|
|
|
|
from httpapi import basic, bearer, call
|
|
from sql import ROOT, compose
|
|
|
|
EVIDENCE = ROOT / "evidence" / "failures.txt"
|
|
LINES = []
|
|
|
|
|
|
def log(text):
|
|
print(text)
|
|
LINES.append(text)
|
|
|
|
|
|
def check(cond, message):
|
|
if not cond:
|
|
log("FAIL: " + message)
|
|
raise SystemExit(1)
|
|
|
|
|
|
def wait_ready(timeout=60):
|
|
deadline = time.time() + timeout
|
|
while time.time() < deadline:
|
|
try:
|
|
status, _, _ = call("GET", "/health/ready", timeout=8)
|
|
except Exception:
|
|
status = 0
|
|
if status == 200:
|
|
return
|
|
time.sleep(0.5)
|
|
raise SystemExit("FAIL: ready timeout")
|
|
|
|
|
|
def limited(payload):
|
|
text = "" if payload is None else str(payload).lower()
|
|
for word in ("redis", "postgres", "traceback", "select ", "password", "argon"):
|
|
check(word not in text, "error leaked " + word)
|
|
|
|
|
|
def recreate_api(ttl):
|
|
env = os.environ.copy()
|
|
env["SESSION_TTL_SECONDS"] = str(ttl)
|
|
proc = __import__("subprocess").run(
|
|
["docker", "compose", "--env-file", ".env", "up", "-d", "--force-recreate", "--no-deps", "--wait", "api"],
|
|
cwd=ROOT,
|
|
env=env,
|
|
stdout=__import__("subprocess").PIPE,
|
|
stderr=__import__("subprocess").PIPE,
|
|
timeout=180,
|
|
check=False,
|
|
)
|
|
if proc.returncode != 0:
|
|
err = proc.stderr.decode("utf-8", "replace")[-500:]
|
|
raise SystemExit("FAIL: recreate api\n" + err)
|
|
wait_ready()
|
|
|
|
|
|
def main():
|
|
suffix = uuid.uuid4().hex[:8]
|
|
password = "fail-password-" + suffix
|
|
username = "fail_" + suffix
|
|
status, _, user = call(
|
|
"POST",
|
|
"/api/v1/users",
|
|
{"username": username, "display_name": "Отказ", "password": password},
|
|
)
|
|
check(status == 201, "register")
|
|
status, _, session = call("POST", "/api/v1/auth/sessions", headers=basic(username, password))
|
|
check(status == 201, "login")
|
|
token = session["token"]
|
|
|
|
log("stop redis")
|
|
compose("stop", "redis")
|
|
try:
|
|
live, _, _ = call("GET", "/health/live", timeout=8)
|
|
ready, _, ready_body = call("GET", "/health/ready", timeout=15)
|
|
anon, anon_headers, anon_body = call("GET", "/api/v1/users/me", timeout=15)
|
|
xid, _, _ = call("GET", "/api/v1/users/me", headers={"X-User-Id": user["id"]}, timeout=15)
|
|
authed, _, authed_body = call("GET", "/api/v1/users/me", headers=bearer(token), timeout=15)
|
|
check(live == 200, "live during redis stop {}".format(live))
|
|
check(ready == 503, "ready during redis stop {}".format(ready))
|
|
check(anon == 401 and anon_headers.get("WWW-Authenticate", "").startswith("Bearer"), "anon {}".format(anon))
|
|
check(xid == 401, "x-user-id during redis stop {}".format(xid))
|
|
check(authed == 503, "bearer during redis stop {}".format(authed))
|
|
limited(ready_body)
|
|
limited(anon_body)
|
|
limited(authed_body)
|
|
log("redis down: live 200, ready 503, anon/x-user-id 401, bearer 503")
|
|
finally:
|
|
compose("start", "redis")
|
|
wait_ready()
|
|
|
|
log("stop postgres")
|
|
compose("stop", "postgres")
|
|
try:
|
|
live, _, _ = call("GET", "/health/live", timeout=8)
|
|
ready, _, _ = call("GET", "/health/ready", timeout=15)
|
|
anon, _, _ = call("GET", "/api/v1/users/me", timeout=15)
|
|
authed, _, authed_body = call("GET", "/api/v1/users/me", headers=bearer(token), timeout=15)
|
|
check(live == 200, "live during postgres stop {}".format(live))
|
|
check(ready == 503, "ready during postgres stop {}".format(ready))
|
|
check(anon == 401, "anon during postgres stop {}".format(anon))
|
|
check(authed == 503, "bearer during postgres stop {}".format(authed))
|
|
limited(authed_body)
|
|
log("postgres down: live 200, ready 503, anon 401, bearer 503")
|
|
finally:
|
|
compose("start", "postgres")
|
|
wait_ready()
|
|
|
|
log("session ttl 2s")
|
|
recreate_api(2)
|
|
try:
|
|
status, _, short = call("POST", "/api/v1/auth/sessions", headers=basic(username, password))
|
|
check(status == 201, "short login")
|
|
expires = datetime.fromisoformat(short["expires_at"].replace("Z", "+00:00"))
|
|
delta = (expires - datetime.now(timezone.utc)).total_seconds()
|
|
check(1 <= delta <= 4, "ttl delta {}".format(delta))
|
|
time.sleep(delta + 1.2)
|
|
status, headers, _ = call("GET", "/api/v1/users/me", headers=bearer(short["token"]), timeout=15)
|
|
check(status == 401 and headers.get("WWW-Authenticate", "").startswith("Bearer"), "expired {}".format(status))
|
|
log("expired session -> 401, delta was {:.2f}s".format(delta))
|
|
finally:
|
|
recreate_api(3600)
|
|
status, _, restored = call("POST", "/api/v1/auth/sessions", headers=basic(username, password))
|
|
check(status == 201, "ttl restored login")
|
|
expires = datetime.fromisoformat(restored["expires_at"].replace("Z", "+00:00"))
|
|
delta = (expires - datetime.now(timezone.utc)).total_seconds()
|
|
check(delta > 300, "ttl not restored {}".format(delta))
|
|
log("ttl restored, expires in {:.0f}s".format(delta))
|
|
|
|
log("PASS failure checks")
|
|
EVIDENCE.write_text("\n".join(LINES) + "\n", encoding="utf-8")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
try:
|
|
main()
|
|
except SystemExit:
|
|
EVIDENCE.write_text("\n".join(LINES) + "\n", encoding="utf-8")
|
|
raise
|
|
except Exception as exc:
|
|
log("FAIL: {}: {}".format(type(exc).__name__, exc))
|
|
EVIDENCE.write_text("\n".join(LINES) + "\n", encoding="utf-8")
|
|
raise SystemExit(1)
|