Skip to content

Commit

Permalink
Reverting changes in PR #60150. Updating installed and removed functi…
Browse files Browse the repository at this point in the history
…ons to return changes when test=True.
  • Loading branch information
garethgreenaway authored and Megan Wilhite committed Oct 11, 2021
1 parent d2ded59 commit d1587a3
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 19 deletions.
25 changes: 20 additions & 5 deletions salt/states/pkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -1822,12 +1822,17 @@ def installed(
)

comment = []
changes = {"installed": {}}
if __opts__["test"]:
if targets:
if sources:
summary = ", ".join(targets)
_targets = targets
else:
summary = ", ".join([_get_desired_pkg(x, targets) for x in targets])
_targets = [_get_desired_pkg(x, targets) for x in targets]
summary = ", ".join(targets)
changes["installed"].update(
{x: {"new": "installed", "old": ""} for x in targets}
)
comment.append(
"The following packages would be installed/updated: {}".format(summary)
)
Expand All @@ -1836,6 +1841,9 @@ def installed(
"The following packages would have their selection status "
"changed from 'purge' to 'install': {}".format(", ".join(to_unpurge))
)
changes["installed"].update(
{x: {"new": "installed", "old": ""} for x in to_unpurge}
)
if to_reinstall:
# Add a comment for each package in to_reinstall with its
# pkg.verify output
Expand All @@ -1848,6 +1856,9 @@ def installed(
reinstall_targets.append(
_get_desired_pkg(reinstall_pkg, to_reinstall)
)
changes["installed"].update(
{x: {"new": "installed", "old": ""} for x in reinstall_targets}
)
msg = "The following packages would be reinstalled: "
msg += ", ".join(reinstall_targets)
comment.append(msg)
Expand All @@ -1861,18 +1872,18 @@ def installed(
"Package '{}' would be reinstalled because the "
"following files have been altered:".format(pkgstr)
)
changes["installed"].update({reinstall_pkg: {}})
comment.append(_nested_output(altered_files[reinstall_pkg]))
ret = {
"name": name,
"changes": {},
"changes": changes,
"result": None,
"comment": "\n".join(comment),
}
if warnings:
ret.setdefault("warnings", []).extend(warnings)
return ret

changes = {"installed": {}}
modified_hold = None
not_modified_hold = None
failed_hold = None
Expand Down Expand Up @@ -2948,9 +2959,13 @@ def _uninstall(
}

if __opts__["test"]:

_changes = {}
_changes.update({x: {"new": "{}d".format(action), "old": ""} for x in targets})

return {
"name": name,
"changes": {},
"changes": _changes,
"result": None,
"comment": "The following packages will be {}d: {}.".format(
action, ", ".join(targets)
Expand Down
13 changes: 0 additions & 13 deletions salt/states/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,19 +508,6 @@ def running(name, enable=None, sig=None, init_delay=None, **kwargs):
ret.update(_enable(name, None, **kwargs))
elif enable is False and before_toggle_enable_status:
ret.update(_disable(name, None, **kwargs))
else:
if __opts__["test"]:
ret["result"] = None
ret["comment"] = "\n".join(
[
_f
for _f in [
"The service {} is set to restart".format(name),
unmask_ret["comment"],
]
if _f
]
)
return ret

# Run the tests
Expand Down
60 changes: 60 additions & 0 deletions tests/pytests/unit/states/test_pkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ def pkgs():
}


@pytest.fixture(scope="module")
def list_pkgs():
return {
"pkga": "1.0.1",
"pkgb": "1.0.2",
"pkgc": "1.0.3",
}


def test_uptodate_with_changes(pkgs):
"""
Test pkg.uptodate with simulated changes
Expand Down Expand Up @@ -518,3 +527,54 @@ def test_mod_aggregate():
}
res = pkg.mod_aggregate(low, chunks, running)
assert res == expected


def test_installed_with_changes_test_true(list_pkgs):
"""
Test pkg.installed with simulated changes
"""

list_pkgs = MagicMock(return_value=list_pkgs)

with patch.dict(
pkg.__salt__,
{
"pkg.list_pkgs": list_pkgs,
},
):

expected = {"installed": {"dummy": {"new": "installed", "old": ""}}}
# Run state with test=true
with patch.dict(pkg.__opts__, {"test": True}):
ret = pkg.installed("dummy", test=True)
assert ret["result"] is None
assert ret["changes"] == expected


@pytest.mark.parametrize("action", ["removed", "purged"])
def test_removed_purged_with_changes_test_true(list_pkgs, action):
"""
Test pkg.removed with simulated changes
"""

list_pkgs = MagicMock(return_value=list_pkgs)

mock_parse_targets = MagicMock(return_value=[{"pkga": None}, "repository"])

with patch.dict(
pkg.__salt__,
{
"pkg.list_pkgs": list_pkgs,
"pkg_resource.parse_targets": mock_parse_targets,
"pkg_resource.version_clean": MagicMock(return_value=None),
},
):

expected = {"pkga": {"new": "{}".format(action), "old": ""}}
pkg_actions = {"removed": pkg.removed, "purged": pkg.purged}

# Run state with test=true
with patch.dict(pkg.__opts__, {"test": True}):
ret = pkg_actions[action]("pkga", test=True)
assert ret["result"] is None
assert ret["changes"] == expected
2 changes: 1 addition & 1 deletion tests/pytests/unit/states/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ def test_running():

with patch.dict(service.__opts__, {"test": True}):
with patch.dict(service.__salt__, {"service.status": tmock}):
assert service.running("salt") == ret[10]
assert service.running("salt") == ret[5]

with patch.dict(service.__salt__, {"service.status": fmock}):
assert service.running("salt") == ret[3]
Expand Down

0 comments on commit d1587a3

Please sign in to comment.