1
0

test_supervisord.py 9.0 KB

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