Fix virt states results when no change happened

This commit is contained in:
Cédric Bosdonnat 2021-05-05 09:48:13 +02:00 committed by Megan Wilhite
parent 5dfb901bcb
commit 641c51f659
3 changed files with 14 additions and 13 deletions

1
changelog/59739.fixed Normal file
View file

@ -0,0 +1 @@
Return empty changes when nothing has been done in virt.defined and virt.running states

View file

@ -670,6 +670,7 @@ def defined(
)
ret["changes"][name] = status
if not status.get("definition"):
ret["changes"] = {}
ret["comment"] = "Domain {} unchanged".format(name)
ret["result"] = True
elif status.get("errors"):
@ -1026,7 +1027,7 @@ def running(
result = True if not __opts__["test"] else None
if ret["result"] is None or ret["result"]:
changed = ret["changes"][name].get("definition", False)
changed = ret["changes"].get(name, {}).get("definition", False)
try:
domain_state = __salt__["virt.vm_state"](name)
if domain_state.get(name) != "running":
@ -1041,6 +1042,8 @@ def running(
if not ret["comment"].endswith("unchanged"):
comment = "{} and started".format(ret["comment"])
ret["comment"] = comment
if name not in ret["changes"]:
ret["changes"][name] = {}
ret["changes"][name]["started"] = True
elif not changed:
ret["comment"] = "Domain {} exists and is running".format(name)

View file

@ -23,7 +23,7 @@ def test_defined_no_change(test):
):
assert virt.defined("myvm") == {
"name": "myvm",
"changes": {"myvm": {"definition": False}},
"changes": {},
"result": True,
"comment": "Domain myvm unchanged",
}
@ -274,16 +274,13 @@ def test_running_no_change(test, running):
"virt.list_domains": MagicMock(return_value=["myvm"]),
},
):
changes = {"definition": False}
comment = "Domain myvm exists and is running"
if running == "shutdown":
changes["started"] = True
comment = "Domain myvm started"
assert virt.running("myvm") == {
"name": "myvm",
"result": True,
"changes": {"myvm": changes},
"comment": comment,
"changes": {"myvm": {"started": True}} if running == "shutdown" else {},
"comment": "Domain myvm started"
if running == "shutdown"
else "Domain myvm exists and is running",
}
if running == "shutdown" and not test:
start_mock.assert_called()
@ -414,7 +411,7 @@ def test_running_start_error():
):
assert virt.running("myvm") == {
"name": "myvm",
"changes": {"myvm": {"definition": False}},
"changes": {},
"result": False,
"comment": "libvirt error msg",
}
@ -438,12 +435,12 @@ def test_running_update(test, running):
"virt.list_domains": MagicMock(return_value=["myvm"]),
},
):
changes = {"myvm": {"definition": True, "cpu": True}}
changes = {"definition": True, "cpu": True}
if running == "shutdown":
changes["myvm"]["started"] = True
changes["started"] = True
assert virt.running("myvm", cpu=2) == {
"name": "myvm",
"changes": changes,
"changes": {"myvm": changes},
"result": True if not test else None,
"comment": "Domain myvm updated"
if running == "running"