35
35
from psutil .tests import bind_unix_socket
36
36
from psutil .tests import check_connection_ntuple
37
37
from psutil .tests import create_sockets
38
+ from psutil .tests import filter_proc_connections
38
39
from psutil .tests import reap_children
39
40
from psutil .tests import retry_on_failure
40
41
from psutil .tests import serialrun
44
45
from psutil .tests import wait_for_file
45
46
46
47
47
- thisproc = psutil .Process ()
48
48
SOCK_SEQPACKET = getattr (socket , "SOCK_SEQPACKET" , object ())
49
49
50
50
51
+ def this_proc_connections (kind ):
52
+ cons = psutil .Process ().connections (kind = kind )
53
+ if kind in ("all" , "unix" ):
54
+ return filter_proc_connections (cons )
55
+ return cons
56
+
57
+
51
58
@serialrun
52
59
class ConnectionTestCase (PsutilTestCase ):
53
60
def setUp (self ):
54
- if NETBSD or FREEBSD or (MACOS and not PY3 ):
55
- # Process opens a UNIX socket to /var/log/run.
56
- return
57
- cons = thisproc .connections (kind = 'all' )
58
- self .assertEqual (cons , [])
61
+ self .assertEqual (this_proc_connections (kind = 'all' ), [])
59
62
60
63
def tearDown (self ):
61
64
# Make sure we closed all resources.
62
- # Some BSDs open a UNIX socket to /var/log/run.
63
- if NETBSD or FREEBSD or (MACOS and not PY3 ):
64
- return
65
- cons = thisproc .connections (kind = 'all' )
66
- self .assertEqual (cons , [])
65
+ self .assertEqual (this_proc_connections (kind = 'all' ), [])
67
66
68
67
def compare_procsys_connections (self , pid , proc_cons , kind = 'all' ):
69
68
"""Given a process PID and its list of connections compare
@@ -95,11 +94,11 @@ def test_system(self):
95
94
96
95
def test_process (self ):
97
96
with create_sockets ():
98
- for conn in psutil . Process (). connections (kind = 'all' ):
97
+ for conn in this_proc_connections (kind = 'all' ):
99
98
check_connection_ntuple (conn )
100
99
101
100
def test_invalid_kind (self ):
102
- self .assertRaises (ValueError , thisproc . connections , kind = '???' )
101
+ self .assertRaises (ValueError , this_proc_connections , kind = '???' )
103
102
self .assertRaises (ValueError , psutil .net_connections , kind = '???' )
104
103
105
104
@@ -108,7 +107,7 @@ class TestUnconnectedSockets(ConnectionTestCase):
108
107
"""Tests sockets which are open but not connected to anything."""
109
108
110
109
def get_conn_from_sock (self , sock ):
111
- cons = thisproc . connections (kind = 'all' )
110
+ cons = this_proc_connections (kind = 'all' )
112
111
smap = dict ([(c .fd , c ) for c in cons ])
113
112
if NETBSD or FREEBSD :
114
113
# NetBSD opens a UNIX socket to /var/log/run
@@ -148,7 +147,7 @@ def check_socket(self, sock):
148
147
149
148
# XXX Solaris can't retrieve system-wide UNIX sockets
150
149
if sock .family == AF_UNIX and HAS_CONNECTIONS_UNIX :
151
- cons = thisproc . connections (kind = 'all' )
150
+ cons = this_proc_connections (kind = 'all' )
152
151
self .compare_procsys_connections (os .getpid (), cons , kind = 'all' )
153
152
return conn
154
153
@@ -210,17 +209,17 @@ class TestConnectedSocket(ConnectionTestCase):
210
209
@unittest .skipIf (SUNOS , "unreliable on SUONS" )
211
210
def test_tcp (self ):
212
211
addr = ("127.0.0.1" , 0 )
213
- self .assertEqual (thisproc . connections (kind = 'tcp4' ), [])
212
+ self .assertEqual (this_proc_connections (kind = 'tcp4' ), [])
214
213
server , client = tcp_socketpair (AF_INET , addr = addr )
215
214
try :
216
- cons = thisproc . connections (kind = 'tcp4' )
215
+ cons = this_proc_connections (kind = 'tcp4' )
217
216
self .assertEqual (len (cons ), 2 )
218
217
self .assertEqual (cons [0 ].status , psutil .CONN_ESTABLISHED )
219
218
self .assertEqual (cons [1 ].status , psutil .CONN_ESTABLISHED )
220
219
# May not be fast enough to change state so it stays
221
220
# commenteed.
222
221
# client.close()
223
- # cons = thisproc.connections (kind='all')
222
+ # cons = this_proc_connections (kind='all')
224
223
# self.assertEqual(len(cons), 1)
225
224
# self.assertEqual(cons[0].status, psutil.CONN_CLOSE_WAIT)
226
225
finally :
@@ -232,7 +231,7 @@ def test_unix(self):
232
231
testfn = self .get_testfn ()
233
232
server , client = unix_socketpair (testfn )
234
233
try :
235
- cons = thisproc . connections (kind = 'unix' )
234
+ cons = this_proc_connections (kind = 'unix' )
236
235
assert not (cons [0 ].laddr and cons [0 ].raddr ), cons
237
236
assert not (cons [1 ].laddr and cons [1 ].raddr ), cons
238
237
if NETBSD or FREEBSD :
@@ -258,7 +257,7 @@ def test_unix(self):
258
257
class TestFilters (ConnectionTestCase ):
259
258
def test_filters (self ):
260
259
def check (kind , families , types ):
261
- for conn in thisproc . connections (kind = kind ):
260
+ for conn in this_proc_connections (kind = kind ):
262
261
self .assertIn (conn .family , families )
263
262
self .assertIn (conn .type , types )
264
263
if not SKIP_SYSCONS :
@@ -373,7 +372,7 @@ def check_conn(proc, conn, family, type, laddr, raddr, status, kinds):
373
372
tcp6_addr = None
374
373
udp6_addr = None
375
374
376
- for p in thisproc .children ():
375
+ for p in psutil . Process () .children ():
377
376
cons = p .connections ()
378
377
self .assertEqual (len (cons ), 1 )
379
378
for conn in cons :
@@ -429,56 +428,56 @@ def check_conn(proc, conn, family, type, laddr, raddr, status, kinds):
429
428
def test_count (self ):
430
429
with create_sockets ():
431
430
# tcp
432
- cons = thisproc . connections (kind = 'tcp' )
431
+ cons = this_proc_connections (kind = 'tcp' )
433
432
self .assertEqual (len (cons ), 2 if supports_ipv6 () else 1 )
434
433
for conn in cons :
435
434
self .assertIn (conn .family , (AF_INET , AF_INET6 ))
436
435
self .assertEqual (conn .type , SOCK_STREAM )
437
436
# tcp4
438
- cons = thisproc . connections (kind = 'tcp4' )
437
+ cons = this_proc_connections (kind = 'tcp4' )
439
438
self .assertEqual (len (cons ), 1 )
440
439
self .assertEqual (cons [0 ].family , AF_INET )
441
440
self .assertEqual (cons [0 ].type , SOCK_STREAM )
442
441
# tcp6
443
442
if supports_ipv6 ():
444
- cons = thisproc . connections (kind = 'tcp6' )
443
+ cons = this_proc_connections (kind = 'tcp6' )
445
444
self .assertEqual (len (cons ), 1 )
446
445
self .assertEqual (cons [0 ].family , AF_INET6 )
447
446
self .assertEqual (cons [0 ].type , SOCK_STREAM )
448
447
# udp
449
- cons = thisproc . connections (kind = 'udp' )
448
+ cons = this_proc_connections (kind = 'udp' )
450
449
self .assertEqual (len (cons ), 2 if supports_ipv6 () else 1 )
451
450
for conn in cons :
452
451
self .assertIn (conn .family , (AF_INET , AF_INET6 ))
453
452
self .assertEqual (conn .type , SOCK_DGRAM )
454
453
# udp4
455
- cons = thisproc . connections (kind = 'udp4' )
454
+ cons = this_proc_connections (kind = 'udp4' )
456
455
self .assertEqual (len (cons ), 1 )
457
456
self .assertEqual (cons [0 ].family , AF_INET )
458
457
self .assertEqual (cons [0 ].type , SOCK_DGRAM )
459
458
# udp6
460
459
if supports_ipv6 ():
461
- cons = thisproc . connections (kind = 'udp6' )
460
+ cons = this_proc_connections (kind = 'udp6' )
462
461
self .assertEqual (len (cons ), 1 )
463
462
self .assertEqual (cons [0 ].family , AF_INET6 )
464
463
self .assertEqual (cons [0 ].type , SOCK_DGRAM )
465
464
# inet
466
- cons = thisproc . connections (kind = 'inet' )
465
+ cons = this_proc_connections (kind = 'inet' )
467
466
self .assertEqual (len (cons ), 4 if supports_ipv6 () else 2 )
468
467
for conn in cons :
469
468
self .assertIn (conn .family , (AF_INET , AF_INET6 ))
470
469
self .assertIn (conn .type , (SOCK_STREAM , SOCK_DGRAM ))
471
470
# inet6
472
471
if supports_ipv6 ():
473
- cons = thisproc . connections (kind = 'inet6' )
472
+ cons = this_proc_connections (kind = 'inet6' )
474
473
self .assertEqual (len (cons ), 2 )
475
474
for conn in cons :
476
475
self .assertEqual (conn .family , AF_INET6 )
477
476
self .assertIn (conn .type , (SOCK_STREAM , SOCK_DGRAM ))
478
477
# Skipped on BSD becayse by default the Python process
479
478
# creates a UNIX socket to '/var/run/log'.
480
479
if HAS_CONNECTIONS_UNIX and not (FREEBSD or NETBSD ):
481
- cons = thisproc . connections (kind = 'unix' )
480
+ cons = this_proc_connections (kind = 'unix' )
482
481
self .assertEqual (len (cons ), 3 )
483
482
for conn in cons :
484
483
self .assertEqual (conn .family , AF_UNIX )
0 commit comments