Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RHELC-1396] fix: errors in utils.py after full test suite #1134

Merged
merged 1 commit into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading