test_varnish.py 2.5 KB

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