Skip to content

Commit

Permalink
#1041: add test for memory_maps() which loads a .so lib and makes sur…
Browse files Browse the repository at this point in the history
…e it gets listed
  • Loading branch information
giampaolo committed Apr 30, 2017
1 parent b37ddf7 commit a85dfa7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
22 changes: 21 additions & 1 deletion psutil/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
"""

from __future__ import print_function

import atexit
import contextlib
import ctypes
import errno
import functools
import os
Expand Down Expand Up @@ -74,6 +76,10 @@
'PYPY', 'PYTHON', 'ROOT_DIR', 'SCRIPTS_DIR', 'TESTFILE_PREFIX',
'TESTFN', 'TESTFN_UNICODE', 'TOX', 'TRAVIS', 'VALID_PROC_STATUSES',
'VERBOSITY',
"HAS_CPU_AFFINITY", "HAS_CPU_FREQ", "HAS_ENVIRON", "HAS_PROC_IO_COUNTERS",
"HAS_IONICE", "HAS_MEMORY_MAPS", "HAS_PROC_CPU_NUM", "HAS_RLIMIT",
"HAS_SENSORS_BATTERY", "HAS_BATTERY""HAS_SENSORS_FANS",
"HAS_SENSORS_TEMPERATURES",
# classes
'ThreadTask'
# test utils
Expand All @@ -92,8 +98,10 @@
'call_until', 'wait_for_pid', 'wait_for_file',
# network
'check_connection_ntuple', 'check_net_address',
'get_free_port', 'unix_socket_path', 'bind_socket', 'bind_unix_socket',
'tcp_socketpair', 'unix_socketpair',
# others
'warn',
'warn', 'copyload_shared_lib', 'is_namedtuple',
]


Expand Down Expand Up @@ -995,3 +1003,15 @@ def is_namedtuple(x):
if not isinstance(f, tuple):
return False
return all(type(n) == str for n in f)


def copyload_shared_lib(src, dst_prefix=TESTFILE_PREFIX):
"""Given an existing shared so / DLL library copies it in
another location and loads it via ctypes.
Return the new path.
"""
newpath = tempfile.mktemp(prefix=dst_prefix,
suffix=os.path.splitext(src)[1])
shutil.copyfile(src, newpath)
ctypes.CDLL(newpath)
return newpath
12 changes: 11 additions & 1 deletion psutil/tests/test_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
from psutil._compat import PY3
from psutil.tests import APPVEYOR
from psutil.tests import call_until
from psutil.tests import copyload_shared_lib
from psutil.tests import create_exe
from psutil.tests import create_proc_children_pair
from psutil.tests import enum
Expand All @@ -43,6 +44,7 @@
from psutil.tests import HAS_CPU_AFFINITY
from psutil.tests import HAS_ENVIRON
from psutil.tests import HAS_IONICE
from psutil.tests import HAS_MEMORY_MAPS
from psutil.tests import HAS_PROC_CPU_NUM
from psutil.tests import HAS_PROC_IO_COUNTERS
from psutil.tests import HAS_RLIMIT
Expand Down Expand Up @@ -611,7 +613,7 @@ def test_memory_full_info(self):
self.assertGreaterEqual(mem.pss, 0)
self.assertGreaterEqual(mem.swap, 0)

@unittest.skipIf(OPENBSD or NETBSD, "not supported")
@unittest.skipIf(not HAS_MEMORY_MAPS, "not supported")
def test_memory_maps(self):
p = psutil.Process()
maps = p.memory_maps()
Expand Down Expand Up @@ -652,6 +654,14 @@ def test_memory_maps(self):
self.assertIsInstance(value, (int, long))
assert value >= 0, value

@unittest.skipIf(not HAS_MEMORY_MAPS, "not supported")
def test_memory_maps_lists_lib(self):
p = psutil.Process()
path = [x.path for x in p.memory_maps() if x.path.endswith(".so")][0]
newpath = copyload_shared_lib(path)
self.addCleanup(safe_rmpath, newpath)
assert any([x.path for x in p.memory_maps() if x.path == newpath])

def test_memory_percent(self):
p = psutil.Process()
ret = p.memory_percent()
Expand Down

0 comments on commit a85dfa7

Please sign in to comment.