Skip to content

Commit

Permalink
Avoid warning noise in logrotate.get (#56105)
Browse files Browse the repository at this point in the history
* 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 <gareth@saltstack.com>
Co-authored-by: Megan Wilhite <mwilhite@vmware.com>
  • Loading branch information
3 people authored Sep 28, 2022
1 parent 784777c commit db54566
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/53988.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Avoid warning noise in lograte.get
2 changes: 1 addition & 1 deletion salt/modules/logrotate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
25 changes: 25 additions & 0 deletions tests/unit/modules/test_logrotate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit db54566

Please sign in to comment.