diff --git a/.dockerignore b/.dockerignore index bc16e55..9b4ba69 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,20 +1,7 @@ -.git -.github -.env -.env.* -!.env.example -secrets -certs -volumes -data -backups -*.key -*.pem -*.p12 -*.pfx -*.jks -.venv -__pycache__ -node_modules -evidence -.DS_Store +* +!Dockerfile +!requirements.txt +!src/ +!src/** +**/__pycache__/ +**/*.py[cod] diff --git a/.github/workflows/application.yml b/.github/workflows/application.yml new file mode 100644 index 0000000..9424371 --- /dev/null +++ b/.github/workflows/application.yml @@ -0,0 +1,22 @@ +name: Application +on: [push, pull_request] +permissions: + contents: read + +jobs: + docker: + runs-on: ubuntu-latest + timeout-minutes: 15 + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-python@v5 + with: + python-version: '3.11' + - run: python -m venv .venv + - run: .venv/bin/python -m pip install -r requirements.txt + - run: ./scripts/check-docker.sh + - uses: actions/upload-artifact@v4 + if: always() + with: + name: docker-checks + path: evidence/docker-checks.txt diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..79ee02d --- /dev/null +++ b/Dockerfile @@ -0,0 +1,26 @@ +FROM python:3.11.15-slim-bookworm@sha256:d29f48a31a8b408ed19272ca1e7b10ebae13b240a27e862d3d4217c528e2e0c3 + +ENV PYTHONDONTWRITEBYTECODE=1 \ + PYTHONUNBUFFERED=1 \ + APP_HOST=0.0.0.0 \ + APP_PORT=8080 + +WORKDIR /app + +COPY requirements.txt . +RUN python -m pip install --no-cache-dir -r requirements.txt \ + && python -m pip check + +RUN groupadd --gid 10001 app \ + && useradd --uid 10001 --gid app --no-create-home app + +COPY src/ ./src/ + +USER 10001:10001 +EXPOSE 8080 +STOPSIGNAL SIGTERM + +HEALTHCHECK --interval=2s --timeout=3s --start-period=5s --retries=3 \ + CMD ["python", "-m", "src.healthcheck"] + +CMD ["python", "-m", "src"] diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..580b905 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,17 @@ +# Основные библиотеки +fastapi==0.142.4 +uvicorn==0.54.0 +pydantic==2.13.5 + +# Их зависимости +annotated-doc==0.0.5 +annotated-types==0.8.0 +anyio==4.15.1 +click==8.5.0 +h11==0.16.0 +idna==3.20 +opentelemetry-api==1.45.1 +pydantic-core==2.46.5 +starlette==1.7.0 +typing-extensions==4.16.0 +typing-inspection==0.4.4 diff --git a/scripts/build.sh b/scripts/build.sh new file mode 100755 index 0000000..c21b741 --- /dev/null +++ b/scripts/build.sh @@ -0,0 +1,5 @@ +#!/usr/bin/env bash +set -euo pipefail + +project_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)" +docker build --tag "${IMAGE_NAME:-mtusi-messenger:lab2}" "$@" "$project_dir" diff --git a/scripts/check-docker.sh b/scripts/check-docker.sh new file mode 100755 index 0000000..cda798b --- /dev/null +++ b/scripts/check-docker.sh @@ -0,0 +1,16 @@ +#!/usr/bin/env bash +set -euo pipefail + +project_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)" +cd "$project_dir" +if [[ ! -x .venv/bin/python ]]; then + echo "Не хватает .venv" >&2 + echo "python3 -m venv .venv" >&2 + echo ".venv/bin/python -m pip install -r requirements.txt" >&2 + exit 1 +fi + +./scripts/build.sh +mkdir -p evidence +.venv/bin/python -u student-tests/docker_lifecycle.py \ + --image "${IMAGE_NAME:-mtusi-messenger:lab2}" 2>&1 | tee evidence/docker-checks.txt diff --git a/scripts/check.sh b/scripts/check.sh new file mode 100755 index 0000000..90a608d --- /dev/null +++ b/scripts/check.sh @@ -0,0 +1,17 @@ +#!/usr/bin/env bash +set -euo pipefail + +project_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)" +cd "$project_dir" + +if [[ ! -x .venv/bin/python ]]; then + echo "Не хватает .venv" >&2 + echo "python3 -m venv .venv" >&2 + echo ".venv/bin/python -m pip install -r requirements.txt" >&2 + exit 1 +fi + +export BASE_URL="${BASE_URL:-http://localhost:8080}" +echo "Проверяем сервер: $BASE_URL" +make check test PYTHON="$project_dir/.venv/bin/python" BASE_URL="$BASE_URL" +.venv/bin/python -m unittest discover -s student-tests -v diff --git a/scripts/demo.sh b/scripts/demo.sh new file mode 100755 index 0000000..47be62a --- /dev/null +++ b/scripts/demo.sh @@ -0,0 +1,42 @@ +#!/usr/bin/env bash +set -euo pipefail + +if ! command -v jq >/dev/null; then + echo "Для demo.sh нужен jq" >&2 + exit 1 +fi + +base_url="${BASE_URL:-http://localhost:8080}" +base_url="${base_url%/}" + +suffix="$(date +%s)_$RANDOM" + +post_json() { + local path="$1" + local body="$2" + shift 2 + curl --fail --silent --show-error --connect-timeout 5 --max-time 10 \ + -H "Content-Type: application/json" --data "$body" "$@" "$base_url$path" +} + +echo "Создаём Алису и Боба" +alice_body="$(jq -nc --arg name "alice_$suffix" '{username: $name, display_name: "Алиса"}')" +bob_body="$(jq -nc --arg name "bob_$suffix" '{username: $name, display_name: "Боб"}')" +alice_id="$(post_json /api/v1/users "$alice_body" | jq -er '.id')" +bob_id="$(post_json /api/v1/users "$bob_body" | jq -er '.id')" + +echo "Создаём общий чат" +chat_body="$(jq -nc --arg alice "$alice_id" --arg bob "$bob_id" \ + '{title: "Учебный чат", member_ids: [$alice, $bob]}')" +chat_id="$(post_json /api/v1/chats "$chat_body" -H "X-User-Id: $alice_id" | jq -er '.id')" +path="/api/v1/chats/$chat_id/messages" + +echo "Отправляем сообщение от Алисы" +post_json "$path" '{"text":"Привет, Боб! 👋"}' -H "X-User-Id: $alice_id" | jq . + +echo "Отправляем ответ от Боба" +post_json "$path" '{"text":"Привет, Алиса!"}' -H "X-User-Id: $bob_id" | jq . + +echo "Читаем общую историю от имени Алисы" +curl --fail --silent --show-error --connect-timeout 5 --max-time 10 \ + -H "X-User-Id: $alice_id" "$base_url$path?limit=50" | jq . diff --git a/scripts/run-local.sh b/scripts/run-local.sh new file mode 100755 index 0000000..93708dd --- /dev/null +++ b/scripts/run-local.sh @@ -0,0 +1,15 @@ +#!/usr/bin/env bash +set -euo pipefail + +project_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)" +cd "$project_dir" + +if [[ ! -x .venv/bin/python ]]; then + echo "Не хватает .venv" >&2 + echo "python3 -m venv .venv" >&2 + echo ".venv/bin/python -m pip install -r requirements.txt" >&2 + exit 1 +fi + + +exec .venv/bin/python -m src diff --git a/scripts/start.sh b/scripts/start.sh new file mode 100755 index 0000000..cd59c42 --- /dev/null +++ b/scripts/start.sh @@ -0,0 +1,19 @@ +#!/usr/bin/env bash +set -euo pipefail + +project_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)" +container="${CONTAINER_NAME:-mtusi-messenger-lab2}" +port="${HOST_PORT:-8080}" + +if [[ ! -f "$project_dir/.env" ]]; then + cp "$project_dir/.env.example" "$project_dir/.env" +fi + + +docker run --detach --name "$container" --label mtusi.lab=2 \ + --cpus 1 --memory 256m \ + --publish "127.0.0.1:$port:8080" \ + --env-file "$project_dir/.env" \ + "${IMAGE_NAME:-mtusi-messenger:lab2}" +"$project_dir/scripts/wait-ready.sh" "$container" +echo "API: http://localhost:$port" diff --git a/scripts/stop.sh b/scripts/stop.sh new file mode 100755 index 0000000..138f686 --- /dev/null +++ b/scripts/stop.sh @@ -0,0 +1,12 @@ +#!/usr/bin/env bash +set -euo pipefail + +container="${CONTAINER_NAME:-mtusi-messenger-lab2}" +if [[ "$(docker inspect --format '{{index .Config.Labels "mtusi.lab"}}' "$container")" != 2 ]]; then + echo "У $container нет метки mtusi.lab=2; остановка отменена" >&2 + exit 1 +fi + +docker stop --time 10 "$container" +docker rm "$container" +echo "Контейнер удалён" diff --git a/scripts/wait-ready.sh b/scripts/wait-ready.sh new file mode 100755 index 0000000..ba39cff --- /dev/null +++ b/scripts/wait-ready.sh @@ -0,0 +1,19 @@ +#!/usr/bin/env bash +set -euo pipefail + +container="${1:-mtusi-messenger-lab2}" +for ((attempt = 0; attempt < 60; attempt++)); do + running="$(docker inspect --format '{{.State.Running}}' "$container")" + health="$(docker inspect --format '{{.State.Health.Status}}' "$container")" + if [[ "$running" != true || "$health" == unhealthy ]]; then + echo "Контейнер $container не готов. Посмотри: docker logs $container" >&2 + exit 1 + fi + if [[ "$health" == healthy ]]; then + echo "$container: healthy" + exit 0 + fi + sleep 1 +done +echo "Контейнер $container не стал готов за 60 секунд" >&2 +exit 1 diff --git a/src/__init__.py b/src/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/__main__.py b/src/__main__.py new file mode 100644 index 0000000..420cbd4 --- /dev/null +++ b/src/__main__.py @@ -0,0 +1,16 @@ +"""Общий запуск""" + +import os + +import uvicorn + + +if __name__ == "__main__": + uvicorn.run( + "src.app:app", + host=os.environ.get("APP_HOST", "0.0.0.0"), + port=int(os.environ.get("APP_PORT", "8080")), + workers=1, + access_log=False, + timeout_graceful_shutdown=5, + ) diff --git a/src/app.py b/src/app.py new file mode 100644 index 0000000..7edfe23 --- /dev/null +++ b/src/app.py @@ -0,0 +1,120 @@ +"""HTTP-адреса, определение пользователя и обработка запросов""" + +import json +import logging +from time import perf_counter +from uuid import UUID, uuid4 + +from fastapi import FastAPI, Request +from fastapi.exceptions import RequestValidationError +from starlette.exceptions import HTTPException + +from src import storage +from src.errors import ApiError, error_response +from src.models import CreateChat, CreateMessage, CreateUser +from src.pagination import make_page + + +app = FastAPI(title="Учебный мессенджер", redirect_slashes=False) +logger = logging.getLogger("uvicorn.error") + + +@app.middleware("http") +async def handle_request(request: Request, call_next): + request.state.request_id = str(uuid4()) + started = perf_counter() + path = request.url.path + try: + is_registration = request.method == "POST" and path == "/api/v1/users" + if path.startswith("/api/v1/") and not is_registration: + request.state.user = storage.current_user(request.headers.get("X-User-Id")) + if request.method == "POST" and path.startswith("/api/v1/"): + content_type = request.headers.get("Content-Type", "").split(";", 1)[0].strip().lower() + if content_type != "application/json": + raise ApiError(415, "unsupported_media_type", "Ожидается application/json") + try: + json.loads((await request.body()).decode("utf-8")) + except (ValueError, RecursionError): + raise ApiError(400, "invalid_request", "Ожидается корректный JSON в UTF-8") + response = await call_next(request) + except ApiError as error: + response = error_response(request, error) + except Exception as error: + # Не печатаем тело запроса или текст исключения: там могут быть сообщения. + logger.error("%s request_id=%s", type(error).__name__, request.state.request_id) + response = error_response(request, ApiError(500, "internal_error", "Внутренняя ошибка сервера")) + response.headers["X-Request-Id"] = request.state.request_id + logger.info( + "%s %s status=%s duration_ms=%.2f request_id=%s", + request.method, path, response.status_code, + (perf_counter() - started) * 1000, request.state.request_id, + ) + return response + + +@app.exception_handler(ApiError) +def handle_api_error(request: Request, error: ApiError): + return error_response(request, error) + + +@app.exception_handler(RequestValidationError) +def handle_validation_error(request: Request, error: RequestValidationError): + # Контракт требует 400 и наш формат ошибки, а не стандартный ответ FastAPI. + return error_response(request, ApiError(400, "invalid_request", "Неверные поля или параметры запроса")) + + +@app.exception_handler(HTTPException) +def handle_http_error(request: Request, error: HTTPException): + code = "not_found" if error.status_code == 404 else "invalid_request" + message = "Адрес не найден" if error.status_code == 404 else "Недопустимый HTTP-запрос" + response = error_response(request, ApiError(error.status_code, code, message)) + if error.headers: + response.headers.update(error.headers) + return response + + +@app.get("/health/live") +def health_live(): + return {"status": "ok"} + + +@app.get("/health/ready") +def health_ready(): + return {"status": "ok"} + + +@app.post("/api/v1/users", status_code=201) +def create_user(data: CreateUser): + return storage.create_user(data.username, data.display_name) + + +@app.get("/api/v1/users/me") +def current_user(request: Request): + return request.state.user + + +@app.post("/api/v1/chats", status_code=201) +def create_chat(request: Request, data: CreateChat): + return storage.create_chat(request.state.user["id"], data.title, data.member_ids) + + +@app.get("/api/v1/chats") +def list_chats(request: Request, limit: str = "50", cursor: str | None = None): + user_id = request.state.user["id"] + return make_page(storage.list_chats(user_id), "chats:" + user_id, limit, cursor) + + +@app.get("/api/v1/chats/{chat_id}") +def get_chat(request: Request, chat_id: UUID): + return storage.get_chat(request.state.user["id"], str(chat_id)) + + +@app.post("/api/v1/chats/{chat_id}/messages", status_code=201) +def create_message(request: Request, chat_id: UUID, data: CreateMessage): + return storage.create_message(request.state.user["id"], str(chat_id), data.text) + + +@app.get("/api/v1/chats/{chat_id}/messages") +def list_messages(request: Request, chat_id: UUID, limit: str = "50", cursor: str | None = None): + items = storage.list_messages(request.state.user["id"], str(chat_id)) + return make_page(items, "messages:" + str(chat_id), limit, cursor) diff --git a/src/errors.py b/src/errors.py new file mode 100644 index 0000000..92ffcc3 --- /dev/null +++ b/src/errors.py @@ -0,0 +1,26 @@ +"""общий формат ошибок""" + +from fastapi import Request +from fastapi.responses import JSONResponse + + +class ApiError(Exception): + def __init__(self, status: int, code: str, message: str): + super().__init__(message) + self.status = status + self.code = code + self.message = message + + +def error_response(request: Request, error: ApiError): + return JSONResponse( + status_code=error.status, + content={ + "error": { + "code": error.code, + "message": error.message, + "request_id": request.state.request_id, + } + }, + headers={"X-Request-Id": request.state.request_id}, + ) diff --git a/src/healthcheck.py b/src/healthcheck.py new file mode 100644 index 0000000..dae4d1d --- /dev/null +++ b/src/healthcheck.py @@ -0,0 +1,15 @@ +"""healthcheck докера""" + +import json +import os +from urllib.request import urlopen + + +if __name__ == "__main__": + port = os.environ.get("APP_PORT", "8080") + try: + with urlopen(f"http://127.0.0.1:{port}/health/ready", timeout=2) as response: + ready = response.status == 200 and json.load(response) == {"status": "ok"} + raise SystemExit(0 if ready else 1) + except (OSError, ValueError): + raise SystemExit(1) diff --git a/src/models.py b/src/models.py new file mode 100644 index 0000000..3f9fb9e --- /dev/null +++ b/src/models.py @@ -0,0 +1,40 @@ +"""допустимые поля и проверка данных""" + +from uuid import UUID + +from pydantic import BaseModel, ConfigDict, Field, field_validator + + +class RequestModel(BaseModel): + model_config = ConfigDict(extra="forbid", strict=True) + + @field_validator("*") + @classmethod + def check_strings(cls, value): + if isinstance(value, str): + if not value.strip(): + raise ValueError("Строка не должна состоять из пробелов") + value.encode("utf-8") + return value + + +class CreateUser(RequestModel): + username: str = Field(pattern=r"^[a-z0-9_]{3,32}$") + display_name: str = Field(min_length=1, max_length=100) + + +class CreateChat(RequestModel): + title: str = Field(min_length=1, max_length=100) + member_ids: list[str] = Field(min_length=1, max_length=100) + + @field_validator("member_ids") + @classmethod + def check_members(cls, values): + member_ids = [str(UUID(value)) for value in values] + if len(set(member_ids)) != len(member_ids): + raise ValueError("Участники не должны повторяться") + return member_ids + + +class CreateMessage(RequestModel): + text: str = Field(min_length=1, max_length=4000) diff --git a/src/pagination.py b/src/pagination.py new file mode 100644 index 0000000..e32dcd1 --- /dev/null +++ b/src/pagination.py @@ -0,0 +1,53 @@ +"""выдача страниц через курсор""" + +import base64 +import binascii +import json +import re + +from src.errors import ApiError + + +def item_key(item): + return item["created_at"], item["id"] + + +def read_cursor(cursor: str, scope: str, items: list): + try: + decoded = base64.b64decode(cursor, altchars=b"-_", validate=True) + data = json.loads(decoded.decode("utf-8")) + if not isinstance(data, dict) or set(data) != {"scope", "created_at", "id"}: + raise ValueError + if data["scope"] != scope: + raise ValueError + + for item in items: + if item["id"] == data["id"] and item["created_at"] == data["created_at"]: + return item_key(item) + except (ValueError, UnicodeError, binascii.Error, RecursionError): + pass + raise ApiError(400, "invalid_request", "Неверный cursor") + + +def make_page(items: list, scope: str, limit: str, cursor: str | None): + try: + if not re.fullmatch(r"[0-9]+", limit): + raise ValueError + size = int(limit) + if not 1 <= size <= 100: + raise ValueError + except ValueError: + raise ApiError(400, "invalid_request", "limit должен быть целым числом от 1 до 100") + + ordered = sorted(items, key=item_key) + if cursor is not None: + last_key = read_cursor(cursor, scope, ordered) + ordered = [item for item in ordered if item_key(item) > last_key] + + page = ordered[:size] + next_cursor = None + if len(ordered) > size: + last = page[-1] + data = {"scope": scope, "created_at": last["created_at"], "id": last["id"]} + next_cursor = base64.urlsafe_b64encode(json.dumps(data).encode("utf-8")).decode("ascii") + return {"items": page, "next_cursor": next_cursor} diff --git a/src/storage.py b/src/storage.py new file mode 100644 index 0000000..37a34a9 --- /dev/null +++ b/src/storage.py @@ -0,0 +1,90 @@ +"""словари, списки и блокировка для одновременных запросов""" + +from datetime import datetime, timezone +from threading import Lock +from uuid import UUID, uuid4 + +from src.errors import ApiError + + +users = {} # id -> пользователь +usernames = {} # username -> id, для проверки уникальности +chats = {} # id -> чат +messages = {} # id чата -> список сообщений +lock = Lock() + + +def new_record(): + return { + "id": str(uuid4()), + "created_at": datetime.now(timezone.utc).isoformat(timespec="microseconds").replace("+00:00", "Z"), + } + + +def create_user(username: str, display_name: str): + with lock: + if username in usernames: + raise ApiError(409, "conflict", "Имя пользователя уже занято") + user = new_record() + user.update(username=username, display_name=display_name) + users[user["id"]] = user + usernames[username] = user["id"] + return user + + +def current_user(user_id: str | None): + if not user_id: + raise ApiError(401, "unauthorized", "Укажите существующего пользователя в X-User-Id") + try: + user_id = str(UUID(user_id)) + except ValueError: + raise ApiError(400, "invalid_request", "X-User-Id должен быть UUID") + with lock: + if user_id not in users: + raise ApiError(401, "unauthorized", "Пользователь не найден") + return users[user_id] + + +def create_chat(user_id: str, title: str, member_ids: list[str]): + with lock: + if user_id not in member_ids: + raise ApiError(400, "invalid_request", "Создатель должен входить в member_ids") + if any(member_id not in users for member_id in member_ids): + raise ApiError(400, "invalid_request", "Все участники должны существовать") + chat = new_record() + chat.update(title=title, member_ids=member_ids.copy()) + chats[chat["id"]] = chat + messages[chat["id"]] = [] + return chat + + +def accessible_chat(user_id: str, chat_id: str): + chat = chats.get(chat_id) + if chat is None or user_id not in chat["member_ids"]: + raise ApiError(404, "not_found", "Чат не найден") + return chat + + +def get_chat(user_id: str, chat_id: str): + with lock: + return accessible_chat(user_id, chat_id) + + +def list_chats(user_id: str): + with lock: + return [chat for chat in chats.values() if user_id in chat["member_ids"]] + + +def create_message(user_id: str, chat_id: str, text: str): + with lock: + accessible_chat(user_id, chat_id) + message = new_record() + message.update(chat_id=chat_id, sender_id=user_id, text=text) + messages[chat_id].append(message) + return message + + +def list_messages(user_id: str, chat_id: str): + with lock: + accessible_chat(user_id, chat_id) + return messages[chat_id].copy() diff --git a/student-tests/docker_lifecycle.py b/student-tests/docker_lifecycle.py new file mode 100644 index 0000000..8e8fd3f --- /dev/null +++ b/student-tests/docker_lifecycle.py @@ -0,0 +1,153 @@ +"""Проверки докера во временном контейнере""" + +import argparse +from datetime import datetime, timezone +import json +import os +import platform +import socket +import subprocess +import time +from urllib.error import URLError +from urllib.request import urlopen +from uuid import uuid4 + +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() + + +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( + 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(): + 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 + 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") + 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) + + 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") + + docker("restart", "--time", "10", container) + wait_ready(container) + after = inspect(container) + assert after["Image"] == before["Image"] + assert after["State"]["StartedAt"] != before["State"]["StartedAt"] + + 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"]) + + check_shutdown(container, base_url, port) + print("PASS: all Docker and API checks completed") + finally: + if created: + docker("rm", "--force", container) + print("Temporary test container removed") + + +if __name__ == "__main__": + main() diff --git a/student-tests/helpers.py b/student-tests/helpers.py new file mode 100644 index 0000000..33a9a94 --- /dev/null +++ b/student-tests/helpers.py @@ -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) diff --git a/student-tests/test_api.py b/student-tests/test_api.py new file mode 100644 index 0000000..8e6fda1 --- /dev/null +++ b/student-tests/test_api.py @@ -0,0 +1,162 @@ +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: + 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": str(uuid4())}) + self.assert_error("GET", "/api/v1/users/me", 400, "invalid_request", + headers={"X-User-Id": "not-a-uuid"}) + 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"] + 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") diff --git a/student-tests/test_concurrency.py b/student-tests/test_concurrency.py new file mode 100644 index 0000000..666ea0b --- /dev/null +++ b/student-tests/test_concurrency.py @@ -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}"})["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)