2012-09-19 18:20:16 +00:00
|
|
|
#!/usr/bin/env python
|
2013-05-09 22:39:30 +00:00
|
|
|
|
2013-08-23 06:36:57 +00:00
|
|
|
# Copyright (c) 2009, Giampaolo Rodola'. All rights reserved.
|
2012-09-19 18:20:16 +00:00
|
|
|
# Use of this source code is governed by a BSD-style license that can be
|
|
|
|
# found in the LICENSE file.
|
|
|
|
|
|
|
|
"""Linux specific tests. These are implicitly run by test_psutil.py."""
|
|
|
|
|
|
|
|
from __future__ import division
|
|
|
|
import unittest
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
import time
|
|
|
|
import os
|
2013-05-09 22:39:30 +00:00
|
|
|
import re
|
2012-09-19 18:20:16 +00:00
|
|
|
|
2013-08-23 06:36:57 +00:00
|
|
|
from test_psutil import *
|
2012-09-19 18:20:16 +00:00
|
|
|
from psutil._compat import PY3
|
|
|
|
import psutil
|
|
|
|
|
|
|
|
|
|
|
|
class LinuxSpecificTestCase(unittest.TestCase):
|
|
|
|
|
2013-08-23 06:36:57 +00:00
|
|
|
@unittest.skipIf(POSIX and not hasattr(os, 'statvfs'),
|
|
|
|
reason="os.statvfs() function not available on this platform")
|
|
|
|
@skip_on_not_implemented()
|
2012-09-19 18:20:16 +00:00
|
|
|
def test_disks(self):
|
|
|
|
# test psutil.disk_usage() and psutil.disk_partitions()
|
|
|
|
# against "df -a"
|
|
|
|
def df(path):
|
|
|
|
out = sh('df -P -B 1 "%s"' % path).strip()
|
|
|
|
lines = out.split('\n')
|
|
|
|
lines.pop(0)
|
|
|
|
line = lines.pop(0)
|
|
|
|
dev, total, used, free = line.split()[:4]
|
|
|
|
if dev == 'none':
|
|
|
|
dev = ''
|
|
|
|
total, used, free = int(total), int(used), int(free)
|
|
|
|
return dev, total, used, free
|
|
|
|
|
|
|
|
for part in psutil.disk_partitions(all=False):
|
|
|
|
usage = psutil.disk_usage(part.mountpoint)
|
|
|
|
dev, total, used, free = df(part.mountpoint)
|
|
|
|
self.assertEqual(part.device, dev)
|
|
|
|
self.assertEqual(usage.total, total)
|
|
|
|
# 10 MB tollerance
|
|
|
|
if abs(usage.free - free) > 10 * 1024 * 1024:
|
|
|
|
self.fail("psutil=%s, df=%s" % (usage.free, free))
|
|
|
|
if abs(usage.used - used) > 10 * 1024 * 1024:
|
|
|
|
self.fail("psutil=%s, df=%s" % (usage.used, used))
|
|
|
|
|
|
|
|
def test_memory_maps(self):
|
|
|
|
sproc = get_test_subprocess()
|
|
|
|
time.sleep(1)
|
|
|
|
p = psutil.Process(sproc.pid)
|
|
|
|
maps = p.get_memory_maps(grouped=False)
|
|
|
|
pmap = sh('pmap -x %s' % p.pid).split('\n')
|
|
|
|
del pmap[0]; del pmap[0] # get rid of header
|
|
|
|
while maps and pmap:
|
|
|
|
this = maps.pop(0)
|
|
|
|
other = pmap.pop(0)
|
|
|
|
addr, _, rss, dirty, mode, path = other.split(None, 5)
|
|
|
|
if not path.startswith('[') and not path.endswith(']'):
|
|
|
|
self.assertEqual(path, os.path.basename(this.path))
|
|
|
|
self.assertEqual(int(rss) * 1024, this.rss)
|
|
|
|
# test only rwx chars, ignore 's' and 'p'
|
|
|
|
self.assertEqual(mode[:3], this.perms[:3])
|
|
|
|
|
|
|
|
def test_vmem_total(self):
|
|
|
|
lines = sh('free').split('\n')[1:]
|
|
|
|
total = int(lines[0].split()[1]) * 1024
|
|
|
|
self.assertEqual(total, psutil.virtual_memory().total)
|
|
|
|
|
2013-08-23 06:36:57 +00:00
|
|
|
@retry_before_failing()
|
2012-09-19 18:20:16 +00:00
|
|
|
def test_vmem_used(self):
|
|
|
|
lines = sh('free').split('\n')[1:]
|
|
|
|
used = int(lines[0].split()[2]) * 1024
|
2013-08-23 06:36:57 +00:00
|
|
|
self.assertAlmostEqual(used, psutil.virtual_memory().used,
|
|
|
|
delta=TOLERANCE)
|
2012-09-19 18:20:16 +00:00
|
|
|
|
2013-08-23 06:36:57 +00:00
|
|
|
@retry_before_failing()
|
2012-09-19 18:20:16 +00:00
|
|
|
def test_vmem_free(self):
|
|
|
|
lines = sh('free').split('\n')[1:]
|
|
|
|
free = int(lines[0].split()[3]) * 1024
|
2013-08-23 06:36:57 +00:00
|
|
|
self.assertAlmostEqual(free, psutil.virtual_memory().free,
|
|
|
|
delta=TOLERANCE)
|
2012-09-19 18:20:16 +00:00
|
|
|
|
2013-08-23 06:36:57 +00:00
|
|
|
@retry_before_failing()
|
2012-09-19 18:20:16 +00:00
|
|
|
def test_vmem_buffers(self):
|
|
|
|
lines = sh('free').split('\n')[1:]
|
|
|
|
buffers = int(lines[0].split()[5]) * 1024
|
2013-08-23 06:36:57 +00:00
|
|
|
self.assertAlmostEqual(buffers, psutil.virtual_memory().buffers,
|
|
|
|
delta=TOLERANCE)
|
2012-09-19 18:20:16 +00:00
|
|
|
|
2013-08-23 06:36:57 +00:00
|
|
|
@retry_before_failing()
|
2012-09-19 18:20:16 +00:00
|
|
|
def test_vmem_cached(self):
|
|
|
|
lines = sh('free').split('\n')[1:]
|
|
|
|
cached = int(lines[0].split()[6]) * 1024
|
2013-08-23 06:36:57 +00:00
|
|
|
self.assertAlmostEqual(cached, psutil.virtual_memory().cached,
|
|
|
|
delta=TOLERANCE)
|
2012-09-19 18:20:16 +00:00
|
|
|
|
|
|
|
def test_swapmem_total(self):
|
|
|
|
lines = sh('free').split('\n')[1:]
|
|
|
|
total = int(lines[2].split()[1]) * 1024
|
|
|
|
self.assertEqual(total, psutil.swap_memory().total)
|
|
|
|
|
2013-08-23 06:36:57 +00:00
|
|
|
@retry_before_failing()
|
2012-09-19 18:20:16 +00:00
|
|
|
def test_swapmem_used(self):
|
|
|
|
lines = sh('free').split('\n')[1:]
|
|
|
|
used = int(lines[2].split()[2]) * 1024
|
2013-08-23 06:36:57 +00:00
|
|
|
self.assertAlmostEqual(used, psutil.swap_memory().used,
|
|
|
|
delta=TOLERANCE)
|
2012-09-19 18:20:16 +00:00
|
|
|
|
2013-08-23 06:36:57 +00:00
|
|
|
@retry_before_failing()
|
2012-09-19 18:20:16 +00:00
|
|
|
def test_swapmem_free(self):
|
|
|
|
lines = sh('free').split('\n')[1:]
|
|
|
|
free = int(lines[2].split()[3]) * 1024
|
2013-08-23 06:36:57 +00:00
|
|
|
self.assertAlmostEqual(free, psutil.swap_memory().free,
|
|
|
|
delta=TOLERANCE)
|
2012-09-19 18:20:16 +00:00
|
|
|
|
2013-05-09 22:39:30 +00:00
|
|
|
def test_cpu_times(self):
|
|
|
|
fields = psutil.cpu_times()._fields
|
|
|
|
kernel_ver = re.findall('\d.\d.\d', os.uname()[2])[0]
|
|
|
|
kernel_ver_info = tuple(map(int, kernel_ver.split('.')))
|
|
|
|
# steal >= 2.6.11
|
|
|
|
# guest >= 2.6.24
|
|
|
|
# guest_nice >= 3.2.0
|
|
|
|
if kernel_ver_info >= (2, 6, 11):
|
2013-08-23 06:36:57 +00:00
|
|
|
self.assertIn('steal', fields)
|
2013-05-09 22:39:30 +00:00
|
|
|
else:
|
2013-08-23 06:36:57 +00:00
|
|
|
self.assertNotIn('steal', fields)
|
2013-05-09 22:39:30 +00:00
|
|
|
if kernel_ver_info >= (2, 6, 24):
|
2013-08-23 06:36:57 +00:00
|
|
|
self.assertIn('guest', fields)
|
2013-05-09 22:39:30 +00:00
|
|
|
else:
|
2013-08-23 06:36:57 +00:00
|
|
|
self.assertNotIn('guest', fields)
|
2013-05-09 22:39:30 +00:00
|
|
|
if kernel_ver_info >= (3, 2, 0):
|
2013-08-23 06:36:57 +00:00
|
|
|
self.assertIn('guest_nice', fields)
|
2013-05-09 22:39:30 +00:00
|
|
|
else:
|
2013-08-23 06:36:57 +00:00
|
|
|
self.assertNotIn('guest_nice', fields)
|
2013-05-09 22:39:30 +00:00
|
|
|
|
2012-09-19 18:20:16 +00:00
|
|
|
|
2013-08-23 06:36:57 +00:00
|
|
|
def test_main():
|
2012-09-19 18:20:16 +00:00
|
|
|
test_suite = unittest.TestSuite()
|
|
|
|
test_suite.addTest(unittest.makeSuite(LinuxSpecificTestCase))
|
2013-08-23 06:36:57 +00:00
|
|
|
result = unittest.TextTestRunner(verbosity=2).run(test_suite)
|
|
|
|
return result.wasSuccessful()
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
if not test_main():
|
|
|
|
sys.exit(1)
|