2013-06-27 11:49:15 +00:00
|
|
|
# Import python
|
2012-09-25 21:15:38 +00:00
|
|
|
import os
|
2013-05-29 10:39:09 +00:00
|
|
|
import time
|
2013-06-25 08:08:48 +00:00
|
|
|
import subprocess
|
|
|
|
|
2013-06-27 11:49:15 +00:00
|
|
|
# Import Salt Testing libs
|
|
|
|
from salttesting.helpers import ensure_in_syspath
|
|
|
|
ensure_in_syspath('../../')
|
|
|
|
|
2013-06-25 08:08:48 +00:00
|
|
|
# Import salt libs
|
2013-06-27 11:49:15 +00:00
|
|
|
import integration
|
2012-09-15 17:41:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SupervisordModuleTest(integration.ModuleCase):
|
|
|
|
'''
|
|
|
|
Validates the supervisorctl functions.
|
|
|
|
'''
|
|
|
|
def setUp(self):
|
|
|
|
super(SupervisordModuleTest, self).setUp()
|
2013-05-28 23:18:12 +00:00
|
|
|
ret = self.run_function('cmd.has_exec', ['virtualenv'])
|
2012-09-15 17:41:01 +00:00
|
|
|
if not ret:
|
2013-05-28 23:18:12 +00:00
|
|
|
self.skipTest('virtualenv not installed')
|
2013-05-29 11:54:57 +00:00
|
|
|
|
|
|
|
self.venv_test_dir = os.path.join(integration.TMP, 'supervisortests')
|
2013-05-28 23:18:12 +00:00
|
|
|
self.venv_dir = os.path.join(self.venv_test_dir, 'venv')
|
2013-05-29 10:39:09 +00:00
|
|
|
self.supervisor_sock = os.path.join(self.venv_dir, 'supervisor.sock')
|
2013-05-28 23:18:12 +00:00
|
|
|
|
2013-05-29 11:54:57 +00:00
|
|
|
if not os.path.exists(self.venv_dir):
|
|
|
|
os.makedirs(self.venv_test_dir)
|
|
|
|
self.run_function('virtualenv.create', [self.venv_dir])
|
2013-06-25 08:08:48 +00:00
|
|
|
self.run_function(
|
|
|
|
'pip.install', [], pkgs='supervisor', bin_env=self.venv_dir)
|
2013-05-28 23:18:12 +00:00
|
|
|
|
|
|
|
self.supervisord = os.path.join(self.venv_dir, 'bin', 'supervisord')
|
|
|
|
if not os.path.exists(self.supervisord):
|
2013-05-29 11:54:57 +00:00
|
|
|
self.skipTest('Failed to install supervisor in test virtualenv')
|
2013-05-28 23:18:12 +00:00
|
|
|
self.supervisor_conf = os.path.join(self.venv_dir, 'supervisor.conf')
|
|
|
|
|
|
|
|
def start_supervisord(self, autostart=True):
|
2013-06-25 08:08:48 +00:00
|
|
|
self.run_state(
|
|
|
|
'file.managed', name=self.supervisor_conf,
|
|
|
|
source='salt://supervisor.conf', template='jinja',
|
2013-05-28 23:18:12 +00:00
|
|
|
context={
|
2013-06-25 08:08:48 +00:00
|
|
|
'supervisor_sock': self.supervisor_sock,
|
|
|
|
'virtual_env': self.venv_dir,
|
2013-05-28 23:18:12 +00:00
|
|
|
'autostart': autostart
|
|
|
|
}
|
|
|
|
)
|
|
|
|
if not os.path.exists(self.supervisor_conf):
|
|
|
|
self.skipTest('failed to create supervisor config file')
|
2013-06-25 08:08:48 +00:00
|
|
|
self.supervisor_proc = subprocess.Popen(
|
|
|
|
[self.supervisord, '-c', self.supervisor_conf]
|
|
|
|
)
|
2013-05-28 23:18:12 +00:00
|
|
|
if self.supervisor_proc.poll() is not None:
|
|
|
|
self.skipTest('failed to start supervisord')
|
2013-05-29 10:39:09 +00:00
|
|
|
timeout = 10
|
|
|
|
while not os.path.exists(self.supervisor_sock):
|
|
|
|
if timeout == 0:
|
2013-06-25 08:08:48 +00:00
|
|
|
self.skipTest(
|
|
|
|
'supervisor socket not found - failed to start supervisord'
|
|
|
|
)
|
2013-05-29 10:39:09 +00:00
|
|
|
break
|
|
|
|
else:
|
|
|
|
time.sleep(1)
|
|
|
|
timeout -= 1
|
2013-05-28 23:18:12 +00:00
|
|
|
|
|
|
|
def tearDown(self):
|
2013-06-25 08:08:48 +00:00
|
|
|
if hasattr(self, 'supervisor_proc') and \
|
|
|
|
self.supervisor_proc.poll() is not None:
|
|
|
|
self.run_function(
|
|
|
|
'supervisord.custom', ['shutdown'],
|
|
|
|
conf_file=self.supervisor_conf, bin_env=self.venv_dir)
|
2013-05-28 23:18:12 +00:00
|
|
|
self.supervisor_proc.wait()
|
2012-09-15 17:41:01 +00:00
|
|
|
|
|
|
|
def test_start_all(self):
|
|
|
|
'''
|
2013-05-28 23:18:12 +00:00
|
|
|
Start all services when they are not running.
|
|
|
|
'''
|
|
|
|
self.start_supervisord(autostart=False)
|
2013-06-25 08:08:48 +00:00
|
|
|
ret = self.run_function(
|
|
|
|
'supervisord.start', [], conf_file=self.supervisor_conf,
|
|
|
|
bin_env=self.venv_dir)
|
2013-09-05 06:54:03 +00:00
|
|
|
self.assertIn('sleep_service: started', ret)
|
|
|
|
self.assertIn('sleep_service2: started', ret)
|
2013-05-28 23:18:12 +00:00
|
|
|
|
|
|
|
def test_start_all_already_running(self):
|
|
|
|
'''
|
|
|
|
Start all services when they are running.
|
2012-09-15 17:41:01 +00:00
|
|
|
'''
|
2013-05-28 23:18:12 +00:00
|
|
|
self.start_supervisord(autostart=True)
|
2013-06-25 08:08:48 +00:00
|
|
|
ret = self.run_function(
|
|
|
|
'supervisord.start', [], conf_file=self.supervisor_conf,
|
|
|
|
bin_env=self.venv_dir
|
|
|
|
)
|
2012-09-17 19:34:50 +00:00
|
|
|
self.assertEqual(ret, '')
|
2012-09-15 17:41:01 +00:00
|
|
|
|
|
|
|
def test_start_one(self):
|
|
|
|
'''
|
2013-05-28 23:18:12 +00:00
|
|
|
Start a specific service that is not running.
|
|
|
|
'''
|
|
|
|
self.start_supervisord(autostart=False)
|
2013-06-25 08:08:48 +00:00
|
|
|
ret = self.run_function(
|
|
|
|
'supervisord.start', ['sleep_service'],
|
|
|
|
conf_file=self.supervisor_conf, bin_env=self.venv_dir)
|
2013-05-28 23:18:12 +00:00
|
|
|
self.assertEqual(ret, 'sleep_service: started')
|
|
|
|
|
|
|
|
def test_start_one_already_running(self):
|
|
|
|
'''
|
2013-06-25 08:08:48 +00:00
|
|
|
Try to start a specific service that is running.
|
2012-09-15 17:41:01 +00:00
|
|
|
'''
|
2013-05-28 23:18:12 +00:00
|
|
|
self.start_supervisord(autostart=True)
|
2013-06-25 08:08:48 +00:00
|
|
|
ret = self.run_function(
|
|
|
|
'supervisord.start', ['sleep_service'],
|
|
|
|
conf_file=self.supervisor_conf, bin_env=self.venv_dir)
|
2013-05-28 23:18:12 +00:00
|
|
|
self.assertEqual(ret, 'sleep_service: ERROR (already started)')
|
2012-09-15 18:02:00 +00:00
|
|
|
|
|
|
|
def test_restart_all(self):
|
|
|
|
'''
|
2013-05-28 23:18:12 +00:00
|
|
|
Restart all services when they are running.
|
2012-09-15 18:02:00 +00:00
|
|
|
'''
|
2013-05-28 23:18:12 +00:00
|
|
|
self.start_supervisord(autostart=True)
|
2013-06-25 08:08:48 +00:00
|
|
|
ret = self.run_function(
|
|
|
|
'supervisord.restart', [], conf_file=self.supervisor_conf,
|
|
|
|
bin_env=self.venv_dir)
|
2013-09-05 03:37:26 +00:00
|
|
|
self.assertIn('sleep_service: stopped', ret)
|
|
|
|
self.assertIn('sleep_service2: stopped', ret)
|
|
|
|
self.assertIn('sleep_service: started', ret)
|
|
|
|
self.assertIn('sleep_service2: started', ret)
|
2013-05-28 23:18:12 +00:00
|
|
|
|
|
|
|
def test_restart_all_not_running(self):
|
|
|
|
'''
|
|
|
|
Restart all services when they are not running.
|
|
|
|
'''
|
|
|
|
self.start_supervisord(autostart=False)
|
2013-06-25 08:08:48 +00:00
|
|
|
ret = self.run_function(
|
|
|
|
'supervisord.restart', [], conf_file=self.supervisor_conf,
|
|
|
|
bin_env=self.venv_dir)
|
2013-09-04 21:40:50 +00:00
|
|
|
# These 2 services might return in different orders so test separately
|
|
|
|
self.assertIn('sleep_service: started', ret)
|
|
|
|
self.assertIn('sleep_service2: started', ret)
|
2012-09-15 18:02:00 +00:00
|
|
|
|
|
|
|
def test_restart_one(self):
|
|
|
|
'''
|
2013-05-28 23:18:12 +00:00
|
|
|
Restart a specific service that is running.
|
|
|
|
'''
|
|
|
|
self.start_supervisord(autostart=True)
|
2013-06-25 08:08:48 +00:00
|
|
|
ret = self.run_function(
|
|
|
|
'supervisord.restart', ['sleep_service'],
|
|
|
|
conf_file=self.supervisor_conf, bin_env=self.venv_dir)
|
2013-05-28 23:18:12 +00:00
|
|
|
self.assertEqual(ret, 'sleep_service: stopped\nsleep_service: started')
|
|
|
|
|
|
|
|
def test_restart_one_not_running(self):
|
|
|
|
'''
|
|
|
|
Restart a specific service that is not running.
|
2012-09-15 18:02:00 +00:00
|
|
|
'''
|
2013-05-28 23:18:12 +00:00
|
|
|
self.start_supervisord(autostart=False)
|
2013-06-25 08:08:48 +00:00
|
|
|
ret = self.run_function(
|
|
|
|
'supervisord.restart', ['sleep_service'],
|
|
|
|
conf_file=self.supervisor_conf, bin_env=self.venv_dir)
|
2013-09-05 06:54:03 +00:00
|
|
|
self.assertIn('sleep_service: ERROR (not running)', ret)
|
|
|
|
self.assertIn('sleep_service: started', ret)
|
2012-09-15 18:02:00 +00:00
|
|
|
|
|
|
|
def test_stop_all(self):
|
|
|
|
'''
|
2013-05-28 23:18:12 +00:00
|
|
|
Stop all services when they are running.
|
2012-09-15 18:02:00 +00:00
|
|
|
'''
|
2013-05-28 23:18:12 +00:00
|
|
|
self.start_supervisord(autostart=True)
|
2013-06-25 08:08:48 +00:00
|
|
|
ret = self.run_function(
|
|
|
|
'supervisord.stop', [], conf_file=self.supervisor_conf,
|
|
|
|
bin_env=self.venv_dir)
|
2013-08-13 06:32:32 +00:00
|
|
|
self.assertIn('sleep_service: stopped', ret)
|
2013-09-06 17:16:04 +00:00
|
|
|
self.assertIn('sleep_service2: stopped', ret)
|
2013-05-28 23:18:12 +00:00
|
|
|
|
|
|
|
def test_stop_all_not_running(self):
|
|
|
|
'''
|
|
|
|
Stop all services when they are not running.
|
|
|
|
'''
|
|
|
|
self.start_supervisord(autostart=False)
|
2013-06-25 08:08:48 +00:00
|
|
|
ret = self.run_function(
|
|
|
|
'supervisord.stop', [], conf_file=self.supervisor_conf,
|
|
|
|
bin_env=self.venv_dir)
|
2012-09-17 19:34:50 +00:00
|
|
|
self.assertEqual(ret, '')
|
2012-09-15 18:02:00 +00:00
|
|
|
|
|
|
|
def test_stop_one(self):
|
|
|
|
'''
|
2013-05-28 23:18:12 +00:00
|
|
|
Stop a specific service that is running.
|
|
|
|
'''
|
|
|
|
self.start_supervisord(autostart=True)
|
2013-06-25 08:08:48 +00:00
|
|
|
ret = self.run_function(
|
|
|
|
'supervisord.stop', ['sleep_service'],
|
|
|
|
conf_file=self.supervisor_conf, bin_env=self.venv_dir)
|
2013-05-28 23:18:12 +00:00
|
|
|
self.assertEqual(ret, 'sleep_service: stopped')
|
|
|
|
|
|
|
|
def test_stop_one_not_running(self):
|
|
|
|
'''
|
|
|
|
Stop a specific service that is not running.
|
2012-09-15 18:02:00 +00:00
|
|
|
'''
|
2013-05-28 23:18:12 +00:00
|
|
|
self.start_supervisord(autostart=False)
|
2013-06-25 08:08:48 +00:00
|
|
|
ret = self.run_function(
|
|
|
|
'supervisord.stop', ['sleep_service'],
|
|
|
|
conf_file=self.supervisor_conf, bin_env=self.venv_dir)
|
2013-05-28 23:18:12 +00:00
|
|
|
self.assertEqual(ret, 'sleep_service: ERROR (not running)')
|
2012-09-15 18:02:00 +00:00
|
|
|
|
|
|
|
def test_status_all(self):
|
|
|
|
'''
|
2013-05-28 23:18:12 +00:00
|
|
|
Status for all services
|
2012-09-15 18:02:00 +00:00
|
|
|
'''
|
2013-05-28 23:18:12 +00:00
|
|
|
self.start_supervisord(autostart=True)
|
2013-06-25 08:08:48 +00:00
|
|
|
ret = self.run_function(
|
|
|
|
'supervisord.status', [], conf_file=self.supervisor_conf,
|
|
|
|
bin_env=self.venv_dir)
|
2013-05-28 23:18:12 +00:00
|
|
|
self.assertEqual(ret.keys(), ['sleep_service', 'sleep_service2'])
|
2012-09-15 18:02:00 +00:00
|
|
|
|
|
|
|
def test_status_one(self):
|
|
|
|
'''
|
2013-05-28 23:18:12 +00:00
|
|
|
Status for a specific service.
|
2012-09-15 18:02:00 +00:00
|
|
|
'''
|
2013-05-28 23:18:12 +00:00
|
|
|
self.start_supervisord(autostart=True)
|
2013-06-25 08:08:48 +00:00
|
|
|
ret = self.run_function(
|
|
|
|
'supervisord.status', ['sleep_service'],
|
|
|
|
conf_file=self.supervisor_conf, bin_env=self.venv_dir)
|
2012-09-17 19:34:50 +00:00
|
|
|
self.assertTrue(ret)
|
2013-06-25 08:08:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
from integration import run_tests
|
|
|
|
run_tests(SupervisordModuleTest)
|