summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2017-04-23 23:12:23 +0200
committerGiampaolo Rodola <g.rodola@gmail.com>2017-04-23 23:12:23 +0200
commitf112ef9a0c7cc65e6604e4572de154e9cf27c95f (patch)
treefc1dee52d17a6b5bf0de7c2d2c027120df3db76d
parent76d36b417ab1f0c7fa9070a2887cbcdae1bea66c (diff)
downloadpsutil-f112ef9a0c7cc65e6604e4572de154e9cf27c95f.tar.gz
add UNIX test case for name()s longer than 15 chars
-rwxr-xr-xpsutil/tests/test_posix.py36
1 files changed, 36 insertions, 0 deletions
diff --git a/psutil/tests/test_posix.py b/psutil/tests/test_posix.py
index 16d1eb7e..8ecca477 100755
--- a/psutil/tests/test_posix.py
+++ b/psutil/tests/test_posix.py
@@ -123,6 +123,42 @@ class TestProcess(unittest.TestCase):
name_psutil = psutil.Process(self.pid).name().lower()
self.assertEqual(name_ps, name_psutil)
+ def test_name_long(self):
+ # On UNIX the kernel truncates the name to the first 15
+ # characters. In sich a case psutil tries to determine the
+ # full name from the cmdline.
+ name = "long-program-name"
+ cmdline = ["long-program-name-extended", "foo", "bar"]
+ with mock.patch("psutil._psplatform.Process.name",
+ return_value=name):
+ with mock.patch("psutil._psplatform.Process.cmdline",
+ return_value=cmdline):
+ p = psutil.Process()
+ self.assertEqual(p.name(), "long-program-name-extended")
+
+ def test_name_long_cmdline_ad_exc(self):
+ # Same as above but emulates a case where cmdline() raises
+ # AccessDenied in which case psutil is supposed to return
+ # the truncated name instead of crashing.
+ name = "long-program-name"
+ with mock.patch("psutil._psplatform.Process.name",
+ return_value=name):
+ with mock.patch("psutil._psplatform.Process.cmdline",
+ side_effect=psutil.AccessDenied(0, "")):
+ p = psutil.Process()
+ self.assertEqual(p.name(), "long-program-name")
+
+ def test_name_long_cmdline_nsp_exc(self):
+ # Same as above but emulates a case where cmdline() raises NSP
+ # which is supposed to propagate.
+ name = "long-program-name"
+ with mock.patch("psutil._psplatform.Process.name",
+ return_value=name):
+ with mock.patch("psutil._psplatform.Process.cmdline",
+ side_effect=psutil.NoSuchProcess(0, "")):
+ p = psutil.Process()
+ self.assertRaises(psutil.NoSuchProcess, p.name)
+
@unittest.skipIf(OSX or BSD, 'ps -o start not available')
def test_create_time(self):
time_ps = ps("ps --no-headers -o start -p %s" % self.pid).split(' ')[0]