c5466a4171
adapt to new naming of parsing methods, remove unused paths in parser module, all module import logwatcher.config instead of importing logwatcher, rename test functions
110 lines
3.7 KiB
Python
110 lines
3.7 KiB
Python
"""
|
|
this module contains all tests logging-related :
|
|
|
|
- the logger should have at least one StreamHandler and one FileHandler each time
|
|
logwatcher is launched.
|
|
- the StreamHandler must watch at an error level
|
|
- the FileHandler must watch at a debug
|
|
level
|
|
-
|
|
"""
|
|
|
|
import logging
|
|
import re
|
|
from logging import DEBUG, ERROR, INFO, WARNING, FileHandler, StreamHandler, getLogger
|
|
|
|
import pytest
|
|
|
|
from logwatcher.config import DATETIME_FORMAT
|
|
from logwatcher.logging_config import _setup_formatter, _setup_handler, setup_logging
|
|
|
|
|
|
def test_setup_handler(tmp_log_file):
|
|
formatter = _setup_formatter()
|
|
handler = _setup_handler(formatter=formatter, level=DEBUG)
|
|
assert isinstance(handler, StreamHandler)
|
|
|
|
handler = _setup_handler(formatter=formatter, level=INFO, filename=tmp_log_file)
|
|
assert isinstance(handler, FileHandler)
|
|
|
|
|
|
def test_count_of_handler(tmp_log_file):
|
|
logger = getLogger()
|
|
count_file_handler = 0
|
|
count_stream_handler = 0
|
|
|
|
for handler in logger.handlers:
|
|
if isinstance(handler, FileHandler):
|
|
count_file_handler += 1
|
|
elif isinstance(handler, StreamHandler):
|
|
count_stream_handler += 1
|
|
else:
|
|
pass
|
|
|
|
assert count_file_handler >= 1
|
|
assert count_stream_handler >= 1
|
|
|
|
|
|
def test_type_of_handlers():
|
|
logger = logging.getLogger("logwatcher")
|
|
for handler in logger.handlers:
|
|
if isinstance(handler, FileHandler):
|
|
assert handler.level == DEBUG
|
|
elif isinstance(handler, StreamHandler):
|
|
assert handler.level == ERROR
|
|
else:
|
|
pytest.fail("handlers must be either a file or a stream handler")
|
|
assert handler.formatter._fmt == "[%(asctime)s] - %(levelname)s: %(message)s"
|
|
assert handler.formatter.datefmt == DATETIME_FORMAT
|
|
|
|
|
|
def test_modified_format():
|
|
"""
|
|
when starting, the logger is first intialized, thus two handlers are at position
|
|
1 and 2 in logger.handlers list.
|
|
"""
|
|
logger = setup_logging()
|
|
for handler in logger.handlers[2:]:
|
|
assert handler.formatter._fmt == "[%(asctime)s] - %(levelname)s: %(message)s"
|
|
|
|
formatter_different_format = _setup_formatter(
|
|
format="%(asctime)s - %(levelname)s: %(message)s"
|
|
)
|
|
logger = setup_logging(formatter=formatter_different_format)
|
|
for handler in logger.handlers[4:]:
|
|
assert handler.formatter._fmt == "%(asctime)s - %(levelname)s: %(message)s"
|
|
|
|
formatter_different_datefmt = _setup_formatter(datefmt="%a %b %Y at %Hh%M")
|
|
logger = setup_logging(formatter=formatter_different_datefmt)
|
|
for handler in logger.handlers[6:]:
|
|
assert handler.formatter._fmt == "[%(asctime)s] - %(levelname)s: %(message)s"
|
|
assert handler.formatter.datefmt == "%a %b %Y at %Hh%M"
|
|
|
|
|
|
def test_logging_in_correct_path(tmp_log_file):
|
|
logger = logging.getLogger("logwatcher")
|
|
# must be both in file and stdout
|
|
logger.critical("hi- BYE")
|
|
logger.error("hi... ok, bye")
|
|
logger.warning("hi, are you alright?")
|
|
|
|
# only in file
|
|
logger.info("hi there")
|
|
logger.debug("hi, how are you? it's been a while!")
|
|
|
|
with open(tmp_log_file) as log_file:
|
|
content = log_file.readlines()
|
|
regex = r"\[\d{2}/\d{2}/\d{4} \d{2}:\d{2}:\d{2}\] - "
|
|
|
|
assert len(content) == 5
|
|
for line in content:
|
|
assert re.search(regex, line)
|
|
|
|
# test critical to warning
|
|
assert re.search(r"CRITICAL: hi- BYE", content[0])
|
|
assert re.search(r"ERROR: hi... ok, bye", content[1])
|
|
assert re.search(r"WARNING: hi, are you alright?", content[2])
|
|
assert re.search(r"INFO: hi there", content[3])
|
|
assert logger.getEffectiveLevel() < WARNING
|
|
assert re.search(r"DEBUG: hi, how are you\? it's been a while!", content[4])
|