示例#1
0
 def test_shared(self):
     free_value = free_physmem().shared
     if free_value == 0:
         raise unittest.SkipTest("free does not support 'shared' column")
     psutil_value = psutil.virtual_memory().shared
     self.assertAlmostEqual(free_value,
                            psutil_value,
                            delta=MEMORY_TOLERANCE)
示例#2
0
 def test_cpu_count_logical(self):
     logical = psutil.cpu_count()
     self.assertIsNotNone(logical)
     self.assertEqual(logical, len(psutil.cpu_times(percpu=True)))
     self.assertGreaterEqual(logical, 1)
     #
     if os.path.exists("/proc/cpuinfo"):
         with open("/proc/cpuinfo") as fd:
             cpuinfo_data = fd.read()
         if "physical id" not in cpuinfo_data:
             raise unittest.SkipTest("cpuinfo doesn't include physical id")
示例#3
0
    def setUpClass(cls):
        other_python = cls.find_other_interpreter()

        if other_python is None:
            raise unittest.SkipTest(
                "could not find interpreter with opposite bitness")

        if IS_64_BIT:
            cls.python64 = sys.executable
            cls.python32 = other_python
        else:
            cls.python64 = other_python
            cls.python32 = sys.executable
示例#4
0
 def test_proc_connections(self):
     suffix = os.path.basename(self.funky_name)
     with unix_socket_path(suffix=suffix) as name:
         try:
             sock = bind_unix_socket(name)
         except UnicodeEncodeError:
             if PY3:
                 raise
             else:
                 raise unittest.SkipTest("not supported")
         with closing(sock):
             conn = psutil.Process().connections('unix')[0]
             self.assertEqual(conn.laddr, name)
示例#5
0
 def test_available(self):
     # "free" output format has changed at some point:
     # https://github.com/giampaolo/psutil/issues/538#issuecomment-147192098
     out = sh("free -b")
     lines = out.split('\n')
     if 'available' not in lines[0]:
         raise unittest.SkipTest("free does not support 'available' column")
     else:
         free_value = int(lines[1].split()[-1])
         psutil_value = psutil.virtual_memory().available
         self.assertAlmostEqual(
             free_value, psutil_value, delta=MEMORY_TOLERANCE,
             msg='%s %s \n%s' % (free_value, psutil_value, out))
示例#6
0
 def test_nic_names(self):
     p = subprocess.Popen("ifconfig -a", shell=1, stdout=subprocess.PIPE)
     output = p.communicate()[0].strip()
     if p.returncode != 0:
         raise unittest.SkipTest('ifconfig returned no output')
     if PY3:
         output = str(output, sys.stdout.encoding)
     for nic in psutil.net_io_counters(pernic=True).keys():
         for line in output.split():
             if line.startswith(nic):
                 break
         else:
             self.fail("couldn't find %s nic in 'ifconfig -a' output\n%s" %
                       (nic, output))
 def test_proc_connections(self):
     name = self.get_testfn(suffix=self.funky_suffix)
     try:
         sock = bind_unix_socket(name)
     except UnicodeEncodeError:
         if PY3:
             raise
         else:
             raise unittest.SkipTest("not supported")
     with closing(sock):
         conn = psutil.Process().connections('unix')[0]
         self.assertIsInstance(conn.laddr, str)
         # AF_UNIX addr not set on OpenBSD
         if not OPENBSD:  # XXX
             self.assertEqual(conn.laddr, name)
 def test_cpu_count(self):
     logical = psutil.cpu_count()
     self.assertEqual(logical, len(psutil.cpu_times(percpu=True)))
     self.assertGreaterEqual(logical, 1)
     #
     if os.path.exists("/proc/cpuinfo"):
         with open("/proc/cpuinfo") as fd:
             cpuinfo_data = fd.read()
         if "physical id" not in cpuinfo_data:
             raise unittest.SkipTest("cpuinfo doesn't include physical id")
     physical = psutil.cpu_count(logical=False)
     if WINDOWS and sys.getwindowsversion()[:2] <= (6, 1):  # <= Vista
         self.assertIsNone(physical)
     else:
         self.assertGreaterEqual(physical, 1)
         self.assertGreaterEqual(logical, physical)
示例#9
0
 def test_proc_connections(self):
     suffix = os.path.basename(self.funky_name)
     with unix_socket_path(suffix=suffix) as name:
         try:
             sock = bind_unix_socket(name)
         except UnicodeEncodeError:
             if PY3:
                 raise
             else:
                 raise unittest.SkipTest("not supported")
         with closing(sock):
             conn = psutil.Process().connections('unix')[0]
             self.assertIsInstance(conn.laddr, str)
             # AF_UNIX addr not set on OpenBSD
             if not OPENBSD and not CIRRUS:  # XXX
                 self.assertEqual(conn.laddr, name)
示例#10
0
 def test_memory_maps(self):
     p = psutil.Process()
     ext = ".so" if POSIX else ".dll"
     old = [
         x.path for x in p.memory_maps()
         if os.path.normcase(x.path).endswith(ext)
     ][0]
     try:
         new = os.path.normcase(
             copyload_shared_lib(old, dst_prefix=self.funky_name))
     except UnicodeEncodeError:
         if PY3:
             raise
         else:
             raise unittest.SkipTest("ctypes can't handle unicode")
     newpaths = [os.path.normcase(x.path) for x in p.memory_maps()]
     self.assertIn(new, newpaths)
示例#11
0
    def test_net_connections(self):
        def find_sock(cons):
            for conn in cons:
                if os.path.basename(conn.laddr).startswith(TESTFILE_PREFIX):
                    return conn
            raise ValueError("connection not found")

        suffix = os.path.basename(self.funky_name)
        with unix_socket_path(suffix=suffix) as name:
            try:
                sock = bind_unix_socket(name)
            except UnicodeEncodeError:
                if PY3:
                    raise
                else:
                    raise unittest.SkipTest("not supported")
            with closing(sock):
                cons = psutil.net_connections(kind='unix')
                conn = find_sock(cons)
                self.assertEqual(conn.laddr, name)
示例#12
0
    def setUp(self):
        super().setUp()

        other_python = self.find_other_interpreter()
        if other_python is None:
            raise unittest.SkipTest(
                "could not find interpreter with opposite bitness")
        if IS_64BIT:
            self.python64 = sys.executable
            self.python32 = other_python
        else:
            self.python64 = other_python
            self.python32 = sys.executable

        env = os.environ.copy()
        env["THINK_OF_A_NUMBER"] = str(os.getpid())
        self.proc32 = self.spawn_testproc([self.python32] + self.test_args,
                                          env=env,
                                          stdin=subprocess.PIPE)
        self.proc64 = self.spawn_testproc([self.python64] + self.test_args,
                                          env=env,
                                          stdin=subprocess.PIPE)
    def test_net_connections(self):
        def find_sock(cons):
            for conn in cons:
                if os.path.basename(conn.laddr).startswith(TESTFN_PREFIX):
                    return conn
            raise ValueError("connection not found")

        name = self.get_testfn(suffix=self.funky_suffix)
        try:
            sock = bind_unix_socket(name)
        except UnicodeEncodeError:
            if PY3:
                raise
            else:
                raise unittest.SkipTest("not supported")
        with closing(sock):
            cons = psutil.net_connections(kind='unix')
            # AF_UNIX addr not set on OpenBSD
            if not OPENBSD:
                conn = find_sock(cons)
                self.assertIsInstance(conn.laddr, str)
                self.assertEqual(conn.laddr, name)