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>
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
"""Настройка набора: адрес стенда, проверка доступности, фикстуры."""
|
||||
|
||||
import os
|
||||
from collections.abc import Iterator
|
||||
import subprocess
|
||||
from collections.abc import Callable, Iterator
|
||||
from typing import Any
|
||||
|
||||
import httpx
|
||||
@@ -11,6 +12,10 @@ 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)
|
||||
@@ -22,15 +27,35 @@ def pytest_collection_modifyitems(
|
||||
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 ошибками."""
|
||||
@@ -80,3 +105,37 @@ def eve(api: Api) -> Actor:
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user