def cleanup(): for name in os.listdir(u('.')): if name.startswith(u(TESTFILE_PREFIX)): try: safe_rmpath(name) except UnicodeEncodeError as exc: warn(exc) for path in _testfiles_created: safe_rmpath(path)
def test_proc_cmdline_mocked(self): # see: https://github.com/giampaolo/psutil/issues/639 p = psutil.Process() fake_file = io.StringIO(u('foo\x00bar\x00')) with mock.patch('psutil._pslinux.open', return_value=fake_file, create=True) as m: p.cmdline() == ['foo', 'bar'] assert m.called fake_file = io.StringIO(u('foo\x00bar\x00\x00')) with mock.patch('psutil._pslinux.open', return_value=fake_file, create=True) as m: p.cmdline() == ['foo', 'bar', ''] assert m.called
def test_cmdline_mocked(self): # see: https://github.com/giampaolo/psutil/issues/639 p = psutil.Process() fake_file = io.StringIO(u('foo\x00bar\x00')) with mock.patch('psutil._pslinux.open', return_value=fake_file, create=True) as m: p.cmdline() == ['foo', 'bar'] assert m.called fake_file = io.StringIO(u('foo\x00bar\x00\x00')) with mock.patch('psutil._pslinux.open', return_value=fake_file, create=True) as m: p.cmdline() == ['foo', 'bar', ''] assert m.called
def _cleanup_files(): DEVNULL.close() for name in os.listdir(u('.')): if name.startswith(u(TESTFILE_PREFIX)): try: safe_rmpath(name) except Exception: traceback.print_exc() for path in _testfiles_created: try: safe_rmpath(path) except Exception: traceback.print_exc()
def expect_exact_path_match(cls): # Do not expect psutil to correctly handle unicode paths on # Python 2 if os.listdir() is not able either. here = '.' if isinstance(cls.funky_name, str) else u('.') with warnings.catch_warnings(): warnings.simplefilter("ignore") return cls.funky_name in os.listdir(here)
def cleanup_test_files(): DEVNULL.close() for name in os.listdir(u('.')): if isinstance(name, unicode): prefix = u(TESTFILE_PREFIX) else: prefix = TESTFILE_PREFIX if name.startswith(prefix): try: safe_rmpath(name) except Exception: traceback.print_exc() for path in _testfiles_created: try: safe_rmpath(path) except Exception: traceback.print_exc()
def _cleanup_files(): DEVNULL.close() for name in os.listdir(u('.')): if isinstance(name, unicode): prefix = u(TESTFILE_PREFIX) else: prefix = TESTFILE_PREFIX if name.startswith(prefix): try: safe_rmpath(name) except Exception: traceback.print_exc() for path in _testfiles_created: try: safe_rmpath(path) except Exception: traceback.print_exc()
def expect_exact_path_match(cls): # Do not expect psutil to correctly handle unicode paths on # Python 2 if os.listdir() is not able either. if PY3: return True else: here = '.' if isinstance(cls.funky_name, str) else u('.') with warnings.catch_warnings(): warnings.simplefilter("ignore") return cls.funky_name in os.listdir(here)
def cleanup_test_files(): logstderr("executing cleanup_test_files() atexit function") DEVNULL.close() for name in os.listdir(u('.')): if isinstance(name, unicode): prefix = u(TESTFILE_PREFIX) else: prefix = TESTFILE_PREFIX if name.startswith(prefix): logstderr("removing temporary test file %r" % name) try: safe_rmpath(name) except Exception: traceback.print_exc() for path in _testfiles_created: logstderr("removing temporary test file %r" % path) try: safe_rmpath(path) except Exception: traceback.print_exc()
def open_mock(name, *args, **kwargs): if name == '/proc/partitions': return io.StringIO(textwrap.dedent(u"""\ major minor #blocks name 8 0 488386584 hda """)) elif name == '/proc/diskstats': return io.StringIO( u(" 3 1 hda 1 2 3 4")) else: return orig_open(name, *args, **kwargs)
def open_mock(name, *args, **kwargs): if name == '/proc/partitions': return io.StringIO(textwrap.dedent(u"""\ major minor #blocks name 8 0 488386584 hda """)) elif name == '/proc/diskstats': return io.StringIO( u(" 3 1 hda 1 2 3 4")) else: return orig_open(name, *args, **kwargs) return orig_open(name, *args)
def test_disk_partitions_mocked(self): # Test that ZFS partitions are returned. with open("/proc/filesystems", "r") as f: data = f.read() if 'zfs' in data: for part in psutil.disk_partitions(): if part.fstype == 'zfs': break else: self.fail("couldn't find any ZFS partition") else: # No ZFS partitions on this system. Let's fake one. fake_file = io.StringIO(u("nodev\tzfs\n")) with mock.patch('psutil._pslinux.open', return_value=fake_file, create=True) as m1: with mock.patch( 'psutil._pslinux.cext.disk_partitions', return_value=[('/dev/sdb3', '/', 'zfs', 'rw')]) as m2: ret = psutil.disk_partitions() assert m1.called assert m2.called assert ret self.assertEqual(ret[0].fstype, 'zfs')
# the timeout used in functions which have to wait GLOBAL_TIMEOUT = 3 # test output verbosity VERBOSITY = 1 if os.getenv('SILENT') or TOX else 2 # be more tolerant if we're on travis / appveyor in order to avoid # false positives if TRAVIS or APPVEYOR: NO_RETRIES *= 3 GLOBAL_TIMEOUT *= 3 # --- files TESTFILE_PREFIX = '$testfn' TESTFN = os.path.join(os.path.realpath(os.getcwd()), TESTFILE_PREFIX) _TESTFN = TESTFN + '-internal' TESTFN_UNICODE = TESTFN + u("-ƒőő") ASCII_FS = sys.getfilesystemencoding().lower() in ('ascii', 'us-ascii') # --- paths ROOT_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..')) SCRIPTS_DIR = os.path.join(ROOT_DIR, 'scripts') HERE = os.path.abspath(os.path.dirname(__file__)) # --- support HAS_CPU_AFFINITY = hasattr(psutil.Process, "cpu_affinity") HAS_CPU_FREQ = hasattr(psutil, "cpu_freq") HAS_CONNECTIONS_UNIX = POSIX and not SUNOS HAS_ENVIRON = hasattr(psutil.Process, "environ") HAS_PROC_IO_COUNTERS = hasattr(psutil.Process, "io_counters")
GLOBAL_TIMEOUT = 5 # be more tolerant if we're on travis / appveyor in order to avoid # false positives if TRAVIS or APPVEYOR: NO_RETRIES *= 3 GLOBAL_TIMEOUT *= 3 # --- file names # Disambiguate TESTFN for parallel testing. if os.name == 'java': # Jython disallows @ in module names TESTFN_PREFIX = '$psutil-%s-' % os.getpid() else: TESTFN_PREFIX = '@psutil-%s-' % os.getpid() UNICODE_SUFFIX = u("-ƒőő") # An invalid unicode string. if PY3: INVALID_UNICODE_SUFFIX = b"f\xc0\x80".decode('utf8', 'surrogateescape') else: INVALID_UNICODE_SUFFIX = "f\xc0\x80" ASCII_FS = sys.getfilesystemencoding().lower() in ('ascii', 'us-ascii') # --- paths ROOT_DIR = os.path.realpath( os.path.join(os.path.dirname(__file__), '..', '..')) SCRIPTS_DIR = os.path.join(ROOT_DIR, 'scripts') HERE = os.path.realpath(os.path.dirname(__file__))
def open_mock(name, *args, **kwargs): if name == ('/proc/partitions'): return orig_open(name, *args, **kwargs) else: return io.StringIO(u("8 1 sda1 2 2 2 2\n")) return orig_open(name, *args)