Skip to content

Commit

Permalink
[RHELC-1393] Move logger initialization to initialize module (oamg#1095)
Browse files Browse the repository at this point in the history
* Move logger initialization to initialize module

Initializing the logger module earlier makes the tool work correctly
when trying to find the /etc/system-release file, and if it is not
there, throw a critical error to the user before anything else happens.

If we don't move the module to this point, the first /etc/system-releaes
check will silently fail and only the next call will throw the
exception, because at this point, we already have the logger
initialized.

To prevent further executions in the system, it is more reliable to
initialize the logger module earlier.

Signed-off-by: Rodolfo Olivieri <[email protected]>

* Revert initialize_file_logger function

Signed-off-by: Rodolfo Olivieri <[email protected]>

---------

Signed-off-by: Rodolfo Olivieri <[email protected]>
  • Loading branch information
r0x0d authored Feb 20, 2024
1 parent bbae477 commit 790db4b
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 53 deletions.
1 change: 0 additions & 1 deletion convert2rhel/backup/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
class RestorableFile(RestorableChange):
def __init__(self, filepath):
super(RestorableFile, self).__init__()

# The filepath we want to back up needs to start with at least a `/`,
# otherwise, let's error out and warn the developer/user that the
# filepath is not what we expect. This is mostly intended to be an
Expand Down
16 changes: 16 additions & 0 deletions convert2rhel/initialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
import os

from convert2rhel import i18n
from convert2rhel import logger as logger_module


loggerinst = logging.getLogger(__name__)


def disable_root_logger():
Expand All @@ -36,6 +40,15 @@ def disable_root_logger():
logging.getLogger().addHandler(logging.NullHandler())


def initialize_logger():
"""
Entrypoint function that aggregates other calls for initialization logic
and setup for logger handlers that do not require root.
"""

return logger_module.setup_logger_handler()


def set_locale():
"""
Set the C locale, also known as the POSIX locale, for the main process as well as the child processes.
Expand Down Expand Up @@ -77,6 +90,9 @@ def run():
# Initialize logging to stop duplicate messages.
disable_root_logger()

# initialize logging
initialize_logger()

from convert2rhel import main

return main.main()
13 changes: 0 additions & 13 deletions convert2rhel/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import logging
import os
import sys

from convert2rhel import actions, applock, backup, breadcrumbs, checks, exceptions, grub
from convert2rhel import logger as logger_module
Expand All @@ -44,15 +43,6 @@ class ConversionPhase:
POST_PONR_CHANGES = 4


def initialize_logger():
"""
Entrypoint function that aggregates other calls for initialization logic
and setup for logger handlers that do not require root.
"""

return logger_module.setup_logger_handler()


def initialize_file_logging(log_name, log_dir):
"""
Archive existing file logs and setup all logging handlers that require
Expand Down Expand Up @@ -87,9 +77,6 @@ def main():
the application lock, to do the conversion process.
"""

# initialize logging
initialize_logger()

# handle command line arguments
toolopts.CLI()

Expand Down
3 changes: 2 additions & 1 deletion convert2rhel/unit_tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
breadcrumbs,
exceptions,
grub,
initialize,
main,
pkghandler,
subscription,
Expand Down Expand Up @@ -355,7 +356,7 @@ class PrintDataCollectionMocked(MockFunctionObject):


class InitializeLoggerMocked(MockFunctionObject):
spec = main.initialize_logger
spec = initialize.initialize_logger


class InitializeFileLoggingMocked(MockFunctionObject):
Expand Down
25 changes: 22 additions & 3 deletions convert2rhel/unit_tests/initialize_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,16 @@

__metaclass__ = type

import os

import pytest
import six

from convert2rhel import applock, initialize
from convert2rhel import logger as logger_module
from convert2rhel import main

from convert2rhel import applock, initialize, main

six.add_move(six.MovedModule("mock", "mock", "unittest.mock"))
from six.moves import mock


@pytest.mark.parametrize(
Expand All @@ -32,6 +37,20 @@
),
)
def test_run(monkeypatch, exit_code, tmp_path):
monkeypatch.setattr(logger_module, "LOG_DIR", str(tmp_path))
monkeypatch.setattr(main, "main", value=lambda: exit_code)
monkeypatch.setattr(applock, "_DEFAULT_LOCK_DIR", str(tmp_path))
assert initialize.run() == exit_code


def test_initialize_logger(monkeypatch):
setup_logger_handler_mock = mock.Mock()

monkeypatch.setattr(
logger_module,
"setup_logger_handler",
value=setup_logger_handler_mock,
)

initialize.initialize_logger()
setup_logger_handler_mock.assert_called_once()
37 changes: 2 additions & 35 deletions convert2rhel/unit_tests/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
CollectEarlyDataMocked,
FinishCollectionMocked,
InitializeFileLoggingMocked,
InitializeLoggerMocked,
MainLockedMocked,
PrintDataCollectionMocked,
PrintSystemInformationMocked,
Expand Down Expand Up @@ -121,19 +120,6 @@ def test_backup_control_unknown_exception(self, monkeypatch, global_backup_contr
main.rollback_changes()


def test_initialize_logger(monkeypatch):
setup_logger_handler_mock = mock.Mock()

monkeypatch.setattr(
logger_module,
"setup_logger_handler",
value=setup_logger_handler_mock,
)

main.initialize_logger()
setup_logger_handler_mock.assert_called_once()


@pytest.mark.parametrize(("exception_type", "exception"), ((IOError, True), (OSError, True), (None, False)))
def test_initialize_file_logging(exception_type, exception, monkeypatch, caplog):
add_file_handler_mock = mock.Mock()
Expand Down Expand Up @@ -216,7 +202,6 @@ def test_help_exit(monkeypatch, tmp_path):
monkeypatch.setattr(applock, "_DEFAULT_LOCK_DIR", str(tmp_path))
monkeypatch.setattr(sys, "argv", ["convert2rhel", "--help"])
monkeypatch.setattr(utils, "require_root", RequireRootMocked())
monkeypatch.setattr(main, "initialize_logger", InitializeLoggerMocked())
monkeypatch.setattr(main, "initialize_file_logging", InitializeFileLoggingMocked())
monkeypatch.setattr(main, "main_locked", MainLockedMocked())

Expand All @@ -228,7 +213,6 @@ def test_help_exit(monkeypatch, tmp_path):

def test_main(monkeypatch, tmp_path):
require_root_mock = mock.Mock()
initialize_logger_mock = mock.Mock()
initialize_file_logging_mock = mock.Mock()
toolopts_cli_mock = mock.Mock()
show_eula_mock = mock.Mock()
Expand All @@ -255,7 +239,6 @@ def test_main(monkeypatch, tmp_path):

monkeypatch.setattr(applock, "_DEFAULT_LOCK_DIR", str(tmp_path))
monkeypatch.setattr(utils, "require_root", require_root_mock)
monkeypatch.setattr(main, "initialize_logger", initialize_logger_mock)
monkeypatch.setattr(main, "initialize_file_logging", initialize_file_logging_mock)
monkeypatch.setattr(toolopts, "CLI", toolopts_cli_mock)
monkeypatch.setattr(main, "show_eula", show_eula_mock)
Expand All @@ -282,7 +265,7 @@ def test_main(monkeypatch, tmp_path):

assert main.main() == 0
assert require_root_mock.call_count == 1
assert initialize_logger_mock.call_count == 1
assert initialize_file_logging_mock.call_count == 1
assert toolopts_cli_mock.call_count == 1
assert show_eula_mock.call_count == 1
assert print_data_collection_mock.call_count == 1
Expand All @@ -308,7 +291,6 @@ def test_main(monkeypatch, tmp_path):
class TestRollbackFromMain:
def test_main_rollback_post_cli_phase(self, monkeypatch, caplog, tmp_path):
require_root_mock = mock.Mock()
initialize_logger_mock = mock.Mock()
initialize_file_logging_mock = mock.Mock()
toolopts_cli_mock = mock.Mock()
show_eula_mock = mock.Mock(side_effect=Exception)
Expand All @@ -318,15 +300,14 @@ def test_main_rollback_post_cli_phase(self, monkeypatch, caplog, tmp_path):

monkeypatch.setattr(applock, "_DEFAULT_LOCK_DIR", str(tmp_path))
monkeypatch.setattr(utils, "require_root", require_root_mock)
monkeypatch.setattr(main, "initialize_logger", initialize_logger_mock)
monkeypatch.setattr(main, "initialize_file_logging", initialize_file_logging_mock)
monkeypatch.setattr(toolopts, "CLI", toolopts_cli_mock)
monkeypatch.setattr(main, "show_eula", show_eula_mock)
monkeypatch.setattr(breadcrumbs, "finish_collection", finish_collection_mock)

assert main.main() == 1
assert require_root_mock.call_count == 1
assert initialize_logger_mock.call_count == 1
assert initialize_file_logging_mock.call_count == 1
assert toolopts_cli_mock.call_count == 1
assert show_eula_mock.call_count == 1
assert finish_collection_mock.call_count == 1
Expand All @@ -335,7 +316,6 @@ def test_main_rollback_post_cli_phase(self, monkeypatch, caplog, tmp_path):
def test_main_traceback_in_clear_versionlock(self, caplog, monkeypatch, tmp_path):
monkeypatch.setattr(applock, "_DEFAULT_LOCK_DIR", str(tmp_path))
monkeypatch.setattr(utils, "require_root", RequireRootMocked())
monkeypatch.setattr(main, "initialize_logger", InitializeLoggerMocked())
monkeypatch.setattr(main, "initialize_file_logging", InitializeFileLoggingMocked())
monkeypatch.setattr(toolopts, "CLI", CLIMocked())
monkeypatch.setattr(main, "show_eula", ShowEulaMocked())
Expand All @@ -360,7 +340,6 @@ def test_main_traceback_in_clear_versionlock(self, caplog, monkeypatch, tmp_path

assert main.main() == 1
assert utils.require_root.call_count == 1
assert main.initialize_logger.call_count == 1
assert toolopts.CLI.call_count == 1
assert main.show_eula.call_count == 1
assert breadcrumbs.print_data_collection.call_count == 1
Expand All @@ -377,7 +356,6 @@ def test_main_traceback_in_clear_versionlock(self, caplog, monkeypatch, tmp_path

def test_main_traceback_before_action_completion(self, monkeypatch, caplog, tmp_path):
require_root_mock = mock.Mock()
initialize_logger_mock = mock.Mock()
initialize_file_logging_mock = mock.Mock()
toolopts_cli_mock = mock.Mock()
show_eula_mock = mock.Mock()
Expand All @@ -397,7 +375,6 @@ def test_main_traceback_before_action_completion(self, monkeypatch, caplog, tmp_

monkeypatch.setattr(applock, "_DEFAULT_LOCK_DIR", str(tmp_path))
monkeypatch.setattr(utils, "require_root", require_root_mock)
monkeypatch.setattr(main, "initialize_logger", initialize_logger_mock)
monkeypatch.setattr(main, "initialize_file_logging", initialize_file_logging_mock)
monkeypatch.setattr(toolopts, "CLI", toolopts_cli_mock)
monkeypatch.setattr(main, "show_eula", show_eula_mock)
Expand All @@ -414,7 +391,6 @@ def test_main_traceback_before_action_completion(self, monkeypatch, caplog, tmp_
monkeypatch.setattr(report, "summary_as_txt", summary_as_txt_mock)

assert main.main() == 1
assert initialize_logger_mock.call_count == 1
assert require_root_mock.call_count == 1
assert initialize_file_logging_mock.call_count == 1
assert toolopts_cli_mock.call_count == 1
Expand All @@ -437,7 +413,6 @@ def test_main_traceback_before_action_completion(self, monkeypatch, caplog, tmp_

def test_main_rollback_pre_ponr_changes_phase(self, monkeypatch, caplog, tmp_path):
require_root_mock = mock.Mock()
initialize_logger_mock = mock.Mock()
initialize_file_logging_mock = mock.Mock()
toolopts_cli_mock = mock.Mock()
show_eula_mock = mock.Mock()
Expand All @@ -459,7 +434,6 @@ def test_main_rollback_pre_ponr_changes_phase(self, monkeypatch, caplog, tmp_pat

monkeypatch.setattr(applock, "_DEFAULT_LOCK_DIR", str(tmp_path))
monkeypatch.setattr(utils, "require_root", require_root_mock)
monkeypatch.setattr(main, "initialize_logger", initialize_logger_mock)
monkeypatch.setattr(main, "initialize_file_logging", initialize_file_logging_mock)
monkeypatch.setattr(toolopts, "CLI", toolopts_cli_mock)
monkeypatch.setattr(main, "show_eula", show_eula_mock)
Expand All @@ -478,7 +452,6 @@ def test_main_rollback_pre_ponr_changes_phase(self, monkeypatch, caplog, tmp_pat
monkeypatch.setattr(report, "summary_as_txt", summary_as_txt_mock)

assert main.main() == 1
assert initialize_logger_mock.call_count == 1
assert require_root_mock.call_count == 1
assert initialize_file_logging_mock.call_count == 1
assert toolopts_cli_mock.call_count == 1
Expand All @@ -500,7 +473,6 @@ def test_main_rollback_pre_ponr_changes_phase(self, monkeypatch, caplog, tmp_pat

def test_main_rollback_analyze_exit_phase(self, global_tool_opts, monkeypatch, tmp_path):
require_root_mock = mock.Mock()
initialize_logger_mock = mock.Mock()
initialize_file_logging_mock = mock.Mock()
toolopts_cli_mock = mock.Mock()
show_eula_mock = mock.Mock()
Expand All @@ -521,7 +493,6 @@ def test_main_rollback_analyze_exit_phase(self, global_tool_opts, monkeypatch, t

monkeypatch.setattr(applock, "_DEFAULT_LOCK_DIR", str(tmp_path))
monkeypatch.setattr(utils, "require_root", require_root_mock)
monkeypatch.setattr(main, "initialize_logger", initialize_logger_mock)
monkeypatch.setattr(main, "initialize_file_logging", initialize_file_logging_mock)
monkeypatch.setattr(toolopts, "CLI", toolopts_cli_mock)
monkeypatch.setattr(main, "show_eula", show_eula_mock)
Expand All @@ -542,7 +513,6 @@ def test_main_rollback_analyze_exit_phase(self, global_tool_opts, monkeypatch, t

assert main.main() == 0
assert require_root_mock.call_count == 1
assert initialize_logger_mock.call_count == 1
assert initialize_file_logging_mock.call_count == 1
assert toolopts_cli_mock.call_count == 1
assert show_eula_mock.call_count == 1
Expand All @@ -560,7 +530,6 @@ def test_main_rollback_analyze_exit_phase(self, global_tool_opts, monkeypatch, t

def test_main_rollback_post_ponr_changes_phase(self, monkeypatch, caplog, tmp_path):
require_root_mock = mock.Mock()
initialize_logger_mock = mock.Mock()
initialize_file_logging_mock = mock.Mock()
toolopts_cli_mock = mock.Mock()
show_eula_mock = mock.Mock()
Expand All @@ -584,7 +553,6 @@ def test_main_rollback_post_ponr_changes_phase(self, monkeypatch, caplog, tmp_pa

monkeypatch.setattr(applock, "_DEFAULT_LOCK_DIR", str(tmp_path))
monkeypatch.setattr(utils, "require_root", require_root_mock)
monkeypatch.setattr(main, "initialize_logger", initialize_logger_mock)
monkeypatch.setattr(main, "initialize_file_logging", initialize_file_logging_mock)
monkeypatch.setattr(toolopts, "CLI", toolopts_cli_mock)
monkeypatch.setattr(main, "show_eula", show_eula_mock)
Expand All @@ -606,7 +574,6 @@ def test_main_rollback_post_ponr_changes_phase(self, monkeypatch, caplog, tmp_pa

assert main.main() == 1
assert require_root_mock.call_count == 1
assert initialize_logger_mock.call_count == 1
assert initialize_file_logging_mock.call_count == 1
assert toolopts_cli_mock.call_count == 1
assert show_eula_mock.call_count == 1
Expand Down

0 comments on commit 790db4b

Please sign in to comment.