Skip to content

Commit

Permalink
Fix unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Diblik <[email protected]>
  • Loading branch information
danmyway committed Nov 22, 2024
1 parent 1e2548b commit 3a15552
Showing 1 changed file with 9 additions and 133 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,105 +79,31 @@ def apply_global_tool_opts(monkeypatch, global_tool_opts):
class TestInstallRhelKernel:
@pytest.mark.parametrize(
(
"subprocess_output",
"pkgs_w_diff_key_id",
"pkgs_w_rhel_key_id",
"no_newer_kernel_call",
"update_kernel",
"action_message",
"action_result",
),
(
(
# Info about installed kernel from yum contains the same version as is listed in the different key_id pkgs
# The latest installed kernel is from CentOS
"Package kernel-4.18.0-193.el8.x86_64 is already installed.",
[
create_pkg_information(
name="kernel",
version="4.18.0",
release="193.el8",
arch="x86_64",
packager="CentOS",
),
create_pkg_information(
name="kernel",
version="4.18.0",
release="183.el8",
arch="x86_64",
packager="CentOS",
),
],
1,
True,
set(()),
actions.ActionResult(level="SUCCESS", id="SUCCESS"),
),
(
# Output from yum contains different version than is listed in different key_id
# Rhel kernel already installed with centos kernels
"Package kernel-4.18.0-205.el8.x86_64 is already installed.",
[
create_pkg_information(
name="kernel",
version="4.18.0",
release="193.el8",
arch="x86_64",
packager="CentOS",
),
create_pkg_information(
name="kernel",
version="4.18.0",
release="183.el8",
arch="x86_64",
packager="CentOS",
),
],
0,
False,
set(()),
actions.ActionResult(level="SUCCESS", id="SUCCESS"),
),
(
# Only rhel kernel already installed
"Package kernel-4.18.0-205.el8.x86_64 is already installed.",
# rhel kernel not installed
[],
0,
False,
set(()),
actions.ActionResult(level="SUCCESS", id="SUCCESS"),
),
(
# Output from yum contains different version than is listed in different key_id
# Rhel kernel already installed in older versin than centos kernel
"Package kernel-4.18.0-183.el8.x86_64 is already installed.",
[
create_pkg_information(
name="kernel",
version="4.18.0",
release="193.el8",
arch="x86_64",
packager="CentOS",
),
],
1,
True,
set(()),
actions.ActionResult(level="SUCCESS", id="SUCCESS"),
),
(
# Output from yum contains info about installing some package - corner case
"Installing kernel-4.18.0-183.el8.x86_64",
# rhel kernel already installed
[
create_pkg_information(
name="kernel",
version="4.18.0",
release="193.el8",
release="183.el8",
arch="x86_64",
packager="CentOS",
packager="Red Hat",
),
],
0,
False,
set(()),
actions.ActionResult(level="SUCCESS", id="SUCCESS"),
),
Expand All @@ -187,70 +113,24 @@ class TestInstallRhelKernel:
def test_install_rhel_kernel(
self,
monkeypatch,
subprocess_output,
pkgs_w_diff_key_id,
pkgs_w_rhel_key_id,
install_rhel_kernel_instance,
no_newer_kernel_call,
update_kernel,
pretend_os,
action_message,
action_result,
):
"""Test the logic of kernel installation&update"""
handle_no_newer_rhel_kernel_available = mock.Mock()

monkeypatch.setattr(
utils, "run_subprocess", RunSubprocessMocked(return_string=subprocess_output, return_code=0)
)
monkeypatch.setattr(
pkghandler,
"get_installed_pkgs_w_different_key_id",
GetInstalledPkgsWDifferentKeyIdMocked(return_value=pkgs_w_diff_key_id),
)
monkeypatch.setattr(pkghandler, "handle_no_newer_rhel_kernel_available", handle_no_newer_rhel_kernel_available)

install_rhel_kernel_instance.run()

assert handle_no_newer_rhel_kernel_available.call_count == no_newer_kernel_call
assert preserve_only_rhel_kernel._kernel_update_needed == update_kernel
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

@pytest.mark.parametrize(
("subprocess_output", "subprocess_return", "action_message", "action_result"),
(
(
"yum command failed",
1,
set(()),
actions.ActionResult(
level="ERROR",
id="FAILED_TO_INSTALL_RHEL_KERNEL",
title="Failed to install RHEL kernel",
description="There was an error while attempting to install the RHEL kernel from yum.",
remediations="Please check that you can access the repositories that provide the RHEL kernel.",
),
),
),
)
@centos8
def test_install_rhel_kernel_yum_fail(
self,
monkeypatch,
subprocess_output,
subprocess_return,
action_message,
action_result,
install_rhel_kernel_instance,
pretend_os,
):
monkeypatch.setattr(
utils, "run_subprocess", RunSubprocessMocked(return_string=subprocess_output, return_code=subprocess_return)
pkghandler, "get_installed_pkgs_by_key_id", GetInstalledPkgsByKeyIdMocked(return_value=pkgs_w_rhel_key_id)
)

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
Expand Down Expand Up @@ -615,14 +495,10 @@ def test_fix_default_kernel_with_no_incorrect_kernel(
class TestUpdateKernel:
@pytest.mark.parametrize(
("update_kernel"),
(
(True),
(False),
),
((True),),
)
@centos8
def test_update_kernel(self, monkeypatch, update_kernel_instance, update_kernel, pretend_os):
preserve_only_rhel_kernel._kernel_update_needed = update_kernel
update_rhel_kernel = mock.Mock()
monkeypatch.setattr(pkghandler, "update_rhel_kernel", update_rhel_kernel)

Expand Down

0 comments on commit 3a15552

Please sign in to comment.