From ae9118654d64db36807564fd0b798e8f763b2348 Mon Sep 17 00:00:00 2001 From: Evgeniy Alekseev Date: Tue, 22 Nov 2022 02:28:52 +0200 Subject: [PATCH] check log record in handler instead of client --- src/ahriman/core/log/http_log_handler.py | 6 ++++- src/ahriman/core/status/client.py | 3 ++- src/ahriman/core/status/web_client.py | 7 ++---- .../ahriman/core/log/test_http_log_handler.py | 23 ++++++++++++++++--- tests/ahriman/core/status/test_client.py | 4 ++-- tests/ahriman/core/status/test_web_client.py | 16 +++---------- 6 files changed, 34 insertions(+), 25 deletions(-) diff --git a/src/ahriman/core/log/http_log_handler.py b/src/ahriman/core/log/http_log_handler.py index f7a90707..c7981b4e 100644 --- a/src/ahriman/core/log/http_log_handler.py +++ b/src/ahriman/core/log/http_log_handler.py @@ -74,7 +74,11 @@ class HttpLogHandler(logging.Handler): Args: record(logging.LogRecord): log record to log """ + package_base = getattr(record, "package_base", None) + if package_base is None: + return # in case if no package base supplied we need just skip log message + try: - self.reporter.logs(record) + self.reporter.logs(package_base, record) except Exception: self.handleError(record) diff --git a/src/ahriman/core/status/client.py b/src/ahriman/core/status/client.py index fcea1451..24bf93ad 100644 --- a/src/ahriman/core/status/client.py +++ b/src/ahriman/core/status/client.py @@ -89,11 +89,12 @@ class Client: """ return InternalStatus(status=BuildStatus()) - def logs(self, record: logging.LogRecord) -> None: + def logs(self, package_base: str, record: logging.LogRecord) -> None: """ post log record Args: + package_base(str) package base record(logging.LogRecord): log record to post to api """ diff --git a/src/ahriman/core/status/web_client.py b/src/ahriman/core/status/web_client.py index 59309358..d2660a88 100644 --- a/src/ahriman/core/status/web_client.py +++ b/src/ahriman/core/status/web_client.py @@ -204,17 +204,14 @@ class WebClient(Client, LazyLogging): self.logger.exception("could not get web service status") return InternalStatus(status=BuildStatus()) - def logs(self, record: logging.LogRecord) -> None: + def logs(self, package_base: str, record: logging.LogRecord) -> None: """ post log record Args: + package_base(str) package base record(logging.LogRecord): log record to post to api """ - package_base = getattr(record, "package_base", None) - if package_base is None: - return # in case if no package base supplised we need just skip log message - payload = { "created": record.created, "message": record.getMessage(), diff --git a/tests/ahriman/core/log/test_http_log_handler.py b/tests/ahriman/core/log/test_http_log_handler.py index 0f82d67f..f99770c8 100644 --- a/tests/ahriman/core/log/test_http_log_handler.py +++ b/tests/ahriman/core/log/test_http_log_handler.py @@ -4,6 +4,7 @@ from pytest_mock import MockerFixture from ahriman.core.configuration import Configuration from ahriman.core.log.http_log_handler import HttpLogHandler +from ahriman.models.package import Package def test_load(configuration: Configuration, mocker: MockerFixture) -> None: @@ -33,24 +34,40 @@ def test_load_exist(configuration: Configuration) -> None: assert handler is new_handler -def test_emit(configuration: Configuration, log_record: logging.LogRecord, mocker: MockerFixture) -> None: +def test_emit(configuration: Configuration, log_record: logging.LogRecord, package_ahriman: Package, + mocker: MockerFixture) -> None: """ must emit log record to reporter """ + log_record.package_base = package_ahriman.base log_mock = mocker.patch("ahriman.core.status.client.Client.logs") + handler = HttpLogHandler(configuration, report=False) handler.emit(log_record) - log_mock.assert_called_once_with(log_record) + log_mock.assert_called_once_with(package_ahriman.base, log_record) -def test_emit_failed(configuration: Configuration, log_record: logging.LogRecord, mocker: MockerFixture) -> None: +def test_emit_failed(configuration: Configuration, log_record: logging.LogRecord, package_ahriman: Package, + mocker: MockerFixture) -> None: """ must call handle error on exception """ + log_record.package_base = package_ahriman.base mocker.patch("ahriman.core.status.client.Client.logs", side_effect=Exception()) handle_error_mock = mocker.patch("logging.Handler.handleError") handler = HttpLogHandler(configuration, report=False) handler.emit(log_record) handle_error_mock.assert_called_once_with(log_record) + + +def test_emit_skip(configuration: Configuration, log_record: logging.LogRecord, mocker: MockerFixture) -> None: + """ + must skip log record posting if no package base set + """ + log_mock = mocker.patch("ahriman.core.status.client.Client.logs") + handler = HttpLogHandler(configuration, report=False) + + handler.emit(log_record) + log_mock.assert_not_called() diff --git a/tests/ahriman/core/status/test_client.py b/tests/ahriman/core/status/test_client.py index 87651426..612c797a 100644 --- a/tests/ahriman/core/status/test_client.py +++ b/tests/ahriman/core/status/test_client.py @@ -68,11 +68,11 @@ def test_get_internal(client: Client) -> None: assert actual == expected -def test_log(client: Client, log_record: logging.LogRecord) -> None: +def test_log(client: Client, package_ahriman: Package, log_record: logging.LogRecord) -> None: """ must process log record without errors """ - client.logs(log_record) + client.logs(package_ahriman.base, log_record) def test_remove(client: Client, package_ahriman: Package) -> None: diff --git a/tests/ahriman/core/status/test_web_client.py b/tests/ahriman/core/status/test_web_client.py index 49b6fa11..75331e83 100644 --- a/tests/ahriman/core/status/test_web_client.py +++ b/tests/ahriman/core/status/test_web_client.py @@ -14,7 +14,7 @@ from ahriman.models.package import Package from ahriman.models.user import User -def test_status_url(web_client: WebClient) -> None: +def test_login_url(web_client: WebClient) -> None: """ must generate login url correctly """ @@ -215,14 +215,13 @@ def test_logs(web_client: WebClient, log_record: logging.LogRecord, package_ahri must process log record """ requests_mock = mocker.patch("requests.Session.post") - log_record.package_base = package_ahriman.base payload = { "created": log_record.created, "message": log_record.getMessage(), "process_id": log_record.process, } - web_client.logs(log_record) + web_client.logs(package_ahriman.base, log_record) requests_mock.assert_called_once_with(pytest.helpers.anyvar(str, True), json=payload) @@ -234,16 +233,7 @@ def test_log_failed(web_client: WebClient, log_record: logging.LogRecord, packag mocker.patch("requests.Session.post", side_effect=Exception()) log_record.package_base = package_ahriman.base with pytest.raises(Exception): - web_client.logs(log_record) - - -def test_log_skip(web_client: WebClient, log_record: logging.LogRecord, mocker: MockerFixture) -> None: - """ - must skip log record posting if no package base set - """ - requests_mock = mocker.patch("requests.Session.post") - web_client.logs(log_record) - requests_mock.assert_not_called() + web_client.logs(package_ahriman.base, log_record) def test_remove(web_client: WebClient, package_ahriman: Package, mocker: MockerFixture) -> None: