From dcc3d3c7548eb92634b84d5376a73f78421ba23f Mon Sep 17 00:00:00 2001 From: Slawek Kaplonski Date: Wed, 27 Mar 2019 10:48:20 +0100 Subject: [PATCH] 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 --- neutron/agent/linux/utils.py | 12 +++++------- neutron/tests/unit/agent/linux/test_utils.py | 5 +++++ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/neutron/agent/linux/utils.py b/neutron/agent/linux/utils.py index 249b3d26fca..d90cf41ccd3 100644 --- a/neutron/agent/linux/utils.py +++ b/neutron/agent/linux/utils.py @@ -324,20 +324,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): diff --git a/neutron/tests/unit/agent/linux/test_utils.py b/neutron/tests/unit/agent/linux/test_utils.py index c09de840a56..1a2e796e8d5 100644 --- a/neutron/tests/unit/agent/linux/test_utils.py +++ b/neutron/tests/unit/agent/linux/test_utils.py @@ -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(