1
0

test_supervisord.py 9.1 KB

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