реализация второй лабы

This commit is contained in:
Ванек
2026-10-22 17:16:01 +03:00
parent 9961362555
commit 0dfbfe35f8
43 changed files with 5114 additions and 337 deletions
+125 -77
View File
@@ -1,12 +1,11 @@
"""Проверки докера во временном контейнере"""
import argparse
"""Полный прогон 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
@@ -16,18 +15,13 @@ from helpers import Client, ROOT
def docker(*args):
result = subprocess.run(["docker", *args], check=True, capture_output=True, text=True, timeout=60)
return result.stdout.strip()
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 wait_ready(container):
subprocess.run([str(ROOT / "scripts/wait-ready.sh"), container], check=True, timeout=70)
def check_shutdown(container, base_url, port):
with socket.create_connection(("127.0.0.1", port), timeout=2) as pending:
pending.sendall(
@@ -68,85 +62,139 @@ def check_shutdown(container, base_url, port):
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--image", default="mtusi-messenger:lab2")
args = parser.parse_args()
container = "mtusi-messenger-lab2-check-" + uuid4().hex[:12]
created = False
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}}"))
revision = subprocess.run(["git", "rev-parse", "--short", "HEAD"], cwd=ROOT, capture_output=True, text=True)
print("Base commit:", revision.stdout.strip() or "source snapshot", "(working tree may include final uncommitted changes)")
print("Command: ./scripts/check-docker.sh")
print("Container limits: 1 CPU, 256 MiB; no volumes; one API process")
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:
docker("run", "--detach", "--name", container, "--label", "mtusi.lab=2",
"--cpus", "1", "--memory", "256m", "--publish", "127.0.0.1::8080", args.image)
created = True
wait_ready(container)
before = inspect(container)
port = int(before["NetworkSettings"]["Ports"]["8080/tcp"][0]["HostPort"])
base_url = f"http://127.0.0.1:{port}"
print("Image:", before["Image"])
print("Python in image:", docker("exec", container, "python", "--version"))
assert before["Mounts"] == [], "Обнаружены подключённые хранилища"
assert before["HostConfig"]["Memory"] == 256 * 1024 * 1024
assert before["HostConfig"]["NanoCpus"] == 1_000_000_000
uid = docker("exec", container, "id", "-u")
assert uid == "10001", "Сервер должен работать от непривилегированного пользователя"
pid1 = docker("exec", container, "python", "-c",
"from pathlib import Path; print(Path('/proc/1/cmdline').read_bytes().replace(b'\\0', b' ').decode().strip())")
assert pid1 == "python -m src", pid1
print("PASS: UID=10001; PID 1=python -m src; Docker health=healthy")
files = json.loads(docker("exec", container, "python", "-c",
"import json; from pathlib import Path; print(json.dumps(sorted(p.name for p in Path('/app').iterdir())))"))
assert files == ["requirements.txt", "src"], files
failed_health = subprocess.run(["docker", "exec", "-e", "APP_PORT=1", container,
"python", "-m", "src.healthcheck"], capture_output=True, timeout=10)
assert failed_health.returncode == 1
print("PASS: image contains only sources and requirements; failed healthcheck returns 1")
env = dict(os.environ, BASE_URL=base_url)
subprocess.run([str(ROOT / "scripts/check.sh")], cwd=ROOT, env=env, check=True, timeout=120)
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-message-" + uuid4().hex
first = client.message(alice, chat, marker)["json"]
second = client.message(bob, chat, "Ответ второго участника")["json"]
path = "/api/v1/chats/" + chat["id"]
history = client.call("GET", path + "/messages", actor=alice, schema="MessagePage")["json"]
assert {item["id"] for item in history["items"]} == {first["id"], second["id"]}
client.call("GET", path, 404, actor=eve)
result = subprocess.run(["docker", "logs", container], capture_output=True, text=True, check=True, timeout=10)
logs = result.stdout + result.stderr
assert marker not in logs and second["text"] not in logs
assert "request_id=" in logs and "duration_ms=" in logs
print("PASS: two members exchanged messages; outsider gets 404; logs contain no message text")
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")
docker("restart", "--time", "10", container)
wait_ready(container)
after = inspect(container)
assert after["Image"] == before["Image"]
assert after["State"]["StartedAt"] != before["State"]["StartedAt"]
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")
port = int(after["NetworkSettings"]["Ports"]["8080/tcp"][0]["HostPort"])
base_url = f"http://127.0.0.1:{port}"
client = Client(base_url)
client.call("GET", "/api/v1/users/me", 401, actor=alice)
fresh = client.user("after_restart")
client.call("GET", path, 404, actor=fresh)
print("PASS: real restart with same image; old user gets 401; old chat gets 404")
print("StartedAt before:", before["State"]["StartedAt"])
print("StartedAt after: ", after["State"]["StartedAt"])
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")
check_shutdown(container, base_url, port)
print("PASS: all Docker and API checks completed")
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:
if created:
docker("rm", "--force", container)
print("Temporary test container removed")
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__":
+7 -5
View File
@@ -25,6 +25,8 @@ class ApiTests(ApiTestCase):
{"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"})
@@ -47,13 +49,13 @@ class ApiTests(ApiTestCase):
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": str(uuid4())})
self.assert_error("GET", "/api/v1/users/me", 400, "invalid_request",
headers={"X-User-Id": "not-a-uuid"})
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",
headers={"X-User-Id": self.alice["id"].upper()})["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):
+1 -1
View File
@@ -39,7 +39,7 @@ class ConcurrencyTests(ApiTestCase):
client = Client()
barrier.wait(timeout=10)
return client.call("POST", "/api/v1/users", [201, 409],
body={"username": username, "display_name": f"Клиент {worker}"})["status"]
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)))
+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])