Mark startup runtime configuration task complete and update startup resource resolution
This commit is contained in:
@@ -4,8 +4,8 @@ import asyncio
|
||||
from pathlib import Path
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
|
||||
import pytest
|
||||
import aiosqlite
|
||||
import pytest
|
||||
from httpx import ASGITransport, AsyncClient
|
||||
|
||||
from app.config import Settings
|
||||
@@ -166,24 +166,22 @@ async def test_lifespan_cleans_up_resources_when_startup_fails(tmp_path: Path) -
|
||||
mock_http_session = MagicMock()
|
||||
mock_http_session.close = AsyncMock()
|
||||
|
||||
with (
|
||||
patch("app.startup.ensure_jail_configs"),
|
||||
patch("app.startup.aiohttp.ClientSession", return_value=mock_http_session),
|
||||
patch("app.startup.AsyncIOScheduler", return_value=mock_scheduler),
|
||||
patch("app.startup.init_db", new=AsyncMock()),
|
||||
patch("app.services.geo_service.init_geoip"),
|
||||
patch("app.services.geo_service.load_cache_from_db", new=AsyncMock(return_value=None)),
|
||||
patch("app.services.geo_service.count_unresolved", new=AsyncMock(return_value=0)),
|
||||
patch("app.services.setup_service.is_setup_complete", new=AsyncMock(return_value=False)),
|
||||
patch("app.tasks.health_check.register", side_effect=RuntimeError("startup failed")),
|
||||
patch("app.tasks.blocklist_import.register"),
|
||||
patch("app.tasks.geo_cache_flush.register"),
|
||||
patch("app.tasks.geo_re_resolve.register"),
|
||||
patch("app.tasks.history_sync.register"),
|
||||
):
|
||||
with pytest.raises(RuntimeError, match="startup failed"):
|
||||
async with _lifespan(app):
|
||||
pass
|
||||
with pytest.raises(RuntimeError, match="startup failed"), \
|
||||
patch("app.startup.ensure_jail_configs"), \
|
||||
patch("app.startup.aiohttp.ClientSession", return_value=mock_http_session), \
|
||||
patch("app.startup.AsyncIOScheduler", return_value=mock_scheduler), \
|
||||
patch("app.startup.init_db", new=AsyncMock()), \
|
||||
patch("app.services.geo_service.init_geoip"), \
|
||||
patch("app.services.geo_service.load_cache_from_db", new=AsyncMock(return_value=None)), \
|
||||
patch("app.services.geo_service.count_unresolved", new=AsyncMock(return_value=0)), \
|
||||
patch("app.services.setup_service.is_setup_complete", new=AsyncMock(return_value=False)), \
|
||||
patch("app.tasks.health_check.register", side_effect=RuntimeError("startup failed")), \
|
||||
patch("app.tasks.blocklist_import.register"), \
|
||||
patch("app.tasks.geo_cache_flush.register"), \
|
||||
patch("app.tasks.geo_re_resolve.register"), \
|
||||
patch("app.tasks.history_sync.register"):
|
||||
async with _lifespan(app):
|
||||
pass
|
||||
|
||||
mock_http_session.close.assert_awaited_once()
|
||||
mock_scheduler.shutdown.assert_called_once_with(wait=False)
|
||||
@@ -297,6 +295,75 @@ async def test_startup_overrides_settings_from_persisted_setup(tmp_path: Path) -
|
||||
assert Path(runtime_db_path).exists()
|
||||
|
||||
|
||||
async def test_startup_loads_geo_cache_from_persisted_runtime_database(tmp_path: Path) -> None:
|
||||
"""Startup must load geo cache from the resolved runtime database."""
|
||||
env_settings = Settings(
|
||||
database_path=str(tmp_path / "pointer.db"),
|
||||
fail2ban_socket="/tmp/fake_fail2ban.sock",
|
||||
fail2ban_config_dir=str(tmp_path / "fail2ban"),
|
||||
session_secret="test-startup-secret",
|
||||
session_duration_minutes=60,
|
||||
timezone="UTC",
|
||||
log_level="debug",
|
||||
)
|
||||
app = create_app(settings=env_settings)
|
||||
|
||||
runtime_db_path = str(tmp_path / "runtime.db")
|
||||
opened_connections: list[tuple[str, aiosqlite.Connection]] = []
|
||||
|
||||
async def fake_open_db(path: str) -> aiosqlite.Connection:
|
||||
connection = await aiosqlite.connect(path)
|
||||
opened_connections.append((path, connection))
|
||||
return connection
|
||||
|
||||
mock_scheduler = MagicMock()
|
||||
mock_scheduler.start = MagicMock()
|
||||
mock_scheduler.shutdown = MagicMock()
|
||||
|
||||
mock_http_session = MagicMock()
|
||||
mock_http_session.close = AsyncMock()
|
||||
|
||||
load_cache = AsyncMock()
|
||||
with (
|
||||
patch("app.startup.ensure_jail_configs"),
|
||||
patch("app.startup.aiohttp.ClientSession", return_value=mock_http_session),
|
||||
patch("app.startup.AsyncIOScheduler", return_value=mock_scheduler),
|
||||
patch("app.startup.open_db", new=AsyncMock(side_effect=fake_open_db)),
|
||||
patch("app.startup.init_db", new=AsyncMock()),
|
||||
patch("app.services.geo_service.init_geoip"),
|
||||
patch("app.services.geo_service.load_cache_from_db", new=load_cache),
|
||||
patch("app.services.geo_service.count_unresolved", new=AsyncMock(return_value=0)),
|
||||
patch("app.services.setup_service.is_setup_complete", new=AsyncMock(return_value=True)),
|
||||
patch(
|
||||
"app.services.setup_service.get_persisted_runtime_settings",
|
||||
new=AsyncMock(
|
||||
return_value={
|
||||
"database_path": runtime_db_path,
|
||||
"fail2ban_socket": "/tmp/persisted.sock",
|
||||
"timezone": "Europe/Berlin",
|
||||
"session_duration_minutes": 123,
|
||||
}
|
||||
),
|
||||
),
|
||||
patch("app.tasks.health_check.register"),
|
||||
patch("app.tasks.blocklist_import.register"),
|
||||
patch("app.tasks.geo_cache_flush.register"),
|
||||
patch("app.tasks.geo_re_resolve.register"),
|
||||
patch("app.tasks.history_sync.register"),
|
||||
):
|
||||
async with _lifespan(app):
|
||||
loaded_db = load_cache.call_args.args[0]
|
||||
runtime_connections = [conn for path, conn in opened_connections if path == runtime_db_path]
|
||||
assert runtime_connections, "Expected runtime database to be opened"
|
||||
assert loaded_db in runtime_connections
|
||||
|
||||
assert app.state.runtime_settings is not None
|
||||
assert app.state.runtime_settings.database_path == runtime_db_path
|
||||
|
||||
for _, connection in opened_connections:
|
||||
await connection.close()
|
||||
|
||||
|
||||
async def test_concurrent_requests_use_request_scoped_db_connections(tmp_path: Path) -> None:
|
||||
"""Concurrent requests each open and close their own database connection."""
|
||||
settings = Settings(
|
||||
|
||||
Reference in New Issue
Block a user