9
from test_psutil import kill, PYTHON, DEVNULL
11
from psutil import wmi
13
warnings.warn("Can't import WMI module; Windows specific tests disabled",
18
class WindowsSpecificTestCase(unittest.TestCase):
21
self.pid = subprocess.Popen([PYTHON, "-E", "-O"], stdout=DEVNULL, stderr=DEVNULL).pid
26
def test_issue_24(self):
28
self.assertRaises(psutil.AccessDenied, p.kill)
32
self.assertEqual(p.name, 'System')
33
# use __str__ to access all common Process properties to check
34
# that nothing strange happens
37
self.assertTrue(p.create_time >= 0.0)
39
rss, vms = p.get_memory_info()
40
except psutil.AccessDenied:
41
# expected on Windows Vista and Windows 7
42
if not platform.uname()[1] in ('vista', 'win-7'):
45
self.assertTrue(rss > 0)
46
self.assertEqual(vms, 0)
50
# --- Process class tests
52
def test_process_name(self):
53
w = wmi.WMI().Win32_Process(ProcessId=self.pid)[0]
54
p = psutil.Process(self.pid)
55
self.assertEqual(p.name, w.Caption)
57
def test_process_path(self):
58
w = wmi.WMI().Win32_Process(ProcessId=self.pid)[0]
59
p = psutil.Process(self.pid)
60
self.assertEqual(os.path.join(p.path, p.name), w.ExecutablePath)
62
def test_process_cmdline(self):
63
w = wmi.WMI().Win32_Process(ProcessId=self.pid)[0]
64
p = psutil.Process(self.pid)
65
self.assertEqual(' '.join(p.cmdline), w.CommandLine)
67
def test_process_username(self):
68
w = wmi.WMI().Win32_Process(ProcessId=self.pid)[0]
69
p = psutil.Process(self.pid)
70
domain, _, username = w.GetOwner()
71
username = "%s\\%s" %(domain, username)
72
self.assertEqual(p.username, username)
74
def test_process_rss_memory(self):
75
w = wmi.WMI().Win32_Process(ProcessId=self.pid)[0]
76
p = psutil.Process(self.pid)
77
rss = p.get_memory_info()[0]
78
self.assertEqual(rss, int(w.WorkingSetSize))
80
def test_process_vsz_memory(self):
81
w = wmi.WMI().Win32_Process(ProcessId=self.pid)[0]
82
p = psutil.Process(self.pid)
83
vsz = p.get_memory_info()[1]
84
self.assertEqual(vsz, int(w.PageFileUsage) * 1024)
86
def test_process_create_time(self):
87
w = wmi.WMI().Win32_Process(ProcessId=self.pid)[0]
88
p = psutil.Process(self.pid)
89
wmic_create = str(w.CreationDate.split('.')[0])
90
psutil_create = time.strftime("%Y%m%d%H%M%S",
91
time.localtime(p.create_time))
92
self.assertEqual(wmic_create, psutil_create)
95
# --- psutil namespace functions and constants tests
97
def test_NUM_CPUS(self):
98
num_cpus = int(os.environ['NUMBER_OF_PROCESSORS'])
99
self.assertEqual(num_cpus, psutil.NUM_CPUS)
101
def test_TOTAL_PHYMEM(self):
102
w = wmi.WMI().Win32_ComputerSystem()[0]
103
self.assertEqual(int(w.TotalPhysicalMemory), psutil.TOTAL_PHYMEM)
105
def test__UPTIME(self):
106
# _UPTIME constant is not public but it is used internally
107
# as value to return for pid 0 creation time.
108
# WMI behaves the same.
109
w = wmi.WMI().Win32_Process(ProcessId=self.pid)[0]
110
p = psutil.Process(0)
111
wmic_create = str(w.CreationDate.split('.')[0])
112
psutil_create = time.strftime("%Y%m%d%H%M%S",
113
time.localtime(p.create_time))
115
def test_get_pids(self):
116
# Note: this test might fail if the OS is starting/killing
117
# other processes in the meantime
118
w = wmi.WMI().Win32_Process()
119
wmi_pids = [x.ProcessId for x in w]
121
psutil_pids = psutil.get_pid_list()
123
if wmi_pids != psutil_pids:
124
difference = filter(lambda x:x not in wmi_pids, psutil_pids) + \
125
filter(lambda x:x not in psutil_pids, wmi_pids)
126
self.fail("difference: " + str(difference))
129
if __name__ == '__main__':
130
test_suite = unittest.TestSuite()
131
test_suite.addTest(unittest.makeSuite(WindowsSpecificTestCase))
132
unittest.TextTestRunner(verbosity=2).run(test_suite)
3
# $Id: _windows.py 798 2010-11-12 20:52:57Z g.rodola $
17
from test_psutil import reap_children, get_test_subprocess, wait_for_pid
20
except ImportError, err:
21
atexit.register(warnings.warn, "Couldn't run wmi tests: %s" % str(err),
25
WIN2000 = platform.win32_ver()[0] == '2000'
28
class WindowsSpecificTestCase(unittest.TestCase):
31
sproc = get_test_subprocess()
32
wait_for_pid(sproc.pid)
38
def test_issue_24(self):
40
self.assertRaises(psutil.AccessDenied, p.kill)
42
def test_special_pid(self):
47
self.assertEqual(p.name, 'System')
48
# use __str__ to access all common Process properties to check
49
# that nothing strange happens
52
self.assertTrue(p.create_time >= 0.0)
54
rss, vms = p.get_memory_info()
55
except psutil.AccessDenied:
56
# expected on Windows Vista and Windows 7
57
if not platform.uname()[1] in ('vista', 'win-7', 'win7'):
60
self.assertTrue(rss > 0)
62
self.assertEqual(vms, 0)
64
def test_signal(self):
65
p = psutil.Process(self.pid)
66
self.assertRaises(ValueError, p.send_signal, signal.SIGINT)
70
# --- Process class tests
72
def test_process_name(self):
73
w = wmi.WMI().Win32_Process(ProcessId=self.pid)[0]
74
p = psutil.Process(self.pid)
75
self.assertEqual(p.name, w.Caption)
77
def test_process_path(self):
78
w = wmi.WMI().Win32_Process(ProcessId=self.pid)[0]
79
p = psutil.Process(self.pid)
80
self.assertEqual(p.exe, w.ExecutablePath)
83
def test_process_cmdline(self):
84
w = wmi.WMI().Win32_Process(ProcessId=self.pid)[0]
85
p = psutil.Process(self.pid)
86
self.assertEqual(' '.join(p.cmdline), w.CommandLine.replace('"', ''))
88
def test_process_username(self):
89
w = wmi.WMI().Win32_Process(ProcessId=self.pid)[0]
90
p = psutil.Process(self.pid)
91
domain, _, username = w.GetOwner()
92
username = "%s\\%s" %(domain, username)
93
self.assertEqual(p.username, username)
95
def test_process_rss_memory(self):
97
w = wmi.WMI().Win32_Process(ProcessId=self.pid)[0]
98
p = psutil.Process(self.pid)
99
rss = p.get_memory_info().rss
100
self.assertEqual(rss, int(w.WorkingSetSize))
102
def test_process_vms_memory(self):
104
w = wmi.WMI().Win32_Process(ProcessId=self.pid)[0]
105
p = psutil.Process(self.pid)
106
vms = p.get_memory_info().vms
107
# http://msdn.microsoft.com/en-us/library/aa394372(VS.85).aspx
108
# ...claims that PageFileUsage is represented in Kilo
109
# bytes but funnily enough on certain platforms bytes are
111
wmi_usage = int(w.PageFileUsage)
112
if (vms != wmi_usage) and (vms != wmi_usage * 1024):
113
self.fail("wmi=%s, psutil=%s" % (wmi_usage, vms))
115
def test_process_create_time(self):
116
w = wmi.WMI().Win32_Process(ProcessId=self.pid)[0]
117
p = psutil.Process(self.pid)
118
wmic_create = str(w.CreationDate.split('.')[0])
119
psutil_create = time.strftime("%Y%m%d%H%M%S",
120
time.localtime(p.create_time))
121
self.assertEqual(wmic_create, psutil_create)
124
# --- psutil namespace functions and constants tests
126
def test_NUM_CPUS(self):
127
num_cpus = int(os.environ['NUMBER_OF_PROCESSORS'])
128
self.assertEqual(num_cpus, psutil.NUM_CPUS)
130
def test_TOTAL_PHYMEM(self):
131
w = wmi.WMI().Win32_ComputerSystem()[0]
132
self.assertEqual(int(w.TotalPhysicalMemory), psutil.TOTAL_PHYMEM)
134
def test__UPTIME(self):
135
# _UPTIME constant is not public but it is used internally
136
# as value to return for pid 0 creation time.
137
# WMI behaves the same.
138
w = wmi.WMI().Win32_Process(ProcessId=self.pid)[0]
139
p = psutil.Process(0)
140
wmic_create = str(w.CreationDate.split('.')[0])
141
psutil_create = time.strftime("%Y%m%d%H%M%S",
142
time.localtime(p.create_time))
144
def test_get_pids(self):
145
# Note: this test might fail if the OS is starting/killing
146
# other processes in the meantime
147
w = wmi.WMI().Win32_Process()
148
wmi_pids = [x.ProcessId for x in w]
150
psutil_pids = psutil.get_pid_list()
152
if wmi_pids != psutil_pids:
153
difference = filter(lambda x:x not in wmi_pids, psutil_pids) + \
154
filter(lambda x:x not in psutil_pids, wmi_pids)
155
self.fail("difference: " + str(difference))
158
if __name__ == '__main__':
159
test_suite = unittest.TestSuite()
160
test_suite.addTest(unittest.makeSuite(WindowsSpecificTestCase))
161
unittest.TextTestRunner(verbosity=2).run(test_suite)