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
Changes from 1 commit
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
Prev Previous commit
Update test coverage apart from new logging functions
Signed-off-by: Andrea Waltlova <[email protected]>
andywaltlova committed Apr 16, 2024
commit 0b411e1a74d3a5c82cdbc5a37ab12fc8c93d3f6d
2 changes: 1 addition & 1 deletion scripts/c2r_script.py
Original file line number Diff line number Diff line change
@@ -379,7 +379,7 @@ def get_system_distro_version():
version_id = None
with open("/etc/system-release", "r") as system_release_file:
data = system_release_file.readline()
match = re.search(r"(.+?)\s?(?:release\s?)?\d", data)
match = re.search(r"(.+?)\s?(?:release\s?)", data)
if match:
# Split and get the first position, which will contain the system
# name.
9 changes: 9 additions & 0 deletions tests/test_archive_report.py
Original file line number Diff line number Diff line change
@@ -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
@@ -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."""

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
@@ -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"),
16 changes: 16 additions & 0 deletions tests/test_logging.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import logging
from mock import patch
import pytest
import scripts

@@ -29,6 +30,21 @@ def test_setup_sos_report(monkeypatch, tmpdir):
)


@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)
12 changes: 10 additions & 2 deletions tests/test_required_file.py
Original file line number Diff line number Diff line change
@@ -28,14 +28,22 @@ def test_create_host(required_file_instance):

def test_create_data(required_file_instance):
with patch("scripts.c2r_script.urlopen") as mock_urlopen, patch(
"scripts.c2r_script.os.path.dirname", return_value="/test"
) as mock_dirname, patch(
"scripts.c2r_script.os.path.exists", return_value=True
) as mock_exists, patch(
"scripts.c2r_script.os.makedirs"
) as mock_makedirs, patch("scripts.c2r_script.open") as mock_open, patch(
) as mock_makedirs, patch(
"scripts.c2r_script.open"
) as mock_open, patch(
"scripts.c2r_script.os.chmod"
) as mock_chmod:
required_file_instance.create_from_data(b"Mocked data")

mock_urlopen.assert_not_called()
mock_makedirs.assert_called_once_with("/test", mode=0o755)
mock_dirname.assert_called_once()
mock_exists.assert_called_once()
mock_makedirs.assert_not_called()
mock_open.assert_called_once_with("/test/path", mode="w")
mock_chmod.assert_called_once_with("/test/path", 0o644)