Пример #1
0
    def test_threads_mocked(self):
        # Test the case where os.listdir() returns a file (thread)
        # which no longer exists by the time we open() it (race
        # condition). threads() is supposed to ignore that instead
        # of raising NSP.
        def open_mock(name, *args, **kwargs):
            if name.startswith('/proc/%s/task' % os.getpid()):
                raise IOError(errno.ENOENT, "")
            else:
                return orig_open(name, *args, **kwargs)

        orig_open = open
        patch_point = 'builtins.open' if PY3 else '__builtin__.open'
        with mock.patch(patch_point, side_effect=open_mock) as m:
            ret = psutil.Process().threads()
            assert m.called
            self.assertEqual(ret, [])

        # ...but if it bumps into something != ENOENT we want an
        # exception.
        def open_mock(name, *args, **kwargs):
            if name.startswith('/proc/%s/task' % os.getpid()):
                raise IOError(errno.EPERM, "")
            else:
                return orig_open(name, *args, **kwargs)

        with mock.patch(patch_point, side_effect=open_mock):
            self.assertRaises(psutil.AccessDenied, psutil.Process().threads)
Пример #2
0
    def test_supports_ipv6(self):
        self.addCleanup(supports_ipv6.cache_clear)
        if supports_ipv6():
            with mock.patch('psutil._common.socket') as s:
                s.has_ipv6 = False
                supports_ipv6.cache_clear()
                assert not supports_ipv6()

            supports_ipv6.cache_clear()
            with mock.patch('psutil._common.socket.socket',
                            side_effect=socket.error) as s:
                assert not supports_ipv6()
                assert s.called

            supports_ipv6.cache_clear()
            with mock.patch('psutil._common.socket.socket',
                            side_effect=socket.gaierror) as s:
                assert not supports_ipv6()
                supports_ipv6.cache_clear()
                assert s.called

            supports_ipv6.cache_clear()
            with mock.patch('psutil._common.socket.socket.bind',
                            side_effect=socket.gaierror) as s:
                assert not supports_ipv6()
                supports_ipv6.cache_clear()
                assert s.called
        else:
            with self.assertRaises(Exception):
                sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
                sock.bind(("::1", 0))
Пример #3
0
    def test_win_service_get(self):
        name = next(psutil.win_service_iter()).name()

        with self.assertRaises(psutil.NoSuchProcess) as cm:
            psutil.win_service_get(name + '???')
        self.assertEqual(cm.exception.name, name + '???')

        # test NoSuchProcess
        service = psutil.win_service_get(name)
        exc = WindowsError(
            psutil._psplatform.cext.ERROR_SERVICE_DOES_NOT_EXIST, "")
        with mock.patch("psutil._psplatform.cext.winservice_query_status",
                        side_effect=exc):
            self.assertRaises(psutil.NoSuchProcess, service.status)
        with mock.patch("psutil._psplatform.cext.winservice_query_config",
                        side_effect=exc):
            self.assertRaises(psutil.NoSuchProcess, service.username)

        # test AccessDenied
        exc = WindowsError(
            psutil._psplatform.cext.ERROR_ACCESS_DENIED, "")
        with mock.patch("psutil._psplatform.cext.winservice_query_status",
                        side_effect=exc):
            self.assertRaises(psutil.AccessDenied, service.status)
        with mock.patch("psutil._psplatform.cext.winservice_query_config",
                        side_effect=exc):
            self.assertRaises(psutil.AccessDenied, service.username)

        # test __str__ and __repr__
        self.assertIn(service.name(), str(service))
        self.assertIn(service.display_name(), str(service))
        self.assertIn(service.name(), repr(service))
        self.assertIn(service.display_name(), repr(service))
 def test_boot_time_fluctuation(self):
     # https://github.com/giampaolo/psutil/issues/1007
     with mock.patch('psutil._pswindows.cext.boot_time', return_value=5):
         self.assertEqual(psutil.boot_time(), 5)
     with mock.patch('psutil._pswindows.cext.boot_time', return_value=4):
         self.assertEqual(psutil.boot_time(), 5)
     with mock.patch('psutil._pswindows.cext.boot_time', return_value=6):
         self.assertEqual(psutil.boot_time(), 5)
     with mock.patch('psutil._pswindows.cext.boot_time', return_value=333):
         self.assertEqual(psutil.boot_time(), 333)
Пример #5
0
 def test_name_long_cmdline_nsp_exc(self):
     # Same as above but emulates a case where cmdline() raises NSP
     # which is supposed to propagate.
     name = "long-program-name"
     with mock.patch("psutil._psplatform.Process.name",
                     return_value=name):
         with mock.patch("psutil._psplatform.Process.cmdline",
                         side_effect=psutil.NoSuchProcess(0, "")):
             p = psutil.Process()
             self.assertRaises(psutil.NoSuchProcess, p.name)
Пример #6
0
 def test_cpu_count_none(self):
     # https://github.com/giampaolo/psutil/issues/1085
     for val in (-1, 0, None):
         with mock.patch('psutil._psplatform.cpu_count_logical',
                         return_value=val) as m:
             self.assertIsNone(psutil.cpu_count())
             assert m.called
         with mock.patch('psutil._psplatform.cpu_count_physical',
                         return_value=val) as m:
             self.assertIsNone(psutil.cpu_count(logical=False))
             assert m.called
Пример #7
0
 def test_name_long_cmdline_ad_exc(self):
     # Same as above but emulates a case where cmdline() raises
     # AccessDenied in which case psutil is supposed to return
     # the truncated name instead of crashing.
     name = "long-program-name"
     with mock.patch("psutil._psplatform.Process.name",
                     return_value=name):
         with mock.patch("psutil._psplatform.Process.cmdline",
                         side_effect=psutil.AccessDenied(0, "")):
             p = psutil.Process()
             self.assertEqual(p.name(), "long-program-name")
Пример #8
0
 def test_name_long(self):
     # On UNIX the kernel truncates the name to the first 15
     # characters. In such a case psutil tries to determine the
     # full name from the cmdline.
     name = "long-program-name"
     cmdline = ["long-program-name-extended", "foo", "bar"]
     with mock.patch("psutil._psplatform.Process.name",
                     return_value=name):
         with mock.patch("psutil._psplatform.Process.cmdline",
                         return_value=cmdline):
             p = psutil.Process()
             self.assertEqual(p.name(), "long-program-name-extended")
Пример #9
0
 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
Пример #10
0
    def test_process_iter(self):
        self.assertIn(os.getpid(), [x.pid for x in psutil.process_iter()])
        sproc = get_test_subprocess()
        self.assertIn(sproc.pid, [x.pid for x in psutil.process_iter()])
        p = psutil.Process(sproc.pid)
        p.kill()
        p.wait()
        self.assertNotIn(sproc.pid, [x.pid for x in psutil.process_iter()])

        with mock.patch("psutil.Process", side_effect=psutil.NoSuchProcess(os.getpid())):
            self.assertEqual(list(psutil.process_iter()), [])
        with mock.patch("psutil.Process", side_effect=psutil.AccessDenied(os.getpid())):
            with self.assertRaises(psutil.AccessDenied):
                list(psutil.process_iter())
Пример #11
0
 def test_net_if_stats_enodev(self):
     # See: https://github.com/giampaolo/psutil/issues/1279
     with mock.patch('psutil._psutil_posix.net_if_mtu',
                     side_effect=OSError(errno.ENODEV, "")) as m:
         ret = psutil.net_if_stats()
         self.assertEqual(ret, {})
         assert m.called
Пример #12
0
 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())
Пример #13
0
 def test_os_waitpid_let_raise(self):
     # os.waitpid() is supposed to catch EINTR and ECHILD only.
     # Test that any other errno results in an exception.
     with mock.patch("psutil._psposix.os.waitpid",
                     side_effect=OSError(errno.EBADF, "")) as m:
         self.assertRaises(OSError, psutil._psposix.wait_pid, os.getpid())
         assert m.called
Пример #14
0
    def test_exe_mocked(self):
        with mock.patch('psutil._pslinux.os.readlink',
                        side_effect=OSError(errno.ENOENT, "")) as m:
            # No such file error; might be raised also if /proc/pid/exe
            # path actually exists for system processes with low pids
            # (about 0-20). In this case psutil is supposed to return
            # an empty string.
            ret = psutil.Process().exe()
            assert m.called
            self.assertEqual(ret, "")

            # ...but if /proc/pid no longer exist we're supposed to treat
            # it as an alias for zombie process
            with mock.patch('psutil._pslinux.os.path.lexists',
                            return_value=False):
                self.assertRaises(psutil.ZombieProcess, psutil.Process().exe)
Пример #15
0
 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))
     with mock.patch('psutil._common.os.stat',
                     side_effect=OSError(errno.EPERM, "foo")):
         self.assertRaises(OSError, isfile_strict, this_file)
     with mock.patch('psutil._common.os.stat',
                     side_effect=OSError(errno.EACCES, "foo")):
         self.assertRaises(OSError, isfile_strict, this_file)
     with mock.patch('psutil._common.os.stat',
                     side_effect=OSError(errno.EINVAL, "foo")):
         assert not isfile_strict(this_file)
     with mock.patch('psutil._common.stat.S_ISREG', return_value=False):
         assert not isfile_strict(this_file)
Пример #16
0
 def test_os_waitpid_bad_ret_status(self):
     # Simulate os.waitpid() returning a bad status.
     with mock.patch("psutil._psposix.os.waitpid",
                     return_value=(1, -1)) as m:
         self.assertRaises(ValueError,
                           psutil._psposix.wait_pid, os.getpid())
         assert m.called
Пример #17
0
    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')
Пример #18
0
    def test_disk_io_counters_kernel_2_6_limited_mocked(self):
        # Tests /proc/diskstats parsing format for 2.6 kernels,
        # where one line of /proc/partitions return a limited
        # amount of metrics when it bumps into a partition
        # (instead of a disk). See:
        # https://github.com/giampaolo/psutil/issues/767
        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)

        orig_open = open
        patch_point = 'builtins.open' if PY3 else '__builtin__.open'
        with mock.patch(patch_point, side_effect=open_mock) as m:
            ret = psutil.disk_io_counters()
            assert m.called
            self.assertEqual(ret.read_count, 1)
            self.assertEqual(ret.read_bytes, 2 * SECTOR_SIZE)
            self.assertEqual(ret.write_count, 3)
            self.assertEqual(ret.write_bytes, 4 * SECTOR_SIZE)

            self.assertEqual(ret.read_merged_count, 0)
            self.assertEqual(ret.read_time, 0)
            self.assertEqual(ret.write_merged_count, 0)
            self.assertEqual(ret.write_time, 0)
            self.assertEqual(ret.busy_time, 0)
Пример #19
0
    def test_disk_io_counters_kernel_2_6_full_mocked(self):
        # Tests /proc/diskstats parsing format for 2.6 kernels,
        # lines reporting all metrics:
        # https://github.com/giampaolo/psutil/issues/767
        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    0   hda 1 2 3 4 5 6 7 8 9 10 11"))
            else:
                return orig_open(name, *args, **kwargs)
            return orig_open(name, *args)

        orig_open = open
        patch_point = 'builtins.open' if PY3 else '__builtin__.open'
        with mock.patch(patch_point, side_effect=open_mock) as m:
            ret = psutil.disk_io_counters()
            assert m.called
            self.assertEqual(ret.read_count, 1)
            self.assertEqual(ret.read_merged_count, 2)
            self.assertEqual(ret.read_bytes, 3 * SECTOR_SIZE)
            self.assertEqual(ret.read_time, 4)
            self.assertEqual(ret.write_count, 5)
            self.assertEqual(ret.write_merged_count, 6)
            self.assertEqual(ret.write_bytes, 7 * SECTOR_SIZE)
            self.assertEqual(ret.write_time, 8)
            self.assertEqual(ret.busy_time, 10)
Пример #20
0
 def test_num_handles(self):
     num_handles = psutil.Process(self.pid).num_handles()
     with mock.patch("psutil._psplatform.cext.proc_num_handles",
                     side_effect=OSError(errno.EPERM, "msg")) as fun:
         self.assertEqual(psutil.Process(self.pid).num_handles(),
                          num_handles)
         assert fun.called
Пример #21
0
 def test_username_no_resolution(self):
     # Emulate a case where the system can't resolve the uid to
     # a username in which case psutil is supposed to return
     # the stringified uid.
     p = psutil.Process()
     with mock.patch("psutil.pwd.getpwuid", side_effect=KeyError) as fun:
         self.assertEqual(p.username(), str(p.uids().real))
         assert fun.called
Пример #22
0
 def test_pid_exists_let_raise(self):
     # According to "man 2 kill" possible error values for kill
     # are (EINVAL, EPERM, ESRCH). Test that any other errno
     # results in an exception.
     with mock.patch("psutil._psposix.os.kill",
                     side_effect=OSError(errno.EBADF, "")) as m:
         self.assertRaises(OSError, psutil._psposix.pid_exists, os.getpid())
         assert m.called
Пример #23
0
 def test_disk_io_counters_no_disks(self):
     # Emulate a case where no disks are installed, see:
     # https://github.com/giampaolo/psutil/issues/1062
     with mock.patch('psutil._psplatform.disk_io_counters',
                     return_value={}) as m:
         self.assertIsNone(psutil.disk_io_counters(perdisk=False))
         self.assertEqual(psutil.disk_io_counters(perdisk=True), {})
         assert m.called
Пример #24
0
 def test_os_waitpid_eintr(self):
     # os.waitpid() is supposed to "retry" on EINTR.
     with mock.patch("psutil._psposix.os.waitpid",
                     side_effect=OSError(errno.EINTR, "")) as m:
         self.assertRaises(
             psutil._psposix.TimeoutExpired,
             psutil._psposix.wait_pid, os.getpid(), timeout=0.01)
         assert m.called
Пример #25
0
 def test_name_type(self):
     # On Windows name() is determined from exe() first, because
     # it's faster; we want to overcome the internal optimization
     # and test name() instead of exe().
     with mock.patch("psutil._psplatform.cext.proc_exe",
                     side_effect=psutil.AccessDenied(os.getpid())) as m:
         self.assertIsInstance(psutil.Process().name(), str)
         assert m.called
Пример #26
0
 def test_sys_per_cpu_times_percent_negative(self):
     # see: https://github.com/giampaolo/psutil/issues/645
     psutil.cpu_times_percent(percpu=True)
     zero_times = [x._make([0 for x in range(len(x._fields))]) for x in psutil.cpu_times(percpu=True)]
     with mock.patch("psutil.cpu_times", return_value=zero_times):
         for cpu in psutil.cpu_times_percent(percpu=True):
             for percent in cpu:
                 self._test_cpu_percent(percent, None, None)
Пример #27
0
 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)
Пример #28
0
 def test_io_counters(self):
     io_counters_1 = psutil.Process(self.pid).io_counters()
     with mock.patch("psutil._psplatform.cext.proc_io_counters",
                     side_effect=OSError(errno.EPERM, "msg")) as fun:
         io_counters_2 = psutil.Process(self.pid).io_counters()
         for i in range(len(io_counters_1)):
             self.assertAlmostEqual(
                 io_counters_1[i], io_counters_2[i], delta=5)
         assert fun.called
Пример #29
0
 def test_cpu_times(self):
     cpu_times_1 = psutil.Process(self.pid).cpu_times()
     with mock.patch("psutil._psplatform.cext.proc_cpu_times",
                     side_effect=OSError(errno.EPERM, "msg")) as fun:
         cpu_times_2 = psutil.Process(self.pid).cpu_times()
         assert fun.called
         self.assertAlmostEqual(
             cpu_times_1.user, cpu_times_2.user, delta=0.01)
         self.assertAlmostEqual(
             cpu_times_1.system, cpu_times_2.system, delta=0.01)
Пример #30
0
 def test_prcess_iter_w_params(self):
     for p in psutil.process_iter(attrs=['pid']):
         self.assertEqual(list(p.info.keys()), ['pid'])
     with self.assertRaises(ValueError):
         list(psutil.process_iter(attrs=['foo']))
     with mock.patch("psutil._psplatform.Process.cpu_times",
                     side_effect=psutil.AccessDenied(0, "")) as m:
         for p in psutil.process_iter(attrs=["pid", "cpu_times"]):
             self.assertIsNone(p.info['cpu_times'])
             self.assertGreaterEqual(p.info['pid'], 0)
         assert m.called
     with mock.patch("psutil._psplatform.Process.cpu_times",
                     side_effect=psutil.AccessDenied(0, "")) as m:
         flag = object()
         for p in psutil.process_iter(
                 attrs=["pid", "cpu_times"], ad_value=flag):
             self.assertIs(p.info['cpu_times'], flag)
             self.assertGreaterEqual(p.info['pid'], 0)
         assert m.called
Пример #31
0
 def test_memory_info(self):
     mem = psutil.Process().memory_info()
     with mock.patch("psutil._psplatform.cext.proc_memory_info",
                     side_effect=OSError(errno.EPERM, "msg")) as fun:
         psutil.Process().memory_info() == mem
         assert fun.called
Пример #32
0
 def test_create_time(self):
     ctime = psutil.Process(self.pid).create_time()
     with mock.patch("psutil._psplatform.cext.proc_times",
                     side_effect=OSError(errno.EPERM, "msg")) as fun:
         self.assertEqual(psutil.Process(self.pid).create_time(), ctime)
         assert fun.called
Пример #33
0
 def test_emulate_secs_left_unknown(self):
     with mock.patch("psutil._pswindows.cext.sensors_battery",
                     return_value=(0, 0, 0, -1)) as m:
         self.assertEqual(psutil.sensors_battery().secsleft,
                          psutil.POWER_TIME_UNKNOWN)
         assert m.called
Пример #34
0
 def test_cpu_times(self):
     cpu_times = psutil.Process().cpu_times()
     with mock.patch("psutil._psplatform.cext.proc_cpu_times",
                     side_effect=OSError(errno.EPERM, "msg")) as fun:
         psutil.Process().cpu_times() == cpu_times
         assert fun.called
Пример #35
0
 def test_create_time(self):
     ctime = psutil.Process().create_time()
     with mock.patch("psutil._psplatform.cext.proc_create_time",
                     side_effect=OSError(errno.EPERM, "msg")) as fun:
         psutil.Process().create_time() == ctime
         assert fun.called
Пример #36
0
 def test_emulate_power_charging(self):
     with mock.patch("psutil._pswindows.cext.sensors_battery",
                     return_value=(0, 8, 0, 0)) as m:
         self.assertEqual(psutil.sensors_battery().secsleft,
                          psutil.POWER_TIME_UNLIMITED)
         assert m.called
Пример #37
0
 def test_emulate_no_battery(self):
     with mock.patch("psutil._pswindows.cext.sensors_battery",
                     return_value=(0, 128, 0, 0)) as m:
         self.assertIsNone(psutil.sensors_battery())
         assert m.called
Пример #38
0
 def test_num_handles(self):
     io_counters = psutil.Process().io_counters()
     with mock.patch("psutil._psplatform.cext.proc_io_counters",
                     side_effect=OSError(errno.EPERM, "msg")) as fun:
         psutil.Process().io_counters() == io_counters
         assert fun.called
Пример #39
0
 def test_wait_for_pid(self):
     wait_for_pid(os.getpid())
     nopid = max(psutil.pids()) + 99999
     with mock.patch('psutil.tests.retry.__iter__', return_value=iter([0])):
         self.assertRaises(psutil.NoSuchProcess, wait_for_pid, nopid)
Пример #40
0
 def test_wait_for_file_no_file(self):
     with mock.patch('psutil.tests.retry.__iter__', return_value=iter([0])):
         self.assertRaises(IOError, wait_for_file, TESTFN)
Пример #41
0
 def test_name(self):
     name = psutil.Process(self.pid).name()
     with mock.patch("psutil._psplatform.cext.proc_exe",
                     side_effect=psutil.AccessDenied(os.getpid())) as fun:
         self.assertEqual(psutil.Process(self.pid).name(), name)
         assert fun.called