Ejemplo n.º 1
0
 def test_wait_procs_no_timeout(self):
     sproc1 = get_test_subprocess()
     sproc2 = get_test_subprocess()
     sproc3 = get_test_subprocess()
     procs = [psutil.Process(x.pid) for x in (sproc1, sproc2, sproc3)]
     for p in procs:
         p.terminate()
     gone, alive = psutil.wait_procs(procs)
Ejemplo n.º 2
0
 def setUp(self):
     env = os.environ.copy()
     env["THINK_OF_A_NUMBER"] = str(os.getpid())
     self.proc32 = get_test_subprocess([self.python32] + self.test_args,
                                       env=env,
                                       stdin=subprocess.PIPE)
     self.proc64 = get_test_subprocess([self.python64] + self.test_args,
                                       env=env,
                                       stdin=subprocess.PIPE)
Ejemplo n.º 3
0
    def test_wait_procs(self):
        def callback(p):
            pids.append(p.pid)

        pids = []
        sproc1 = get_test_subprocess()
        sproc2 = get_test_subprocess()
        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)

        self.assertLess(time.time() - t, 0.5)
        self.assertEqual(gone, [])
        self.assertEqual(len(alive), 3)
        self.assertEqual(pids, [])
        for p in alive:
            self.assertFalse(hasattr(p, 'returncode'))

        @retry_before_failing(30)
        def test(procs, callback):
            gone, alive = psutil.wait_procs(procs, timeout=0.03,
                                            callback=callback)
            self.assertEqual(len(gone), 1)
            self.assertEqual(len(alive), 2)
            return gone, alive

        sproc3.terminate()
        gone, alive = test(procs, callback)
        self.assertIn(sproc3.pid, [x.pid for x in gone])
        if POSIX:
            self.assertEqual(gone.pop().returncode, -signal.SIGTERM)
        else:
            self.assertEqual(gone.pop().returncode, 1)
        self.assertEqual(pids, [sproc3.pid])
        for p in alive:
            self.assertFalse(hasattr(p, 'returncode'))

        @retry_before_failing(30)
        def test(procs, callback):
            gone, alive = psutil.wait_procs(procs, timeout=0.03,
                                            callback=callback)
            self.assertEqual(len(gone), 3)
            self.assertEqual(len(alive), 0)
            return gone, alive

        sproc1.terminate()
        sproc2.terminate()
        gone, alive = test(procs, callback)
        self.assertEqual(set(pids), set([sproc1.pid, sproc2.pid, sproc3.pid]))
        for p in gone:
            self.assertTrue(hasattr(p, 'returncode'))
Ejemplo n.º 4
0
def subprocess_supports_unicode(name):
    """Return True if both the fs and the subprocess module can
    deal with a unicode file name.
    """
    if PY3:
        return True
    try:
        safe_rmpath(name)
        create_exe(name)
        get_test_subprocess(cmd=[name])
    except UnicodeEncodeError:
        return False
    else:
        reap_children()
        return True
Ejemplo n.º 5
0
    def test_pids(self):
        # Note: this test might fail if the OS is starting/killing
        # other processes in the meantime
        if SUNOS or AIX:
            cmd = ["ps", "-A", "-o", "pid"]
        else:
            cmd = ["ps", "ax", "-o", "pid"]
        p = get_test_subprocess(cmd, stdout=subprocess.PIPE)
        output = p.communicate()[0].strip()
        assert p.poll() == 0
        if PY3:
            output = str(output, sys.stdout.encoding)
        pids_ps = []
        for line in output.split('\n')[1:]:
            if line:
                pid = int(line.split()[0].strip())
                pids_ps.append(pid)
        # remove ps subprocess pid which is supposed to be dead in meantime
        pids_ps.remove(p.pid)
        pids_psutil = psutil.pids()
        pids_ps.sort()
        pids_psutil.sort()

        # on OSX and OPENBSD ps doesn't show pid 0
        if OSX or OPENBSD and 0 not in pids_ps:
            pids_ps.insert(0, 0)
        self.assertEqual(pids_ps, pids_psutil)
Ejemplo n.º 6
0
 def test_proc_exe(self):
     subp = get_test_subprocess(cmd=[self.funky_name])
     p = psutil.Process(subp.pid)
     exe = p.exe()
     self.assertIsInstance(exe, str)
     if self.expect_exact_path_match():
         self.assertEqual(exe, self.funky_name)
Ejemplo n.º 7
0
 def test_proc_cmdline(self):
     subp = get_test_subprocess(cmd=[self.funky_name])
     p = psutil.Process(subp.pid)
     cmdline = p.cmdline()
     for part in cmdline:
         self.assertIsInstance(part, str)
     if self.expect_exact_path_match():
         self.assertEqual(cmdline, [self.funky_name])
Ejemplo n.º 8
0
 def test_reap_children(self):
     subp = get_test_subprocess()
     p = psutil.Process(subp.pid)
     assert p.is_running()
     reap_children()
     assert not p.is_running()
     assert not psutil.tests._pids_started
     assert not psutil.tests._subprocesses_started
Ejemplo n.º 9
0
 def test_pid_exists(self):
     sproc = get_test_subprocess()
     self.assertTrue(psutil.pid_exists(sproc.pid))
     p = psutil.Process(sproc.pid)
     p.kill()
     p.wait()
     self.assertFalse(psutil.pid_exists(sproc.pid))
     self.assertFalse(psutil.pid_exists(-1))
     self.assertEqual(psutil.pid_exists(0), 0 in psutil.pids())
Ejemplo n.º 10
0
 def test_ctrl_signals(self):
     p = psutil.Process(get_test_subprocess().pid)
     p.send_signal(signal.CTRL_C_EVENT)
     p.send_signal(signal.CTRL_BREAK_EVENT)
     p.kill()
     p.wait()
     self.assertRaises(psutil.NoSuchProcess,
                       p.send_signal, signal.CTRL_C_EVENT)
     self.assertRaises(psutil.NoSuchProcess,
                       p.send_signal, signal.CTRL_BREAK_EVENT)
Ejemplo n.º 11
0
    def test_process_iter(self):
        self.assertIn(os.getpid(), [x.pid for x in psutil.process_iter()])
        sproc = get_test_subprocess()
        self.assertIn(sproc.pid, [x.pid for x in psutil.process_iter()])
        p = psutil.Process(sproc.pid)
        p.kill()
        p.wait()
        self.assertNotIn(sproc.pid, [x.pid for x in psutil.process_iter()])

        with mock.patch("psutil.Process", side_effect=psutil.NoSuchProcess(os.getpid())):
            self.assertEqual(list(psutil.process_iter()), [])
        with mock.patch("psutil.Process", side_effect=psutil.AccessDenied(os.getpid())):
            with self.assertRaises(psutil.AccessDenied):
                list(psutil.process_iter())
Ejemplo n.º 12
0
 def test_proc_name(self):
     subp = get_test_subprocess(cmd=[self.funky_name])
     if WINDOWS:
         # On Windows name() is determined from exe() first, because
         # it's faster; we want to overcome the internal optimization
         # and test name() instead of exe().
         with mock.patch("psutil._psplatform.cext.proc_exe",
                         side_effect=psutil.AccessDenied(os.getpid())) as m:
             name = psutil.Process(subp.pid).name()
             assert m.called
     else:
         name = psutil.Process(subp.pid).name()
     self.assertIsInstance(name, str)
     if self.expect_exact_path_match():
         self.assertEqual(name, os.path.basename(self.funky_name))
Ejemplo n.º 13
0
 def test_proc_environ(self):
     # Note: differently from others, this test does not deal
     # with fs paths. On Python 2 subprocess module is broken as
     # it's not able to handle with non-ASCII env vars, so
     # we use "è", which is part of the extended ASCII table
     # (unicode point <= 255).
     env = os.environ.copy()
     funky_str = TESTFN_UNICODE if PY3 else 'è'
     env['FUNNY_ARG'] = funky_str
     sproc = get_test_subprocess(env=env)
     p = psutil.Process(sproc.pid)
     env = p.environ()
     for k, v in env.items():
         self.assertIsInstance(k, str)
         self.assertIsInstance(v, str)
     self.assertEqual(env['FUNNY_ARG'], funky_str)
Ejemplo n.º 14
0
 def setUpClass(cls):
     cls.pid = get_test_subprocess([PYTHON_EXE, "-E", "-O"],
                                   stdin=subprocess.PIPE).pid
     wait_for_pid(cls.pid)
Ejemplo n.º 15
0
        # be executed.
        if SUNOS:
            kind = 'inet'
        try:
            self.execute('connections', kind=kind)
        finally:
            for s in socks:
                s.close()

    @unittest.skipUnless(hasattr(psutil.Process, 'environ'),
                         "Linux, OSX and Windows")
    def test_environ(self):
        self.execute("environ")


p = get_test_subprocess()
DEAD_PROC = psutil.Process(p.pid)
DEAD_PROC.kill()
DEAD_PROC.wait()
del p


class TestProcessObjectLeaksZombie(TestProcessObjectLeaks):
    """Same as above but looks for leaks occurring when dealing with
    zombie processes raising NoSuchProcess exception.
    """
    proc = DEAD_PROC

    def call(self, *args, **kwargs):
        try:
            TestProcessObjectLeaks.call(self, *args, **kwargs)
Ejemplo n.º 16
0
        # be executed.
        if SUNOS:
            kind = 'inet'
        try:
            self.execute('connections', kind=kind)
        finally:
            for s in socks:
                s.close()

    @unittest.skipUnless(hasattr(psutil.Process, 'environ'),
                         "Linux, OSX and Windows")
    def test_environ(self):
        self.execute("environ")


p = get_test_subprocess()
DEAD_PROC = psutil.Process(p.pid)
DEAD_PROC.kill()
DEAD_PROC.wait()
del p


class TestProcessObjectLeaksZombie(TestProcessObjectLeaks):
    """Same as above but looks for leaks occurring when dealing with
    zombie processes raising NoSuchProcess exception.
    """
    proc = DEAD_PROC

    def call(self, *args, **kwargs):
        try:
            TestProcessObjectLeaks.call(self, *args, **kwargs)
Ejemplo n.º 17
0
 def setUpClass(cls):
     cls.pid = get_test_subprocess([PYTHON, "-E", "-O"],
                                   stdin=subprocess.PIPE).pid
     wait_for_pid(cls.pid)
Ejemplo n.º 18
0
 def setUpClass(cls):
     cls.pid = get_test_subprocess().pid
Ejemplo n.º 19
0
 def test_reap_children(self):
     subp = get_test_subprocess()
     p = psutil.Process(subp.pid)
     assert p.is_running()
     reap_children()
     assert not p.is_running()
Ejemplo n.º 20
0
 def setUpClass(cls):
     super(TestTerminatedProcessLeaks, cls).setUpClass()
     p = get_test_subprocess()
     cls.proc = psutil.Process(p.pid)
     cls.proc.kill()
     cls.proc.wait()
 def test_reap_children(self):
     subp = get_test_subprocess()
     assert psutil.pid_exists(subp.pid)
     reap_children()
     assert not psutil.pid_exists(subp.pid)
Ejemplo n.º 22
0
 def setUpClass(cls):
     super(TestTerminatedProcessLeaks, cls).setUpClass()
     p = get_test_subprocess()
     cls.proc = psutil.Process(p.pid)
     cls.proc.kill()
     cls.proc.wait()
Ejemplo n.º 23
0
 def test_proc_name(self):
     subp = get_test_subprocess(cmd=[self.funky_name])
     name = psutil.Process(subp.pid).name()
     self.assertIsInstance(name, str)
     if self.expect_exact_path_match():
         self.assertEqual(name, os.path.basename(self.funky_name))
Ejemplo n.º 24
0
 def setUpClass(cls):
     cls.pid = get_test_subprocess().pid
Ejemplo n.º 25
0
 def test_reap_children(self):
     subp = get_test_subprocess()
     assert psutil.pid_exists(subp.pid)
     reap_children()
     assert not psutil.pid_exists(subp.pid)