[Tests] Move assert_start_raises_init_error method to TestNode

This commit is contained in:
John Newbery 2018-02-07 09:36:13 -05:00 committed by MarcoFalke
parent af20f9b1d4
commit 0ec08a672d
7 changed files with 44 additions and 37 deletions

View file

@ -25,13 +25,13 @@ class ConfArgsTest(BitcoinTestFramework):
# Check that using -datadir argument on non-existent directory fails # Check that using -datadir argument on non-existent directory fails
self.nodes[0].datadir = new_data_dir self.nodes[0].datadir = new_data_dir
self.assert_start_raises_init_error(0, ['-datadir='+new_data_dir], 'Error: Specified data directory "' + new_data_dir + '" does not exist.') self.nodes[0].assert_start_raises_init_error(['-datadir='+new_data_dir], 'Error: Specified data directory "' + new_data_dir + '" does not exist.')
# Check that using non-existent datadir in conf file fails # Check that using non-existent datadir in conf file fails
conf_file = os.path.join(default_data_dir, "bitcoin.conf") conf_file = os.path.join(default_data_dir, "bitcoin.conf")
with open(conf_file, 'a', encoding='utf8') as f: with open(conf_file, 'a', encoding='utf8') as f:
f.write("datadir=" + new_data_dir + "\n") f.write("datadir=" + new_data_dir + "\n")
self.assert_start_raises_init_error(0, ['-conf='+conf_file], 'Error reading configuration file: specified data directory "' + new_data_dir + '" does not exist.') self.nodes[0].assert_start_raises_init_error(['-conf='+conf_file], 'Error reading configuration file: specified data directory "' + new_data_dir + '" does not exist.')
# Create the directory and ensure the config file now works # Create the directory and ensure the config file now works
os.mkdir(new_data_dir) os.mkdir(new_data_dir)

View file

@ -30,7 +30,7 @@ class LoggingTest(BitcoinTestFramework):
invdir = os.path.join(self.nodes[0].datadir, "regtest", "foo") invdir = os.path.join(self.nodes[0].datadir, "regtest", "foo")
invalidname = os.path.join("foo", "foo.log") invalidname = os.path.join("foo", "foo.log")
self.stop_node(0) self.stop_node(0)
self.assert_start_raises_init_error(0, ["-debuglogfile=%s" % (invalidname)], self.nodes[0].assert_start_raises_init_error(["-debuglogfile=%s" % (invalidname)],
"Error: Could not open debug log file") "Error: Could not open debug log file")
assert not os.path.isfile(os.path.join(invdir, "foo.log")) assert not os.path.isfile(os.path.join(invdir, "foo.log"))
@ -44,7 +44,7 @@ class LoggingTest(BitcoinTestFramework):
self.stop_node(0) self.stop_node(0)
invdir = os.path.join(self.options.tmpdir, "foo") invdir = os.path.join(self.options.tmpdir, "foo")
invalidname = os.path.join(invdir, "foo.log") invalidname = os.path.join(invdir, "foo.log")
self.assert_start_raises_init_error(0, ["-debuglogfile=%s" % invalidname], self.nodes[0].assert_start_raises_init_error(["-debuglogfile=%s" % invalidname],
"Error: Could not open debug log file") "Error: Could not open debug log file")
assert not os.path.isfile(os.path.join(invdir, "foo.log")) assert not os.path.isfile(os.path.join(invdir, "foo.log"))

View file

@ -24,12 +24,12 @@ class UacommentTest(BitcoinTestFramework):
self.log.info("test -uacomment max length") self.log.info("test -uacomment max length")
self.stop_node(0) self.stop_node(0)
expected = "exceeds maximum length (256). Reduce the number or size of uacomments." expected = "exceeds maximum length (256). Reduce the number or size of uacomments."
self.assert_start_raises_init_error(0, ["-uacomment=" + 'a' * 256], expected) self.nodes[0].assert_start_raises_init_error(["-uacomment=" + 'a' * 256], expected)
self.log.info("test -uacomment unsafe characters") self.log.info("test -uacomment unsafe characters")
for unsafe_char in ['/', ':', '(', ')']: for unsafe_char in ['/', ':', '(', ')']:
expected = "User Agent comment (" + unsafe_char + ") contains unsafe characters" expected = "User Agent comment (" + unsafe_char + ") contains unsafe characters"
self.assert_start_raises_init_error(0, ["-uacomment=" + unsafe_char], expected) self.nodes[0].assert_start_raises_init_error(["-uacomment=" + unsafe_char], expected)
if __name__ == '__main__': if __name__ == '__main__':
UacommentTest().main() UacommentTest().main()

View file

@ -281,27 +281,6 @@ class BitcoinTestFramework():
self.stop_node(i) self.stop_node(i)
self.start_node(i, extra_args) self.start_node(i, extra_args)
def assert_start_raises_init_error(self, i, extra_args=None, expected_msg=None, *args, **kwargs):
with tempfile.SpooledTemporaryFile(max_size=2**16) as log_stderr:
try:
self.start_node(i, extra_args, stderr=log_stderr, *args, **kwargs)
self.stop_node(i)
except Exception as e:
assert 'bitcoind exited' in str(e) # node must have shutdown
self.nodes[i].running = False
self.nodes[i].process = None
if expected_msg is not None:
log_stderr.seek(0)
stderr = log_stderr.read().decode('utf-8')
if expected_msg not in stderr:
raise AssertionError("Expected error \"" + expected_msg + "\" not found in:\n" + stderr)
else:
if expected_msg is None:
assert_msg = "bitcoind should have exited with an error"
else:
assert_msg = "bitcoind should have exited with expected error " + expected_msg
raise AssertionError(assert_msg)
def wait_for_node_exit(self, i, timeout): def wait_for_node_exit(self, i, timeout):
self.nodes[i].process.wait(timeout) self.nodes[i].process.wait(timeout)

View file

@ -12,6 +12,7 @@ import logging
import os import os
import re import re
import subprocess import subprocess
import tempfile
import time import time
from .authproxy import JSONRPCException from .authproxy import JSONRPCException
@ -165,6 +166,33 @@ class TestNode():
def wait_until_stopped(self, timeout=BITCOIND_PROC_WAIT_TIMEOUT): def wait_until_stopped(self, timeout=BITCOIND_PROC_WAIT_TIMEOUT):
wait_until(self.is_node_stopped, timeout=timeout) wait_until(self.is_node_stopped, timeout=timeout)
def assert_start_raises_init_error(self, extra_args=None, expected_msg=None, *args, **kwargs):
"""Attempt to start the node and expect it to raise an error.
Will throw if bitcoind starts without an error.
Will throw if an expected_msg is provided and it does not appear in bitcoind's stdout."""
with tempfile.SpooledTemporaryFile(max_size=2**16) as log_stderr:
try:
self.start(extra_args, stderr=log_stderr, *args, **kwargs)
self.wait_for_rpc_connection()
self.stop_node()
self.wait_util_stopped()
except Exception as e:
assert 'bitcoind exited' in str(e) # node must have shutdown
self.running = False
self.process = None
if expected_msg is not None:
log_stderr.seek(0)
stderr = log_stderr.read().decode('utf-8')
if expected_msg not in stderr:
raise AssertionError("Expected error \"" + expected_msg + "\" not found in:\n" + stderr)
else:
if expected_msg is None:
assert_msg = "bitcoind should have exited with an error"
else:
assert_msg = "bitcoind should have exited with expected error " + expected_msg
raise AssertionError(assert_msg)
def node_encrypt_wallet(self, passphrase): def node_encrypt_wallet(self, passphrase):
""""Encrypts the wallet. """"Encrypts the wallet.

View file

@ -23,7 +23,7 @@ class WalletHDTest(BitcoinTestFramework):
# Make sure can't switch off usehd after wallet creation # Make sure can't switch off usehd after wallet creation
self.stop_node(1) self.stop_node(1)
self.assert_start_raises_init_error(1, ['-usehd=0'], 'already existing HD wallet') self.nodes[1].assert_start_raises_init_error(['-usehd=0'], 'already existing HD wallet')
self.start_node(1) self.start_node(1)
connect_nodes_bi(self.nodes, 0, 1) connect_nodes_bi(self.nodes, 0, 1)

View file

@ -62,27 +62,27 @@ class MultiWalletTest(BitcoinTestFramework):
# should not initialize if wallet path can't be created # should not initialize if wallet path can't be created
self.assert_start_raises_init_error(0, ['-wallet=wallet.dat/bad'], 'Not a directory') self.assert_start_raises_init_error(0, ['-wallet=wallet.dat/bad'], 'Not a directory')
self.assert_start_raises_init_error(0, ['-walletdir=wallets'], 'Error: Specified -walletdir "wallets" does not exist') self.nodes[0].assert_start_raises_init_error(['-walletdir=wallets'], 'Error: Specified -walletdir "wallets" does not exist')
self.assert_start_raises_init_error(0, ['-walletdir=wallets'], 'Error: Specified -walletdir "wallets" is a relative path', cwd=data_dir()) self.nodes[0].assert_start_raises_init_error(['-walletdir=wallets'], 'Error: Specified -walletdir "wallets" is a relative path', cwd=data_dir())
self.assert_start_raises_init_error(0, ['-walletdir=debug.log'], 'Error: Specified -walletdir "debug.log" is not a directory', cwd=data_dir()) self.nodes[0].assert_start_raises_init_error(['-walletdir=debug.log'], 'Error: Specified -walletdir "debug.log" is not a directory', cwd=data_dir())
# should not initialize if there are duplicate wallets # should not initialize if there are duplicate wallets
self.assert_start_raises_init_error(0, ['-wallet=w1', '-wallet=w1'], 'Error loading wallet w1. Duplicate -wallet filename specified.') self.nodes[0].assert_start_raises_init_error(['-wallet=w1', '-wallet=w1'], 'Error loading wallet w1. Duplicate -wallet filename specified.')
# should not initialize if one wallet is a copy of another # should not initialize if one wallet is a copy of another
shutil.copyfile(wallet_dir('w8'), wallet_dir('w8_copy')) shutil.copyfile(wallet_dir('w8'), wallet_dir('w8_copy'))
self.assert_start_raises_init_error(0, ['-wallet=w8', '-wallet=w8_copy'], 'duplicates fileid') self.nodes[0].assert_start_raises_init_error(['-wallet=w8', '-wallet=w8_copy'], 'duplicates fileid')
# should not initialize if wallet file is a symlink # should not initialize if wallet file is a symlink
os.symlink('w8', wallet_dir('w8_symlink')) os.symlink('w8', wallet_dir('w8_symlink'))
self.assert_start_raises_init_error(0, ['-wallet=w8_symlink'], 'Invalid -wallet path') self.nodes[0].assert_start_raises_init_error(['-wallet=w8_symlink'], 'Invalid -wallet path')
# should not initialize if the specified walletdir does not exist # should not initialize if the specified walletdir does not exist
self.assert_start_raises_init_error(0, ['-walletdir=bad'], 'Error: Specified -walletdir "bad" does not exist') self.nodes[0].assert_start_raises_init_error(['-walletdir=bad'], 'Error: Specified -walletdir "bad" does not exist')
# should not initialize if the specified walletdir is not a directory # should not initialize if the specified walletdir is not a directory
not_a_dir = wallet_dir('notadir') not_a_dir = wallet_dir('notadir')
open(not_a_dir, 'a').close() open(not_a_dir, 'a').close()
self.assert_start_raises_init_error(0, ['-walletdir=' + not_a_dir], 'Error: Specified -walletdir "' + not_a_dir + '" is not a directory') self.nodes[0].assert_start_raises_init_error(['-walletdir=' + not_a_dir], 'Error: Specified -walletdir "' + not_a_dir + '" is not a directory')
# if wallets/ doesn't exist, datadir should be the default wallet dir # if wallets/ doesn't exist, datadir should be the default wallet dir
wallet_dir2 = data_dir('walletdir') wallet_dir2 = data_dir('walletdir')
@ -103,7 +103,7 @@ class MultiWalletTest(BitcoinTestFramework):
competing_wallet_dir = os.path.join(self.options.tmpdir, 'competing_walletdir') competing_wallet_dir = os.path.join(self.options.tmpdir, 'competing_walletdir')
os.mkdir(competing_wallet_dir) os.mkdir(competing_wallet_dir)
self.restart_node(0, ['-walletdir='+competing_wallet_dir]) self.restart_node(0, ['-walletdir='+competing_wallet_dir])
self.assert_start_raises_init_error(1, ['-walletdir='+competing_wallet_dir], 'Error initializing wallet database environment') self.nodes[1].assert_start_raises_init_error(['-walletdir='+competing_wallet_dir], 'Error initializing wallet database environment')
self.restart_node(0, extra_args) self.restart_node(0, extra_args)