diff options
Diffstat (limited to 'psutil/tests/test_misc.py')
-rwxr-xr-x | psutil/tests/test_misc.py | 167 |
1 files changed, 101 insertions, 66 deletions
diff --git a/psutil/tests/test_misc.py b/psutil/tests/test_misc.py index d946eb62..aa30cbd6 100755 --- a/psutil/tests/test_misc.py +++ b/psutil/tests/test_misc.py @@ -23,12 +23,17 @@ import psutil.tests from psutil import LINUX from psutil import POSIX from psutil import WINDOWS +from psutil._common import bcat +from psutil._common import cat from psutil._common import debug +from psutil._common import isfile_strict from psutil._common import memoize from psutil._common import memoize_when_activated +from psutil._common import parse_environ_block from psutil._common import supports_ipv6 from psutil._common import wrap_numbers from psutil._compat import PY3 +from psutil._compat import FileNotFoundError from psutil._compat import redirect_stderr from psutil.tests import APPVEYOR from psutil.tests import CI_TESTING @@ -50,11 +55,11 @@ from psutil.tests import unittest # =================================================================== -# --- Misc / generic tests. +# --- Test classes' repr(), str(), ... # =================================================================== -class TestMisc(PsutilTestCase): +class TestSpecialMethods(PsutilTestCase): def test_process__repr__(self, func=repr): p = psutil.Process(self.spawn_testproc().pid) @@ -95,6 +100,12 @@ class TestMisc(PsutilTestCase): def test_process__str__(self): self.test_process__repr__(func=str) + def test_error__repr__(self): + self.assertEqual(repr(psutil.Error()), "psutil.Error()") + + def test_error__str__(self): + self.assertEqual(str(psutil.Error()), "") + def test_no_such_process__repr__(self): self.assertEqual( repr(psutil.NoSuchProcess(321)), @@ -173,6 +184,14 @@ class TestMisc(PsutilTestCase): s = set([psutil.Process(), psutil.Process()]) self.assertEqual(len(s), 1) + +# =================================================================== +# --- Misc, generic, corner cases +# =================================================================== + + +class TestMisc(PsutilTestCase): + def test__all__(self): dir_psutil = dir(psutil) for name in dir_psutil: @@ -189,7 +208,7 @@ class TestMisc(PsutilTestCase): continue if (fun.__doc__ is not None and 'deprecated' not in fun.__doc__.lower()): - self.fail('%r not in psutil.__all__' % name) + raise self.fail('%r not in psutil.__all__' % name) # Import 'star' will break if __all__ is inconsistent, see: # https://github.com/giampaolo/psutil/issues/656 @@ -208,6 +227,72 @@ class TestMisc(PsutilTestCase): p.foo = '1' self.assertNotIn('foo', p.as_dict()) + def test_serialization(self): + def check(ret): + if json is not None: + json.loads(json.dumps(ret)) + a = pickle.dumps(ret) + b = pickle.loads(a) + self.assertEqual(ret, b) + + check(psutil.Process().as_dict()) + check(psutil.virtual_memory()) + check(psutil.swap_memory()) + check(psutil.cpu_times()) + check(psutil.cpu_times_percent(interval=0)) + check(psutil.net_io_counters()) + if LINUX and not os.path.exists('/proc/diskstats'): + pass + else: + if not APPVEYOR: + check(psutil.disk_io_counters()) + check(psutil.disk_partitions()) + 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): + return self.skipTest("can't find setup.py") + module = import_module_by_path(setup_py) + self.assertRaises(SystemExit, module.setup) + self.assertEqual(module.get_version(), psutil.__version__) + + def test_ad_on_process_creation(self): + # We are supposed to be able to instantiate Process also in case + # of zombie processes or access denied. + with mock.patch.object(psutil.Process, 'create_time', + side_effect=psutil.AccessDenied) as meth: + psutil.Process() + assert meth.called + with mock.patch.object(psutil.Process, 'create_time', + side_effect=psutil.ZombieProcess(1)) as meth: + psutil.Process() + assert meth.called + with mock.patch.object(psutil.Process, 'create_time', + side_effect=ValueError) as meth: + with self.assertRaises(ValueError): + psutil.Process() + assert meth.called + + 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: + reload_module(psutil) + self.assertIn("version conflict", str(cm.exception).lower()) + + +# =================================================================== +# --- psutil/_common.py utils +# =================================================================== + + +class TestCommonModule(PsutilTestCase): + def test_memoize(self): @memoize def foo(*args, **kwargs): @@ -271,8 +356,6 @@ class TestMisc(PsutilTestCase): self.assertEqual(len(calls), 2) def test_parse_environ_block(self): - from psutil._common import parse_environ_block - def k(s): return s.upper() if WINDOWS else s @@ -327,7 +410,6 @@ class TestMisc(PsutilTestCase): sock.close() def test_isfile_strict(self): - from psutil._common import isfile_strict this_file = os.path.abspath(__file__) assert isfile_strict(this_file) assert not isfile_strict(os.path.dirname(this_file)) @@ -343,64 +425,6 @@ class TestMisc(PsutilTestCase): with mock.patch('psutil._common.stat.S_ISREG', return_value=False): assert not isfile_strict(this_file) - def test_serialization(self): - def check(ret): - if json is not None: - json.loads(json.dumps(ret)) - a = pickle.dumps(ret) - b = pickle.loads(a) - self.assertEqual(ret, b) - - check(psutil.Process().as_dict()) - check(psutil.virtual_memory()) - check(psutil.swap_memory()) - check(psutil.cpu_times()) - check(psutil.cpu_times_percent(interval=0)) - check(psutil.net_io_counters()) - if LINUX and not os.path.exists('/proc/diskstats'): - pass - else: - if not APPVEYOR: - check(psutil.disk_io_counters()) - check(psutil.disk_partitions()) - 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): - return self.skipTest("can't find setup.py") - module = import_module_by_path(setup_py) - self.assertRaises(SystemExit, module.setup) - self.assertEqual(module.get_version(), psutil.__version__) - - def test_ad_on_process_creation(self): - # We are supposed to be able to instantiate Process also in case - # of zombie processes or access denied. - with mock.patch.object(psutil.Process, 'create_time', - side_effect=psutil.AccessDenied) as meth: - psutil.Process() - assert meth.called - with mock.patch.object(psutil.Process, 'create_time', - side_effect=psutil.ZombieProcess(1)) as meth: - psutil.Process() - assert meth.called - with mock.patch.object(psutil.Process, 'create_time', - side_effect=ValueError) as meth: - with self.assertRaises(ValueError): - psutil.Process() - assert meth.called - - 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: - reload_module(psutil) - self.assertIn("version conflict", str(cm.exception).lower()) - def test_debug(self): if PY3: from io import StringIO @@ -430,6 +454,17 @@ class TestMisc(PsutilTestCase): self.assertIn("no such file", msg) self.assertIn("/foo", msg) + def test_cat_bcat(self): + testfn = self.get_testfn() + with open(testfn, "wt") as f: + f.write("foo") + self.assertEqual(cat(testfn), "foo") + self.assertEqual(bcat(testfn), b"foo") + self.assertRaises(FileNotFoundError, cat, testfn + '-invalid') + self.assertRaises(FileNotFoundError, bcat, testfn + '-invalid') + self.assertEqual(cat(testfn + '-invalid', fallback="bar"), "bar") + self.assertEqual(bcat(testfn + '-invalid', fallback="bar"), "bar") + # =================================================================== # --- Tests for wrap_numbers() function. @@ -717,8 +752,8 @@ class TestScripts(PsutilTestCase): if name.endswith('.py'): if 'test_' + os.path.splitext(name)[0] not in meths: # self.assert_stdout(name) - self.fail('no test defined for %r script' - % os.path.join(SCRIPTS_DIR, name)) + raise self.fail('no test defined for %r script' + % os.path.join(SCRIPTS_DIR, name)) @unittest.skipIf(not POSIX, "POSIX only") def test_executable(self): |