Files
mtusi2026lab7-safronov/service-tests/unit/conftest.py
ricoromaandClaude Opus 5.5 64210dace0 feat: load process secrets from OpenBao via cert auth
OpenBao 2.7.3 (raft, TLS 1.3 with client certificates) holds the
PostgreSQL role passwords, the Valkey password and the S3 keys; each
process logs in with its own mTLS identity, reads only its paths and
renews its token in the background. Operator init, unseal, configure
and seed go through infra/openbao/bao.py and make bao_*; checks are
make bao_check and driver.py openbao-outage.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-26 13:55:09 +03:00

97 lines
2.9 KiB
Python

"""Быстрые тесты сервисов: без PostgreSQL, Valkey, S3, брокера, docker и сети.
`settings` читает `.env` и `config.toml` по абсолютному пути, поэтому поля, от
которых зависят ветки сервисов, подставляются здесь: результат не зависит от
локальных файлов и окружения (design change `unit-of-work`, решение 13).
"""
import pytest
from fakes import (
FakeCache,
FakeDatabase,
FakeJobPublisher,
FakeObjectStorage,
FakeUnitOfWork,
)
from src.config import settings
from src.infra.secrets import substitute_for_tests
from src.schemas.enums import MetricBucketsEnum
# глобалы клиентов процесса API (src/api/dependencies.py → db/session.py,
# cache/instance.py, s3/instance.py) берут секции с секретами из OpenBao
# (change openbao-secrets): быстрые тесты подставляют их кодом до импорта
# клиентов, OpenBao и сети нет
substitute_for_tests(
"api",
database=settings.database,
cache=settings.cache,
s3=settings.s3,
)
@pytest.fixture
def anyio_backend() -> str:
return "asyncio"
@pytest.fixture(autouse=True)
def fixed_settings(monkeypatch: pytest.MonkeyPatch) -> None:
fixed = (
(
settings.worker,
{
"max_attempts": 3,
"job_timeout_seconds": 60,
"lease_margin_seconds": 10,
"retry_base_delay_seconds": 5,
"retry_max_delay_seconds": 300,
"reconnect_seconds": 0.01,
},
),
(settings.auth, {"session_ttl_seconds": 3600}),
(settings.idempotency, {"ttl_seconds": 86400}),
(settings.cache, {"chat_ttl_seconds": 600}),
(
settings.uploads,
{"body_timeout_seconds": 1, "max_unfinished_jobs": 0},
),
(settings.app, {"instance_id": "test-instance"}),
(
settings.telemetry,
{
"trace_sample_ratio": 1.0,
"metric_buckets": MetricBucketsEnum.DEFAULT,
"metric_export_interval_seconds": 15,
},
),
)
for section, fields in fixed:
for name, value in fields.items():
monkeypatch.setattr(section, name, value)
@pytest.fixture
def database() -> FakeDatabase:
return FakeDatabase()
@pytest.fixture
def uow(database: FakeDatabase) -> FakeUnitOfWork:
return FakeUnitOfWork(database)
@pytest.fixture
def cache(database: FakeDatabase) -> FakeCache:
return FakeCache(database)
@pytest.fixture
def storage(database: FakeDatabase) -> FakeObjectStorage:
return FakeObjectStorage(database)
@pytest.fixture
def publisher(database: FakeDatabase) -> FakeJobPublisher:
return FakeJobPublisher(database)