Files
mtusi2026lab2/scripts/verify_lifecycle.py
T

291 lines
9.7 KiB
Python

import argparse
import hashlib
import json
import time
from datetime import UTC, datetime
from pathlib import Path
from uuid import uuid4
import httpx
from scripts.stand import (
BASE,
ROOT,
actor,
auth,
cache,
command,
compose,
extra_api,
instances,
ready,
sql,
)
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--output", required=True)
args = parser.parse_args()
output = Path(args.output)
if output.exists():
raise SystemExit("Choose new output path")
receipt = {
"date": datetime.now(UTC).isoformat(),
"commit": command(["git", "rev-parse", "HEAD"]),
"command": "python -m scripts.verify_lifecycle",
}
ready()
source_hashes = {
p.name: hashlib.sha256(p.read_bytes()).hexdigest()
for p in (ROOT / "src").glob("*.py")
}
installed_hashes = json.loads(
compose(
"exec",
"-T",
"api",
"python",
"-c",
"import hashlib,json;from pathlib import Path;"
"print(json.dumps({p.name:"
"hashlib.sha256(p.read_bytes()).hexdigest() "
"for p in Path('/app/src').glob('*.py')}))",
)
)
assert source_hashes == installed_hashes
receipt["runtime_source_hashes_match"] = True
api_info = json.loads(
command(["docker", "inspect", compose("ps", "-q", "api")])
)[0]
receipt["image_revision"] = api_info["Config"]["Labels"][
"org.opencontainers.image.revision"
]
with httpx.Client(base_url=BASE, trust_env=False, timeout=15) as client:
user, password, session = actor(client)
headers = auth(session)
chat = client.post(
"/api/v1/chats",
headers=headers,
json={"title": "Lifecycle", "member_ids": [user["id"]]},
).json()
path = "/api/v1/chats/" + chat["id"] + "/messages"
message = client.post(
path, headers=headers, json={"text": "Synthetic retained message"}
)
assert message.status_code == 201
message_id = message.json()["id"]
before = instances()
started = time.perf_counter()
command(["sh", "scripts/restart.sh"])
ready()
after = instances()
for role in before:
assert before[role]["started_at"] != after[role]["started_at"]
assert (
client.get(path, headers=headers).json()["items"][0]["id"]
== message_id
)
receipt["persistence"] = {
"same_token": True,
"same_message": True,
"before": before,
"after": after,
"seconds": time.perf_counter() - started,
}
migration_count = sql("SELECT count(*) FROM schema_migrations")
compose("run", "--rm", "--no-deps", "migrate")
assert sql("SELECT count(*) FROM schema_migrations") == migration_count
receipt["migration_repeat"] = "PASS"
files_before = {
p.name: hashlib.sha256(p.read_bytes()).hexdigest()
for p in (ROOT / "secrets").iterdir()
}
command(["python3", "scripts/bootstrap.py"])
assert files_before == {
p.name: hashlib.sha256(p.read_bytes()).hexdigest()
for p in (ROOT / "secrets").iterdir()
}
receipt["bootstrap_preserves_secrets"] = True
dump = compose(
"exec",
"-T",
"postgres",
"pg_dump",
"-U",
"messenger",
"--data-only",
"messenger",
)
assert password not in dump and session["token"] not in dump
del dump
privacy = json.loads(
sql(
"SELECT json_build_object('argon2id',"
"password_hash LIKE '$argon2id$%','digest_bytes',"
"(SELECT min(octet_length(token_digest)) FROM sessions "
"WHERE user_id=users.id)) FROM users WHERE id='"
+ user["id"]
+ "'"
)
)
assert privacy == {"argon2id": True, "digest_bytes": 32}
receipt["credential_storage"] = {
**privacy,
"raw_password_and_token_absent_in_dump": True,
}
cache("FLUSHDB")
assert (
client.get("/api/v1/users/me", headers=headers).status_code == 200
)
info_before = cache("INFO", "stats")
for _ in range(20):
assert (
client.get("/api/v1/users/me", headers=headers).status_code
== 200
)
info_after = cache("INFO", "stats")
def hits(value):
return int(
next(
line.split(":")[1]
for line in value.splitlines()
if line.startswith("keyspace_hits:")
)
)
assert hits(info_after) - hits(info_before) >= 20
receipt["cache_hits"] = hits(info_after) - hits(info_before)
compose("stop", "cache")
try:
started = time.perf_counter()
assert (
client.get("/api/v1/users/me", headers=headers).status_code
== 200
)
receipt["cache_stopped"] = {
"status": 200,
"seconds": time.perf_counter() - started,
}
finally:
compose("up", "-d", "--no-recreate", "--wait", "cache")
cache("FLUSHDB")
assert (
client.get(path, headers=headers).json()["items"][0]["id"]
== message_id
)
receipt["cache_flush_preserves_truth"] = True
for mode in ["pause", "stop"]:
compose(mode, "postgres")
try:
started = time.perf_counter()
result = client.get("/api/v1/users/me", headers=headers)
elapsed = time.perf_counter() - started
assert result.status_code == 503 and elapsed < 6
assert result.json()["error"]["code"] == "unavailable"
assert (
result.headers["x-request-id"]
== result.json()["error"]["request_id"]
)
assert client.get("/health/live").status_code == 200
receipt["postgres_" + mode] = {
"status": result.status_code,
"seconds": elapsed,
"cached_identity_not_trusted": True,
}
finally:
if mode == "pause":
compose("unpause", "postgres")
else:
compose("up", "-d", "--no-recreate", "--wait", "postgres")
ready(timeout=45)
assert (
client.get(path, headers=headers).json()["items"][0]["id"]
== message_id
)
# Отказ посередине вставки участников откатывает весь чат.
outsider, _, _ = actor(client)
title = "rollback_" + uuid4().hex
function = "rollback_" + uuid4().hex[:8]
sql(
"CREATE FUNCTION "
+ function
+ "() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN "
+ "IF NEW.user_id='"
+ outsider["id"]
+ "'::uuid THEN RAISE EXCEPTION 'synthetic member failure'; "
+ "END IF; RETURN NEW; END $$; CREATE TRIGGER "
+ function
+ " BEFORE INSERT ON members FOR EACH ROW EXECUTE FUNCTION "
+ function
+ "()"
)
try:
result = client.post(
"/api/v1/chats",
headers=headers,
json={
"title": title,
"member_ids": [user["id"], outsider["id"]],
},
)
assert result.status_code == 500
assert (
sql("SELECT count(*) FROM chats WHERE title='" + title + "'")
== "0"
)
receipt["chat_transaction_rollback"] = {
"injected_failure_status": 500,
"remaining_chat_rows": 0,
}
finally:
sql(
"DROP TRIGGER "
+ function
+ " ON members; DROP FUNCTION "
+ function
+ "()"
)
# Короткий TTL — на отдельном API; обычный smoke сохраняет TTL3600.
name, base = extra_api(ttl=2)
try:
with httpx.Client(
base_url=base, trust_env=False, timeout=15
) as short:
_, _, expiring = actor(short)
assert (
short.get(
"/api/v1/users/me", headers=auth(expiring)
).status_code
== 200
)
time.sleep(2.2)
assert (
short.get(
"/api/v1/users/me", headers=auth(expiring)
).status_code
== 401
)
receipt["expiry"] = {"ttl_seconds": 2, "status_after_expiry": 401}
finally:
command(["docker", "rm", "-f", name])
assert (
client.delete(
"/api/v1/auth/sessions/current", headers=headers
).status_code
== 204
)
assert (
client.get("/api/v1/users/me", headers=headers).status_code == 401
)
receipt["logout_after_cache_hit"] = "PASS"
receipt["result"] = "PASS"
output.parent.mkdir(parents=True, exist_ok=True)
output.write_text(json.dumps(receipt, ensure_ascii=False, indent=2) + "\n")
print(json.dumps(receipt, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()