Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Sep 27, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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/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