def kill_pid(pid): if pid == os.getpid(): raise RuntimeError('I cannot kill myself') if pid is None: raise TypeError('PID is None') if not psutil.pid_exists(pid): raise psutil.NoSuchProcess(pid=pid) proc = psutil.Process(pid=pid) proc.kill()
def test_signal_stop(self, mock_send_signal): proc = PostmasterProcess(-123) self.assertEqual(proc.signal_stop('immediate'), False) mock_send_signal.side_effect = [None, psutil.NoSuchProcess(123), psutil.AccessDenied()] proc = PostmasterProcess(123) self.assertEqual(proc.signal_stop('immediate'), None) self.assertEqual(proc.signal_stop('immediate'), True) self.assertEqual(proc.signal_stop('immediate'), False)
def test_kill_game_invalid_PID(self): # test when pids do exist pid_list = [1, 2, 3, 10, 4] game1 = GameObject.min_init('game1', 100) with patch( "flask_app.TrackingThread.psutil.Process.kill") as mocked_kill: mocked_kill.side_effect = psutil.NoSuchProcess(None) game1.kill()
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)
def test_start(self, mock_frompidfile, mock_frompid, mock_popen): mock_frompidfile.return_value._is_postmaster_process.return_value = False mock_frompid.return_value = "proc 123" mock_popen.return_value.stdout.readline.return_value = '123' self.assertEqual(PostmasterProcess.start('true', '/tmp', '/tmp/test.conf', []), "proc 123") mock_frompid.assert_called_with(123) mock_frompidfile.side_effect = psutil.NoSuchProcess(123) self.assertEqual(PostmasterProcess.start('true', '/tmp', '/tmp/test.conf', []), "proc 123")
def wrapper(self, *args, **kwargs): try: return fun(self, *args, **kwargs) except OSError as err: if err.errno in ACCESS_DENIED_SET: raise psutil.AccessDenied(None, None) if err.errno == errno.ESRCH: raise psutil.NoSuchProcess(None, None) raise
def __init__(self, pid): self.pid = pid if pid == 10: self._name = 'myprocess10' elif pid == 222: self._name = 'myprocess222' else: raise psutil.NoSuchProcess(pid)
def _MockedProcess(pid): if pid not in mock_pid_map: raise psutil.NoSuchProcess(pid=pid) username, cmdline, cpuutil, memutil = mock_pid_map[pid] p = MagicMock() # mocked process p.username.return_value = username p.cmdline.return_value = [cmdline] p.cpu_percent.return_value = cpuutil p.memory_percent.return_value = memutil return p
def conn_iter(self, kind='inet'): try: for conn in _pslinux._connections.retrieve_iter(kind, self.pid): yield conn except EnvironmentError as err: if err.errno in (errno.ENOENT, errno.ESRCH): raise psutil.NoSuchProcess(self.pid, self._name) if err.errno in (errno.EPERM, errno.EACCES): raise psutil.AccessDenied(self.pid, self._name) raise
def wrapper(self, *args, **kwargs): try: return callable(self, *args, **kwargs) except OSError: err = sys.exc_info()[1] if err.errno in ACCESS_DENIED_SET: raise psutil.AccessDenied(None, None) if err.errno == errno.ESRCH: raise psutil.NoSuchProcess(None, None) raise
def wrapper(self, *args, **kwargs): try: return fun(self, *args, **kwargs) except OSError as err: from psutil._pswindows import ACCESS_DENIED_SET if err.errno in ACCESS_DENIED_SET: raise psutil.AccessDenied(None, None) if err.errno == errno.EparserH: raise psutil.NoSuchProcess(None, None) raise
def _MockedProcess(pid): if pid not in mock_pid_map: raise psutil.NoSuchProcess(pid=pid) username, cmdline, cpuutil, memutil = mock_pid_map[pid] p = mock(strict=True) p.username = lambda: username p.cmdline = lambda: [cmdline] p.cpu_percent = lambda: cpuutil p.memory_percent = lambda: memutil return p
def test_is_alive(self, mock_process): tunnel = Tunnel('server', 123, 456, pid=12345) proc = MockProcess(tunnel.pid, "ssh") mock_process.return_value = proc assert tunnel.is_alive() proc.running = False assert not tunnel.is_alive() proc.running = True mock_process.side_effect = psutil.NoSuchProcess(tunnel.pid) assert not tunnel.is_alive()
def _get_psutil_process(pid, ctime): if pid is None: pid = os.getpid() if ctime is None: return psutil.Process(pid) try: psps = (psp for psp in psutil.process_iter() if psp.pid() == pid and psp.create_time() == ctime) return psps[0] except IndexError: raise psutil.NoSuchProcess(pid)
def __start_callback(self, program): from pony.orm import select for p in select(p for p in Project if p.program == program)[:]: try: pid = p.pid if pid and psutil.Process(p.pid).is_running(): continue else: raise psutil.NoSuchProcess(pid) except psutil.NoSuchProcess: self.____start_callback(p.command, p.id)
def _get_pid(proc: Union[int, psutil.Process], *, check_running: bool = False) -> int: if isinstance(proc, int): return proc else: if check_running: if not proc.is_running(): raise psutil.NoSuchProcess(proc.pid) return cast(int, proc.pid)
def make_mock_process( pid, open_files, name='', as_dict_throw=False, signal_throw=False, wait_throw=False, timeout=False, ): # type: (int, List[str], str, bool, bool, bool) -> psutil.Process """ Creates a mock psutil.Process object suitable for these unit tests. If `throw` is True, it sets the side_effect of `as_dict` to throw a NoSuchProcess exception. """ mock_process = create_autospec(psutil.Process) mock_process.pid = pid mock_process.name.return_value = name mock_process.as_dict.__name__ = 'as_dict' if as_dict_throw: mock_process.as_dict.side_effect = psutil.NoSuchProcess(pid) # This is hacky, but it works to simulate a TimeoutExpired actually being # raised by as_dict. elif timeout: mock_process.as_dict.side_effect = TimeoutExpired() else: mock_process.as_dict.return_value = { 'pid': pid, 'open_files': [MockPopenFile(f) for f in open_files], } if signal_throw: mock_process.send_signal.side_effect = psutil.NoSuchProcess(pid) else: mock_process.send_signal.return_value = None if wait_throw: mock_process.wait.side_effect = psutil.TimeoutExpired(5) else: mock_process.wait.return_value = None return mock_process
def process(self, processes, pid=None): for stack_frame in inspect.stack(): # grr_response_client/unprivileged/communication.py needs a real process. if ("unprivileged" in stack_frame.filename and "communication.py" in stack_frame.filename): return psutil.Process.old_target(pid=pid) if not pid: return psutil.Process.old_target() for p in processes: if p.pid == pid: return p raise psutil.NoSuchProcess("No process with pid %d." % pid)
def test__signal_processes(self): mock_procs = [mock.MagicMock() for _ in range(4)] mock_procs[0].is_running.return_value = False mock_procs[1].is_running.return_value = True mock_procs[1].signal_func.side_effect = psutil.NoSuchProcess("") mock_procs[2].is_running.return_value = True mock_procs[2].signal_func.side_effect = Exception("") mock_procs[3].is_running.return_value = True proc._signal_processes(mock_procs, "signal_func") mock_procs[1].signal_func.assert_called_once() mock_procs[2].signal_func.assert_called_once() mock_procs[3].signal_func.assert_called_once()
def __call__(klass, *args, **kwargs): instance = ParameterizedSingleton.__call__(klass, *args, **kwargs) #we know this is new if the cache attribute is missing if not hasattr(instance, '_cache'): instance._cache = {'original': psutil.Process(*args, **kwargs)} initialScan(instance) return instance if klass.isValid(instance): if psutil.Process( instance.pid).create_time == instance.create_time: return instance klass.delete(instance) raise psutil.NoSuchProcess( instance.pid, None, "no process found with pid %s" % instance.pid)
def test_iter_processes_process_iter_raises_psutil_exception(self): """If psutil.process_iter raises the exception, silently stop iteration.""" def id_or_raise(o): if isinstance(o, Exception): raise o else: return o with mock.patch('psutil.process_iter', **PATCH_OPTS) as mock_process_iter: mock_process_iter.return_value= (id_or_raise(i) for i in ['first', psutil.NoSuchProcess('The Exception'), 'never seen']) items = [item for item in self.pg.iter_processes()] self.assertEqual(['first'], items)
def test_stop(self, mock_get_pid, mock_is_running): mock_is_running.return_value = True mock_get_pid.return_value = 0 self.assertTrue(self.p.stop()) mock_get_pid.return_value = -1 self.assertFalse(self.p.stop()) mock_get_pid.return_value = 123 with patch('os.kill', Mock(side_effect=[OSError(errno.ESRCH, ''), OSError, None])),\ patch('psutil.Process', Mock(side_effect=psutil.NoSuchProcess(123))): self.assertTrue(self.p.stop()) self.assertFalse(self.p.stop()) self.p.stop_safepoint_reached.clear() self.assertTrue(self.p.stop()) with patch.object(Postgresql, '_signal_postmaster_stop', Mock(return_value=(123, None))): with patch.object(Postgresql, 'is_pid_running', Mock(side_effect=[True, False, False])): self.assertTrue(self.p.stop())
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())
def _MockedProcess(pid): if pid not in mock_pid_map: if pid == 99995: # simulate a bug reported in #95 raise FileNotFoundError("/proc/99995/stat") else: # for a process that does not exist, NoSuchProcess is the # type of exceptions supposed to be raised by psutil raise psutil.NoSuchProcess(pid=pid) username, cmdline, cpuutil, memutil = mock_pid_map[pid] p = mock(strict=True) p.username = lambda: username p.cmdline = lambda: [cmdline] p.cpu_percent = lambda: cpuutil p.memory_percent = lambda: memutil return p
def test_wait_for_user_backends_to_close(self, mock_wait): c1 = Mock() c1.cmdline = Mock(return_value=["postgres: startup process"]) c2 = Mock() c2.cmdline = Mock(return_value=["postgres: postgres postgres [local] idle"]) c3 = Mock() c3.cmdline = Mock(side_effect=psutil.NoSuchProcess(123)) with patch('psutil.Process.children', Mock(return_value=[c1, c2, c3])): proc = PostmasterProcess(123) self.assertIsNone(proc.wait_for_user_backends_to_close()) mock_wait.assert_called_with([c2]) c3.cmdline = Mock(side_effect=psutil.AccessDenied(123)) with patch('psutil.Process.children', Mock(return_value=[c3])): proc = PostmasterProcess(123) self.assertIsNone(proc.wait_for_user_backends_to_close())
def test_agents(self): expected_output = {'running': ['puppet-agent']} temp_pids = [1, 230, 9876] mock_response = mock.Mock() mock_response.side_effect = [ command_returns.get_pid(1, 'systemd'), command_returns.get_pid(230, 'puppet-agent'), psutil.NoSuchProcess(9876) ] with mock.patch('ae_preflight.profile.psutil.pids') as pids: pids.return_value = temp_pids with mock.patch('ae_preflight.profile.psutil.Process', side_effect=mock_response): returns = profile.check_for_agents(True) self.assertEquals(expected_output, returns, 'Returned values did not match expected output')
def proc_rlimit( proc: Union[int, psutil.Process], res: int, new_limits: Optional[Tuple[int, int]] = None) -> Tuple[int, int]: """Identical to ``Process.rlimit()``, but is implemented for some platforms other than Linux. WARNING: This function may not be able to set the soft and hard resource limits in one operation. If it returns with an error, one or both of the limits may have been changed. Args: proc: Either an integer PID or a ``psutil.Process`` specifying the process to operate on. If a ``psutil.Process`` is given and ``new_limits`` is passed, this function preemptively checks ``Process.is_running()``. res: The resource (one of the ``resource.RLIMIT_*`` constants) to get/set. new_limits: If given and not ``None``, the new ``(soft, hard)`` resource limits to set. Returns: A tuple of the given process's ``(soft, hard)`` limits for the given resource (prior to setting the new limits). """ pid = _get_pid(proc, check_running=(new_limits is not None)) if new_limits is not None: soft, hard = new_limits if soft > hard: raise ValueError("current limit exceeds maximum limit") if soft < 0: soft = resource.RLIM_INFINITY if hard < 0: hard = resource.RLIM_INFINITY new_limits = (soft, hard) try: return _psimpl.proc_rlimit(pid, res, new_limits) except ProcessLookupError as ex: raise psutil.NoSuchProcess(pid) from ex except PermissionError as ex: raise psutil.AccessDenied(pid) from ex
def test_iter_processes_process_iter_raises_psutil_exception(self): """If psutil.process_iter raises the exception, silently stop iteration.""" def id_or_raise(o): if isinstance(o, Exception): raise o else: return o with unittest.mock.patch("psutil.process_iter", **PATCH_OPTS) as mock_process_iter: mock_process_iter.return_value = ( id_or_raise(i) for i in ["first", psutil.NoSuchProcess("The Exception"), "never seen"]) items = [item for item in self.pg.iter_processes()] self.assertEqual(["first"], items)
def testTimeoutWithLongRunning(self): """ Test if process timeout watcher kills the process that runs too long, and properly reports that it was killed. """ # Create a process that runs infinitely. proc = subprocess.Popen(['sh', '-c', 'yes'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, preexec_fn=os.setpgrp, encoding="utf-8", errors="ignore") print("Started `yes` with PID {0}".format(proc.pid)) future = setup_process_timeout(proc, 5) # Simulate waiting for the process. proc.wait() # Execution reaches this spot, which means the process was killed. # (Or it ran way too long and the OS killed it. Usually tests run # quick enough this isn't the case...) killed = future() self.assertTrue( killed, "Process timeout watcher said it did not kill the " "process, but it should have.") with self.assertRaises(psutil.NoSuchProcess): # Try to fetch the process from the system. It shouldn't exist. osproc = psutil.Process(proc.pid) # There can be rare cases that the OS so quickly recycles the PID. if osproc.exe() != 'yes': # (Let's just say it's very, very, VERY rare that it recycles # the PID to another execution of 'yes'...) # If the process exists but it isn't the process we started, # it's the same as if it doesn't existed. raise psutil.NoSuchProcess(proc.pid) # NOTE: This assertion is only viable on Unix systems! self.assertEqual( proc.returncode, -signal.SIGTERM, "`yes` died in a way that it wasn't the process " "timeout watcher killing it.")
def test_race_condition(self, mock_get_utility, mock_process, mock_net): # This tests a race condition, or permission problem, or OS # incompatibility in which, for some reason, no process name can be # found to match the identified listening PID. import psutil from psutil._common import sconn conns = [ sconn(fd=-1, family=2, type=1, laddr=("0.0.0.0", 30), raddr=(), status="LISTEN", pid=None), sconn(fd=3, family=2, type=1, laddr=("192.168.5.10", 32783), raddr=("20.40.60.80", 22), status="ESTABLISHED", pid=1234), sconn(fd=-1, family=10, type=1, laddr=("::1", 54321), raddr=("::1", 111), status="CLOSE_WAIT", pid=None), sconn(fd=3, family=2, type=1, laddr=("0.0.0.0", 17), raddr=(), status="LISTEN", pid=4416) ] mock_net.return_value = conns mock_process.side_effect = psutil.NoSuchProcess("No such PID") # We simulate being unable to find the process name of PID 4416, # which results in returning False. self.assertFalse(self._call(17)) self.assertEqual(mock_get_utility.generic_notification.call_count, 0) mock_process.assert_called_once_with(4416)