File fix-exception-in-yumpkg.remove-for-not-installed-pac.patch of Package salt

From 40d9cde9b90965e60520f36dbe189fb64d15559d 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 9737508377..9f8f548e5f 100644
--- a/salt/modules/yumpkg.py
+++ b/salt/modules/yumpkg.py
@@ -2123,6 +2123,8 @@ def remove(name=None, pkgs=None, **kwargs):  # pylint: disable=W0613
     pkg_params.update(pkg_matches)
 
     for target in pkg_params:
+        if target not in old:
+            continue
         version_to_remove = pkg_params[target]
 
         # Check if package version set to be removed is actually installed:
diff --git a/tests/pytests/unit/modules/test_yumpkg.py b/tests/pytests/unit/modules/test_yumpkg.py
index 3b35272550..c3456f7e29 100644
--- a/tests/pytests/unit/modules/test_yumpkg.py
+++ b/tests/pytests/unit/modules/test_yumpkg.py
@@ -1284,6 +1284,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.34.1


openSUSE Build Service is sponsored by