Skip to content

Commit 6d36f59

Browse files
committed
Merge #12076: qa: Use node.datadir instead of tmpdir in test framework
c8330d4 qa: Use node.datadir instead of tmpdir in test framework (MarcoFalke) Pull request description: Commit c53c983 introduced the utility function `get_datadir_path`, however not all places in the code use this util function. Using the util function everywhere makes it easier to review pull requests related to the datadir. This commit replaces datadir path creation with the `datadir` member of `TestNode`, which itself uses `get_datadir_path`. Tree-SHA512: c75707ab7149d732a6d56152a5813138a33459d3d07577b60b89f2a207c83b7663fac5f203593677c9892d1c23a5eba4bd45c5c4ababf040d720b437240fcddf
2 parents 185d484 + c8330d4 commit 6d36f59

13 files changed

+74
-71
lines changed

test/functional/feature_bip9_softforks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ def test_BIP(self, bipName, activated_version, invalidate, invalidatePostSignatu
241241
self.test.clear_all_connections()
242242
self.stop_nodes()
243243
self.nodes = []
244-
shutil.rmtree(self.options.tmpdir + "/node0")
244+
shutil.rmtree(get_datadir_path(self.options.tmpdir, 0))
245245
self.setup_chain()
246246
self.setup_network()
247247
self.test.add_all_connections(self.nodes)

test/functional/feature_config_args.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import re
99

1010
from test_framework.test_framework import BitcoinTestFramework
11-
from test_framework.util import get_datadir_path
11+
1212

1313
class ConfArgsTest(BitcoinTestFramework):
1414
def set_test_params(self):
@@ -20,7 +20,7 @@ def run_test(self):
2020
# Remove the -datadir argument so it doesn't override the config file
2121
self.nodes[0].args = [arg for arg in self.nodes[0].args if not arg.startswith("-datadir")]
2222

23-
default_data_dir = get_datadir_path(self.options.tmpdir, 0)
23+
default_data_dir = self.nodes[0].datadir
2424
new_data_dir = os.path.join(default_data_dir, 'newdatadir')
2525
new_data_dir_2 = os.path.join(default_data_dir, 'newdatadir2')
2626

test/functional/feature_pruning.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def set_test_params(self):
4444
def setup_network(self):
4545
self.setup_nodes()
4646

47-
self.prunedir = self.options.tmpdir + "/node2/regtest/blocks/"
47+
self.prunedir = os.path.join(self.nodes[2].datadir, 'regtest', 'blocks', '')
4848

4949
connect_nodes(self.nodes[0], 1)
5050
connect_nodes(self.nodes[1], 2)

test/functional/mempool_persist.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ def run_test(self):
9393
self.start_node(0)
9494
wait_until(lambda: len(self.nodes[0].getrawmempool()) == 5)
9595

96-
mempooldat0 = os.path.join(self.options.tmpdir, 'node0', 'regtest', 'mempool.dat')
97-
mempooldat1 = os.path.join(self.options.tmpdir, 'node1', 'regtest', 'mempool.dat')
96+
mempooldat0 = os.path.join(self.nodes[0].datadir, 'regtest', 'mempool.dat')
97+
mempooldat1 = os.path.join(self.nodes[1].datadir, 'regtest', 'mempool.dat')
9898
self.log.debug("Remove the mempool.dat file. Verify that savemempool to disk via RPC re-creates it")
9999
os.remove(mempooldat0)
100100
self.nodes[0].savemempool()

test/functional/rpc_bind.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def run_allowip_test(self, allow_ips, rpchost, rpcport):
4848
self.nodes[0].rpchost = None
4949
self.start_nodes([base_args])
5050
# connect to node through non-loopback interface
51-
node = get_rpc_proxy(rpc_url(get_datadir_path(self.options.tmpdir, 0), 0, "%s:%d" % (rpchost, rpcport)), 0, coveragedir=self.options.coveragedir)
51+
node = get_rpc_proxy(rpc_url(self.nodes[0].datadir, 0, "%s:%d" % (rpchost, rpcport)), 0, coveragedir=self.options.coveragedir)
5252
node.getnetworkinfo()
5353
self.stop_nodes()
5454

test/functional/rpc_users.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,18 @@
55
"""Test multiple RPC users."""
66

77
from test_framework.test_framework import BitcoinTestFramework
8-
from test_framework.util import str_to_b64str, assert_equal
8+
from test_framework.util import (
9+
assert_equal,
10+
get_datadir_path,
11+
str_to_b64str,
12+
)
913

1014
import os
1115
import http.client
1216
import urllib.parse
1317

14-
class HTTPBasicsTest (BitcoinTestFramework):
18+
19+
class HTTPBasicsTest(BitcoinTestFramework):
1520
def set_test_params(self):
1621
self.num_nodes = 2
1722

@@ -22,10 +27,10 @@ def setup_chain(self):
2227
rpcauth2 = "rpcauth=rt2:f8607b1a88861fac29dfccf9b52ff9f$ff36a0c23c8c62b4846112e50fa888416e94c17bfd4c42f88fd8f55ec6a3137e"
2328
rpcuser = "rpcuser=rpcuser💻"
2429
rpcpassword = "rpcpassword=rpcpassword🔑"
25-
with open(os.path.join(self.options.tmpdir+"/node0", "bitcoin.conf"), 'a', encoding='utf8') as f:
30+
with open(os.path.join(get_datadir_path(self.options.tmpdir, 0), "bitcoin.conf"), 'a', encoding='utf8') as f:
2631
f.write(rpcauth+"\n")
2732
f.write(rpcauth2+"\n")
28-
with open(os.path.join(self.options.tmpdir+"/node1", "bitcoin.conf"), 'a', encoding='utf8') as f:
33+
with open(os.path.join(get_datadir_path(self.options.tmpdir, 1), "bitcoin.conf"), 'a', encoding='utf8') as f:
2934
f.write(rpcuser+"\n")
3035
f.write(rpcpassword+"\n")
3136

@@ -54,7 +59,7 @@ def run_test(self):
5459
resp = conn.getresponse()
5560
assert_equal(resp.status, 200)
5661
conn.close()
57-
62+
5863
#Use new authpair to confirm both work
5964
headers = {"Authorization": "Basic " + str_to_b64str(authpairnew)}
6065

test/functional/test_framework/test_framework.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ def add_nodes(self, num_nodes, extra_args=None, rpchost=None, timewait=None, bin
228228
assert_equal(len(extra_args), num_nodes)
229229
assert_equal(len(binary), num_nodes)
230230
for i in range(num_nodes):
231-
self.nodes.append(TestNode(i, self.options.tmpdir, rpchost=rpchost, timewait=timewait, binary=binary[i], stderr=None, mocktime=self.mocktime, coverage_dir=self.options.coveragedir, extra_conf=extra_confs[i], extra_args=extra_args[i], use_cli=self.options.usecli))
231+
self.nodes.append(TestNode(i, get_datadir_path(self.options.tmpdir, i), rpchost=rpchost, timewait=timewait, binary=binary[i], stderr=None, mocktime=self.mocktime, coverage_dir=self.options.coveragedir, extra_conf=extra_confs[i], extra_args=extra_args[i], use_cli=self.options.usecli))
232232

233233
def start_node(self, i, *args, **kwargs):
234234
"""Start a bitcoind"""
@@ -379,7 +379,7 @@ def _initialize_chain(self):
379379
args = [os.getenv("BITCOIND", "bitcoind"), "-datadir=" + datadir]
380380
if i > 0:
381381
args.append("-connect=127.0.0.1:" + str(p2p_port(0)))
382-
self.nodes.append(TestNode(i, self.options.cachedir, extra_conf=["bind=127.0.0.1"], extra_args=[],rpchost=None, timewait=None, binary=None, stderr=None, mocktime=self.mocktime, coverage_dir=None))
382+
self.nodes.append(TestNode(i, get_datadir_path(self.options.cachedir, i), extra_conf=["bind=127.0.0.1"], extra_args=[],rpchost=None, timewait=None, binary=None, stderr=None, mocktime=self.mocktime, coverage_dir=None))
383383
self.nodes[i].args = args
384384
self.start_node(i)
385385

test/functional/test_framework/test_node.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ class TestNode():
4444
To make things easier for the test writer, any unrecognised messages will
4545
be dispatched to the RPC connection."""
4646

47-
def __init__(self, i, dirname, rpchost, timewait, binary, stderr, mocktime, coverage_dir, extra_conf=None, extra_args=None, use_cli=False):
47+
def __init__(self, i, datadir, rpchost, timewait, binary, stderr, mocktime, coverage_dir, extra_conf=None, extra_args=None, use_cli=False):
4848
self.index = i
49-
self.datadir = os.path.join(dirname, "node" + str(i))
49+
self.datadir = datadir
5050
self.rpchost = rpchost
5151
if timewait:
5252
self.rpc_timeout = timewait
@@ -60,7 +60,7 @@ def __init__(self, i, dirname, rpchost, timewait, binary, stderr, mocktime, cove
6060
self.stderr = stderr
6161
self.coverage_dir = coverage_dir
6262
if extra_conf != None:
63-
append_config(dirname, i, extra_conf)
63+
append_config(datadir, extra_conf)
6464
# Most callers will just need to add extra args to the standard list below.
6565
# For those callers that need more flexibility, they can just set the args property directly.
6666
# Note that common args are set in the config file (see initialize_datadir)

test/functional/test_framework/util.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ def rpc_url(datadir, i, rpchost=None):
284284
################
285285

286286
def initialize_datadir(dirname, n):
287-
datadir = os.path.join(dirname, "node" + str(n))
287+
datadir = get_datadir_path(dirname, n)
288288
if not os.path.isdir(datadir):
289289
os.makedirs(datadir)
290290
with open(os.path.join(datadir, "bitcoin.conf"), 'w', encoding='utf8') as f:
@@ -300,8 +300,7 @@ def initialize_datadir(dirname, n):
300300
def get_datadir_path(dirname, n):
301301
return os.path.join(dirname, "node" + str(n))
302302

303-
def append_config(dirname, n, options):
304-
datadir = get_datadir_path(dirname, n)
303+
def append_config(datadir, options):
305304
with open(os.path.join(datadir, "bitcoin.conf"), 'a', encoding='utf8') as f:
306305
for option in options:
307306
f.write(option + "\n")

test/functional/wallet_backup.py

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ def stop_three(self):
9090
self.stop_node(2)
9191

9292
def erase_three(self):
93-
os.remove(self.options.tmpdir + "/node0/regtest/wallets/wallet.dat")
94-
os.remove(self.options.tmpdir + "/node1/regtest/wallets/wallet.dat")
95-
os.remove(self.options.tmpdir + "/node2/regtest/wallets/wallet.dat")
93+
os.remove(os.path.join(self.nodes[0].datadir, 'regtest', 'wallets', 'wallet.dat'))
94+
os.remove(os.path.join(self.nodes[1].datadir, 'regtest', 'wallets', 'wallet.dat'))
95+
os.remove(os.path.join(self.nodes[2].datadir, 'regtest', 'wallets', 'wallet.dat'))
9696

9797
def run_test(self):
9898
self.log.info("Generating initial blockchain")
@@ -116,13 +116,13 @@ def run_test(self):
116116
self.do_one_round()
117117

118118
self.log.info("Backing up")
119-
tmpdir = self.options.tmpdir
120-
self.nodes[0].backupwallet(tmpdir + "/node0/wallet.bak")
121-
self.nodes[0].dumpwallet(tmpdir + "/node0/wallet.dump")
122-
self.nodes[1].backupwallet(tmpdir + "/node1/wallet.bak")
123-
self.nodes[1].dumpwallet(tmpdir + "/node1/wallet.dump")
124-
self.nodes[2].backupwallet(tmpdir + "/node2/wallet.bak")
125-
self.nodes[2].dumpwallet(tmpdir + "/node2/wallet.dump")
119+
120+
self.nodes[0].backupwallet(os.path.join(self.nodes[0].datadir, 'wallet.bak'))
121+
self.nodes[0].dumpwallet(os.path.join(self.nodes[0].datadir, 'wallet.dump'))
122+
self.nodes[1].backupwallet(os.path.join(self.nodes[1].datadir, 'wallet.bak'))
123+
self.nodes[1].dumpwallet(os.path.join(self.nodes[1].datadir, 'wallet.dump'))
124+
self.nodes[2].backupwallet(os.path.join(self.nodes[2].datadir, 'wallet.bak'))
125+
self.nodes[2].dumpwallet(os.path.join(self.nodes[2].datadir, 'wallet.dump'))
126126

127127
self.log.info("More transactions")
128128
for i in range(5):
@@ -150,13 +150,13 @@ def run_test(self):
150150
self.erase_three()
151151

152152
# Start node2 with no chain
153-
shutil.rmtree(self.options.tmpdir + "/node2/regtest/blocks")
154-
shutil.rmtree(self.options.tmpdir + "/node2/regtest/chainstate")
153+
shutil.rmtree(os.path.join(self.nodes[2].datadir, 'regtest', 'blocks'))
154+
shutil.rmtree(os.path.join(self.nodes[2].datadir, 'regtest', 'chainstate'))
155155

156156
# Restore wallets from backup
157-
shutil.copyfile(tmpdir + "/node0/wallet.bak", tmpdir + "/node0/regtest/wallets/wallet.dat")
158-
shutil.copyfile(tmpdir + "/node1/wallet.bak", tmpdir + "/node1/regtest/wallets/wallet.dat")
159-
shutil.copyfile(tmpdir + "/node2/wallet.bak", tmpdir + "/node2/regtest/wallets/wallet.dat")
157+
shutil.copyfile(os.path.join(self.nodes[0].datadir, 'wallet.bak'), os.path.join(self.nodes[0].datadir, 'regtest', 'wallets', 'wallet.dat'))
158+
shutil.copyfile(os.path.join(self.nodes[1].datadir, 'wallet.bak'), os.path.join(self.nodes[1].datadir, 'regtest', 'wallets', 'wallet.dat'))
159+
shutil.copyfile(os.path.join(self.nodes[2].datadir, 'wallet.bak'), os.path.join(self.nodes[2].datadir, 'regtest', 'wallets', 'wallet.dat'))
160160

161161
self.log.info("Re-starting nodes")
162162
self.start_three()
@@ -171,18 +171,18 @@ def run_test(self):
171171
self.erase_three()
172172

173173
#start node2 with no chain
174-
shutil.rmtree(self.options.tmpdir + "/node2/regtest/blocks")
175-
shutil.rmtree(self.options.tmpdir + "/node2/regtest/chainstate")
174+
shutil.rmtree(os.path.join(self.nodes[2].datadir, 'regtest', 'blocks'))
175+
shutil.rmtree(os.path.join(self.nodes[2].datadir, 'regtest', 'chainstate'))
176176

177177
self.start_three()
178178

179179
assert_equal(self.nodes[0].getbalance(), 0)
180180
assert_equal(self.nodes[1].getbalance(), 0)
181181
assert_equal(self.nodes[2].getbalance(), 0)
182182

183-
self.nodes[0].importwallet(tmpdir + "/node0/wallet.dump")
184-
self.nodes[1].importwallet(tmpdir + "/node1/wallet.dump")
185-
self.nodes[2].importwallet(tmpdir + "/node2/wallet.dump")
183+
self.nodes[0].importwallet(os.path.join(self.nodes[0].datadir, 'wallet.dump'))
184+
self.nodes[1].importwallet(os.path.join(self.nodes[1].datadir, 'wallet.dump'))
185+
self.nodes[2].importwallet(os.path.join(self.nodes[2].datadir, 'wallet.dump'))
186186

187187
sync_blocks(self.nodes)
188188

@@ -192,10 +192,10 @@ def run_test(self):
192192

193193
# Backup to source wallet file must fail
194194
sourcePaths = [
195-
tmpdir + "/node0/regtest/wallets/wallet.dat",
196-
tmpdir + "/node0/./regtest/wallets/wallet.dat",
197-
tmpdir + "/node0/regtest/wallets/",
198-
tmpdir + "/node0/regtest/wallets"]
195+
os.path.join(self.nodes[0].datadir, 'regtest', 'wallets', 'wallet.dat'),
196+
os.path.join(self.nodes[0].datadir, 'regtest', '.', 'wallets', 'wallet.dat'),
197+
os.path.join(self.nodes[0].datadir, 'regtest', 'wallets', ''),
198+
os.path.join(self.nodes[0].datadir, 'regtest', 'wallets')]
199199

200200
for sourcePath in sourcePaths:
201201
assert_raises_rpc_error(-4, "backup failed", self.nodes[0].backupwallet, sourcePath)

0 commit comments

Comments
 (0)