~ubuntu-branches/ubuntu/raring/python-psutil/raring

« back to all changes in this revision

Viewing changes to test/_posix.py

  • Committer: Package Import Robot
  • Author(s): Julian Taylor
  • Date: 2012-07-02 23:51:39 UTC
  • mfrom: (2.1.11 sid)
  • Revision ID: package-import@ubuntu.com-20120702235139-fdlsb5x3t0b2jr06
Tags: 0.5.1-1ubuntu1
* Merge from Debian unstable.  Remaining changes:
  - Switch to dh_python2. (LP: #788514)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/usr/bin/env python
2
2
#
3
 
# $Id: _posix.py 1204 2011-10-24 19:19:01Z g.rodola $
 
3
# $Id: _posix.py 1386 2012-06-27 15:44:36Z g.rodola $
4
4
#
5
5
# Copyright (c) 2009, Jay Loden, Giampaolo Rodola'. All rights reserved.
6
6
# Use of this source code is governed by a BSD-style license that can be
13
13
import time
14
14
import sys
15
15
import os
 
16
import datetime
16
17
 
17
18
import psutil
18
19
 
 
20
from psutil._compat import PY3
19
21
from test_psutil import (get_test_subprocess, reap_children, PYTHON, LINUX, OSX,
20
 
                         ignore_access_denied, sh)
 
22
                         BSD, ignore_access_denied, sh, skipIf)
21
23
 
22
24
 
23
25
def ps(cmd):
28
30
        cmd = cmd.replace(" --no-headers ", " ")
29
31
    p = subprocess.Popen(cmd, shell=1, stdout=subprocess.PIPE)
30
32
    output = p.communicate()[0].strip()
31
 
    if sys.version_info >= (3,):
 
33
    if PY3:
32
34
        output = str(output, sys.stdout.encoding)
33
35
    if not LINUX:
34
36
        output = output.split('\n')[1]
44
46
    # for ps -o arguments see: http://unixhelp.ed.ac.uk/CGI/man-cgi?ps
45
47
 
46
48
    def setUp(self):
47
 
        self.pid = get_test_subprocess([PYTHON, "-E", "-O"]).pid
 
49
        self.pid = get_test_subprocess([PYTHON, "-E", "-O"],
 
50
                                       stdin=subprocess.PIPE).pid
48
51
 
49
52
    def tearDown(self):
50
53
        reap_children()
95
98
        name_psutil = psutil.Process(self.pid).name.lower()
96
99
        self.assertEqual(name_ps, name_psutil)
97
100
 
 
101
    @skipIf(OSX or BSD)
 
102
    def test_process_create_time(self):
 
103
        time_ps = ps("ps --no-headers -o start -p %s" %self.pid).split(' ')[0]
 
104
        time_psutil = psutil.Process(self.pid).create_time
 
105
        time_psutil = datetime.datetime.fromtimestamp(
 
106
                        time_psutil).strftime("%H:%M:%S")
 
107
        self.assertEqual(time_ps, time_psutil)
 
108
 
98
109
    def test_process_exe(self):
99
110
        ps_pathname = ps("ps --no-headers -o command -p %s" %self.pid).split(' ')[0]
100
111
        psutil_pathname = psutil.Process(self.pid).exe
120
131
        # other processes in the meantime
121
132
        p = get_test_subprocess(["ps", "ax", "-o", "pid"], stdout=subprocess.PIPE)
122
133
        output = p.communicate()[0].strip()
123
 
        if sys.version_info >= (3,):
 
134
        if PY3:
124
135
            output = str(output, sys.stdout.encoding)
125
136
        output = output.replace('PID', '')
126
137
        p.wait()
142
153
            difference = [x for x in pids_psutil if x not in pids_ps] + \
143
154
                         [x for x in pids_ps if x not in pids_psutil]
144
155
            self.fail("difference: " + str(difference))
145
 
            
 
156
 
146
157
    def test_nic_names(self):
147
158
        p = subprocess.Popen("ifconfig -a", shell=1, stdout=subprocess.PIPE)
148
159
        output = p.communicate()[0].strip()
149
 
        if sys.version_info >= (3,):
 
160
        if PY3:
150
161
            output = str(output, sys.stdout.encoding)
151
162
        for nic in psutil.network_io_counters(pernic=True).keys():
152
163
            for line in output.split():
155
166
            else:
156
167
                self.fail("couldn't find %s nic in 'ifconfig -a' output" % nic)
157
168
 
 
169
    def test_get_users(self):
 
170
        out = sh("who")
 
171
        lines = out.split('\n')
 
172
        users = [x.split()[0] for x in lines]
 
173
        self.assertEqual(len(users), len(psutil.get_users()))
 
174
        terminals = [x.split()[1] for x in lines]
 
175
        for u in psutil.get_users():
 
176
            self.assertTrue(u.name in users, u.name)
 
177
            self.assertTrue(u.terminal in terminals, u.terminal)
 
178
 
158
179
 
159
180
if __name__ == '__main__':
160
181
    test_suite = unittest.TestSuite()
161
182
    test_suite.addTest(unittest.makeSuite(PosixSpecificTestCase))
162
183
    unittest.TextTestRunner(verbosity=2).run(test_suite)
163
 
 
164