migrate test_logrotate to pytest

This commit is contained in:
Frode Gundersen 2023-01-10 18:13:01 +00:00 committed by Megan Wilhite
parent 1c8b5a7b05
commit 1830881388
2 changed files with 108 additions and 103 deletions

View file

@ -0,0 +1,108 @@
"""
:codeauthor: Jayesh Kariya <jayeshk@saltstack.com>
Test cases for salt.modules.logrotate
"""
import pytest
import salt.modules.logrotate as logrotate
from salt.exceptions import SaltInvocationError
from tests.support.mock import MagicMock, patch
@pytest.fixture
def PARSE_CONF():
return {
"include files": {"rsyslog": ["/var/log/syslog"]},
"rotate": 1,
"/var/log/wtmp": {"rotate": 1},
}
@pytest.fixture
def configure_loader_modules():
return {logrotate: {}}
# 'show_conf' function tests: 1
def test_show_conf():
"""
Test if it show parsed configuration
"""
with patch("salt.modules.logrotate._parse_conf", MagicMock(return_value=True)):
assert logrotate.show_conf()
# 'set_' function tests: 4
def test_set(PARSE_CONF):
"""
Test if it set a new value for a specific configuration line
"""
with patch(
"salt.modules.logrotate._parse_conf", MagicMock(return_value=PARSE_CONF)
), patch.dict(logrotate.__salt__, {"file.replace": MagicMock(return_value=True)}):
assert logrotate.set_("rotate", "2")
def test_set_failed(PARSE_CONF):
"""
Test if it fails to set a new value for a specific configuration line
"""
with patch(
"salt.modules.logrotate._parse_conf", MagicMock(return_value=PARSE_CONF)
):
kwargs = {"key": "/var/log/wtmp", "value": 2}
pytest.raises(SaltInvocationError, logrotate.set_, **kwargs)
def test_set_setting(PARSE_CONF):
"""
Test if it set a new value for a specific configuration line
"""
with patch.dict(
logrotate.__salt__, {"file.replace": MagicMock(return_value=True)}
), patch("salt.modules.logrotate._parse_conf", MagicMock(return_value=PARSE_CONF)):
assert logrotate.set_("/var/log/wtmp", "rotate", "2")
def test_set_setting_failed(PARSE_CONF):
"""
Test if it fails to set a new value for a specific configuration line
"""
with patch(
"salt.modules.logrotate._parse_conf", MagicMock(return_value=PARSE_CONF)
):
kwargs = {"key": "rotate", "value": "/var/log/wtmp", "setting": "2"}
pytest.raises(SaltInvocationError, logrotate.set_, **kwargs)
def test_get(PARSE_CONF):
"""
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
assert logrotate.get("rotate") == 1
# A single key returns the wrong value
assert logrotate.get("rotate") != 2
# A single key returns the right stanza value
assert logrotate.get("/var/log/wtmp", "rotate") == 1
# A single key returns the wrong stanza value
assert 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")
assert log_mock.debug.called
assert not log_mock.warn.called

View file

@ -1,103 +0,0 @@
"""
:codeauthor: Jayesh Kariya <jayeshk@saltstack.com>
"""
import salt.modules.logrotate as logrotate
from salt.exceptions import SaltInvocationError
from tests.support.mixins import LoaderModuleMockMixin
from tests.support.mock import MagicMock, patch
from tests.support.unit import TestCase
PARSE_CONF = {
"include files": {"rsyslog": ["/var/log/syslog"]},
"rotate": 1,
"/var/log/wtmp": {"rotate": 1},
}
class LogrotateTestCase(TestCase, LoaderModuleMockMixin):
"""
Test cases for salt.modules.logrotate
"""
def setup_loader_modules(self):
return {logrotate: {}}
# 'show_conf' function tests: 1
def test_show_conf(self):
"""
Test if it show parsed configuration
"""
with patch("salt.modules.logrotate._parse_conf", MagicMock(return_value=True)):
self.assertTrue(logrotate.show_conf())
# 'set_' function tests: 4
def test_set(self):
"""
Test if it set a new value for a specific configuration line
"""
with patch(
"salt.modules.logrotate._parse_conf", MagicMock(return_value=PARSE_CONF)
), patch.dict(
logrotate.__salt__, {"file.replace": MagicMock(return_value=True)}
):
self.assertTrue(logrotate.set_("rotate", "2"))
def test_set_failed(self):
"""
Test if it fails to set a new value for a specific configuration line
"""
with patch(
"salt.modules.logrotate._parse_conf", MagicMock(return_value=PARSE_CONF)
):
kwargs = {"key": "/var/log/wtmp", "value": 2}
self.assertRaises(SaltInvocationError, logrotate.set_, **kwargs)
def test_set_setting(self):
"""
Test if it set a new value for a specific configuration line
"""
with patch.dict(
logrotate.__salt__, {"file.replace": MagicMock(return_value=True)}
), patch(
"salt.modules.logrotate._parse_conf", MagicMock(return_value=PARSE_CONF)
):
self.assertTrue(logrotate.set_("/var/log/wtmp", "rotate", "2"))
def test_set_setting_failed(self):
"""
Test if it fails to set a new value for a specific configuration line
"""
with patch(
"salt.modules.logrotate._parse_conf", MagicMock(return_value=PARSE_CONF)
):
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)