test_lvs.py 8.4 KB

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