salt/tests/unit/states/test_apache_module.py

94 lines
3.2 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
'''
:codeauthor: :email:`Jayesh Kariya <jayeshk@saltstack.com>`
'''
# Import Python libs
from __future__ import absolute_import
# Import Salt Testing Libs
from tests.support.unit import skipIf, TestCase
from tests.support.mock import (
NO_MOCK,
NO_MOCK_REASON,
MagicMock,
patch
)
# Import Salt Libs
from salt.states import apache_module
apache_module.__opts__ = {}
apache_module.__salt__ = {}
@skipIf(NO_MOCK, NO_MOCK_REASON)
class ApacheModuleTestCase(TestCase):
'''
Test cases for salt.states.apache_module
'''
2015-12-13 12:54:17 +00:00
# 'enabled' function tests: 1
2015-12-13 12:54:17 +00:00
def test_enabled(self):
'''
Test to ensure an Apache module is enabled.
'''
name = 'cgi'
ret = {'name': name,
'result': True,
'changes': {},
'comment': ''}
mock = MagicMock(side_effect=[True, False, False])
mock_str = MagicMock(return_value={'Status': ['enabled']})
with patch.dict(apache_module.__salt__,
{'apache.check_mod_enabled': mock,
'apache.a2enmod': mock_str}):
comt = ('{0} already enabled.'.format(name))
ret.update({'comment': comt})
2015-12-13 12:54:17 +00:00
self.assertDictEqual(apache_module.enabled(name), ret)
comt = ('Apache module {0} is set to be enabled.'.format(name))
ret.update({'comment': comt, 'result': None,
'changes': {'new': 'cgi', 'old': None}})
with patch.dict(apache_module.__opts__, {'test': True}):
2015-12-13 12:54:17 +00:00
self.assertDictEqual(apache_module.enabled(name), ret)
comt = ('Failed to enable {0} Apache module'.format(name))
ret.update({'comment': comt, 'result': False, 'changes': {}})
with patch.dict(apache_module.__opts__, {'test': False}):
2015-12-13 12:54:17 +00:00
self.assertDictEqual(apache_module.enabled(name), ret)
2015-12-13 12:54:17 +00:00
# 'disabled' function tests: 1
2015-12-13 12:54:17 +00:00
def test_disabled(self):
'''
Test to ensure an Apache module is disabled.
'''
name = 'cgi'
ret = {'name': name,
'result': None,
'changes': {},
'comment': ''}
mock = MagicMock(side_effect=[True, True, False])
mock_str = MagicMock(return_value={'Status': ['disabled']})
with patch.dict(apache_module.__salt__,
{'apache.check_mod_enabled': mock,
'apache.a2dismod': mock_str}):
comt = ('Apache module {0} is set to be disabled.'.format(name))
ret.update({'comment': comt, 'changes': {'new': None, 'old': 'cgi'}})
with patch.dict(apache_module.__opts__, {'test': True}):
2015-12-13 12:54:17 +00:00
self.assertDictEqual(apache_module.disabled(name), ret)
comt = ('Failed to disable {0} Apache module'.format(name))
ret.update({'comment': comt, 'result': False,
'changes': {}})
with patch.dict(apache_module.__opts__, {'test': False}):
2015-12-13 12:54:17 +00:00
self.assertDictEqual(apache_module.disabled(name), ret)
comt = ('{0} already disabled.'.format(name))
ret.update({'comment': comt, 'result': True})
2015-12-13 12:54:17 +00:00
self.assertDictEqual(apache_module.disabled(name), ret)