mirror of
https://github.com/arcan1s/ahriman.git
synced 2025-07-23 02:39:57 +00:00
refactor: move logs rotation to separated trigger which is enabled by default
Previous solution, well, worked kinda fine-ish, though we have much better mechanisms to do so
This commit is contained in:
19
tests/ahriman/core/housekeeping/conftest.py
Normal file
19
tests/ahriman/core/housekeeping/conftest.py
Normal file
@ -0,0 +1,19 @@
|
||||
import pytest
|
||||
|
||||
from ahriman.core.configuration import Configuration
|
||||
from ahriman.core.housekeeping import LogsRotationTrigger
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def logs_rotation_trigger(configuration: Configuration) -> LogsRotationTrigger:
|
||||
"""
|
||||
logs roration trigger fixture
|
||||
|
||||
Args:
|
||||
configuration(Configuration): configuration fixture
|
||||
|
||||
Returns:
|
||||
LogsRotationTrigger: logs rotation trigger test instance
|
||||
"""
|
||||
_, repository_id = configuration.check_loaded()
|
||||
return LogsRotationTrigger(repository_id, configuration)
|
@ -0,0 +1,26 @@
|
||||
from pytest_mock import MockerFixture
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
from ahriman.core.configuration import Configuration
|
||||
from ahriman.core.housekeeping import LogsRotationTrigger
|
||||
from ahriman.core.status import Client
|
||||
from ahriman.models.result import Result
|
||||
|
||||
|
||||
def test_configuration_sections(configuration: Configuration) -> None:
|
||||
"""
|
||||
must correctly parse target list
|
||||
"""
|
||||
assert LogsRotationTrigger.configuration_sections(configuration) == ["logs-rotation"]
|
||||
|
||||
|
||||
def test_rotate(logs_rotation_trigger: LogsRotationTrigger, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must rotate logs
|
||||
"""
|
||||
client_mock = MagicMock()
|
||||
context_mock = mocker.patch("ahriman.core._Context.get", return_value=client_mock)
|
||||
|
||||
logs_rotation_trigger.on_result(Result(), [])
|
||||
context_mock.assert_called_once_with(Client)
|
||||
client_mock.logs_rotate.assert_called_once_with(logs_rotation_trigger.keep_last_records)
|
@ -20,14 +20,12 @@ def test_load(configuration: Configuration, mocker: MockerFixture) -> None:
|
||||
|
||||
add_mock = mocker.patch("logging.Logger.addHandler")
|
||||
load_mock = mocker.patch("ahriman.core.status.Client.load")
|
||||
atexit_mock = mocker.patch("atexit.register")
|
||||
|
||||
_, repository_id = configuration.check_loaded()
|
||||
handler = HttpLogHandler.load(repository_id, configuration, report=False)
|
||||
assert handler
|
||||
add_mock.assert_called_once_with(handler)
|
||||
load_mock.assert_called_once_with(repository_id, configuration, report=False)
|
||||
atexit_mock.assert_called_once_with(handler.rotate)
|
||||
|
||||
|
||||
def test_load_exist(configuration: Configuration) -> None:
|
||||
@ -96,16 +94,3 @@ def test_emit_skip(configuration: Configuration, log_record: logging.LogRecord,
|
||||
|
||||
handler.emit(log_record)
|
||||
log_mock.assert_not_called()
|
||||
|
||||
|
||||
def test_rotate(configuration: Configuration, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must rotate logs
|
||||
"""
|
||||
rotate_mock = mocker.patch("ahriman.core.status.Client.logs_rotate")
|
||||
|
||||
_, repository_id = configuration.check_loaded()
|
||||
handler = HttpLogHandler(repository_id, configuration, report=False, suppress_errors=False)
|
||||
|
||||
handler.rotate()
|
||||
rotate_mock.assert_called_once_with(handler.keep_last_records)
|
||||
|
@ -37,6 +37,9 @@ target =
|
||||
[keyring]
|
||||
target = keyring
|
||||
|
||||
[logs-rotation]
|
||||
keep_last_logs = 5
|
||||
|
||||
[mirrorlist]
|
||||
target = mirrorlist
|
||||
servers = http://localhost
|
||||
|
Reference in New Issue
Block a user