test_modjk.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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.unit import TestCase
  9. from tests.support.mock import (
  10. patch,
  11. )
  12. # Import Salt Libs
  13. import salt.modules.modjk as modjk
  14. class ModjkTestCase(TestCase):
  15. '''
  16. Test cases for salt.modules.modjk
  17. '''
  18. # 'version' function tests: 1
  19. def test_version(self):
  20. '''
  21. Test for return the modjk version
  22. '''
  23. with patch.object(modjk, '_do_http', return_value=
  24. {'worker.jk_version': 'mod_jk/1.2.37'}):
  25. self.assertEqual(modjk.version(), '1.2.37')
  26. # 'get_running' function tests: 1
  27. def test_get_running(self):
  28. '''
  29. Test for get the current running config (not from disk)
  30. '''
  31. with patch.object(modjk, '_do_http', return_value={}):
  32. self.assertDictEqual(modjk.get_running(), {})
  33. # 'dump_config' function tests: 1
  34. def test_dump_config(self):
  35. '''
  36. Test for dump the original configuration that was loaded from disk
  37. '''
  38. with patch.object(modjk, '_do_http', return_value={}):
  39. self.assertDictEqual(modjk.dump_config(), {})
  40. # 'list_configured_members' function tests: 1
  41. def test_list_configured_members(self):
  42. '''
  43. Test for return a list of member workers from the configuration files
  44. '''
  45. with patch.object(modjk, '_do_http', return_value={}):
  46. self.assertListEqual(modjk.list_configured_members('loadbalancer1'),
  47. [])
  48. with patch.object(modjk, '_do_http', return_value=
  49. {'worker.loadbalancer1.balance_workers': 'SALT'}):
  50. self.assertListEqual(modjk.list_configured_members('loadbalancer1'),
  51. ['SALT'])
  52. # 'workers' function tests: 1
  53. def test_workers(self):
  54. '''
  55. Test for return a list of member workers and their status
  56. '''
  57. with patch.object(modjk, '_do_http', return_value=
  58. {'worker.list': 'Salt1,Salt2'}):
  59. self.assertDictEqual(modjk.workers(), {})
  60. # 'recover_all' function tests: 1
  61. def test_recover_all(self):
  62. '''
  63. Test for set the all the workers in lbn to recover and
  64. activate them if they are not
  65. '''
  66. with patch.object(modjk, '_do_http', return_value={}):
  67. self.assertDictEqual(modjk.recover_all('loadbalancer1'), {})
  68. with patch.object(modjk, '_do_http', return_value=
  69. {'worker.loadbalancer1.balance_workers': 'SALT'}):
  70. with patch.object(modjk, 'worker_status',
  71. return_value={'activation': 'ACT',
  72. 'state': 'OK'}):
  73. self.assertDictEqual(modjk.recover_all('loadbalancer1'),
  74. {'SALT': {'activation': 'ACT',
  75. 'state': 'OK'}})
  76. # 'reset_stats' function tests: 1
  77. def test_reset_stats(self):
  78. '''
  79. Test for reset all runtime statistics for the load balancer
  80. '''
  81. with patch.object(modjk, '_do_http', return_value=
  82. {'worker.result.type': 'OK'}):
  83. self.assertTrue(modjk.reset_stats('loadbalancer1'))
  84. # 'lb_edit' function tests: 1
  85. def test_lb_edit(self):
  86. '''
  87. Test for edit the loadbalancer settings
  88. '''
  89. with patch.object(modjk, '_do_http', return_value=
  90. {'worker.result.type': 'OK'}):
  91. self.assertTrue(modjk.lb_edit('loadbalancer1', {'vlr': 1,
  92. 'vlt': 60}))
  93. # 'bulk_stop' function tests: 1
  94. def test_bulk_stop(self):
  95. '''
  96. Test for stop all the given workers in the specific load balancer
  97. '''
  98. with patch.object(modjk, '_do_http', return_value=
  99. {'worker.result.type': 'OK'}):
  100. self.assertTrue(modjk.bulk_stop(["node1", "node2", "node3"],
  101. 'loadbalancer1'))
  102. # 'bulk_activate' function tests: 1
  103. def test_bulk_activate(self):
  104. '''
  105. Test for activate all the given workers in the specific load balancer
  106. '''
  107. with patch.object(modjk, '_do_http', return_value=
  108. {'worker.result.type': 'OK'}):
  109. self.assertTrue(modjk.bulk_activate(["node1", "node2", "node3"],
  110. 'loadbalancer1'))
  111. # 'bulk_disable' function tests: 1
  112. def test_bulk_disable(self):
  113. '''
  114. Test for disable all the given workers in the specific load balancer
  115. '''
  116. with patch.object(modjk, '_do_http', return_value=
  117. {'worker.result.type': 'OK'}):
  118. self.assertTrue(modjk.bulk_disable(["node1", "node2", "node3"],
  119. 'loadbalancer1'))
  120. # 'bulk_recover' function tests: 1
  121. def test_bulk_recover(self):
  122. '''
  123. Test for recover all the given workers in the specific load balancer
  124. '''
  125. with patch.object(modjk, '_do_http', return_value=
  126. {'worker.result.type': 'OK'}):
  127. self.assertTrue(modjk.bulk_recover(["node1", "node2", "node3"],
  128. 'loadbalancer1'))
  129. # 'worker_status' function tests: 1
  130. def test_worker_status(self):
  131. '''
  132. Test for return the state of the worker
  133. '''
  134. with patch.object(modjk, '_do_http', return_value=
  135. {'worker.node1.activation': 'ACT',
  136. 'worker.node1.state': 'OK'}):
  137. self.assertDictEqual(modjk.worker_status("node1"),
  138. {'activation': 'ACT', 'state': 'OK'})
  139. with patch.object(modjk, '_do_http', return_value={}):
  140. self.assertFalse(modjk.worker_status("node1"))
  141. # 'worker_recover' function tests: 1
  142. def test_worker_recover(self):
  143. '''
  144. Test for set the worker to recover this module will fail
  145. if it is in OK state
  146. '''
  147. with patch.object(modjk, '_do_http', return_value={}):
  148. self.assertDictEqual(modjk.worker_recover("node1", 'loadbalancer1'),
  149. {})
  150. # 'worker_disable' function tests: 1
  151. def test_worker_disable(self):
  152. '''
  153. Test for set the worker to disable state in the lbn load balancer
  154. '''
  155. with patch.object(modjk, '_do_http', return_value=
  156. {'worker.result.type': 'OK'}):
  157. self.assertTrue(modjk.worker_disable('node1', 'loadbalancer1'))
  158. # 'worker_activate' function tests: 1
  159. def test_worker_activate(self):
  160. '''
  161. Test for set the worker to activate state in the lbn load balancer
  162. '''
  163. with patch.object(modjk, '_do_http', return_value=
  164. {'worker.result.type': 'OK'}):
  165. self.assertTrue(modjk.worker_activate('node1', 'loadbalancer1'))
  166. # 'worker_stop' function tests: 1
  167. def test_worker_stop(self):
  168. '''
  169. Test for set the worker to stopped state in the lbn load balancer
  170. '''
  171. with patch.object(modjk, '_do_http', return_value=
  172. {'worker.result.type': 'OK'}):
  173. self.assertTrue(modjk.worker_stop('node1', 'loadbalancer1'))
  174. # 'worker_edit' function tests: 1
  175. def test_worker_edit(self):
  176. '''
  177. Test for edit the worker settings
  178. '''
  179. with patch.object(modjk, '_do_http', return_value=
  180. {'worker.result.type': 'OK'}):
  181. self.assertTrue(modjk.worker_edit('node1', 'loadbalancer1',
  182. {'vwf': 500, 'vwd': 60}))