3535from psutil .tests import bind_unix_socket
3636from psutil .tests import check_connection_ntuple
3737from psutil .tests import create_sockets
38+ from psutil .tests import filter_proc_connections
3839from psutil .tests import reap_children
3940from psutil .tests import retry_on_failure
4041from psutil .tests import serialrun
4445from psutil .tests import wait_for_file
4546
4647
47- thisproc = psutil .Process ()
4848SOCK_SEQPACKET = getattr (socket , "SOCK_SEQPACKET" , object ())
4949
5050
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+
5158@serialrun
5259class ConnectionTestCase (PsutilTestCase ):
5360 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' ), [])
5962
6063 def tearDown (self ):
6164 # 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' ), [])
6766
6867 def compare_procsys_connections (self , pid , proc_cons , kind = 'all' ):
6968 """Given a process PID and its list of connections compare
@@ -95,11 +94,11 @@ def test_system(self):
9594
9695 def test_process (self ):
9796 with create_sockets ():
98- for conn in psutil . Process (). connections (kind = 'all' ):
97+ for conn in this_proc_connections (kind = 'all' ):
9998 check_connection_ntuple (conn )
10099
101100 def test_invalid_kind (self ):
102- self .assertRaises (ValueError , thisproc . connections , kind = '???' )
101+ self .assertRaises (ValueError , this_proc_connections , kind = '???' )
103102 self .assertRaises (ValueError , psutil .net_connections , kind = '???' )
104103
105104
@@ -108,7 +107,7 @@ class TestUnconnectedSockets(ConnectionTestCase):
108107 """Tests sockets which are open but not connected to anything."""
109108
110109 def get_conn_from_sock (self , sock ):
111- cons = thisproc . connections (kind = 'all' )
110+ cons = this_proc_connections (kind = 'all' )
112111 smap = dict ([(c .fd , c ) for c in cons ])
113112 if NETBSD or FREEBSD :
114113 # NetBSD opens a UNIX socket to /var/log/run
@@ -148,7 +147,7 @@ def check_socket(self, sock):
148147
149148 # XXX Solaris can't retrieve system-wide UNIX sockets
150149 if sock .family == AF_UNIX and HAS_CONNECTIONS_UNIX :
151- cons = thisproc . connections (kind = 'all' )
150+ cons = this_proc_connections (kind = 'all' )
152151 self .compare_procsys_connections (os .getpid (), cons , kind = 'all' )
153152 return conn
154153
@@ -210,17 +209,17 @@ class TestConnectedSocket(ConnectionTestCase):
210209 @unittest .skipIf (SUNOS , "unreliable on SUONS" )
211210 def test_tcp (self ):
212211 addr = ("127.0.0.1" , 0 )
213- self .assertEqual (thisproc . connections (kind = 'tcp4' ), [])
212+ self .assertEqual (this_proc_connections (kind = 'tcp4' ), [])
214213 server , client = tcp_socketpair (AF_INET , addr = addr )
215214 try :
216- cons = thisproc . connections (kind = 'tcp4' )
215+ cons = this_proc_connections (kind = 'tcp4' )
217216 self .assertEqual (len (cons ), 2 )
218217 self .assertEqual (cons [0 ].status , psutil .CONN_ESTABLISHED )
219218 self .assertEqual (cons [1 ].status , psutil .CONN_ESTABLISHED )
220219 # May not be fast enough to change state so it stays
221220 # commenteed.
222221 # client.close()
223- # cons = thisproc.connections (kind='all')
222+ # cons = this_proc_connections (kind='all')
224223 # self.assertEqual(len(cons), 1)
225224 # self.assertEqual(cons[0].status, psutil.CONN_CLOSE_WAIT)
226225 finally :
@@ -232,7 +231,7 @@ def test_unix(self):
232231 testfn = self .get_testfn ()
233232 server , client = unix_socketpair (testfn )
234233 try :
235- cons = thisproc . connections (kind = 'unix' )
234+ cons = this_proc_connections (kind = 'unix' )
236235 assert not (cons [0 ].laddr and cons [0 ].raddr ), cons
237236 assert not (cons [1 ].laddr and cons [1 ].raddr ), cons
238237 if NETBSD or FREEBSD :
@@ -258,7 +257,7 @@ def test_unix(self):
258257class TestFilters (ConnectionTestCase ):
259258 def test_filters (self ):
260259 def check (kind , families , types ):
261- for conn in thisproc . connections (kind = kind ):
260+ for conn in this_proc_connections (kind = kind ):
262261 self .assertIn (conn .family , families )
263262 self .assertIn (conn .type , types )
264263 if not SKIP_SYSCONS :
@@ -373,7 +372,7 @@ def check_conn(proc, conn, family, type, laddr, raddr, status, kinds):
373372 tcp6_addr = None
374373 udp6_addr = None
375374
376- for p in thisproc .children ():
375+ for p in psutil . Process () .children ():
377376 cons = p .connections ()
378377 self .assertEqual (len (cons ), 1 )
379378 for conn in cons :
@@ -429,56 +428,56 @@ def check_conn(proc, conn, family, type, laddr, raddr, status, kinds):
429428 def test_count (self ):
430429 with create_sockets ():
431430 # tcp
432- cons = thisproc . connections (kind = 'tcp' )
431+ cons = this_proc_connections (kind = 'tcp' )
433432 self .assertEqual (len (cons ), 2 if supports_ipv6 () else 1 )
434433 for conn in cons :
435434 self .assertIn (conn .family , (AF_INET , AF_INET6 ))
436435 self .assertEqual (conn .type , SOCK_STREAM )
437436 # tcp4
438- cons = thisproc . connections (kind = 'tcp4' )
437+ cons = this_proc_connections (kind = 'tcp4' )
439438 self .assertEqual (len (cons ), 1 )
440439 self .assertEqual (cons [0 ].family , AF_INET )
441440 self .assertEqual (cons [0 ].type , SOCK_STREAM )
442441 # tcp6
443442 if supports_ipv6 ():
444- cons = thisproc . connections (kind = 'tcp6' )
443+ cons = this_proc_connections (kind = 'tcp6' )
445444 self .assertEqual (len (cons ), 1 )
446445 self .assertEqual (cons [0 ].family , AF_INET6 )
447446 self .assertEqual (cons [0 ].type , SOCK_STREAM )
448447 # udp
449- cons = thisproc . connections (kind = 'udp' )
448+ cons = this_proc_connections (kind = 'udp' )
450449 self .assertEqual (len (cons ), 2 if supports_ipv6 () else 1 )
451450 for conn in cons :
452451 self .assertIn (conn .family , (AF_INET , AF_INET6 ))
453452 self .assertEqual (conn .type , SOCK_DGRAM )
454453 # udp4
455- cons = thisproc . connections (kind = 'udp4' )
454+ cons = this_proc_connections (kind = 'udp4' )
456455 self .assertEqual (len (cons ), 1 )
457456 self .assertEqual (cons [0 ].family , AF_INET )
458457 self .assertEqual (cons [0 ].type , SOCK_DGRAM )
459458 # udp6
460459 if supports_ipv6 ():
461- cons = thisproc . connections (kind = 'udp6' )
460+ cons = this_proc_connections (kind = 'udp6' )
462461 self .assertEqual (len (cons ), 1 )
463462 self .assertEqual (cons [0 ].family , AF_INET6 )
464463 self .assertEqual (cons [0 ].type , SOCK_DGRAM )
465464 # inet
466- cons = thisproc . connections (kind = 'inet' )
465+ cons = this_proc_connections (kind = 'inet' )
467466 self .assertEqual (len (cons ), 4 if supports_ipv6 () else 2 )
468467 for conn in cons :
469468 self .assertIn (conn .family , (AF_INET , AF_INET6 ))
470469 self .assertIn (conn .type , (SOCK_STREAM , SOCK_DGRAM ))
471470 # inet6
472471 if supports_ipv6 ():
473- cons = thisproc . connections (kind = 'inet6' )
472+ cons = this_proc_connections (kind = 'inet6' )
474473 self .assertEqual (len (cons ), 2 )
475474 for conn in cons :
476475 self .assertEqual (conn .family , AF_INET6 )
477476 self .assertIn (conn .type , (SOCK_STREAM , SOCK_DGRAM ))
478477 # Skipped on BSD becayse by default the Python process
479478 # creates a UNIX socket to '/var/run/log'.
480479 if HAS_CONNECTIONS_UNIX and not (FREEBSD or NETBSD ):
481- cons = thisproc . connections (kind = 'unix' )
480+ cons = this_proc_connections (kind = 'unix' )
482481 self .assertEqual (len (cons ), 3 )
483482 for conn in cons :
484483 self .assertEqual (conn .family , AF_UNIX )
0 commit comments