ЛР 2: собственные тесты (student_tests), lifecycle и SIGTERM под compose

This commit is contained in:
2026-11-03 11:33:16 +03:00
parent aa1b644585
commit 3feb4a9da3
6 changed files with 810 additions and 0 deletions
+7
View File
@@ -0,0 +1,7 @@
PASS two users created and read two messages: HTTP 201, then HTTP 200
PASS third user cannot read or write the chat: HTTP 404
PASS request logs include IDs and statuses, without message contents
Command: scripts/restart.sh (API + PostgreSQL + Valkey, volumes kept)
PASS after restart: same tokens read the same history
PASS logout revokes only alice's session; bob's session still works
PASS persistent lifecycle: data and sessions survive full restart
+6
View File
@@ -0,0 +1,6 @@
Image: mtusi-messenger:lab2
Scenario: idle; docker stop --timeout 10 mtusi-messenger-sigterm-60edb300
PASS idle: exit code 0; shutdown 0.339s; new connections rejected after 0.119s
Scenario: incomplete POST; docker stop --timeout 10 mtusi-messenger-sigterm-859f8a87
PASS incomplete POST: exit code 0; shutdown 8.356s; new connections rejected after 0.117s
PASS SIGTERM: both scenarios completed within 10 seconds
+15
View File
@@ -0,0 +1,15 @@
Testing http://localhost:8080
PASS malformed JSON and invalid UTF-8 -> 400 with error envelope
PASS duplicate username -> 409
PASS outsider cannot access chat -> 404
PASS empty/whitespace message -> 400
PASS extra JSON field -> 400
PASS pagination: 7 messages read through multiple pages
PASS concurrency: 10 clients sent 100 messages; exactly 100 unique messages stored
PASS invalid chat UUID -> 400; unknown valid UUID -> 404
PASS chat members: 1..100, unique, existing, caller included
PASS invalid cursor/limit and cursor from another chat -> 400
PASS pagination: 7 chats, changing limit between pages
PASS strict types, Unicode, password rules, Bearer-only identification
PASS: 12 student test scenarios completed
+585
View File
@@ -0,0 +1,585 @@
#!/usr/bin/env python3
import concurrent.futures
import json
import os
import sys
import uuid
import urllib.error
import urllib.parse
import urllib.request
from datetime import datetime
import base64
BASE_URL = os.environ.get("BASE_URL", "http://localhost:8080").rstrip("/")
PASSWORD = "correct-horse-battery"
def request(method, path, expected, body=None, headers=None, raw_body=None):
url = BASE_URL + path
request_headers = dict(headers or {})
if raw_body is not None:
data = raw_body
elif body is not None:
data = json.dumps(body, ensure_ascii=False).encode("utf-8")
else:
data = None
if body is not None or raw_body is not None:
request_headers.setdefault("Content-Type", "application/json")
req = urllib.request.Request(
url,
data=data,
method=method,
headers=request_headers,
)
try:
with urllib.request.urlopen(req, timeout=10) as response:
status = response.status
response_headers = response.headers
response_body = response.read()
except urllib.error.HTTPError as exc:
status = exc.code
response_headers = exc.headers
response_body = exc.read()
if status != expected:
raise AssertionError(
f"{method} {path}: expected HTTP {expected}, got {status}. "
f"Body={response_body!r}"
)
request_id = response_headers.get("X-Request-Id")
if not request_id:
raise AssertionError(
f"{method} {path}: missing X-Request-Id"
)
parsed = None
if response_body:
content_type = response_headers.get_content_type()
if content_type == "application/json":
parsed = json.loads(response_body.decode("utf-8"))
if status >= 400:
codes = {
400: "invalid_request", 401: "unauthorized", 404: "not_found",
409: "conflict", 413: "payload_too_large", 415: "unsupported_media_type",
429: "rate_limited", 500: "internal_error", 503: "unavailable",
}
if not isinstance(parsed, dict) or set(parsed) != {"error"}:
raise AssertionError(f"Invalid error response: {parsed!r}")
error = parsed["error"]
if not isinstance(error, dict) or set(error) != {"code", "message", "request_id"}:
raise AssertionError(f"Invalid error fields: {error!r}")
if error["code"] != codes[status] or error["request_id"] != request_id:
raise AssertionError(f"Wrong error code or request ID: {error!r}")
if not isinstance(error["message"], str) or not error["message"].strip():
raise AssertionError("Error message must be a nonempty string")
return {
"status": status,
"headers": response_headers,
"json": parsed,
"bytes": response_body,
}
def unique_username(prefix):
# username должен соответствовать [a-z0-9_]{3,32}
suffix = uuid.uuid4().hex[:12]
return f"{prefix}_{suffix}"
def basic(username, password=PASSWORD):
raw = f"{username}:{password}".encode("utf-8")
return "Basic " + base64.b64encode(raw).decode()
def create_user(prefix="usr", login=True):
user = request("POST", "/api/v1/users", 201, body={
"username": unique_username(prefix),
"display_name": f"Test {prefix}",
"password": PASSWORD,
})["json"]
if login:
session = request("POST", "/api/v1/auth/sessions", 201,
headers={"Authorization": basic(user["username"])})["json"]
user["token"] = session["token"]
return user
def auth(user):
return {"Authorization": "Bearer " + user["token"]}
def create_chat(owner, members, title="Student test chat"):
result = request(
"POST",
"/api/v1/chats",
201,
headers=auth(owner),
body={
"title": title,
"member_ids": [u["id"] for u in members],
},
)
return result["json"]
def create_message(user, chat, text):
return request(
"POST",
f"/api/v1/chats/{chat['id']}/messages",
201,
headers=auth(user),
body={"text": text},
)["json"]
def test_malformed_json():
for body in (b'{"username": "broken",', b"\xff"):
request("POST", "/api/v1/users", 400, raw_body=body)
print("PASS malformed JSON and invalid UTF-8 -> 400 with error envelope")
def test_duplicate_username():
username = unique_username("dup")
body = {
"username": username,
"display_name": "First",
"password": PASSWORD,
}
request(
"POST",
"/api/v1/users",
201,
body=body,
)
request(
"POST",
"/api/v1/users",
409,
body={
"username": username,
"display_name": "Second",
"password": PASSWORD,
},
)
print("PASS duplicate username -> 409")
def test_outsider_chat():
alice = create_user("alice")
bob = create_user("bob")
eve = create_user("eve")
chat = create_chat(
alice,
[alice, bob],
"Private chat",
)
request(
"GET",
f"/api/v1/chats/{chat['id']}",
404,
headers=auth(eve),
)
request(
"GET",
f"/api/v1/chats/{chat['id']}/messages",
404,
headers=auth(eve),
)
request(
"POST",
f"/api/v1/chats/{chat['id']}/messages",
404,
headers=auth(eve),
body={"text": "Not allowed"},
)
page = request("GET", "/api/v1/chats", 200, headers=auth(eve))["json"]
if page != {"items": [], "next_cursor": None}:
raise AssertionError("Outsider can see a private chat in the list")
print("PASS outsider cannot access chat -> 404")
def test_empty_and_whitespace_message():
alice = create_user("emptya")
bob = create_user("emptyb")
chat = create_chat(
alice,
[alice, bob],
"Empty message test",
)
request(
"POST",
f"/api/v1/chats/{chat['id']}/messages",
400,
headers=auth(alice),
body={"text": ""},
)
request(
"POST",
f"/api/v1/chats/{chat['id']}/messages",
400,
headers=auth(alice),
body={"text": " \t\n"},
)
print("PASS empty/whitespace message -> 400")
def test_extra_json_field():
request(
"POST",
"/api/v1/users",
400,
body={
"username": unique_username("extra"),
"display_name": "Extra",
"password": PASSWORD,
"admin": True,
},
)
print("PASS extra JSON field -> 400")
def test_multi_page_history():
alice = create_user("pagea")
bob = create_user("pageb")
chat = create_chat(
alice,
[alice, bob],
"Pagination test",
)
expected = []
for i in range(7):
message = create_message(
alice,
chat,
f"pagination-message-{i}",
)
expected.append(message)
expected.sort(
key=lambda message: (
datetime.fromisoformat(
message["created_at"].replace("Z", "+00:00")
),
uuid.UUID(message["id"]).int,
)
)
expected_ids = [
message["id"]
for message in expected
]
collected = []
cursor = None
for _ in range(len(expected) + 1):
path = (
f"/api/v1/chats/{chat['id']}/messages"
f"?limit=2"
)
if cursor is not None:
path += "&cursor=" + urllib.parse.quote(
cursor,
safe="",
)
page = request(
"GET",
path,
200,
headers=auth(bob),
)["json"]
ids = [item["id"] for item in page["items"]]
collected.extend(ids)
cursor = page["next_cursor"]
if cursor is None:
break
else:
raise AssertionError("Pagination did not reach the last page")
if collected != expected_ids:
raise AssertionError(
"Pagination lost, duplicated or reordered messages.\n"
f"Expected: {expected_ids}\n"
f"Received: {collected}"
)
if len(collected) != len(set(collected)):
raise AssertionError(
"Pagination returned duplicate messages"
)
print(
f"PASS pagination: {len(collected)} messages "
"read through multiple pages"
)
def test_concurrent_100_messages():
users = [
create_user(f"c{i}")
for i in range(10)
]
chat = create_chat(
users[0],
users,
"Concurrency test",
)
def worker(client_index):
sent = []
for message_index in range(10):
text = (
f"client-{client_index}-"
f"message-{message_index}"
)
result = create_message(
users[client_index],
chat,
text,
)
sent.append(result)
return sent
all_sent = []
with concurrent.futures.ThreadPoolExecutor(
max_workers=10
) as executor:
futures = [
executor.submit(worker, i)
for i in range(10)
]
for future in concurrent.futures.as_completed(
futures
):
all_sent.extend(future.result())
if len(all_sent) != 100:
raise AssertionError(
f"Expected 100 successful POSTs, got {len(all_sent)}"
)
page = request(
"GET",
f"/api/v1/chats/{chat['id']}/messages?limit=100",
200,
headers=auth(users[0]),
)["json"]
messages = page["items"]
if len(messages) != 100:
raise AssertionError(
f"Expected exactly 100 stored messages, "
f"got {len(messages)}"
)
ids = [message["id"] for message in messages]
if len(set(ids)) != 100:
raise AssertionError(
"Expected 100 unique message IDs"
)
if set(ids) != {message["id"] for message in all_sent}:
raise AssertionError("Stored messages differ from successful POST responses")
texts = [message["text"] for message in messages]
if len(set(texts)) != 100:
raise AssertionError(
"Expected 100 unique message texts"
)
if page["next_cursor"] is not None:
raise AssertionError(
"100 messages with limit=100 should fit in one page"
)
print(
"PASS concurrency: 10 clients sent 100 messages; "
"exactly 100 unique messages stored"
)
def test_invalid_chat_ids():
user = create_user("uuid")
for chat_id, expected in (("not-a-uuid", 400), (str(uuid.uuid4()), 404)):
path = f"/api/v1/chats/{chat_id}"
request("GET", path, expected, headers=auth(user))
request("GET", path + "/messages", expected, headers=auth(user))
request("POST", path + "/messages", expected,
headers=auth(user), body={"text": "Test"})
print("PASS invalid chat UUID -> 400; unknown valid UUID -> 404")
def test_chat_member_limits():
users = [create_user("member", login=(i == 0)) for i in range(101)]
owner = users[0]
for members in ([], users, [owner, owner], users[1:3]):
request("POST", "/api/v1/chats", 400, headers=auth(owner), body={
"title": "Invalid members", "member_ids": [user["id"] for user in members],
})
request("POST", "/api/v1/chats", 400, headers=auth(owner), body={
"title": "Unknown member", "member_ids": [owner["id"], str(uuid.uuid4())],
})
chat = create_chat(owner, users[:100])
if len(chat["member_ids"]) != 100:
raise AssertionError("Chat must accept exactly 100 members")
print("PASS chat members: 1..100, unique, existing, caller included")
def test_invalid_pagination():
user = create_user("cursor")
chat = create_chat(user, [user])
other_chat = create_chat(user, [user])
other_message = create_message(user, other_chat, "Other history")
paths = ("/api/v1/chats", f"/api/v1/chats/{chat['id']}/messages")
for path in paths:
for query in ("cursor=", "cursor=unknown", "limit=0", "limit=101", "limit=abc", "limit=1.5"):
request("GET", path + "?" + query, 400, headers=auth(user))
request("GET", paths[1] + "?cursor=" + other_message["id"], 400, headers=auth(user))
print("PASS invalid cursor/limit and cursor from another chat -> 400")
def test_multi_page_chats():
user = create_user("chatpage")
chats = [create_chat(user, [user], f"Chat {i}") for i in range(7)]
chats.sort(key=lambda chat: (chat["created_at"], uuid.UUID(chat["id"]).int))
collected = []
cursor = None
for page_number in range(len(chats) + 1):
query = {"limit": 2 if page_number == 0 else 3}
if cursor is not None:
query["cursor"] = cursor
page = request("GET", "/api/v1/chats?" + urllib.parse.urlencode(query),
200, headers=auth(user))["json"]
collected.extend(chat["id"] for chat in page["items"])
cursor = page["next_cursor"]
if cursor is None:
break
else:
raise AssertionError("Chat pagination did not reach the last page")
if collected != [chat["id"] for chat in chats]:
raise AssertionError("Chat pagination lost, duplicated or reordered chats")
print("PASS pagination: 7 chats, changing limit between pages")
def test_validation_and_unicode():
for display_name in ("", " \t", "x" * 101, 123, None):
request("POST", "/api/v1/users", 400, body={
"username": unique_username("invalid"), "display_name": display_name, "password": PASSWORD,
})
request("POST", "/api/v1/users", 400,
body={"username": True, "display_name": "Test", "password": PASSWORD})
for password in ("short", "x" * 129, 12345678901234, None):
request("POST", "/api/v1/users", 400, body={
"username": unique_username("badpw"), "display_name": "Test", "password": password,
})
request("POST", "/api/v1/users", 400,
body={"username": unique_username("nopw"), "display_name": "Test"})
unicode_password = "\U00020000" * 12 # пароль не из ASCII проходит через Basic/UTF-8 и KDF
user = request("POST", "/api/v1/users", 201, body={
"username": unique_username("unicode"), "display_name": "\U00020000" * 100,
"password": unicode_password,
})["json"]
session = request("POST", "/api/v1/auth/sessions", 201,
headers={"Authorization": basic(user["username"], unicode_password)})["json"]
user["token"] = session["token"]
chat = create_chat(user, [user])
path = f"/api/v1/chats/{chat['id']}/messages"
text = "\U00020000" * 4000
message = create_message(user, chat, text)
if message["text"] != text:
raise AssertionError("Unicode message was altered")
for invalid in (text + "x", 123, None):
request("POST", path, 400, headers=auth(user), body={"text": invalid})
request("GET", "/api/v1/users/me", 401)
request("GET", "/api/v1/users/me", 401, headers={"X-User-Id": user["id"]})
request("GET", "/api/v1/users/me", 401,
headers={"Authorization": basic(user["username"], unicode_password)})
print("PASS strict types, Unicode, password rules, Bearer-only identification")
def main():
tests = [
test_malformed_json,
test_duplicate_username,
test_outsider_chat,
test_empty_and_whitespace_message,
test_extra_json_field,
test_multi_page_history,
test_concurrent_100_messages,
test_invalid_chat_ids,
test_chat_member_limits,
test_invalid_pagination,
test_multi_page_chats,
test_validation_and_unicode,
]
print(f"Testing {BASE_URL}")
for test in tests:
test()
print()
print(
f"PASS: {len(tests)} student test scenarios completed"
)
if __name__ == "__main__":
try:
main()
except Exception as exc:
print(
f"FAIL: {type(exc).__name__}: {exc}",
file=sys.stderr,
)
sys.exit(1)
+68
View File
@@ -0,0 +1,68 @@
#!/usr/bin/env python3
"""Жизненный цикл ЛР 2: данные и сессии переживают перезапуск всего стенда (API, PostgreSQL, Valkey).
Нужен запущенный compose-стенд; перезапуск делает scripts/restart.sh (volumes сохраняются)."""
import os
import sys
import uuid
import test_lab2 as api
from test_sigterm import ROOT, run, wait_ready
def compose_logs():
return run("docker", "compose", "--project-directory", str(ROOT), "--env-file", str(ROOT / ".env"),
"logs", "api", "--no-color").stdout
def main():
api.BASE_URL = os.environ.get("BASE_URL", "http://localhost:8080").rstrip("/")
wait_ready(api.BASE_URL)
alice = api.create_user("lifealice")
bob = api.create_user("lifebob")
outsider = api.create_user("lifeoutsider")
chat = api.create_chat(alice, [alice, bob], "Lifecycle test")
texts = ["private-lifecycle-" + uuid.uuid4().hex for _ in range(2)]
sent = [api.create_message(user, chat, text) for user, text in zip((alice, bob), texts)]
path = f"/api/v1/chats/{chat['id']}/messages"
page = api.request("GET", path, 200, headers=api.auth(bob))["json"]
if [m["id"] for m in page["items"]] != [m["id"] for m in sent]:
raise AssertionError("Created messages are missing or reordered")
print("PASS two users created and read two messages: HTTP 201, then HTTP 200")
api.request("GET", path, 404, headers=api.auth(outsider))
api.request("POST", path, 404, headers=api.auth(outsider), body={"text": "Forbidden"})
print("PASS third user cannot read or write the chat: HTTP 404")
logs = compose_logs()
if any(text in logs for text in texts):
raise AssertionError("Message contents leaked into container logs")
if "req_id=" not in logs or "with status 201" not in logs:
raise AssertionError("Request logs are missing")
print("PASS request logs include IDs and statuses, without message contents")
print("Command: scripts/restart.sh (API + PostgreSQL + Valkey, volumes kept)", flush=True)
run(str(ROOT / "scripts" / "restart.sh"), timeout=120)
wait_ready(api.BASE_URL, timeout=90)
# те же токены, те же данные
api.request("GET", "/api/v1/users/me", 200, headers=api.auth(alice))
after = api.request("GET", path, 200, headers=api.auth(bob))["json"]
if after["items"] != page["items"]:
raise AssertionError("History changed after restart")
api.request("GET", path, 404, headers=api.auth(outsider))
print("PASS after restart: same tokens read the same history")
api.request("DELETE", "/api/v1/auth/sessions/current", 204, headers=api.auth(alice))
api.request("GET", path, 401, headers=api.auth(alice))
api.request("GET", path, 200, headers=api.auth(bob))
print("PASS logout revokes only alice's session; bob's session still works")
print("PASS persistent lifecycle: data and sessions survive full restart")
if __name__ == "__main__":
try:
main()
except Exception as exc:
print(f"FAIL: {type(exc).__name__}: {exc}", file=sys.stderr)
sys.exit(1)
+129
View File
@@ -0,0 +1,129 @@
#!/usr/bin/env python3
import concurrent.futures
import os
import socket
import subprocess
import sys
import time
import urllib.error
import urllib.request
import uuid
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
IMAGE = os.environ.get("IMAGE", "mtusi-messenger:lab2")
NETWORK = os.environ.get("COMPOSE_NETWORK", "mtusi-lab2_default")
PORT = int(os.environ.get("SIGTERM_PORT", "18080"))
def run(*args, check=True, timeout=60):
return subprocess.run(
args, check=check, text=True, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, timeout=timeout,
)
def docker_run_args(name, port):
# Отдельный контейнер API в сети compose: те же PostgreSQL и Valkey, секрет только для чтения
return ["docker", "run", "-d", "--name", name, "--network", NETWORK,
"-p", f"127.0.0.1:{port}:8080",
"-v", f"{ROOT}/secrets/database_url:/run/secrets/database_url:ro",
"-e", "DATABASE_URL_FILE=/run/secrets/database_url",
"-e", "CACHE_URL=redis://valkey:6379/0",
IMAGE]
def wait_ready(base_url, timeout=60):
deadline = time.monotonic() + timeout
while time.monotonic() < deadline:
try:
with urllib.request.urlopen(base_url + "/health/ready", timeout=1) as response:
if response.status == 200:
return
except (urllib.error.URLError, ConnectionError, TimeoutError):
pass
time.sleep(0.2)
raise RuntimeError("Test container did not become ready")
def connection_rejected(base_url):
try:
with urllib.request.urlopen(base_url + "/health/ready", timeout=0.5):
return False
except urllib.error.HTTPError:
return False
except (urllib.error.URLError, ConnectionError, TimeoutError):
return True
def test_shutdown(incomplete_request):
name = "mtusi-messenger-sigterm-" + uuid.uuid4().hex[:8]
base_url = f"http://127.0.0.1:{PORT}"
scenario = "incomplete POST" if incomplete_request else "idle"
pending = None
try:
run(*docker_run_args(name, PORT))
wait_ready(base_url)
if incomplete_request:
pending = socket.create_connection(("127.0.0.1", PORT), timeout=3)
pending.sendall(
b"POST /api/v1/users HTTP/1.1\r\n"
b"Host: localhost\r\nContent-Type: application/json\r\n"
b"Content-Length: 100\r\nExpect: 100-continue\r\n\r\n"
)
response = b""
while b"\r\n\r\n" not in response:
part = pending.recv(1024)
if not part:
raise AssertionError("Connection closed before 100 Continue")
response += part
if b"100 Continue" not in response:
raise AssertionError(f"Expected 100 Continue, received {response!r}")
pending.sendall(b"{")
print(f"Scenario: {scenario}; docker stop --timeout 10 {name}", flush=True)
start = time.monotonic()
with concurrent.futures.ThreadPoolExecutor(max_workers=1) as executor:
stopped = executor.submit(run, "docker", "stop", "--timeout", "10", name)
deadline = start + 2
while not connection_rejected(base_url):
if time.monotonic() >= deadline:
raise AssertionError("New connections are still accepted during shutdown")
time.sleep(0.05)
rejected_after = time.monotonic() - start
stopped.result(timeout=12)
elapsed = time.monotonic() - start
exit_code = int(run("docker", "inspect", name, "--format", "{{.State.ExitCode}}").stdout)
if exit_code != 0:
raise AssertionError(f"Container exited with code {exit_code}, expected 0")
if elapsed > 10:
raise AssertionError(f"Shutdown exceeded 10 seconds: {elapsed:.3f}s")
if not connection_rejected(base_url):
raise AssertionError("API still accepts connections after shutdown")
logs = run("docker", "logs", name).stdout
if "Application shutdown complete" not in logs:
raise AssertionError("Application did not complete its shutdown handler")
print(f"PASS {scenario}: exit code 0; shutdown {elapsed:.3f}s; "
f"new connections rejected after {rejected_after:.3f}s")
finally:
if pending is not None:
pending.close()
run("docker", "rm", "-f", name, check=False)
def main():
print(f"Image: {IMAGE}")
test_shutdown(False)
test_shutdown(True)
print("PASS SIGTERM: both scenarios completed within 10 seconds")
if __name__ == "__main__":
try:
main()
except Exception as exc:
print(f"FAIL: {type(exc).__name__}: {exc}", file=sys.stderr)
sys.exit(1)