mirror of
https://github.com/arcan1s/ahriman.git
synced 2025-06-28 06:41:43 +00:00
runtime logger handler selector
This commit is contained in:
@ -7,6 +7,7 @@ from pytest_mock import MockerFixture
|
||||
from ahriman.application.handlers import Handler
|
||||
from ahriman.core.configuration import Configuration
|
||||
from ahriman.core.exceptions import ExitCode, MissingArchitectureError, MultipleArchitecturesError
|
||||
from ahriman.models.log_handler import LogHandler
|
||||
|
||||
|
||||
def test_architectures_extract(args: argparse.Namespace, configuration: Configuration, mocker: MockerFixture) -> None:
|
||||
@ -56,17 +57,20 @@ def test_call(args: argparse.Namespace, configuration: Configuration, mocker: Mo
|
||||
must call inside lock
|
||||
"""
|
||||
args.configuration = Path("")
|
||||
args.log_handler = LogHandler.Console
|
||||
args.quiet = False
|
||||
args.report = False
|
||||
mocker.patch("ahriman.application.handlers.Handler.run")
|
||||
configuration_mock = mocker.patch("ahriman.core.configuration.Configuration.from_path", return_value=configuration)
|
||||
log_handler_mock = mocker.patch("ahriman.core.log.Log.handler", return_value=args.log_handler)
|
||||
log_load_mock = mocker.patch("ahriman.core.log.Log.load")
|
||||
enter_mock = mocker.patch("ahriman.application.lock.Lock.__enter__")
|
||||
exit_mock = mocker.patch("ahriman.application.lock.Lock.__exit__")
|
||||
|
||||
assert Handler.call(args, "x86_64")
|
||||
configuration_mock.assert_called_once_with(args.configuration, "x86_64")
|
||||
log_load_mock.assert_called_once_with(configuration, quiet=args.quiet, report=args.report)
|
||||
log_handler_mock.assert_called_once_with(args.log_handler)
|
||||
log_load_mock.assert_called_once_with(configuration, args.log_handler, quiet=args.quiet, report=args.report)
|
||||
enter_mock.assert_called_once_with()
|
||||
exit_mock.assert_called_once_with(None, None, None)
|
||||
|
||||
|
@ -6,6 +6,7 @@ from pytest_mock import MockerFixture
|
||||
from ahriman.application.handlers import Handler
|
||||
from ahriman.models.action import Action
|
||||
from ahriman.models.build_status import BuildStatusEnum
|
||||
from ahriman.models.log_handler import LogHandler
|
||||
from ahriman.models.sign_settings import SignSettings
|
||||
from ahriman.models.user_access import UserAccess
|
||||
|
||||
@ -37,6 +38,14 @@ def test_parser_option_lock(parser: argparse.ArgumentParser) -> None:
|
||||
assert isinstance(args.lock, Path)
|
||||
|
||||
|
||||
def test_parser_option_log_handler(parser: argparse.ArgumentParser) -> None:
|
||||
"""
|
||||
must convert log-handler option to LogHandler instance
|
||||
"""
|
||||
args = parser.parse_args(["--log-handler", "console", "service-config"])
|
||||
assert isinstance(args.log_handler, LogHandler)
|
||||
|
||||
|
||||
def test_multiple_architectures(parser: argparse.ArgumentParser) -> None:
|
||||
"""
|
||||
must accept multiple architectures
|
||||
|
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)
|
||||
|
0
tests/ahriman/models/test_log_handler.py
Normal file
0
tests/ahriman/models/test_log_handler.py
Normal file
Reference in New Issue
Block a user