summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2016-09-21 12:08:23 +0200
committerGiampaolo Rodola <g.rodola@gmail.com>2016-09-21 12:08:23 +0200
commite5e84c9dc72cf498ef748a52072fd75c9bd27d05 (patch)
tree1fe60315e0e4a0bbb0e18a9227bda458366ee153
parente51dde4ae3ab11b383c41b4bb03d9e37413be890 (diff)
downloadpsutil-e5e84c9dc72cf498ef748a52072fd75c9bd27d05.tar.gz
refactoring
-rw-r--r--psutil/_pslinux.py2
-rw-r--r--psutil/tests/test_linux.py79
2 files changed, 43 insertions, 38 deletions
diff --git a/psutil/_pslinux.py b/psutil/_pslinux.py
index 906b2e76..4ed20dea 100644
--- a/psutil/_pslinux.py
+++ b/psutil/_pslinux.py
@@ -295,7 +295,7 @@ def calculate_avail_vmem(mems):
https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/
commit/?id=34e431b0ae398fc54ea69ff85ec700722c9da773
- XXX: on recent kernels this calculation differs by ~1% than
+ XXX: on recent kernels this calculation differs by ~1.5% than
"MemAvailable:" as it's calculated slightly differently, see:
https://gitlab.com/procps-ng/procps/issues/42
https://github.com/famzah/linux-memavailable-procfs/issues/2
diff --git a/psutil/tests/test_linux.py b/psutil/tests/test_linux.py
index 202829f5..abc73d38 100644
--- a/psutil/tests/test_linux.py
+++ b/psutil/tests/test_linux.py
@@ -6,6 +6,7 @@
"""Linux specific tests."""
+from __future__ import division
import collections
import contextlib
import errno
@@ -228,22 +229,26 @@ class TestSystemVirtualMemory(unittest.TestCase):
msg='%s %s \n%s' % (free_value, psutil_value, out))
def test_warnings_mocked(self):
- def open_mock(*args, **kwargs):
- return io.BytesIO(textwrap.dedent("""\
- Active(anon): 6145416 kB
- Active(file): 2950064 kB
- Buffers: 287952 kB
- Inactive(anon): 574764 kB
- Inactive(file): 1567648 kB
- MemAvailable: 6574984 kB
- MemFree: 2057400 kB
- MemTotal: 16325648 kB
- Shmem: 577588 kB
- SReclaimable: 346648 kB
- """).encode())
+ def open_mock(name, *args, **kwargs):
+ if name == '/proc/meminfo':
+ return io.BytesIO(textwrap.dedent("""\
+ Active(anon): 6145416 kB
+ Active(file): 2950064 kB
+ Buffers: 287952 kB
+ Inactive(anon): 574764 kB
+ Inactive(file): 1567648 kB
+ MemAvailable: 6574984 kB
+ MemFree: 2057400 kB
+ MemTotal: 16325648 kB
+ Shmem: 577588 kB
+ SReclaimable: 346648 kB
+ """).encode())
+ else:
+ return orig_open(name, *args, **kwargs)
- with mock.patch('psutil._pslinux.open', create=True,
- side_effect=open_mock) as m:
+ orig_open = open
+ patch_point = 'builtins.open' if PY3 else '__builtin__.open'
+ with mock.patch(patch_point, create=True, side_effect=open_mock) as m:
with warnings.catch_warnings(record=True) as ws:
warnings.simplefilter("always")
ret = psutil.virtual_memory()
@@ -278,25 +283,29 @@ class TestSystemVirtualMemory(unittest.TestCase):
def test_avail_old_comes_from_kernel(self):
# Make sure "MemAvailable:" coluimn is used instead of relying
# on our internal algorithm to calculate avail mem.
- def open_mock(*args, **kwargs):
- return io.BytesIO(textwrap.dedent("""\
- Active: 9444728 kB
- Active(anon): 6145416 kB
- Active(file): 2950064 kB
- Buffers: 287952 kB
- Cached: 4818144 kB
- Inactive(file): 1578132 kB
- Inactive(anon): 574764 kB
- Inactive(file): 1567648 kB
- MemAvailable: 6574984 kB
- MemFree: 2057400 kB
- MemTotal: 16325648 kB
- Shmem: 577588 kB
- SReclaimable: 346648 kB
- """).encode())
+ def open_mock(name, *args, **kwargs):
+ if name == "/proc/meminfo":
+ return io.BytesIO(textwrap.dedent("""\
+ Active: 9444728 kB
+ Active(anon): 6145416 kB
+ Active(file): 2950064 kB
+ Buffers: 287952 kB
+ Cached: 4818144 kB
+ Inactive(file): 1578132 kB
+ Inactive(anon): 574764 kB
+ Inactive(file): 1567648 kB
+ MemAvailable: 6574984 kB
+ MemFree: 2057400 kB
+ MemTotal: 16325648 kB
+ Shmem: 577588 kB
+ SReclaimable: 346648 kB
+ """).encode())
+ else:
+ return orig_open(name, *args, **kwargs)
- with mock.patch('psutil._pslinux.open', create=True,
- side_effect=open_mock) as m:
+ orig_open = open
+ patch_point = 'builtins.open' if PY3 else '__builtin__.open'
+ with mock.patch(patch_point, create=True, side_effect=open_mock) as m:
ret = psutil.virtual_memory()
assert m.called
self.assertEqual(ret.available, 6574984 * 1024)
@@ -571,7 +580,6 @@ class TestSystemNetwork(unittest.TestCase):
"""))
else:
return orig_open(name, *args, **kwargs)
- return orig_open(name, *args)
orig_open = open
patch_point = 'builtins.open' if PY3 else '__builtin__.open'
@@ -654,7 +662,6 @@ class TestSystemDisks(unittest.TestCase):
u(" 3 0 1 hda 2 3 4 5 6 7 8 9 10 11 12"))
else:
return orig_open(name, *args, **kwargs)
- return orig_open(name, *args)
orig_open = open
patch_point = 'builtins.open' if PY3 else '__builtin__.open'
@@ -687,7 +694,6 @@ class TestSystemDisks(unittest.TestCase):
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'
@@ -722,7 +728,6 @@ class TestSystemDisks(unittest.TestCase):
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'