OLD | NEW |
| (Empty) |
1 #!/usr/bin/env python | |
2 # | |
3 # $Id: _linux.py 1142 2011-10-05 18:45:49Z g.rodola $ | |
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.""" | |
10 | |
11 import unittest | |
12 import subprocess | |
13 import sys | |
14 | |
15 from test_psutil import sh | |
16 import psutil | |
17 | |
18 | |
19 class LinuxSpecificTestCase(unittest.TestCase): | |
20 | |
21 def test_cached_phymem(self): | |
22 # test psutil.cached_phymem against "cached" column of free | |
23 # command line utility | |
24 p = subprocess.Popen("free", shell=1, stdout=subprocess.PIPE) | |
25 output = p.communicate()[0].strip() | |
26 if sys.version_info >= (3,): | |
27 output = str(output, sys.stdout.encoding) | |
28 free_cmem = int(output.split('\n')[1].split()[6]) | |
29 psutil_cmem = psutil.cached_phymem() / 1024 | |
30 self.assertEqual(free_cmem, psutil_cmem) | |
31 | |
32 def test_phymem_buffers(self): | |
33 # test psutil.phymem_buffers against "buffers" column of free | |
34 # command line utility | |
35 p = subprocess.Popen("free", shell=1, stdout=subprocess.PIPE) | |
36 output = p.communicate()[0].strip() | |
37 if sys.version_info >= (3,): | |
38 output = str(output, sys.stdout.encoding) | |
39 free_cmem = int(output.split('\n')[1].split()[5]) | |
40 psutil_cmem = psutil.phymem_buffers() / 1024 | |
41 self.assertEqual(free_cmem, psutil_cmem) | |
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 | |
68 | |
69 if __name__ == '__main__': | |
70 test_suite = unittest.TestSuite() | |
71 test_suite.addTest(unittest.makeSuite(LinuxSpecificTestCase)) | |
72 unittest.TextTestRunner(verbosity=2).run(test_suite) | |
OLD | NEW |