summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSeth Pellegrino <seth.pellegrino@jivesoftware.com>2015-12-03 11:57:06 -0800
committerSeth Pellegrino <seth.pellegrino@jivesoftware.com>2015-12-17 11:00:26 -0800
commit450cb8d57d11fb5f5e9964d14490974f81f0a536 (patch)
tree7f73b6686cb30555ef11128bc1d76d51ccdaf065
parente40efe13102390aa769b0de65877da57c0b3429d (diff)
downloadpsutil-450cb8d57d11fb5f5e9964d14490974f81f0a536.tar.gz
Add import-time tests for psutil
Two new tests that validate: 1) that psutil can be sucessfuly reload()'d 2) that a missing procfs doesn't prevent importing, or correct functionality on linux.
-rw-r--r--psutil/__init__.py1
-rw-r--r--test/_linux.py62
-rw-r--r--test/test_psutil.py13
3 files changed, 73 insertions, 3 deletions
diff --git a/psutil/__init__.py b/psutil/__init__.py
index 90ce5baf..32eab7b1 100644
--- a/psutil/__init__.py
+++ b/psutil/__init__.py
@@ -108,7 +108,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 d2a85713..0435256b 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 = []