Skip to content

Commit fa8a1d7

Browse files
MarcoFalkejtimon
andcommitted
test: Adapt test framework for chains other than "regtest"
Co-Authored-By: Jorge Timón <[email protected]>
1 parent 68f5466 commit fa8a1d7

File tree

6 files changed

+28
-24
lines changed

6 files changed

+28
-24
lines changed

test/functional/feature_blocksdir.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ def set_test_params(self):
1818

1919
def run_test(self):
2020
self.stop_node(0)
21-
assert os.path.isdir(os.path.join(self.nodes[0].datadir, "regtest", "blocks"))
21+
assert os.path.isdir(os.path.join(self.nodes[0].datadir, self.chain, "blocks"))
2222
assert not os.path.isdir(os.path.join(self.nodes[0].datadir, "blocks"))
2323
shutil.rmtree(self.nodes[0].datadir)
24-
initialize_datadir(self.options.tmpdir, 0)
24+
initialize_datadir(self.options.tmpdir, 0, self.chain)
2525
self.log.info("Starting with nonexistent blocksdir ...")
2626
blocksdir_path = os.path.join(self.options.tmpdir, 'blocksdir')
2727
self.nodes[0].assert_start_raises_init_error(["-blocksdir=" + blocksdir_path], 'Error: Specified blocks directory "{}" does not exist.'.format(blocksdir_path))
@@ -30,8 +30,8 @@ def run_test(self):
3030
self.start_node(0, ["-blocksdir=" + blocksdir_path])
3131
self.log.info("mining blocks..")
3232
self.nodes[0].generatetoaddress(10, self.nodes[0].get_deterministic_priv_key().address)
33-
assert os.path.isfile(os.path.join(blocksdir_path, "regtest", "blocks", "blk00000.dat"))
34-
assert os.path.isdir(os.path.join(self.nodes[0].datadir, "regtest", "blocks", "index"))
33+
assert os.path.isfile(os.path.join(blocksdir_path, self.chain, "blocks", "blk00000.dat"))
34+
assert os.path.isdir(os.path.join(self.nodes[0].datadir, self.chain, "blocks", "index"))
3535

3636

3737
if __name__ == '__main__':

test/functional/interface_bitcoin_cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def run_test(self):
2929
rpc_response = self.nodes[0].getblockchaininfo()
3030
assert_equal(cli_response, rpc_response)
3131

32-
user, password = get_auth_cookie(self.nodes[0].datadir)
32+
user, password = get_auth_cookie(self.nodes[0].datadir, self.chain)
3333

3434
self.log.info("Test -stdinrpcpass option")
3535
assert_equal(0, self.nodes[0].cli('-rpcuser=%s' % user, '-stdinrpcpass', input=password).getblockcount())

test/functional/rpc_bind.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def run_allowip_test(self, allow_ips, rpchost, rpcport):
5555
self.nodes[0].rpchost = None
5656
self.start_nodes([node_args])
5757
# connect to node through non-loopback interface
58-
node = get_rpc_proxy(rpc_url(self.nodes[0].datadir, 0, "%s:%d" % (rpchost, rpcport)), 0, coveragedir=self.options.coveragedir)
58+
node = get_rpc_proxy(rpc_url(self.nodes[0].datadir, 0, self.chain, "%s:%d" % (rpchost, rpcport)), 0, coveragedir=self.options.coveragedir)
5959
node.getnetworkinfo()
6060
self.stop_nodes()
6161

test/functional/test_framework/test_framework.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ class BitcoinTestFramework(metaclass=BitcoinTestMetaClass):
9191

9292
def __init__(self):
9393
"""Sets test framework defaults. Do not override this method. Instead, override the set_test_params() method"""
94+
self.chain = 'regtest'
9495
self.setup_clean_chain = False
9596
self.nodes = []
9697
self.network_thread = None
@@ -342,6 +343,7 @@ def add_nodes(self, num_nodes, extra_args=None, *, rpchost=None, binary=None):
342343
self.nodes.append(TestNode(
343344
i,
344345
get_datadir_path(self.options.tmpdir, i),
346+
chain=self.chain,
345347
rpchost=rpchost,
346348
timewait=self.rpc_timeout,
347349
bitcoind=binary[i],
@@ -477,11 +479,12 @@ def _initialize_chain(self):
477479
if not os.path.isdir(cache_node_dir):
478480
self.log.debug("Creating cache directory {}".format(cache_node_dir))
479481

480-
initialize_datadir(self.options.cachedir, CACHE_NODE_ID)
482+
initialize_datadir(self.options.cachedir, CACHE_NODE_ID, self.chain)
481483
self.nodes.append(
482484
TestNode(
483485
CACHE_NODE_ID,
484486
cache_node_dir,
487+
chain=self.chain,
485488
extra_conf=["bind=127.0.0.1"],
486489
extra_args=['-disablewallet'],
487490
rpchost=None,
@@ -515,7 +518,7 @@ def _initialize_chain(self):
515518
self.nodes = []
516519

517520
def cache_path(*paths):
518-
return os.path.join(cache_node_dir, "regtest", *paths)
521+
return os.path.join(cache_node_dir, self.chain, *paths)
519522

520523
os.rmdir(cache_path('wallets')) # Remove empty wallets dir
521524
for entry in os.listdir(cache_path()):
@@ -526,15 +529,15 @@ def cache_path(*paths):
526529
self.log.debug("Copy cache directory {} to node {}".format(cache_node_dir, i))
527530
to_dir = get_datadir_path(self.options.tmpdir, i)
528531
shutil.copytree(cache_node_dir, to_dir)
529-
initialize_datadir(self.options.tmpdir, i) # Overwrite port/rpcport in bitcoin.conf
532+
initialize_datadir(self.options.tmpdir, i, self.chain) # Overwrite port/rpcport in bitcoin.conf
530533

531534
def _initialize_chain_clean(self):
532535
"""Initialize empty blockchain for use by the test.
533536
534537
Create an empty blockchain and num_nodes wallets.
535538
Useful if a test case wants complete control over initialization."""
536539
for i in range(self.num_nodes):
537-
initialize_datadir(self.options.tmpdir, i)
540+
initialize_datadir(self.options.tmpdir, i, self.chain)
538541

539542
def skip_if_no_py3_zmq(self):
540543
"""Attempt to import the zmq package and skip the test if the import fails."""

test/functional/test_framework/test_node.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class TestNode():
5959
To make things easier for the test writer, any unrecognised messages will
6060
be dispatched to the RPC connection."""
6161

62-
def __init__(self, i, datadir, *, rpchost, timewait, bitcoind, bitcoin_cli, coverage_dir, cwd, extra_conf=None, extra_args=None, use_cli=False, start_perf=False):
62+
def __init__(self, i, datadir, *, chain, rpchost, timewait, bitcoind, bitcoin_cli, coverage_dir, cwd, extra_conf=None, extra_args=None, use_cli=False, start_perf=False):
6363
"""
6464
Kwargs:
6565
start_perf (bool): If True, begin profiling the node with `perf` as soon as
@@ -70,6 +70,7 @@ def __init__(self, i, datadir, *, rpchost, timewait, bitcoind, bitcoin_cli, cove
7070
self.datadir = datadir
7171
self.stdout_dir = os.path.join(self.datadir, "stdout")
7272
self.stderr_dir = os.path.join(self.datadir, "stderr")
73+
self.chain = chain
7374
self.rpchost = rpchost
7475
self.rpc_timeout = timewait
7576
self.binary = bitcoind
@@ -197,7 +198,7 @@ def start(self, extra_args=None, *, cwd=None, stdout=None, stderr=None, **kwargs
197198
# Delete any existing cookie file -- if such a file exists (eg due to
198199
# unclean shutdown), it will get overwritten anyway by bitcoind, and
199200
# potentially interfere with our attempt to authenticate
200-
delete_cookie_file(self.datadir)
201+
delete_cookie_file(self.datadir, self.chain)
201202

202203
# add environment variable LIBC_FATAL_STDERR_=1 so that libc errors are written to stderr and not the terminal
203204
subp_env = dict(os.environ, LIBC_FATAL_STDERR_="1")
@@ -219,7 +220,7 @@ def wait_for_rpc_connection(self):
219220
raise FailedToStartError(self._node_msg(
220221
'bitcoind exited with status {} during initialization'.format(self.process.returncode)))
221222
try:
222-
rpc = get_rpc_proxy(rpc_url(self.datadir, self.index, self.rpchost), self.index, timeout=self.rpc_timeout, coveragedir=self.coverage_dir)
223+
rpc = get_rpc_proxy(rpc_url(self.datadir, self.index, self.chain, self.rpchost), self.index, timeout=self.rpc_timeout, coveragedir=self.coverage_dir)
223224
rpc.getblockcount()
224225
# If the call to getblockcount() succeeds then the RPC connection is up
225226
self.log.debug("RPC successfully started")
@@ -306,7 +307,7 @@ def wait_until_stopped(self, timeout=BITCOIND_PROC_WAIT_TIMEOUT):
306307

307308
@contextlib.contextmanager
308309
def assert_debug_log(self, expected_msgs):
309-
debug_log = os.path.join(self.datadir, 'regtest', 'debug.log')
310+
debug_log = os.path.join(self.datadir, self.chain, 'debug.log')
310311
with open(debug_log, encoding='utf-8') as dl:
311312
dl.seek(0, 2)
312313
prev_size = dl.tell()

test/functional/test_framework/util.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,8 @@ def p2p_port(n):
271271
def rpc_port(n):
272272
return PORT_MIN + PORT_RANGE + n + (MAX_NODES * PortSeed.n) % (PORT_RANGE - 1 - MAX_NODES)
273273

274-
def rpc_url(datadir, i, rpchost=None):
275-
rpc_u, rpc_p = get_auth_cookie(datadir)
274+
def rpc_url(datadir, i, chain, rpchost):
275+
rpc_u, rpc_p = get_auth_cookie(datadir, chain)
276276
host = '127.0.0.1'
277277
port = rpc_port(i)
278278
if rpchost:
@@ -286,13 +286,13 @@ def rpc_url(datadir, i, rpchost=None):
286286
# Node functions
287287
################
288288

289-
def initialize_datadir(dirname, n):
289+
def initialize_datadir(dirname, n, chain):
290290
datadir = get_datadir_path(dirname, n)
291291
if not os.path.isdir(datadir):
292292
os.makedirs(datadir)
293293
with open(os.path.join(datadir, "bitcoin.conf"), 'w', encoding='utf8') as f:
294-
f.write("regtest=1\n")
295-
f.write("[regtest]\n")
294+
f.write("{}=1\n".format(chain))
295+
f.write("[{}]\n".format(chain))
296296
f.write("port=" + str(p2p_port(n)) + "\n")
297297
f.write("rpcport=" + str(rpc_port(n)) + "\n")
298298
f.write("server=1\n")
@@ -312,7 +312,7 @@ def append_config(datadir, options):
312312
for option in options:
313313
f.write(option + "\n")
314314

315-
def get_auth_cookie(datadir):
315+
def get_auth_cookie(datadir, chain):
316316
user = None
317317
password = None
318318
if os.path.isfile(os.path.join(datadir, "bitcoin.conf")):
@@ -325,7 +325,7 @@ def get_auth_cookie(datadir):
325325
assert password is None # Ensure that there is only one rpcpassword line
326326
password = line.split("=")[1].strip("\n")
327327
try:
328-
with open(os.path.join(datadir, "regtest", ".cookie"), 'r', encoding="ascii") as f:
328+
with open(os.path.join(datadir, chain, ".cookie"), 'r', encoding="ascii") as f:
329329
userpass = f.read()
330330
split_userpass = userpass.split(':')
331331
user = split_userpass[0]
@@ -337,10 +337,10 @@ def get_auth_cookie(datadir):
337337
return user, password
338338

339339
# If a cookie file exists in the given datadir, delete it.
340-
def delete_cookie_file(datadir):
341-
if os.path.isfile(os.path.join(datadir, "regtest", ".cookie")):
340+
def delete_cookie_file(datadir, chain):
341+
if os.path.isfile(os.path.join(datadir, chain, ".cookie")):
342342
logger.debug("Deleting leftover cookie file")
343-
os.remove(os.path.join(datadir, "regtest", ".cookie"))
343+
os.remove(os.path.join(datadir, chain, ".cookie"))
344344

345345
def get_bip9_status(node, key):
346346
info = node.getblockchaininfo()

0 commit comments

Comments
 (0)