Skip to content

Commit

Permalink
Fix unit_test
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Diblik <[email protected]>
  • Loading branch information
danmyway committed Oct 30, 2024
1 parent f742d70 commit 8b69060
Showing 1 changed file with 9 additions and 19 deletions.
28 changes: 9 additions & 19 deletions convert2rhel/unit_tests/checks_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>.
__metaclass__ = type

import os

import pytest

from convert2rhel import checks
Expand Down Expand Up @@ -46,24 +48,12 @@ def testis_initramfs_file_valid(latest_installed_kernel, subprocess_output, expe
assert "Output of lsinitrd: {}".format(subprocess_output[0]) in caplog.records[-1].message


@pytest.mark.parametrize(
("latest_installed_kernel", "subprocess_output", "expected", "file_content"),
(("6.1.7-200.fc37.x86_64", ("can't decode byte", 99), False, b"\xF8\x88\x80\x80\x80"),),
)
def test_is_initramfs_file_valid_unicodedecodeerror(
latest_installed_kernel, subprocess_output, expected, file_content, tmpdir, caplog, monkeypatch
):
initramfs_file = tmpdir.mkdir("/boot").join("initramfs-%s.img")
initramfs_file = str(initramfs_file)
initramfs_file = initramfs_file % latest_installed_kernel
with open(initramfs_file, mode="wb") as f:
f.write(file_content)
def test_is_initramfs_file_valid_unicodedecodeerror(monkeypatch):
def mock_run(*args, **kwargs):
raise UnicodeDecodeError("utf-8", b"", 0, 1, "can't decode bytes")

monkeypatch.setattr(checks, "INITRAMFS_FILEPATH", initramfs_file)
monkeypatch.setattr(checks, "run_subprocess", RunSubprocessMocked(return_value=subprocess_output))
result = checks.is_initramfs_file_valid(initramfs_file)
assert result == expected
monkeypatch.setattr(checks, "run_subprocess", mock_run)
monkeypatch.setattr(os.path, "exists", lambda x: True)
result = checks.is_initramfs_file_valid("mock_file")

if not expected:
assert "Couldn't verify initramfs file. It may be corrupted." in caplog.records[-2].message
assert "Output of lsinitrd: %s" % subprocess_output[0] in caplog.records[-1].message
assert result is False

0 comments on commit 8b69060

Please sign in to comment.