From f12b73cee0ab3216f182c7b68f0baac8a2b075c0 Mon Sep 17 00:00:00 2001 From: "Gary T. Giesen" Date: Sat, 4 Jul 2026 16:29:54 -0400 Subject: [PATCH] Fix thorium reg.list crash on non-string add value reg.list_() called add.split(",") whenever add was not a list, which raised "AttributeError: 'int' object has no attribute 'split'" when an integer (or other scalar) was passed via SLS. Only split strings and wrap any other non-list scalar in a single-element list instead. Fixes #43364 --- changelog/43364.fixed.md | 1 + salt/thorium/reg.py | 4 ++- tests/pytests/unit/thorium/__init__.py | 0 tests/pytests/unit/thorium/test_reg.py | 49 ++++++++++++++++++++++++++ 4 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 changelog/43364.fixed.md create mode 100644 tests/pytests/unit/thorium/__init__.py create mode 100644 tests/pytests/unit/thorium/test_reg.py diff --git a/changelog/43364.fixed.md b/changelog/43364.fixed.md new file mode 100644 index 000000000000..b11330e00e99 --- /dev/null +++ b/changelog/43364.fixed.md @@ -0,0 +1 @@ +Fixed thorium reg.list to accept a non-string, non-list add value (such as an integer) instead of raising AttributeError. diff --git a/salt/thorium/reg.py b/salt/thorium/reg.py index 57842202cdfa..b187037f1b2d 100644 --- a/salt/thorium/reg.py +++ b/salt/thorium/reg.py @@ -60,8 +60,10 @@ def list_(name, add, match, stamp=False, prune=0): - stamp: True """ ret = {"name": name, "changes": {}, "comment": "", "result": True} - if not isinstance(add, list): + if isinstance(add, str): add = add.split(",") + elif not isinstance(add, list): + add = [add] if name not in __reg__: __reg__[name] = {} __reg__[name]["val"] = [] diff --git a/tests/pytests/unit/thorium/__init__.py b/tests/pytests/unit/thorium/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/tests/pytests/unit/thorium/test_reg.py b/tests/pytests/unit/thorium/test_reg.py new file mode 100644 index 000000000000..b726536f3e67 --- /dev/null +++ b/tests/pytests/unit/thorium/test_reg.py @@ -0,0 +1,49 @@ +""" + tests.pytests.unit.thorium.test_reg + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + + Unit tests for the thorium reg module +""" + +import pytest + +import salt.thorium.reg as reg + + +@pytest.fixture +def setup_reg_dunders(): + # The thorium loader injects ``__reg__`` and ``__events__`` as module + # globals. Set them directly since they are not standard salt dunders + # handled by the loader mock fixture. + reg.__dict__["__reg__"] = {} + reg.__dict__["__events__"] = [ + { + "tag": "phil/was/here", + "data": {"data": {42: "the answer"}, "_stamp": "2017-09-06"}, + } + ] + try: + yield + finally: + reg.__dict__.pop("__reg__", None) + reg.__dict__.pop("__events__", None) + + +def test_list_integer_add(setup_reg_dunders): + """ + An integer ``add`` value must not raise AttributeError (no .split on int) + and the integer key should be looked up in the event data. + """ + ret = reg.list_("myregister", add=42, match="phil/was/here") + assert ret["result"] is True + assert reg.__dict__["__reg__"]["myregister"]["val"] == [{42: "the answer"}] + + +def test_list_string_add_still_splits(setup_reg_dunders): + """ + A comma separated string ``add`` value must still be split into keys. + """ + reg.__dict__["__events__"][0]["data"]["data"] = {"a": 1, "b": 2} + ret = reg.list_("myregister", add="a,b", match="phil/was/here") + assert ret["result"] is True + assert reg.__dict__["__reg__"]["myregister"]["val"] == [{"a": 1, "b": 2}]