Skip to content

Commit

Permalink
[RHELC-1711] Except UnicodeDecodeError when parsing initramfs (oamg#1359
Browse files Browse the repository at this point in the history
)

* Except UnicodeDecodeError when parsing initramfs

Signed-off-by: Daniel Diblik <[email protected]>

* Fix unit_test

Signed-off-by: Daniel Diblik <[email protected]>

* Enable the verification test

Signed-off-by: Daniel Diblik <[email protected]>

---------

Signed-off-by: Daniel Diblik <[email protected]>
  • Loading branch information
danmyway authored Oct 31, 2024
1 parent 2c31ae5 commit 7be034f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
12 changes: 8 additions & 4 deletions convert2rhel/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,14 @@ def is_initramfs_file_valid(filepath):
return False

logger.debug("Checking if the '%s' file is not corrupted.", filepath)
out, return_code = run_subprocess(
cmd=["/usr/bin/lsinitrd", filepath],
print_output=False,
)
try:
out, return_code = run_subprocess(
cmd=["/usr/bin/lsinitrd", filepath],
print_output=False,
)
except UnicodeDecodeError as e:
out = e
return_code = 99 # Having this a high number to not mess with the actual process return codes.

if return_code != 0:
logger.info("Couldn't verify initramfs file. It may be corrupted.")
Expand Down
13 changes: 13 additions & 0 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 @@ -44,3 +46,14 @@ def testis_initramfs_file_valid(latest_installed_kernel, subprocess_output, expe
if not expected:
assert "Couldn't verify initramfs file. It may be corrupted." in caplog.records[-2].message
assert "Output of lsinitrd: {}".format(subprocess_output[0]) in caplog.records[-1].message


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, "run_subprocess", mock_run)
monkeypatch.setattr(os.path, "exists", lambda x: True)
result = checks.is_initramfs_file_valid("mock_file")

assert result is False
6 changes: 0 additions & 6 deletions plans/tier1.fmf
Original file line number Diff line number Diff line change
Expand Up @@ -217,12 +217,6 @@ adjust+:
- kernel-boot-files/missing_kernel_boot_files

/corrupted_initramfs_file:
adjust+:
- enabled: false
when: distro == alma-8-latest, alma-9-latest, rocky-8.8
because: |
An unhandled UnicodeDecodeError breaks the conversion.
Reference https://issues.redhat.com/browse/RHELC-1711
discover+:
test+<:
- kernel-boot-files/corrupted_initramfs_file
Expand Down

0 comments on commit 7be034f

Please sign in to comment.