Files
mtusi2026lab2/tests/grade4.py
T
2026-11-10 23:47:45 +03:00

107 lines
3.5 KiB
Python

#!/usr/bin/env python3
import base64
import subprocess
from concurrent.futures import ThreadPoolExecutor
from client import Client, Failure, arguments, check, run
def main():
args = arguments("Grade 4 concurrency and dependency outage checks")
client = Client(args.base_url, args.ca_file, timeout=4)
client.call("GET", "/health/ready", schema="Health")
username = "race_" + __import__("uuid").uuid4().hex[:16]
password = "Concurrent-test-" + __import__("uuid").uuid4().hex
registration = {
"username": username,
"display_name": "Registration race",
"password": password,
}
def register_once():
return Client(args.base_url, args.ca_file, timeout=10).call(
"POST", "/api/v1/users", expected=[201, 409], body=registration
)["status"]
with ThreadPoolExecutor(max_workers=8) as executor:
statuses = list(executor.map(lambda _: register_once(), range(8)))
check(statuses.count(201) == 1, "concurrent registration created other than one user")
check(statuses.count(409) == 7, "duplicate registrations did not return 409")
credentials = base64.b64encode((username + ":wrong-password-123").encode()).decode()
client.call(
"POST",
"/api/v1/auth/sessions",
expected=401,
headers={"Authorization": "Basic " + credentials},
)
actor = client.user("grade4")
client.call("GET", "/api/v1/users/me", expected=401)
missing_id = str(__import__("uuid").uuid4())
rollback_title = "invalid-member-" + __import__("uuid").uuid4().hex
client.call(
"POST",
"/api/v1/chats",
expected=400,
actor=actor,
body={"title": rollback_title, "member_ids": [actor["id"], missing_id]},
)
chats = client.call(
"GET", "/api/v1/chats?limit=100", actor=actor, schema="ChatPage"
)["json"]["items"]
check(all(chat["title"] != rollback_title for chat in chats), "invalid chat was persisted")
client.call("GET", "/api/v1/users/me", actor=actor, schema="User")
subprocess.run(
["docker", "compose", "stop", "redis"],
check=True,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
try:
client.call("GET", "/api/v1/users/me", actor=actor, schema="User")
finally:
subprocess.run(
["docker", "compose", "start", "redis"],
check=True,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
client.call("GET", "/health/ready", schema="Health")
subprocess.run(
["docker", "compose", "stop", "postgres"],
check=True,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
try:
client.call("GET", "/health/ready", expected=503)
client.call("GET", "/api/v1/users/me", expected=503, actor=actor)
finally:
subprocess.run(
["docker", "compose", "start", "postgres"],
check=True,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
for _ in range(60):
try:
client.call("GET", "/health/ready", schema="Health")
break
except Failure:
import time
time.sleep(1)
else:
raise Failure("PostgreSQL did not recover")
client.call("DELETE", "/api/v1/auth/sessions/current", expected=204, actor=actor)
client.call("GET", "/api/v1/users/me", expected=401, actor=actor)
print("PASS: registration race, invalid password, logout and dependency outages")
if __name__ == "__main__":
run(main)