diff options
author | Giampaolo Rodola <g.rodola@gmail.com> | 2015-12-18 20:20:40 +0100 |
---|---|---|
committer | Giampaolo Rodola <g.rodola@gmail.com> | 2015-12-18 20:20:40 +0100 |
commit | 0d541cb6d5c6c2d8d894a5aa59e4d7bef34f9c2b (patch) | |
tree | a65207d52f038415c658733202f3cb32f99d6079 | |
parent | 89869392bf95150feb75adeef89dd4e5959dee96 (diff) | |
parent | 450cb8d57d11fb5f5e9964d14490974f81f0a536 (diff) | |
download | psutil-0d541cb6d5c6c2d8d894a5aa59e4d7bef34f9c2b.tar.gz |
Merge pull request #719 from sethp-jive/tests/add-import-tests
Add import-time tests for linux psutil
-rw-r--r-- | psutil/__init__.py | 1 | ||||
-rw-r--r-- | test/_linux.py | 62 | ||||
-rw-r--r-- | test/test_psutil.py | 13 |
3 files changed, 73 insertions, 3 deletions
diff --git a/psutil/__init__.py b/psutil/__init__.py index 7c8731e8..727b1b64 100644 --- a/psutil/__init__.py +++ b/psutil/__init__.py @@ -109,7 +109,6 @@ if sys.platform.startswith("linux"): RLIMIT_SIGPENDING = _psutil_linux.RLIMIT_SIGPENDING except AttributeError: pass - del _psutil_linux elif sys.platform.startswith("win32"): from . import _pswindows as _psplatform diff --git a/test/_linux.py b/test/_linux.py index 5e7d10a7..d2125cec 100644 --- a/test/_linux.py +++ b/test/_linux.py @@ -15,6 +15,7 @@ import io import os import pprint import re +import shutil import socket import struct import sys @@ -34,6 +35,7 @@ from psutil._compat import u from test_psutil import call_until from test_psutil import get_kernel_version from test_psutil import get_test_subprocess +from test_psutil import importlib from test_psutil import LINUX from test_psutil import MEMORY_TOLERANCE from test_psutil import POSIX @@ -44,7 +46,6 @@ from test_psutil import TRAVIS from test_psutil import unittest from test_psutil import which - HERE = os.path.abspath(os.path.dirname(__file__)) # procps-ng 3.3.10 changed the output format of free # and removed the 'buffers/cache line' @@ -472,6 +473,65 @@ class LinuxSpecificTestCase(unittest.TestCase): psutil.PROCFS_PATH = "/proc" os.rmdir(tdir) + def test_no_procfs_for_import(self): + my_procfs = tempfile.mkdtemp() + + with open(os.path.join(my_procfs, 'stat'), 'w') as f: + f.write('cpu 0 0 0 0 0 0 0 0 0 0\n') + f.write('cpu0 0 0 0 0 0 0 0 0 0 0\n') + f.write('cpu1 0 0 0 0 0 0 0 0 0 0\n') + + try: + orig_open = open + + def open_mock(name, *args): + if name.startswith('/proc'): + raise IOError(errno.ENOENT, 'rejecting access for test') + return orig_open(name, *args) + patch_point = 'builtins.open' if PY3 else '__builtin__.open' + with mock.patch(patch_point, side_effect=open_mock): + importlib.reload(psutil) + + self.assertRaises(IOError, psutil.cpu_times) + self.assertRaises(IOError, psutil.cpu_times, percpu=True) + self.assertRaises(IOError, psutil.cpu_percent) + self.assertRaises(IOError, psutil.cpu_percent, percpu=True) + self.assertRaises(IOError, psutil.cpu_times_percent) + self.assertRaises( + IOError, psutil.cpu_times_percent, percpu=True) + + psutil.PROCFS_PATH = my_procfs + + self.assertEqual(psutil.cpu_percent(), 0) + self.assertEqual(sum(psutil.cpu_times_percent()), 0) + + # since we don't know the number of CPUs at import time, + # we awkwardly say there are none until the second call + per_cpu_percent = psutil.cpu_percent(percpu=True) + self.assertEqual(sum(per_cpu_percent), 0) + + # ditto awkward length + per_cpu_times_percent = psutil.cpu_times_percent(percpu=True) + self.assertEqual(sum(map(sum, per_cpu_times_percent)), 0) + + # much user, very busy + with open(os.path.join(my_procfs, 'stat'), 'w') as f: + f.write('cpu 1 0 0 0 0 0 0 0 0 0\n') + f.write('cpu0 1 0 0 0 0 0 0 0 0 0\n') + f.write('cpu1 1 0 0 0 0 0 0 0 0 0\n') + + self.assertNotEqual(psutil.cpu_percent(), 0) + self.assertNotEqual( + sum(psutil.cpu_percent(percpu=True)), 0) + self.assertNotEqual(sum(psutil.cpu_times_percent()), 0) + self.assertNotEqual( + sum(map(sum, psutil.cpu_times_percent(percpu=True))), 0) + finally: + shutil.rmtree(my_procfs) + importlib.reload(psutil) + + self.assertEqual(psutil.PROCFS_PATH, '/proc') + @unittest.skipUnless( get_kernel_version() >= (2, 6, 36), "prlimit() not available on this Linux kernel version") diff --git a/test/test_psutil.py b/test/test_psutil.py index 2a0c5a2f..d8c910ab 100644 --- a/test/test_psutil.py +++ b/test/test_psutil.py @@ -22,8 +22,8 @@ import contextlib import datetime import errno import functools -import imp import json +import imp import os import pickle import pprint @@ -71,6 +71,14 @@ if sys.version_info >= (3, 4): else: enum = None +if PY3: + import importlib + # python <=3.3 + if not hasattr(importlib, 'reload'): + import imp as importlib +else: + import imp as importlib + # =================================================================== # --- Constants @@ -3200,6 +3208,9 @@ class TestUnicode(unittest.TestCase): self.assertIsInstance(path, str) self.assertEqual(os.path.normcase(path), os.path.normcase(self.uexe)) + def test_psutil_is_reloadable(self): + importlib.reload(psutil) + def main(): tests = [] |