test_supervisord.py 8.5 KB

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