From f0d199e87930b6e5d5f9edfc0b077e21b2487766 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Fri, 15 May 2015 00:53:07 -0400 Subject: add test for setup.py script --- test/test_psutil.py | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'test/test_psutil.py') diff --git a/test/test_psutil.py b/test/test_psutil.py index b53ff680..fa2b62c2 100644 --- a/test/test_psutil.py +++ b/test/test_psutil.py @@ -22,6 +22,7 @@ import contextlib import datetime import errno import functools +import imp import json import os import pickle @@ -2635,6 +2636,12 @@ class TestMisc(unittest.TestCase): check(psutil.disk_usage(os.getcwd())) check(psutil.users()) + def test_setup_script(self): + here = os.path.abspath(os.path.dirname(__file__)) + setup_py = os.path.realpath(os.path.join(here, '..', 'setup.py')) + module = imp.load_source('setup', setup_py) + self.assertRaises(SystemExit, module.setup) + # =================================================================== # --- Example script tests -- cgit v1.2.1 From d3239a650eaa2595fdd8c2eca9cda9a599b17a5b Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Wed, 3 Jun 2015 11:48:31 +0200 Subject: fix #628: Process.name() on Linux truncates spaces and ')' --- test/test_psutil.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'test/test_psutil.py') diff --git a/test/test_psutil.py b/test/test_psutil.py index fa2b62c2..75bc20de 100644 --- a/test/test_psutil.py +++ b/test/test_psutil.py @@ -1543,6 +1543,26 @@ class TestProcess(unittest.TestCase): pyexe = os.path.basename(os.path.realpath(sys.executable)).lower() assert pyexe.startswith(name), (pyexe, name) + @unittest.skipUnless(POSIX, "posix only") + # TODO: add support for other compilers + @unittest.skipUnless(which("gcc"), "gcc not available") + def test_prog_w_funky_name(self): + # Test that name(), exe() and cmdline() correctly handle programs + # with funky chars such as spaces and ")", see: + # https://github.com/giampaolo/psutil/issues/628 + funky_name = "/tmp/foo bar )" + _, c_file = tempfile.mkstemp(prefix='psutil-', suffix='.c', dir="/tmp") + self.addCleanup(lambda: safe_remove(c_file)) + with open(c_file, "w") as f: + f.write("void main() { pause(); }") + subprocess.check_call(["gcc", c_file, "-o", funky_name]) + self.addCleanup(lambda: safe_remove(funky_name)) + sproc = get_test_subprocess([funky_name, "arg1", "arg2"]) + p = psutil.Process(sproc.pid) + self.assertEqual(p.name(), "foo bar )") + self.assertEqual(p.exe(), "/tmp/foo bar )") + self.assertEqual(p.cmdline(), ["/tmp/foo bar )", "arg1", "arg2"]) + @unittest.skipUnless(POSIX, 'posix only') def test_uids(self): p = psutil.Process() -- cgit v1.2.1 From 636fb7eaf042aaa303aaae60159c103f7bd8e293 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Wed, 3 Jun 2015 11:51:12 +0200 Subject: addCleanup sooner rather than later --- test/test_psutil.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test/test_psutil.py') diff --git a/test/test_psutil.py b/test/test_psutil.py index 75bc20de..470dafc4 100644 --- a/test/test_psutil.py +++ b/test/test_psutil.py @@ -1553,10 +1553,10 @@ class TestProcess(unittest.TestCase): funky_name = "/tmp/foo bar )" _, c_file = tempfile.mkstemp(prefix='psutil-', suffix='.c', dir="/tmp") self.addCleanup(lambda: safe_remove(c_file)) + self.addCleanup(lambda: safe_remove(funky_name)) with open(c_file, "w") as f: f.write("void main() { pause(); }") subprocess.check_call(["gcc", c_file, "-o", funky_name]) - self.addCleanup(lambda: safe_remove(funky_name)) sproc = get_test_subprocess([funky_name, "arg1", "arg2"]) p = psutil.Process(sproc.pid) self.assertEqual(p.name(), "foo bar )") -- cgit v1.2.1 From 870e00f71c15ef39f8259ff16b682e495ea5d6eb Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Thu, 4 Jun 2015 03:42:11 +0200 Subject: #629: rename test/_linux.py (and others) to test/test_pslinux.py in order to ease test discovery for nose and pytest --- test/test_psutil.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'test/test_psutil.py') diff --git a/test/test_psutil.py b/test/test_psutil.py index 470dafc4..ed85708c 100644 --- a/test/test_psutil.py +++ b/test/test_psutil.py @@ -2767,23 +2767,23 @@ def main(): tests.append(LimitedUserTestCase) if POSIX: - from _posix import PosixSpecificTestCase + from test_posix import PosixSpecificTestCase tests.append(PosixSpecificTestCase) # import the specific platform test suite stc = None if LINUX: - from _linux import LinuxSpecificTestCase as stc + from test_linux import LinuxSpecificTestCase as stc elif WINDOWS: - from _windows import WindowsSpecificTestCase as stc - from _windows import TestDualProcessImplementation + from test_windows import WindowsSpecificTestCase as stc + from test_windows import TestDualProcessImplementation tests.append(TestDualProcessImplementation) elif OSX: - from _osx import OSXSpecificTestCase as stc + from test_osx import OSXSpecificTestCase as stc elif BSD: - from _bsd import BSDSpecificTestCase as stc + from test_bsd import BSDSpecificTestCase as stc elif SUNOS: - from _sunos import SunOSSpecificTestCase as stc + from test_sunos import SunOSSpecificTestCase as stc if stc is not None: tests.append(stc) -- cgit v1.2.1 From 931432d83e203e757d0c6668c02386d7f762ea66 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Thu, 4 Jun 2015 03:59:48 +0200 Subject: Revert "#629: rename test/_linux.py (and others) to test/test_pslinux.py in order to ease test discovery for nose and pytest" This reverts commit 870e00f71c15ef39f8259ff16b682e495ea5d6eb. --- test/test_psutil.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'test/test_psutil.py') diff --git a/test/test_psutil.py b/test/test_psutil.py index ed85708c..470dafc4 100644 --- a/test/test_psutil.py +++ b/test/test_psutil.py @@ -2767,23 +2767,23 @@ def main(): tests.append(LimitedUserTestCase) if POSIX: - from test_posix import PosixSpecificTestCase + from _posix import PosixSpecificTestCase tests.append(PosixSpecificTestCase) # import the specific platform test suite stc = None if LINUX: - from test_linux import LinuxSpecificTestCase as stc + from _linux import LinuxSpecificTestCase as stc elif WINDOWS: - from test_windows import WindowsSpecificTestCase as stc - from test_windows import TestDualProcessImplementation + from _windows import WindowsSpecificTestCase as stc + from _windows import TestDualProcessImplementation tests.append(TestDualProcessImplementation) elif OSX: - from test_osx import OSXSpecificTestCase as stc + from _osx import OSXSpecificTestCase as stc elif BSD: - from test_bsd import BSDSpecificTestCase as stc + from _bsd import BSDSpecificTestCase as stc elif SUNOS: - from test_sunos import SunOSSpecificTestCase as stc + from _sunos import SunOSSpecificTestCase as stc if stc is not None: tests.append(stc) -- cgit v1.2.1 From b5d675ab1f2ab3d849564173539891ab56cd1703 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Fri, 12 Jun 2015 01:50:40 -0700 Subject: fix test on Windows --- test/test_psutil.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'test/test_psutil.py') diff --git a/test/test_psutil.py b/test/test_psutil.py index 470dafc4..66209ead 100644 --- a/test/test_psutil.py +++ b/test/test_psutil.py @@ -713,10 +713,11 @@ class TestSystemAPIs(unittest.TestCase): self.assertEqual(logical, len(psutil.cpu_times(percpu=True))) self.assertGreaterEqual(logical, 1) # - with open("/proc/cpuinfo") as fd: - cpuinfo_data = fd.read() - if "physical id" not in cpuinfo_data: - raise unittest.SkipTest("cpuinfo doesn't include physical id") + if LINUX: + with open("/proc/cpuinfo") as fd: + cpuinfo_data = fd.read() + if "physical id" not in cpuinfo_data: + raise unittest.SkipTest("cpuinfo doesn't include physical id") physical = psutil.cpu_count(logical=False) self.assertGreaterEqual(physical, 1) self.assertGreaterEqual(logical, physical) -- cgit v1.2.1 From 661e22dfce9ee66cba41dd46159d5ac8742bf6a5 Mon Sep 17 00:00:00 2001 From: Arni Mar Jonsson Date: Tue, 16 Jun 2015 16:41:15 +0000 Subject: fixing cmdline() for empty-string argumemts on posix systems --- test/test_psutil.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'test/test_psutil.py') diff --git a/test/test_psutil.py b/test/test_psutil.py index 66209ead..050197d5 100644 --- a/test/test_psutil.py +++ b/test/test_psutil.py @@ -1558,11 +1558,11 @@ class TestProcess(unittest.TestCase): with open(c_file, "w") as f: f.write("void main() { pause(); }") subprocess.check_call(["gcc", c_file, "-o", funky_name]) - sproc = get_test_subprocess([funky_name, "arg1", "arg2"]) + sproc = get_test_subprocess([funky_name, "arg1", "arg2", "", "arg3", ""]) p = psutil.Process(sproc.pid) self.assertEqual(p.name(), "foo bar )") self.assertEqual(p.exe(), "/tmp/foo bar )") - self.assertEqual(p.cmdline(), ["/tmp/foo bar )", "arg1", "arg2"]) + self.assertEqual(p.cmdline(), ["/tmp/foo bar )", "arg1", "arg2", "", "arg3", ""]) @unittest.skipUnless(POSIX, 'posix only') def test_uids(self): -- cgit v1.2.1 From 17325efdd1b500870f58e794b7c2e02bf3516c78 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Wed, 17 Jun 2015 11:50:46 +0200 Subject: fix #632: [Linux] better error message if cannot parse process UNIX connections. --- test/test_psutil.py | 2 ++ 1 file changed, 2 insertions(+) (limited to 'test/test_psutil.py') diff --git a/test/test_psutil.py b/test/test_psutil.py index 66209ead..50749c46 100644 --- a/test/test_psutil.py +++ b/test/test_psutil.py @@ -1560,6 +1560,8 @@ class TestProcess(unittest.TestCase): subprocess.check_call(["gcc", c_file, "-o", funky_name]) sproc = get_test_subprocess([funky_name, "arg1", "arg2"]) p = psutil.Process(sproc.pid) + # ...in order to try to prevent occasional failures on travis + wait_for_pid(p.pid) self.assertEqual(p.name(), "foo bar )") self.assertEqual(p.exe(), "/tmp/foo bar )") self.assertEqual(p.cmdline(), ["/tmp/foo bar )", "arg1", "arg2"]) -- cgit v1.2.1 From 6a7d1db1e6dcbef4d172f94d16c6e4903e1fb3d7 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Thu, 18 Jun 2015 03:48:03 +0200 Subject: PEP8 fixes + update HISTORY/CREDITS --- test/test_psutil.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'test/test_psutil.py') diff --git a/test/test_psutil.py b/test/test_psutil.py index 14b5187e..b93cd863 100644 --- a/test/test_psutil.py +++ b/test/test_psutil.py @@ -1558,13 +1558,15 @@ class TestProcess(unittest.TestCase): with open(c_file, "w") as f: f.write("void main() { pause(); }") subprocess.check_call(["gcc", c_file, "-o", funky_name]) - sproc = get_test_subprocess([funky_name, "arg1", "arg2", "", "arg3", ""]) + sproc = get_test_subprocess( + [funky_name, "arg1", "arg2", "", "arg3", ""]) p = psutil.Process(sproc.pid) # ...in order to try to prevent occasional failures on travis wait_for_pid(p.pid) self.assertEqual(p.name(), "foo bar )") self.assertEqual(p.exe(), "/tmp/foo bar )") - self.assertEqual(p.cmdline(), ["/tmp/foo bar )", "arg1", "arg2", "", "arg3", ""]) + self.assertEqual( + p.cmdline(), ["/tmp/foo bar )", "arg1", "arg2", "", "arg3", ""]) @unittest.skipUnless(POSIX, 'posix only') def test_uids(self): -- cgit v1.2.1 From 48f898afa584ac9eed209b63e46a93f1876278f4 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Sat, 20 Jun 2015 02:25:11 +0200 Subject: improve linux test coverage --- test/test_psutil.py | 2 ++ 1 file changed, 2 insertions(+) (limited to 'test/test_psutil.py') diff --git a/test/test_psutil.py b/test/test_psutil.py index b93cd863..842fff78 100644 --- a/test/test_psutil.py +++ b/test/test_psutil.py @@ -675,6 +675,8 @@ class TestSystemAPIs(unittest.TestCase): self.assertFalse(psutil.pid_exists(sproc.pid)) self.assertFalse(psutil.pid_exists(-1)) self.assertEqual(psutil.pid_exists(0), 0 in psutil.pids()) + # pid 0 + psutil.pid_exists(0) == 0 in psutil.pids() def test_pid_exists_2(self): reap_children() -- cgit v1.2.1 From 3163e7ec7df88b6fdfd9b1fcdec29aa0fef695e8 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Sat, 20 Jun 2015 03:03:38 +0200 Subject: (linux) proc io nice: provide better error messages --- test/test_psutil.py | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'test/test_psutil.py') diff --git a/test/test_psutil.py b/test/test_psutil.py index 842fff78..bc750fdc 100644 --- a/test/test_psutil.py +++ b/test/test_psutil.py @@ -1356,7 +1356,17 @@ class TestProcess(unittest.TestCase): ioclass, value = p.ionice() self.assertEqual(ioclass, 2) self.assertEqual(value, 7) + # self.assertRaises(ValueError, p.ionice, 2, 10) + self.assertRaises(ValueError, p.ionice, 2, -1) + self.assertRaises(ValueError, p.ionice, 4) + self.assertRaises(TypeError, p.ionice, 2, "foo") + self.assertRaisesRegexp( + ValueError, "can't specify value with IOPRIO_CLASS_NONE", + p.ionice, psutil.IOPRIO_CLASS_NONE, 1) + self.assertRaisesRegexp( + ValueError, "can't specify value with IOPRIO_CLASS_IDLE", + p.ionice, psutil.IOPRIO_CLASS_IDLE, 1) finally: p.ionice(IOPRIO_CLASS_NONE) else: -- cgit v1.2.1 From 4bd1596035b2d0b1beb795b98dce662054639d48 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Sat, 20 Jun 2015 03:15:10 +0200 Subject: improve test coverage --- test/test_psutil.py | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'test/test_psutil.py') diff --git a/test/test_psutil.py b/test/test_psutil.py index bc750fdc..0db9beb2 100644 --- a/test/test_psutil.py +++ b/test/test_psutil.py @@ -1411,6 +1411,12 @@ class TestProcess(unittest.TestCase): p = psutil.Process(sproc.pid) p.rlimit(psutil.RLIMIT_NOFILE, (5, 5)) self.assertEqual(p.rlimit(psutil.RLIMIT_NOFILE), (5, 5)) + # If pid is 0 prlimit() applies to the calling process and + # we don't want that. + with self.assertRaises(ValueError): + psutil._psplatform.Process(0).rlimit(0) + with self.assertRaises(ValueError): + p.rlimit(psutil.RLIMIT_NOFILE, (5, 5, 5)) def test_num_threads(self): # on certain platforms such as Linux we might test for exact -- cgit v1.2.1 From 2214f431217ffbd6af1d76eadd100df64ccdaffb Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Sat, 20 Jun 2015 03:29:58 +0200 Subject: test we are able to instantiate Process() in case of AD and zombie process --- test/test_psutil.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'test/test_psutil.py') diff --git a/test/test_psutil.py b/test/test_psutil.py index 0db9beb2..8cda3691 100644 --- a/test/test_psutil.py +++ b/test/test_psutil.py @@ -46,6 +46,10 @@ try: import ipaddress # python >= 3.3 except ImportError: ipaddress = None +try: + from unittest import mock # py3 +except ImportError: + import mock # requires "pip install mock" import psutil from psutil._compat import PY3, callable, long, unicode @@ -2685,6 +2689,22 @@ class TestMisc(unittest.TestCase): module = imp.load_source('setup', setup_py) self.assertRaises(SystemExit, module.setup) + def test_ad_on_process_creation(self): + # We are supposed to be able to instantiate Process also in case + # of zombie processes or access denied. + with mock.patch.object(psutil.Process, 'create_time', + side_effect=psutil.AccessDenied) as meth: + psutil.Process() + assert meth.called + with mock.patch.object(psutil.Process, 'create_time', + side_effect=psutil.ZombieProcess(1)) as meth: + psutil.Process() + assert meth.called + with mock.patch.object(psutil.Process, 'create_time', + side_effect=ValueError) as meth: + with self.assertRaises(ValueError): + psutil.Process() + # =================================================================== # --- Example script tests -- cgit v1.2.1 From 84b2e3b4fc762ab77eebb381ed311b885373d912 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Sat, 20 Jun 2015 03:34:35 +0200 Subject: improve tests for Process.__str__ --- test/test_psutil.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) (limited to 'test/test_psutil.py') diff --git a/test/test_psutil.py b/test/test_psutil.py index 8cda3691..a7e1f958 100644 --- a/test/test_psutil.py +++ b/test/test_psutil.py @@ -2593,6 +2593,16 @@ class TestMisc(unittest.TestCase): p.wait() self.assertIn(str(sproc.pid), str(p)) self.assertIn("terminated", str(p)) + # test error conditions + with mock.patch.object(psutil.Process, 'name', + side_effect=psutil.ZombieProcess(1)) as meth: + self.assertIn("zombie", str(p)) + self.assertIn("pid", str(p)) + assert meth.called + with mock.patch.object(psutil.Process, 'name', + side_effect=psutil.AccessDenied) as meth: + self.assertIn("pid", str(p)) + assert meth.called def test__eq__(self): p1 = psutil.Process() @@ -2695,15 +2705,16 @@ class TestMisc(unittest.TestCase): with mock.patch.object(psutil.Process, 'create_time', side_effect=psutil.AccessDenied) as meth: psutil.Process() - assert meth.called + assert meth.called with mock.patch.object(psutil.Process, 'create_time', side_effect=psutil.ZombieProcess(1)) as meth: psutil.Process() - assert meth.called + assert meth.called with mock.patch.object(psutil.Process, 'create_time', side_effect=ValueError) as meth: with self.assertRaises(ValueError): psutil.Process() + assert meth.called # =================================================================== -- cgit v1.2.1 From b6e452b51f661babf4889b9d4b9e3537d273799f Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Sat, 20 Jun 2015 14:06:32 +0200 Subject: improve test coverage for open_files on linux --- test/test_psutil.py | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'test/test_psutil.py') diff --git a/test/test_psutil.py b/test/test_psutil.py index a7e1f958..7609d35d 100644 --- a/test/test_psutil.py +++ b/test/test_psutil.py @@ -1725,6 +1725,7 @@ class TestProcess(unittest.TestCase): files = p.open_files() self.assertFalse(TESTFN in files) with open(TESTFN, 'w'): + # give the kernel some time to see the new file call_until(p.open_files, "len(ret) != %i" % len(files)) filenames = [x.path for x in p.open_files()] self.assertIn(TESTFN, filenames) @@ -2604,6 +2605,9 @@ class TestMisc(unittest.TestCase): self.assertIn("pid", str(p)) assert meth.called + def test__repr__(self): + repr(psutil.Process()) + def test__eq__(self): p1 = psutil.Process() p2 = psutil.Process() -- cgit v1.2.1 From fcf5ca3e938045c72d26495353446eabea70d4e3 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Sat, 20 Jun 2015 15:07:46 +0200 Subject: enhance test coverage --- test/test_psutil.py | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'test/test_psutil.py') diff --git a/test/test_psutil.py b/test/test_psutil.py index 7609d35d..b6dc1348 100644 --- a/test/test_psutil.py +++ b/test/test_psutil.py @@ -584,6 +584,7 @@ class TestSystemAPIs(unittest.TestCase): sproc3 = get_test_subprocess() procs = [psutil.Process(x.pid) for x in (sproc1, sproc2, sproc3)] self.assertRaises(ValueError, psutil.wait_procs, procs, timeout=-1) + self.assertRaises(TypeError, psutil.wait_procs, procs, callback=1) t = time.time() gone, alive = psutil.wait_procs(procs, timeout=0.01, callback=callback) @@ -1371,6 +1372,9 @@ class TestProcess(unittest.TestCase): self.assertRaisesRegexp( ValueError, "can't specify value with IOPRIO_CLASS_IDLE", p.ionice, psutil.IOPRIO_CLASS_IDLE, 1) + self.assertRaisesRegexp( + ValueError, "'ioclass' argument must be specified", + p.ionice, value=1) finally: p.ionice(IOPRIO_CLASS_NONE) else: @@ -1661,6 +1665,11 @@ class TestProcess(unittest.TestCase): if POSIX: import pwd self.assertEqual(p.username(), pwd.getpwuid(os.getuid()).pw_name) + with mock.patch("psutil.pwd.getpwuid", + side_effect=KeyError) as fun: + p.username() == str(p.uids().real) + assert fun.called + elif WINDOWS and 'USERNAME' in os.environ: expected_username = os.environ['USERNAME'] expected_domain = os.environ['USERDOMAIN'] @@ -2247,6 +2256,7 @@ class TestProcess(unittest.TestCase): proc.stdin self.assertTrue(hasattr(proc, 'name')) self.assertTrue(hasattr(proc, 'stdin')) + self.assertTrue(dir(proc)) self.assertRaises(AttributeError, getattr, proc, 'foo') finally: proc.kill() -- cgit v1.2.1 From 0d6368d61c24402b567134aff1e9b067aed8bb07 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Sat, 20 Jun 2015 15:16:49 +0200 Subject: enhance test coverage --- test/test_psutil.py | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) (limited to 'test/test_psutil.py') diff --git a/test/test_psutil.py b/test/test_psutil.py index b6dc1348..a7213f4f 100644 --- a/test/test_psutil.py +++ b/test/test_psutil.py @@ -1136,13 +1136,30 @@ class TestProcess(unittest.TestCase): def test_send_signal(self): sig = signal.SIGKILL if POSIX else signal.SIGTERM sproc = get_test_subprocess() - test_pid = sproc.pid - p = psutil.Process(test_pid) + p = psutil.Process(sproc.pid) p.send_signal(sig) exit_sig = p.wait() - self.assertFalse(psutil.pid_exists(test_pid)) + self.assertFalse(psutil.pid_exists(p.pid)) if POSIX: self.assertEqual(exit_sig, sig) + # + sproc = get_test_subprocess() + p = psutil.Process(sproc.pid) + p.send_signal(sig) + with mock.patch('psutil.os.kill', + side_effect=OSError(errno.ESRCH, "")) as fun: + with self.assertRaises(psutil.NoSuchProcess): + p.send_signal(sig) + assert fun.called + # + sproc = get_test_subprocess() + p = psutil.Process(sproc.pid) + p.send_signal(sig) + with mock.patch('psutil.os.kill', + side_effect=OSError(errno.EPERM, "")) as fun: + with self.assertRaises(psutil.AccessDenied): + p.send_signal(sig) + assert fun.called def test_wait(self): # check exit code signal -- cgit v1.2.1 From 5ae30c7989e30702b457c2cae69e6b7e4a8b96ed Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Sat, 20 Jun 2015 15:26:09 +0200 Subject: UNIX: prevent sending signals to PID 0 --- test/test_psutil.py | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'test/test_psutil.py') diff --git a/test/test_psutil.py b/test/test_psutil.py index a7213f4f..31655b72 100644 --- a/test/test_psutil.py +++ b/test/test_psutil.py @@ -2233,6 +2233,10 @@ class TestProcess(unittest.TestCase): except psutil.AccessDenied: pass + self.assertRaisesRegexp( + ValueError, "preventing sending signal to process with PID 0", + p.send_signal(signal.SIGTERM)) + self.assertIn(p.ppid(), (0, 1)) # self.assertEqual(p.exe(), "") p.cmdline() -- cgit v1.2.1 From d2a37e772906194b89c639e82ef9372fa3e222ce Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Thu, 9 Jul 2015 10:18:46 +0200 Subject: update HISTORY --- test/test_psutil.py | 1 - 1 file changed, 1 deletion(-) (limited to 'test/test_psutil.py') diff --git a/test/test_psutil.py b/test/test_psutil.py index 31655b72..91423841 100644 --- a/test/test_psutil.py +++ b/test/test_psutil.py @@ -2250,7 +2250,6 @@ class TestProcess(unittest.TestCase): except psutil.AccessDenied: pass - # username property try: if POSIX: self.assertEqual(p.username(), 'root') -- cgit v1.2.1