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

From 30a2c8c042f0fe57253a8ab47220d897bc89bd17 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/unit/modules/test_yumpkg.py | 25 +++++++++++++++++++++++++
 2 files changed, 27 insertions(+)

diff --git a/salt/modules/yumpkg.py b/salt/modules/yumpkg.py
index 0fb41a0400..c9f9f2c2d3 100644
--- a/salt/modules/yumpkg.py
+++ b/salt/modules/yumpkg.py
@@ -2051,6 +2051,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/unit/modules/test_yumpkg.py b/tests/unit/modules/test_yumpkg.py
index e22c0b9251..373d2e09cb 100644
--- a/tests/unit/modules/test_yumpkg.py
+++ b/tests/unit/modules/test_yumpkg.py
@@ -1099,6 +1099,31 @@ class YumTestCase(TestCase, LoaderModuleMockMixin):
                 call = cmd_mock.mock_calls[0][1][0]
                 assert call == expected, call
 
+    def test_remove_not_existing(self):
+        """
+        Test if no exception on removing not installed package
+        """
+        name = "foo"
+        def list_pkgs_mock():
+            return {}
+        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):
+
+            with patch.dict(yumpkg.__grains__, {"os": "CentOS", "osrelease": 7}):
+                yumpkg.remove(name)
+                cmd_mock.assert_not_called()
+
     def test_install_with_epoch(self):
         """
         Tests that we properly identify a version containing an epoch as an
-- 
2.32.0


openSUSE Build Service is sponsored by