OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # | 2 # |
3 # $Id: _linux.py 707 2010-10-19 18:16:08Z g.rodola $ | 3 # $Id: _linux.py 1142 2011-10-05 18:45:49Z g.rodola $ |
4 # | 4 # |
| 5 # Copyright (c) 2009, Jay Loden, Giampaolo Rodola'. All rights reserved. |
| 6 # Use of this source code is governed by a BSD-style license that can be |
| 7 # found in the LICENSE file. |
| 8 |
| 9 """Linux specific tests. These are implicitly run by test_psutil.py.""" |
5 | 10 |
6 import unittest | 11 import unittest |
7 import subprocess | 12 import subprocess |
8 import sys | 13 import sys |
9 | 14 |
| 15 from test_psutil import sh |
10 import psutil | 16 import psutil |
11 | 17 |
12 | 18 |
13 class LinuxSpecificTestCase(unittest.TestCase): | 19 class LinuxSpecificTestCase(unittest.TestCase): |
14 | 20 |
15 def test_cached_phymem(self): | 21 def test_cached_phymem(self): |
16 # test psutil.cached_phymem against "cached" column of free | 22 # test psutil.cached_phymem against "cached" column of free |
17 # command line utility | 23 # command line utility |
18 p = subprocess.Popen("free", shell=1, stdout=subprocess.PIPE) | 24 p = subprocess.Popen("free", shell=1, stdout=subprocess.PIPE) |
19 output = p.communicate()[0].strip() | 25 output = p.communicate()[0].strip() |
20 if sys.version_info >= (3,): | 26 if sys.version_info >= (3,): |
21 output = str(output, sys.stdout.encoding) | 27 output = str(output, sys.stdout.encoding) |
22 free_cmem = int(output.split('\n')[1].split()[6]) | 28 free_cmem = int(output.split('\n')[1].split()[6]) |
23 psutil_cmem = psutil.cached_phymem() / 1024 | 29 psutil_cmem = psutil.cached_phymem() / 1024 |
24 self.assertEqual(free_cmem, psutil_cmem) | 30 self.assertEqual(free_cmem, psutil_cmem) |
25 | 31 |
26 def test_phymem_buffers(self): | 32 def test_phymem_buffers(self): |
27 # test psutil.phymem_buffers against "buffers" column of free | 33 # test psutil.phymem_buffers against "buffers" column of free |
28 # command line utility | 34 # command line utility |
29 p = subprocess.Popen("free", shell=1, stdout=subprocess.PIPE) | 35 p = subprocess.Popen("free", shell=1, stdout=subprocess.PIPE) |
30 output = p.communicate()[0].strip() | 36 output = p.communicate()[0].strip() |
31 if sys.version_info >= (3,): | 37 if sys.version_info >= (3,): |
32 output = str(output, sys.stdout.encoding) | 38 output = str(output, sys.stdout.encoding) |
33 free_cmem = int(output.split('\n')[1].split()[5]) | 39 free_cmem = int(output.split('\n')[1].split()[5]) |
34 psutil_cmem = psutil.phymem_buffers() / 1024 | 40 psutil_cmem = psutil.phymem_buffers() / 1024 |
35 self.assertEqual(free_cmem, psutil_cmem) | 41 self.assertEqual(free_cmem, psutil_cmem) |
36 | 42 |
| 43 def test_disks(self): |
| 44 # test psutil.disk_usage() and psutil.disk_partitions() |
| 45 # against "df -a" |
| 46 def df(path): |
| 47 out = sh('df -P -B 1 "%s"' % path).strip() |
| 48 lines = out.split('\n') |
| 49 lines.pop(0) |
| 50 line = lines.pop(0) |
| 51 dev, total, used, free = line.split()[:4] |
| 52 if dev == 'none': |
| 53 dev = '' |
| 54 total, used, free = int(total), int(used), int(free) |
| 55 return dev, total, used, free |
| 56 |
| 57 for part in psutil.disk_partitions(all=False): |
| 58 usage = psutil.disk_usage(part.mountpoint) |
| 59 dev, total, used, free = df(part.mountpoint) |
| 60 self.assertEqual(part.device, dev) |
| 61 self.assertEqual(usage.total, total) |
| 62 # 10 MB tollerance |
| 63 if abs(usage.free - free) > 10 * 1024 * 1024: |
| 64 self.fail("psutil=%s, df=%s" % usage.free, free) |
| 65 if abs(usage.used - used) > 10 * 1024 * 1024: |
| 66 self.fail("psutil=%s, df=%s" % usage.used, used) |
| 67 |
37 | 68 |
38 if __name__ == '__main__': | 69 if __name__ == '__main__': |
39 test_suite = unittest.TestSuite() | 70 test_suite = unittest.TestSuite() |
40 test_suite.addTest(unittest.makeSuite(LinuxSpecificTestCase)) | 71 test_suite.addTest(unittest.makeSuite(LinuxSpecificTestCase)) |
41 unittest.TextTestRunner(verbosity=2).run(test_suite) | 72 unittest.TextTestRunner(verbosity=2).run(test_suite) |
42 | |
43 | |
44 | |
45 | |
OLD | NEW |