Skip to content

Commit

Permalink
fix: errors in utils.py after full test suite
Browse files Browse the repository at this point in the history
Since of merging #1029 we get an issue with unit tests that causes a lot
of strange errors to occur. Likely due to a stream not being closed
correctly.

This aims to either fix or mitigate this from occuring during test runs
as it should not appear in convert2rhel by itself.
  • Loading branch information
Venefilyn committed Mar 6, 2024
1 parent f992a1d commit 6e94925
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
9 changes: 4 additions & 5 deletions convert2rhel/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,6 @@ class LogfileBufferHandler(BufferingHandler):
which will keep a buffer of the logs and flush it to the FileHandler
"""

name = "logfile_buffer_handler"

def __init__(self, capacity, handler_name="file_handler"):
"""
Initialize the handler with the buffer size.
Expand All @@ -86,6 +84,7 @@ def __init__(self, capacity, handler_name="file_handler"):
super(LogfileBufferHandler, self).__init__(capacity)
# the FileLogger handler that we are logging to
self._handler_name = handler_name
self.set_name("logfile_buffer_handler")

@property
def target(self):
Expand All @@ -97,7 +96,7 @@ def target(self):
:return logging.Handler: Either the found FileHandler setup or temporary NullHandler
"""
for handler in logger.handlers:
if handler.name == self._handler_name:
if hasattr(handler, "name") and handler.name == self._handler_name:
return handler
return logging.NullHandler()

Expand Down Expand Up @@ -175,8 +174,8 @@ def add_file_handler(log_name, log_dir):
# We now have a FileHandler added, but we still need the logs from before
# this point. Luckily we have the memory buffer that we can flush logs from
for handler in logger.handlers:
if handler.name == "logfile_buffer_handler":
handler.flush()
if hasattr(handler, "name") and handler.name == "logfile_buffer_handler":
handler.close()
# after we've flushed to the file we don't need the handler anymore
logger.removeHandler(handler)
break
Expand Down
8 changes: 6 additions & 2 deletions convert2rhel/unit_tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
__metaclass__ = type

import logging
import os
import sys

Expand Down Expand Up @@ -73,12 +74,15 @@ def pkg_root():


@pytest.fixture(autouse=True)
def setup_logger(tmpdir, request):
def setup_logger(request):
# This makes it so we can skip this using @pytest.mark.noautofixtures
if "noautofixtures" in request.keywords:
return
setup_logger_handler()
add_file_handler(log_name="convert2rhel", log_dir=str(tmpdir))
# get root logger
logger = logging.getLogger("convert2rhel")
for handler in logger.handlers:
logger.removeHandler(handler)


@pytest.fixture
Expand Down
13 changes: 7 additions & 6 deletions convert2rhel/unit_tests/logger_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@
from convert2rhel import logger as logger_module


@pytest.mark.noautofixtures
def test_logger_handlers(monkeypatch, tmpdir, read_std, global_tool_opts):
"""Test if the logger handlers emits the events to the file and stdout."""
monkeypatch.setattr("convert2rhel.toolopts.tool_opts", global_tool_opts)

# initializing the logger first
log_fname = "convert2rhel.log"
log_fname = "customlogfile.log"
global_tool_opts.debug = True # debug entries > stdout if True
logger_module.setup_logger_handler()
logger_module.add_file_handler(log_name=log_fname, log_dir=str(tmpdir))
Expand All @@ -41,16 +42,16 @@ def test_logger_handlers(monkeypatch, tmpdir, read_std, global_tool_opts):
logger.info("Test info: %s", "data")
logger.debug("Test debug: %s", "other data")

# Test if logs were emmited to the file
with open(str(tmpdir.join(log_fname))) as log_f:
assert "Test info: data" in log_f.readline().rstrip()
assert "Test debug: other data" in log_f.readline().rstrip()

# Test if logs were emmited to the stdout
stdouterr_out, stdouterr_err = read_std()
assert "Test info: data" in stdouterr_out
assert "Test debug: other data" in stdouterr_out

# Test if logs were emmited to the file
with open(str(tmpdir.join(log_fname))) as log_f:
assert "Test info: data" in log_f.readline().rstrip()
assert "Test debug: other data" in log_f.readline().rstrip()


def test_tools_opts_debug(monkeypatch, read_std, is_py2, global_tool_opts):
monkeypatch.setattr("convert2rhel.toolopts.tool_opts", global_tool_opts)
Expand Down

0 comments on commit 6e94925

Please sign in to comment.