Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/66440.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed ``TypeError: argument of type 'NoneType' is not iterable`` in beacon state functions (``present``, ``absent``, ``enabled``, ``disabled``) when ``beacons.list`` returns ``None`` due to event-system failure. The return value is now defaulted to an empty dict before iteration.
8 changes: 8 additions & 0 deletions salt/states/beacon.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ def present(name, save=False, **kwargs):
ret = {"name": name, "result": True, "changes": {}, "comment": []}

current_beacons = __salt__["beacons.list"](return_yaml=False, **kwargs)
if current_beacons is None:
current_beacons = {}
beacon_data = [{k: v} for k, v in kwargs.items()]

if name in current_beacons:
Expand Down Expand Up @@ -172,6 +174,8 @@ def absent(name, save=False, **kwargs):
ret = {"name": name, "result": True, "changes": {}, "comment": []}

current_beacons = __salt__["beacons.list"](return_yaml=False, **kwargs)
if current_beacons is None:
current_beacons = {}
if name in current_beacons:
if __opts__.get("test"):
kwargs["test"] = True
Expand Down Expand Up @@ -219,6 +223,8 @@ def enabled(name, **kwargs):
ret = {"name": name, "result": True, "changes": {}, "comment": []}

current_beacons = __salt__["beacons.list"](return_yaml=False, **kwargs)
if current_beacons is None:
current_beacons = {}
if name in current_beacons:
if __opts__.get("test"):
kwargs["test"] = True
Expand Down Expand Up @@ -259,6 +265,8 @@ def disabled(name, **kwargs):
ret = {"name": name, "result": True, "changes": {}, "comment": []}

current_beacons = __salt__["beacons.list"](return_yaml=False, **kwargs)
if current_beacons is None:
current_beacons = {}
if name in current_beacons:
if __opts__.get("test"):
kwargs["test"] = True
Expand Down
42 changes: 42 additions & 0 deletions tests/pytests/unit/states/test_beacon.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,29 @@ def test_present():
assert beacon.present(beacon_name) == ret


def test_present_none_beacons():
"""
Test present when beacons.list returns None.
"""
beacon_name = "test_beacon"

mock_mod = MagicMock(
return_value={"name": beacon_name, "result": True, "changes": {}, "comment": []}
)
mock_lst = MagicMock(return_value=None)
with patch.dict(
beacon.__salt__,
{
"beacons.list": mock_lst,
"beacons.add": mock_mod,
},
):
with patch.dict(beacon.__opts__, {"test": False}):
ret = beacon.present(beacon_name)
assert ret["result"] is True
assert ret["comment"] == f"Adding {beacon_name} to beacons"


def test_absent():
"""
Test to ensure a job is absent from the schedule.
Expand All @@ -61,3 +84,22 @@ def test_absent():
comt = "ps not configured in beacons"
ret.update({"comment": comt, "result": True})
assert beacon.absent(beacon_name) == ret


def test_absent_none_beacons():
"""
Test absent when beacons.list returns None.
"""
beacon_name = "test_beacon"

mock_lst = MagicMock(return_value=None)
with patch.dict(
beacon.__salt__,
{
"beacons.list": mock_lst,
},
):
with patch.dict(beacon.__opts__, {"test": False}):
ret = beacon.absent(beacon_name)
assert ret["result"] is True
assert f"{beacon_name} not configured in beacons" in ret["comment"]
Loading