summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2020-04-24 18:28:16 +0200
committerGiampaolo Rodola <g.rodola@gmail.com>2020-04-24 18:28:16 +0200
commita5e4b1c529dda4cb7df2abb679f66d4508ed2bf2 (patch)
tree72781979d3d34dd3ceeddfa373bcc616b2042f0c
parent47d3dd3fca440bf3bc90e8a0f60ce60f4a0f8b36 (diff)
downloadpsutil-a5e4b1c529dda4cb7df2abb679f66d4508ed2bf2.tar.gz
get rid of unix_socket_path
-rw-r--r--psutil/tests/__init__.py28
-rwxr-xr-xpsutil/tests/test_connections.py85
-rwxr-xr-xpsutil/tests/test_testutils.py49
-rwxr-xr-xpsutil/tests/test_unicode.py59
4 files changed, 98 insertions, 123 deletions
diff --git a/psutil/tests/__init__.py b/psutil/tests/__init__.py
index 1e0d4437..1fd3ade7 100644
--- a/psutil/tests/__init__.py
+++ b/psutil/tests/__init__.py
@@ -99,8 +99,8 @@ __all__ = [
'call_until', 'wait_for_pid', 'wait_for_file',
# network
'check_net_address',
- 'get_free_port', 'unix_socket_path', 'bind_socket', 'bind_unix_socket',
- 'tcp_socketpair', 'unix_socketpair', 'create_sockets',
+ 'get_free_port', 'bind_socket', 'bind_unix_socket', 'tcp_socketpair',
+ 'unix_socketpair', 'create_sockets',
# compat
'reload_module', 'import_module_by_path',
# others
@@ -989,22 +989,6 @@ def get_free_port(host='127.0.0.1'):
return sock.getsockname()[1]
-@contextlib.contextmanager
-def unix_socket_path(suffix=""):
- """A context manager which returns a non-existent file name
- and tries to delete it on exit.
- """
- assert psutil.POSIX
- path = get_testfn(suffix=suffix)
- try:
- yield path
- finally:
- try:
- os.unlink(path)
- except OSError:
- pass
-
-
def bind_socket(family=AF_INET, type=SOCK_STREAM, addr=None):
"""Binds a generic socket."""
if addr is None and family in (AF_INET, AF_INET6):
@@ -1095,8 +1079,8 @@ def create_sockets():
socks.append(bind_socket(socket.AF_INET6, socket.SOCK_STREAM))
socks.append(bind_socket(socket.AF_INET6, socket.SOCK_DGRAM))
if POSIX and HAS_CONNECTIONS_UNIX:
- fname1 = unix_socket_path().__enter__()
- fname2 = unix_socket_path().__enter__()
+ fname1 = get_testfn()
+ fname2 = get_testfn()
s1, s2 = unix_socketpair(fname1)
s3 = bind_unix_socket(fname2, type=socket.SOCK_DGRAM)
# self.addCleanup(safe_rmpath, fname1)
@@ -1107,10 +1091,6 @@ def create_sockets():
finally:
for s in socks:
s.close()
- if fname1 is not None:
- safe_rmpath(fname1)
- if fname2 is not None:
- safe_rmpath(fname2)
def check_net_address(addr, family):
diff --git a/psutil/tests/test_connections.py b/psutil/tests/test_connections.py
index 148728bf..d1bb4931 100755
--- a/psutil/tests/test_connections.py
+++ b/psutil/tests/test_connections.py
@@ -46,7 +46,6 @@ from psutil.tests import SKIP_SYSCONS
from psutil.tests import tcp_socketpair
from psutil.tests import TRAVIS
from psutil.tests import unittest
-from psutil.tests import unix_socket_path
from psutil.tests import unix_socketpair
from psutil.tests import wait_for_file
@@ -267,19 +266,17 @@ class TestUnconnectedSockets(Base, unittest.TestCase):
@unittest.skipIf(not POSIX, 'POSIX only')
def test_unix_tcp(self):
- with unix_socket_path() as name:
- with closing(bind_unix_socket(name, type=SOCK_STREAM)) as sock:
- conn = self.check_socket(sock)
- assert not conn.raddr
- self.assertEqual(conn.status, psutil.CONN_NONE)
+ with closing(bind_unix_socket(get_testfn(), type=SOCK_STREAM)) as sock:
+ conn = self.check_socket(sock)
+ assert not conn.raddr
+ self.assertEqual(conn.status, psutil.CONN_NONE)
@unittest.skipIf(not POSIX, 'POSIX only')
def test_unix_udp(self):
- with unix_socket_path() as name:
- with closing(bind_unix_socket(name, type=SOCK_STREAM)) as sock:
- conn = self.check_socket(sock)
- assert not conn.raddr
- self.assertEqual(conn.status, psutil.CONN_NONE)
+ with closing(bind_unix_socket(get_testfn(), type=SOCK_STREAM)) as sock:
+ conn = self.check_socket(sock)
+ assert not conn.raddr
+ self.assertEqual(conn.status, psutil.CONN_NONE)
class TestConnectedSocket(Base, unittest.TestCase):
@@ -311,39 +308,39 @@ class TestConnectedSocket(Base, unittest.TestCase):
@unittest.skipIf(not POSIX, 'POSIX only')
def test_unix(self):
- with unix_socket_path() as name:
- server, client = unix_socketpair(name)
- try:
- cons = thisproc.connections(kind='unix')
- assert not (cons[0].laddr and cons[0].raddr)
- assert not (cons[1].laddr and cons[1].raddr)
- if NETBSD or FREEBSD:
- # On NetBSD creating a UNIX socket will cause
- # a UNIX connection to /var/run/log.
- cons = [c for c in cons if c.raddr != '/var/run/log']
- if CIRRUS:
- cons = [c for c in cons if c.fd in
- (server.fileno(), client.fileno())]
- self.assertEqual(len(cons), 2, msg=cons)
- if LINUX or FREEBSD or SUNOS:
- # remote path is never set
- self.assertEqual(cons[0].raddr, "")
- self.assertEqual(cons[1].raddr, "")
- # one local address should though
- self.assertEqual(name, cons[0].laddr or cons[1].laddr)
- elif OPENBSD:
- # No addresses whatsoever here.
- for addr in (cons[0].laddr, cons[0].raddr,
- cons[1].laddr, cons[1].raddr):
- self.assertEqual(addr, "")
- else:
- # On other systems either the laddr or raddr
- # of both peers are set.
- self.assertEqual(cons[0].laddr or cons[1].laddr, name)
- self.assertEqual(cons[0].raddr or cons[1].raddr, name)
- finally:
- server.close()
- client.close()
+ testfn = get_testfn()
+ server, client = unix_socketpair(testfn)
+ try:
+ cons = thisproc.connections(kind='unix')
+ assert not (cons[0].laddr and cons[0].raddr)
+ assert not (cons[1].laddr and cons[1].raddr)
+ if NETBSD or FREEBSD:
+ # On NetBSD creating a UNIX socket will cause
+ # a UNIX connection to /var/run/log.
+ cons = [c for c in cons if c.raddr != '/var/run/log']
+ if CIRRUS:
+ cons = [c for c in cons if c.fd in
+ (server.fileno(), client.fileno())]
+ self.assertEqual(len(cons), 2, msg=cons)
+ if LINUX or FREEBSD or SUNOS:
+ # remote path is never set
+ self.assertEqual(cons[0].raddr, "")
+ self.assertEqual(cons[1].raddr, "")
+ # one local address should though
+ self.assertEqual(testfn, cons[0].laddr or cons[1].laddr)
+ elif OPENBSD:
+ # No addresses whatsoever here.
+ for addr in (cons[0].laddr, cons[0].raddr,
+ cons[1].laddr, cons[1].raddr):
+ self.assertEqual(addr, "")
+ else:
+ # On other systems either the laddr or raddr
+ # of both peers are set.
+ self.assertEqual(cons[0].laddr or cons[1].laddr, testfn)
+ self.assertEqual(cons[0].raddr or cons[1].raddr, testfn)
+ finally:
+ server.close()
+ client.close()
class TestFilters(Base, unittest.TestCase):
diff --git a/psutil/tests/test_testutils.py b/psutil/tests/test_testutils.py
index d434ba5e..55c5d3df 100755
--- a/psutil/tests/test_testutils.py
+++ b/psutil/tests/test_testutils.py
@@ -46,7 +46,6 @@ from psutil.tests import safe_rmpath
from psutil.tests import tcp_socketpair
from psutil.tests import TestMemoryLeak
from psutil.tests import unittest
-from psutil.tests import unix_socket_path
from psutil.tests import unix_socketpair
from psutil.tests import wait_for_file
from psutil.tests import wait_for_pid
@@ -255,19 +254,19 @@ class TestNetUtils(unittest.TestCase):
@unittest.skipIf(not POSIX, "POSIX only")
def test_bind_unix_socket(self):
- with unix_socket_path() as name:
- sock = bind_unix_socket(name)
- with contextlib.closing(sock):
- self.assertEqual(sock.family, socket.AF_UNIX)
- self.assertEqual(sock.type, socket.SOCK_STREAM)
- self.assertEqual(sock.getsockname(), name)
- assert os.path.exists(name)
- assert stat.S_ISSOCK(os.stat(name).st_mode)
+ name = get_testfn()
+ sock = bind_unix_socket(name)
+ with contextlib.closing(sock):
+ self.assertEqual(sock.family, socket.AF_UNIX)
+ self.assertEqual(sock.type, socket.SOCK_STREAM)
+ self.assertEqual(sock.getsockname(), name)
+ assert os.path.exists(name)
+ assert stat.S_ISSOCK(os.stat(name).st_mode)
# UDP
- with unix_socket_path() as name:
- sock = bind_unix_socket(name, type=socket.SOCK_DGRAM)
- with contextlib.closing(sock):
- self.assertEqual(sock.type, socket.SOCK_DGRAM)
+ name = get_testfn()
+ sock = bind_unix_socket(name, type=socket.SOCK_DGRAM)
+ with contextlib.closing(sock):
+ self.assertEqual(sock.type, socket.SOCK_DGRAM)
def tcp_tcp_socketpair(self):
addr = ("127.0.0.1", get_free_port())
@@ -287,18 +286,18 @@ class TestNetUtils(unittest.TestCase):
p = psutil.Process()
num_fds = p.num_fds()
assert not p.connections(kind='unix')
- with unix_socket_path() as name:
- server, client = unix_socketpair(name)
- try:
- assert os.path.exists(name)
- assert stat.S_ISSOCK(os.stat(name).st_mode)
- self.assertEqual(p.num_fds() - num_fds, 2)
- self.assertEqual(len(p.connections(kind='unix')), 2)
- self.assertEqual(server.getsockname(), name)
- self.assertEqual(client.getpeername(), name)
- finally:
- client.close()
- server.close()
+ name = get_testfn()
+ server, client = unix_socketpair(name)
+ try:
+ assert os.path.exists(name)
+ assert stat.S_ISSOCK(os.stat(name).st_mode)
+ self.assertEqual(p.num_fds() - num_fds, 2)
+ self.assertEqual(len(p.connections(kind='unix')), 2)
+ self.assertEqual(server.getsockname(), name)
+ self.assertEqual(client.getpeername(), name)
+ finally:
+ client.close()
+ server.close()
def test_create_sockets(self):
with create_sockets() as socks:
diff --git a/psutil/tests/test_unicode.py b/psutil/tests/test_unicode.py
index 4dcd3594..6fbaa43f 100755
--- a/psutil/tests/test_unicode.py
+++ b/psutil/tests/test_unicode.py
@@ -107,7 +107,6 @@ from psutil.tests import TESTFN_PREFIX
from psutil.tests import TRAVIS
from psutil.tests import UNICODE_SUFFIX
from psutil.tests import unittest
-from psutil.tests import unix_socket_path
import psutil
@@ -226,20 +225,20 @@ class _BaseFSAPIsTests(object):
@unittest.skipIf(not POSIX, "POSIX only")
def test_proc_connections(self):
suffix = os.path.basename(self.funky_name)
- with unix_socket_path(suffix=suffix) as name:
- try:
- sock = bind_unix_socket(name)
- except UnicodeEncodeError:
- if PY3:
- raise
- else:
- raise unittest.SkipTest("not supported")
- with closing(sock):
- conn = psutil.Process().connections('unix')[0]
- self.assertIsInstance(conn.laddr, str)
- # AF_UNIX addr not set on OpenBSD
- if not OPENBSD and not CIRRUS: # XXX
- self.assertEqual(conn.laddr, name)
+ name = get_testfn(suffix=suffix)
+ try:
+ sock = bind_unix_socket(name)
+ except UnicodeEncodeError:
+ if PY3:
+ raise
+ else:
+ raise unittest.SkipTest("not supported")
+ with closing(sock):
+ conn = psutil.Process().connections('unix')[0]
+ self.assertIsInstance(conn.laddr, str)
+ # AF_UNIX addr not set on OpenBSD
+ if not OPENBSD and not CIRRUS: # XXX
+ self.assertEqual(conn.laddr, name)
@unittest.skipIf(not POSIX, "POSIX only")
@unittest.skipIf(not HAS_CONNECTIONS_UNIX, "can't list UNIX sockets")
@@ -252,21 +251,21 @@ class _BaseFSAPIsTests(object):
raise ValueError("connection not found")
suffix = os.path.basename(self.funky_name)
- with unix_socket_path(suffix=suffix) as name:
- try:
- sock = bind_unix_socket(name)
- except UnicodeEncodeError:
- if PY3:
- raise
- else:
- raise unittest.SkipTest("not supported")
- with closing(sock):
- cons = psutil.net_connections(kind='unix')
- # AF_UNIX addr not set on OpenBSD
- if not OPENBSD:
- conn = find_sock(cons)
- self.assertIsInstance(conn.laddr, str)
- self.assertEqual(conn.laddr, name)
+ name = get_testfn(suffix=suffix)
+ try:
+ sock = bind_unix_socket(name)
+ except UnicodeEncodeError:
+ if PY3:
+ raise
+ else:
+ raise unittest.SkipTest("not supported")
+ with closing(sock):
+ cons = psutil.net_connections(kind='unix')
+ # AF_UNIX addr not set on OpenBSD
+ if not OPENBSD:
+ conn = find_sock(cons)
+ self.assertIsInstance(conn.laddr, str)
+ self.assertEqual(conn.laddr, name)
def test_disk_usage(self):
dname = self.funky_name + "2"