Skip to content

Commit

Permalink
Improve unit tests to increase coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
r0x0d committed May 15, 2024
1 parent cc4e19a commit c630878
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 47 deletions.
2 changes: 1 addition & 1 deletion convert2rhel/subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -714,8 +714,8 @@ def disable_repos():
description="As part of the conversion process, convert2rhel disables all current subscription-manager repositories and enables only repositories required for the conversion. convert2rhel was unable to disable these repositories, and the conversion is unable to proceed.",
diagnosis="Failed to disable repositories: %s." % (output),
)

loggerinst.info("Repositories disabled.")
return


def enable_repos(rhel_repoids):
Expand Down
44 changes: 0 additions & 44 deletions convert2rhel/unit_tests/backup/subscription_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,50 +144,6 @@ def test_restore_auto_attach_not_enabled(self, auto_attach_subscription, monkeyp
auto_attach_subscription.restore()
assert subscription.remove_subscription.call_count == 0

@pytest.mark.parametrize(
("return_code", "exception"),
(
(1, True),
(0, False),
),
)
def test_auto_attach_subscription(self, monkeypatch, return_code, exception):
monkeypatch.setattr(
utils,
"run_subprocess",
RunSubprocessMocked(return_code=return_code),
)
if exception:
with pytest.raises(subscription.SubscriptionAutoAttachmentError):
subscription.auto_attach_subscription()
else:
try:
subscription.auto_attach_subscription()
except subscription.SubscriptionAutoAttachmentError:
assert False

@pytest.mark.parametrize(
("return_code", "exception"),
(
(1, True),
(0, False),
),
)
def test_remove_subscription(self, monkeypatch, return_code, exception):
monkeypatch.setattr(
utils,
"run_subprocess",
RunSubprocessMocked(return_code=return_code),
)
if exception:
with pytest.raises(subscription.SubscriptionRemovalError):
subscription.remove_subscription()
else:
try:
subscription.remove_subscription()
except subscription.SubscriptionRemovalError:
assert False


class TestRestorableDisableRepositories:
@pytest.mark.parametrize(
Expand Down
77 changes: 75 additions & 2 deletions convert2rhel/unit_tests/subscription_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@
from convert2rhel.backup import files
from convert2rhel.systeminfo import Version, system_info
from convert2rhel.unit_tests import (
AutoAttachSubscriptionMocked,
PromptUserMocked,
RemoveAutoAttachSubscriptionMocked,
RunSubprocessMocked,
UnregisterSystemMocked,
create_pkg_information,
Expand Down Expand Up @@ -1334,3 +1332,78 @@ def test_get_rhsm_facts_json_decode_error_el8(monkeypatch, global_tool_opts, cap
assert isinstance(facts, dict)
assert len(facts) == 0
assert run_mock.call_count == 0


@pytest.mark.parametrize(
("return_code", "exception"),
(
(1, True),
(0, False),
),
)
def test_auto_attach_subscription(monkeypatch, return_code, exception):
monkeypatch.setattr(
utils,
"run_subprocess",
RunSubprocessMocked(return_code=return_code),
)
if exception:
with pytest.raises(subscription.SubscriptionAutoAttachmentError):
subscription.auto_attach_subscription()
else:
try:
subscription.auto_attach_subscription()
except subscription.SubscriptionAutoAttachmentError:
assert False


@pytest.mark.parametrize(
("return_code", "exception"),
(
(1, True),
(0, False),
),
)
def test_remove_subscription(monkeypatch, return_code, exception):
monkeypatch.setattr(
utils,
"run_subprocess",
RunSubprocessMocked(return_code=return_code),
)
if exception:
with pytest.raises(subscription.SubscriptionRemovalError):
subscription.remove_subscription()
else:
try:
subscription.remove_subscription()
except subscription.SubscriptionRemovalError:
assert False


@pytest.mark.parametrize(
(
"toolopts_disablerepo",
"expected_cmd",
),
(
(["test", "repo"], ["subscription-manager", "repos", "--disable=test", "--disable=repo"]),
(None, ["subscription-manager", "repos", "--disable=*"]),
),
)
def test_disable_repos(toolopts_disablerepo, expected_cmd, global_tool_opts, monkeypatch, caplog):
global_tool_opts.disablerepo = toolopts_disablerepo
monkeypatch.setattr(utils, "run_subprocess", RunSubprocessMocked(return_code=0))
monkeypatch.setattr(subscription, "tool_opts", global_tool_opts)

subscription.disable_repos()
assert utils.run_subprocess.cmd == expected_cmd
assert "Repositories disabled" in caplog.records[-1].message


def test_disable_repos_critical_error(monkeypatch, caplog):
monkeypatch.setattr(utils, "run_subprocess", RunSubprocessMocked(return_code=1, return_string="error"))
with pytest.raises(exceptions.CriticalError):
subscription.disable_repos()
assert "Could not disable subscription-manager repositories:\nerror" in caplog.records[-1].message

assert utils.run_subprocess.cmd == ["subscription-manager", "repos", "--disable=*"]

0 comments on commit c630878

Please sign in to comment.