test_apache_module.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Jayesh Kariya <jayeshk@saltstack.com>
  4. '''
  5. # Import Python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. # Import Salt Testing Libs
  8. from tests.support.mixins import LoaderModuleMockMixin
  9. from tests.support.unit import TestCase
  10. from tests.support.mock import (
  11. MagicMock,
  12. patch
  13. )
  14. # Import Salt Libs
  15. import salt.states.apache_module as apache_module
  16. class ApacheModuleTestCase(TestCase, LoaderModuleMockMixin):
  17. '''
  18. Test cases for salt.states.apache_module
  19. '''
  20. def setup_loader_modules(self):
  21. return {apache_module: {}}
  22. # 'enabled' function tests: 1
  23. def test_enabled(self):
  24. '''
  25. Test to ensure an Apache module is enabled.
  26. '''
  27. name = 'cgi'
  28. ret = {'name': name,
  29. 'result': True,
  30. 'changes': {},
  31. 'comment': ''}
  32. mock = MagicMock(side_effect=[True, False, False])
  33. mock_str = MagicMock(return_value={'Status': ['enabled']})
  34. with patch.dict(apache_module.__salt__,
  35. {'apache.check_mod_enabled': mock,
  36. 'apache.a2enmod': mock_str}):
  37. comt = '{0} already enabled.'.format(name)
  38. ret.update({'comment': comt})
  39. self.assertDictEqual(apache_module.enabled(name), ret)
  40. comt = 'Apache module {0} is set to be enabled.'.format(name)
  41. ret.update({'comment': comt, 'result': None,
  42. 'changes': {'new': 'cgi', 'old': None}})
  43. with patch.dict(apache_module.__opts__, {'test': True}):
  44. self.assertDictEqual(apache_module.enabled(name), ret)
  45. comt = 'Failed to enable {0} Apache module'.format(name)
  46. ret.update({'comment': comt, 'result': False, 'changes': {}})
  47. with patch.dict(apache_module.__opts__, {'test': False}):
  48. self.assertDictEqual(apache_module.enabled(name), ret)
  49. # 'disabled' function tests: 1
  50. def test_disabled(self):
  51. '''
  52. Test to ensure an Apache module is disabled.
  53. '''
  54. name = 'cgi'
  55. ret = {'name': name,
  56. 'result': None,
  57. 'changes': {},
  58. 'comment': ''}
  59. mock = MagicMock(side_effect=[True, True, False])
  60. mock_str = MagicMock(return_value={'Status': ['disabled']})
  61. with patch.dict(apache_module.__salt__,
  62. {'apache.check_mod_enabled': mock,
  63. 'apache.a2dismod': mock_str}):
  64. comt = 'Apache module {0} is set to be disabled.'.format(name)
  65. ret.update({'comment': comt, 'changes': {'new': None, 'old': 'cgi'}})
  66. with patch.dict(apache_module.__opts__, {'test': True}):
  67. self.assertDictEqual(apache_module.disabled(name), ret)
  68. comt = 'Failed to disable {0} Apache module'.format(name)
  69. ret.update({'comment': comt, 'result': False,
  70. 'changes': {}})
  71. with patch.dict(apache_module.__opts__, {'test': False}):
  72. self.assertDictEqual(apache_module.disabled(name), ret)
  73. comt = '{0} already disabled.'.format(name)
  74. ret.update({'comment': comt, 'result': True})
  75. self.assertDictEqual(apache_module.disabled(name), ret)