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

Replace some prints by log messages #70

Merged
merged 5 commits into from
Apr 16, 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
221 changes: 179 additions & 42 deletions scripts/c2r_script.py

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions tests/test_archive_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,12 @@ def test_archive_old_report(create_json_report_mock, tmpdir):
archive_analysis_report(create_json_report_mock)

assert len(os.listdir(tmp_archive_dir)) == 1


@patch("scripts.c2r_script.os.path.exists", return_value=True)
@patch("scripts.c2r_script.shutil.move")
def test_archive_old_report_no_dir(mock_dir_exists, mock_move, create_json_report_mock):
archive_analysis_report(create_json_report_mock)

mock_dir_exists.assert_called_once()
mock_move.assert_called_once()
2 changes: 1 addition & 1 deletion tests/test_cleanup.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def test_cleanup_with_file_to_keep(mock_yum_undo):


@patch("scripts.c2r_script.YUM_TRANSACTIONS_TO_UNDO", new=set([1]))
@patch("scripts.c2r_script.run_subprocess", return_value=("", 1))
@patch("scripts.c2r_script.run_subprocess", return_value=("", 0))
def test_cleanup_with_undo_yum(mock_yum_undo):
"""Only downloaded files are removed."""

Expand Down
10 changes: 10 additions & 0 deletions tests/test_convert2rhel_inhibitors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from mock import patch
from scripts.c2r_script import check_convert2rhel_inhibitors_before_run


@patch("scripts.c2r_script.os.path.exists", return_value=False)
@patch("scripts.c2r_script._check_ini_file_modified", return_value=False)
def test_no_inhibitors(mock_ini_file_modified, mock_custom_ini):
check_convert2rhel_inhibitors_before_run()
mock_ini_file_modified.assert_called_once()
mock_custom_ini.assert_called_once()
8 changes: 8 additions & 0 deletions tests/test_get_system_distro_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ def test_get_system_distro_version_existing_file_centos():
assert version_id == "7.9"


@patch("__builtin__.open", mock_open(read_data="foo 7.9.0 (Core)\n"))
def test_get_version_no_matching_system_distro():
distribution_id, version_id = get_system_distro_version()

assert distribution_id is None
assert version_id == "7.9"


@patch(
"__builtin__.open",
mock_open(read_data="Red Hat Enterprise Linux Server release 7.9 (Maipo)\n"),
Expand Down
87 changes: 87 additions & 0 deletions tests/test_logging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import os
import logging
from mock import patch
import pytest
import scripts

from scripts.c2r_script import (
setup_sos_report,
setup_logger_handler,
archive_old_logger_files,
)


def test_setup_sos_report(monkeypatch, tmpdir):
sos_report_folder = str(tmpdir)
monkeypatch.setattr(scripts.c2r_script, "SOS_REPORT_FOLDER", sos_report_folder)

setup_sos_report()

sos_report_file = os.path.join(
sos_report_folder, "convert2rhel-worker-scripts-analysis-logs"
)
assert os.path.exists(sos_report_folder)
assert os.path.exists(sos_report_file)

with open(sos_report_file) as handler:
assert (
":/var/log/convert2rhel-worker-scripts/convert2rhel-worker-script-analysis.log"
== handler.read().strip()
)


@patch("scripts.c2r_script.os.makedirs")
@patch("scripts.c2r_script.os.path.exists", side_effect=[False, True])
def test_setup_sos_report_no_sos_report_folder(
patch_exists, patch_makedirs, monkeypatch, tmpdir
):
sos_report_folder = str(tmpdir)
monkeypatch.setattr(scripts.c2r_script, "SOS_REPORT_FOLDER", sos_report_folder)

setup_sos_report()

# Folder created
assert patch_exists.call_count == 2
patch_makedirs.assert_called_once_with(sos_report_folder)


@pytest.mark.noautofixtures
def test_setup_logger_handler(monkeypatch, tmpdir):
log_dir = str(tmpdir)
monkeypatch.setattr(scripts.c2r_script, "LOG_DIR", log_dir)
monkeypatch.setattr(scripts.c2r_script, "LOG_FILENAME", "filelog.log")

logger = logging.getLogger(__name__)
setup_logger_handler()

# emitting some log entries
logger.info("Test info: %s", "data")
logger.debug("Test debug: %s", "other data")

assert os.path.exists(log_dir)


def test_archive_old_logger_files(monkeypatch, tmpdir):
log_dir = str(tmpdir)
archive_dir = os.path.join(log_dir, "archive")
monkeypatch.setattr(scripts.c2r_script, "LOG_DIR", log_dir)
monkeypatch.setattr(scripts.c2r_script, "LOG_FILENAME", "test.log")

original_log_file = tmpdir.join("test.log")
original_log_file.write("test")

archive_old_logger_files()

assert os.path.exists(log_dir)
assert os.path.exists(archive_dir)
assert len(os.listdir(archive_dir)) == 1


def test_archive_old_logger_files_no_log_file(monkeypatch, tmpdir):
log_dir = str(tmpdir.join("something-else"))
monkeypatch.setattr(scripts.c2r_script, "LOG_DIR", log_dir)
monkeypatch.setattr(scripts.c2r_script, "LOG_FILENAME", "test.log")

archive_old_logger_files()

assert not os.path.exists(log_dir)
87 changes: 82 additions & 5 deletions tests/test_main_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,15 @@
@patch("scripts.c2r_script.archive_analysis_report", side_effect=Mock())
@patch("scripts.c2r_script.check_for_inhibitors_in_rollback", return_value="")
@patch("scripts.c2r_script.update_insights_inventory", side_effect=Mock())
@patch("scripts.c2r_script.setup_sos_report", side_effect=Mock())
@patch("scripts.c2r_script.archive_old_logger_files", side_effect=Mock())
@patch("scripts.c2r_script.setup_logger_handler", side_effect=Mock())
# fmt: on
# pylint: disable=too-many-locals
def test_main_success_c2r_installed(
mock_setup_logger_handler,
mock_setup_sos_report,
mock_archive_old_logger_files,
mock_update_insights_inventory,
mock_rollback_inhibitor_check,
mock_archive_analysis_report,
Expand All @@ -39,13 +45,18 @@ def test_main_success_c2r_installed(
mock_install_or_update_convert2rhel,
mock_gather_json_report,
capsys, # to check for rollback info in stdout
caplog,
):
main()

output = capsys.readouterr().out
assert "rollback" not in output
assert "Convert2RHEL Analysis script finished successfully!" in output
assert "rollback" not in caplog.text
assert "Convert2RHEL Analysis script finished successfully!" in caplog.text
assert '"alert": false' in output

assert mock_setup_logger_handler.call_count == 1
assert mock_setup_sos_report.call_count == 1
assert mock_archive_old_logger_files.call_count == 1
assert mock_rollback_inhibitor_check.call_count == 1
assert mock_update_insights_inventory.call_count == 0
assert mock_install_or_update_convert2rhel.call_count == 1
Expand Down Expand Up @@ -78,9 +89,15 @@ def test_main_success_c2r_installed(
@patch("scripts.c2r_script.archive_analysis_report", side_effect=Mock())
@patch("scripts.c2r_script.check_for_inhibitors_in_rollback", return_value="")
@patch("scripts.c2r_script.update_insights_inventory", side_effect=Mock())
@patch("scripts.c2r_script.setup_sos_report", side_effect=Mock())
@patch("scripts.c2r_script.archive_old_logger_files", side_effect=Mock())
@patch("scripts.c2r_script.setup_logger_handler", side_effect=Mock())
# fmt: on
# pylint: disable=too-many-locals
def test_main_success_c2r_updated(
mock_setup_logger_handler,
mock_setup_sos_report,
mock_archive_old_logger_files,
mock_update_insights_inventory,
mock_rollback_inhibitor_check,
mock_archive_analysis_report,
Expand All @@ -95,15 +112,19 @@ def test_main_success_c2r_updated(
mock_install_or_update_convert2rhel,
mock_gather_json_report,
capsys, # to check for rollback info in stdout
caplog,
):
main()

output = capsys.readouterr().out
assert "rollback" not in output
assert "Convert2RHEL Analysis script finished successfully!" in output
assert "rollback" not in caplog.text
assert "Convert2RHEL Analysis script finished successfully!" in caplog.text
assert '"alert": false' in output
assert mock_rollback_inhibitor_check.call_count == 1

assert mock_rollback_inhibitor_check.call_count == 1
assert mock_setup_logger_handler.call_count == 1
assert mock_setup_sos_report.call_count == 1
assert mock_archive_old_logger_files.call_count == 1
assert mock_update_insights_inventory.call_count == 0
assert mock_install_or_update_convert2rhel.call_count == 1
assert mock_inhibitor_check.call_count == 1
Expand Down Expand Up @@ -134,8 +155,15 @@ def test_main_success_c2r_updated(
@patch("scripts.c2r_script.is_eligible_releases", return_value=True)
@patch("scripts.c2r_script.archive_analysis_report", side_effect=Mock())
@patch("scripts.c2r_script.update_insights_inventory", side_effect=Mock())
@patch("scripts.c2r_script.setup_sos_report", side_effect=Mock())
@patch("scripts.c2r_script.archive_old_logger_files", side_effect=Mock())
@patch("scripts.c2r_script.setup_logger_handler", side_effect=Mock())
# fmt: on
# pylint: disable=too-many-locals
def test_main_process_error(
mock_setup_logger_handler,
mock_setup_sos_report,
mock_archive_old_logger_files,
mock_update_insights_inventory,
mock_archive_analysis_report,
mock_is_eligible_releases,
Expand All @@ -157,6 +185,9 @@ def test_main_process_error(
assert "Process error" in output
assert '"alert": true' in output

assert mock_setup_logger_handler.call_count == 1
assert mock_setup_sos_report.call_count == 1
assert mock_archive_old_logger_files.call_count == 1
assert mock_update_insights_inventory.call_count == 0
assert mock_install_or_update_convert2rhel.call_count == 1
assert mock_inhibitor_check.call_count == 1
Expand Down Expand Up @@ -185,8 +216,14 @@ def test_main_process_error(
@patch("scripts.c2r_script.is_eligible_releases", return_value=True)
@patch("scripts.c2r_script.archive_analysis_report", side_effect=Mock())
@patch("scripts.c2r_script.update_insights_inventory", side_effect=Mock())
@patch("scripts.c2r_script.setup_sos_report", side_effect=Mock())
@patch("scripts.c2r_script.archive_old_logger_files", side_effect=Mock())
@patch("scripts.c2r_script.setup_logger_handler", side_effect=Mock())
# fmt: on
def test_main_general_exception(
mock_setup_logger_handler,
mock_setup_sos_report,
mock_archive_old_logger_files,
mock_update_insights_inventory,
mock_archive_analysis_report,
mock_is_eligible_releases,
Expand All @@ -206,6 +243,9 @@ def test_main_general_exception(
assert "'Mock' object is not iterable" in output
assert '"alert": true' in output

assert mock_setup_logger_handler.call_count == 1
assert mock_setup_sos_report.call_count == 1
assert mock_archive_old_logger_files.call_count == 1
assert mock_update_insights_inventory.call_count == 0
assert mock_install_or_update_convert2rhel.call_count == 1
assert mock_inhibitor_check.call_count == 1
Expand Down Expand Up @@ -233,8 +273,15 @@ def test_main_general_exception(
@patch("scripts.c2r_script.is_eligible_releases", return_value=True)
@patch("scripts.c2r_script.archive_analysis_report", side_effect=Mock())
@patch("scripts.c2r_script.update_insights_inventory", side_effect=Mock())
@patch("scripts.c2r_script.setup_sos_report", side_effect=Mock())
@patch("scripts.c2r_script.archive_old_logger_files", side_effect=Mock())
@patch("scripts.c2r_script.setup_logger_handler", side_effect=Mock())
# fmt: on
# pylint: disable=too-many-locals
def test_main_inhibited_ini_modified(
mock_setup_logger_handler,
mock_setup_sos_report,
mock_archive_old_logger_files,
mock_update_insights_inventory,
mock_archive_analysis_report,
mock_is_eligible_releases,
Expand All @@ -254,6 +301,9 @@ def test_main_inhibited_ini_modified(
assert "/etc/convert2rhel.ini was modified" in output
assert '"alert": true' in output

assert mock_setup_logger_handler.call_count == 1
assert mock_setup_sos_report.call_count == 1
assert mock_archive_old_logger_files.call_count == 1
assert mock_update_insights_inventory.call_count == 0
assert mock_archive_analysis_report.call_count == 0
assert mock_get_system_distro_version.call_count == 1
Expand Down Expand Up @@ -282,8 +332,14 @@ def test_main_inhibited_ini_modified(
@patch("scripts.c2r_script.is_eligible_releases", return_value=True)
@patch("scripts.c2r_script.archive_analysis_report", side_effect=Mock())
@patch("scripts.c2r_script.update_insights_inventory", side_effect=Mock())
@patch("scripts.c2r_script.setup_sos_report", side_effect=Mock())
@patch("scripts.c2r_script.archive_old_logger_files", side_effect=Mock())
@patch("scripts.c2r_script.setup_logger_handler", side_effect=Mock())
# fmt: on
def test_main_inhibited_custom_ini(
mock_setup_logger_handler,
mock_setup_sos_report,
mock_archive_old_logger_files,
mock_update_insights_inventory,
mock_archive_analysis_report,
mock_is_eligible_releases,
Expand All @@ -302,6 +358,9 @@ def test_main_inhibited_custom_ini(
assert ".convert2rhel.ini was found" in output
assert '"alert": true' in output

assert mock_setup_logger_handler.call_count == 1
assert mock_setup_sos_report.call_count == 1
assert mock_archive_old_logger_files.call_count == 1
assert mock_update_insights_inventory.call_count == 0
assert mock_archive_analysis_report.call_count == 2
assert mock_get_system_distro_version.call_count == 1
Expand Down Expand Up @@ -330,9 +389,15 @@ def test_main_inhibited_custom_ini(
@patch("scripts.c2r_script.archive_analysis_report", side_effect=Mock())
@patch("scripts.c2r_script.check_for_inhibitors_in_rollback", return_value="")
@patch("scripts.c2r_script.update_insights_inventory", side_effect=Mock())
@patch("scripts.c2r_script.setup_sos_report", side_effect=Mock())
@patch("scripts.c2r_script.archive_old_logger_files", side_effect=Mock())
@patch("scripts.c2r_script.setup_logger_handler", side_effect=Mock())
# fmt: on
# pylint: disable=too-many-locals
def test_main_inhibited_c2r_installed_no_rollback_err(
mock_setup_logger_handler,
mock_setup_sos_report,
mock_archive_old_logger_files,
mock_update_insights_inventory,
mock_rollback_inhibitor_check,
mock_archive_analysis_report,
Expand Down Expand Up @@ -365,6 +430,9 @@ def test_main_inhibited_c2r_installed_no_rollback_err(
assert mock_get_system_distro_version.call_count == 1
assert mock_is_eligible_releases.call_count == 1
assert mock_archive_analysis_report.call_count == 0
assert mock_setup_logger_handler.call_count == 1
assert mock_setup_sos_report.call_count == 1
assert mock_archive_old_logger_files.call_count == 1


# fmt: off
Expand All @@ -389,9 +457,15 @@ def test_main_inhibited_c2r_installed_no_rollback_err(
@patch("scripts.c2r_script.archive_analysis_report", side_effect=Mock())
@patch("scripts.c2r_script.check_for_inhibitors_in_rollback", return_value="rollback error")
@patch("scripts.c2r_script.update_insights_inventory", side_effect=Mock())
@patch("scripts.c2r_script.setup_sos_report", side_effect=Mock())
@patch("scripts.c2r_script.archive_old_logger_files", side_effect=Mock())
@patch("scripts.c2r_script.setup_logger_handler", side_effect=Mock())
# fmt: on
# pylint: disable=too-many-locals
def test_main_inhibited_c2r_installed_rollback_errors(
mock_setup_logger_handler,
mock_setup_sos_report,
mock_archive_old_logger_files,
mock_update_insights_inventory,
mock_rollback_inhibitor_check,
mock_archive_analysis_report,
Expand Down Expand Up @@ -419,6 +493,9 @@ def test_main_inhibited_c2r_installed_rollback_errors(
assert "A rollback of changes performed by convert2rhel failed" in output
assert '"alert": true' in output

assert mock_setup_logger_handler.call_count == 1
assert mock_setup_sos_report.call_count == 1
assert mock_archive_old_logger_files.call_count == 1
assert mock_update_insights_inventory.call_count == 0
assert mock_install_or_update_convert2rhel.call_count == 1
assert mock_inhibitor_check.call_count == 1
Expand Down
Loading
Loading