1
0

test_haproxy.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # coding: utf-8
  2. # Python libs
  3. from __future__ import absolute_import
  4. # Salt libs
  5. import salt.beacons.haproxy as haproxy
  6. from tests.support.mixins import LoaderModuleMockMixin
  7. from tests.support.mock import MagicMock, patch
  8. # Salt testing libs
  9. from tests.support.unit import TestCase
  10. class HAProxyBeaconTestCase(TestCase, LoaderModuleMockMixin):
  11. """
  12. Test case for salt.beacons.haproxy
  13. """
  14. def setup_loader_modules(self):
  15. return {haproxy: {"__context__": {}, "__salt__": {}}}
  16. def test_non_list_config(self):
  17. config = {}
  18. ret = haproxy.validate(config)
  19. self.assertEqual(
  20. ret, (False, "Configuration for haproxy beacon must be a list.")
  21. )
  22. def test_empty_config(self):
  23. config = [{}]
  24. ret = haproxy.validate(config)
  25. self.assertEqual(
  26. ret, (False, "Configuration for haproxy beacon requires backends.")
  27. )
  28. def test_no_servers(self):
  29. config = [{"backends": {"www-backend": {"threshold": 45}}}]
  30. ret = haproxy.validate(config)
  31. self.assertEqual(ret, (False, "Backends for haproxy beacon require servers."))
  32. def test_threshold_reached(self):
  33. config = [{"backends": {"www-backend": {"threshold": 45, "servers": ["web1"]}}}]
  34. ret = haproxy.validate(config)
  35. self.assertEqual(ret, (True, "Valid beacon configuration"))
  36. mock = MagicMock(return_value=46)
  37. with patch.dict(haproxy.__salt__, {"haproxy.get_sessions": mock}):
  38. ret = haproxy.beacon(config)
  39. self.assertEqual(ret, [{"threshold": 45, "scur": 46, "server": "web1"}])
  40. def test_threshold_not_reached(self):
  41. config = [
  42. {"backends": {"www-backend": {"threshold": 100, "servers": ["web1"]}}}
  43. ]
  44. ret = haproxy.validate(config)
  45. self.assertEqual(ret, (True, "Valid beacon configuration"))
  46. mock = MagicMock(return_value=50)
  47. with patch.dict(haproxy.__salt__, {"haproxy.get_sessions": mock}):
  48. ret = haproxy.beacon(config)
  49. self.assertEqual(ret, [])