add pkg.latest show installable version in test mode

This commit is contained in:
nicholasmhughes 2023-04-02 17:01:57 -04:00 committed by Megan Wilhite
parent b93e9a9e2d
commit e4ba3fd7d2
2 changed files with 96 additions and 31 deletions

View file

@ -989,6 +989,61 @@ def _resolve_capabilities(pkgs, refresh=False, **kwargs):
return ret, False
def _get_installable_versions(targets, current=None):
"""
.. versionadded:: 3007.0
Return a dictionary of changes that will be made to install a version of
each target package specified in the ``targets`` dictionary. If ``current``
is specified, it should be a dictionary of package names to currently
installed versions. The function returns a dictionary of changes, where the
keys are the package names and the values are dictionaries with two keys:
"old" and "new". The value for "old" is the currently installed version (if
available) or an empty string, and the value for "new" is the latest
available version of the package or "installed".
:param targets: A dictionary where the keys are package names and the
values indicate a specific version or ``None`` if the
latest should be used.
:type targets: dict
:param current: A dictionary where the keys are package names and the
values are currently installed versions.
:type current: dict or None
:return: A dictionary of changes to be made to install a version of
each package.
:rtype: dict
"""
if current is None:
current = {}
changes = installable_versions = {}
latest_targets = [_get_desired_pkg(x, targets) for x, y in targets.items() if not y]
latest_versions = __salt__["pkg.latest_version"](*latest_targets)
if latest_targets:
# single pkg returns str
if isinstance(latest_versions, str):
installable_versions = {latest_targets[0]: latest_versions}
elif isinstance(latest_versions, dict):
installable_versions = latest_versions
explicit_targets = [
_get_desired_pkg(x, targets) for x in targets if x not in latest_targets
]
if explicit_targets:
explicit_versions = __salt__["pkg.list_repo_pkgs"](*explicit_targets)
for tgt, ver_list in explicit_versions.items():
if ver_list:
installable_versions[tgt] = ver_list[0]
changes.update(
{
x: {
"new": installable_versions.get(x) or "installed",
"old": current.get(x, ""),
}
for x in targets
}
)
return changes
def installed(
name,
version=None,
@ -1806,36 +1861,14 @@ def installed(
changes = {}
if __opts__["test"]:
if targets:
installable_versions = {}
if not sources:
latest_targets = [
_get_desired_pkg(x, targets) for x, y in targets.items() if not y
]
latest_versions = __salt__["pkg.latest_version"](*latest_targets)
# single pkg returns str
if isinstance(latest_versions, str):
installable_versions = {latest_targets[0]: latest_versions}
elif isinstance(latest_versions, dict):
installable_versions = latest_versions
explicit_targets = [
_get_desired_pkg(x, targets)
for x in targets
if x not in latest_targets
]
if explicit_targets:
explicit_versions = __salt__["pkg.list_repo_pkgs"](
*explicit_targets
)
for tgt, ver_list in explicit_versions.items():
if ver_list:
installable_versions[tgt] = ver_list[0]
summary = ", ".join(targets)
changes.update(
{
x: {"new": installable_versions.get(x) or "installed", "old": ""}
for x in targets
if sources:
installable_versions = {
x: {"new": "installed", "old": ""} for x in targets
}
)
else:
installable_versions = _get_installable_versions(targets)
changes.update(installable_versions)
summary = ", ".join(targets)
comment.append(
"The following packages would be installed/updated: {}".format(summary)
)
@ -2486,6 +2519,8 @@ def latest(
**kwargs
):
"""
.. versionchanged:: 3007.0
Ensure that the named package is installed and the latest available
package. If the package can be updated, this state function will update
the package. Generally it is better for the
@ -2764,10 +2799,10 @@ def latest(
comments.append(
"{} packages are already up-to-date".format(up_to_date_count)
)
changes = _get_installable_versions(targets, cur)
return {
"name": name,
"changes": {},
"changes": changes,
"result": None,
"comment": "\n".join(comments),
}

View file

@ -1015,3 +1015,33 @@ def test_installed_with_single_normalize_32bit():
assert "xz-devel.i686" in call_yum_mock.mock_calls[0].args[0]
assert ret["result"]
assert ret["changes"] == expected
def test__get_installable_versions_no_version_found():
mock_latest_versions = MagicMock(return_value={})
mock_list_repo_pkgs = MagicMock(return_value={})
with patch.dict(
pkg.__salt__,
{
"pkg.latest_version": mock_latest_versions,
"pkg.list_pkgs": mock_list_repo_pkgs,
},
), patch.dict(pkg.__opts__, {"test": True}):
expected = {"dummy": {"new": "installed", "old": ""}}
ret = pkg._get_installable_versions({"dummy": None}, current=None)
assert ret == expected
def test__get_installable_versions_version_found():
mock_latest_versions = MagicMock(return_value={"dummy": "1.0.1"})
mock_list_repo_pkgs = MagicMock(return_value={})
with patch.dict(
pkg.__salt__,
{
"pkg.latest_version": mock_latest_versions,
"pkg.list_pkgs": mock_list_repo_pkgs,
},
), patch.dict(pkg.__opts__, {"test": True}):
expected = {"dummy": {"new": "1.0.1", "old": ""}}
ret = pkg._get_installable_versions({"dummy": None}, current=None)
assert ret == expected