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

From 01a670dad69e03bd8bf2da76a6a81e847af20aab Mon Sep 17 00:00:00 2001
From: Alexander Graul <agraul@suse.com>
Date: Tue, 25 Jan 2022 17:12:47 +0100
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/pytests/unit/modules/test_aptpkg.py | 18 ++++++++++++++++++
 2 files changed, 27 insertions(+)

diff --git a/salt/modules/aptpkg.py b/salt/modules/aptpkg.py
index 938e37cc9e..3289f6604d 100644
--- a/salt/modules/aptpkg.py
+++ b/salt/modules/aptpkg.py
@@ -3461,6 +3461,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/pytests/unit/modules/test_aptpkg.py b/tests/pytests/unit/modules/test_aptpkg.py
index 4226957eeb..eb72447c3a 100644
--- a/tests/pytests/unit/modules/test_aptpkg.py
+++ b/tests/pytests/unit/modules/test_aptpkg.py
@@ -385,6 +385,24 @@ def test_info_installed_attr(lowpkg_info_var):
         assert ret["wget"] == expected_pkg
 
 
+def test_info_installed_attr_without_status(lowpkg_info_var):
+    """
+    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:
+    """
+    mock = MagicMock(return_value=lowpkg_info_var)
+    with patch.dict(aptpkg.__salt__, {"lowpkg.info": mock}):
+        aptpkg.info_installed("wget", attr="version")
+        assert "status" in mock.call_args.kwargs["attr"]
+        assert "version" in mock.call_args.kwargs["attr"]
+
+
 def test_info_installed_all_versions(lowpkg_info_var):
     """
     Test info_installed 'all_versions'.
-- 
2.39.2


openSUSE Build Service is sponsored by