File info_installed-works-without-status-attr-now.patch of Package salt

From 0ef6eed4f5e120a584843c33272066ba477feb3f Mon Sep 17 00:00:00 2001
From: Jochen Breuer <brejoc@gmail.com>
Date: Tue, 19 May 2020 10:34:35 +0200
Subject: [PATCH] info_installed works without status attr now

If 'status' was excluded via attr, info_installed was no longer able to
detect if a package was installed or not. Now info_installed adds the
'status' for the 'lowpkg.info' request again.
---
 salt/modules/aptpkg.py            |  9 +++++++++
 tests/unit/modules/test_aptpkg.py | 20 ++++++++++++++++++++
 2 files changed, 29 insertions(+)

diff --git a/salt/modules/aptpkg.py b/salt/modules/aptpkg.py
index db0480b45d..e4a9872aad 100644
--- a/salt/modules/aptpkg.py
+++ b/salt/modules/aptpkg.py
@@ -2923,6 +2923,15 @@ def info_installed(*names, **kwargs):
     failhard = kwargs.pop("failhard", True)
     kwargs.pop("errors", None)  # Only for compatibility with RPM
     attr = kwargs.pop("attr", None)  # Package attributes to return
+
+    # status is needed to see if a package is installed. So we have to add it,
+    # even if it's excluded via attr parameter. Otherwise all packages are
+    # returned.
+    if attr:
+        attr_list = set(attr.split(","))
+        attr_list.add("status")
+        attr = ",".join(attr_list)
+
     all_versions = kwargs.pop(
         "all_versions", False
     )  # This is for backward compatible structure only
diff --git a/tests/unit/modules/test_aptpkg.py b/tests/unit/modules/test_aptpkg.py
index 3c9744e224..51dfce29eb 100644
--- a/tests/unit/modules/test_aptpkg.py
+++ b/tests/unit/modules/test_aptpkg.py
@@ -297,6 +297,26 @@ class AptPkgTestCase(TestCase, LoaderModuleMockMixin):
         self.assertEqual(aptpkg.info_installed("wget"), installed)
         self.assertEqual(len(aptpkg.info_installed()), 1)
 
+    def test_info_installed_attr_without_status(self):
+        """
+        Test info_installed 'attr' for inclusion of 'status' attribute.
+
+        Since info_installed should only return installed packages, we need to
+        call __salt__['lowpkg.info'] with the 'status' attribute even if the user
+        is not asking for it in 'attr'. Otherwise info_installed would not be able
+        to check if the package is installed and would return everything.
+
+        :return:
+        """
+        with patch(
+            "salt.modules.aptpkg.__salt__",
+            {"lowpkg.info": MagicMock(return_value=LOWPKG_INFO)},
+        ) as wget_lowpkg:
+            ret = aptpkg.info_installed("wget", attr="version")
+            calls = wget_lowpkg["lowpkg.info"].call_args_list.pop()
+            self.assertIn("status", calls.kwargs["attr"])
+            self.assertIn("version", calls.kwargs["attr"])
+
     @patch(
         "salt.modules.aptpkg.__salt__",
         {"lowpkg.info": MagicMock(return_value=LOWPKG_INFO)},
-- 
2.29.2


openSUSE Build Service is sponsored by