test_haproxy.py 2.4 KB

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