1
0

test_haproxy.py 2.4 KB

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