test_apache_conf.py 3.2 KB

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