Add extra unit test for get_cmdline_from_pid function
In patch [1] get_cmdline_from_pid function was modified to be able to parse process' cmdline files with arguments separated by space instead of '\0' char. This patch adds one extra UT and small refactor which was pointed in comments to [1] but which I was not able to change there. [1] https://review.openstack.org/#/c/647605/ Change-Id: Ibd91d0472a686eca79a1126154d9cdf4587c1a19 Related-Bug: #1820870 (cherry picked from commitdcc3d3c754
) (cherry picked from commit4bab42e94c
)
This commit is contained in:
parent
f7b5b7170d
commit
b5ce088ad9
@ -314,20 +314,18 @@ def get_cmdline_from_pid(pid):
|
||||
if not process_is_running(pid):
|
||||
return []
|
||||
with open('/proc/%s/cmdline' % pid, 'r') as f:
|
||||
cmdline = f.readline()
|
||||
cmdline_args = cmdline.split('\0')[:-1]
|
||||
cmdline = f.readline().split('\0')[:-1]
|
||||
|
||||
# NOTE(slaweq): sometimes it may happen that values in
|
||||
# /proc/{pid}/cmdline are separated by space instead of NUL char,
|
||||
# in such case we would have everything in one element of cmdline_args
|
||||
# list and it would not match to expected cmd so we need to try to
|
||||
# split it by spaces
|
||||
if len(cmdline_args) == 1:
|
||||
cmdline_args = cmdline_args[0].split(' ')
|
||||
if len(cmdline) == 1:
|
||||
cmdline = cmdline[0].split(' ')
|
||||
|
||||
LOG.debug("Found cmdline %s for rocess with PID %s.",
|
||||
cmdline_args, pid)
|
||||
return cmdline_args
|
||||
LOG.debug("Found cmdline %s for process with PID %s.", cmdline, pid)
|
||||
return cmdline
|
||||
|
||||
|
||||
def cmd_matches_expected(cmd, expected_cmd):
|
||||
|
@ -322,6 +322,11 @@ class TestGetCmdlineFromPid(base.BaseTestCase):
|
||||
expected_cmdline = ["python3", "test-binary", "test", "option"]
|
||||
self._test_cmdline(process_cmd, expected_cmdline)
|
||||
|
||||
def test_cmdline_with_single_argument(self):
|
||||
process_cmd = "test-binary\0"
|
||||
expected_cmdline = ["test-binary"]
|
||||
self._test_cmdline(process_cmd, expected_cmdline)
|
||||
|
||||
def test_no_process_running(self):
|
||||
self.process_is_running_mock.return_value = False
|
||||
mock_open = self.useFixture(
|
||||
|
Loading…
Reference in New Issue
Block a user