test_lvs.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: Rupesh Tare <rupesht@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.lvs as lvs
  9. # Import Salt Testing Libs
  10. from tests.support.mixins import LoaderModuleMockMixin
  11. from tests.support.mock import MagicMock, patch
  12. from tests.support.unit import TestCase
  13. class LvsTestCase(TestCase, LoaderModuleMockMixin):
  14. """
  15. Test cases for salt.modules.lvs
  16. """
  17. def setup_loader_modules(self):
  18. return {lvs: {}}
  19. def test_add_service(self):
  20. """
  21. Test for Add a virtual service.
  22. """
  23. with patch.object(lvs, "__detect_os", return_value="C"):
  24. with patch.object(lvs, "_build_cmd", return_value="B"):
  25. with patch.dict(
  26. lvs.__salt__,
  27. {
  28. "cmd.run_all": MagicMock(
  29. return_value={"retcode": "ret", "stderr": "stderr"}
  30. )
  31. },
  32. ):
  33. self.assertEqual(lvs.add_service(), "stderr")
  34. def test_edit_service(self):
  35. """
  36. Test for Edit the virtual service.
  37. """
  38. with patch.object(lvs, "__detect_os", return_value="C"):
  39. with patch.object(lvs, "_build_cmd", return_value="B"):
  40. with patch.dict(
  41. lvs.__salt__,
  42. {
  43. "cmd.run_all": MagicMock(
  44. return_value={"retcode": "ret", "stderr": "stderr"}
  45. )
  46. },
  47. ):
  48. self.assertEqual(lvs.edit_service(), "stderr")
  49. def test_delete_service(self):
  50. """
  51. Test for Delete the virtual service.
  52. """
  53. with patch.object(lvs, "__detect_os", return_value="C"):
  54. with patch.object(lvs, "_build_cmd", return_value="B"):
  55. with patch.dict(
  56. lvs.__salt__,
  57. {
  58. "cmd.run_all": MagicMock(
  59. return_value={"retcode": "ret", "stderr": "stderr"}
  60. )
  61. },
  62. ):
  63. self.assertEqual(lvs.delete_service(), "stderr")
  64. def test_add_server(self):
  65. """
  66. Test for Add a real server to a virtual service.
  67. """
  68. with patch.object(lvs, "__detect_os", return_value="C"):
  69. with patch.object(lvs, "_build_cmd", return_value="B"):
  70. with patch.dict(
  71. lvs.__salt__,
  72. {
  73. "cmd.run_all": MagicMock(
  74. return_value={"retcode": "ret", "stderr": "stderr"}
  75. )
  76. },
  77. ):
  78. self.assertEqual(lvs.add_server(), "stderr")
  79. def test_edit_server(self):
  80. """
  81. Test for Edit a real server to a virtual service.
  82. """
  83. with patch.object(lvs, "__detect_os", return_value="C"):
  84. with patch.object(lvs, "_build_cmd", return_value="B"):
  85. with patch.dict(
  86. lvs.__salt__,
  87. {
  88. "cmd.run_all": MagicMock(
  89. return_value={"retcode": "ret", "stderr": "stderr"}
  90. )
  91. },
  92. ):
  93. self.assertEqual(lvs.edit_server(), "stderr")
  94. def test_delete_server(self):
  95. """
  96. Test for Delete the realserver from the virtual service.
  97. """
  98. with patch.object(lvs, "__detect_os", return_value="C"):
  99. with patch.object(lvs, "_build_cmd", return_value="B"):
  100. with patch.dict(
  101. lvs.__salt__,
  102. {
  103. "cmd.run_all": MagicMock(
  104. return_value={"retcode": "ret", "stderr": "stderr"}
  105. )
  106. },
  107. ):
  108. self.assertEqual(lvs.delete_server(), "stderr")
  109. def test_clear(self):
  110. """
  111. Test for Clear the virtual server table
  112. """
  113. with patch.object(lvs, "__detect_os", return_value="C"):
  114. with patch.dict(
  115. lvs.__salt__,
  116. {
  117. "cmd.run_all": MagicMock(
  118. return_value={"retcode": "ret", "stderr": "stderr"}
  119. )
  120. },
  121. ):
  122. self.assertEqual(lvs.clear(), "stderr")
  123. def test_get_rules(self):
  124. """
  125. Test for Get the virtual server rules
  126. """
  127. with patch.object(lvs, "__detect_os", return_value="C"):
  128. with patch.dict(lvs.__salt__, {"cmd.run": MagicMock(return_value="A")}):
  129. self.assertEqual(lvs.get_rules(), "A")
  130. def test_list_(self):
  131. """
  132. Test for List the virtual server table
  133. """
  134. with patch.object(lvs, "__detect_os", return_value="C"):
  135. with patch.object(lvs, "_build_cmd", return_value="B"):
  136. with patch.dict(
  137. lvs.__salt__,
  138. {
  139. "cmd.run_all": MagicMock(
  140. return_value={"retcode": "ret", "stderr": "stderr"}
  141. )
  142. },
  143. ):
  144. self.assertEqual(lvs.list_("p", "s"), "stderr")
  145. def test_zero(self):
  146. """
  147. Test for Zero the packet, byte and rate counters in a
  148. service or all services.
  149. """
  150. with patch.object(lvs, "__detect_os", return_value="C"):
  151. with patch.object(lvs, "_build_cmd", return_value="B"):
  152. with patch.dict(
  153. lvs.__salt__,
  154. {
  155. "cmd.run_all": MagicMock(
  156. return_value={"retcode": "ret", "stderr": "stderr"}
  157. )
  158. },
  159. ):
  160. self.assertEqual(lvs.zero("p", "s"), "stderr")
  161. def test_check_service(self):
  162. """
  163. Test for Check the virtual service exists.
  164. """
  165. with patch.object(lvs, "__detect_os", return_value="C"):
  166. with patch.object(lvs, "_build_cmd", return_value="B"):
  167. with patch.dict(
  168. lvs.__salt__,
  169. {
  170. "cmd.run_all": MagicMock(
  171. return_value={"retcode": "ret", "stderr": "stderr"}
  172. )
  173. },
  174. ):
  175. with patch.object(lvs, "get_rules", return_value="C"):
  176. self.assertEqual(
  177. lvs.check_service("p", "s"), "Error: service not exists"
  178. )
  179. def test_check_server(self):
  180. """
  181. Test for Check the real server exists in the specified service.
  182. """
  183. with patch.object(lvs, "__detect_os", return_value="C"):
  184. with patch.object(lvs, "_build_cmd", return_value="B"):
  185. with patch.dict(
  186. lvs.__salt__,
  187. {
  188. "cmd.run_all": MagicMock(
  189. return_value={"retcode": "ret", "stderr": "stderr"}
  190. )
  191. },
  192. ):
  193. with patch.object(lvs, "get_rules", return_value="C"):
  194. self.assertEqual(
  195. lvs.check_server("p", "s"), "Error: server not exists"
  196. )