sysctl.persist should check in-memory values event if config is correct

On FreeBSD, it ignored the in-memory value if the on-disk value is
correct.
This commit is contained in:
Alan Somers 2022-08-11 13:45:01 -06:00 committed by Megan Wilhite
parent 98d1cdb972
commit 52b9a29ea3
3 changed files with 47 additions and 1 deletions

1
changelog/62461.fixed Normal file
View file

@ -0,0 +1 @@
sysctl.persist now updates the in-memory value on FreeBSD even if the on-disk value was already correct.

View file

@ -167,7 +167,12 @@ def persist(name, value, config="/etc/sysctl.conf"):
rest_v = rest.split()[0] rest_v = rest.split()[0]
rest = rest[len(rest_v) :] rest = rest[len(rest_v) :]
if rest_v == value: 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) new_line = _formatfor(key, value, config, rest)
nlines.append(new_line) nlines.append(new_line)
edited = True edited = True

View file

@ -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(): def test_persist_updated():
""" """
Tests sysctl.conf success Tests sysctl.conf success