OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # |
| 3 # $Id: _windows.py 798 2010-11-12 20:52:57Z g.rodola $ |
| 4 # |
| 5 |
| 6 |
| 7 import os |
| 8 import unittest |
| 9 import platform |
| 10 import subprocess |
| 11 import signal |
| 12 import time |
| 13 import warnings |
| 14 import atexit |
| 15 |
| 16 import psutil |
| 17 from test_psutil import reap_children, get_test_subprocess, wait_for_pid |
| 18 try: |
| 19 import wmi |
| 20 except ImportError, err: |
| 21 atexit.register(warnings.warn, "Couldn't run wmi tests: %s" % str(err), |
| 22 RuntimeWarning) |
| 23 wmi = None |
| 24 |
| 25 WIN2000 = platform.win32_ver()[0] == '2000' |
| 26 |
| 27 |
| 28 class WindowsSpecificTestCase(unittest.TestCase): |
| 29 |
| 30 def setUp(self): |
| 31 sproc = get_test_subprocess() |
| 32 wait_for_pid(sproc.pid) |
| 33 self.pid = sproc.pid |
| 34 |
| 35 def tearDown(self): |
| 36 reap_children() |
| 37 |
| 38 def test_issue_24(self): |
| 39 p = psutil.Process(0) |
| 40 self.assertRaises(psutil.AccessDenied, p.kill) |
| 41 |
| 42 def test_special_pid(self): |
| 43 if not WIN2000: |
| 44 p = psutil.Process(4) |
| 45 else: |
| 46 p = psutil.Process(8) |
| 47 self.assertEqual(p.name, 'System') |
| 48 # use __str__ to access all common Process properties to check |
| 49 # that nothing strange happens |
| 50 str(p) |
| 51 p.username |
| 52 self.assertTrue(p.create_time >= 0.0) |
| 53 try: |
| 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'): |
| 58 raise |
| 59 else: |
| 60 self.assertTrue(rss > 0) |
| 61 if not WIN2000: |
| 62 self.assertEqual(vms, 0) |
| 63 |
| 64 def test_signal(self): |
| 65 p = psutil.Process(self.pid) |
| 66 self.assertRaises(ValueError, p.send_signal, signal.SIGINT) |
| 67 |
| 68 if wmi is not None: |
| 69 |
| 70 # --- Process class tests |
| 71 |
| 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) |
| 76 |
| 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) |
| 81 |
| 82 if not WIN2000: |
| 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('"',
'')) |
| 87 |
| 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) |
| 94 |
| 95 def test_process_rss_memory(self): |
| 96 time.sleep(0.1) |
| 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)) |
| 101 |
| 102 def test_process_vms_memory(self): |
| 103 time.sleep(0.1) |
| 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 |
| 110 # returned instead. |
| 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)) |
| 114 |
| 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) |
| 122 |
| 123 |
| 124 # --- psutil namespace functions and constants tests |
| 125 |
| 126 def test_NUM_CPUS(self): |
| 127 num_cpus = int(os.environ['NUMBER_OF_PROCESSORS']) |
| 128 self.assertEqual(num_cpus, psutil.NUM_CPUS) |
| 129 |
| 130 def test_TOTAL_PHYMEM(self): |
| 131 w = wmi.WMI().Win32_ComputerSystem()[0] |
| 132 self.assertEqual(int(w.TotalPhysicalMemory), psutil.TOTAL_PHYMEM) |
| 133 |
| 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)) |
| 143 |
| 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] |
| 149 wmi_pids.sort() |
| 150 psutil_pids = psutil.get_pid_list() |
| 151 psutil_pids.sort() |
| 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)) |
| 156 |
| 157 |
| 158 if __name__ == '__main__': |
| 159 test_suite = unittest.TestSuite() |
| 160 test_suite.addTest(unittest.makeSuite(WindowsSpecificTestCase)) |
| 161 unittest.TextTestRunner(verbosity=2).run(test_suite) |
| 162 |
OLD | NEW |