Skip to content

Commit

Permalink
Allow setting None for leaf nodes (#121)
Browse files Browse the repository at this point in the history
* Add support for settings with None values

* add tests + bugfix

* revert changes on module_index

* some Linting

* Apply suggestions from code review

Co-authored-by: Fabio Bertagna <[email protected]>

* revision (separate tests)

* make the linter not cry

---------

Co-authored-by: Fabio Bertagna <[email protected]>
  • Loading branch information
LuminatiHD and DonGiovanni83 authored May 3, 2024
1 parent 4365c7e commit 8a0a23b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
8 changes: 6 additions & 2 deletions plugins/module_utils/config_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,8 +435,12 @@ def set(self, value: str, setting: str) -> None:
_setting: Element = self._config_xml_tree.find(xpath)

# If the element is present we will verify it's .text value
elif _setting.text in [None, "", " "]:
raise NotImplementedError("Currently only text settings supported")
elif _setting.text is None or _setting.text.strip() == "":
# check if setting has children
if list(_setting):
raise AttributeError(
f"Cannot assign value to node '{_setting}' with child elements."
)

_setting.text = value

Expand Down
47 changes: 47 additions & 0 deletions tests/unit/plugins/module_utils/test_config_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@
},
},
},
"test_module_4": {
"hasync_parent": "hasync",
"remote_system_username": "hasync/username",
"php_requirements": ["req_1", "req_2"],
"configure_functions": {
"test_configure_function": {
"name": "test_configure_function",
"configure_params": ["param_1"],
},
},
},
"missing_php_requirements": {
"setting_1": "settings/one",
"setting_2": "settings/two",
Expand Down Expand Up @@ -105,6 +116,9 @@
<one>1</one>
<two>2</two>
</settings>
<hasync>
<username />
</hasync>
</opnsense>
"""

Expand Down Expand Up @@ -527,3 +541,36 @@ def test_set_with_missing_element(sample_config_path):
},
}
new_config.save()


def test_fail_set_on_parent_node(sample_config_path):
"""
Test case to verify that setting a value for a parent node will fail.
Args:
- sample_config_path (str): The path to the temporary test configuration file.
"""
with OPNsenseModuleConfig(
module_name="test_module_4",
config_context_names=["test_module_4"],
path=sample_config_path,
check_mode=False,
) as new_config:
with pytest.raises(AttributeError):
new_config.set("test", "hasync_parent")


def test_success_set_on_empty_leaf_node(sample_config_path):
"""
Test case to verify that setting a leaf node with a value of None will succeed.
- sample_config_path (str): The path to the temporary test configuration file.
"""
with OPNsenseModuleConfig(
module_name="test_module_4",
config_context_names=["test_module_4"],
path=sample_config_path,
check_mode=False,
) as new_config:
new_config.set("test", "remote_system_username")
assert new_config.get("remote_system_username").text == "test"
new_config.save()

0 comments on commit 8a0a23b

Please sign in to comment.