Skip to content

Commit

Permalink
sysctl.persist should check in-memory values event if config is correct
Browse files Browse the repository at this point in the history
On FreeBSD, it ignored the in-memory value if the on-disk value is
correct.
  • Loading branch information
asomers authored and Megan Wilhite committed Sep 27, 2022
1 parent 98d1cdb commit 52b9a29
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/62461.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sysctl.persist now updates the in-memory value on FreeBSD even if the on-disk value was already correct.
7 changes: 6 additions & 1 deletion salt/modules/freebsd_sysctl.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,12 @@ def persist(name, value, config="/etc/sysctl.conf"):
rest_v = rest.split()[0]
rest = rest[len(rest_v) :]
if rest_v == value:
return "Already set"
# If it is correct in the config file, check in memory
if str(get(name)) != value:
assign(name, value)
return "Updated"
else:
return "Already set"
new_line = _formatfor(key, value, config, rest)
nlines.append(new_line)
edited = True
Expand Down
40 changes: 40 additions & 0 deletions tests/pytests/unit/modules/test_freebsd_sysctl.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,46 @@ def test_persist_no_conf_failure():
)


def test_persist_nochange():
"""
Tests success when no changes need to be made
"""
mock_get_cmd = MagicMock(return_value="1")
content = "vfs.usermount=1\n"
with patch("salt.utils.files.fopen", mock_open(read_data=content)):
with patch.dict(
freebsd_sysctl.__salt__,
{"cmd.run": mock_get_cmd},
):
assert freebsd_sysctl.persist("vfs.usermount", 1) == "Already set"


def test_persist_in_memory():
"""
Tests success when the on-disk value is correct but the in-memory value
needs updating.
"""
mock_get_cmd = MagicMock(return_value="0")
set_cmd = {
"pid": 1337,
"retcode": 0,
"stderr": "",
"stdout": "vfs.usermount: 0 -> 1",
}
mock_set_cmd = MagicMock(return_value=set_cmd)
content = "vfs.usermount=1\n"
with patch("salt.utils.files.fopen", mock_open(read_data=content)):
with patch.dict(
freebsd_sysctl.__salt__,
{"cmd.run": mock_get_cmd},
):
with patch.dict(
freebsd_sysctl.__salt__,
{"cmd.run_all": mock_set_cmd},
):
assert freebsd_sysctl.persist("vfs.usermount", 1) == "Updated"


def test_persist_updated():
"""
Tests sysctl.conf success
Expand Down

0 comments on commit 52b9a29

Please sign in to comment.