Skip to content

Commit 82a4375

Browse files
committed
improve tests reliability
1 parent 89eac06 commit 82a4375

9 files changed

+541
-499
lines changed

MANIFEST.in

+1
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ include psutil/tests/test_misc.py
164164
include psutil/tests/test_osx.py
165165
include psutil/tests/test_posix.py
166166
include psutil/tests/test_process.py
167+
include psutil/tests/test_process_all.py
167168
include psutil/tests/test_sunos.py
168169
include psutil/tests/test_system.py
169170
include psutil/tests/test_testutils.py

Makefile

+4
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ test-process: ## Run process-related API tests.
140140
${MAKE} build
141141
$(TEST_PREFIX) $(PYTHON) $(TSCRIPT) $(ARGS) psutil/tests/test_process.py
142142

143+
test-process-all: ## Run tests which iterate over all process PIDs.
144+
${MAKE} build
145+
$(TEST_PREFIX) $(PYTHON) $(TSCRIPT) $(ARGS) psutil/tests/test_process_all.py
146+
143147
test-system: ## Run system-related API tests.
144148
${MAKE} build
145149
$(TEST_PREFIX) $(PYTHON) $(TSCRIPT) $(ARGS) psutil/tests/test_system.py

psutil/tests/__init__.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
from psutil import SUNOS
4848
from psutil import WINDOWS
4949
from psutil._common import bytes2human
50+
from psutil._common import debug
5051
from psutil._common import memoize
5152
from psutil._common import print_color
5253
from psutil._common import supports_ipv6
@@ -105,7 +106,7 @@
105106
# sync primitives
106107
'call_until', 'wait_for_pid', 'wait_for_file',
107108
# network
108-
'check_net_address',
109+
'check_net_address', 'filter_proc_connections',
109110
'get_free_port', 'bind_socket', 'bind_unix_socket', 'tcp_socketpair',
110111
'unix_socketpair', 'create_sockets',
111112
# compat
@@ -1871,6 +1872,20 @@ def check_status(conn):
18711872
check_status(conn)
18721873

18731874

1875+
def filter_proc_connections(cons):
1876+
"""Our process may start with some open UNIX sockets which are not
1877+
initialized by us, invalidating unit tests.
1878+
"""
1879+
new = []
1880+
for conn in cons:
1881+
if POSIX and conn.family == socket.AF_UNIX:
1882+
if MACOS and "/syslog" in conn.raddr:
1883+
debug("skipping %s" % str(conn))
1884+
continue
1885+
new.append(conn)
1886+
return new
1887+
1888+
18741889
# ===================================================================
18751890
# --- compatibility
18761891
# ===================================================================

psutil/tests/test_connections.py

+29-30
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
from psutil.tests import bind_unix_socket
3636
from psutil.tests import check_connection_ntuple
3737
from psutil.tests import create_sockets
38+
from psutil.tests import filter_proc_connections
3839
from psutil.tests import reap_children
3940
from psutil.tests import retry_on_failure
4041
from psutil.tests import serialrun
@@ -44,26 +45,24 @@
4445
from psutil.tests import wait_for_file
4546

4647

47-
thisproc = psutil.Process()
4848
SOCK_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
5259
class 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):
258257
class 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

Comments
 (0)