2023-05-05 11:15:58 +02:00
|
|
|
From deaee93b2f83f1524ec136afc1a5198b33d293d2 Mon Sep 17 00:00:00 2001
|
2020-10-14 17:29:00 +02:00
|
|
|
From: Alberto Planas <aplanas@suse.com>
|
|
|
|
Date: Mon, 5 Oct 2020 16:24:16 +0200
|
|
|
|
Subject: [PATCH] zypperpkg: ignore retcode 104 for search()
|
|
|
|
(bsc#1176697) (#270)
|
|
|
|
|
|
|
|
---
|
2021-01-08 13:41:50 +01:00
|
|
|
salt/modules/zypperpkg.py | 28 ++++++---
|
2023-05-05 11:15:58 +02:00
|
|
|
tests/unit/modules/test_zypperpkg.py | 87 ++++++++++++++++++++++------
|
|
|
|
2 files changed, 89 insertions(+), 26 deletions(-)
|
2020-10-14 17:29:00 +02:00
|
|
|
|
|
|
|
diff --git a/salt/modules/zypperpkg.py b/salt/modules/zypperpkg.py
|
2023-05-05 11:15:58 +02:00
|
|
|
index d8220a1fdd..4bb10f445a 100644
|
2020-10-14 17:29:00 +02:00
|
|
|
--- a/salt/modules/zypperpkg.py
|
|
|
|
+++ b/salt/modules/zypperpkg.py
|
2023-05-05 11:15:58 +02:00
|
|
|
@@ -103,6 +103,8 @@ class _Zypper:
|
2020-10-14 17:29:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
LOCK_EXIT_CODE = 7
|
|
|
|
+ NOT_FOUND_EXIT_CODE = 104
|
|
|
|
+
|
2021-01-08 13:41:50 +01:00
|
|
|
XML_DIRECTIVES = ["-x", "--xmlout"]
|
2020-10-14 17:29:00 +02:00
|
|
|
# ZYPPER_LOCK is not affected by --root
|
2021-01-08 13:41:50 +01:00
|
|
|
ZYPPER_LOCK = "/var/run/zypp.pid"
|
2023-05-05 11:15:58 +02:00
|
|
|
@@ -134,6 +136,7 @@ class _Zypper:
|
2020-10-14 17:29:00 +02:00
|
|
|
self.__no_raise = False
|
|
|
|
self.__refresh = False
|
|
|
|
self.__ignore_repo_failure = False
|
|
|
|
+ self.__ignore_not_found = False
|
|
|
|
self.__systemd_scope = False
|
|
|
|
self.__root = None
|
|
|
|
|
2023-05-05 11:15:58 +02:00
|
|
|
@@ -153,6 +156,9 @@ class _Zypper:
|
2020-10-14 17:29:00 +02:00
|
|
|
# Ignore exit code for 106 (repo is not available)
|
2021-01-08 13:41:50 +01:00
|
|
|
if "no_repo_failure" in kwargs:
|
|
|
|
self.__ignore_repo_failure = kwargs["no_repo_failure"]
|
2020-10-14 17:29:00 +02:00
|
|
|
+ # Ignore exit code for 104 (package not found)
|
|
|
|
+ if "ignore_not_found" in kwargs:
|
|
|
|
+ self.__ignore_not_found = kwargs["ignore_not_found"]
|
2021-01-08 13:41:50 +01:00
|
|
|
if "systemd_scope" in kwargs:
|
|
|
|
self.__systemd_scope = kwargs["systemd_scope"]
|
|
|
|
if "root" in kwargs:
|
2023-05-05 11:15:58 +02:00
|
|
|
@@ -333,6 +339,10 @@ class _Zypper:
|
2020-10-14 17:29:00 +02:00
|
|
|
if self.__root:
|
2021-01-08 13:41:50 +01:00
|
|
|
self.__cmd.extend(["--root", self.__root])
|
2020-10-14 17:29:00 +02:00
|
|
|
|
|
|
|
+ # Do not consider 104 as a retcode error
|
|
|
|
+ if self.__ignore_not_found:
|
|
|
|
+ kwargs["success_retcodes"] = [_Zypper.NOT_FOUND_EXIT_CODE]
|
|
|
|
+
|
|
|
|
self.__cmd.extend(args)
|
2021-01-08 13:41:50 +01:00
|
|
|
kwargs["output_loglevel"] = "trace"
|
|
|
|
kwargs["python_shell"] = False
|
2023-05-05 11:15:58 +02:00
|
|
|
@@ -479,9 +489,11 @@ class Wildcard:
|
2020-10-14 17:29:00 +02:00
|
|
|
Get available versions of the package.
|
|
|
|
:return:
|
2021-01-08 13:41:50 +01:00
|
|
|
"""
|
|
|
|
- solvables = self.zypper.nolock.xml.call(
|
|
|
|
- "se", "-xv", self.name
|
|
|
|
- ).getElementsByTagName("solvable")
|
2020-10-14 17:29:00 +02:00
|
|
|
+ solvables = (
|
|
|
|
+ self.zypper(ignore_not_found=True)
|
|
|
|
+ .nolock.xml.call("se", "-v", self.name)
|
|
|
|
+ .getElementsByTagName("solvable")
|
|
|
|
+ )
|
|
|
|
if not solvables:
|
2021-01-08 13:41:50 +01:00
|
|
|
raise CommandExecutionError(
|
|
|
|
"No packages found matching '{}'".format(self.name)
|
2023-05-05 11:15:58 +02:00
|
|
|
@@ -1086,7 +1098,7 @@ def list_repo_pkgs(*args, **kwargs):
|
2020-10-14 17:29:00 +02:00
|
|
|
|
2021-01-08 13:41:50 +01:00
|
|
|
root = kwargs.get("root") or None
|
|
|
|
for node in (
|
|
|
|
- __zypper__(root=root)
|
2020-10-14 17:29:00 +02:00
|
|
|
+ __zypper__(root=root, ignore_not_found=True)
|
2021-01-08 13:41:50 +01:00
|
|
|
.xml.call("se", "-s", *targets)
|
|
|
|
.getElementsByTagName("solvable")
|
|
|
|
):
|
2023-05-05 11:15:58 +02:00
|
|
|
@@ -2556,7 +2568,9 @@ def owner(*paths, **kwargs):
|
2020-10-14 17:29:00 +02:00
|
|
|
def _get_visible_patterns(root=None):
|
2021-01-08 13:41:50 +01:00
|
|
|
"""Get all available patterns in the repo that are visible."""
|
2020-10-14 17:29:00 +02:00
|
|
|
patterns = {}
|
2021-01-08 13:41:50 +01:00
|
|
|
- search_patterns = __zypper__(root=root).nolock.xml.call("se", "-t", "pattern")
|
2020-10-14 17:29:00 +02:00
|
|
|
+ search_patterns = __zypper__(root=root, ignore_not_found=True).nolock.xml.call(
|
|
|
|
+ "se", "-t", "pattern"
|
|
|
|
+ )
|
2021-01-08 13:41:50 +01:00
|
|
|
for element in search_patterns.getElementsByTagName("solvable"):
|
|
|
|
installed = element.getAttribute("status") == "installed"
|
|
|
|
patterns[element.getAttribute("name")] = {
|
2023-05-05 11:15:58 +02:00
|
|
|
@@ -2753,7 +2767,7 @@ def search(criteria, refresh=False, **kwargs):
|
2020-10-14 17:29:00 +02:00
|
|
|
|
|
|
|
cmd.append(criteria)
|
2021-01-08 13:41:50 +01:00
|
|
|
solvables = (
|
|
|
|
- __zypper__(root=root)
|
2020-10-14 17:29:00 +02:00
|
|
|
+ __zypper__(root=root, ignore_not_found=True)
|
2021-01-08 13:41:50 +01:00
|
|
|
.nolock.noraise.xml.call(*cmd)
|
|
|
|
.getElementsByTagName("solvable")
|
|
|
|
)
|
2023-05-05 11:15:58 +02:00
|
|
|
@@ -3005,7 +3019,7 @@ def _get_patches(installed_only=False, root=None):
|
2021-01-08 13:41:50 +01:00
|
|
|
"""
|
2020-10-14 17:29:00 +02:00
|
|
|
patches = {}
|
2021-01-08 13:41:50 +01:00
|
|
|
for element in (
|
|
|
|
- __zypper__(root=root)
|
2020-10-14 17:29:00 +02:00
|
|
|
+ __zypper__(root=root, ignore_not_found=True)
|
2021-01-08 13:41:50 +01:00
|
|
|
.nolock.xml.call("se", "-t", "patch")
|
|
|
|
.getElementsByTagName("solvable")
|
|
|
|
):
|
2020-10-14 17:29:00 +02:00
|
|
|
diff --git a/tests/unit/modules/test_zypperpkg.py b/tests/unit/modules/test_zypperpkg.py
|
2023-05-05 11:15:58 +02:00
|
|
|
index 22137a2544..5e4c967520 100644
|
2020-10-14 17:29:00 +02:00
|
|
|
--- a/tests/unit/modules/test_zypperpkg.py
|
|
|
|
+++ b/tests/unit/modules/test_zypperpkg.py
|
2023-05-05 11:15:58 +02:00
|
|
|
@@ -28,7 +28,10 @@ class ZyppCallMock:
|
2020-10-14 17:29:00 +02:00
|
|
|
|
|
|
|
def __call__(self, *args, **kwargs):
|
|
|
|
# If the call is for a configuration modifier, we return self
|
2021-01-08 13:41:50 +01:00
|
|
|
- if any(i in kwargs for i in ("no_repo_failure", "systemd_scope", "root")):
|
2020-10-14 17:29:00 +02:00
|
|
|
+ if any(
|
|
|
|
+ i in kwargs
|
|
|
|
+ for i in ("no_repo_failure", "ignore_not_found", "systemd_scope", "root")
|
|
|
|
+ ):
|
|
|
|
return self
|
|
|
|
return MagicMock(return_value=self.__return_value)()
|
|
|
|
|
2023-05-05 11:15:58 +02:00
|
|
|
@@ -1662,8 +1665,9 @@ class ZypperTestCase(TestCase, LoaderModuleMockMixin):
|
2020-10-14 17:29:00 +02:00
|
|
|
<solvable status="installed" name="libzypp" kind="package" edition="16.2.4-19.5" arch="x86_64" repository="(System Packages)"/>
|
|
|
|
</solvable-list></search-result></stream>
|
|
|
|
"""
|
|
|
|
- _zpr = MagicMock()
|
|
|
|
- _zpr.nolock.xml.call = MagicMock(return_value=minidom.parseString(xmldoc))
|
|
|
|
+ __zpr = MagicMock()
|
|
|
|
+ __zpr.nolock.xml.call.return_value = minidom.parseString(xmldoc)
|
|
|
|
+ _zpr = MagicMock(return_value=__zpr)
|
|
|
|
wcard = zypper.Wildcard(_zpr)
|
2021-01-08 13:41:50 +01:00
|
|
|
wcard.name, wcard.version = "libzypp", "*"
|
|
|
|
assert wcard._get_scope_versions(wcard._get_available_versions()) == [
|
2023-05-05 11:15:58 +02:00
|
|
|
@@ -1685,8 +1689,9 @@ class ZypperTestCase(TestCase, LoaderModuleMockMixin):
|
2020-10-14 17:29:00 +02:00
|
|
|
</solvable-list></search-result></stream>
|
|
|
|
"""
|
|
|
|
|
|
|
|
- _zpr = MagicMock()
|
|
|
|
- _zpr.nolock.xml.call = MagicMock(return_value=minidom.parseString(xmldoc))
|
|
|
|
+ __zpr = MagicMock()
|
|
|
|
+ __zpr.nolock.xml.call.return_value = minidom.parseString(xmldoc)
|
|
|
|
+ _zpr = MagicMock(return_value=__zpr)
|
|
|
|
wcard = zypper.Wildcard(_zpr)
|
2021-01-08 13:41:50 +01:00
|
|
|
wcard.name, wcard.version = "libzypp", "16.2.*-2*"
|
|
|
|
assert wcard._get_scope_versions(wcard._get_available_versions()) == [
|
2023-05-05 11:15:58 +02:00
|
|
|
@@ -1707,8 +1712,9 @@ class ZypperTestCase(TestCase, LoaderModuleMockMixin):
|
2020-10-14 17:29:00 +02:00
|
|
|
</solvable-list></search-result></stream>
|
|
|
|
"""
|
|
|
|
|
|
|
|
- _zpr = MagicMock()
|
|
|
|
- _zpr.nolock.xml.call = MagicMock(return_value=minidom.parseString(xmldoc))
|
|
|
|
+ __zpr = MagicMock()
|
|
|
|
+ __zpr.nolock.xml.call.return_value = minidom.parseString(xmldoc)
|
|
|
|
+ _zpr = MagicMock(return_value=__zpr)
|
|
|
|
wcard = zypper.Wildcard(_zpr)
|
2021-01-08 13:41:50 +01:00
|
|
|
wcard.name, wcard.version = "libzypp", "16.2.5*"
|
|
|
|
assert wcard._get_scope_versions(wcard._get_available_versions()) == [
|
2023-05-05 11:15:58 +02:00
|
|
|
@@ -1728,8 +1734,9 @@ class ZypperTestCase(TestCase, LoaderModuleMockMixin):
|
2020-10-14 17:29:00 +02:00
|
|
|
</solvable-list></search-result></stream>
|
|
|
|
"""
|
|
|
|
|
|
|
|
- _zpr = MagicMock()
|
|
|
|
- _zpr.nolock.xml.call = MagicMock(return_value=minidom.parseString(xmldoc))
|
|
|
|
+ __zpr = MagicMock()
|
|
|
|
+ __zpr.nolock.xml.call.return_value = minidom.parseString(xmldoc)
|
|
|
|
+ _zpr = MagicMock(return_value=__zpr)
|
|
|
|
wcard = zypper.Wildcard(_zpr)
|
2021-01-08 13:41:50 +01:00
|
|
|
wcard.name, wcard.version = "libzypp", "*.1"
|
|
|
|
assert wcard._get_scope_versions(wcard._get_available_versions()) == [
|
2023-05-05 11:15:58 +02:00
|
|
|
@@ -1750,8 +1757,9 @@ class ZypperTestCase(TestCase, LoaderModuleMockMixin):
|
2020-10-14 17:29:00 +02:00
|
|
|
<solvable status="other-version" name="libzypp" kind="package" edition="17.2.6-27.9.1" arch="x86_64" repository="foo"/>
|
|
|
|
</solvable-list></search-result></stream>
|
|
|
|
"""
|
|
|
|
- _zpr = MagicMock()
|
|
|
|
- _zpr.nolock.xml.call = MagicMock(return_value=minidom.parseString(xmldoc))
|
|
|
|
+ __zpr = MagicMock()
|
|
|
|
+ __zpr.nolock.xml.call.return_value = minidom.parseString(xmldoc)
|
|
|
|
+ _zpr = MagicMock(return_value=__zpr)
|
2021-01-08 13:41:50 +01:00
|
|
|
assert zypper.Wildcard(_zpr)("libzypp", "16.2.4*") == "16.2.4-19.5"
|
|
|
|
assert zypper.Wildcard(_zpr)("libzypp", "16.2*") == "16.2.5-25.1"
|
|
|
|
assert zypper.Wildcard(_zpr)("libzypp", "*6-*") == "17.2.6-27.9.1"
|
2023-05-05 11:15:58 +02:00
|
|
|
@@ -1770,8 +1778,10 @@ class ZypperTestCase(TestCase, LoaderModuleMockMixin):
|
2020-10-14 17:29:00 +02:00
|
|
|
<solvable status="other-version" name="libzypp" kind="package" edition="17.2.6-27.9.1" arch="x86_64" repository="foo"/>
|
|
|
|
</solvable-list></search-result></stream>
|
|
|
|
"""
|
|
|
|
- _zpr = MagicMock()
|
|
|
|
- _zpr.nolock.xml.call = MagicMock(return_value=minidom.parseString(xmldoc))
|
|
|
|
+ __zpr = MagicMock()
|
|
|
|
+ __zpr.nolock.xml.call.return_value = minidom.parseString(xmldoc)
|
|
|
|
+ _zpr = MagicMock(return_value=__zpr)
|
|
|
|
+
|
2021-01-08 13:41:50 +01:00
|
|
|
assert zypper.Wildcard(_zpr)("libzypp", None) is None
|
2020-10-14 17:29:00 +02:00
|
|
|
|
|
|
|
def test_wildcard_to_query_typecheck(self):
|
2023-05-05 11:15:58 +02:00
|
|
|
@@ -1787,8 +1797,9 @@ class ZypperTestCase(TestCase, LoaderModuleMockMixin):
|
2020-10-14 17:29:00 +02:00
|
|
|
<solvable status="other-version" name="libzypp" kind="package" edition="17.2.6-27.9.1" arch="x86_64" repository="foo"/>
|
|
|
|
</solvable-list></search-result></stream>
|
|
|
|
"""
|
|
|
|
- _zpr = MagicMock()
|
|
|
|
- _zpr.nolock.xml.call = MagicMock(return_value=minidom.parseString(xmldoc))
|
|
|
|
+ __zpr = MagicMock()
|
|
|
|
+ __zpr.nolock.xml.call.return_value = minidom.parseString(xmldoc)
|
|
|
|
+ _zpr = MagicMock(return_value=__zpr)
|
2021-01-08 13:41:50 +01:00
|
|
|
assert isinstance(zypper.Wildcard(_zpr)("libzypp", "*.1"), str)
|
2020-10-14 17:29:00 +02:00
|
|
|
|
|
|
|
def test_wildcard_to_query_condition_preservation(self):
|
2023-05-05 11:15:58 +02:00
|
|
|
@@ -1804,8 +1815,9 @@ class ZypperTestCase(TestCase, LoaderModuleMockMixin):
|
2020-10-14 17:29:00 +02:00
|
|
|
<solvable status="other-version" name="libzypp" kind="package" edition="17.2.6-27.9.1" arch="x86_64" repository="foo"/>
|
|
|
|
</solvable-list></search-result></stream>
|
|
|
|
"""
|
|
|
|
- _zpr = MagicMock()
|
|
|
|
- _zpr.nolock.xml.call = MagicMock(return_value=minidom.parseString(xmldoc))
|
|
|
|
+ __zpr = MagicMock()
|
|
|
|
+ __zpr.nolock.xml.call.return_value = minidom.parseString(xmldoc)
|
|
|
|
+ _zpr = MagicMock(return_value=__zpr)
|
|
|
|
|
|
|
|
for op in zypper.Wildcard.Z_OP:
|
2021-01-08 13:41:50 +01:00
|
|
|
assert zypper.Wildcard(_zpr)(
|
2023-05-05 11:15:58 +02:00
|
|
|
@@ -1831,8 +1843,10 @@ class ZypperTestCase(TestCase, LoaderModuleMockMixin):
|
2020-10-14 17:29:00 +02:00
|
|
|
<solvable status="other-version" name="libzypp" kind="package" edition="17.2.6-27.9.1" arch="x86_64" repository="foo"/>
|
|
|
|
</solvable-list></search-result></stream>
|
|
|
|
"""
|
|
|
|
- _zpr = MagicMock()
|
|
|
|
- _zpr.nolock.xml.call = MagicMock(return_value=minidom.parseString(xmldoc))
|
|
|
|
+ __zpr = MagicMock()
|
|
|
|
+ __zpr.nolock.xml.call.return_value = minidom.parseString(xmldoc)
|
|
|
|
+ _zpr = MagicMock(return_value=__zpr)
|
|
|
|
+
|
|
|
|
with self.assertRaises(CommandExecutionError):
|
2021-01-08 13:41:50 +01:00
|
|
|
for op in [">>", "==", "<<", "+"]:
|
|
|
|
zypper.Wildcard(_zpr)("libzypp", "{}*.1".format(op))
|
2023-05-05 11:15:58 +02:00
|
|
|
@@ -1958,3 +1972,38 @@ pattern() = package-c"""
|
|
|
|
self.assertFalse(zypper.__zypper__._is_rpm_lock())
|
|
|
|
self.assertEqual(lockf_mock.call_count, 2)
|
|
|
|
zypper.__zypper__._reset()
|
2020-10-14 17:29:00 +02:00
|
|
|
+
|
|
|
|
+ def test_search(self):
|
|
|
|
+ """Test zypperpkg.search()"""
|
|
|
|
+ xml_mock = MagicMock(return_value=[])
|
|
|
|
+ zypp_mock = MagicMock(return_value=xml_mock)
|
|
|
|
+ ZyppCallMock(return_value=xml_mock)
|
|
|
|
+ with patch("salt.modules.zypperpkg.__zypper__", zypp_mock):
|
|
|
|
+ zypper.search("emacs")
|
|
|
|
+ zypp_mock.assert_called_with(root=None, ignore_not_found=True)
|
|
|
|
+ xml_mock.nolock.noraise.xml.call.assert_called_with("search", "emacs")
|
|
|
|
+
|
|
|
|
+ def test_search_not_found(self):
|
|
|
|
+ """Test zypperpkg.search()"""
|
|
|
|
+ ret = {
|
|
|
|
+ "stdout": "<?xml version='1.0'?><stream></stream>",
|
|
|
|
+ "stderr": None,
|
|
|
|
+ "retcode": 104,
|
|
|
|
+ }
|
|
|
|
+ run_all_mock = MagicMock(return_value=ret)
|
|
|
|
+ with patch.dict(zypper.__salt__, {"cmd.run_all": run_all_mock}):
|
|
|
|
+ self.assertRaises(CommandExecutionError, zypper.search, "vim")
|
|
|
|
+ run_all_mock.assert_called_with(
|
|
|
|
+ [
|
|
|
|
+ "zypper",
|
|
|
|
+ "--non-interactive",
|
|
|
|
+ "--xmlout",
|
|
|
|
+ "--no-refresh",
|
|
|
|
+ "search",
|
|
|
|
+ "vim",
|
|
|
|
+ ],
|
|
|
|
+ success_retcodes=[104],
|
|
|
|
+ output_loglevel="trace",
|
|
|
|
+ python_shell=False,
|
|
|
|
+ env={"ZYPP_READONLY_HACK": "1"},
|
|
|
|
+ )
|
|
|
|
--
|
2023-05-05 11:15:58 +02:00
|
|
|
2.39.2
|
2020-10-14 17:29:00 +02:00
|
|
|
|
|
|
|
|