перенос 2 лабы

This commit is contained in:
Ванек
2026-10-29 13:00:25 +03:00
parent 0ab3a341aa
commit 74aa224251
47 changed files with 5783 additions and 22 deletions
+201
View File
@@ -0,0 +1,201 @@
"""Полный прогон lab2 в собственном Compose project; основные данные не затрагивает."""
from datetime import datetime, timezone
import json
import os
import platform
import socket
import subprocess
import sys
import time
from urllib.error import URLError
from urllib.request import urlopen
from uuid import uuid4
from helpers import Client, ROOT
def docker(*args):
return subprocess.run(["docker", *args], check=True, capture_output=True, text=True, timeout=120).stdout.strip()
def inspect(container):
return json.loads(docker("inspect", container))[0]
def check_shutdown(container, base_url, port):
with socket.create_connection(("127.0.0.1", port), timeout=2) as pending:
pending.sendall(
b"POST /api/v1/users HTTP/1.1\r\nHost: localhost\r\n"
b"Content-Type: application/json\r\nContent-Length: 1000\r\n"
b"Expect: 100-continue\r\nConnection: close\r\n\r\n"
)
assert b"100 Continue" in pending.recv(1024), "Сервер не начал читать тело запроса"
pending.sendall(b'{"username":')
started = time.monotonic()
docker("kill", "--signal=TERM", container)
first_refusal = None
while inspect(container)["State"]["Running"]:
assert time.monotonic() - started < 10, "SIGTERM: превышены 10 секунд"
try:
with urlopen(base_url + "/health/live", timeout=0.3):
pass
except (URLError, OSError):
if first_refusal is None:
first_refusal = time.monotonic() - started
time.sleep(0.05)
elapsed = time.monotonic() - started
assert elapsed < 10, "SIGTERM: превышены 10 секунд"
assert first_refusal is not None and first_refusal < elapsed, "Не показан отказ новых запросов при остановке"
state = inspect(container)["State"]
assert not state["OOMKilled"] and state["ExitCode"] in (0, 143), state
result = subprocess.run(["docker", "logs", container], capture_output=True, text=True, check=True, timeout=10)
logs = result.stdout + result.stderr
assert "Application shutdown complete" in logs, "Нет подтверждения корректного завершения"
try:
with urlopen(base_url + "/health/live", timeout=0.5):
raise AssertionError("Остановленный сервер отвечает")
except (URLError, OSError):
pass
print(f"PASS: SIGTERM with unfinished request: {elapsed:.2f}s; new requests refused after {first_refusal:.2f}s; exit={state['ExitCode']}; OOM=false")
def main():
with socket.socket() as sock:
sock.bind(("127.0.0.1", 0))
port = sock.getsockname()[1]
project = "lab2-check-" + uuid4().hex[:10]
env = dict(os.environ, COMPOSE_PROJECT_NAME=project, HOST_PORT=str(port), SESSION_TTL_SECONDS="3600")
base_url = f"http://127.0.0.1:{port}"
def compose(*args, capture=True, stdin=None):
return subprocess.run(["docker", "compose", *args], cwd=ROOT, env=env, input=stdin,
text=True, capture_output=capture, check=True, timeout=180)
def ready():
subprocess.run([str(ROOT / "scripts/start.sh")], cwd=ROOT, env=env,
check=True, capture_output=True, text=True, timeout=180)
print("PASS: start.sh built and started the stack, readiness reached")
def states():
return {service: inspect(compose("ps", "-q", service).stdout.strip()) for service in ("api", "postgres", "valkey")}
def scalar(query, params=()):
code = """import json,sys
from src import db
data=json.load(sys.stdin)
db.pool.open(wait=True,timeout=15)
try:
with db.connection() as conn:
row=conn.execute(data['query'], data['params']).fetchone()
print(json.dumps(next(iter(row.values()))))
finally:
db.pool.close()
"""
return json.loads(compose("exec", "-T", "api", "python", "-c", code,
stdin=json.dumps({"query": query, "params": params})).stdout)
print("UTC:", datetime.now(timezone.utc).isoformat())
print("Host:", platform.system(), platform.machine(), "Python", platform.python_version())
print("Docker:", docker("info", "--format", "{{.ServerVersion}} {{.Architecture}} CPUs={{.NCPU}} RAM={{.MemTotal}}"))
print("Commit:", subprocess.run(["git", "rev-parse", "--short", "HEAD"], cwd=ROOT, capture_output=True, text=True).stdout.strip(), "+ working tree")
print("Command: ./scripts/check-docker.sh; project:", project)
try:
ready()
before = states()
for service, info in before.items():
print(service, "image=" + info["Image"], "started=" + info["State"]["StartedAt"],
"memory=" + str(info["HostConfig"]["Memory"]), "nano_cpus=" + str(info["HostConfig"]["NanoCpus"]))
api = compose("ps", "-q", "api").stdout.strip()
assert docker("exec", api, "id", "-u") == "10001"
assert before["api"]["Config"]["Cmd"] == ["python", "-m", "src"]
for service in ("postgres", "valkey"):
assert any(mount["Type"] == "volume" for mount in before[service]["Mounts"])
assert all(mount["Destination"].startswith("/run/secrets/") for mount in before["api"]["Mounts"])
bad_health = subprocess.run(["docker", "exec", "-e", "APP_PORT=1", api, "python", "-m", "src.healthcheck"], capture_output=True)
assert bad_health.returncode == 1
print("PASS: non-root UID 10001; Python PID 1; healthcheck; named volumes; no API data volume")
subprocess.run([str(ROOT / "scripts/check.sh")], cwd=ROOT, env=dict(env, BASE_URL=base_url), check=True, timeout=240)
demo = subprocess.run([str(ROOT / "scripts/demo.sh")], cwd=ROOT, env=dict(env, BASE_URL=base_url),
check=True, capture_output=True, text=True, timeout=30)
assert "Привет из lab2!" in demo.stdout
print("PASS: demo.sh registered users, logged in and read two messages")
client = Client(base_url)
alice, bob, eve, chat = client.bootstrap()
marker = "private-" + uuid4().hex
client.message(alice, chat, marker)
logs = docker("logs", api)
assert marker not in logs and alice["token"] not in logs and alice["registration"]["password"] not in logs
logs_result = subprocess.run(["docker", "logs", api], capture_output=True, text=True, check=True)
assert "request_id=" in logs_result.stderr + logs_result.stdout
assert marker not in logs_result.stderr + logs_result.stdout
assert alice["token"] not in logs_result.stderr + logs_result.stdout
assert alice["registration"]["password"] not in logs_result.stderr + logs_result.stdout
assert scalar("SELECT bool_and(password_hash LIKE %s) FROM users", ("$argon2id$%",))
print("PASS: Argon2id in PostgreSQL; logs do not contain message text, passwords or session tokens")
title = "rollback_" + uuid4().hex
client.call("POST", "/api/v1/chats", 400, actor=alice,
body={"title": title, "member_ids": [alice["id"], str(uuid4())]})
assert scalar("SELECT count(*) FROM chats WHERE title=%s", (title,)) == 0
count = scalar("SELECT count(*) FROM messages")
compose("exec", "-T", "api", "python", "-m", "src.migrate")
compose("exec", "-T", "api", "python", "-m", "src.migrate")
assert scalar("SELECT count(*) FROM schema_migrations") == 1
assert scalar("SELECT count(*) FROM messages") == count
print("PASS: failed membership insert rolled back chat; migrations run twice without duplicate data")
before = states()
subprocess.run(["make", "test-persistence", "PYTHON=" + sys.executable,
"ACTION_SCRIPT=scripts/restart.sh", "BASE_URL=" + base_url], cwd=ROOT, env=env, check=True, timeout=240)
after = states()
for service in before:
assert before[service]["State"]["StartedAt"] != after[service]["State"]["StartedAt"]
print("Restart", service, before[service]["State"]["StartedAt"], "->", after[service]["State"]["StartedAt"])
client.call("GET", "/api/v1/users/me", actor=alice, schema="User")
print("PASS: API, PostgreSQL and Valkey really restarted; previous token still works")
for service in ("valkey", "postgres"):
try:
compose("stop", "--timeout", "10", service)
started = time.monotonic()
client.call("GET", "/api/v1/users/me", 503, actor=alice)
elapsed = time.monotonic() - started
assert elapsed < 6, elapsed
client.call("GET", "/api/v1/users/me", 401)
client.call("GET", "/health/ready", 503)
client.call("GET", "/health/live", schema="Health")
print(f"PASS: {service} unavailable -> 503 in {elapsed:.2f}s; anonymous -> 401; readiness 503, liveness 200")
finally:
compose("start", service)
subprocess.run([str(ROOT / "scripts/wait-ready.sh")], cwd=ROOT, env=env, check=True, timeout=90)
client.call("GET", "/api/v1/users/me", actor=alice, schema="User")
print("PASS:", service, "recovered without a new login")
env["SESSION_TTL_SECONDS"] = "2"
compose("up", "-d", "--force-recreate", "api")
subprocess.run([str(ROOT / "scripts/wait-ready.sh")], cwd=ROOT, env=env, check=True, timeout=90)
short = client.user("short_ttl")
client.call("GET", "/api/v1/users/me", actor=short, schema="User")
time.sleep(2.2)
client.call("GET", "/api/v1/users/me", 401, actor=short)
print("PASS: SESSION_TTL_SECONDS=2; valid token expires without logout")
api = compose("ps", "-q", "api").stdout.strip()
check_shutdown(api, base_url, port)
print("PASS: all lab2 Docker checks completed")
finally:
try:
subprocess.run([str(ROOT / "scripts/stop.sh")], cwd=ROOT, env=env,
check=True, capture_output=True, text=True, timeout=90)
volumes = docker("volume", "ls", "--quiet", "--filter", "label=com.docker.compose.project=" + project)
if volumes:
print("PASS: stop.sh removed containers and preserved named volumes")
finally:
compose("down", "--volumes", "--timeout", "10")
print("Removed only temporary Compose project and its test volumes:", project)
if __name__ == "__main__":
main()
+39
View File
@@ -0,0 +1,39 @@
from pathlib import Path
import sys
import unittest
from urllib.parse import urlencode
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "tests"))
from client import Client
class ApiTestCase(unittest.TestCase):
def setUp(self):
self.client = Client()
self.alice, self.bob, self.eve, self.chat = self.client.bootstrap()
self.path = "/api/v1/chats/" + self.chat["id"]
def assert_error(self, method, path, status, code, **kwargs):
result = self.client.call(method, path, status, **kwargs)
self.assertEqual(result["json"]["error"]["code"], code)
return result["json"]["error"]
def read_all(self, path, actor, schema, limit=3):
items = []
cursor = None
seen_cursors = set()
while True:
query = {"limit": limit}
if cursor is not None:
query["cursor"] = cursor
page = self.client.call("GET", path + "?" + urlencode(query), actor=actor, schema=schema)["json"]
self.assertLessEqual(len(page["items"]), limit)
items.extend(page["items"])
cursor = page["next_cursor"]
if cursor is None:
return items
self.assertTrue(page["items"], "Пустая страница не должна вести к следующей")
self.assertNotIn(cursor, seen_cursors, "Зацикливание пагинации")
seen_cursors.add(cursor)
+164
View File
@@ -0,0 +1,164 @@
import base64
import json
from urllib.parse import urlencode
from uuid import uuid4
from helpers import ApiTestCase
class ApiTests(ApiTestCase):
def test_duplicate_username(self):
self.assert_error("POST", "/api/v1/users", 409, "conflict", body=self.alice["registration"])
def test_invalid_user_fields(self):
invalid_bodies = [
{}, [], None,
{"username": "ab", "display_name": "Имя"},
{"username": "UPPER", "display_name": "Имя"},
{"username": "name\n", "display_name": "Имя"},
{"username": "язык", "display_name": "Имя"},
{"username": "a" * 33, "display_name": "Имя"},
{"username": 123, "display_name": "Имя"},
{"username": "valid_name", "display_name": " \t\n"},
{"username": "valid_name", "display_name": "👋" * 101},
{"username": "valid_name", "display_name": False},
{"username": "valid_name", "display_name": "Имя", "extra": 1},
]
for body in invalid_bodies:
if isinstance(body, dict):
body = {**body, "password": "Synthetic-password-123"}
with self.subTest(body=body):
self.assert_error("POST", "/api/v1/users", 400, "invalid_request",
raw=json.dumps(body).encode(), headers={"Content-Type": "application/json"})
def test_malformed_json_and_encoding(self):
invalid_bodies = [
b'{"username":', b'', b'not JSON', b'\xff',
'{"username":"valid_name","display_name":"Имя"}'.encode("utf-16"),
b'{"username":"valid_name","display_name":"\\ud800"}',
]
for body in invalid_bodies:
with self.subTest(body=body):
self.assert_error("POST", "/api/v1/users", 400, "invalid_request",
raw=body, headers={"Content-Type": "application/json"})
def test_wrong_content_type(self):
self.assert_error("POST", "/api/v1/users", 415, "unsupported_media_type",
raw=b'{}', headers={"Content-Type": "text/plain"})
def test_identification_and_authentication_order(self):
self.assert_error("GET", "/api/v1/users/me", 401, "unauthorized")
self.assert_error("GET", "/api/v1/users/me", 401, "unauthorized",
headers={"X-User-Id": self.alice["id"]})
self.assert_error("GET", "/api/v1/users/me", 401, "unauthorized",
headers={"Authorization": "Bearer unknown-session"})
self.assert_error("POST", self.path + "/messages", 401, "unauthorized",
raw=b'{', headers={"Content-Type": "application/json"})
me = self.client.call("GET", "/api/v1/users/me", schema="User", actor=self.alice,
headers={"X-User-Id": self.bob["id"]})["json"]
self.assertEqual(me, self.alice["user"])
def test_invalid_chat_members(self):
invalid_members = [
[], [self.bob["id"]], [self.alice["id"], str(uuid4())],
[self.alice["id"], self.alice["id"]],
[self.alice["id"], self.alice["id"].upper()],
[self.alice["id"], "not-a-uuid"], [True], self.alice["id"],
[self.alice["id"]] + [str(uuid4()) for _ in range(100)],
]
for members in invalid_members:
with self.subTest(members=members):
self.assert_error("POST", "/api/v1/chats", 400, "invalid_request", actor=self.alice,
body={"title": "Чат", "member_ids": members})
chats = self.read_all("/api/v1/chats", self.alice, "ChatPage")
self.assertEqual([chat["id"] for chat in chats], [self.chat["id"]])
def test_invalid_chat_title(self):
for title in ("", " \n", "a" * 101, 123, None):
with self.subTest(title=title):
self.assert_error("POST", "/api/v1/chats", 400, "invalid_request", actor=self.alice,
body={"title": title, "member_ids": [self.alice["id"]]})
def test_chat_access(self):
outsider = self.assert_error("GET", self.path, 404, "not_found", actor=self.eve)
missing = self.assert_error("GET", "/api/v1/chats/" + str(uuid4()), 404, "not_found", actor=self.eve)
self.assertEqual(outsider["message"], missing["message"])
self.assert_error("GET", self.path + "/messages", 404, "not_found", actor=self.eve)
self.assert_error("POST", self.path + "/messages", 404, "not_found", actor=self.eve, body={"text": "Чужое"})
self.assertEqual(self.read_all("/api/v1/chats", self.eve, "ChatPage"), [])
self.assert_error("GET", "/api/v1/chats/not-a-uuid", 400, "invalid_request", actor=self.alice)
def test_invalid_message_fields(self):
for text in ("", " \t\n", "👋" * 4001, 123, True, None, []):
with self.subTest(text_type=type(text).__name__):
self.assert_error("POST", self.path + "/messages", 400, "invalid_request",
actor=self.alice, body={"text": text})
self.assert_error("POST", self.path + "/messages", 400, "invalid_request", actor=self.alice,
body={"text": "Подмена автора", "sender_id": self.bob["id"]})
self.assertEqual(self.read_all(self.path + "/messages", self.alice, "MessagePage"), [])
def test_unicode_and_text_preservation(self):
for text in (" Привет, мир 👋\n", "👋" * 4000):
message = self.client.message(self.bob, self.chat, text)["json"]
self.assertEqual(message["text"], text)
self.assertEqual(message["sender_id"], self.bob["id"])
items = self.read_all(self.path + "/messages", self.alice, "MessagePage")
self.assertEqual([item["text"] for item in items], [" Привет, мир 👋\n", "👋" * 4000])
def test_message_pagination_and_changing_limit(self):
sent = [self.client.message(self.alice, self.chat, f"Сообщение {i}")["json"] for i in range(8)]
items = self.read_all(self.path + "/messages", self.bob, "MessagePage", limit=3)
self.assertEqual(items, sorted(sent, key=lambda item: (item["created_at"], item["id"])))
first = self.client.call("GET", self.path + "/messages?limit=1", actor=self.alice, schema="MessagePage")["json"]
query = urlencode({"limit": 100, "cursor": first["next_cursor"]})
rest = self.client.call("GET", self.path + "/messages?" + query, actor=self.alice, schema="MessagePage")["json"]
self.assertEqual(first["items"] + rest["items"], items)
self.assertIsNone(rest["next_cursor"])
def test_default_page_size(self):
for i in range(51):
self.client.message(self.alice, self.chat, f"Сообщение {i}")
page = self.client.call("GET", self.path + "/messages", actor=self.alice, schema="MessagePage")["json"]
self.assertEqual(len(page["items"]), 50)
self.assertIsNotNone(page["next_cursor"])
def test_chat_pagination_and_cursor_owner(self):
for i in range(4):
self.client.call("POST", "/api/v1/chats", 201, actor=self.alice,
body={"title": f"Чат {i}", "member_ids": [self.alice["id"], self.bob["id"]]})
chats = self.read_all("/api/v1/chats", self.alice, "ChatPage", limit=2)
self.assertEqual(len(chats), 5)
self.assertEqual(len({chat["id"] for chat in chats}), 5)
self.assertEqual(chats, sorted(chats, key=lambda item: (item["created_at"], item["id"])))
first = self.client.call("GET", "/api/v1/chats?limit=1", actor=self.alice, schema="ChatPage")["json"]
query = urlencode({"cursor": first["next_cursor"]})
self.assert_error("GET", "/api/v1/chats?" + query, 400, "invalid_request", actor=self.bob)
def test_invalid_limits_and_cursors(self):
bad_queries = [{"limit": value} for value in ("0", "101", "-1", "1.5", "1.0", "abc", "")]
bad_queries += [{"cursor": value} for value in ("", "not-base64!", "e30=", "W10=")]
for path in ("/api/v1/chats", self.path + "/messages"):
for query in bad_queries:
with self.subTest(path=path, query=query):
self.assert_error("GET", path + "?" + urlencode(query), 400, "invalid_request", actor=self.alice)
def test_cursor_cannot_be_used_for_another_chat_or_changed(self):
for i in range(2):
self.client.message(self.alice, self.chat, f"Сообщение {i}")
first = self.client.call("GET", self.path + "/messages?limit=1", actor=self.alice, schema="MessagePage")["json"]
cursor = first["next_cursor"]
other = self.client.call("POST", "/api/v1/chats", 201, actor=self.alice, schema="Chat",
body={"title": "Другой чат", "member_ids": [self.alice["id"]]})["json"]
path = "/api/v1/chats/" + other["id"] + "/messages?" + urlencode({"cursor": cursor})
self.assert_error("GET", path, 400, "invalid_request", actor=self.alice)
data = json.loads(base64.urlsafe_b64decode(cursor))
data["id"] = str(uuid4())
changed = base64.urlsafe_b64encode(json.dumps(data).encode()).decode()
self.assert_error("GET", self.path + "/messages?" + urlencode({"cursor": changed}),
400, "invalid_request", actor=self.alice)
def test_framework_errors_and_request_ids(self):
first = self.assert_error("GET", "/unknown", 404, "not_found")
second = self.assert_error("GET", "/unknown", 404, "not_found")
self.assertNotEqual(first["request_id"], second["request_id"])
self.assert_error("POST", "/health/live", 405, "invalid_request")
+47
View File
@@ -0,0 +1,47 @@
from concurrent.futures import ThreadPoolExecutor
import threading
from uuid import uuid4
from helpers import ApiTestCase, Client
class ConcurrencyTests(ApiTestCase):
def test_ten_clients_send_one_hundred_messages(self):
barrier = threading.Barrier(10)
def send_messages(worker):
client = Client()
actor = self.alice if worker % 2 == 0 else self.bob
barrier.wait(timeout=10)
sent = []
for index in range(10):
message = client.message(actor, self.chat, f"parallel-{worker}-{index}")["json"]
self.assertEqual(message["sender_id"], actor["id"])
sent.append(message)
return sent
with ThreadPoolExecutor(max_workers=10) as pool:
batches = list(pool.map(send_messages, range(10)))
sent = [message for batch in batches for message in batch]
history = self.read_all(self.path + "/messages", self.alice, "MessagePage", limit=17)
self.assertEqual(len(history), 100)
self.assertEqual(len({message["id"] for message in history}), 100)
self.assertEqual(history, sorted(sent, key=lambda item: (item["created_at"], item["id"])))
self.assertEqual({message["text"] for message in history},
{f"parallel-{worker}-{index}" for worker in range(10) for index in range(10)})
print("PASS: 10 clients sent 100 messages; history contains exactly 100 unique messages.")
def test_simultaneous_registration_of_one_username(self):
username = "race_" + uuid4().hex[:20]
barrier = threading.Barrier(10)
def register(worker):
client = Client()
barrier.wait(timeout=10)
return client.call("POST", "/api/v1/users", [201, 409],
body={"username": username, "display_name": f"Клиент {worker}", "password": "Synthetic-password-123"})["status"]
with ThreadPoolExecutor(max_workers=10) as pool:
statuses = list(pool.map(register, range(10)))
self.assertEqual(statuses.count(201), 1)
self.assertEqual(statuses.count(409), 9)
+51
View File
@@ -0,0 +1,51 @@
import base64
from uuid import uuid4
from helpers import ApiTestCase
class SessionTests(ApiTestCase):
def basic(self, username, password):
value = base64.b64encode((username + ":" + password).encode()).decode()
return {"Authorization": "Basic " + value}
def test_wrong_password_and_basic_only_for_login(self):
registration = self.alice["registration"]
self.assert_error("POST", "/api/v1/auth/sessions", 401, "unauthorized",
headers=self.basic(registration["username"], "wrong-password"))
self.assert_error("POST", "/api/v1/auth/sessions", 401, "unauthorized")
self.assert_error("POST", "/api/v1/auth/sessions", 401, "unauthorized",
headers={"Authorization": "Basic not-base64!"})
self.assert_error("GET", "/api/v1/users/me", 401, "unauthorized",
headers=self.basic(registration["username"], registration["password"]))
def test_logout_revokes_only_one_session(self):
registration = self.alice["registration"]
second = self.client.call("POST", "/api/v1/auth/sessions", 201, schema="Session",
headers=self.basic(registration["username"], registration["password"]))["json"]
self.client.call("DELETE", "/api/v1/auth/sessions/current", 204, actor=self.alice)
self.assert_error("GET", "/api/v1/users/me", 401, "unauthorized", actor=self.alice)
self.assert_error("DELETE", "/api/v1/auth/sessions/current", 401, "unauthorized", actor=self.alice)
self.client.call("GET", "/api/v1/users/me", schema="User", headers={"Authorization": "Bearer " + second["token"]})
def test_password_validation(self):
for password in ("short", "x" * 129, 123, None, " " * 12):
body = {"username": "pw_" + uuid4().hex[:20], "display_name": "Пароль", "password": password}
self.assert_error("POST", "/api/v1/users", 400, "invalid_request", body=body)
def test_unicode_password_with_colon_and_private_response(self):
username = "pw_" + uuid4().hex[:20]
password = "Пароль:с:двоеточием-123"
user = self.client.call("POST", "/api/v1/users", 201, schema="User",
body={"username": username, "display_name": "Пароль", "password": password})["json"]
session = self.client.call("POST", "/api/v1/auth/sessions", 201, schema="Session",
headers=self.basic(username, password))["json"]
self.assertEqual(session["user"], user)
self.assertEqual(set(user), {"id", "username", "display_name", "created_at"})
self.assertGreaterEqual(len(session["token"]), 32)
def test_sql_like_message_is_plain_text(self):
text = "'; DROP TABLE messages; -- Привет"
message = self.client.message(self.alice, self.chat, text)["json"]
self.assertEqual(message["text"], text)
self.assertEqual(self.read_all(self.path + "/messages", self.bob, "MessagePage"), [message])