mirror of
https://github.com/thegeeklab/prometheus-pve-sd.git
synced 2024-11-01 08:00:38 +00:00
a2f513156a
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: Robert Kaussow <mail@thegeeklab.de>
61 lines
1.3 KiB
Python
61 lines
1.3 KiB
Python
"""Pytest conftest fixtures."""
|
|
|
|
import logging
|
|
import os
|
|
import sys
|
|
from contextlib import contextmanager
|
|
|
|
import pytest
|
|
from _pytest.logging import LogCaptureHandler
|
|
|
|
from prometheuspvesd.utils import Singleton
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def reset_singletons():
|
|
Singleton._instances = {}
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def reset_os_environment():
|
|
os.environ.clear()
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def reset_sys_argv():
|
|
sys.argv = ["prometheus-pve-sd"]
|
|
|
|
|
|
@contextmanager
|
|
def local_caplog_fn(level=logging.INFO, name="prometheuspvesd"):
|
|
"""
|
|
Context manager that captures records from non-propagating loggers.
|
|
|
|
After the end of the 'with' statement, the log level is restored to its original
|
|
value. Code adapted from https://github.com/pytest-dev/pytest/issues/3697#issuecomment-790925527.
|
|
|
|
:param int level: The level.
|
|
:param logging.Logger logger: The logger to update.
|
|
"""
|
|
|
|
logger = logging.getLogger(name)
|
|
|
|
old_level = logger.level
|
|
logger.setLevel(level)
|
|
|
|
handler = LogCaptureHandler()
|
|
logger.addHandler(handler)
|
|
|
|
try:
|
|
yield handler
|
|
finally:
|
|
logger.setLevel(old_level)
|
|
logger.removeHandler(handler)
|
|
|
|
|
|
@pytest.fixture
|
|
def local_caplog():
|
|
"""Fixture that yields a context manager for capturing records from non-propagating loggers."""
|
|
|
|
yield local_caplog_fn
|