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
Thanks. utACK 5737be099f83dfb367a342c36f7f0ae5ea31bc29. This is also required for #9662.
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:
I don't think this if/else construction is needed. subprocess.Popen's stderr argument defaults to None, so you can just call 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:
try:
thing_we_expect_to_fail()
except ExceptionType as e:
do_some_asserts_on_the_exception_raised()
else:
# oops, we were expecting an exception
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)
Consider opening the file using the 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)
nits of @jnewbery addressed
Please squash.
squashed @MarcoFalke
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)
Nit: No need to stop the node here. This is done by the test framework.
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.
Right this line is only executed in exceptional circumstances, in which case the whole frameworks shuts down.
Post merge utACK 025dec0e5bf001ba297f7430affe4098627ea5ce