Skip to content

Commit

Permalink
Merge kernel installation and verification actions
Browse files Browse the repository at this point in the history
And improve comments and action ID to reflect the main goals of
functions and actions.
  • Loading branch information
bocekm committed Nov 26, 2024
1 parent 7b449a5 commit c6f7e1c
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

class ListNonRedHatPkgsLeft(actions.Action):
id = "LIST_NON_RED_HAT_PKGS_LEFT"
dependencies = ("KERNEL_PACKAGES_INSTALLATION",)
dependencies = ("REMOVE_NON_RHEL_KERNELS",)

def run(self):
"""List all the packages that have not been replaced by the
Expand Down
45 changes: 25 additions & 20 deletions convert2rhel/actions/conversion/preserve_only_rhel_kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,35 +30,32 @@ class InstallRhelKernel(actions.Action):
dependencies = ("CONVERT_SYSTEM_PACKAGES",)

def run(self):
"""Install and update the RHEL kernel."""
"""Ensure that the RHEL kernel is installed.
The RHEL kernel might have not been installed during the main conversion transaction in case the installed
non-RHEL kernel(s) conflicted with the available RHEL kernels.
"""
super(InstallRhelKernel, self).run()
loggerinst.task("Prepare kernel")
loggerinst.info("Verifying that RHEL kernel has been installed")

rhel_kernels = pkghandler.get_installed_pkgs_by_key_id(system_info.key_ids_rhel, name="kernel")

if not rhel_kernels:
# install the rhel kernel when any isn't installed
loggerinst.debug("handle_no_newer_rhel_kernel_available")
loggerinst.debug(
"RHEL kernel has not been installed in the main conversion transaction."
"We are about to handle its installation separately."
)
pkghandler.handle_no_newer_rhel_kernel_available()


class VerifyRhelKernelInstalled(actions.Action):
id = "VERIFY_RHEL_KERNEL_INSTALLED"
dependencies = ("INSTALL_RHEL_KERNEL",)

def run(self):
"""Verify that the RHEL kernel has been successfully installed and raise an ERROR if not"""
super(VerifyRhelKernelInstalled, self).run()

loggerinst.info("Verifying that RHEL kernel has been installed")
installed_rhel_kernels = pkghandler.get_installed_pkgs_by_key_id(system_info.key_ids_rhel, name="kernel")
if len(installed_rhel_kernels) <= 0:
self.set_result(
level="ERROR",
id="NO_RHEL_KERNEL_INSTALLED",
title="No RHEL kernel installed",
description="There is no RHEL kernel installed on the system.",
remediations="Verify that the repository used for installing kernel contains RHEL packages.",
remediations="Verify that the repository used for installing kernel contains RHEL packages and install"
" the kernel manually.",
)
return

Expand All @@ -73,7 +70,7 @@ def run(self):

class FixInvalidGrub2Entries(actions.Action):
id = "FIX_INVALID_GRUB2_ENTRIES"
dependencies = ("KERNEL_PACKAGES_INSTALLATION",)
dependencies = ("REMOVE_NON_RHEL_KERNELS",)

def run(self):
"""
Expand Down Expand Up @@ -171,11 +168,11 @@ def run(self):


class KernelPkgsInstall(actions.Action):
id = "KERNEL_PACKAGES_INSTALLATION"
dependencies = ("VERIFY_RHEL_KERNEL_INSTALLED",)
id = "REMOVE_NON_RHEL_KERNELS"
dependencies = ("INSTALL_RHEL_KERNEL",)

def run(self):
"""Install kernel packages and remove non-RHEL kernels."""
"""Remove non-RHEL kernels."""
super(KernelPkgsInstall, self).run()

kernel_pkgs_to_install = self.remove_non_rhel_kernels()
Expand Down Expand Up @@ -213,9 +210,17 @@ def install_additional_rhel_kernel_pkgs(self, additional_pkgs):

class UpdateKernel(actions.Action):
id = "UPDATE_KERNEL"
dependencies = ("FIX_DEFAULT_KERNEL",)
dependencies = ("REMOVE_NON_RHEL_KERNELS",)

def run(self):
"""Ensure that the latest RHEL kernel is installed.
The RHEL kernel we've installed during the main conversion transaction is not up-to-date in case
the latest available RHEL kernel conflicted with the installed non-RHEL kernels.
At this point though all non-RHEL kernels are already removed so the latest RHEL kernel won't conflict with
them anymore.
"""
super(UpdateKernel, self).run()

pkghandler.update_rhel_kernel()
16 changes: 8 additions & 8 deletions convert2rhel/pkghandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -594,19 +594,19 @@ def install_gpg_keys():


def handle_no_newer_rhel_kernel_available():
"""Handle cases when the installed third party (non-RHEL) kernel has the
same version as (or newer than) the RHEL one available in the RHEL repo(s).
"""Handle cases when the installed non-RHEL kernel has the
same version as (or newer than) the latest RHEL kernel available in the RHEL repo(s).
"""
installed, available = get_kernel_availability()
to_install = [kernel for kernel in available if kernel not in installed]
installed, all_available = get_kernel_availability()
available_to_install = [kernel for kernel in all_available if kernel not in installed]

if not to_install:
# All the available RHEL kernel versions are already installed
if not available_to_install:
# All the available RHEL kernels are already installed
if len(installed) > 1:
# There's more than one installed non-RHEL kernel. We'll remove one
# of them - the one that has the same version as the available RHEL
# kernel
older = available[-1]
older = all_available[-1]
utils.remove_pkgs(pkgs_to_remove=["kernel-{}".format(older)])
pkgmanager.call_yum_cmd(command="install", args=["kernel-{}".format(older)])
else:
Expand All @@ -615,7 +615,7 @@ def handle_no_newer_rhel_kernel_available():
return

# Install the latest out of the available non-clashing RHEL kernels
pkgmanager.call_yum_cmd(command="install", args=["kernel-{}".format(to_install[-1])])
pkgmanager.call_yum_cmd(command="install", args=["kernel-{}".format(available_to_install[-1])])


def get_kernel_availability():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,6 @@ def install_rhel_kernel_instance():
return preserve_only_rhel_kernel.InstallRhelKernel()


@pytest.fixture
def verify_rhel_kernel_installed_instance():
return preserve_only_rhel_kernel.VerifyRhelKernelInstalled()


@pytest.fixture
def fix_invalid_grub2_entries_instance():
return preserve_only_rhel_kernel.FixInvalidGrub2Entries()
Expand Down Expand Up @@ -81,31 +76,17 @@ class TestInstallRhelKernel:
(
"pkgs_w_rhel_key_id",
"no_newer_kernel_call",
"action_message",
"action_result",
),
(
(
# rhel kernel not installed
[],
1,
set(()),
actions.ActionResult(level="SUCCESS", id="SUCCESS"),
),
(
# rhel kernel already installed
[
create_pkg_information(
name="kernel",
version="4.18.0",
release="183.el8",
arch="x86_64",
packager="Red Hat",
),
],
[create_pkg_information(name="kernel")],
0,
set(()),
actions.ActionResult(level="SUCCESS", id="SUCCESS"),
),
),
)
Expand All @@ -117,10 +98,8 @@ def test_install_rhel_kernel(
install_rhel_kernel_instance,
no_newer_kernel_call,
pretend_os,
action_message,
action_result,
):
"""Test the logic of kernel installation&update"""
"""Test the logic of kernel installation"""
handle_no_newer_rhel_kernel_available = mock.Mock()

monkeypatch.setattr(pkghandler, "handle_no_newer_rhel_kernel_available", handle_no_newer_rhel_kernel_available)
Expand All @@ -131,9 +110,43 @@ def test_install_rhel_kernel(
install_rhel_kernel_instance.run()

assert handle_no_newer_rhel_kernel_available.call_count == no_newer_kernel_call
assert action_message.issuperset(install_rhel_kernel_instance.messages)
assert action_message.issubset(install_rhel_kernel_instance.messages)
assert action_result == install_rhel_kernel_instance.result

def test_verify_rhel_kernel_installed(self, monkeypatch, install_rhel_kernel_instance):
monkeypatch.setattr(
pkghandler,
"get_installed_pkgs_by_key_id",
GetInstalledPkgsByKeyIdMocked(return_value=[create_pkg_information(name="kernel")]),
)
install_rhel_kernel_instance.run()
expected = set(
(
actions.ActionMessage(
level="INFO",
id="RHEL_KERNEL_INSTALL_VERIFIED",
title="RHEL kernel install verified",
description="The RHEL kernel has been verified to be on the system.",
diagnosis=None,
remediations=None,
),
)
)
assert expected.issuperset(install_rhel_kernel_instance.messages)
assert expected.issubset(install_rhel_kernel_instance.messages)

def test_verify_rhel_kernel_installed_not_installed(self, monkeypatch, install_rhel_kernel_instance):
monkeypatch.setattr(pkghandler, "get_installed_pkgs_by_key_id", mock.Mock(return_value=[]))
monkeypatch.setattr(pkghandler, "handle_no_newer_rhel_kernel_available", mock.Mock())

install_rhel_kernel_instance.run()
unit_tests.assert_actions_result(
install_rhel_kernel_instance,
level="ERROR",
id="NO_RHEL_KERNEL_INSTALLED",
title="No RHEL kernel installed",
description="There is no RHEL kernel installed on the system.",
remediations="Verify that the repository used for installing kernel contains RHEL packages and install the"
" kernel manually.",
)


class TestKernelPkgsInstall:
Expand Down Expand Up @@ -197,43 +210,6 @@ def test_install_additional_rhel_kernel_pkgs(self, monkeypatch, kernel_packages_
assert pkgmanager.call_yum_cmd.call_count == 2


class TestVerifyRHELKernelInstalled:
def test_verify_rhel_kernel_installed(self, monkeypatch, verify_rhel_kernel_installed_instance):
monkeypatch.setattr(
pkghandler,
"get_installed_pkgs_by_key_id",
GetInstalledPkgsByKeyIdMocked(return_value=[create_pkg_information(name="kernel")]),
)
verify_rhel_kernel_installed_instance.run()
expected = set(
(
actions.ActionMessage(
level="INFO",
id="RHEL_KERNEL_INSTALL_VERIFIED",
title="RHEL kernel install verified",
description="The RHEL kernel has been verified to be on the system.",
diagnosis=None,
remediations=None,
),
)
)
assert expected.issuperset(verify_rhel_kernel_installed_instance.messages)
assert expected.issubset(verify_rhel_kernel_installed_instance.messages)

def test_verify_rhel_kernel_installed_not_installed(self, monkeypatch, verify_rhel_kernel_installed_instance):
monkeypatch.setattr(pkghandler, "get_installed_pkgs_by_key_id", mock.Mock(return_value=[]))

verify_rhel_kernel_installed_instance.run()
unit_tests.assert_actions_result(
verify_rhel_kernel_installed_instance,
level="ERROR",
id="NO_RHEL_KERNEL_INSTALLED",
title="No RHEL kernel installed",
description="There is no RHEL kernel installed on the system.",
remediations="Verify that the repository used for installing kernel contains RHEL packages.",
)


class TestFixInvalidGrub2Entries:
def test_fix_invalid_grub2_entries(self, caplog, monkeypatch, fix_invalid_grub2_entries_instance):
monkeypatch.setattr(system_info, "version", Version(8, 0))
Expand Down

0 comments on commit c6f7e1c

Please sign in to comment.