Skip to content

Commit 357d750

Browse files
author
MarcoFalke
committed
Merge bitcoin/bitcoin#26956: test: refactor: introduce replace_in_config helper
b530d96 test: refactor: introduce `replace_in_config` helper (Sebastian Falbesoner) Pull request description: Currently two functional tests (p2p_permissions.py and wallet_crosschain.py) include quite similar code for substituting strings in a TestNode's bitcoind configuration file, so refactoring that out to a dedicated helper method seems to make sense (probably other tests could need that too in the future). ACKs for top commit: kouloumos: ACK b530d96 Tree-SHA512: 5ca65a2ef3292460e5720d5c6acf7326335001858e8f71ab054560117f9479dbadb1dacd8c9235f67f3fcfd08dbc761b62704f379cbf619bba8804f16a25bc7b
2 parents ceb74b8 + b530d96 commit 357d750

File tree

4 files changed

+19
-18
lines changed

4 files changed

+19
-18
lines changed

test/functional/p2p_permissions.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,12 @@ def run_test(self):
5656
# For this, we need to use whitebind instead of bind
5757
# by modifying the configuration file.
5858
ip_port = "127.0.0.1:{}".format(p2p_port(1))
59-
self.replaceinconfig(1, "bind=127.0.0.1", "whitebind=bloomfilter,forcerelay@" + ip_port)
59+
self.nodes[1].replace_in_config([("bind=127.0.0.1", "whitebind=bloomfilter,forcerelay@" + ip_port)])
6060
self.checkpermission(
6161
["-whitelist=noban@127.0.0.1"],
6262
# Check parameter interaction forcerelay should activate relay
6363
["noban", "bloomfilter", "forcerelay", "relay", "download"])
64-
self.replaceinconfig(1, "whitebind=bloomfilter,forcerelay@" + ip_port, "bind=127.0.0.1")
64+
self.nodes[1].replace_in_config([("whitebind=bloomfilter,forcerelay@" + ip_port, "bind=127.0.0.1")])
6565

6666
self.checkpermission(
6767
# legacy whitelistrelay should be ignored
@@ -138,12 +138,6 @@ def checkpermission(self, args, expectedPermissions):
138138
if p not in peerinfo['permissions']:
139139
raise AssertionError("Expected permissions %r is not granted." % p)
140140

141-
def replaceinconfig(self, nodeid, old, new):
142-
with open(self.nodes[nodeid].bitcoinconf, encoding="utf8") as f:
143-
newText = f.read().replace(old, new)
144-
with open(self.nodes[nodeid].bitcoinconf, 'w', encoding="utf8") as f:
145-
f.write(newText)
146-
147141

148142
if __name__ == '__main__':
149143
P2PPermissionsTests().main()

test/functional/test_framework/test_framework.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -533,11 +533,7 @@ def get_bin_from_version(version, bin_name, bin_default):
533533
self.nodes.append(test_node_i)
534534
if not test_node_i.version_is_at_least(170000):
535535
# adjust conf for pre 17
536-
conf_file = test_node_i.bitcoinconf
537-
with open(conf_file, 'r', encoding='utf8') as conf:
538-
conf_data = conf.read()
539-
with open(conf_file, 'w', encoding='utf8') as conf:
540-
conf.write(conf_data.replace('[regtest]', ''))
536+
test_node_i.replace_in_config([('[regtest]', '')])
541537

542538
def start_node(self, i, *args, **kwargs):
543539
"""Start a bitcoind"""

test/functional/test_framework/test_node.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,21 @@ def is_node_stopped(self):
387387
def wait_until_stopped(self, timeout=BITCOIND_PROC_WAIT_TIMEOUT):
388388
wait_until_helper(self.is_node_stopped, timeout=timeout, timeout_factor=self.timeout_factor)
389389

390+
def replace_in_config(self, replacements):
391+
"""
392+
Perform replacements in the configuration file.
393+
The substitutions are passed as a list of search-replace-tuples, e.g.
394+
[("old", "new"), ("foo", "bar"), ...]
395+
"""
396+
with open(self.bitcoinconf, 'r', encoding='utf8') as conf:
397+
conf_data = conf.read()
398+
for replacement in replacements:
399+
assert_equal(len(replacement), 2)
400+
old, new = replacement[0], replacement[1]
401+
conf_data = conf_data.replace(old, new)
402+
with open(self.bitcoinconf, 'w', encoding='utf8') as conf:
403+
conf.write(conf_data)
404+
390405
@property
391406
def chain_path(self) -> Path:
392407
return Path(self.datadir) / self.chain

test/functional/wallet_crosschain.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,7 @@ def setup_network(self):
2525
# Switch node 1 to testnet before starting it.
2626
self.nodes[1].chain = 'testnet3'
2727
self.nodes[1].extra_args = ['-maxconnections=0', '-prune=550'] # disable testnet sync
28-
with open(self.nodes[1].bitcoinconf, 'r', encoding='utf8') as conf:
29-
conf_data = conf.read()
30-
with open (self.nodes[1].bitcoinconf, 'w', encoding='utf8') as conf:
31-
conf.write(conf_data.replace('regtest=', 'testnet=').replace('[regtest]', '[test]'))
32-
28+
self.nodes[1].replace_in_config([('regtest=', 'testnet='), ('[regtest]', '[test]')])
3329
self.start_nodes()
3430

3531
def run_test(self):

0 commit comments

Comments
 (0)