Merge "Ignore invalid PIDs in swift-init"

This commit is contained in:
Jenkins 2015-07-09 05:56:36 +00:00 committed by Gerrit Code Review
commit 23258fb88c
2 changed files with 36 additions and 2 deletions

View File

@ -497,7 +497,11 @@ class Server(object):
"""Generator, yields (pid_file, pids)
"""
for pid_file in self.pid_files(**kwargs):
yield pid_file, int(open(pid_file).read().strip())
try:
pid = int(open(pid_file).read().strip())
except ValueError:
pid = None
yield pid_file, pid
def signal_pids(self, sig, **kwargs):
"""Send a signal to pids for this server
@ -509,6 +513,10 @@ class Server(object):
"""
pids = {}
for pid_file, pid in self.iter_pid_files(**kwargs):
if not pid: # Catches None and 0
print _('Removing pid file %s with invalid pid') % pid_file
remove_file(pid_file)
continue
try:
if sig != signal.SIG_DFL:
print _('Signal %s pid: %s signal: %s') % (self.server,

View File

@ -609,7 +609,8 @@ class TestServer(unittest.TestCase):
self.assertRaises(StopIteration, iter.next)
# test invalid value in pid file
server = manager.Server('auth', run_dir=t)
self.assertRaises(ValueError, server.iter_pid_files().next)
pid_file, pid = server.iter_pid_files().next()
self.assertEqual(None, pid)
# test object-server doesn't steal pids from object-replicator
server = manager.Server('object', run_dir=t)
self.assertRaises(StopIteration, server.iter_pid_files().next)
@ -698,10 +699,12 @@ class TestServer(unittest.TestCase):
def test_signal_pids(self):
temp_files = (
('var/run/zero-server.pid', 0),
('var/run/proxy-server.pid', 1),
('var/run/auth-server.pid', 2),
('var/run/one-server.pid', 3),
('var/run/object-server.pid', 4),
('var/run/invalid-server.pid', 'Forty-Two'),
('proc/3/cmdline', 'swift-another-server')
)
with temptree(*zip(*temp_files)) as t:
@ -758,6 +761,29 @@ class TestServer(unittest.TestCase):
self.assert_('removing pid file' in output.lower())
one_pid = self.join_run_dir('one-server.pid')
self.assert_(one_pid in output)
server = manager.Server('zero', run_dir=manager.RUN_DIR)
self.assertTrue(os.path.exists(
self.join_run_dir('zero-server.pid'))) # sanity
# test verbose warns on removing pid file with invalid pid
pids = server.signal_pids(signal.SIG_DFL, verbose=True)
output = pop_stream(f)
old_stdout.write('output %s' % output)
self.assert_('with invalid pid' in output.lower())
self.assertFalse(os.path.exists(
self.join_run_dir('zero-server.pid')))
server = manager.Server('invalid-server',
run_dir=manager.RUN_DIR)
self.assertTrue(os.path.exists(
self.join_run_dir('invalid-server.pid'))) # sanity
# test verbose warns on removing pid file with invalid pid
pids = server.signal_pids(signal.SIG_DFL, verbose=True)
output = pop_stream(f)
old_stdout.write('output %s' % output)
self.assert_('with invalid pid' in output.lower())
self.assertFalse(os.path.exists(
self.join_run_dir('invalid-server.pid')))
# reset mock os with no running pids
manager.os = MockOs([])
# test warning with insufficient permissions