test_varnish.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. # Import Salt Libs
  14. import salt.modules.varnish as varnish
  15. class VarnishTestCase(TestCase, LoaderModuleMockMixin):
  16. '''
  17. Test cases for salt.modules.varnish
  18. '''
  19. def setup_loader_modules(self):
  20. return {varnish: {}}
  21. def test_version(self):
  22. '''
  23. Test to return server version from varnishd -V
  24. '''
  25. with patch.dict(varnish.__salt__,
  26. {'cmd.run': MagicMock(return_value='(varnish-2.0)')}):
  27. self.assertEqual(varnish.version(), '2.0')
  28. def test_ban(self):
  29. '''
  30. Test to add ban to the varnish cache
  31. '''
  32. with patch.object(varnish, '_run_varnishadm',
  33. return_value={'retcode': 0}):
  34. self.assertTrue(varnish.ban('ban_expression'))
  35. def test_ban_list(self):
  36. '''
  37. Test to list varnish cache current bans
  38. '''
  39. with patch.object(varnish, '_run_varnishadm',
  40. return_value={'retcode': True}):
  41. self.assertFalse(varnish.ban_list())
  42. with patch.object(varnish, '_run_varnishadm',
  43. return_value={'retcode': False,
  44. 'stdout': 'A\nB\nC'}):
  45. self.assertEqual(varnish.ban_list(), ['B', 'C'])
  46. def test_purge(self):
  47. '''
  48. Test to purge the varnish cache
  49. '''
  50. with patch.object(varnish, 'ban', return_value=True):
  51. self.assertTrue(varnish.purge())
  52. def test_param_set(self):
  53. '''
  54. Test to set a param in varnish cache
  55. '''
  56. with patch.object(varnish, '_run_varnishadm',
  57. return_value={'retcode': 0}):
  58. self.assertTrue(varnish.param_set('param', 'value'))
  59. def test_param_show(self):
  60. '''
  61. Test to show params of varnish cache
  62. '''
  63. with patch.object(varnish, '_run_varnishadm',
  64. return_value={'retcode': True,
  65. 'stdout': 'A\nB\nC'}):
  66. self.assertFalse(varnish.param_show('param'))
  67. with patch.object(varnish, '_run_varnishadm',
  68. return_value={'retcode': False,
  69. 'stdout': 'A .1\nB .2\n'}):
  70. self.assertEqual(varnish.param_show('param'), {'A': '.1'})