mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-04-29 14:59:39 -04:00
test: add interface_ipc_cli.py testing bitcoin-cli -ipcconnect
Co-authored-by: Sjors Provoost <sjors@sprovoost.nl>
This commit is contained in:
parent
113b46d310
commit
9ff5bc7565
6 changed files with 107 additions and 3 deletions
|
@ -26,6 +26,7 @@ function(create_test_config)
|
|||
set_configure_variable(WITH_ZMQ ENABLE_ZMQ)
|
||||
set_configure_variable(ENABLE_EXTERNAL_SIGNER ENABLE_EXTERNAL_SIGNER)
|
||||
set_configure_variable(WITH_USDT ENABLE_USDT_TRACEPOINTS)
|
||||
set_configure_variable(ENABLE_IPC ENABLE_IPC)
|
||||
|
||||
configure_file(config.ini.in config.ini USE_SOURCE_PERMISSIONS @ONLY)
|
||||
endfunction()
|
||||
|
|
|
@ -26,3 +26,4 @@ RPCAUTH=@abs_top_srcdir@/share/rpcauth/rpcauth.py
|
|||
@ENABLE_ZMQ_TRUE@ENABLE_ZMQ=true
|
||||
@ENABLE_EXTERNAL_SIGNER_TRUE@ENABLE_EXTERNAL_SIGNER=true
|
||||
@ENABLE_USDT_TRACEPOINTS_TRUE@ENABLE_USDT_TRACEPOINTS=true
|
||||
@ENABLE_IPC_TRUE@ENABLE_IPC=true
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
|
||||
from decimal import Decimal
|
||||
import re
|
||||
import subprocess
|
||||
|
||||
from test_framework.blocktools import COINBASE_MATURITY
|
||||
from test_framework.netutil import test_ipv6_local
|
||||
|
@ -400,6 +401,14 @@ class TestBitcoinCli(BitcoinTestFramework):
|
|||
self.log.info("Test that only one of -addrinfo, -generate, -getinfo, -netinfo may be specified at a time")
|
||||
assert_raises_process_error(1, "Only one of -getinfo, -netinfo may be specified", self.nodes[0].cli('-getinfo', '-netinfo').send_cli)
|
||||
|
||||
if not self.is_ipc_enabled():
|
||||
self.log.info("Test bitcoin-cli -ipcconnect triggers error if not built with IPC support")
|
||||
args = [self.binary_paths.bitcoincli, "-ipcconnect=unix", "-getinfo"]
|
||||
result = subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
|
||||
assert_equal(result.stdout, "error: bitcoin-cli was not built with IPC support\n")
|
||||
assert_equal(result.stderr, None)
|
||||
assert_equal(result.returncode, 1)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
TestBitcoinCli(__file__).main()
|
||||
|
|
81
test/functional/interface_ipc_cli.py
Executable file
81
test/functional/interface_ipc_cli.py
Executable file
|
@ -0,0 +1,81 @@
|
|||
#!/usr/bin/env python3
|
||||
# Copyright (c) 2025 The Bitcoin Core developers
|
||||
# Distributed under the MIT software license, see the accompanying
|
||||
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
"""Test IPC with bitcoin-cli"""
|
||||
|
||||
from test_framework.test_framework import BitcoinTestFramework
|
||||
from test_framework.util import (
|
||||
assert_equal,
|
||||
rpc_port
|
||||
)
|
||||
|
||||
import subprocess
|
||||
import tempfile
|
||||
|
||||
class TestBitcoinIpcCli(BitcoinTestFramework):
|
||||
def set_test_params(self):
|
||||
self.setup_clean_chain = True
|
||||
self.num_nodes = 1
|
||||
|
||||
def skip_test_if_missing_module(self):
|
||||
self.skip_if_no_ipc()
|
||||
|
||||
def setup_nodes(self):
|
||||
# Always run IPC node binary
|
||||
self.binary_paths.bitcoind = self.binary_paths.bitcoin_node
|
||||
|
||||
# Work around default CI path exceeding maximum socket path length.
|
||||
# On Linux sun_path is 108 bytes, on macOS it's only 104. Includes
|
||||
# null terminator.
|
||||
self.socket_path = self.options.tmpdir + "/node0/regtest/node.sock"
|
||||
if len(self.socket_path.encode('utf-8')) < 104:
|
||||
self.extra_args = [["-ipcbind=unix"]]
|
||||
self.default_socket = True
|
||||
else:
|
||||
self.socket_path = tempfile.mktemp()
|
||||
self.extra_args = [[f"-ipcbind=unix:{self.socket_path}"]]
|
||||
self.default_socket = False
|
||||
super().setup_nodes()
|
||||
|
||||
def test_cli(self, args, error=None):
|
||||
# Intentionally set wrong RPC password so only IPC not HTTP connections work
|
||||
args = [self.binary_paths.bitcoincli, f"-datadir={self.nodes[0].datadir_path}", "-rpcpassword=wrong"] + args + ["echo", "foo"]
|
||||
result = subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
|
||||
if error:
|
||||
assert_equal(result.stdout, error)
|
||||
else:
|
||||
assert_equal(result.stdout, '[\n "foo"\n]\n')
|
||||
assert_equal(result.stderr, None)
|
||||
assert_equal(result.returncode, 1 if error else 0)
|
||||
|
||||
def run_test(self):
|
||||
if not self.default_socket:
|
||||
self.log.info("Skipping a few checks because temporary directory path is too long")
|
||||
|
||||
http_auth_error = "error: Authorization failed: Incorrect rpcuser or rpcpassword\n"
|
||||
http_connect_error = f"error: timeout on transient error: Could not connect to the server 127.0.0.1:{rpc_port(self.nodes[0].index)}\n\nMake sure the bitcoind server is running and that you are connecting to the correct RPC port.\nUse \"bitcoin-cli -help\" for more info.\n"
|
||||
http_ipc_error = "error: -rpcconnect and -ipcconnect options cannot both be enabled\n"
|
||||
ipc_connect_error = "error: Connection refused\n\nProbably bitcoin-node is not running or not listening on a unix socket. Can be started with:\n\n bitcoin-node -chain=regtest -ipcbind=unix\n"
|
||||
|
||||
for started in (True, False):
|
||||
auto_error = None if started else http_connect_error
|
||||
http_error = http_auth_error if started else http_connect_error
|
||||
ipc_error = None if started else ipc_connect_error
|
||||
|
||||
if self.default_socket:
|
||||
self.test_cli([], auto_error)
|
||||
self.test_cli(["-rpcconnect=127.0.0.1"], http_error)
|
||||
self.test_cli(["-ipcconnect=auto"], auto_error)
|
||||
self.test_cli(["-ipcconnect=auto", "-rpcconnect=127.0.0.1"], http_error)
|
||||
self.test_cli(["-ipcconnect=unix"], ipc_error)
|
||||
|
||||
self.test_cli([f"-ipcconnect=unix:{self.socket_path}"], ipc_error)
|
||||
self.test_cli(["-noipcconnect"], http_error)
|
||||
self.test_cli(["-ipcconnect=unix", "-rpcconnect=127.0.0.1"], http_ipc_error)
|
||||
|
||||
self.stop_node(0)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
TestBitcoinIpcCli(__file__).main()
|
|
@ -298,13 +298,15 @@ class BitcoinTestFramework(metaclass=BitcoinTestMetaClass):
|
|||
"bitcoin-chainstate": ("bitcoinchainstate", "BITCOINCHAINSTATE"),
|
||||
"bitcoin-wallet": ("bitcoinwallet", "BITCOINWALLET"),
|
||||
}
|
||||
for binary, [attribute_name, env_variable_name] in binaries.items():
|
||||
default_filename = os.path.join(
|
||||
def binary_path(binary):
|
||||
return os.path.join(
|
||||
self.config["environment"]["BUILDDIR"],
|
||||
"bin",
|
||||
binary + self.config["environment"]["EXEEXT"],
|
||||
)
|
||||
setattr(paths, attribute_name, os.getenv(env_variable_name, default=default_filename))
|
||||
for binary, [attribute_name, env_variable_name] in binaries.items():
|
||||
setattr(paths, attribute_name, os.getenv(env_variable_name) or binary_path(binary))
|
||||
paths.bitcoin_node = binary_path("bitcoin-node")
|
||||
return paths
|
||||
|
||||
def get_binaries(self, bin_dir=None):
|
||||
|
@ -1037,6 +1039,11 @@ class BitcoinTestFramework(metaclass=BitcoinTestMetaClass):
|
|||
if not self.is_cli_compiled():
|
||||
raise SkipTest("bitcoin-cli has not been compiled.")
|
||||
|
||||
def skip_if_no_ipc(self):
|
||||
"""Skip the running test if ipc is not enabled."""
|
||||
if not self.is_ipc_enabled():
|
||||
raise SkipTest("ipc is not enabled.")
|
||||
|
||||
def skip_if_no_previous_releases(self):
|
||||
"""Skip the running test if previous releases are not available."""
|
||||
if not self.has_previous_releases():
|
||||
|
@ -1099,5 +1106,9 @@ class BitcoinTestFramework(metaclass=BitcoinTestMetaClass):
|
|||
"""Checks whether the wallet module was compiled with BDB support."""
|
||||
return self.config["components"].getboolean("USE_BDB")
|
||||
|
||||
def is_ipc_enabled(self):
|
||||
"""Checks whether ipc is enabled."""
|
||||
return self.config["components"].getboolean("ENABLE_IPC")
|
||||
|
||||
def has_blockfile(self, node, filenum: str):
|
||||
return (node.blocks_path/ f"blk{filenum}.dat").is_file()
|
||||
|
|
|
@ -408,6 +408,7 @@ BASE_SCRIPTS = [
|
|||
'rpc_help.py',
|
||||
'p2p_handshake.py',
|
||||
'p2p_handshake.py --v2transport',
|
||||
'interface_ipc_cli.py',
|
||||
'feature_dirsymlinks.py',
|
||||
'feature_help.py',
|
||||
'feature_shutdown.py',
|
||||
|
|
Loading…
Add table
Reference in a new issue