From 785ecf3f4f446294173d0f500f1083108e23ed8b Mon Sep 17 00:00:00 2001 From: Rodolfo Olivieri Date: Mon, 15 Aug 2022 11:23:57 -0300 Subject: [PATCH] Check if shim-x64 package is present in the system In PR#553 we removed a condition from the shim-x64 special case workaround where it was only supposed to handle UEFI systems, and now we are handling BIOS as well. The problem is that in the BIOS systems, this package may not be installed at all. This commit introduces a quick check to validate if the package is present on the system before trying to backup it's protection file under `/etc/yum/protected.d`. Signed-off-by: Rodolfo Olivieri --- convert2rhel/special_cases.py | 42 +++++++++++-------- convert2rhel/unit_tests/special_cases_test.py | 20 ++++++--- 2 files changed, 40 insertions(+), 22 deletions(-) diff --git a/convert2rhel/special_cases.py b/convert2rhel/special_cases.py index 0b81a5dc4a..1d7f3ade27 100644 --- a/convert2rhel/special_cases.py +++ b/convert2rhel/special_cases.py @@ -101,25 +101,33 @@ def perform_java_openjdk_workaround(): def unprotect_shim_x64(): - """Remove the shim-x64 package yum protection on UEFI-based Oracle Linux 7 systems as it causes a yum traceback. + """Remove the shim-x64 package protection on Oracle Linux 7 as it causes yum to traceback. - The package is protected through the /etc/yum/protected.d/shim-x64.conf file. It is installed with the - Oracle Linux 7 shim-x64 package. The same package on RHEL 7 does not install this file - it's OL specific - no - need to add it back after a successful conversion to RHEL. + The package is protected through the /etc/yum/protected.d/shim-x64.conf + file. It is installed with the Oracle Linux 7 shim-x64 package. The same + package on RHEL 7 does not install this file - it's OL specific - no need to + add it back after a successful conversion to RHEL. Related: https://bugzilla.redhat.com/show_bug.cgi?id=2009368 """ - logger.info("Removing shim-x64 package yum protection.") - if system_info.id == "oracle" and system_info.version.major == 7: - shim_x64_pkg_protection_file.backup() - try: - os.remove(shim_x64_pkg_protection_file.filepath) - logger.info( - "'%s' removed in accordance with https://bugzilla.redhat.com/show_bug.cgi?id=2009368." - % shim_x64_pkg_protection_file.filepath - ) - except OSError as err: - # For permissions reasons (unlikely as we run as root) or because it does not exist - logger.error("Unable to remove '%s': %s" % (shim_x64_pkg_protection_file.filepath, err.strerror)) + + logger.info("Checking if shim-x64 package is installed.") + shim_x64 = system_info.is_rpm_installed(name="shim-x64") + + if shim_x64: + logger.info("Removing shim-x64 package yum protection.") + if system_info.id == "oracle" and system_info.version.major == 7: + shim_x64_pkg_protection_file.backup() + try: + os.remove(shim_x64_pkg_protection_file.filepath) + logger.info( + "'%s' removed in accordance with https://bugzilla.redhat.com/show_bug.cgi?id=2009368." + % shim_x64_pkg_protection_file.filepath + ) + except OSError as err: + # For permissions reasons (unlikely as we run as root) or because it does not exist + logger.error("Unable to remove '%s': %s" % (shim_x64_pkg_protection_file.filepath, err.strerror)) + else: + logger.info("Relevant to Oracle Linux 7 only. Skipping.") else: - logger.info("Relevant to Oracle Linux 7 only. Skipping.") + logger.info("shim-x64 package is not installed. Nothing to do.") diff --git a/convert2rhel/unit_tests/special_cases_test.py b/convert2rhel/unit_tests/special_cases_test.py index c928a53a7a..aa64e67b24 100644 --- a/convert2rhel/unit_tests/special_cases_test.py +++ b/convert2rhel/unit_tests/special_cases_test.py @@ -90,17 +90,27 @@ def test_perform_java_openjdk_workaround( @pytest.mark.parametrize( - ("sys_id", "removal_ok", "log_msg"), + ("sys_id", "removal_ok", "log_msg", "is_rpm_installed"), ( - ("oracle", True, "removed in accordance with"), - ("oracle", False, "Unable to remove"), - ("centos", True, "Relevant to Oracle Linux 7 only"), + ("oracle", True, "removed in accordance with", True), + ("oracle", False, "Unable to remove", True), + ("centos", True, "Relevant to Oracle Linux 7 only", True), + ("centos", False, "shim-x64 package is not installed. Nothing to do.", False), ), ) @mock.patch("os.remove") -def test_unprotect_shim_x64(mock_os_remove, sys_id, removal_ok, log_msg, monkeypatch, caplog): +def test_unprotect_shim_x64( + mock_os_remove, + sys_id, + removal_ok, + log_msg, + is_rpm_installed, + monkeypatch, + caplog, +): monkeypatch.setattr(system_info, "id", sys_id) monkeypatch.setattr(system_info, "version", namedtuple("Version", ["major", "minor"])(7, 0)) + monkeypatch.setattr(special_cases.system_info, "is_rpm_installed", value=mock.Mock(return_value=is_rpm_installed)) if not removal_ok: mock_os_remove.side_effect = OSError()