Merge bitcoin/bitcoin#25333: test: Fix out-of-range port collisions

fa7a711a30b707cbdee4435dd0a956bffb7aaccb test: Fix out-of-range port collisions (MacroFake)

Pull request description:

  Otherwise the test will fail if two tests running in parallel use the same port. See https://github.com/bitcoin/bitcoin/pull/25096#discussion_r892558783 and https://github.com/bitcoin/bitcoin/pull/25312

ACKs for top commit:
  dergoegge:
    ACK fa7a711a30b707cbdee4435dd0a956bffb7aaccb - This gets rid of some rather arbitrary choices for ports in some of our functional tests that can cause port collisions across test runs, resulting in intermittent failures.

Tree-SHA512: ac73da8a498230b992ab12e1ee3c4ff3d868cd63c00d2c71537d156cb7c8f8be8598ec574646b17c5a44ae3ac5bb54bf29d300f054a36cec6f6ce8054a0da0a4
This commit is contained in:
MacroFake 2022-06-10 17:17:27 +02:00 committed by pasta
parent b75089667c
commit 1fae0c2bbb
No known key found for this signature in database
GPG Key ID: 52527BEDABE87984
3 changed files with 10 additions and 19 deletions

View File

@ -18,12 +18,12 @@ from test_framework.test_framework import (
SkipTest, SkipTest,
) )
from test_framework.util import ( from test_framework.util import (
PORT_MIN,
PORT_RANGE,
assert_equal, assert_equal,
p2p_port,
rpc_port, rpc_port,
) )
class BindExtraTest(BitcoinTestFramework): class BindExtraTest(BitcoinTestFramework):
def set_test_params(self): def set_test_params(self):
self.setup_clean_chain = True self.setup_clean_chain = True
@ -33,11 +33,6 @@ class BindExtraTest(BitcoinTestFramework):
self.num_nodes = 2 self.num_nodes = 2
def setup_network(self): def setup_network(self):
# Override setup_network() because we want to put the result of
# p2p_port() in self.extra_args[], before the nodes are started.
# p2p_port() is not usable in set_test_params() because PortSeed.n is
# not set at that time.
# Due to OS-specific network stats queries, we only run on Linux. # Due to OS-specific network stats queries, we only run on Linux.
self.log.info("Checking for Linux") self.log.info("Checking for Linux")
if not sys.platform.startswith('linux'): if not sys.platform.startswith('linux'):
@ -45,8 +40,8 @@ class BindExtraTest(BitcoinTestFramework):
loopback_ipv4 = addr_to_hex("127.0.0.1") loopback_ipv4 = addr_to_hex("127.0.0.1")
# Start custom ports after p2p and rpc ports. # Start custom ports by reusing unused p2p ports
port = PORT_MIN + 2 * PORT_RANGE port = p2p_port(self.num_nodes)
# Array of tuples [command line arguments, expected bind addresses]. # Array of tuples [command line arguments, expected bind addresses].
self.expected = [] self.expected = []

View File

@ -31,19 +31,15 @@ addnode connect to generic DNS name
""" """
import socket import socket
import os
from test_framework.socks5 import Socks5Configuration, Socks5Command, Socks5Server, AddressType from test_framework.socks5 import Socks5Configuration, Socks5Command, Socks5Server, AddressType
from test_framework.test_framework import BitcoinTestFramework from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import ( from test_framework.util import (
PORT_MIN,
PORT_RANGE,
assert_equal, assert_equal,
p2p_port,
) )
from test_framework.netutil import test_ipv6_local from test_framework.netutil import test_ipv6_local
RANGE_BEGIN = PORT_MIN + 2 * PORT_RANGE # Start after p2p and rpc ports
# Networks returned by RPC getpeerinfo. # Networks returned by RPC getpeerinfo.
NET_UNROUTABLE = "not_publicly_routable" NET_UNROUTABLE = "not_publicly_routable"
NET_IPV4 = "ipv4" NET_IPV4 = "ipv4"
@ -65,19 +61,19 @@ class ProxyTest(BitcoinTestFramework):
# Create two proxies on different ports # Create two proxies on different ports
# ... one unauthenticated # ... one unauthenticated
self.conf1 = Socks5Configuration() self.conf1 = Socks5Configuration()
self.conf1.addr = ('127.0.0.1', RANGE_BEGIN + (os.getpid() % 1000)) self.conf1.addr = ('127.0.0.1', p2p_port(self.num_nodes))
self.conf1.unauth = True self.conf1.unauth = True
self.conf1.auth = False self.conf1.auth = False
# ... one supporting authenticated and unauthenticated (Tor) # ... one supporting authenticated and unauthenticated (Tor)
self.conf2 = Socks5Configuration() self.conf2 = Socks5Configuration()
self.conf2.addr = ('127.0.0.1', RANGE_BEGIN + 1000 + (os.getpid() % 1000)) self.conf2.addr = ('127.0.0.1', p2p_port(self.num_nodes + 1))
self.conf2.unauth = True self.conf2.unauth = True
self.conf2.auth = True self.conf2.auth = True
if self.have_ipv6: if self.have_ipv6:
# ... one on IPv6 with similar configuration # ... one on IPv6 with similar configuration
self.conf3 = Socks5Configuration() self.conf3 = Socks5Configuration()
self.conf3.af = socket.AF_INET6 self.conf3.af = socket.AF_INET6
self.conf3.addr = ('::1', RANGE_BEGIN + 2000 + (os.getpid() % 1000)) self.conf3.addr = ('::1', p2p_port(self.num_nodes + 2))
self.conf3.unauth = True self.conf3.unauth = True
self.conf3.auth = True self.conf3.auth = True
else: else:

View File

@ -42,8 +42,8 @@ class AddrTest(BitcoinTestFramework):
def set_test_params(self): def set_test_params(self):
self.num_nodes = 1 self.num_nodes = 1
# Use some of the remaining p2p ports for the onion binds. # Use some of the remaining p2p ports for the onion binds.
self.onion_port1 = p2p_port(1) self.onion_port1 = p2p_port(self.num_nodes)
self.onion_port2 = p2p_port(2) self.onion_port2 = p2p_port(self.num_nodes + 1)
self.extra_args = [ self.extra_args = [
[f"-bind=127.0.0.1:{self.onion_port1}=onion", f"-bind=127.0.0.1:{self.onion_port2}=onion"], [f"-bind=127.0.0.1:{self.onion_port1}=onion", f"-bind=127.0.0.1:{self.onion_port2}=onion"],
] ]