test_apache.py 2.7 KB

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