test_rh_service.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Jayesh Kariya <jayeshk@saltstack.com>
  4. '''
  5. # Import Python Libs
  6. from __future__ import absolute_import, unicode_literals, print_function
  7. import textwrap
  8. # Import Salt Testing Libs
  9. from tests.support.mixins import LoaderModuleMockMixin
  10. from tests.support.unit import TestCase
  11. from tests.support.mock import (
  12. MagicMock,
  13. patch,
  14. )
  15. # Import Salt Libs
  16. import salt.modules.rh_service as rh_service
  17. RET = ['hostname', 'mountall', 'network-interface', 'network-manager',
  18. 'salt-api', 'salt-master', 'salt-minion']
  19. class RhServiceTestCase(TestCase, LoaderModuleMockMixin):
  20. '''
  21. Test cases for salt.modules.rh_service
  22. '''
  23. def setup_loader_modules(self):
  24. return {
  25. rh_service: {
  26. '_upstart_disable': None,
  27. '_upstart_enable': None,
  28. '_upstart_is_enabled': None
  29. }
  30. }
  31. @staticmethod
  32. def _m_lst():
  33. '''
  34. Return value for [].
  35. '''
  36. return MagicMock(return_value=[])
  37. @staticmethod
  38. def _m_ret():
  39. '''
  40. Return value for RET.
  41. '''
  42. return MagicMock(return_value=RET)
  43. @staticmethod
  44. def _m_bool(bol=True):
  45. '''
  46. Return Bool value.
  47. '''
  48. return MagicMock(return_value=bol)
  49. def test__chkconfig_is_enabled(self):
  50. '''
  51. test _chkconfig_is_enabled function
  52. '''
  53. name = 'atd'
  54. chkconfig_out = textwrap.dedent('''\
  55. {0} 0:off 1:off 2:off 3:on 4:on 5:on 6:off
  56. '''.format(name))
  57. xinetd_out = textwrap.dedent('''\
  58. xinetd based services:
  59. {0} on
  60. '''.format(name))
  61. with patch.object(rh_service, '_runlevel', MagicMock(return_value=3)):
  62. mock_run = MagicMock(return_value={'retcode': 0,
  63. 'stdout': chkconfig_out})
  64. with patch.dict(rh_service.__salt__, {'cmd.run_all': mock_run}):
  65. self.assertTrue(rh_service._chkconfig_is_enabled(name))
  66. self.assertFalse(rh_service._chkconfig_is_enabled(name, 2))
  67. self.assertTrue(rh_service._chkconfig_is_enabled(name, 3))
  68. mock_run = MagicMock(return_value={'retcode': 0,
  69. 'stdout': xinetd_out})
  70. with patch.dict(rh_service.__salt__, {'cmd.run_all': mock_run}):
  71. self.assertTrue(rh_service._chkconfig_is_enabled(name))
  72. self.assertTrue(rh_service._chkconfig_is_enabled(name, 2))
  73. self.assertTrue(rh_service._chkconfig_is_enabled(name, 3))
  74. # 'get_enabled' function tests: 1
  75. def test_get_enabled(self):
  76. '''
  77. Test if it return the enabled services. Use the ``limit``
  78. param to restrict results to services of that type.
  79. '''
  80. with patch.object(rh_service, '_upstart_services', self._m_ret()):
  81. with patch.object(rh_service, '_upstart_is_enabled', MagicMock(return_value=False)):
  82. self.assertListEqual(rh_service.get_enabled('upstart'), [])
  83. mock_run = MagicMock(return_value='salt stack')
  84. with patch.dict(rh_service.__salt__, {'cmd.run': mock_run}):
  85. with patch.object(rh_service, '_sysv_services', self._m_ret()):
  86. with patch.object(rh_service, '_sysv_is_enabled',
  87. self._m_bool()):
  88. self.assertListEqual(rh_service.get_enabled('sysvinit'),
  89. RET)
  90. with patch.object(rh_service, '_upstart_services', self._m_lst()):
  91. with patch.object(rh_service, '_upstart_is_enabled', MagicMock(return_value=True)):
  92. self.assertListEqual(rh_service.get_enabled(), RET)
  93. # 'get_disabled' function tests: 1
  94. def test_get_disabled(self):
  95. '''
  96. Test if it return the disabled services. Use the ``limit``
  97. param to restrict results to services of that type.
  98. '''
  99. with patch.object(rh_service, '_upstart_services', self._m_ret()):
  100. with patch.object(rh_service, '_upstart_is_enabled', MagicMock(return_value=False)):
  101. self.assertListEqual(rh_service.get_disabled('upstart'), RET)
  102. mock_run = MagicMock(return_value='salt stack')
  103. with patch.dict(rh_service.__salt__, {'cmd.run': mock_run}):
  104. with patch.object(rh_service, '_sysv_services', self._m_ret()):
  105. with patch.object(rh_service, '_sysv_is_enabled',
  106. self._m_bool(False)):
  107. self.assertListEqual(rh_service.get_disabled('sysvinit'),
  108. RET)
  109. with patch.object(rh_service, '_upstart_services',
  110. self._m_lst()):
  111. with patch.object(rh_service, '_upstart_is_enabled', MagicMock(return_value=False)):
  112. self.assertListEqual(rh_service.get_disabled(), RET)
  113. # 'get_all' function tests: 1
  114. def test_get_all(self):
  115. '''
  116. Test if it return all installed services. Use the ``limit``
  117. param to restrict results to services of that type.
  118. '''
  119. with patch.object(rh_service, '_upstart_services', self._m_ret()):
  120. self.assertListEqual(rh_service.get_all('upstart'), RET)
  121. with patch.object(rh_service, '_sysv_services', self._m_ret()):
  122. self.assertListEqual(rh_service.get_all('sysvinit'), RET)
  123. with patch.object(rh_service, '_upstart_services', self._m_lst()):
  124. self.assertListEqual(rh_service.get_all(), RET)
  125. # 'available' function tests: 1
  126. def test_available(self):
  127. '''
  128. Test if it return True if the named service is available.
  129. '''
  130. with patch.object(rh_service, '_service_is_upstart', self._m_bool()):
  131. self.assertTrue(rh_service.available('salt-api', 'upstart'))
  132. with patch.object(rh_service, '_service_is_sysv', self._m_bool()):
  133. self.assertTrue(rh_service.available('salt-api', 'sysvinit'))
  134. with patch.object(rh_service, '_service_is_upstart',
  135. self._m_bool()):
  136. self.assertTrue(rh_service.available('salt-api'))
  137. # 'missing' function tests: 1
  138. def test_missing(self):
  139. '''
  140. Test if it return True if the named service is not available.
  141. '''
  142. with patch.object(rh_service, '_service_is_upstart',
  143. self._m_bool(False)):
  144. self.assertTrue(rh_service.missing('sshd', 'upstart'))
  145. with patch.object(rh_service, '_service_is_sysv',
  146. self._m_bool(False)):
  147. self.assertTrue(rh_service.missing('sshd'))
  148. with patch.object(rh_service, '_service_is_sysv', self._m_bool()):
  149. self.assertFalse(rh_service.missing('sshd', 'sysvinit'))
  150. with patch.object(rh_service, '_service_is_upstart',
  151. self._m_bool()):
  152. self.assertFalse(rh_service.missing('sshd'))
  153. # 'start' function tests: 1
  154. def test_start(self):
  155. '''
  156. Test if it start the specified service.
  157. '''
  158. with patch.object(rh_service, '_service_is_upstart', self._m_bool()):
  159. with patch.dict(rh_service.__salt__, {'cmd.retcode':
  160. self._m_bool(False)}):
  161. self.assertTrue(rh_service.start('salt-api'))
  162. # 'stop' function tests: 1
  163. def test_stop(self):
  164. '''
  165. Test if it stop the specified service.
  166. '''
  167. with patch.object(rh_service, '_service_is_upstart', self._m_bool()):
  168. with patch.dict(rh_service.__salt__, {'cmd.retcode':
  169. self._m_bool(False)}):
  170. self.assertTrue(rh_service.stop('salt-api'))
  171. # 'restart' function tests: 1
  172. def test_restart(self):
  173. '''
  174. Test if it restart the specified service.
  175. '''
  176. with patch.object(rh_service, '_service_is_upstart', self._m_bool()):
  177. with patch.dict(rh_service.__salt__, {'cmd.retcode':
  178. self._m_bool(False)}):
  179. self.assertTrue(rh_service.restart('salt-api'))
  180. # 'reload_' function tests: 1
  181. def test_reload(self):
  182. '''
  183. Test if it reload the specified service.
  184. '''
  185. with patch.object(rh_service, '_service_is_upstart', self._m_bool()):
  186. with patch.dict(rh_service.__salt__, {'cmd.retcode':
  187. self._m_bool(False)}):
  188. self.assertTrue(rh_service.reload_('salt-api'))
  189. # 'status' function tests: 1
  190. def test_status(self):
  191. '''
  192. Test if it return the status for a service,
  193. returns a bool whether the service is running.
  194. '''
  195. with patch.object(rh_service, '_service_is_upstart', self._m_bool()):
  196. mock_run = MagicMock(return_value='start/running')
  197. with patch.dict(rh_service.__salt__, {'cmd.run': mock_run}):
  198. self.assertTrue(rh_service.status('salt-api'))
  199. with patch.object(rh_service, '_service_is_upstart',
  200. self._m_bool(False)):
  201. with patch.dict(rh_service.__salt__, {'status.pid':
  202. self._m_bool()}):
  203. self.assertTrue(rh_service.status('salt-api', sig=True))
  204. mock_ret = MagicMock(return_value=0)
  205. with patch.dict(rh_service.__salt__, {'cmd.retcode': mock_ret}):
  206. self.assertTrue(rh_service.status('salt-api'))
  207. # 'enable' function tests: 1
  208. def test_enable(self):
  209. '''
  210. Test if it enable the named service to start at boot.
  211. '''
  212. mock_bool = MagicMock(side_effect=[True, True, False])
  213. with patch.object(rh_service, '_service_is_upstart', mock_bool):
  214. with patch.object(rh_service, '_upstart_is_enabled', MagicMock(return_value=True)):
  215. with patch.object(rh_service, '_upstart_enable', MagicMock(return_value=False)):
  216. self.assertFalse(rh_service.enable('salt-api'))
  217. with patch.object(rh_service, '_upstart_enable', MagicMock(return_value=True)):
  218. self.assertTrue(rh_service.enable('salt-api'))
  219. with patch.object(rh_service, '_sysv_enable', self._m_bool()):
  220. self.assertTrue(rh_service.enable('salt-api'))
  221. # 'disable' function tests: 1
  222. def test_disable(self):
  223. '''
  224. Test if it disable the named service to start at boot.
  225. '''
  226. mock_bool = MagicMock(side_effect=[True, True, False])
  227. with patch.object(rh_service, '_service_is_upstart', mock_bool):
  228. with patch.object(rh_service, '_upstart_is_enabled', MagicMock(return_value=True)):
  229. with patch.object(rh_service, '_upstart_disable', MagicMock(return_value=False)):
  230. self.assertFalse(rh_service.disable('salt-api'))
  231. with patch.object(rh_service, '_upstart_disable', MagicMock(return_value=True)):
  232. self.assertTrue(rh_service.disable('salt-api'))
  233. with patch.object(rh_service, '_sysv_disable', self._m_bool()):
  234. self.assertTrue(rh_service.disable('salt-api'))
  235. # 'enabled' function tests: 1
  236. def test_enabled(self):
  237. '''
  238. Test if it check to see if the named service is enabled
  239. to start on boot.
  240. '''
  241. mock_bool = MagicMock(side_effect=[True, False])
  242. with patch.object(rh_service, '_service_is_upstart', mock_bool):
  243. with patch.object(rh_service, '_upstart_is_enabled', MagicMock(return_value=False)):
  244. self.assertFalse(rh_service.enabled('salt-api'))
  245. with patch.object(rh_service, '_sysv_is_enabled', self._m_bool()):
  246. self.assertTrue(rh_service.enabled('salt-api'))
  247. # 'disabled' function tests: 1
  248. def test_disabled(self):
  249. '''
  250. Test if it check to see if the named service is disabled
  251. to start on boot.
  252. '''
  253. mock_bool = MagicMock(side_effect=[True, False])
  254. with patch.object(rh_service, '_service_is_upstart', mock_bool):
  255. with patch.object(rh_service, '_upstart_is_enabled', MagicMock(return_value=False)):
  256. self.assertTrue(rh_service.disabled('salt-api'))
  257. with patch.object(rh_service, '_sysv_is_enabled',
  258. self._m_bool(False)):
  259. self.assertTrue(rh_service.disabled('salt-api'))