assert_start_raises_init_error make it possible to test expected error condition when bitcoind is starting.
ping @MarcoFalke and @jonasschnelli mentioned the need on #9662 and https://github.com/bitcoin/bitcoin/pull/9728
assert_start_raises_init_error make it possible to test expected error condition when bitcoind is starting.
ping @MarcoFalke and @jonasschnelli mentioned the need on #9662 and https://github.com/bitcoin/bitcoin/pull/9728
338@@ -338,7 +339,10 @@ def start_node(i, dirname, extra_args=None, rpchost=None, timewait=None, binary=
339 binary = os.getenv("BITCOIND", "bitcoind")
340 args = [ binary, "-datadir="+datadir, "-server", "-keypool=1", "-discover=0", "-rest", "-mocktime="+str(get_mocktime()) ]
341 if extra_args is not None: args.extend(extra_args)
342- bitcoind_processes[i] = subprocess.Popen(args)
343+ if stderr is not None:
bitcoind_processes[i] = subprocess.Popen(args, stderr=stderr)
364+ if expected_msg is None:
365+ assert_msg = "bitcoind should have exited with an error"
366+ else:
367+ assert_msg = "bitcoind should have exited with expected error " + expected_msg
368+ raise AssertionError(assert_msg)
369+ except AssertionError as e:
yuck! raising an exception in expectation of catching it in the very next line.
I think try/except/else is a much cleaner construction, as in:
0try:
1 thing_we_expect_to_fail()
2except ExceptionType as e:
3 do_some_asserts_on_the_exception_raised()
4else:
5 # oops, we were expecting an exception
6 assert False, "we were expecting an exception!"
355@@ -352,6 +356,28 @@ def start_node(i, dirname, extra_args=None, rpchost=None, timewait=None, binary=
356
357 return proxy
358
359+def assert_start_raises_init_error(i, dirname, extra_args=None, expected_msg=None):
360+ log_stderr = tempfile.SpooledTemporaryFile(max_size=2**16)
with log_stderr as...
form. That means the file will be closed automatically (even if an exception is raised on the way), and you won’t have to close it yourself with the finally
branch at the bottom.
367+ assert_msg = "bitcoind should have exited with expected error " + expected_msg
368+ raise AssertionError(assert_msg)
369+ except AssertionError as e:
370+ raise
371+ except Exception as e:
372+ assert('bitcoind exited' in str(e)) #node must have shutdown
minor nit: assert is a statement, not a function. This should be:
assert ‘bitcoind exited’ in str(e)
352@@ -352,6 +353,25 @@ def start_node(i, dirname, extra_args=None, rpchost=None, timewait=None, binary=
353
354 return proxy
355
356+def assert_start_raises_init_error(i, dirname, extra_args=None, expected_msg=None):
357+ with tempfile.SpooledTemporaryFile(max_size=2**16) as log_stderr:
358+ try:
359+ node = start_node(i, dirname, extra_args, stderr=log_stderr)
360+ stop_node(node, i)
It does need to be stopped, or this function would not be side effect free.
EDIT: Ah no it does not, as if it passes here, the test is finished, which would close the node anyway.