Merge "Remove dependency on strings for kill_process"
This commit is contained in:
commit
241e010769
@ -219,11 +219,8 @@ def kill_process(pid, signal, run_as_root=False):
|
||||
"""Kill the process with the given pid using the given signal."""
|
||||
try:
|
||||
execute(['kill', '-%d' % signal, pid], run_as_root=run_as_root)
|
||||
except ProcessExecutionError as ex:
|
||||
# TODO(dalvarez): this check has i18n issues. Maybe we can use
|
||||
# use gettext module setting a global locale or just pay attention
|
||||
# to returncode instead of checking the ex message.
|
||||
if 'No such process' not in str(ex):
|
||||
except ProcessExecutionError:
|
||||
if process_is_running(pid):
|
||||
raise
|
||||
|
||||
|
||||
@ -308,8 +305,15 @@ def remove_abs_path(cmd):
|
||||
return cmd
|
||||
|
||||
|
||||
def process_is_running(pid):
|
||||
"""Find if the given PID is running in the system.
|
||||
|
||||
"""
|
||||
return pid and os.path.exists('/proc/%s' % pid)
|
||||
|
||||
|
||||
def get_cmdline_from_pid(pid):
|
||||
if pid is None or not os.path.exists('/proc/%s' % pid):
|
||||
if not process_is_running(pid):
|
||||
return []
|
||||
with open('/proc/%s/cmdline' % pid, 'r') as f:
|
||||
return f.readline().split('\0')[:-1]
|
||||
|
@ -245,15 +245,17 @@ class TestFindForkTopParent(base.BaseTestCase):
|
||||
|
||||
|
||||
class TestKillProcess(base.BaseTestCase):
|
||||
def _test_kill_process(self, pid, exception_message=None,
|
||||
kill_signal=signal.SIGKILL):
|
||||
if exception_message:
|
||||
exc = utils.ProcessExecutionError(exception_message, returncode=0)
|
||||
def _test_kill_process(self, pid, raise_exception=False,
|
||||
kill_signal=signal.SIGKILL, pid_killed=True):
|
||||
if raise_exception:
|
||||
exc = utils.ProcessExecutionError('', returncode=0)
|
||||
else:
|
||||
exc = None
|
||||
with mock.patch.object(utils, 'execute',
|
||||
side_effect=exc) as mock_execute:
|
||||
utils.kill_process(pid, kill_signal, run_as_root=True)
|
||||
with mock.patch.object(utils, 'process_is_running',
|
||||
return_value=not pid_killed):
|
||||
utils.kill_process(pid, kill_signal, run_as_root=True)
|
||||
|
||||
mock_execute.assert_called_with(['kill', '-%d' % kill_signal, pid],
|
||||
run_as_root=True)
|
||||
@ -262,11 +264,14 @@ class TestKillProcess(base.BaseTestCase):
|
||||
self._test_kill_process('1')
|
||||
|
||||
def test_kill_process_returns_none_for_stale_pid(self):
|
||||
self._test_kill_process('1', 'No such process')
|
||||
self._test_kill_process('1', raise_exception=True)
|
||||
|
||||
def test_kill_process_raises_exception_for_execute_exception(self):
|
||||
with testtools.ExpectedException(utils.ProcessExecutionError):
|
||||
self._test_kill_process('1', 'Invalid')
|
||||
# Simulate that the process is running after trying to kill due to
|
||||
# any reason such as, for example, Permission denied
|
||||
self._test_kill_process('1', raise_exception=True,
|
||||
pid_killed=False)
|
||||
|
||||
def test_kill_process_with_different_signal(self):
|
||||
self._test_kill_process('1', kill_signal=signal.SIGTERM)
|
||||
|
Loading…
x
Reference in New Issue
Block a user