From 52b9a29ea3c11df04b9ad195730f38acc8b5129b Mon Sep 17 00:00:00 2001 From: Alan Somers Date: Thu, 11 Aug 2022 13:45:01 -0600 Subject: [PATCH] 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. --- changelog/62461.fixed | 1 + salt/modules/freebsd_sysctl.py | 7 +++- .../unit/modules/test_freebsd_sysctl.py | 40 +++++++++++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 changelog/62461.fixed diff --git a/changelog/62461.fixed b/changelog/62461.fixed new file mode 100644 index 00000000000..4f8f76ad943 --- /dev/null +++ b/changelog/62461.fixed @@ -0,0 +1 @@ +sysctl.persist now updates the in-memory value on FreeBSD even if the on-disk value was already correct. diff --git a/salt/modules/freebsd_sysctl.py b/salt/modules/freebsd_sysctl.py index 1cdc91d941c..3d6dd930ec5 100644 --- a/salt/modules/freebsd_sysctl.py +++ b/salt/modules/freebsd_sysctl.py @@ -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 diff --git a/tests/pytests/unit/modules/test_freebsd_sysctl.py b/tests/pytests/unit/modules/test_freebsd_sysctl.py index 8f14e829ef1..ad62afb737e 100644 --- a/tests/pytests/unit/modules/test_freebsd_sysctl.py +++ b/tests/pytests/unit/modules/test_freebsd_sysctl.py @@ -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