|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) 2017 The Bitcoin Core developers |
| 3 | +# Distributed under the MIT software license, see the accompanying |
| 4 | +# file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 5 | +"""Test various command line arguments and configuration file parameters.""" |
| 6 | + |
| 7 | +import os |
| 8 | + |
| 9 | +from test_framework.test_framework import BitcoinTestFramework |
| 10 | +from test_framework.util import get_datadir_path |
| 11 | + |
| 12 | +class ConfArgsTest(BitcoinTestFramework): |
| 13 | + def set_test_params(self): |
| 14 | + self.setup_clean_chain = True |
| 15 | + self.num_nodes = 1 |
| 16 | + |
| 17 | + def run_test(self): |
| 18 | + self.stop_node(0) |
| 19 | + # Remove the -datadir argument so it doesn't override the config file |
| 20 | + self.nodes[0].args = [arg for arg in self.nodes[0].args if not arg.startswith("-datadir")] |
| 21 | + |
| 22 | + default_data_dir = get_datadir_path(self.options.tmpdir, 0) |
| 23 | + new_data_dir = os.path.join(default_data_dir, 'newdatadir') |
| 24 | + new_data_dir_2 = os.path.join(default_data_dir, 'newdatadir2') |
| 25 | + |
| 26 | + # Check that using -datadir argument on non-existent directory fails |
| 27 | + 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 | + |
| 30 | + # Check that using non-existent datadir in conf file fails |
| 31 | + conf_file = os.path.join(default_data_dir, "bitcoin.conf") |
| 32 | + with open(conf_file, 'a', encoding='utf8') as f: |
| 33 | + 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 | + |
| 36 | + # Create the directory and ensure the config file now works |
| 37 | + os.mkdir(new_data_dir) |
| 38 | + self.start_node(0, ['-conf='+conf_file, '-wallet=w1']) |
| 39 | + self.stop_node(0) |
| 40 | + assert os.path.isfile(os.path.join(new_data_dir, 'regtest', 'wallets', 'w1')) |
| 41 | + |
| 42 | + # Ensure command line argument overrides datadir in conf |
| 43 | + os.mkdir(new_data_dir_2) |
| 44 | + self.nodes[0].datadir = new_data_dir_2 |
| 45 | + self.start_node(0, ['-datadir='+new_data_dir_2, '-conf='+conf_file, '-wallet=w2']) |
| 46 | + assert os.path.isfile(os.path.join(new_data_dir_2, 'regtest', 'wallets', 'w2')) |
| 47 | + |
| 48 | +if __name__ == '__main__': |
| 49 | + ConfArgsTest().main() |
0 commit comments