Skip to content

Commit be887da

Browse files
committed
test: make feature_bind_port_(discover|externalip).py auto-detect the skip condition
Instead of requiring a run with an explicit `--ihave1111and2222`, detect if the `1.1.1.1` and `2.2.2.2` addresses are set up and if not, then skip the test. To detect whether the addresses are set use `bitcoind` - start it and ask it to bind on them and see if it will error with "Unable to bind". Since this is what the tests do anyway, just start the nodes and see if an exception will be raised like `FailedToStartError` / "Unable to bind". This makes it possible for the CI to run `feature_bind_port_discover.py` and `feature_bind_port_externalip.py` by just setting up the addresses, without having to explicitly provide `--ihave1111and2222`.
1 parent 377b200 commit be887da

File tree

2 files changed

+69
-31
lines changed

2 files changed

+69
-31
lines changed

test/functional/feature_bind_port_discover.py

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
"""
88

99
from test_framework.test_framework import BitcoinTestFramework, SkipTest
10+
from test_framework.test_node import (
11+
FailedToStartError,
12+
)
1013
from test_framework.util import (
1114
assert_equal,
1215
assert_not_equal,
@@ -15,8 +18,8 @@
1518
)
1619

1720
# We need to bind to a routable address for this test to exercise the relevant code
18-
# and also must have another routable address on another interface which must not
19-
# be named "lo" or "lo0".
21+
# and also must have another routable address. Those addresses must be on an interface
22+
# that is UP and is not a loopback interface (IFF_LOOPBACK).
2023
# To set these routable addresses on the machine, use:
2124
# Linux:
2225
# First find your interfaces: ip addr show
@@ -70,19 +73,28 @@ def setup_nodes(self):
7073
# False. We do not want any -bind= thus set has_explicit_bind to True.
7174
for node in self.nodes:
7275
node.has_explicit_bind = True
73-
self.start_nodes()
74-
75-
def add_options(self, parser):
76-
parser.add_argument(
77-
"--ihave1111and2222", action='store_true', dest="ihave1111and2222",
78-
help=f"Run the test, assuming {ADDR1} and {ADDR2} are configured on the machine",
79-
default=False)
8076

81-
def skip_test_if_missing_module(self):
82-
if not self.options.ihave1111and2222:
83-
raise SkipTest(
84-
f"To run this test make sure that {ADDR1} and {ADDR2} (routable addresses) are "
85-
"assigned to the interfaces on this machine and rerun with --ihave1111and2222")
77+
try:
78+
self.start_nodes()
79+
except FailedToStartError as e:
80+
for node in self.nodes:
81+
if node.running:
82+
if node.rpc_connected:
83+
node.stop_node(wait=node.rpc_timeout)
84+
else:
85+
node.process.kill()
86+
node.process.wait(timeout=node.rpc_timeout)
87+
node.process = None
88+
node.stdout.close()
89+
node.stderr.close()
90+
node.running = False
91+
if 'Unable to bind to ' in str(e):
92+
raise SkipTest(
93+
f'To run this test make sure that {ADDR1} and {ADDR2} '
94+
'(routable addresses) are assigned to non-loopback '
95+
'interfaces on this machine')
96+
else:
97+
raise e
8698

8799
def run_test(self):
88100
self.log.info(

test/functional/feature_bind_port_externalip.py

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,23 @@
77
"""
88

99
from test_framework.test_framework import BitcoinTestFramework, SkipTest
10+
from test_framework.test_node import (
11+
FailedToStartError,
12+
)
1013
from test_framework.util import assert_equal, p2p_port
1114

12-
# We need to bind to a routable address for this test to exercise the relevant code.
13-
# To set a routable address on the machine use:
15+
# We need to bind to a routable address for this test to exercise the relevant
16+
# code. Those addresses must be on an interface that is UP and is not a loopback
17+
# interface (IFF_LOOPBACK).
18+
# To set these routable addresses on the machine, use:
1419
# Linux:
15-
# ifconfig lo:0 1.1.1.1/32 up # to set up
16-
# ifconfig lo:0 down # to remove it, after the test
17-
# FreeBSD:
18-
# ifconfig lo0 1.1.1.1/32 alias # to set up
19-
# ifconfig lo0 1.1.1.1 -alias # to remove it, after the test
20+
# First find your interfaces: ip addr show
21+
# Then use your actual interface names (replace INTERFACE_NAME with yours):
22+
# 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
23+
# 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
24+
# FreeBSD and MacOS:
25+
# ifconfig INTERFACE_NAME 1.1.1.1/32 alias # to set up
26+
# ifconfig INTERFACE_NAME 1.1.1.1 -alias # to remove it, after the test
2027
ADDR = '1.1.1.1'
2128

2229
# array of tuples [arguments, expected port in localaddresses]
@@ -45,17 +52,36 @@ def set_test_params(self):
4552
self.num_nodes = len(EXPECTED)
4653
self.extra_args = list(map(lambda e: e[0], EXPECTED))
4754

48-
def add_options(self, parser):
49-
parser.add_argument(
50-
"--ihave1111", action='store_true', dest="ihave1111",
51-
help=f"Run the test, assuming {ADDR} is configured on the machine",
52-
default=False)
55+
def setup_network(self):
56+
self.setup_nodes()
5357

54-
def skip_test_if_missing_module(self):
55-
if not self.options.ihave1111:
56-
raise SkipTest(
57-
f"To run this test make sure that {ADDR} (a routable address) is assigned "
58-
"to one of the interfaces on this machine and rerun with --ihave1111")
58+
def setup_nodes(self):
59+
self.add_nodes(self.num_nodes, self.extra_args)
60+
# TestNode.start() will add -bind= to extra_args if has_explicit_bind is
61+
# False. We do not want any -bind= thus set has_explicit_bind to True.
62+
for node in self.nodes:
63+
node.has_explicit_bind = True
64+
try:
65+
self.start_nodes()
66+
except FailedToStartError as e:
67+
for node in self.nodes:
68+
if node.running:
69+
if node.rpc_connected:
70+
node.stop_node(wait=node.rpc_timeout)
71+
else:
72+
node.process.kill()
73+
node.process.wait(timeout=node.rpc_timeout)
74+
node.process = None
75+
node.stdout.close()
76+
node.stderr.close()
77+
node.running = False
78+
if 'Unable to bind to ' in str(e):
79+
raise SkipTest(
80+
f'To run this test make sure that {ADDR} '
81+
'(routable address) is assigned to non-loopback '
82+
'interface on this machine')
83+
else:
84+
raise e
5985

6086
def run_test(self):
6187
self.log.info("Test the proper port is used for -externalip=")

0 commit comments

Comments
 (0)