test_supervisord.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. # -*- coding: utf-8 -*-
  2. '''
  3. Tests for the supervisord state
  4. '''
  5. # Import python lins
  6. from __future__ import absolute_import, unicode_literals, print_function
  7. import os
  8. import time
  9. import subprocess
  10. # Import Salt Testing libs
  11. from tests.support.runtests import RUNTIME_VARS
  12. from tests.support.case import ModuleCase
  13. from tests.support.unit import skipIf
  14. from tests.support.mixins import SaltReturnAssertsMixin
  15. # Import salt libs
  16. import salt.utils.path
  17. from salt.modules.virtualenv_mod import KNOWN_BINARY_NAMES
  18. # Import 3rd-party libs
  19. from salt.ext import six
  20. @skipIf(six.PY3, 'supervisor does not work under python 3')
  21. @skipIf(salt.utils.path.which_bin(KNOWN_BINARY_NAMES) is None, 'virtualenv not installed')
  22. @skipIf(salt.utils.path.which('supervisorctl') is None, 'supervisord not installed')
  23. class SupervisordTest(ModuleCase, SaltReturnAssertsMixin):
  24. '''
  25. Validate the supervisord states.
  26. '''
  27. def setUp(self):
  28. super(SupervisordTest, self).setUp()
  29. self.venv_test_dir = os.path.join(RUNTIME_VARS.TMP, 'supervisortests')
  30. self.venv_dir = os.path.join(self.venv_test_dir, 'venv')
  31. self.supervisor_sock = os.path.join(self.venv_dir, 'supervisor.sock')
  32. if not os.path.exists(self.venv_dir):
  33. os.makedirs(self.venv_test_dir)
  34. self.run_function('virtualenv.create', [self.venv_dir])
  35. self.run_function(
  36. 'pip.install', [], pkgs='supervisor', bin_env=self.venv_dir)
  37. self.supervisord = os.path.join(self.venv_dir, 'bin', 'supervisord')
  38. if not os.path.exists(self.supervisord):
  39. self.skipTest('Failed to installe supervisor in test virtualenv')
  40. self.supervisor_conf = os.path.join(self.venv_dir, 'supervisor.conf')
  41. def start_supervisord(self, autostart=True):
  42. self.run_state(
  43. 'file.managed', name=self.supervisor_conf,
  44. source='salt://supervisor.conf', template='jinja',
  45. context={
  46. 'supervisor_sock': self.supervisor_sock,
  47. 'virtual_env': self.venv_dir,
  48. 'autostart': autostart
  49. }
  50. )
  51. if not os.path.exists(self.supervisor_conf):
  52. self.skipTest('failed to create supervisor config file')
  53. self.supervisor_proc = subprocess.Popen(
  54. [self.supervisord, '-c', self.supervisor_conf]
  55. )
  56. if self.supervisor_proc.poll() is not None:
  57. self.skipTest('failed to start supervisord')
  58. timeout = 10
  59. while not os.path.exists(self.supervisor_sock):
  60. if timeout == 0:
  61. self.skipTest(
  62. 'supervisor socket not found - failed to start supervisord'
  63. )
  64. break
  65. else:
  66. time.sleep(1)
  67. timeout -= 1
  68. def tearDown(self):
  69. if hasattr(self, 'supervisor_proc') and \
  70. self.supervisor_proc.poll() is not None:
  71. self.run_function(
  72. 'supervisord.custom', ['shutdown'],
  73. conf_file=self.supervisor_conf, bin_env=self.venv_dir)
  74. self.supervisor_proc.wait()
  75. del self.supervisor_proc
  76. del self.venv_test_dir
  77. del self.venv_dir
  78. del self.supervisord
  79. del self.supervisor_conf
  80. del self.supervisor_sock
  81. def test_running_stopped(self):
  82. '''
  83. supervisord.running restart = False
  84. When service is stopped.
  85. '''
  86. self.start_supervisord(autostart=False)
  87. ret = self.run_state(
  88. 'supervisord.running', name='sleep_service',
  89. bin_env=self.venv_dir, conf_file=self.supervisor_conf
  90. )
  91. self.assertSaltTrueReturn(ret)
  92. self.assertInSaltReturn('sleep_service', ret, ['changes'])
  93. def test_running_started(self):
  94. '''
  95. supervisord.running restart = False
  96. When service is running.
  97. '''
  98. self.start_supervisord(autostart=True)
  99. ret = self.run_state(
  100. 'supervisord.running', name='sleep_service',
  101. bin_env=self.venv_dir, conf_file=self.supervisor_conf
  102. )
  103. self.assertSaltTrueReturn(ret)
  104. self.assertNotInSaltReturn('sleep_service', ret, ['changes'])
  105. def test_running_needsupdate(self):
  106. '''
  107. supervisord.running restart = False
  108. When service needs to be added.
  109. '''
  110. self.start_supervisord(autostart=False)
  111. self.run_function('supervisord.remove', [
  112. 'sleep_service',
  113. None,
  114. self.supervisor_conf,
  115. self.venv_dir
  116. ])
  117. ret = self.run_state(
  118. 'supervisord.running', name='sleep_service',
  119. bin_env=self.venv_dir, conf_file=self.supervisor_conf
  120. )
  121. self.assertSaltTrueReturn(ret)
  122. self.assertInSaltReturn('sleep_service', ret, ['changes'])
  123. def test_running_notexists(self):
  124. '''
  125. supervisord.running restart = False
  126. When service doesn't exist.
  127. '''
  128. self.start_supervisord(autostart=True)
  129. ret = self.run_state(
  130. 'supervisord.running', name='does_not_exist',
  131. bin_env=self.venv_dir, conf_file=self.supervisor_conf
  132. )
  133. self.assertSaltFalseReturn(ret)
  134. def test_restart_started(self):
  135. '''
  136. supervisord.running restart = True
  137. When service is running.
  138. '''
  139. self.start_supervisord(autostart=True)
  140. ret = self.run_state(
  141. 'supervisord.running', name='sleep_service',
  142. restart=True,
  143. bin_env=self.venv_dir, conf_file=self.supervisor_conf
  144. )
  145. self.assertSaltTrueReturn(ret)
  146. self.assertInSaltReturn('sleep_service', ret, ['changes'])
  147. def test_restart_stopped(self):
  148. '''
  149. supervisord.running restart = True
  150. When service is stopped.
  151. '''
  152. self.start_supervisord(autostart=False)
  153. ret = self.run_state(
  154. 'supervisord.running', name='sleep_service',
  155. restart=True,
  156. bin_env=self.venv_dir, conf_file=self.supervisor_conf
  157. )
  158. self.assertSaltTrueReturn(ret)
  159. self.assertInSaltReturn('sleep_service', ret, ['changes'])
  160. def test_restart_needsupdate(self):
  161. '''
  162. supervisord.running restart = True
  163. When service needs to be added.
  164. '''
  165. self.start_supervisord(autostart=False)
  166. self.run_function('supervisord.remove', [
  167. 'sleep_service',
  168. None,
  169. self.supervisor_conf,
  170. self.venv_dir
  171. ])
  172. ret = self.run_state(
  173. 'supervisord.running', name='sleep_service',
  174. restart=True,
  175. bin_env=self.venv_dir, conf_file=self.supervisor_conf
  176. )
  177. self.assertSaltTrueReturn(ret)
  178. self.assertInSaltReturn('sleep_service', ret, ['changes'])
  179. def test_restart_notexists(self):
  180. '''
  181. supervisord.running restart = True
  182. When service does not exist.
  183. '''
  184. self.start_supervisord(autostart=True)
  185. ret = self.run_state(
  186. 'supervisord.running', name='does_not_exist',
  187. restart=True,
  188. bin_env=self.venv_dir, conf_file=self.supervisor_conf
  189. )
  190. self.assertSaltFalseReturn(ret)
  191. self.assertNotInSaltReturn('sleep_service', ret, ['changes'])
  192. def test_dead_started(self):
  193. '''
  194. supervisord.dead
  195. When service is running.
  196. '''
  197. self.start_supervisord(autostart=True)
  198. ret = self.run_state(
  199. 'supervisord.dead', name='sleep_service',
  200. bin_env=self.venv_dir, conf_file=self.supervisor_conf
  201. )
  202. self.assertSaltTrueReturn(ret)
  203. def test_dead_stopped(self):
  204. '''
  205. supervisord.dead
  206. When service is stopped.
  207. '''
  208. self.start_supervisord(autostart=False)
  209. ret = self.run_state(
  210. 'supervisord.dead', name='sleep_service',
  211. bin_env=self.venv_dir, conf_file=self.supervisor_conf
  212. )
  213. self.assertSaltTrueReturn(ret)
  214. def test_dead_removed(self):
  215. '''
  216. supervisord.dead
  217. When service needs to be added.
  218. '''
  219. self.start_supervisord(autostart=False)
  220. self.run_function('supervisord.remove', [
  221. 'sleep_service',
  222. None,
  223. self.supervisor_conf,
  224. self.venv_dir
  225. ])
  226. ret = self.run_state(
  227. 'supervisord.dead', name='sleep_service',
  228. bin_env=self.venv_dir, conf_file=self.supervisor_conf
  229. )
  230. self.assertSaltTrueReturn(ret)
  231. def test_dead_notexists(self):
  232. '''
  233. supervisord.dead
  234. When service does not exist.
  235. '''
  236. self.start_supervisord(autostart=True)
  237. ret = self.run_state(
  238. 'supervisord.dead', name='does_not_exist',
  239. bin_env=self.venv_dir, conf_file=self.supervisor_conf
  240. )
  241. self.assertSaltTrueReturn(ret)