salt/fix-exception-in-yumpkg.remove-for-not-installed-pac.patch

77 lines
2.7 KiB
Diff
Raw Normal View History

From 9413059223107924c6594e6c72e50fcbcc441e60 Mon Sep 17 00:00:00 2001
From: Victor Zhestkov <35733135+vzhestkov@users.noreply.github.com>
Date: Thu, 24 Jun 2021 13:17:13 +0300
Subject: [PATCH] Fix exception in yumpkg.remove for not installed
package (#380)
---
salt/modules/yumpkg.py | 2 ++
tests/pytests/unit/modules/test_yumpkg.py | 37 +++++++++++++++++++++++
2 files changed, 39 insertions(+)
diff --git a/salt/modules/yumpkg.py b/salt/modules/yumpkg.py
index dd81c6f1e9..273f0fb370 100644
--- a/salt/modules/yumpkg.py
+++ b/salt/modules/yumpkg.py
@@ -2087,6 +2087,8 @@ def remove(name=None, pkgs=None, **kwargs): # pylint: disable=W0613
old = list_pkgs()
targets = []
for target in pkg_params:
+ if target not in old:
+ continue
version_to_remove = pkg_params[target]
installed_versions = old[target].split(",")
diff --git a/tests/pytests/unit/modules/test_yumpkg.py b/tests/pytests/unit/modules/test_yumpkg.py
index 7e3ed517ea..b5572db123 100644
--- a/tests/pytests/unit/modules/test_yumpkg.py
+++ b/tests/pytests/unit/modules/test_yumpkg.py
@@ -1219,6 +1219,43 @@ def test_install_error_reporting():
assert exc_info.value.info == expected, exc_info.value.info
+def test_remove_not_installed():
+ """
+ Tests that no exception raised on removing not installed package
+ """
+ name = "foo"
+ list_pkgs_mock = MagicMock(return_value={})
+ cmd_mock = MagicMock(
+ return_value={"pid": 12345, "retcode": 0, "stdout": "", "stderr": ""}
+ )
+ salt_mock = {
+ "cmd.run_all": cmd_mock,
+ "lowpkg.version_cmp": rpm.version_cmp,
+ "pkg_resource.parse_targets": MagicMock(
+ return_value=({name: None}, "repository")
+ ),
+ }
+ with patch.object(yumpkg, "list_pkgs", list_pkgs_mock), patch(
+ "salt.utils.systemd.has_scope", MagicMock(return_value=False)
+ ), patch.dict(yumpkg.__salt__, salt_mock):
+
+ # Test yum
+ with patch.dict(yumpkg.__context__, {"yum_bin": "yum"}), patch.dict(
+ yumpkg.__grains__, {"os": "CentOS", "osrelease": 7}
+ ):
+ yumpkg.remove(name)
+ cmd_mock.assert_not_called()
+
+ # Test dnf
+ yumpkg.__context__.pop("yum_bin")
+ cmd_mock.reset_mock()
+ with patch.dict(yumpkg.__context__, {"yum_bin": "dnf"}), patch.dict(
+ yumpkg.__grains__, {"os": "Fedora", "osrelease": 27}
+ ):
+ yumpkg.remove(name)
+ cmd_mock.assert_not_called()
+
+
def test_upgrade_with_options():
with patch.object(yumpkg, "list_pkgs", MagicMock(return_value={})), patch(
"salt.utils.systemd.has_scope", MagicMock(return_value=False)
--
2.33.0