优化UI
增加JAVA环境配置 增加VC运行库安装 增加检测升级功能 更新PY模块
This commit is contained in:
@@ -9,6 +9,7 @@ Test utilities.
|
||||
"""
|
||||
|
||||
from __future__ import print_function
|
||||
|
||||
import atexit
|
||||
import contextlib
|
||||
import ctypes
|
||||
@@ -46,15 +47,16 @@ from psutil import WINDOWS
|
||||
from psutil._common import bytes2human
|
||||
from psutil._common import print_color
|
||||
from psutil._common import supports_ipv6
|
||||
from psutil._compat import PY3
|
||||
from psutil._compat import FileExistsError
|
||||
from psutil._compat import FileNotFoundError
|
||||
from psutil._compat import PY3
|
||||
from psutil._compat import range
|
||||
from psutil._compat import super
|
||||
from psutil._compat import u
|
||||
from psutil._compat import unicode
|
||||
from psutil._compat import which
|
||||
|
||||
|
||||
if PY3:
|
||||
import unittest
|
||||
else:
|
||||
@@ -72,6 +74,9 @@ if sys.version_info >= (3, 4):
|
||||
else:
|
||||
enum = None
|
||||
|
||||
if POSIX:
|
||||
from psutil._psposix import wait_pid
|
||||
|
||||
|
||||
__all__ = [
|
||||
# constants
|
||||
@@ -482,9 +487,6 @@ def terminate(proc_or_pid, sig=signal.SIGTERM, wait_timeout=GLOBAL_TIMEOUT):
|
||||
Does nothing if the process does not exist.
|
||||
Return process exit status.
|
||||
"""
|
||||
if POSIX:
|
||||
from psutil._psposix import wait_pid
|
||||
|
||||
def wait(proc, timeout):
|
||||
if isinstance(proc, subprocess.Popen) and not PY3:
|
||||
proc.wait()
|
||||
@@ -952,6 +954,15 @@ class TestMemoryLeak(PsutilTestCase):
|
||||
retries = 10 if CI_TESTING else 5
|
||||
verbose = True
|
||||
_thisproc = psutil.Process()
|
||||
_psutil_debug_orig = bool(os.getenv('PSUTIL_DEBUG', 0))
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
psutil._set_debug(False) # avoid spamming to stderr
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
psutil._set_debug(cls._psutil_debug_orig)
|
||||
|
||||
def _get_mem(self):
|
||||
# USS is the closest thing we have to "real" memory usage and it
|
||||
@@ -1724,8 +1735,8 @@ else:
|
||||
in memory via ctypes.
|
||||
Return the new absolutized, normcased path.
|
||||
"""
|
||||
from ctypes import wintypes
|
||||
from ctypes import WinError
|
||||
from ctypes import wintypes
|
||||
ext = ".dll"
|
||||
dst = get_testfn(suffix=suffix + ext)
|
||||
libs = [x.path for x in psutil.Process().memory_maps() if
|
||||
|
||||
@@ -10,4 +10,6 @@ $ python -m psutil.tests
|
||||
"""
|
||||
|
||||
from .runner import main
|
||||
|
||||
|
||||
main()
|
||||
|
||||
@@ -21,6 +21,7 @@ Parallel:
|
||||
"""
|
||||
|
||||
from __future__ import print_function
|
||||
|
||||
import atexit
|
||||
import optparse
|
||||
import os
|
||||
@@ -28,6 +29,8 @@ import sys
|
||||
import textwrap
|
||||
import time
|
||||
import unittest
|
||||
|
||||
|
||||
try:
|
||||
import ctypes
|
||||
except ImportError:
|
||||
@@ -298,16 +301,15 @@ def get_runner(parallel=False):
|
||||
|
||||
# Used by test_*,py modules.
|
||||
def run_from_name(name):
|
||||
if CI_TESTING:
|
||||
print_sysinfo()
|
||||
suite = TestLoader().from_name(name)
|
||||
runner = get_runner()
|
||||
runner.run(suite)
|
||||
|
||||
|
||||
def setup():
|
||||
# Note: doc states that altering os.environment may cause memory
|
||||
# leaks on some platforms.
|
||||
# Sets PSUTIL_TESTING and PSUTIL_DEBUG in the C module.
|
||||
psutil._psplatform.cext.set_testing()
|
||||
psutil._set_debug(True)
|
||||
|
||||
|
||||
def main():
|
||||
|
||||
@@ -10,11 +10,11 @@
|
||||
|
||||
import re
|
||||
|
||||
import psutil
|
||||
from psutil import AIX
|
||||
from psutil.tests import PsutilTestCase
|
||||
from psutil.tests import sh
|
||||
from psutil.tests import unittest
|
||||
import psutil
|
||||
|
||||
|
||||
@unittest.skipIf(not AIX, "AIX only")
|
||||
|
||||
@@ -20,12 +20,12 @@ from psutil import BSD
|
||||
from psutil import FREEBSD
|
||||
from psutil import NETBSD
|
||||
from psutil import OPENBSD
|
||||
from psutil.tests import spawn_testproc
|
||||
from psutil.tests import HAS_BATTERY
|
||||
from psutil.tests import TOLERANCE_SYS_MEM
|
||||
from psutil.tests import PsutilTestCase
|
||||
from psutil.tests import retry_on_failure
|
||||
from psutil.tests import sh
|
||||
from psutil.tests import TOLERANCE_SYS_MEM
|
||||
from psutil.tests import spawn_testproc
|
||||
from psutil.tests import terminate
|
||||
from psutil.tests import unittest
|
||||
from psutil.tests import which
|
||||
|
||||
@@ -8,7 +8,6 @@
|
||||
|
||||
import os
|
||||
import socket
|
||||
import sys
|
||||
import textwrap
|
||||
from contextlib import closing
|
||||
from socket import AF_INET
|
||||
@@ -28,17 +27,17 @@ from psutil import WINDOWS
|
||||
from psutil._common import supports_ipv6
|
||||
from psutil._compat import PY3
|
||||
from psutil.tests import AF_UNIX
|
||||
from psutil.tests import HAS_CONNECTIONS_UNIX
|
||||
from psutil.tests import SKIP_SYSCONS
|
||||
from psutil.tests import PsutilTestCase
|
||||
from psutil.tests import bind_socket
|
||||
from psutil.tests import bind_unix_socket
|
||||
from psutil.tests import check_connection_ntuple
|
||||
from psutil.tests import create_sockets
|
||||
from psutil.tests import HAS_CONNECTIONS_UNIX
|
||||
from psutil.tests import PsutilTestCase
|
||||
from psutil.tests import reap_children
|
||||
from psutil.tests import retry_on_failure
|
||||
from psutil.tests import serialrun
|
||||
from psutil.tests import skip_on_access_denied
|
||||
from psutil.tests import SKIP_SYSCONS
|
||||
from psutil.tests import tcp_socketpair
|
||||
from psutil.tests import unittest
|
||||
from psutil.tests import unix_socketpair
|
||||
@@ -47,7 +46,6 @@ from psutil.tests import wait_for_file
|
||||
|
||||
thisproc = psutil.Process()
|
||||
SOCK_SEQPACKET = getattr(socket, "SOCK_SEQPACKET", object())
|
||||
PYTHON_39 = sys.version_info[:2] == (3, 9)
|
||||
|
||||
|
||||
@serialrun
|
||||
|
||||
@@ -18,6 +18,7 @@ import sys
|
||||
import time
|
||||
import traceback
|
||||
|
||||
import psutil
|
||||
from psutil import AIX
|
||||
from psutil import BSD
|
||||
from psutil import FREEBSD
|
||||
@@ -33,25 +34,24 @@ from psutil._compat import FileNotFoundError
|
||||
from psutil._compat import long
|
||||
from psutil._compat import range
|
||||
from psutil.tests import APPVEYOR
|
||||
from psutil.tests import check_connection_ntuple
|
||||
from psutil.tests import CI_TESTING
|
||||
from psutil.tests import create_sockets
|
||||
from psutil.tests import enum
|
||||
from psutil.tests import GITHUB_ACTIONS
|
||||
from psutil.tests import HAS_CPU_FREQ
|
||||
from psutil.tests import HAS_NET_IO_COUNTERS
|
||||
from psutil.tests import HAS_SENSORS_FANS
|
||||
from psutil.tests import HAS_SENSORS_TEMPERATURES
|
||||
from psutil.tests import PYPY
|
||||
from psutil.tests import SKIP_SYSCONS
|
||||
from psutil.tests import VALID_PROC_STATUSES
|
||||
from psutil.tests import PsutilTestCase
|
||||
from psutil.tests import check_connection_ntuple
|
||||
from psutil.tests import create_sockets
|
||||
from psutil.tests import enum
|
||||
from psutil.tests import is_namedtuple
|
||||
from psutil.tests import kernel_version
|
||||
from psutil.tests import process_namespace
|
||||
from psutil.tests import PsutilTestCase
|
||||
from psutil.tests import PYPY
|
||||
from psutil.tests import serialrun
|
||||
from psutil.tests import SKIP_SYSCONS
|
||||
from psutil.tests import unittest
|
||||
from psutil.tests import VALID_PROC_STATUSES
|
||||
import psutil
|
||||
|
||||
|
||||
# ===================================================================
|
||||
@@ -360,7 +360,6 @@ def proc_info(pid):
|
||||
elif isinstance(exc, psutil.NoSuchProcess):
|
||||
tcase.assertProcessGone(proc)
|
||||
str(exc)
|
||||
assert exc.msg
|
||||
|
||||
def do_wait():
|
||||
if pid != 0:
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
"""Linux specific tests."""
|
||||
|
||||
from __future__ import division
|
||||
|
||||
import collections
|
||||
import contextlib
|
||||
import errno
|
||||
@@ -23,31 +24,39 @@ import warnings
|
||||
|
||||
import psutil
|
||||
from psutil import LINUX
|
||||
from psutil._compat import basestring
|
||||
from psutil._compat import FileNotFoundError
|
||||
from psutil._compat import PY3
|
||||
from psutil._compat import FileNotFoundError
|
||||
from psutil._compat import basestring
|
||||
from psutil._compat import u
|
||||
from psutil.tests import call_until
|
||||
from psutil.tests import GITHUB_ACTIONS
|
||||
from psutil.tests import GLOBAL_TIMEOUT
|
||||
from psutil.tests import HAS_BATTERY
|
||||
from psutil.tests import HAS_CPU_FREQ
|
||||
from psutil.tests import HAS_GETLOADAVG
|
||||
from psutil.tests import HAS_RLIMIT
|
||||
from psutil.tests import mock
|
||||
from psutil.tests import PsutilTestCase
|
||||
from psutil.tests import PYPY
|
||||
from psutil.tests import TOLERANCE_DISK_USAGE
|
||||
from psutil.tests import TOLERANCE_SYS_MEM
|
||||
from psutil.tests import PsutilTestCase
|
||||
from psutil.tests import ThreadTask
|
||||
from psutil.tests import call_until
|
||||
from psutil.tests import mock
|
||||
from psutil.tests import reload_module
|
||||
from psutil.tests import retry_on_failure
|
||||
from psutil.tests import safe_rmpath
|
||||
from psutil.tests import sh
|
||||
from psutil.tests import skip_on_not_implemented
|
||||
from psutil.tests import ThreadTask
|
||||
from psutil.tests import TOLERANCE_DISK_USAGE
|
||||
from psutil.tests import TOLERANCE_SYS_MEM
|
||||
from psutil.tests import unittest
|
||||
from psutil.tests import which
|
||||
|
||||
|
||||
if LINUX:
|
||||
from psutil._pslinux import CLOCK_TICKS
|
||||
from psutil._pslinux import RootFsDeviceFinder
|
||||
from psutil._pslinux import calculate_avail_vmem
|
||||
from psutil._pslinux import open_binary
|
||||
|
||||
|
||||
HERE = os.path.abspath(os.path.dirname(__file__))
|
||||
SIOCGIFADDR = 0x8915
|
||||
SIOCGIFCONF = 0x8912
|
||||
@@ -58,6 +67,7 @@ if LINUX:
|
||||
SECTOR_SIZE = 512
|
||||
EMPTY_TEMPERATURES = not glob.glob('/sys/class/hwmon/hwmon*')
|
||||
|
||||
|
||||
# =====================================================================
|
||||
# --- utils
|
||||
# =====================================================================
|
||||
@@ -141,7 +151,7 @@ def free_swap():
|
||||
"""Parse 'free' cmd and return swap memory's s total, used and free
|
||||
values.
|
||||
"""
|
||||
out = sh('free -b', env={"LANG": "C.UTF-8"})
|
||||
out = sh(["free", "-b"], env={"LANG": "C.UTF-8"})
|
||||
lines = out.split('\n')
|
||||
for line in lines:
|
||||
if line.startswith('Swap'):
|
||||
@@ -160,7 +170,7 @@ def free_physmem():
|
||||
# and 'cached' memory which may have different positions so we
|
||||
# do not return them.
|
||||
# https://github.com/giampaolo/psutil/issues/538#issuecomment-57059946
|
||||
out = sh('free -b', env={"LANG": "C.UTF-8"})
|
||||
out = sh(["free", "-b"], env={"LANG": "C.UTF-8"})
|
||||
lines = out.split('\n')
|
||||
for line in lines:
|
||||
if line.startswith('Mem'):
|
||||
@@ -174,7 +184,7 @@ def free_physmem():
|
||||
|
||||
|
||||
def vmstat(stat):
|
||||
out = sh("vmstat -s", env={"LANG": "C.UTF-8"})
|
||||
out = sh(["vmstat", "-s"], env={"LANG": "C.UTF-8"})
|
||||
for line in out.split("\n"):
|
||||
line = line.strip()
|
||||
if stat in line:
|
||||
@@ -183,7 +193,7 @@ def vmstat(stat):
|
||||
|
||||
|
||||
def get_free_version_info():
|
||||
out = sh("free -V").strip()
|
||||
out = sh(["free", "-V"]).strip()
|
||||
if 'UNKNOWN' in out:
|
||||
raise unittest.SkipTest("can't determine free version")
|
||||
return tuple(map(int, out.split()[-1].split('.')))
|
||||
@@ -243,7 +253,8 @@ class TestSystemVirtualMemory(PsutilTestCase):
|
||||
# self.assertEqual(free_value, psutil_value)
|
||||
vmstat_value = vmstat('total memory') * 1024
|
||||
psutil_value = psutil.virtual_memory().total
|
||||
self.assertAlmostEqual(vmstat_value, psutil_value)
|
||||
self.assertAlmostEqual(
|
||||
vmstat_value, psutil_value, delta=TOLERANCE_SYS_MEM)
|
||||
|
||||
@retry_on_failure()
|
||||
def test_used(self):
|
||||
@@ -303,7 +314,7 @@ class TestSystemVirtualMemory(PsutilTestCase):
|
||||
def test_available(self):
|
||||
# "free" output format has changed at some point:
|
||||
# https://github.com/giampaolo/psutil/issues/538#issuecomment-147192098
|
||||
out = sh("free -b")
|
||||
out = sh(["free", "-b"])
|
||||
lines = out.split('\n')
|
||||
if 'available' not in lines[0]:
|
||||
raise unittest.SkipTest("free does not support 'available' column")
|
||||
@@ -357,9 +368,6 @@ class TestSystemVirtualMemory(PsutilTestCase):
|
||||
def test_avail_old_percent(self):
|
||||
# Make sure that our calculation of avail mem for old kernels
|
||||
# is off by max 15%.
|
||||
from psutil._pslinux import calculate_avail_vmem
|
||||
from psutil._pslinux import open_binary
|
||||
|
||||
mems = {}
|
||||
with open_binary('/proc/meminfo') as f:
|
||||
for line in f:
|
||||
@@ -715,7 +723,7 @@ class TestSystemCPUCountLogical(PsutilTestCase):
|
||||
|
||||
|
||||
@unittest.skipIf(not LINUX, "LINUX only")
|
||||
class TestSystemCPUCountPhysical(PsutilTestCase):
|
||||
class TestSystemCPUCountCores(PsutilTestCase):
|
||||
|
||||
@unittest.skipIf(not which("lscpu"), "lscpu utility not available")
|
||||
def test_against_lscpu(self):
|
||||
@@ -728,9 +736,9 @@ class TestSystemCPUCountPhysical(PsutilTestCase):
|
||||
self.assertEqual(psutil.cpu_count(logical=False), len(core_ids))
|
||||
|
||||
def test_method_2(self):
|
||||
meth_1 = psutil._pslinux.cpu_count_physical()
|
||||
meth_1 = psutil._pslinux.cpu_count_cores()
|
||||
with mock.patch('glob.glob', return_value=[]) as m:
|
||||
meth_2 = psutil._pslinux.cpu_count_physical()
|
||||
meth_2 = psutil._pslinux.cpu_count_cores()
|
||||
assert m.called
|
||||
if meth_1 is not None:
|
||||
self.assertEqual(meth_1, meth_2)
|
||||
@@ -738,7 +746,7 @@ class TestSystemCPUCountPhysical(PsutilTestCase):
|
||||
def test_emulate_none(self):
|
||||
with mock.patch('glob.glob', return_value=[]) as m1:
|
||||
with mock.patch('psutil._common.open', create=True) as m2:
|
||||
self.assertIsNone(psutil._pslinux.cpu_count_physical())
|
||||
self.assertIsNone(psutil._pslinux.cpu_count_cores())
|
||||
assert m1.called
|
||||
assert m2.called
|
||||
|
||||
@@ -768,18 +776,14 @@ class TestSystemCPUFrequency(PsutilTestCase):
|
||||
if path.startswith('/sys/devices/system/cpu/'):
|
||||
return False
|
||||
else:
|
||||
if path == "/proc/cpuinfo":
|
||||
flags.append(None)
|
||||
return os_path_exists(path)
|
||||
|
||||
flags = []
|
||||
os_path_exists = os.path.exists
|
||||
try:
|
||||
with mock.patch("os.path.exists", side_effect=path_exists_mock):
|
||||
reload_module(psutil._pslinux)
|
||||
ret = psutil.cpu_freq()
|
||||
assert ret
|
||||
assert flags
|
||||
self.assertEqual(ret.max, 0.0)
|
||||
self.assertEqual(ret.min, 0.0)
|
||||
for freq in psutil.cpu_freq(percpu=True):
|
||||
@@ -1263,6 +1267,68 @@ class TestSystemDiskIoCounters(PsutilTestCase):
|
||||
self.assertRaises(NotImplementedError, psutil.disk_io_counters)
|
||||
|
||||
|
||||
@unittest.skipIf(not LINUX, "LINUX only")
|
||||
class TestRootFsDeviceFinder(PsutilTestCase):
|
||||
|
||||
def setUp(self):
|
||||
dev = os.stat("/").st_dev
|
||||
self.major = os.major(dev)
|
||||
self.minor = os.minor(dev)
|
||||
|
||||
def test_call_methods(self):
|
||||
finder = RootFsDeviceFinder()
|
||||
if os.path.exists("/proc/partitions"):
|
||||
finder.ask_proc_partitions()
|
||||
else:
|
||||
self.assertRaises(FileNotFoundError, finder.ask_proc_partitions)
|
||||
if os.path.exists("/sys/dev/block/%s:%s/uevent" % (
|
||||
self.major, self.minor)):
|
||||
finder.ask_sys_dev_block()
|
||||
else:
|
||||
self.assertRaises(FileNotFoundError, finder.ask_sys_dev_block)
|
||||
finder.ask_sys_class_block()
|
||||
|
||||
@unittest.skipIf(GITHUB_ACTIONS, "unsupported on GITHUB_ACTIONS")
|
||||
def test_comparisons(self):
|
||||
finder = RootFsDeviceFinder()
|
||||
self.assertIsNotNone(finder.find())
|
||||
|
||||
a = b = c = None
|
||||
if os.path.exists("/proc/partitions"):
|
||||
a = finder.ask_proc_partitions()
|
||||
if os.path.exists("/sys/dev/block/%s:%s/uevent" % (
|
||||
self.major, self.minor)):
|
||||
b = finder.ask_sys_class_block()
|
||||
c = finder.ask_sys_dev_block()
|
||||
|
||||
base = a or b or c
|
||||
if base and a:
|
||||
self.assertEqual(base, a)
|
||||
if base and b:
|
||||
self.assertEqual(base, b)
|
||||
if base and c:
|
||||
self.assertEqual(base, c)
|
||||
|
||||
@unittest.skipIf(not which("findmnt"), "findmnt utility not available")
|
||||
@unittest.skipIf(GITHUB_ACTIONS, "unsupported on GITHUB_ACTIONS")
|
||||
def test_against_findmnt(self):
|
||||
psutil_value = RootFsDeviceFinder().find()
|
||||
findmnt_value = sh("findmnt -o SOURCE -rn /")
|
||||
self.assertEqual(psutil_value, findmnt_value)
|
||||
|
||||
def test_disk_partitions_mocked(self):
|
||||
with mock.patch(
|
||||
'psutil._pslinux.cext.disk_partitions',
|
||||
return_value=[('/dev/root', '/', 'ext4', 'rw')]) as m:
|
||||
part = psutil.disk_partitions()[0]
|
||||
assert m.called
|
||||
if not GITHUB_ACTIONS:
|
||||
self.assertNotEqual(part.device, "/dev/root")
|
||||
self.assertEqual(part.device, RootFsDeviceFinder().find())
|
||||
else:
|
||||
self.assertEqual(part.device, "/dev/root")
|
||||
|
||||
|
||||
# =====================================================================
|
||||
# --- misc
|
||||
# =====================================================================
|
||||
@@ -1431,9 +1497,7 @@ class TestMisc(PsutilTestCase):
|
||||
# - Process(tid) is supposed to work
|
||||
# - pids() should not return the TID
|
||||
# See: https://github.com/giampaolo/psutil/issues/687
|
||||
t = ThreadTask()
|
||||
t.start()
|
||||
try:
|
||||
with ThreadTask():
|
||||
p = psutil.Process()
|
||||
threads = p.threads()
|
||||
self.assertEqual(len(threads), 2)
|
||||
@@ -1442,8 +1506,6 @@ class TestMisc(PsutilTestCase):
|
||||
pt = psutil.Process(tid)
|
||||
pt.as_dict()
|
||||
self.assertNotIn(tid, psutil.pids())
|
||||
finally:
|
||||
t.stop()
|
||||
|
||||
def test_pid_exists_no_proc_status(self):
|
||||
# Internally pid_exists relies on /proc/{pid}/status.
|
||||
@@ -1837,6 +1899,22 @@ class TestProcess(PsutilTestCase):
|
||||
assert not files
|
||||
assert m.called
|
||||
|
||||
def test_open_files_enametoolong(self):
|
||||
# Simulate a case where /proc/{pid}/fd/{fd} symlink
|
||||
# points to a file with full path longer than PATH_MAX, see:
|
||||
# https://github.com/giampaolo/psutil/issues/1940
|
||||
p = psutil.Process()
|
||||
files = p.open_files()
|
||||
with open(self.get_testfn(), 'w'):
|
||||
# give the kernel some time to see the new file
|
||||
call_until(p.open_files, "len(ret) != %i" % len(files))
|
||||
patch_point = 'psutil._pslinux.os.readlink'
|
||||
with mock.patch(patch_point,
|
||||
side_effect=OSError(errno.ENAMETOOLONG, "")) as m:
|
||||
files = p.open_files()
|
||||
assert not files
|
||||
assert m.called
|
||||
|
||||
# --- mocked tests
|
||||
|
||||
def test_terminal_mocked(self):
|
||||
@@ -1985,8 +2063,6 @@ class TestProcess(PsutilTestCase):
|
||||
self.assertEqual(exc.exception.name, p.name())
|
||||
|
||||
def test_stat_file_parsing(self):
|
||||
from psutil._pslinux import CLOCK_TICKS
|
||||
|
||||
args = [
|
||||
"0", # pid
|
||||
"(cat)", # name
|
||||
@@ -2072,6 +2148,16 @@ class TestProcess(PsutilTestCase):
|
||||
self.assertEqual(gids.saved, 1006)
|
||||
self.assertEqual(p._proc._get_eligible_cpus(), list(range(0, 8)))
|
||||
|
||||
def test_connections_enametoolong(self):
|
||||
# Simulate a case where /proc/{pid}/fd/{fd} symlink points to
|
||||
# a file with full path longer than PATH_MAX, see:
|
||||
# https://github.com/giampaolo/psutil/issues/1940
|
||||
with mock.patch('psutil._pslinux.os.readlink',
|
||||
side_effect=OSError(errno.ENAMETOOLONG, "")) as m:
|
||||
p = psutil.Process()
|
||||
assert not p.connections()
|
||||
assert m.called
|
||||
|
||||
|
||||
@unittest.skipIf(not LINUX, "LINUX only")
|
||||
class TestProcessAgainstStatus(PsutilTestCase):
|
||||
|
||||
@@ -16,6 +16,7 @@ because of how its JIT handles memory, so tests are skipped.
|
||||
"""
|
||||
|
||||
from __future__ import print_function
|
||||
|
||||
import functools
|
||||
import os
|
||||
|
||||
@@ -29,8 +30,6 @@ from psutil import SUNOS
|
||||
from psutil import WINDOWS
|
||||
from psutil._compat import ProcessLookupError
|
||||
from psutil._compat import super
|
||||
from psutil.tests import create_sockets
|
||||
from psutil.tests import get_testfn
|
||||
from psutil.tests import HAS_CPU_AFFINITY
|
||||
from psutil.tests import HAS_CPU_FREQ
|
||||
from psutil.tests import HAS_ENVIRON
|
||||
@@ -43,12 +42,14 @@ from psutil.tests import HAS_RLIMIT
|
||||
from psutil.tests import HAS_SENSORS_BATTERY
|
||||
from psutil.tests import HAS_SENSORS_FANS
|
||||
from psutil.tests import HAS_SENSORS_TEMPERATURES
|
||||
from psutil.tests import TestMemoryLeak
|
||||
from psutil.tests import create_sockets
|
||||
from psutil.tests import get_testfn
|
||||
from psutil.tests import process_namespace
|
||||
from psutil.tests import skip_on_access_denied
|
||||
from psutil.tests import spawn_testproc
|
||||
from psutil.tests import system_namespace
|
||||
from psutil.tests import terminate
|
||||
from psutil.tests import TestMemoryLeak
|
||||
from psutil.tests import unittest
|
||||
|
||||
|
||||
@@ -347,7 +348,7 @@ class TestModuleFunctionsLeaks(TestMemoryLeak):
|
||||
self.execute(lambda: psutil.cpu_count(logical=True))
|
||||
|
||||
@fewtimes_if_linux()
|
||||
def test_cpu_count_physical(self):
|
||||
def test_cpu_count_cores(self):
|
||||
self.execute(lambda: psutil.cpu_count(logical=False))
|
||||
|
||||
@fewtimes_if_linux()
|
||||
@@ -456,6 +457,9 @@ class TestModuleFunctionsLeaks(TestMemoryLeak):
|
||||
def test_users(self):
|
||||
self.execute(psutil.users)
|
||||
|
||||
def test_set_debug(self):
|
||||
self.execute(lambda: psutil._set_debug(False))
|
||||
|
||||
if WINDOWS:
|
||||
|
||||
# --- win services
|
||||
|
||||
@@ -17,16 +17,19 @@ import os
|
||||
import pickle
|
||||
import socket
|
||||
import stat
|
||||
import sys
|
||||
|
||||
import psutil
|
||||
import psutil.tests
|
||||
from psutil import LINUX
|
||||
from psutil import POSIX
|
||||
from psutil import WINDOWS
|
||||
from psutil._common import debug
|
||||
from psutil._common import memoize
|
||||
from psutil._common import memoize_when_activated
|
||||
from psutil._common import supports_ipv6
|
||||
from psutil._common import wrap_numbers
|
||||
from psutil._compat import PY3
|
||||
from psutil._compat import redirect_stderr
|
||||
from psutil.tests import APPVEYOR
|
||||
from psutil.tests import CI_TESTING
|
||||
from psutil.tests import HAS_BATTERY
|
||||
@@ -35,20 +38,15 @@ from psutil.tests import HAS_NET_IO_COUNTERS
|
||||
from psutil.tests import HAS_SENSORS_BATTERY
|
||||
from psutil.tests import HAS_SENSORS_FANS
|
||||
from psutil.tests import HAS_SENSORS_TEMPERATURES
|
||||
from psutil.tests import import_module_by_path
|
||||
from psutil.tests import mock
|
||||
from psutil.tests import PsutilTestCase
|
||||
from psutil.tests import PYTHON_EXE
|
||||
from psutil.tests import reload_module
|
||||
from psutil.tests import ROOT_DIR
|
||||
from psutil.tests import SCRIPTS_DIR
|
||||
from psutil.tests import PsutilTestCase
|
||||
from psutil.tests import import_module_by_path
|
||||
from psutil.tests import mock
|
||||
from psutil.tests import reload_module
|
||||
from psutil.tests import sh
|
||||
from psutil.tests import unittest
|
||||
import psutil
|
||||
import psutil.tests
|
||||
|
||||
|
||||
PYTHON_39 = sys.version_info[:2] == (3, 9)
|
||||
|
||||
|
||||
# ===================================================================
|
||||
@@ -97,57 +95,71 @@ class TestMisc(PsutilTestCase):
|
||||
def test_process__str__(self):
|
||||
self.test_process__repr__(func=str)
|
||||
|
||||
def test_no_such_process__repr__(self, func=repr):
|
||||
def test_no_such_process__repr__(self):
|
||||
self.assertEqual(
|
||||
repr(psutil.NoSuchProcess(321)),
|
||||
"psutil.NoSuchProcess process no longer exists (pid=321)")
|
||||
"psutil.NoSuchProcess(pid=321, msg='process no longer exists')")
|
||||
self.assertEqual(
|
||||
repr(psutil.NoSuchProcess(321, name='foo')),
|
||||
"psutil.NoSuchProcess process no longer exists (pid=321, "
|
||||
"name='foo')")
|
||||
self.assertEqual(
|
||||
repr(psutil.NoSuchProcess(321, msg='foo')),
|
||||
"psutil.NoSuchProcess foo")
|
||||
repr(psutil.NoSuchProcess(321, name="name", msg="msg")),
|
||||
"psutil.NoSuchProcess(pid=321, name='name', msg='msg')")
|
||||
|
||||
def test_zombie_process__repr__(self, func=repr):
|
||||
def test_no_such_process__str__(self):
|
||||
self.assertEqual(
|
||||
str(psutil.NoSuchProcess(321)),
|
||||
"process no longer exists (pid=321)")
|
||||
self.assertEqual(
|
||||
str(psutil.NoSuchProcess(321, name="name", msg="msg")),
|
||||
"msg (pid=321, name='name')")
|
||||
|
||||
def test_zombie_process__repr__(self):
|
||||
self.assertEqual(
|
||||
repr(psutil.ZombieProcess(321)),
|
||||
"psutil.ZombieProcess process still exists but it's a zombie "
|
||||
"(pid=321)")
|
||||
'psutil.ZombieProcess(pid=321, msg="PID still '
|
||||
'exists but it\'s a zombie")')
|
||||
self.assertEqual(
|
||||
repr(psutil.ZombieProcess(321, name='foo')),
|
||||
"psutil.ZombieProcess process still exists but it's a zombie "
|
||||
"(pid=321, name='foo')")
|
||||
self.assertEqual(
|
||||
repr(psutil.ZombieProcess(321, name='foo', ppid=1)),
|
||||
"psutil.ZombieProcess process still exists but it's a zombie "
|
||||
"(pid=321, name='foo', ppid=1)")
|
||||
self.assertEqual(
|
||||
repr(psutil.ZombieProcess(321, msg='foo')),
|
||||
"psutil.ZombieProcess foo")
|
||||
repr(psutil.ZombieProcess(321, name="name", ppid=320, msg="foo")),
|
||||
"psutil.ZombieProcess(pid=321, ppid=320, name='name', msg='foo')")
|
||||
|
||||
def test_access_denied__repr__(self, func=repr):
|
||||
def test_zombie_process__str__(self):
|
||||
self.assertEqual(
|
||||
str(psutil.ZombieProcess(321)),
|
||||
"PID still exists but it's a zombie (pid=321)")
|
||||
self.assertEqual(
|
||||
str(psutil.ZombieProcess(321, name="name", ppid=320, msg="foo")),
|
||||
"foo (pid=321, ppid=320, name='name')")
|
||||
|
||||
def test_access_denied__repr__(self):
|
||||
self.assertEqual(
|
||||
repr(psutil.AccessDenied(321)),
|
||||
"psutil.AccessDenied (pid=321)")
|
||||
"psutil.AccessDenied(pid=321)")
|
||||
self.assertEqual(
|
||||
repr(psutil.AccessDenied(321, name='foo')),
|
||||
"psutil.AccessDenied (pid=321, name='foo')")
|
||||
self.assertEqual(
|
||||
repr(psutil.AccessDenied(321, msg='foo')),
|
||||
"psutil.AccessDenied foo")
|
||||
repr(psutil.AccessDenied(321, name="name", msg="msg")),
|
||||
"psutil.AccessDenied(pid=321, name='name', msg='msg')")
|
||||
|
||||
def test_timeout_expired__repr__(self, func=repr):
|
||||
def test_access_denied__str__(self):
|
||||
self.assertEqual(
|
||||
repr(psutil.TimeoutExpired(321)),
|
||||
"psutil.TimeoutExpired timeout after 321 seconds")
|
||||
str(psutil.AccessDenied(321)),
|
||||
"(pid=321)")
|
||||
self.assertEqual(
|
||||
repr(psutil.TimeoutExpired(321, pid=111)),
|
||||
"psutil.TimeoutExpired timeout after 321 seconds (pid=111)")
|
||||
str(psutil.AccessDenied(321, name="name", msg="msg")),
|
||||
"msg (pid=321, name='name')")
|
||||
|
||||
def test_timeout_expired__repr__(self):
|
||||
self.assertEqual(
|
||||
repr(psutil.TimeoutExpired(321, pid=111, name='foo')),
|
||||
"psutil.TimeoutExpired timeout after 321 seconds "
|
||||
"(pid=111, name='foo')")
|
||||
repr(psutil.TimeoutExpired(5)),
|
||||
"psutil.TimeoutExpired(seconds=5, msg='timeout after 5 seconds')")
|
||||
self.assertEqual(
|
||||
repr(psutil.TimeoutExpired(5, pid=321, name="name")),
|
||||
"psutil.TimeoutExpired(pid=321, name='name', seconds=5, "
|
||||
"msg='timeout after 5 seconds')")
|
||||
|
||||
def test_timeout_expired__str__(self):
|
||||
self.assertEqual(
|
||||
str(psutil.TimeoutExpired(5)),
|
||||
"timeout after 5 seconds")
|
||||
self.assertEqual(
|
||||
str(psutil.TimeoutExpired(5, pid=321, name="name")),
|
||||
"timeout after 5 seconds (pid=321, name='name')")
|
||||
|
||||
def test_process__eq__(self):
|
||||
p1 = psutil.Process()
|
||||
@@ -354,6 +366,8 @@ class TestMisc(PsutilTestCase):
|
||||
check(psutil.disk_usage(os.getcwd()))
|
||||
check(psutil.users())
|
||||
|
||||
# XXX: https://github.com/pypa/setuptools/pull/2896
|
||||
@unittest.skipIf(APPVEYOR, "temporarily disabled due to setuptools bug")
|
||||
def test_setup_script(self):
|
||||
setup_py = os.path.join(ROOT_DIR, 'setup.py')
|
||||
if CI_TESTING and not os.path.exists(setup_py):
|
||||
@@ -387,6 +401,35 @@ class TestMisc(PsutilTestCase):
|
||||
reload_module(psutil)
|
||||
self.assertIn("version conflict", str(cm.exception).lower())
|
||||
|
||||
def test_debug(self):
|
||||
if PY3:
|
||||
from io import StringIO
|
||||
else:
|
||||
from StringIO import StringIO
|
||||
|
||||
with redirect_stderr(StringIO()) as f:
|
||||
debug("hello")
|
||||
msg = f.getvalue()
|
||||
assert msg.startswith("psutil-debug"), msg
|
||||
self.assertIn("hello", msg)
|
||||
self.assertIn(__file__.replace('.pyc', '.py'), msg)
|
||||
|
||||
# supposed to use repr(exc)
|
||||
with redirect_stderr(StringIO()) as f:
|
||||
debug(ValueError("this is an error"))
|
||||
msg = f.getvalue()
|
||||
self.assertIn("ignoring ValueError", msg)
|
||||
self.assertIn("'this is an error'", msg)
|
||||
|
||||
# supposed to use str(exc), because of extra info about file name
|
||||
with redirect_stderr(StringIO()) as f:
|
||||
exc = OSError(2, "no such file")
|
||||
exc.filename = "/foo"
|
||||
debug(exc)
|
||||
msg = f.getvalue()
|
||||
self.assertIn("no such file", msg)
|
||||
self.assertIn("/foo", msg)
|
||||
|
||||
|
||||
# ===================================================================
|
||||
# --- Tests for wrap_numbers() function.
|
||||
@@ -679,11 +722,12 @@ class TestScripts(PsutilTestCase):
|
||||
|
||||
@unittest.skipIf(not POSIX, "POSIX only")
|
||||
def test_executable(self):
|
||||
for name in os.listdir(SCRIPTS_DIR):
|
||||
if name.endswith('.py'):
|
||||
path = os.path.join(SCRIPTS_DIR, name)
|
||||
if not stat.S_IXUSR & os.stat(path)[stat.ST_MODE]:
|
||||
self.fail('%r is not executable' % path)
|
||||
for root, dirs, files in os.walk(SCRIPTS_DIR):
|
||||
for file in files:
|
||||
if file.endswith('.py'):
|
||||
path = os.path.join(root, file)
|
||||
if not stat.S_IXUSR & os.stat(path)[stat.ST_MODE]:
|
||||
raise self.fail('%r is not executable' % path)
|
||||
|
||||
def test_disk_usage(self):
|
||||
self.assert_stdout('disk_usage.py')
|
||||
|
||||
@@ -11,17 +11,22 @@ import time
|
||||
|
||||
import psutil
|
||||
from psutil import MACOS
|
||||
from psutil import POSIX
|
||||
from psutil.tests import HAS_BATTERY
|
||||
from psutil.tests import TOLERANCE_DISK_USAGE
|
||||
from psutil.tests import TOLERANCE_SYS_MEM
|
||||
from psutil.tests import PsutilTestCase
|
||||
from psutil.tests import retry_on_failure
|
||||
from psutil.tests import sh
|
||||
from psutil.tests import spawn_testproc
|
||||
from psutil.tests import terminate
|
||||
from psutil.tests import TOLERANCE_DISK_USAGE
|
||||
from psutil.tests import TOLERANCE_SYS_MEM
|
||||
from psutil.tests import unittest
|
||||
|
||||
|
||||
if POSIX:
|
||||
from psutil._psutil_posix import getpagesize
|
||||
|
||||
|
||||
def sysctl(cmdline):
|
||||
"""Expects a sysctl command with an argument and parse the result
|
||||
returning only the value of interest.
|
||||
@@ -36,8 +41,6 @@ def sysctl(cmdline):
|
||||
|
||||
def vm_stat(field):
|
||||
"""Wrapper around 'vm_stat' cmdline utility."""
|
||||
from psutil._psutil_posix import getpagesize
|
||||
|
||||
out = sh('vm_stat')
|
||||
for line in out.split('\n'):
|
||||
if field in line:
|
||||
@@ -137,7 +140,7 @@ class TestSystemAPIs(PsutilTestCase):
|
||||
num = sysctl("sysctl hw.logicalcpu")
|
||||
self.assertEqual(num, psutil.cpu_count(logical=True))
|
||||
|
||||
def test_cpu_count_physical(self):
|
||||
def test_cpu_count_cores(self):
|
||||
num = sysctl("sysctl hw.physicalcpu")
|
||||
self.assertEqual(num, psutil.cpu_count(logical=False))
|
||||
|
||||
|
||||
@@ -23,18 +23,19 @@ from psutil import OPENBSD
|
||||
from psutil import POSIX
|
||||
from psutil import SUNOS
|
||||
from psutil.tests import CI_TESTING
|
||||
from psutil.tests import spawn_testproc
|
||||
from psutil.tests import HAS_NET_IO_COUNTERS
|
||||
from psutil.tests import mock
|
||||
from psutil.tests import PsutilTestCase
|
||||
from psutil.tests import PYTHON_EXE
|
||||
from psutil.tests import PsutilTestCase
|
||||
from psutil.tests import mock
|
||||
from psutil.tests import retry_on_failure
|
||||
from psutil.tests import sh
|
||||
from psutil.tests import skip_on_access_denied
|
||||
from psutil.tests import spawn_testproc
|
||||
from psutil.tests import terminate
|
||||
from psutil.tests import unittest
|
||||
from psutil.tests import which
|
||||
|
||||
|
||||
if POSIX:
|
||||
import mmap
|
||||
import resource
|
||||
|
||||
@@ -21,7 +21,6 @@ import time
|
||||
import types
|
||||
|
||||
import psutil
|
||||
|
||||
from psutil import AIX
|
||||
from psutil import BSD
|
||||
from psutil import LINUX
|
||||
@@ -33,15 +32,12 @@ from psutil import POSIX
|
||||
from psutil import SUNOS
|
||||
from psutil import WINDOWS
|
||||
from psutil._common import open_text
|
||||
from psutil._compat import PY3
|
||||
from psutil._compat import FileNotFoundError
|
||||
from psutil._compat import long
|
||||
from psutil._compat import PY3
|
||||
from psutil._compat import super
|
||||
from psutil.tests import APPVEYOR
|
||||
from psutil.tests import call_until
|
||||
from psutil.tests import CI_TESTING
|
||||
from psutil.tests import copyload_shared_lib
|
||||
from psutil.tests import create_exe
|
||||
from psutil.tests import GITHUB_ACTIONS
|
||||
from psutil.tests import GLOBAL_TIMEOUT
|
||||
from psutil.tests import HAS_CPU_AFFINITY
|
||||
@@ -52,17 +48,20 @@ from psutil.tests import HAS_PROC_CPU_NUM
|
||||
from psutil.tests import HAS_PROC_IO_COUNTERS
|
||||
from psutil.tests import HAS_RLIMIT
|
||||
from psutil.tests import HAS_THREADS
|
||||
from psutil.tests import mock
|
||||
from psutil.tests import process_namespace
|
||||
from psutil.tests import PsutilTestCase
|
||||
from psutil.tests import PYPY
|
||||
from psutil.tests import PYTHON_EXE
|
||||
from psutil.tests import PsutilTestCase
|
||||
from psutil.tests import ThreadTask
|
||||
from psutil.tests import call_until
|
||||
from psutil.tests import copyload_shared_lib
|
||||
from psutil.tests import create_exe
|
||||
from psutil.tests import mock
|
||||
from psutil.tests import process_namespace
|
||||
from psutil.tests import reap_children
|
||||
from psutil.tests import retry_on_failure
|
||||
from psutil.tests import sh
|
||||
from psutil.tests import skip_on_access_denied
|
||||
from psutil.tests import skip_on_not_implemented
|
||||
from psutil.tests import ThreadTask
|
||||
from psutil.tests import unittest
|
||||
from psutil.tests import wait_for_pid
|
||||
|
||||
@@ -718,6 +717,12 @@ class TestProcess(PsutilTestCase):
|
||||
if NETBSD or OPENBSD or AIX:
|
||||
self.assertEqual(p.cmdline()[0], PYTHON_EXE)
|
||||
else:
|
||||
if MACOS and CI_TESTING:
|
||||
pyexe = p.cmdline()[0]
|
||||
if pyexe != PYTHON_EXE:
|
||||
self.assertEqual(' '.join(p.cmdline()[1:]),
|
||||
' '.join(cmdline[1:]))
|
||||
return
|
||||
self.assertEqual(' '.join(p.cmdline()), ' '.join(cmdline))
|
||||
|
||||
@unittest.skipIf(PYPY, "broken on PYPY")
|
||||
@@ -1326,6 +1331,20 @@ class TestProcess(PsutilTestCase):
|
||||
self.assertEqual(p.status(), psutil.STATUS_ZOMBIE)
|
||||
assert m.called
|
||||
|
||||
def test_reused_pid(self):
|
||||
# Emulate a case where PID has been reused by another process.
|
||||
subp = self.spawn_testproc()
|
||||
p = psutil.Process(subp.pid)
|
||||
p._ident = (p.pid, p.create_time() + 100)
|
||||
assert not p.is_running()
|
||||
assert p != psutil.Process(subp.pid)
|
||||
msg = "process no longer exists and its PID has been reused"
|
||||
self.assertRaisesRegex(psutil.NoSuchProcess, msg, p.suspend)
|
||||
self.assertRaisesRegex(psutil.NoSuchProcess, msg, p.resume)
|
||||
self.assertRaisesRegex(psutil.NoSuchProcess, msg, p.terminate)
|
||||
self.assertRaisesRegex(psutil.NoSuchProcess, msg, p.kill)
|
||||
self.assertRaisesRegex(psutil.NoSuchProcess, msg, p.children)
|
||||
|
||||
def test_pid_0(self):
|
||||
# Process(0) is supposed to work on all platforms except Linux
|
||||
if 0 not in psutil.pids():
|
||||
@@ -1369,7 +1388,6 @@ class TestProcess(PsutilTestCase):
|
||||
def test_environ(self):
|
||||
def clean_dict(d):
|
||||
# Most of these are problematic on Travis.
|
||||
d.pop("PSUTIL_TESTING", None)
|
||||
d.pop("PLAT", None)
|
||||
d.pop("HOME", None)
|
||||
if MACOS:
|
||||
@@ -1395,11 +1413,13 @@ class TestProcess(PsutilTestCase):
|
||||
code = textwrap.dedent("""
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
char * const argv[] = {"cat", 0};
|
||||
char * const envp[] = {"A=1", "X", "C=3", 0};
|
||||
|
||||
int main(void) {
|
||||
/* Close stderr on exec so parent can wait for the execve to
|
||||
* finish. */
|
||||
// Close stderr on exec so parent can wait for the
|
||||
// execve to finish.
|
||||
if (fcntl(2, F_SETFD, FD_CLOEXEC) != 0)
|
||||
return 0;
|
||||
return execve("/bin/cat", argv, envp);
|
||||
|
||||
@@ -31,10 +31,9 @@ from psutil import WINDOWS
|
||||
from psutil._compat import FileNotFoundError
|
||||
from psutil._compat import long
|
||||
from psutil.tests import ASCII_FS
|
||||
from psutil.tests import check_net_address
|
||||
from psutil.tests import CI_TESTING
|
||||
from psutil.tests import DEVNULL
|
||||
from psutil.tests import enum
|
||||
from psutil.tests import GITHUB_ACTIONS
|
||||
from psutil.tests import GLOBAL_TIMEOUT
|
||||
from psutil.tests import HAS_BATTERY
|
||||
from psutil.tests import HAS_CPU_FREQ
|
||||
@@ -44,12 +43,13 @@ from psutil.tests import HAS_SENSORS_BATTERY
|
||||
from psutil.tests import HAS_SENSORS_FANS
|
||||
from psutil.tests import HAS_SENSORS_TEMPERATURES
|
||||
from psutil.tests import IS_64BIT
|
||||
from psutil.tests import mock
|
||||
from psutil.tests import PsutilTestCase
|
||||
from psutil.tests import PYPY
|
||||
from psutil.tests import retry_on_failure
|
||||
from psutil.tests import GITHUB_ACTIONS
|
||||
from psutil.tests import UNICODE_SUFFIX
|
||||
from psutil.tests import PsutilTestCase
|
||||
from psutil.tests import check_net_address
|
||||
from psutil.tests import enum
|
||||
from psutil.tests import mock
|
||||
from psutil.tests import retry_on_failure
|
||||
from psutil.tests import unittest
|
||||
|
||||
|
||||
@@ -316,16 +316,16 @@ class TestCpuAPIs(PsutilTestCase):
|
||||
if "physical id" not in cpuinfo_data:
|
||||
raise unittest.SkipTest("cpuinfo doesn't include physical id")
|
||||
|
||||
def test_cpu_count_physical(self):
|
||||
def test_cpu_count_cores(self):
|
||||
logical = psutil.cpu_count()
|
||||
physical = psutil.cpu_count(logical=False)
|
||||
if physical is None:
|
||||
raise self.skipTest("physical cpu_count() is None")
|
||||
cores = psutil.cpu_count(logical=False)
|
||||
if cores is None:
|
||||
raise self.skipTest("cpu_count_cores() is None")
|
||||
if WINDOWS and sys.getwindowsversion()[:2] <= (6, 1): # <= Vista
|
||||
self.assertIsNone(physical)
|
||||
self.assertIsNone(cores)
|
||||
else:
|
||||
self.assertGreaterEqual(physical, 1)
|
||||
self.assertGreaterEqual(logical, physical)
|
||||
self.assertGreaterEqual(cores, 1)
|
||||
self.assertGreaterEqual(logical, cores)
|
||||
|
||||
def test_cpu_count_none(self):
|
||||
# https://github.com/giampaolo/psutil/issues/1085
|
||||
@@ -334,7 +334,7 @@ class TestCpuAPIs(PsutilTestCase):
|
||||
return_value=val) as m:
|
||||
self.assertIsNone(psutil.cpu_count())
|
||||
assert m.called
|
||||
with mock.patch('psutil._psplatform.cpu_count_physical',
|
||||
with mock.patch('psutil._psplatform.cpu_count_cores',
|
||||
return_value=val) as m:
|
||||
self.assertIsNone(psutil.cpu_count(logical=False))
|
||||
assert m.called
|
||||
|
||||
@@ -17,25 +17,28 @@ import socket
|
||||
import stat
|
||||
import subprocess
|
||||
|
||||
import psutil
|
||||
import psutil.tests
|
||||
from psutil import FREEBSD
|
||||
from psutil import NETBSD
|
||||
from psutil import POSIX
|
||||
from psutil._common import open_binary
|
||||
from psutil._common import open_text
|
||||
from psutil._common import supports_ipv6
|
||||
from psutil.tests import CI_TESTING
|
||||
from psutil.tests import HAS_CONNECTIONS_UNIX
|
||||
from psutil.tests import PYTHON_EXE
|
||||
from psutil.tests import PsutilTestCase
|
||||
from psutil.tests import TestMemoryLeak
|
||||
from psutil.tests import bind_socket
|
||||
from psutil.tests import bind_unix_socket
|
||||
from psutil.tests import call_until
|
||||
from psutil.tests import chdir
|
||||
from psutil.tests import CI_TESTING
|
||||
from psutil.tests import create_sockets
|
||||
from psutil.tests import get_free_port
|
||||
from psutil.tests import HAS_CONNECTIONS_UNIX
|
||||
from psutil.tests import is_namedtuple
|
||||
from psutil.tests import mock
|
||||
from psutil.tests import process_namespace
|
||||
from psutil.tests import PsutilTestCase
|
||||
from psutil.tests import PYTHON_EXE
|
||||
from psutil.tests import reap_children
|
||||
from psutil.tests import retry
|
||||
from psutil.tests import retry_on_failure
|
||||
@@ -45,13 +48,11 @@ from psutil.tests import serialrun
|
||||
from psutil.tests import system_namespace
|
||||
from psutil.tests import tcp_socketpair
|
||||
from psutil.tests import terminate
|
||||
from psutil.tests import TestMemoryLeak
|
||||
from psutil.tests import unittest
|
||||
from psutil.tests import unix_socketpair
|
||||
from psutil.tests import wait_for_file
|
||||
from psutil.tests import wait_for_pid
|
||||
import psutil
|
||||
import psutil.tests
|
||||
|
||||
|
||||
# ===================================================================
|
||||
# --- Unit tests for test utilities.
|
||||
@@ -350,6 +351,7 @@ class TestNetUtils(PsutilTestCase):
|
||||
@serialrun
|
||||
class TestMemLeakClass(TestMemoryLeak):
|
||||
|
||||
@retry_on_failure()
|
||||
def test_times(self):
|
||||
def fun():
|
||||
cnt['cnt'] += 1
|
||||
|
||||
@@ -79,6 +79,7 @@ import traceback
|
||||
import warnings
|
||||
from contextlib import closing
|
||||
|
||||
import psutil
|
||||
from psutil import BSD
|
||||
from psutil import OPENBSD
|
||||
from psutil import POSIX
|
||||
@@ -87,28 +88,27 @@ from psutil._compat import PY3
|
||||
from psutil._compat import u
|
||||
from psutil.tests import APPVEYOR
|
||||
from psutil.tests import ASCII_FS
|
||||
from psutil.tests import bind_unix_socket
|
||||
from psutil.tests import chdir
|
||||
from psutil.tests import CI_TESTING
|
||||
from psutil.tests import copyload_shared_lib
|
||||
from psutil.tests import create_exe
|
||||
from psutil.tests import get_testfn
|
||||
from psutil.tests import HAS_CONNECTIONS_UNIX
|
||||
from psutil.tests import HAS_ENVIRON
|
||||
from psutil.tests import HAS_MEMORY_MAPS
|
||||
from psutil.tests import INVALID_UNICODE_SUFFIX
|
||||
from psutil.tests import PsutilTestCase
|
||||
from psutil.tests import PYPY
|
||||
from psutil.tests import TESTFN_PREFIX
|
||||
from psutil.tests import UNICODE_SUFFIX
|
||||
from psutil.tests import PsutilTestCase
|
||||
from psutil.tests import bind_unix_socket
|
||||
from psutil.tests import chdir
|
||||
from psutil.tests import copyload_shared_lib
|
||||
from psutil.tests import create_exe
|
||||
from psutil.tests import get_testfn
|
||||
from psutil.tests import safe_mkdir
|
||||
from psutil.tests import safe_rmpath
|
||||
from psutil.tests import serialrun
|
||||
from psutil.tests import skip_on_access_denied
|
||||
from psutil.tests import spawn_testproc
|
||||
from psutil.tests import terminate
|
||||
from psutil.tests import TESTFN_PREFIX
|
||||
from psutil.tests import UNICODE_SUFFIX
|
||||
from psutil.tests import unittest
|
||||
import psutil
|
||||
|
||||
|
||||
if APPVEYOR:
|
||||
|
||||
@@ -27,15 +27,15 @@ from psutil.tests import APPVEYOR
|
||||
from psutil.tests import GITHUB_ACTIONS
|
||||
from psutil.tests import HAS_BATTERY
|
||||
from psutil.tests import IS_64BIT
|
||||
from psutil.tests import mock
|
||||
from psutil.tests import PsutilTestCase
|
||||
from psutil.tests import PY3
|
||||
from psutil.tests import PYPY
|
||||
from psutil.tests import TOLERANCE_DISK_USAGE
|
||||
from psutil.tests import PsutilTestCase
|
||||
from psutil.tests import mock
|
||||
from psutil.tests import retry_on_failure
|
||||
from psutil.tests import sh
|
||||
from psutil.tests import spawn_testproc
|
||||
from psutil.tests import terminate
|
||||
from psutil.tests import TOLERANCE_DISK_USAGE
|
||||
from psutil.tests import unittest
|
||||
|
||||
|
||||
@@ -47,24 +47,13 @@ if WINDOWS and not PYPY:
|
||||
import win32process
|
||||
import wmi # requires "pip install wmi" / "make setup-dev-env"
|
||||
|
||||
if WINDOWS:
|
||||
from psutil._pswindows import convert_oserror
|
||||
|
||||
|
||||
cext = psutil._psplatform.cext
|
||||
|
||||
|
||||
def wrap_exceptions(fun):
|
||||
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.ESRCH:
|
||||
raise psutil.NoSuchProcess(None, None)
|
||||
raise
|
||||
return wrapper
|
||||
|
||||
|
||||
@unittest.skipIf(not WINDOWS, "WINDOWS only")
|
||||
@unittest.skipIf(PYPY, "pywin32 not available on PYPY")
|
||||
# https://github.com/giampaolo/psutil/pull/1762#issuecomment-632892692
|
||||
@@ -100,7 +89,7 @@ class TestCpuAPIs(WindowsTestCase):
|
||||
proc = w.Win32_Processor()[0]
|
||||
self.assertEqual(psutil.cpu_count(), proc.NumberOfLogicalProcessors)
|
||||
|
||||
def test_cpu_count_phys_vs_wmi(self):
|
||||
def test_cpu_count_cores_vs_wmi(self):
|
||||
w = wmi.WMI()
|
||||
proc = w.Win32_Processor()[0]
|
||||
self.assertEqual(psutil.cpu_count(logical=False), proc.NumberOfCores)
|
||||
@@ -633,7 +622,6 @@ class TestDualProcessImplementation(PsutilTestCase):
|
||||
assert fun.called
|
||||
|
||||
def test_cmdline(self):
|
||||
from psutil._pswindows import convert_oserror
|
||||
for pid in psutil.pids():
|
||||
try:
|
||||
a = cext.proc_cmdline(pid, use_peb=True)
|
||||
@@ -725,7 +713,7 @@ class RemoteProcessTestCase(PsutilTestCase):
|
||||
p = psutil.Process(self.proc32.pid)
|
||||
e = p.environ()
|
||||
self.assertIn("THINK_OF_A_NUMBER", e)
|
||||
self.assertEquals(e["THINK_OF_A_NUMBER"], str(os.getpid()))
|
||||
self.assertEqual(e["THINK_OF_A_NUMBER"], str(os.getpid()))
|
||||
|
||||
def test_environ_64(self):
|
||||
p = psutil.Process(self.proc64.pid)
|
||||
|
||||
Reference in New Issue
Block a user