Files
mtusi2026lab2/student-tests/conftest.py
T
ricoromaandClaude Opus 5.5 5b65051a60 test: add automatic failure checks on a separate stand
make failure_checks brings up a throwaway compose project with its own
PostgreSQL and Valkey, a default and a tight API (2 s sessions, pool of
one), and runs the pytest tests marked fault through an explicit hook:
session expiry, database stop and return, pool exhaustion, chat creation
rollback, readiness, Valkey stop, hang, return and flush. The stand and
its volume are removed on any outcome; the working stand is untouched.
Regular runs deselect the fault tests, and 13 coverage exclusions go away.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-01 23:20:34 +03:00

142 lines
4.7 KiB
Python

"""Настройка набора: адрес стенда, проверка доступности, фикстуры."""
import os
import subprocess
from collections.abc import Callable, Iterator
from typing import Any
import httpx
import pytest
from api_client import Actor, Api
from spec_coverage import COLLECTED, Collected
BASE_URL = os.environ.get("BASE_URL", "http://localhost:8080").rstrip("/")
# hook стенда отказов и «тесный» API задаёт make failure_checks
FAULT_HOOK = os.environ.get("FAULT_HOOK", "")
FAULT_TIGHT_BASE_URL = os.environ.get("FAULT_TIGHT_BASE_URL", "").rstrip("/")
FAULT_HOOK_TIMEOUT_S = 120
@pytest.hookimpl(tryfirst=True)
def pytest_collection_modifyitems(
config: pytest.Config,
items: list[pytest.Item],
) -> None:
"""Маркеры `spec` всех собранных тестов, до отбора по `-k` / `-m`."""
collected = Collected()
for item in items:
collected.modules.add(item.path.name)
is_fault = item.get_closest_marker("fault") is not None
for mark in item.iter_markers("spec"):
if len(mark.args) != 2 or mark.kwargs:
raise pytest.UsageError(
f"{item.nodeid}: spec marker takes (capability, scenario)",
)
collected.marks.setdefault(mark.args, []).append(item.nodeid)
if is_fault:
collected.fault.add(mark.args)
config.stash[COLLECTED] = collected
@pytest.hookimpl(trylast=True)
def pytest_collection_finish(session: pytest.Session) -> None:
"""Проверки отказов без стенда make failure_checks не запускаются.
Выход до первого теста: без hook ни одно хранилище не трогается.
"""
selected = [
item for item in session.items if item.get_closest_marker("fault")
]
if selected and not (FAULT_HOOK and FAULT_TIGHT_BASE_URL):
pytest.exit(
"fault checks need the failure stand and its hook: "
"run make failure_checks",
returncode=2,
)
@pytest.fixture(scope="session", autouse=True)
def _server_reachable() -> None:
"""Без сервера прогон останавливается одной строкой, а не 100 ошибками."""
try:
resp = httpx.get(f"{BASE_URL}/health/live", timeout=2)
except httpx.HTTPError as exc:
pytest.exit(
f"server is not reachable at {BASE_URL}: {exc!r}",
returncode=2,
)
if resp.status_code != 200:
pytest.exit(
f"{BASE_URL}/health/live answered {resp.status_code}: "
"another service on this port?",
returncode=2,
)
@pytest.fixture(scope="session")
def base_url() -> str:
return BASE_URL
@pytest.fixture(scope="session")
def api() -> Iterator[Api]:
with Api(BASE_URL) as client:
yield client
@pytest.fixture
def alice(api: Api) -> Actor:
return api.register(display_name="Алиса")
@pytest.fixture
def bob(api: Api) -> Actor:
return api.register(display_name="Боб")
@pytest.fixture
def eve(api: Api) -> Actor:
"""Посторонний: ни в одном чате фикстур не состоит."""
return api.register(display_name="Ева")
@pytest.fixture
def chat(api: Api, alice: Actor, bob: Actor) -> dict[str, Any]:
"""Чат Алисы и Боба."""
return api.create_chat(alice, bob, title="Алиса и Боб")
@pytest.fixture(scope="session")
def stand() -> Callable[..., str]:
"""Действие над стендом отказов через hook; возвращает его stdout.
`stand("stop", "postgres")` — «стенд, останови postgres». Hook —
исполняемый файл, вызов без shell, как `ACTION_SCRIPT` в
`tests/resilience.py`. Сам тест docker не трогает. Не путать с
маркером `fault`: он только выбирает тесты (`-m fault`).
"""
def call(*args: str) -> str:
result = subprocess.run(
[FAULT_HOOK, *args],
capture_output=True,
text=True,
timeout=FAULT_HOOK_TIMEOUT_S,
check=False,
)
assert result.returncode == 0, (
f"fault hook {' '.join(args)}: {result.stderr.strip()}"
)
return result.stdout.strip()
return call
@pytest.fixture(scope="session")
def tight_api() -> Iterator[Api]:
"""API стенда отказов со сроком сессии 2 с и пулом из одного
соединения."""
with Api(FAULT_TIGHT_BASE_URL) as client:
yield client