test_haproxyconn.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. # Import Salt Libs
  11. import salt.modules.haproxyconn as haproxyconn
  12. class Mockcmds(object):
  13. '''
  14. Mock of cmds
  15. '''
  16. def __init__(self):
  17. self.backend = None
  18. self.server = None
  19. self.weight = None
  20. def listServers(self, backend):
  21. '''
  22. Mock of listServers method
  23. '''
  24. self.backend = backend
  25. return 'Name: server01 Status: UP Weight: 1 bIn: 22 bOut: 12\n' \
  26. 'Name: server02 Status: MAINT Weight: 2 bIn: 0 bOut: 0'
  27. def enableServer(self, server, backend):
  28. '''
  29. Mock of enableServer method
  30. '''
  31. self.backend = backend
  32. self.server = server
  33. return 'server enabled'
  34. def disableServer(self, server, backend):
  35. '''
  36. Mock of disableServer method
  37. '''
  38. self.backend = backend
  39. self.server = server
  40. return 'server disabled'
  41. def getWeight(self, server, backend, weight=0):
  42. '''
  43. Mock of getWeight method
  44. '''
  45. self.backend = backend
  46. self.server = server
  47. self.weight = weight
  48. return 'server weight'
  49. @staticmethod
  50. def showFrontends():
  51. '''
  52. Mock of showFrontends method
  53. '''
  54. return 'frontend-alpha\n' \
  55. 'frontend-beta\n' \
  56. 'frontend-gamma'
  57. @staticmethod
  58. def showBackends():
  59. '''
  60. Mock of showBackends method
  61. '''
  62. return 'backend-alpha\n' \
  63. 'backend-beta\n' \
  64. 'backend-gamma'
  65. class Mockhaproxy(object):
  66. '''
  67. Mock of haproxy
  68. '''
  69. def __init__(self):
  70. self.cmds = Mockcmds()
  71. class MockHaConn(object):
  72. '''
  73. Mock of HaConn
  74. '''
  75. def __init__(self, socket=None):
  76. self.ha_cmd = None
  77. def sendCmd(self, ha_cmd, objectify=False):
  78. '''
  79. Mock of sendCmd method
  80. '''
  81. self.ha_cmd = ha_cmd
  82. self.objectify = objectify
  83. return ha_cmd
  84. class HaproxyConnTestCase(TestCase, LoaderModuleMockMixin):
  85. '''
  86. Test cases for salt.modules.haproxyconn
  87. '''
  88. def setup_loader_modules(self):
  89. return {haproxyconn: {'haproxy': Mockhaproxy(), '_get_conn': MockHaConn}}
  90. # 'list_servers' function tests: 1
  91. def test_list_servers(self):
  92. '''
  93. Test list_servers
  94. '''
  95. self.assertTrue(haproxyconn.list_servers('mysql'))
  96. # 'enable_server' function tests: 1
  97. def test_enable_server(self):
  98. '''
  99. Test enable_server
  100. '''
  101. self.assertTrue(haproxyconn.enable_server('web1.salt.com', 'www'))
  102. # 'disable_server' function tests: 1
  103. def test_disable_server(self):
  104. '''
  105. Test disable_server
  106. '''
  107. self.assertTrue(haproxyconn.disable_server('db1.salt.com', 'mysql'))
  108. # 'get_weight' function tests: 1
  109. def test_get_weight(self):
  110. '''
  111. Test get the weight of a server
  112. '''
  113. self.assertTrue(haproxyconn.get_weight('db1.salt.com', 'mysql'))
  114. # 'set_weight' function tests: 1
  115. def test_set_weight(self):
  116. '''
  117. Test setting the weight of a given server
  118. '''
  119. self.assertTrue(haproxyconn.set_weight('db1.salt.com', 'mysql',
  120. weight=11))
  121. # 'show_frontends' function tests: 1
  122. def test_show_frontends(self):
  123. '''
  124. Test print all frontends received from the HAProxy socket
  125. '''
  126. self.assertTrue(haproxyconn.show_frontends())
  127. def test_list_frontends(self):
  128. '''
  129. Test listing all frontends
  130. '''
  131. self.assertEqual(
  132. sorted(haproxyconn.list_frontends()),
  133. sorted(['frontend-alpha', 'frontend-beta', 'frontend-gamma'])
  134. )
  135. # 'show_backends' function tests: 1
  136. def test_show_backends(self):
  137. '''
  138. Test print all backends received from the HAProxy socket
  139. '''
  140. self.assertTrue(haproxyconn.show_backends())
  141. def test_list_backends(self):
  142. '''
  143. Test listing of all backends
  144. '''
  145. self.assertEqual(
  146. sorted(haproxyconn.list_backends()),
  147. sorted(['backend-alpha', 'backend-beta', 'backend-gamma'])
  148. )
  149. def test_get_backend(self):
  150. '''
  151. Test get_backend and compare returned value
  152. '''
  153. expected_data = {
  154. 'server01': {
  155. 'status': 'UP',
  156. 'weight': 1,
  157. 'bin': 22,
  158. 'bout': 12
  159. },
  160. 'server02': {
  161. 'status': 'MAINT',
  162. 'weight': 2,
  163. 'bin': 0,
  164. 'bout': 0
  165. }
  166. }
  167. self.assertDictEqual(haproxyconn.get_backend('test'), expected_data)
  168. def test_wait_state_true(self):
  169. '''
  170. Test a successful wait for state
  171. '''
  172. self.assertTrue(haproxyconn.wait_state('test', 'server01'))
  173. def test_wait_state_false(self):
  174. '''
  175. Test a failed wait for state, with a timeout of 0
  176. '''
  177. self.assertFalse(haproxyconn.wait_state('test', 'server02', 'up', 0))