test_apache.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 Libs
  8. import salt.states.apache as apache
  9. import salt.utils.files
  10. # Import Salt Testing Libs
  11. from tests.support.mixins import LoaderModuleMockMixin
  12. from tests.support.mock import MagicMock, mock_open, patch
  13. from tests.support.unit import TestCase
  14. class ApacheTestCase(TestCase, LoaderModuleMockMixin):
  15. """
  16. Test cases for salt.states.apache
  17. """
  18. def setup_loader_modules(self):
  19. return {apache: {}}
  20. # 'configfile' function tests: 1
  21. def test_configfile(self):
  22. """
  23. Test to allows for inputting a yaml dictionary into a file
  24. for apache configuration files.
  25. """
  26. with patch("os.path.exists", MagicMock(return_value=True)):
  27. name = "/etc/distro/specific/apache.conf"
  28. config = 'VirtualHost: this: "*:80"'
  29. new_config = 'LiteralHost: that: "*:79"'
  30. ret = {"name": name, "result": True, "changes": {}, "comment": ""}
  31. with patch.object(salt.utils.files, "fopen", mock_open(read_data=config)):
  32. mock_config = MagicMock(return_value=config)
  33. with patch.dict(apache.__salt__, {"apache.config": mock_config}):
  34. ret.update({"comment": "Configuration is up to date."})
  35. self.assertDictEqual(apache.configfile(name, config), ret)
  36. with patch.object(salt.utils.files, "fopen", mock_open(read_data=config)):
  37. mock_config = MagicMock(return_value=new_config)
  38. with patch.dict(apache.__salt__, {"apache.config": mock_config}):
  39. ret.update(
  40. {
  41. "comment": "Configuration will update.",
  42. "changes": {"new": new_config, "old": config},
  43. "result": None,
  44. }
  45. )
  46. with patch.dict(apache.__opts__, {"test": True}):
  47. self.assertDictEqual(apache.configfile(name, new_config), ret)
  48. with patch.object(salt.utils.files, "fopen", mock_open(read_data=config)):
  49. mock_config = MagicMock(return_value=new_config)
  50. with patch.dict(apache.__salt__, {"apache.config": mock_config}):
  51. ret.update(
  52. {
  53. "comment": "Successfully created configuration.",
  54. "result": True,
  55. }
  56. )
  57. with patch.dict(apache.__opts__, {"test": False}):
  58. self.assertDictEqual(apache.configfile(name, config), ret)