test_modjk.py 7.4 KB

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