From 496787af0482898136c9fe3ccb310b7e053c03d2 Mon Sep 17 00:00:00 2001 From: John Newbery Date: Wed, 7 Feb 2018 10:38:25 -0500 Subject: [PATCH] [Tests] Require exact match in assert_start_raises_init_eror() --- test/functional/feature_blocksdir.py | 8 +++++--- test/functional/feature_config_args.py | 6 +++--- test/functional/feature_logging.py | 7 +++---- test/functional/feature_uacomment.py | 7 +++++-- test/functional/test_framework/test_node.py | 11 ++++++++--- test/functional/wallet_multiwallet.py | 21 +++++++++++++++------ 6 files changed, 39 insertions(+), 21 deletions(-) diff --git a/test/functional/feature_blocksdir.py b/test/functional/feature_blocksdir.py index ce5333a4fdcfa..c04867e457f5a 100755 --- a/test/functional/feature_blocksdir.py +++ b/test/functional/feature_blocksdir.py @@ -22,10 +22,12 @@ def run_test(self): shutil.rmtree(self.nodes[0].datadir) initialize_datadir(self.options.tmpdir, 0) self.log.info("Starting with nonexistent blocksdir ...") - self.nodes[0].assert_start_raises_init_error(["-blocksdir="+self.options.tmpdir+ "/blocksdir"], "Specified blocks director") - os.mkdir(self.options.tmpdir+ "/blocksdir") + blocksdir_path = os.path.join(self.options.tmpdir, 'blocksdir') + expected_err = "Error: Specified blocks directory \"{}\" does not exist.\n".format(blocksdir_path) + self.nodes[0].assert_start_raises_init_error(["-blocksdir=" + blocksdir_path], expected_err) + os.mkdir(blocksdir_path) self.log.info("Starting with existing blocksdir ...") - self.start_node(0, ["-blocksdir="+self.options.tmpdir+ "/blocksdir"]) + self.start_node(0, ["-blocksdir=" + blocksdir_path]) self.log.info("mining blocks..") self.nodes[0].generate(10) assert(os.path.isfile(os.path.join(self.options.tmpdir, "blocksdir", "regtest", "blocks", "blk00000.dat"))) diff --git a/test/functional/feature_config_args.py b/test/functional/feature_config_args.py index b29177807e29e..3e228753d4fbc 100755 --- a/test/functional/feature_config_args.py +++ b/test/functional/feature_config_args.py @@ -5,6 +5,7 @@ """Test various command line arguments and configuration file parameters.""" import os +import re from test_framework.test_framework import PivxTestFramework from test_framework.util import get_datadir_path @@ -26,7 +27,7 @@ def run_test(self): # Check that using -datadir argument on non-existent directory fails self.nodes[0].datadir = new_data_dir - self.nodes[0].assert_start_raises_init_error(['-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 "' + re.escape(new_data_dir) + '" does not exist.') # Check that using non-existent datadir in conf file fails conf_file = os.path.join(default_data_dir, "pivx.conf") @@ -37,8 +38,7 @@ def run_test(self): f.write("datadir=" + new_data_dir + "\n") f.write(conf_file_contents) - # Temporarily disabled, because this test would access the user's home dir (~/.pivx) - #self.nodes[0].assert_start_raises_init_error(['-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 "' + re.escape(new_data_dir) + '" does not exist.') # Create the directory and ensure the config file now works os.mkdir(new_data_dir) diff --git a/test/functional/feature_logging.py b/test/functional/feature_logging.py index 577d8e04c1658..f9fc6e4efc1ef 100755 --- a/test/functional/feature_logging.py +++ b/test/functional/feature_logging.py @@ -35,8 +35,8 @@ def run_test(self): invdir = os.path.join(self.nodes[0].datadir, "regtest", "foo") invalidname = os.path.join("foo", "foo.log") self.stop_node(0) - self.nodes[0].assert_start_raises_init_error(["-debuglogfile=%s" % (invalidname)], - "Error: Could not open debug log file") + exp_stderr = "Error: Could not open debug log file \S+$" + self.nodes[0].assert_start_raises_init_error(["-debuglogfile=%s" % (invalidname)], exp_stderr) assert not os.path.isfile(os.path.join(invdir, "foo.log")) self.log.info("Invalid relative filename throws") @@ -50,8 +50,7 @@ def run_test(self): self.stop_node(0) invdir = os.path.join(self.options.tmpdir, "foo") invalidname = os.path.join(invdir, "foo.log") - self.nodes[0].assert_start_raises_init_error(["-debuglogfile=%s" % invalidname], - "Error: Could not open debug log file") + self.nodes[0].assert_start_raises_init_error(["-debuglogfile=%s" % invalidname], exp_stderr) assert not os.path.isfile(os.path.join(invdir, "foo.log")) self.log.info("Invalid absolute filename throws") diff --git a/test/functional/feature_uacomment.py b/test/functional/feature_uacomment.py index 78409af6f668f..4e9011778d19c 100755 --- a/test/functional/feature_uacomment.py +++ b/test/functional/feature_uacomment.py @@ -4,6 +4,8 @@ # file COPYING or http://www.opensource.org/licenses/mit-license.php. """Test the -uacomment option.""" +import re + from test_framework.test_framework import PivxTestFramework from test_framework.util import assert_equal @@ -23,13 +25,14 @@ def run_test(self): self.log.info("test -uacomment max length") self.stop_node(0) - expected = "exceeds maximum length (256). Reduce the number or size of -uacomment." + expected = "Error: Total length of network version string \([0-9]+\) exceeds maximum length \(256\). Reduce the number or size of -uacomment." self.nodes[0].assert_start_raises_init_error(["-uacomment=" + 'a' * 256], expected) self.log.info("test -uacomment unsafe characters") for unsafe_char in ['/', ':', '(', ')']: - expected = "User Agent comment (" + unsafe_char + ") contains unsafe characters" + expected = "Error: User Agent comment \(" + re.escape(unsafe_char) + "\) contains unsafe characters." self.nodes[0].assert_start_raises_init_error(["-uacomment=" + unsafe_char], expected) + if __name__ == '__main__': UacommentTest().main() diff --git a/test/functional/test_framework/test_node.py b/test/functional/test_framework/test_node.py index f402fa2858813..7e7b6c873ca78 100755 --- a/test/functional/test_framework/test_node.py +++ b/test/functional/test_framework/test_node.py @@ -212,8 +212,12 @@ def wait_until_stopped(self, timeout=BITCOIND_PROC_WAIT_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. + + extra_args: extra arguments to pass through to bitcoind + expected_msg: regex that stderr should match when bitcoind fails + Will throw if pivxd starts without an error. - Will throw if an expected_msg is provided and it does not appear in pivxd's stdout.""" + Will throw if an expected_msg is provided and it does not match pivxd's stdout.""" with tempfile.SpooledTemporaryFile(max_size=2**16) as log_stderr: try: self.start(extra_args, stderr=log_stderr, *args, **kwargs) @@ -224,11 +228,12 @@ def assert_start_raises_init_error(self, extra_args=None, expected_msg=None, *ar assert 'pivxd exited' in str(e) # node must have shutdown self.running = False self.process = None + # Check stderr for expected message 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) + if re.fullmatch(expected_msg + '\n', stderr) is None: + raise AssertionError('Expected message "{}" does not match stderr:\n"{}"'.format(expected_msg, stderr)) else: if expected_msg is None: assert_msg = "pivxd should have exited with an error" diff --git a/test/functional/wallet_multiwallet.py b/test/functional/wallet_multiwallet.py index b92dee0d171c2..c198bcc9462d5 100755 --- a/test/functional/wallet_multiwallet.py +++ b/test/functional/wallet_multiwallet.py @@ -65,22 +65,30 @@ def wallet_file(name): assert_equal(os.path.isfile(wallet_file(wallet_name)), True) # should not initialize if wallet path can't be created - self.nodes[0].assert_start_raises_init_error(['-wallet=wallet.dat/bad'], 'Not a directory') + exp_stderr = "\n\n\*+\n" + \ + "EXCEPTION: .*\n" + \ + "boost::filesystem::create_directory: Not a directory:.*\n" + \ + "pivx in .*\n" + self.nodes[0].assert_start_raises_init_error(['-wallet=wallet.dat/bad'], exp_stderr) self.nodes[0].assert_start_raises_init_error(['-walletdir=wallets'], 'Error: Specified -walletdir "wallets" does not exist') self.nodes[0].assert_start_raises_init_error(['-walletdir=wallets'], 'Error: Specified -walletdir "wallets" is a relative path', 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 - self.nodes[0].assert_start_raises_init_error(['-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: Error loading wallet w1. Duplicate -wallet filename specified.') # should not initialize if one wallet is a copy of another shutil.copyfile(wallet_dir('w8'), wallet_dir('w8_copy')) - self.nodes[0].assert_start_raises_init_error(['-wallet=w8', '-wallet=w8_copy'], 'duplicates fileid') + exp_stderr = "\n\n\*+\n" + \ + "EXCEPTION: .*\n" + \ + "CDB: Can't open database w8_copy \(duplicates fileid \w+ from w8\)\s*\n" + \ + "pivx in .*\n" + self.nodes[0].assert_start_raises_init_error(['-wallet=w8', '-wallet=w8_copy'], exp_stderr) # should not initialize if wallet file is a symlink os.symlink('w8', wallet_dir('w8_symlink')) - self.nodes[0].assert_start_raises_init_error(['-wallet=w8_symlink'], 'Invalid -wallet path') + self.nodes[0].assert_start_raises_init_error(['-wallet=w8_symlink'], 'Error: Invalid -wallet path \'w8_symlink\'\. .*') # should not initialize if the specified walletdir does not exist self.nodes[0].assert_start_raises_init_error(['-walletdir=bad'], 'Error: Specified -walletdir "bad" does not exist') @@ -120,8 +128,9 @@ def wallet_file(name): competing_wallet_dir = os.path.join(self.options.tmpdir, 'competing_walletdir') os.mkdir(competing_wallet_dir) - self.restart_node(0, ['-walletdir='+competing_wallet_dir]) - self.nodes[1].assert_start_raises_init_error(['-walletdir='+competing_wallet_dir], 'Error initializing wallet database environment') + self.restart_node(0, ['-walletdir=' + competing_wallet_dir]) + exp_stderr = "Error: Error initializing wallet database environment \"\S+competing_walletdir\"!" + self.nodes[1].assert_start_raises_init_error(['-walletdir=' + competing_wallet_dir], exp_stderr) self.restart_node(0, extra_args)