fixes saltstack/salt#63985 add pkg.installed show installable version in test mode

This commit is contained in:
nicholasmhughes 2023-03-28 15:36:03 -04:00 committed by Megan Wilhite
parent eaeccf72c6
commit b93e9a9e2d
3 changed files with 35 additions and 6 deletions

1
changelog/63985.added.md Normal file
View file

@ -0,0 +1 @@
Add pkg.installed show installable version in test mode

View file

@ -1007,6 +1007,8 @@ def installed(
**kwargs
):
"""
.. versionchanged:: 3007.0
Ensure that the package is installed, and that it is the correct version
(if specified).
@ -1804,12 +1806,36 @@ def installed(
changes = {}
if __opts__["test"]:
if targets:
if sources:
_targets = targets
else:
_targets = [_get_desired_pkg(x, targets) for x in 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": "installed", "old": ""} for x in targets})
changes.update(
{
x: {"new": installable_versions.get(x) or "installed", "old": ""}
for x in targets
}
)
comment.append(
"The following packages would be installed/updated: {}".format(summary)
)

View file

@ -546,16 +546,18 @@ def test_installed_with_changes_test_true(list_pkgs):
Test pkg.installed with simulated changes
"""
latest_pkgs = MagicMock(return_value="some version here")
list_pkgs = MagicMock(return_value=list_pkgs)
with patch.dict(
pkg.__salt__,
{
"pkg.latest_version": latest_pkgs,
"pkg.list_pkgs": list_pkgs,
},
):
expected = {"dummy": {"new": "installed", "old": ""}}
expected = {"dummy": {"new": "some version here", "old": ""}}
# Run state with test=true
with patch.dict(pkg.__opts__, {"test": True}):
ret = pkg.installed("dummy", test=True)