def test_sanity_version_check(self): # see: https://github.com/giampaolo/psutil/issues/564 with mock.patch( "psutil._psplatform.cext.version", return_value="0.0.0"): with self.assertRaises(ImportError) as cm: importlib.reload(psutil) self.assertIn("version conflict", str(cm.exception).lower())
def test_no_procfs_on_import(self, tb): 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, **kwargs): if name.startswith('/proc'): raise IOError(errno.ENOENT, 'rejecting access for test') return orig_open(name, *args, **kwargs) patch_point = 'builtins.open' if PY3 else '__builtin__.open' with mock.patch(patch_point, side_effect=open_mock): importlib.reload(psutil) assert tb.called 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')
def test_memory_percent_0_division(self): import collections try: retval = collections.namedtuple("mem", "total")(0) with mock.patch( "psutil._psplatform.virtual_memory", return_value=retval): self.assertRaises(ValueError, psutil.Process().memory_percent) finally: importlib.reload(psutil)
def test_sector_size_mock(self): # Test SECTOR_SIZE fallback in case 'hw_sector_size' file # does not exist. def open_mock(name, *args, **kwargs): if PY3 and isinstance(name, bytes): name = name.decode() if name.startswith("/sys/block/sda/queue/hw_sector_size"): flag.append(None) raise IOError(errno.ENOENT, '') else: return orig_open(name, *args, **kwargs) flag = [] orig_open = open patch_point = 'builtins.open' if PY3 else '__builtin__.open' try: with mock.patch(patch_point, side_effect=open_mock): importlib.reload(psutil._pslinux) importlib.reload(psutil) self.assertEqual(flag, [None]) self.assertEqual(psutil._pslinux.SECTOR_SIZE, 512) finally: importlib.reload(psutil._pslinux) importlib.reload(psutil)
def test_sector_size_mock(self): # Test SECTOR_SIZE fallback in case 'hw_sector_size' file # does not exist. def open_mock(name, *args, **kwargs): if PY3 and isinstance(name, bytes): name = name.decode() if name.startswith("/sys/block/sda/queue/hw_sector_size"): flag.append(None) raise IOError(errno.ENOENT, '') else: return orig_open(name, *args, **kwargs) flag = [] orig_open = open patch_point = 'builtins.open' if PY3 else '__builtin__.open' try: with mock.patch(patch_point, side_effect=open_mock): importlib.reload(psutil._pslinux) importlib.reload(psutil) self.assertEqual(flag, [None]) self.assertEqual(psutil._pslinux.SECTOR_SIZE, 512) finally: importlib.reload(psutil._pslinux) importlib.reload(psutil)
def test_psutil_is_reloadable(self): importlib.reload(psutil)
def test_psutil_is_reloadable(self): importlib.reload(psutil)