Skip to content

Commit

Permalink
fixes saltstack#63985 add pkg.installed show installable version in t…
Browse files Browse the repository at this point in the history
…est mode
  • Loading branch information
nicholasmhughes committed Mar 28, 2023
1 parent 8a1e4c1 commit 37ff2ab
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
1 change: 1 addition & 0 deletions changelog/63985.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add pkg.installed show installable version in test mode
36 changes: 31 additions & 5 deletions salt/states/pkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down Expand Up @@ -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)
)
Expand Down
4 changes: 3 additions & 1 deletion tests/pytests/unit/states/test_pkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 37ff2ab

Please sign in to comment.