salt/tests/unit/modules/test_logrotate.py

91 lines
2.8 KiB
Python
Raw Normal View History

2015-02-03 08:29:01 +00:00
# -*- coding: utf-8 -*-
'''
:codeauthor: :email:`Jayesh Kariya <jayeshk@saltstack.com>`
'''
# Import Python Libs
from __future__ import absolute_import
# Import Salt Libs
from salt.exceptions import SaltInvocationError
import salt.modules.logrotate as logrotate
2015-02-03 08:29:01 +00:00
# Import Salt Testing Libs
2017-02-19 15:35:30 +00:00
from tests.support.mixins import LoaderModuleMockMixin
from tests.support.unit import TestCase, skipIf
from tests.support.mock import (
2015-02-03 08:29:01 +00:00
MagicMock,
patch,
NO_MOCK,
NO_MOCK_REASON
)
PARSE_CONF = {
'include files': {
'rsyslog': ['/var/log/syslog']
},
2016-11-04 16:43:02 +00:00
'rotate': 1,
'/var/log/wtmp': {
2016-11-04 16:43:02 +00:00
'rotate': 1
}
}
2015-02-03 08:29:01 +00:00
@skipIf(NO_MOCK, NO_MOCK_REASON)
2017-02-19 15:35:30 +00:00
class LogrotateTestCase(TestCase, LoaderModuleMockMixin):
2015-02-03 08:29:01 +00:00
'''
Test cases for salt.modules.logrotate
'''
def setup_loader_modules(self):
return {logrotate: {}}
2017-02-19 15:35:30 +00:00
2015-02-03 08:29:01 +00:00
# 'show_conf' function tests: 1
def test_show_conf(self):
'''
Test if it show parsed configuration
'''
2017-04-10 13:00:57 +00:00
with patch('salt.modules.logrotate._parse_conf',
MagicMock(return_value=True)):
self.assertTrue(logrotate.show_conf())
2015-02-03 08:29:01 +00:00
# 'set_' function tests: 4
2015-02-03 08:29:01 +00:00
def test_set(self):
'''
Test if it set a new value for a specific configuration line
'''
2017-04-10 13:00:57 +00:00
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'))
2015-02-03 08:29:01 +00:00
def test_set_failed(self):
'''
Test if it fails to set a new value for a specific configuration line
'''
2017-04-10 13:00:57 +00:00
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)
2015-02-03 08:29:01 +00:00
def test_set_setting(self):
'''
Test if it set a new value for a specific configuration line
'''
with patch.dict(logrotate.__salt__,
2017-04-10 13:00:57 +00:00
{'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
'''
2017-04-10 13:00:57 +00:00
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)