Skip to content

Commit 0ec08a6

Browse files
jnewberyMarcoFalke
authored andcommitted
[Tests] Move assert_start_raises_init_error method to TestNode
1 parent af20f9b commit 0ec08a6

File tree

7 files changed

+44
-37
lines changed

7 files changed

+44
-37
lines changed

test/functional/feature_config_args.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ def run_test(self):
2525

2626
# Check that using -datadir argument on non-existent directory fails
2727
self.nodes[0].datadir = new_data_dir
28-
self.assert_start_raises_init_error(0, ['-datadir='+new_data_dir], 'Error: Specified data directory "' + new_data_dir + '" does not exist.')
28+
self.nodes[0].assert_start_raises_init_error(['-datadir='+new_data_dir], 'Error: Specified data directory "' + new_data_dir + '" does not exist.')
2929

3030
# Check that using non-existent datadir in conf file fails
3131
conf_file = os.path.join(default_data_dir, "bitcoin.conf")
3232
with open(conf_file, 'a', encoding='utf8') as f:
3333
f.write("datadir=" + new_data_dir + "\n")
34-
self.assert_start_raises_init_error(0, ['-conf='+conf_file], 'Error reading configuration file: specified data directory "' + new_data_dir + '" does not exist.')
34+
self.nodes[0].assert_start_raises_init_error(['-conf='+conf_file], 'Error reading configuration file: specified data directory "' + new_data_dir + '" does not exist.')
3535

3636
# Create the directory and ensure the config file now works
3737
os.mkdir(new_data_dir)

test/functional/feature_logging.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def run_test(self):
3030
invdir = os.path.join(self.nodes[0].datadir, "regtest", "foo")
3131
invalidname = os.path.join("foo", "foo.log")
3232
self.stop_node(0)
33-
self.assert_start_raises_init_error(0, ["-debuglogfile=%s" % (invalidname)],
33+
self.nodes[0].assert_start_raises_init_error(["-debuglogfile=%s" % (invalidname)],
3434
"Error: Could not open debug log file")
3535
assert not os.path.isfile(os.path.join(invdir, "foo.log"))
3636

@@ -44,7 +44,7 @@ def run_test(self):
4444
self.stop_node(0)
4545
invdir = os.path.join(self.options.tmpdir, "foo")
4646
invalidname = os.path.join(invdir, "foo.log")
47-
self.assert_start_raises_init_error(0, ["-debuglogfile=%s" % invalidname],
47+
self.nodes[0].assert_start_raises_init_error(["-debuglogfile=%s" % invalidname],
4848
"Error: Could not open debug log file")
4949
assert not os.path.isfile(os.path.join(invdir, "foo.log"))
5050

test/functional/feature_uacomment.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ def run_test(self):
2424
self.log.info("test -uacomment max length")
2525
self.stop_node(0)
2626
expected = "exceeds maximum length (256). Reduce the number or size of uacomments."
27-
self.assert_start_raises_init_error(0, ["-uacomment=" + 'a' * 256], expected)
27+
self.nodes[0].assert_start_raises_init_error(["-uacomment=" + 'a' * 256], expected)
2828

2929
self.log.info("test -uacomment unsafe characters")
3030
for unsafe_char in ['/', ':', '(', ')']:
3131
expected = "User Agent comment (" + unsafe_char + ") contains unsafe characters"
32-
self.assert_start_raises_init_error(0, ["-uacomment=" + unsafe_char], expected)
32+
self.nodes[0].assert_start_raises_init_error(["-uacomment=" + unsafe_char], expected)
3333

3434
if __name__ == '__main__':
3535
UacommentTest().main()

test/functional/test_framework/test_framework.py

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -281,27 +281,6 @@ def restart_node(self, i, extra_args=None):
281281
self.stop_node(i)
282282
self.start_node(i, extra_args)
283283

284-
def assert_start_raises_init_error(self, i, extra_args=None, expected_msg=None, *args, **kwargs):
285-
with tempfile.SpooledTemporaryFile(max_size=2**16) as log_stderr:
286-
try:
287-
self.start_node(i, extra_args, stderr=log_stderr, *args, **kwargs)
288-
self.stop_node(i)
289-
except Exception as e:
290-
assert 'bitcoind exited' in str(e) # node must have shutdown
291-
self.nodes[i].running = False
292-
self.nodes[i].process = None
293-
if expected_msg is not None:
294-
log_stderr.seek(0)
295-
stderr = log_stderr.read().decode('utf-8')
296-
if expected_msg not in stderr:
297-
raise AssertionError("Expected error \"" + expected_msg + "\" not found in:\n" + stderr)
298-
else:
299-
if expected_msg is None:
300-
assert_msg = "bitcoind should have exited with an error"
301-
else:
302-
assert_msg = "bitcoind should have exited with expected error " + expected_msg
303-
raise AssertionError(assert_msg)
304-
305284
def wait_for_node_exit(self, i, timeout):
306285
self.nodes[i].process.wait(timeout)
307286

test/functional/test_framework/test_node.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import os
1313
import re
1414
import subprocess
15+
import tempfile
1516
import time
1617

1718
from .authproxy import JSONRPCException
@@ -165,6 +166,33 @@ def is_node_stopped(self):
165166
def wait_until_stopped(self, timeout=BITCOIND_PROC_WAIT_TIMEOUT):
166167
wait_until(self.is_node_stopped, timeout=timeout)
167168

169+
def assert_start_raises_init_error(self, extra_args=None, expected_msg=None, *args, **kwargs):
170+
"""Attempt to start the node and expect it to raise an error.
171+
172+
Will throw if bitcoind starts without an error.
173+
Will throw if an expected_msg is provided and it does not appear in bitcoind's stdout."""
174+
with tempfile.SpooledTemporaryFile(max_size=2**16) as log_stderr:
175+
try:
176+
self.start(extra_args, stderr=log_stderr, *args, **kwargs)
177+
self.wait_for_rpc_connection()
178+
self.stop_node()
179+
self.wait_util_stopped()
180+
except Exception as e:
181+
assert 'bitcoind exited' in str(e) # node must have shutdown
182+
self.running = False
183+
self.process = None
184+
if expected_msg is not None:
185+
log_stderr.seek(0)
186+
stderr = log_stderr.read().decode('utf-8')
187+
if expected_msg not in stderr:
188+
raise AssertionError("Expected error \"" + expected_msg + "\" not found in:\n" + stderr)
189+
else:
190+
if expected_msg is None:
191+
assert_msg = "bitcoind should have exited with an error"
192+
else:
193+
assert_msg = "bitcoind should have exited with expected error " + expected_msg
194+
raise AssertionError(assert_msg)
195+
168196
def node_encrypt_wallet(self, passphrase):
169197
""""Encrypts the wallet.
170198

test/functional/wallet_hd.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def run_test (self):
2323

2424
# Make sure can't switch off usehd after wallet creation
2525
self.stop_node(1)
26-
self.assert_start_raises_init_error(1, ['-usehd=0'], 'already existing HD wallet')
26+
self.nodes[1].assert_start_raises_init_error(['-usehd=0'], 'already existing HD wallet')
2727
self.start_node(1)
2828
connect_nodes_bi(self.nodes, 0, 1)
2929

test/functional/wallet_multiwallet.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,27 +62,27 @@ def run_test(self):
6262
# should not initialize if wallet path can't be created
6363
self.assert_start_raises_init_error(0, ['-wallet=wallet.dat/bad'], 'Not a directory')
6464

65-
self.assert_start_raises_init_error(0, ['-walletdir=wallets'], 'Error: Specified -walletdir "wallets" does not exist')
66-
self.assert_start_raises_init_error(0, ['-walletdir=wallets'], 'Error: Specified -walletdir "wallets" is a relative path', cwd=data_dir())
67-
self.assert_start_raises_init_error(0, ['-walletdir=debug.log'], 'Error: Specified -walletdir "debug.log" is not a directory', cwd=data_dir())
65+
self.nodes[0].assert_start_raises_init_error(['-walletdir=wallets'], 'Error: Specified -walletdir "wallets" does not exist')
66+
self.nodes[0].assert_start_raises_init_error(['-walletdir=wallets'], 'Error: Specified -walletdir "wallets" is a relative path', cwd=data_dir())
67+
self.nodes[0].assert_start_raises_init_error(['-walletdir=debug.log'], 'Error: Specified -walletdir "debug.log" is not a directory', cwd=data_dir())
6868

6969
# should not initialize if there are duplicate wallets
70-
self.assert_start_raises_init_error(0, ['-wallet=w1', '-wallet=w1'], 'Error loading wallet w1. Duplicate -wallet filename specified.')
70+
self.nodes[0].assert_start_raises_init_error(['-wallet=w1', '-wallet=w1'], 'Error loading wallet w1. Duplicate -wallet filename specified.')
7171

7272
# should not initialize if one wallet is a copy of another
7373
shutil.copyfile(wallet_dir('w8'), wallet_dir('w8_copy'))
74-
self.assert_start_raises_init_error(0, ['-wallet=w8', '-wallet=w8_copy'], 'duplicates fileid')
74+
self.nodes[0].assert_start_raises_init_error(['-wallet=w8', '-wallet=w8_copy'], 'duplicates fileid')
7575

7676
# should not initialize if wallet file is a symlink
7777
os.symlink('w8', wallet_dir('w8_symlink'))
78-
self.assert_start_raises_init_error(0, ['-wallet=w8_symlink'], 'Invalid -wallet path')
78+
self.nodes[0].assert_start_raises_init_error(['-wallet=w8_symlink'], 'Invalid -wallet path')
7979

8080
# should not initialize if the specified walletdir does not exist
81-
self.assert_start_raises_init_error(0, ['-walletdir=bad'], 'Error: Specified -walletdir "bad" does not exist')
81+
self.nodes[0].assert_start_raises_init_error(['-walletdir=bad'], 'Error: Specified -walletdir "bad" does not exist')
8282
# should not initialize if the specified walletdir is not a directory
8383
not_a_dir = wallet_dir('notadir')
8484
open(not_a_dir, 'a').close()
85-
self.assert_start_raises_init_error(0, ['-walletdir=' + not_a_dir], 'Error: Specified -walletdir "' + not_a_dir + '" is not a directory')
85+
self.nodes[0].assert_start_raises_init_error(['-walletdir=' + not_a_dir], 'Error: Specified -walletdir "' + not_a_dir + '" is not a directory')
8686

8787
# if wallets/ doesn't exist, datadir should be the default wallet dir
8888
wallet_dir2 = data_dir('walletdir')
@@ -103,7 +103,7 @@ def run_test(self):
103103
competing_wallet_dir = os.path.join(self.options.tmpdir, 'competing_walletdir')
104104
os.mkdir(competing_wallet_dir)
105105
self.restart_node(0, ['-walletdir='+competing_wallet_dir])
106-
self.assert_start_raises_init_error(1, ['-walletdir='+competing_wallet_dir], 'Error initializing wallet database environment')
106+
self.nodes[1].assert_start_raises_init_error(['-walletdir='+competing_wallet_dir], 'Error initializing wallet database environment')
107107

108108
self.restart_node(0, extra_args)
109109

0 commit comments

Comments
 (0)