2015-02-03 08:29:01 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
'''
|
|
|
|
:codeauthor: :email:`Jayesh Kariya <jayeshk@saltstack.com>`
|
|
|
|
'''
|
|
|
|
|
2015-02-03 18:34:49 +00:00
|
|
|
# Import Python Libs
|
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2016-10-30 08:29:39 +00:00
|
|
|
# Import Salt Libs
|
|
|
|
from salt.exceptions import SaltInvocationError
|
2017-03-21 17:15:36 +00:00
|
|
|
import salt.modules.logrotate as logrotate
|
2016-10-30 08:29:39 +00:00
|
|
|
|
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
|
2017-02-27 13:58:07 +00:00
|
|
|
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
|
|
|
|
)
|
|
|
|
|
2016-10-30 08:29:39 +00:00
|
|
|
PARSE_CONF = {
|
|
|
|
'include files': {
|
|
|
|
'rsyslog': ['/var/log/syslog']
|
|
|
|
},
|
2016-11-04 16:43:02 +00:00
|
|
|
'rotate': 1,
|
2016-10-30 08:29:39 +00:00
|
|
|
'/var/log/wtmp': {
|
2016-11-04 16:43:02 +00:00
|
|
|
'rotate': 1
|
2016-10-30 08:29:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
'''
|
2017-03-22 12:12:36 +00:00
|
|
|
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
|
|
|
|
2016-10-30 08:29:39 +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)}):
|
2016-10-30 08:29:39 +00:00
|
|
|
self.assertTrue(logrotate.set_('rotate', '2'))
|
2015-02-03 08:29:01 +00:00
|
|
|
|
2016-10-30 08:29:39 +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)
|
2016-10-30 08:29:39 +00:00
|
|
|
|
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
|
|
|
|
'''
|
2016-10-30 08:29:39 +00:00
|
|
|
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)):
|
2016-10-30 08:29:39 +00:00
|
|
|
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)
|