Skip to content

Commit 185d484

Browse files
committed
Merge #12718: [Tests] Require exact match in assert_start_raises_init_eror (jnewbery)
fae1374 qa: Allow for partial_match when checking init error (MarcoFalke) 5812273 [Tests] Require exact match in assert_start_raises_init_eror() (John Newbery) 0ec08a6 [Tests] Move assert_start_raises_init_error method to TestNode (John Newbery) Pull request description: Extracted from #12379, because the changes are important on their own. This allows for exact testing, since the match can be specified with a strict regex. Internal details (such as exact formatting of the error message) can still be fuzzed away by regex wildcards. Tree-SHA512: 605d2c9c42362a32d42321b066637577a026d0bb8cfc1c9f5737a4ca6503ffe85457a5122cea6e1101053ccc6c8aa1bbae3602e1fa7d2988bf7d5c275f412f66
2 parents ad82317 + fae1374 commit 185d484

File tree

7 files changed

+66
-43
lines changed

7 files changed

+66
-43
lines changed

test/functional/feature_config_args.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"""Test various command line arguments and configuration file parameters."""
66

77
import os
8+
import re
89

910
from test_framework.test_framework import BitcoinTestFramework
1011
from test_framework.util import get_datadir_path
@@ -25,13 +26,13 @@ def run_test(self):
2526

2627
# Check that using -datadir argument on non-existent directory fails
2728
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.')
29+
self.nodes[0].assert_start_raises_init_error(['-datadir=' + new_data_dir], 'Error: Specified data directory "' + re.escape(new_data_dir) + '" does not exist.')
2930

3031
# Check that using non-existent datadir in conf file fails
3132
conf_file = os.path.join(default_data_dir, "bitcoin.conf")
3233
with open(conf_file, 'a', encoding='utf8') as f:
3334
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.')
35+
self.nodes[0].assert_start_raises_init_error(['-conf=' + conf_file], 'Error reading configuration file: specified data directory "' + re.escape(new_data_dir) + '" does not exist.')
3536

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

test/functional/feature_logging.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ 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)],
34-
"Error: Could not open debug log file")
33+
exp_stderr = "Error: Could not open debug log file \S+$"
34+
self.nodes[0].assert_start_raises_init_error(["-debuglogfile=%s" % (invalidname)], exp_stderr)
3535
assert not os.path.isfile(os.path.join(invdir, "foo.log"))
3636

3737
# check that invalid log (relative) works after path exists
@@ -44,8 +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],
48-
"Error: Could not open debug log file")
47+
self.nodes[0].assert_start_raises_init_error(["-debuglogfile=%s" % invalidname], exp_stderr)
4948
assert not os.path.isfile(os.path.join(invdir, "foo.log"))
5049

5150
# check that invalid log (absolute) works after path exists

test/functional/feature_uacomment.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55
"""Test the -uacomment option."""
66

7+
import re
8+
79
from test_framework.test_framework import BitcoinTestFramework
810
from test_framework.util import assert_equal
911

12+
1013
class UacommentTest(BitcoinTestFramework):
1114
def set_test_params(self):
1215
self.num_nodes = 1
@@ -23,13 +26,14 @@ def run_test(self):
2326

2427
self.log.info("test -uacomment max length")
2528
self.stop_node(0)
26-
expected = "exceeds maximum length (256). Reduce the number or size of uacomments."
27-
self.assert_start_raises_init_error(0, ["-uacomment=" + 'a' * 256], expected)
29+
expected = "Error: Total length of network version string \([0-9]+\) exceeds maximum length \(256\). Reduce the number or size of uacomments."
30+
self.nodes[0].assert_start_raises_init_error(["-uacomment=" + 'a' * 256], expected)
2831

2932
self.log.info("test -uacomment unsafe characters")
3033
for unsafe_char in ['/', ':', '(', ')']:
31-
expected = "User Agent comment (" + unsafe_char + ") contains unsafe characters"
32-
self.assert_start_raises_init_error(0, ["-uacomment=" + unsafe_char], expected)
34+
expected = "Error: User Agent comment \(" + re.escape(unsafe_char) + "\) contains unsafe characters."
35+
self.nodes[0].assert_start_raises_init_error(["-uacomment=" + unsafe_char], expected)
36+
3337

3438
if __name__ == '__main__':
3539
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: 36 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,41 @@ 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, partial_match=False, *args, **kwargs):
170+
"""Attempt to start the node and expect it to raise an error.
171+
172+
extra_args: extra arguments to pass through to bitcoind
173+
expected_msg: regex that stderr should match when bitcoind fails
174+
175+
Will throw if bitcoind starts without an error.
176+
Will throw if an expected_msg is provided and it does not match bitcoind's stdout."""
177+
with tempfile.SpooledTemporaryFile(max_size=2**16) as log_stderr:
178+
try:
179+
self.start(extra_args, stderr=log_stderr, *args, **kwargs)
180+
self.wait_for_rpc_connection()
181+
self.stop_node()
182+
self.wait_util_stopped()
183+
except Exception as e:
184+
assert 'bitcoind exited' in str(e) # node must have shutdown
185+
self.running = False
186+
self.process = None
187+
# Check stderr for expected message
188+
if expected_msg is not None:
189+
log_stderr.seek(0)
190+
stderr = log_stderr.read().decode('utf-8').strip()
191+
if partial_match:
192+
if re.search(expected_msg, stderr, flags=re.MULTILINE) is None:
193+
raise AssertionError('Expected message "{}" does not partially match stderr:\n"{}"'.format(expected_msg, stderr))
194+
else:
195+
if re.fullmatch(expected_msg, stderr) is None:
196+
raise AssertionError('Expected message "{}" does not fully match stderr:\n"{}"'.format(expected_msg, stderr))
197+
else:
198+
if expected_msg is None:
199+
assert_msg = "bitcoind should have exited with an error"
200+
else:
201+
assert_msg = "bitcoind should have exited with expected error " + expected_msg
202+
raise AssertionError(assert_msg)
203+
168204
def node_encrypt_wallet(self, passphrase):
169205
""""Encrypts the wallet.
170206

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'], "Error: Error loading : You can't disable HD on an 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: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
Verify that a bitcoind node can load multiple wallet files
88
"""
99
import os
10+
import re
1011
import shutil
1112

1213
from test_framework.test_framework import BitcoinTestFramework
@@ -60,29 +61,31 @@ def run_test(self):
6061
assert_equal(os.path.isfile(wallet_dir(wallet_name)), True)
6162

6263
# should not initialize if wallet path can't be created
63-
self.assert_start_raises_init_error(0, ['-wallet=wallet.dat/bad'], 'Not a directory')
64+
exp_stderr = "boost::filesystem::create_directory: (The system cannot find the path specified|Not a directory):"
65+
self.nodes[0].assert_start_raises_init_error(['-wallet=wallet.dat/bad'], exp_stderr, partial_match=True)
6466

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())
67+
self.nodes[0].assert_start_raises_init_error(['-walletdir=wallets'], 'Error: Specified -walletdir "wallets" does not exist')
68+
self.nodes[0].assert_start_raises_init_error(['-walletdir=wallets'], 'Error: Specified -walletdir "wallets" is a relative path', cwd=data_dir())
69+
self.nodes[0].assert_start_raises_init_error(['-walletdir=debug.log'], 'Error: Specified -walletdir "debug.log" is not a directory', cwd=data_dir())
6870

6971
# 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.')
72+
self.nodes[0].assert_start_raises_init_error(['-wallet=w1', '-wallet=w1'], 'Error: Error loading wallet w1. Duplicate -wallet filename specified.')
7173

7274
# should not initialize if one wallet is a copy of another
7375
shutil.copyfile(wallet_dir('w8'), wallet_dir('w8_copy'))
74-
self.assert_start_raises_init_error(0, ['-wallet=w8', '-wallet=w8_copy'], 'duplicates fileid')
76+
exp_stderr = "CDB: Can't open database w8_copy \(duplicates fileid \w+ from w8\)"
77+
self.nodes[0].assert_start_raises_init_error(['-wallet=w8', '-wallet=w8_copy'], exp_stderr, partial_match=True)
7578

7679
# should not initialize if wallet file is a symlink
7780
os.symlink('w8', wallet_dir('w8_symlink'))
78-
self.assert_start_raises_init_error(0, ['-wallet=w8_symlink'], 'Invalid -wallet path')
81+
self.nodes[0].assert_start_raises_init_error(['-wallet=w8_symlink'], 'Error: Invalid -wallet path \'w8_symlink\'\. .*')
7982

8083
# 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')
84+
self.nodes[0].assert_start_raises_init_error(['-walletdir=bad'], 'Error: Specified -walletdir "bad" does not exist')
8285
# should not initialize if the specified walletdir is not a directory
8386
not_a_dir = wallet_dir('notadir')
8487
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')
88+
self.nodes[0].assert_start_raises_init_error(['-walletdir=' + not_a_dir], 'Error: Specified -walletdir "' + re.escape(not_a_dir) + '" is not a directory')
8689

8790
# if wallets/ doesn't exist, datadir should be the default wallet dir
8891
wallet_dir2 = data_dir('walletdir')
@@ -102,8 +105,9 @@ def run_test(self):
102105

103106
competing_wallet_dir = os.path.join(self.options.tmpdir, 'competing_walletdir')
104107
os.mkdir(competing_wallet_dir)
105-
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')
108+
self.restart_node(0, ['-walletdir=' + competing_wallet_dir])
109+
exp_stderr = "Error: Error initializing wallet database environment \"\S+competing_walletdir\"!"
110+
self.nodes[1].assert_start_raises_init_error(['-walletdir=' + competing_wallet_dir], exp_stderr, partial_match=True)
107111

108112
self.restart_node(0, extra_args)
109113

0 commit comments

Comments
 (0)