~ubuntu-branches/ubuntu/oneiric/python-psutil/oneiric

« back to all changes in this revision

Viewing changes to test/_windows.py

  • Committer: Bazaar Package Importer
  • Author(s): Sandro Tosi
  • Date: 2011-04-04 20:26:42 UTC
  • mfrom: (2.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20110404202642-u2fyar19eabqb2mn
Tags: 0.2.1-1
* New upstream release
* debian/copyright
  - extended packaging copyright years
* debian/rules
  - use the correct PYTHONPATH when running tests, for all supported versions
* debian/control
  - it now contains also extensions, so it's an arch:any package
  - move python-support from b-d-i to b-d
  - we now need python-all-dev in b-d
  - added procps to b-d, needed to run tests
* debian/{control, pyversion}
  - removed pyversion, replaced by XS-P-V field

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
import os
2
 
import unittest
3
 
import platform
4
 
import subprocess
5
 
import time
6
 
import warnings
7
 
 
8
 
import psutil
9
 
from test_psutil import kill, PYTHON, DEVNULL
10
 
try:
11
 
    from psutil import wmi
12
 
except ImportError:
13
 
    warnings.warn("Can't import WMI module; Windows specific tests disabled",
14
 
                  RuntimeWarning)
15
 
    wmi = None
16
 
 
17
 
 
18
 
class WindowsSpecificTestCase(unittest.TestCase):
19
 
 
20
 
    def setUp(self):
21
 
        self.pid = subprocess.Popen([PYTHON, "-E", "-O"], stdout=DEVNULL, stderr=DEVNULL).pid
22
 
 
23
 
    def tearDown(self):
24
 
        kill(self.pid)
25
 
 
26
 
    def test_issue_24(self):
27
 
        p = psutil.Process(0)
28
 
        self.assertRaises(psutil.AccessDenied, p.kill)
29
 
 
30
 
    def test_pid_4(self):
31
 
        p = psutil.Process(4)
32
 
        self.assertEqual(p.name, 'System')
33
 
        # use __str__ to access all common Process properties to check
34
 
        # that nothing strange happens
35
 
        str(p)
36
 
        p.username
37
 
        self.assertTrue(p.create_time >= 0.0)
38
 
        try:
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'):
43
 
                raise
44
 
        else:
45
 
            self.assertTrue(rss > 0)
46
 
            self.assertEqual(vms, 0)
47
 
 
48
 
    if wmi is not None:
49
 
 
50
 
        # --- Process class tests
51
 
 
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)
56
 
 
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)
61
 
 
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)
66
 
 
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)
73
 
 
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))
79
 
 
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)
85
 
 
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)
93
 
 
94
 
 
95
 
        # --- psutil namespace functions and constants tests
96
 
 
97
 
        def test_NUM_CPUS(self):
98
 
            num_cpus = int(os.environ['NUMBER_OF_PROCESSORS'])
99
 
            self.assertEqual(num_cpus, psutil.NUM_CPUS)
100
 
 
101
 
        def test_TOTAL_PHYMEM(self):
102
 
            w = wmi.WMI().Win32_ComputerSystem()[0]
103
 
            self.assertEqual(int(w.TotalPhysicalMemory), psutil.TOTAL_PHYMEM)
104
 
 
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))
114
 
 
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]
120
 
            wmi_pids.sort()
121
 
            psutil_pids = psutil.get_pid_list()
122
 
            psutil_pids.sort()
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))
127
 
 
128
 
 
129
 
if __name__ == '__main__':
130
 
    test_suite = unittest.TestSuite()
131
 
    test_suite.addTest(unittest.makeSuite(WindowsSpecificTestCase))
132
 
    unittest.TextTestRunner(verbosity=2).run(test_suite)
133
 
 
 
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