Skip to content

Commit ec19206

Browse files
committed
test_misc.py: work around socket path name too long on Linux
When running the integration test suite in a deeply nested directory tree, the path name of the Unix domain socket might be longer than can fit in a struct sockaddr_un. On Linux, we can use the /proc/self/cwd trick to shorten the path name. Changelog-Fixed: test_misc.py tests no longer fail when run in a deeply nested directory on Linux.
1 parent fcd92fe commit ec19206

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

tests/test_misc.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -818,7 +818,12 @@ def test_address(node_factory):
818818

819819
# Now test UNIX domain binding
820820
l1.stop()
821-
l1.daemon.opts['bind-addr'] = os.path.join(l1.daemon.lightning_dir, TEST_NETWORK, "sock")
821+
bind_addr = os.path.join(l1.daemon.lightning_dir, TEST_NETWORK, "sock")
822+
if len(bind_addr) >= 108 and os.uname()[0] == "Linux":
823+
bind_addr = os.path.join('/proc/self/cwd',
824+
os.path.relpath(node_factory.directory, os.path.dirname(bind_addr)),
825+
os.path.relpath(bind_addr, node_factory.directory))
826+
l1.daemon.opts['bind-addr'] = bind_addr
822827
l1.start()
823828

824829
# Test dev-allow-localhost
@@ -874,12 +879,21 @@ def test_listconfigs_plugins(node_factory, bitcoind, chainparams):
874879
assert [p['active'] for p in plugins if p['name'].endswith('offers')] == [True]
875880

876881

882+
def connect_unix(socket_path: str):
883+
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
884+
try:
885+
sock.connect(socket_path)
886+
except OSError as err:
887+
if err.args[0] == 'AF_UNIX path too long' and os.uname()[0] == 'Linux':
888+
sock.connect(os.path.join('/proc/self/cwd', os.path.relpath(socket_path)))
889+
return sock
890+
891+
877892
def test_multirpc(node_factory):
878893
"""Test that we can do multiple RPC without waiting for response"""
879894
l1 = node_factory.get_node()
880895

881-
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
882-
sock.connect(l1.rpc.socket_path)
896+
sock = connect_unix(l1.rpc.socket_path)
883897

884898
commands = [
885899
b'{"id":1,"jsonrpc":"2.0","method":"listpeers","params":[]}',
@@ -905,8 +919,7 @@ def test_multiplexed_rpc(node_factory):
905919
"""Test that we can do multiple RPCs which exit in different orders"""
906920
l1 = node_factory.get_node()
907921

908-
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
909-
sock.connect(l1.rpc.socket_path)
922+
sock = connect_unix(l1.rpc.socket_path)
910923

911924
# Neighbouring ones may be in or out of order.
912925
commands = [
@@ -936,8 +949,7 @@ def test_malformed_rpc(node_factory):
936949
"""Test that we get a correct response to malformed RPC commands"""
937950
l1 = node_factory.get_node()
938951

939-
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
940-
sock.connect(l1.rpc.socket_path)
952+
sock = connect_unix(l1.rpc.socket_path)
941953

942954
# No ID
943955
sock.sendall(b'{"jsonrpc":"2.0","method":"getinfo","params":[]}')
@@ -2023,8 +2035,7 @@ def test_check_command(node_factory):
20232035
host='x', port="abcd")
20242036

20252037
# FIXME: python wrapper doesn't let us test array params.
2026-
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
2027-
sock.connect(l1.rpc.socket_path)
2038+
sock = connect_unix(l1.rpc.socket_path)
20282039

20292040
sock.sendall(b'{"id":1, "jsonrpc":"2.0","method":"check","params":["help"]}')
20302041
obj, _ = l1.rpc._readobj(sock, b'')

0 commit comments

Comments
 (0)