mirror of
https://github.com/arcan1s/ahriman.git
synced 2025-07-23 18:59:56 +00:00
runtime logger handler selector
This commit is contained in:
31
tests/ahriman/core/log/test_journal_handler.py
Normal file
31
tests/ahriman/core/log/test_journal_handler.py
Normal file
@ -0,0 +1,31 @@
|
||||
import sys
|
||||
|
||||
from pytest_mock import MockerFixture
|
||||
|
||||
|
||||
# because of how imports work it must be first test
|
||||
def test_dummy_journal_handler(mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must import dummy journal handler if upstream systemd was not found
|
||||
"""
|
||||
mocker.patch.dict(sys.modules, {"systemd.journal": None})
|
||||
from logging import NullHandler
|
||||
from ahriman.core.log.journal_handler import JournalHandler
|
||||
assert issubclass(JournalHandler, NullHandler)
|
||||
|
||||
|
||||
def test_init() -> None:
|
||||
"""
|
||||
must init dummy handler
|
||||
"""
|
||||
from ahriman.core.log.journal_handler import _JournalHandler
|
||||
assert _JournalHandler(42, answer=42)
|
||||
|
||||
|
||||
def test_journal_handler() -> None:
|
||||
"""
|
||||
must import journal handler
|
||||
"""
|
||||
from systemd.journal import JournalHandler as UpstreamJournalHandler
|
||||
from ahriman.core.log.journal_handler import JournalHandler
|
||||
assert JournalHandler is UpstreamJournalHandler
|
@ -1,21 +1,59 @@
|
||||
import logging
|
||||
import pytest
|
||||
import sys
|
||||
|
||||
from logging.config import fileConfig
|
||||
from pytest_mock import MockerFixture
|
||||
from systemd.journal import JournalHandler
|
||||
|
||||
from ahriman.core.configuration import Configuration
|
||||
from ahriman.core.log import Log
|
||||
from ahriman.models.log_handler import LogHandler
|
||||
|
||||
|
||||
def test_handler() -> None:
|
||||
"""
|
||||
must extract journald handler if available
|
||||
"""
|
||||
assert Log.handler(None) == LogHandler.Journald
|
||||
|
||||
|
||||
def test_handler_selected() -> None:
|
||||
"""
|
||||
must return selected log handler
|
||||
"""
|
||||
assert Log.handler(LogHandler.Console) == LogHandler.Console
|
||||
|
||||
|
||||
def test_handler_syslog(mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must return syslog handler if no journal is available
|
||||
"""
|
||||
mocker.patch("pathlib.Path.exists", return_value=True)
|
||||
mocker.patch.dict(sys.modules, {"systemd.journal": None})
|
||||
assert Log.handler(None) == LogHandler.Syslog
|
||||
|
||||
|
||||
def test_handler_console(mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must return console handler if no journal is available and no log device was found
|
||||
"""
|
||||
mocker.patch("pathlib.Path.exists", return_value=False)
|
||||
mocker.patch.dict(sys.modules, {"systemd.journal": None})
|
||||
assert Log.handler(None) == LogHandler.Console
|
||||
|
||||
|
||||
def test_load(configuration: Configuration, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must load logging
|
||||
"""
|
||||
logging_mock = mocker.patch("ahriman.core.log.log.fileConfig")
|
||||
logging_mock = mocker.patch("ahriman.core.log.log.fileConfig", side_effect=fileConfig)
|
||||
http_log_mock = mocker.patch("ahriman.core.log.http_log_handler.HttpLogHandler.load")
|
||||
|
||||
Log.load(configuration, quiet=False, report=False)
|
||||
logging_mock.assert_called_once_with(configuration.logging_path)
|
||||
Log.load(configuration, LogHandler.Journald, quiet=False, report=False)
|
||||
logging_mock.assert_called_once_with(pytest.helpers.anyvar(int), disable_existing_loggers=True)
|
||||
http_log_mock.assert_called_once_with(configuration, report=False)
|
||||
assert all(isinstance(handler, JournalHandler) for handler in logging.getLogger().handlers)
|
||||
|
||||
|
||||
def test_load_fallback(configuration: Configuration, mocker: MockerFixture) -> None:
|
||||
@ -23,7 +61,7 @@ def test_load_fallback(configuration: Configuration, mocker: MockerFixture) -> N
|
||||
must fall back to stderr without errors
|
||||
"""
|
||||
mocker.patch("ahriman.core.log.log.fileConfig", side_effect=PermissionError())
|
||||
Log.load(configuration, quiet=False, report=False)
|
||||
Log.load(configuration, LogHandler.Journald, quiet=False, report=False)
|
||||
|
||||
|
||||
def test_load_quiet(configuration: Configuration, mocker: MockerFixture) -> None:
|
||||
@ -31,5 +69,5 @@ def test_load_quiet(configuration: Configuration, mocker: MockerFixture) -> None
|
||||
must disable logging in case if quiet flag set
|
||||
"""
|
||||
disable_mock = mocker.patch("logging.disable")
|
||||
Log.load(configuration, quiet=True, report=False)
|
||||
Log.load(configuration, LogHandler.Journald, quiet=True, report=False)
|
||||
disable_mock.assert_called_once_with(logging.WARNING)
|
||||
|
Reference in New Issue
Block a user