test_apache_conf.py 3.0 KB

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