202 lines
10 KiB
Python
202 lines
10 KiB
Python
"""Полный прогон 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()
|