From db5456609e1be92d698eb33cb7d959cd006ae58c Mon Sep 17 00:00:00 2001 From: Adam Bolte Date: Wed, 28 Sep 2022 23:59:46 +1000 Subject: [PATCH] Avoid warning noise in logrotate.get (#56105) * Avoid warning noise in logrotate.get There is no guarantee that a lookup failure is necessarily a problem, so presenting a message to this effect as a warning has led to confusion for some (and annoyance for others). This resolves the issue by showing the message at the debug level. Fixes #53988 * Adding a test for changes to logrotate.get. * Fix pre-commit and add changelog Co-authored-by: Gareth J. Greenaway Co-authored-by: Megan Wilhite --- changelog/53988.fixed | 1 + salt/modules/logrotate.py | 2 +- tests/unit/modules/test_logrotate.py | 25 +++++++++++++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 changelog/53988.fixed diff --git a/changelog/53988.fixed b/changelog/53988.fixed new file mode 100644 index 000000000000..8220d4acddd6 --- /dev/null +++ b/changelog/53988.fixed @@ -0,0 +1 @@ +Avoid warning noise in lograte.get diff --git a/salt/modules/logrotate.py b/salt/modules/logrotate.py index d9801ab16241..314fc607cfca 100644 --- a/salt/modules/logrotate.py +++ b/salt/modules/logrotate.py @@ -159,7 +159,7 @@ def get(key, value=None, conf_file=_DEFAULT_CONF): if value: if stanza: return stanza.get(value, False) - _LOG.warning("Block '%s' not present or empty.", key) + _LOG.debug("Block '%s' not present or empty.", key) return stanza diff --git a/tests/unit/modules/test_logrotate.py b/tests/unit/modules/test_logrotate.py index 5f589a61d911..4cc0fc49db3a 100644 --- a/tests/unit/modules/test_logrotate.py +++ b/tests/unit/modules/test_logrotate.py @@ -76,3 +76,28 @@ def test_set_setting_failed(self): ): kwargs = {"key": "rotate", "value": "/var/log/wtmp", "setting": "2"} self.assertRaises(SaltInvocationError, logrotate.set_, **kwargs) + + def test_get(self): + """ + Test if get a value for a specific configuration line + """ + with patch( + "salt.modules.logrotate._parse_conf", MagicMock(return_value=PARSE_CONF) + ): + # A single key returns the right value + self.assertEqual(logrotate.get("rotate"), 1) + + # A single key returns the wrong value + self.assertNotEqual(logrotate.get("rotate"), 2) + + # A single key returns the right stanza value + self.assertEqual(logrotate.get("/var/log/wtmp", "rotate"), 1) + + # A single key returns the wrong stanza value + self.assertNotEqual(logrotate.get("/var/log/wtmp", "rotate"), 2) + + # Ensure we're logging the message as debug not warn + with patch.object(logrotate, "_LOG") as log_mock: + res = logrotate.get("/var/log/utmp", "rotate") + self.assertTrue(log_mock.debug.called) + self.assertFalse(log_mock.warn.called)