test_apache_site.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_site as apache_site
  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 ApacheSiteTestCase(TestCase, LoaderModuleMockMixin):
  11. """
  12. Test cases for salt.states.apache_site
  13. """
  14. def setup_loader_modules(self):
  15. return {apache_site: {}}
  16. # 'enabled' function tests: 1
  17. def test_enabled(self):
  18. """
  19. Test to ensure an Apache site 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_site.__salt__,
  27. {"apache.check_site_enabled": mock, "apache.a2ensite": mock_str},
  28. ):
  29. comt = "{0} already enabled.".format(name)
  30. ret.update({"comment": comt})
  31. self.assertDictEqual(apache_site.enabled(name), ret)
  32. comt = "Apache site {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_site.__opts__, {"test": True}):
  37. self.assertDictEqual(apache_site.enabled(name), ret)
  38. comt = "Failed to enable {0} Apache site".format(name)
  39. ret.update({"comment": comt, "result": False, "changes": {}})
  40. with patch.dict(apache_site.__opts__, {"test": False}):
  41. self.assertDictEqual(apache_site.enabled(name), ret)
  42. # 'disabled' function tests: 1
  43. def test_disabled(self):
  44. """
  45. Test to ensure an Apache site 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_site.__salt__,
  53. {"apache.check_site_enabled": mock, "apache.a2dissite": mock_str},
  54. ):
  55. comt = "Apache site {0} is set to be disabled.".format(name)
  56. ret.update({"comment": comt, "changes": {"new": None, "old": name}})
  57. with patch.dict(apache_site.__opts__, {"test": True}):
  58. self.assertDictEqual(apache_site.disabled(name), ret)
  59. comt = "Failed to disable {0} Apache site".format(name)
  60. ret.update({"comment": comt, "result": False, "changes": {}})
  61. with patch.dict(apache_site.__opts__, {"test": False}):
  62. self.assertDictEqual(apache_site.disabled(name), ret)
  63. comt = "{0} already disabled.".format(name)
  64. ret.update({"comment": comt, "result": True})
  65. self.assertDictEqual(apache_site.disabled(name), ret)