OLD | NEW |
| (Empty) |
1 #!/usr/bin/env python | |
2 # | |
3 # $Id: _osx.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 """OSX specific tests. These are implicitly run by test_psutil.py.""" | |
10 | |
11 import unittest | |
12 import subprocess | |
13 import time | |
14 import sys | |
15 | |
16 import psutil | |
17 | |
18 from test_psutil import reap_children, get_test_subprocess, sh | |
19 #from _posix import ps | |
20 | |
21 | |
22 def sysctl(cmdline): | |
23 """Expects a sysctl command with an argument and parse the result | |
24 returning only the value of interest. | |
25 """ | |
26 p = subprocess.Popen(cmdline, shell=1, stdout=subprocess.PIPE) | |
27 result = p.communicate()[0].strip().split()[1] | |
28 if sys.version_info >= (3,): | |
29 result = str(result, sys.stdout.encoding) | |
30 try: | |
31 return int(result) | |
32 except ValueError: | |
33 return result | |
34 | |
35 | |
36 class OSXSpecificTestCase(unittest.TestCase): | |
37 | |
38 def setUp(self): | |
39 self.pid = get_test_subprocess().pid | |
40 | |
41 def tearDown(self): | |
42 reap_children() | |
43 | |
44 def test_TOTAL_PHYMEM(self): | |
45 sysctl_hwphymem = sysctl('sysctl hw.memsize') | |
46 self.assertEqual(sysctl_hwphymem, psutil.TOTAL_PHYMEM) | |
47 | |
48 def test_process_create_time(self): | |
49 cmdline = "ps -o lstart -p %s" %self.pid | |
50 p = subprocess.Popen(cmdline, shell=1, stdout=subprocess.PIPE) | |
51 output = p.communicate()[0] | |
52 if sys.version_info >= (3,): | |
53 output = str(output, sys.stdout.encoding) | |
54 start_ps = output.replace('STARTED', '').strip() | |
55 start_psutil = psutil.Process(self.pid).create_time | |
56 start_psutil = time.strftime("%a %b %e %H:%M:%S %Y", | |
57 time.localtime(start_psutil)) | |
58 self.assertEqual(start_ps, start_psutil) | |
59 | |
60 def test_disks(self): | |
61 # test psutil.disk_usage() and psutil.disk_partitions() | |
62 # against "df -a" | |
63 def df(path): | |
64 out = sh('df -k "%s"' % path).strip() | |
65 lines = out.split('\n') | |
66 lines.pop(0) | |
67 line = lines.pop(0) | |
68 dev, total, used, free = line.split()[:4] | |
69 if dev == 'none': | |
70 dev = '' | |
71 total = int(total) * 1024 | |
72 used = int(used) * 1024 | |
73 free = int(free) * 1024 | |
74 return dev, total, used, free | |
75 | |
76 for part in psutil.disk_partitions(all=False): | |
77 usage = psutil.disk_usage(part.mountpoint) | |
78 dev, total, used, free = df(part.mountpoint) | |
79 self.assertEqual(part.device, dev) | |
80 self.assertEqual(usage.total, total) | |
81 # 10 MB tollerance | |
82 if abs(usage.free - free) > 10 * 1024 * 1024: | |
83 self.fail("psutil=%s, df=%s" % usage.free, free) | |
84 if abs(usage.used - used) > 10 * 1024 * 1024: | |
85 self.fail("psutil=%s, df=%s" % usage.used, used) | |
86 | |
87 | |
88 if __name__ == '__main__': | |
89 test_suite = unittest.TestSuite() | |
90 test_suite.addTest(unittest.makeSuite(OSXSpecificTestCase)) | |
91 unittest.TextTestRunner(verbosity=2).run(test_suite) | |
OLD | NEW |