Fix the logic in pkgng.latest_version when querying by origin

The old version was doubly-wrong: internally it could select the wrong
package, but then it would always through away the version and return
nothing.

Also, the new pkg invocation ensures that pkg's output will always return
0-1 lines, allowing us to simplify the parsing.  This fixes a second
bug, one that would raise an IndexError if the package was not found.

Fixes #60105
Fixes #60108
This commit is contained in:
Alan Somers 2021-04-29 16:46:47 -06:00 committed by Megan Wilhite
parent b70f47d627
commit 2c201e23f7
4 changed files with 17 additions and 35 deletions

1
changelog/60105.fixed Normal file
View file

@ -0,0 +1 @@
Fixed an IndexError in pkgng.latest_version when querying an unknown package.

1
changelog/60108.fixed Normal file
View file

@ -0,0 +1 @@
Fixed pkgng.latest_version when querying by origin (e.g. "shells/bash").

View file

@ -299,32 +299,21 @@ def latest_version(*names, **kwargs):
pkgs = list_pkgs(versions_as_list=True, jail=jail, chroot=chroot, root=root)
for name in names:
# FreeBSD supports packages in format java/openjdk7
cmd = _pkg(jail, chroot, root) + ["search", "-eqS"]
if "/" in name:
cmd = _pkg(jail, chroot, root) + ["search"]
# FreeBSD's pkg supports searching by origin, like java/openjdk7
cmd.append("origin")
else:
cmd = _pkg(jail, chroot, root) + [
"search",
"-S",
"name",
"-e",
]
cmd.append("-q")
cmd.append("name")
if not salt.utils.data.is_true(refresh):
cmd.append("-U")
cmd.append(name)
pkgver = _get_version(
name,
sorted(
__salt__["cmd.run"](
cmd, python_shell=False, output_loglevel="trace"
).splitlines(),
reverse=True,
).pop(0),
)
if pkgver is not None:
pkg_output = __salt__["cmd.run"](
cmd, python_shell=False, output_loglevel="trace"
);
if pkg_output != "":
pkgver = pkg_output.rsplit("-", 1)[1]
installed = pkgs.get(name, [])
if not installed:
ret[name] = pkgver

View file

@ -34,24 +34,19 @@ def test_latest_version(pkgs_as_list):
with patch.dict(pkgng.__salt__, {"cmd.run": search_cmd}):
result = pkgng.latest_version("bash")
search_cmd.assert_called_with(
["pkg", "search", "-S", "name", "-e", "-q", "-U", "bash"], output_loglevel='trace', python_shell=False
["pkg", "search", "-eqS", "name", "-U", "bash"], output_loglevel='trace', python_shell=False
)
assert result == "5.1.4"
@pytest.mark.xfail(
strict=True,
reason="https://github.com/saltstack/salt/issues/60108"
)
def test_latest_version_origin(pkgs_as_list):
"Test pkgng.latest_version with a specific package origin"
pkgs_mock = MagicMock(side_effect=pkgs_as_list)
search_cmd = MagicMock(return_value="bash-5.1.4_2\nbash-completion-2.11,2\nbash-static-5.1.4_2,bashc-5.0.9")
search_cmd = MagicMock(return_value="bash-5.1.4_2")
with patch("salt.modules.pkgng.list_pkgs", pkgs_mock):
with patch.dict(pkgng.__salt__, {"cmd.run": search_cmd}):
result = pkgng.latest_version("shells/bash")
search_cmd.assert_called_with(
# TODO: use -S origin. See #60108
["pkg", "search", "-q", "-U", "shells/bash"], output_loglevel='trace', python_shell=False
["pkg", "search", "-eqS", "origin", "-U", "shells/bash"], output_loglevel='trace', python_shell=False
)
assert result == "5.1.4_2"
@ -63,14 +58,10 @@ def test_latest_version_outofdatedate(pkgs_as_list):
with patch.dict(pkgng.__salt__, {"cmd.run": search_cmd}):
result = pkgng.latest_version("openvpn")
search_cmd.assert_called_with(
["pkg", "search", "-S", "name", "-e", "-q", "-U", "openvpn"], output_loglevel='trace', python_shell=False
["pkg", "search", "-eqS", "name", "-U", "openvpn"], output_loglevel='trace', python_shell=False
)
assert result == "2.4.8_3"
@pytest.mark.xfail(
strict=True,
reason="https://github.com/saltstack/salt/issues/60105"
)
def test_latest_version_unavailable(pkgs_as_list):
"Test pkgng.latest_version when the requested package is not available"
pkgs_mock = MagicMock(side_effect=pkgs_as_list)
@ -79,7 +70,7 @@ def test_latest_version_unavailable(pkgs_as_list):
with patch.dict(pkgng.__salt__, {"cmd.run": search_cmd}):
result = pkgng.latest_version("does_not_exist")
search_cmd.assert_called_with(
["pkg", "search", "-S", "name", "-e", "-q", "-U", "does_not_exist"], output_loglevel='trace', python_shell=False
["pkg", "search", "-eqS", "name", "-U", "does_not_exist"], output_loglevel='trace', python_shell=False
)
def test_latest_version_uptodate(pkgs_as_list):
@ -90,7 +81,7 @@ def test_latest_version_uptodate(pkgs_as_list):
with patch.dict(pkgng.__salt__, {"cmd.run": search_cmd}):
result = pkgng.latest_version("openvpn")
search_cmd.assert_called_with(
["pkg", "search", "-S", "name", "-e", "-q", "-U", "openvpn"], output_loglevel='trace', python_shell=False
["pkg", "search", "-eqS", "name", "-U", "openvpn"], output_loglevel='trace', python_shell=False
)
assert result == ""