2025-02-05 11:32:35 +01:00
|
|
|
# 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.
|
2025-02-05 11:39:09 +01:00
|
|
|
"""Tests for anta.logger."""
|
|
|
|
|
2025-02-05 11:32:35 +01:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
import logging
|
|
|
|
from unittest.mock import patch
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2025-02-05 11:39:09 +01:00
|
|
|
from anta.logger import anta_log_exception, exc_to_str, tb_to_str
|
2025-02-05 11:32:35 +01:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
2025-02-05 11:39:09 +01:00
|
|
|
("exception", "message", "calling_logger", "debug_value", "expected_message"),
|
2025-02-05 11:32:35 +01:00
|
|
|
[
|
2025-02-05 11:39:09 +01:00
|
|
|
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",
|
|
|
|
),
|
2025-02-05 11:32:35 +01:00
|
|
|
pytest.param(
|
|
|
|
ValueError("exception message"),
|
|
|
|
"custom logger",
|
|
|
|
logging.getLogger("custom"),
|
|
|
|
False,
|
2025-02-05 11:39:09 +01:00
|
|
|
"custom logger\nValueError: exception message",
|
2025-02-05 11:32:35 +01:00
|
|
|
id="custom logger",
|
|
|
|
),
|
|
|
|
pytest.param(
|
2025-02-05 11:39:09 +01:00
|
|
|
ValueError("exception message"),
|
|
|
|
"Use with custom message",
|
|
|
|
None,
|
|
|
|
True,
|
|
|
|
"Use with custom message\nValueError: exception message",
|
|
|
|
id="__DEBUG__ on",
|
2025-02-05 11:32:35 +01:00
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_anta_log_exception(
|
2025-02-05 11:39:09 +01:00
|
|
|
caplog: pytest.LogCaptureFixture,
|
2025-02-05 11:32:35 +01:00
|
|
|
exception: Exception,
|
|
|
|
message: str | None,
|
|
|
|
calling_logger: logging.Logger | None,
|
2025-02-05 11:39:09 +01:00
|
|
|
debug_value: bool,
|
2025-02-05 11:32:35 +01:00
|
|
|
expected_message: str,
|
|
|
|
) -> None:
|
2025-02-05 11:39:09 +01:00
|
|
|
"""Test anta_log_exception."""
|
2025-02-05 11:32:35 +01:00
|
|
|
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
|
2025-02-05 11:39:09 +01:00
|
|
|
except ValueError as exc:
|
|
|
|
with patch("anta.logger.__DEBUG__", new=debug_value):
|
|
|
|
anta_log_exception(exc, message=message, calling_logger=calling_logger)
|
2025-02-05 11:32:35 +01:00
|
|
|
|
|
|
|
# Two log captured
|
2025-02-05 11:39:09 +01:00
|
|
|
if debug_value:
|
2025-02-05 11:32:35 +01:00
|
|
|
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
|
2025-02-05 11:39:09 +01:00
|
|
|
if debug_value:
|
2025-02-05 11:32:35 +01:00
|
|
|
assert "Traceback" in caplog.text
|
2025-02-05 11:39:09 +01:00
|
|
|
|
|
|
|
|
|
|
|
def my_raising_function(exception: Exception) -> None:
|
|
|
|
"""Raise Exception."""
|
|
|
|
raise exception
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
("exception", "expected_output"),
|
|
|
|
[(ValueError("test"), "ValueError: test"), (ValueError(), "ValueError")],
|
|
|
|
)
|
|
|
|
def test_exc_to_str(exception: Exception, expected_output: str) -> None:
|
|
|
|
"""Test exc_to_str."""
|
|
|
|
assert exc_to_str(exception) == expected_output
|
|
|
|
|
|
|
|
|
|
|
|
def test_tb_to_str() -> None:
|
|
|
|
"""Test tb_to_str."""
|
|
|
|
try:
|
|
|
|
my_raising_function(ValueError("test"))
|
|
|
|
except ValueError as exc:
|
|
|
|
output = tb_to_str(exc)
|
|
|
|
assert "Traceback" in output
|
|
|
|
assert 'my_raising_function(ValueError("test"))' in output
|