summaryrefslogtreecommitdiffstats
path: root/tests/units/test_logger.py
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-03-28 06:11:39 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-03-28 06:11:39 +0000
commit1fd6a618b60d7168fd8f37585d5d39d22d775afd (patch)
treefbc6d0c213b8acdd0a31deafe5c5fc0d05a3a312 /tests/units/test_logger.py
parentInitial commit. (diff)
downloadanta-1fd6a618b60d7168fd8f37585d5d39d22d775afd.tar.xz
anta-1fd6a618b60d7168fd8f37585d5d39d22d775afd.zip
Adding upstream version 0.13.0.upstream/0.13.0
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests/units/test_logger.py')
-rw-r--r--tests/units/test_logger.py80
1 files changed, 80 insertions, 0 deletions
diff --git a/tests/units/test_logger.py b/tests/units/test_logger.py
new file mode 100644
index 0000000..6e1e5b4
--- /dev/null
+++ b/tests/units/test_logger.py
@@ -0,0 +1,80 @@
+# Copyright (c) 2023-2024 Arista Networks, Inc.
+# Use of this source code is governed by the Apache License 2.0
+# that can be found in the LICENSE file.
+"""
+Tests for anta.logger
+"""
+from __future__ import annotations
+
+import logging
+from typing import TYPE_CHECKING
+from unittest.mock import patch
+
+import pytest
+
+from anta.logger import anta_log_exception
+
+if TYPE_CHECKING:
+ from pytest import LogCaptureFixture
+
+
+@pytest.mark.parametrize(
+ "exception, message, calling_logger, __DEBUG__value, expected_message",
+ [
+ pytest.param(ValueError("exception message"), None, None, False, "ValueError (exception message)", id="exception only"),
+ pytest.param(ValueError("exception message"), "custom message", None, False, "custom message\nValueError (exception message)", id="custom message"),
+ pytest.param(
+ ValueError("exception message"),
+ "custom logger",
+ logging.getLogger("custom"),
+ False,
+ "custom logger\nValueError (exception message)",
+ id="custom logger",
+ ),
+ pytest.param(
+ ValueError("exception message"), "Use with custom message", None, True, "Use with custom message\nValueError (exception message)", id="__DEBUG__ on"
+ ),
+ ],
+)
+def test_anta_log_exception(
+ caplog: LogCaptureFixture,
+ exception: Exception,
+ message: str | None,
+ calling_logger: logging.Logger | None,
+ __DEBUG__value: bool,
+ expected_message: str,
+) -> None:
+ """
+ Test anta_log_exception
+ """
+
+ if calling_logger is not None:
+ # https://github.com/pytest-dev/pytest/issues/3697
+ calling_logger.propagate = True
+ caplog.set_level(logging.ERROR, logger=calling_logger.name)
+ else:
+ caplog.set_level(logging.ERROR)
+ # Need to raise to trigger nice stacktrace for __DEBUG__ == True
+ try:
+ raise exception
+ except ValueError as e:
+ with patch("anta.logger.__DEBUG__", __DEBUG__value):
+ anta_log_exception(e, message=message, calling_logger=calling_logger)
+
+ # Two log captured
+ if __DEBUG__value:
+ assert len(caplog.record_tuples) == 2
+ else:
+ assert len(caplog.record_tuples) == 1
+ logger, level, message = caplog.record_tuples[0]
+
+ if calling_logger is not None:
+ assert calling_logger.name == logger
+ else:
+ assert logger == "anta.logger"
+
+ assert level == logging.CRITICAL
+ assert message == expected_message
+ # the only place where we can see the stracktrace is in the capture.text
+ if __DEBUG__value is True:
+ assert "Traceback" in caplog.text