test: make feature_bind_port_(discover|externalip).py auto-detect the skip condition
What changed, and why it matters
This commit only changes Bitcoin Core's internal functional test scripts. It removes a manual command-line flag that testers had to pass to skip certain network-binding tests, and instead makes the tests automatically detect whether the required network addresses are available. It also adds a cleanup helper for test nodes that fail to start. There is no change to the Bitcoin node software that users run, no change to consensus rules, networking protocol, wallet handling, or any security-critical code path. It is purely a developer-facing testing improvement.
No security action needed. This is a normal test-framework refactor; review as part of routine QA tooling maintenance.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch modifies three files under test/functional/. feature_bind_port_discover.py and feature_bind_port_externalip.py now auto-skip when bitcoind cannot bind to the routable addresses 1.1.1.5/1111:1111::5, rather than requiring –ihave1111and2222/–ihave1111 flags. test_framework.py gains cleanup_partially_started_nodes() to stop or kill leftover test-node processes after a FailedToStartError. No production code is touched.
Changed components
test/functional/feature_bind_port_discover.pytest/functional/feature_bind_port_externalip.pytest/functional/test_framework/test_framework.pyInspect captured patch +74 / −41
diff --git a/test/functional/feature_bind_port_discover.py b/test/functional/feature_bind_port_discover.py
index 613b111b..407b1364 100755
--- a/test/functional/feature_bind_port_discover.py
+++ b/test/functional/feature_bind_port_discover.py
@@ -7,6 +7,9 @@ Test that -discover does not add all interfaces' addresses if we listen on only
"""
from test_framework.test_framework import BitcoinTestFramework, SkipTest
+from test_framework.test_node import (
+ FailedToStartError,
+)
from test_framework.util import (
assert_equal,
assert_not_equal,
@@ -20,18 +23,14 @@ from test_framework.util import (
# Linux:
# First find your interfaces: ip addr show
# Then use your actual interface names (replace INTERFACE_NAME with yours):
-# ip addr add 1.1.1.1/32 dev INTERFACE_NAME && ip addr add 2.2.2.2/32 dev INTERFACE_NAME # to set up
-# ip addr del 1.1.1.1/32 dev INTERFACE_NAME && ip addr del 2.2.2.2/32 dev INTERFACE_NAME # to remove it
-#
-# macOS:
-# ifconfig en0 alias 1.1.1.1 && ifconfig en0 alias 2.2.2.2 # to set up
-# ifconfig en0 1.1.1.1 -alias && ifconfig en0 2.2.2.2 -alias # to remove it, after the test
+# ip addr add 1.1.1.5/32 dev INTERFACE_NAME && ip addr add 1111:1111::5/128 dev INTERFACE_NAME # to set up
+# ip addr del 1.1.1.5/32 dev INTERFACE_NAME && ip addr del 1111:1111::5/128 dev INTERFACE_NAME # to remove it
#
-# FreeBSD:
-# ifconfig em0 1.1.1.1/32 alias && ifconfig wlan0 2.2.2.2/32 alias # to set up
-# ifconfig em0 1.1.1.1 -alias && ifconfig wlan0 2.2.2.2 -alias # to remove it, after the test
-ADDR1 = '1.1.1.1'
-ADDR2 = '2.2.2.2'
+# FreeBSD and MacOS:
+# ifconfig INTERFACE_NAME 1.1.1.5/32 alias && ifconfig INTERFACE_NAME inet6 1111:1111::5/128 alias # to set up
+# ifconfig INTERFACE_NAME 1.1.1.5 -alias && ifconfig INTERFACE_NAME inet6 1111:1111::5 -alias # to remove it, after the test
+ADDR1 = '1.1.1.5' # This and the address below are set in the CI environment, don't change it just here (keep them in sync).
+ADDR2 = '1111:1111::5'
class BindPortDiscoverTest(BitcoinTestFramework):
def set_test_params(self):
@@ -69,19 +68,17 @@ class BindPortDiscoverTest(BitcoinTestFramework):
# False. We do not want any -bind= thus set has_explicit_bind to True.
for node in self.nodes:
node.has_explicit_bind = True
- self.start_nodes()
-
- def add_options(self, parser):
- parser.add_argument(
- "--ihave1111and2222", action='store_true', dest="ihave1111and2222",
- help=f"Run the test, assuming {ADDR1} and {ADDR2} are configured on the machine",
- default=False)
- def skip_test_if_missing_module(self):
- if not self.options.ihave1111and2222:
- raise SkipTest(
- f"To run this test make sure that {ADDR1} and {ADDR2} (routable addresses) are "
- "assigned to the interfaces on this machine and rerun with --ihave1111and2222")
+ try:
+ self.start_nodes()
+ except FailedToStartError as e:
+ self.cleanup_partially_started_nodes()
+ if 'Unable to bind to ' in str(e):
+ raise SkipTest(
+ f'To run this test make sure that {ADDR1} and {ADDR2} '
+ '(routable addresses) are assigned to non-loopback '
+ 'interfaces on this machine')
+ raise
def run_test(self):
self.log.info(
diff --git a/test/functional/feature_bind_port_externalip.py b/test/functional/feature_bind_port_externalip.py
index 0e7764db..074797ac 100755
--- a/test/functional/feature_bind_port_externalip.py
+++ b/test/functional/feature_bind_port_externalip.py
@@ -7,17 +7,24 @@ Test that the proper port is used for -externalip=
"""
from test_framework.test_framework import BitcoinTestFramework, SkipTest
+from test_framework.test_node import (
+ FailedToStartError,
+)
from test_framework.util import assert_equal, p2p_port
-# We need to bind to a routable address for this test to exercise the relevant code.
-# To set a routable address on the machine use:
+# We need to bind to a routable address for this test to exercise the relevant
+# code. Those addresses must be on an interface that is UP and is not a loopback
+# interface (IFF_LOOPBACK).
+# To set these routable addresses on the machine, use:
# Linux:
-# ifconfig lo:0 1.1.1.1/32 up # to set up
-# ifconfig lo:0 down # to remove it, after the test
-# FreeBSD:
-# ifconfig lo0 1.1.1.1/32 alias # to set up
-# ifconfig lo0 1.1.1.1 -alias # to remove it, after the test
-ADDR = '1.1.1.1'
+# First find your interfaces: ip addr show
+# Then use your actual interface names (replace INTERFACE_NAME with yours):
+# ip addr add 1.1.1.5/32 dev INTERFACE_NAME # to set up
+# ip addr del 1.1.1.5/32 dev INTERFACE_NAME # to remove it
+# FreeBSD and MacOS:
+# ifconfig INTERFACE_NAME 1.1.1.5/32 alias # to set up
+# ifconfig INTERFACE_NAME 1.1.1.5 -alias # to remove it, after the test
+ADDR = '1.1.1.5' # This is set in the CI environment, don't change it just here (keep them in sync).
# array of tuples [arguments, expected port in localaddresses]
EXPECTED = [
@@ -45,17 +52,25 @@ class BindPortExternalIPTest(BitcoinTestFramework):
self.num_nodes = len(EXPECTED)
self.extra_args = list(map(lambda e: e[0], EXPECTED))
- def add_options(self, parser):
- parser.add_argument(
- "--ihave1111", action='store_true', dest="ihave1111",
- help=f"Run the test, assuming {ADDR} is configured on the machine",
- default=False)
+ def setup_network(self):
+ self.setup_nodes()
- def skip_test_if_missing_module(self):
- if not self.options.ihave1111:
- raise SkipTest(
- f"To run this test make sure that {ADDR} (a routable address) is assigned "
- "to one of the interfaces on this machine and rerun with --ihave1111")
+ def setup_nodes(self):
+ self.add_nodes(self.num_nodes, self.extra_args)
+ # TestNode.start() will add -bind= to extra_args if has_explicit_bind is
+ # False. We do not want any -bind= thus set has_explicit_bind to True.
+ for node in self.nodes:
+ node.has_explicit_bind = True
+ try:
+ self.start_nodes()
+ except FailedToStartError as e:
+ self.cleanup_partially_started_nodes()
+ if 'Unable to bind to ' in str(e):
+ raise SkipTest(
+ f'To run this test make sure that {ADDR} '
+ '(routable address) is assigned to non-loopback '
+ 'interface on this machine')
+ raise
def run_test(self):
self.log.info("Test the proper port is used for -externalip=")
diff --git a/test/functional/test_framework/test_framework.py b/test/functional/test_framework/test_framework.py
index b809e68f..8cc7121f 100755
--- a/test/functional/test_framework/test_framework.py
+++ b/test/functional/test_framework/test_framework.py
@@ -535,6 +535,27 @@ class BitcoinTestFramework(metaclass=BitcoinTestMetaClass):
# Wait for nodes to stop
node.wait_until_stopped()
+ def cleanup_partially_started_nodes(self):
+ """Tear down nodes left running after a failed start_nodes().
+
+ After start_nodes() raises (e.g. FailedToStartError), some nodes may be
+ RPC-connected, some may have a live process without RPC, and some may
+ already have exited. Stop the connected ones cleanly and force-kill the
+ rest so the framework's teardown can proceed.
+ """
+ for node in self.nodes:
+ if not node.running:
+ continue
+ if node.rpc_connected:
+ node.stop_node(wait=node.rpc_timeout)
+ else:
+ node.process.kill()
+ node.process.wait(timeout=node.rpc_timeout)
+ node.process = None
+ node.stdout.close()
+ node.stderr.close()
+ node.running = False
+
def restart_node(self, i, extra_args=None, clear_addrman=False, *, expected_stderr=''):
"""Stop and start a test node"""
self.stop_node(i, expected_stderr=expected_stderr)
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.