/ test / functional / feature_filelock.py
feature_filelock.py
 1  #!/usr/bin/env python3
 2  # Copyright (c) 2018-present 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  """Check that it's not possible to start a second bitcoind instance using the same datadir or wallet."""
 6  import random
 7  import string
 8  
 9  from test_framework.test_framework import BitcoinTestFramework
10  from test_framework.test_node import (
11      BITCOIN_PID_FILENAME_DEFAULT,
12      ErrorMatch,
13  )
14  
15  class FilelockTest(BitcoinTestFramework):
16      def set_test_params(self):
17          self.setup_clean_chain = True
18          self.num_nodes = 2
19          self.uses_wallet = None
20  
21      def setup_network(self):
22          self.add_nodes(self.num_nodes, extra_args=None)
23          self.nodes[0].start()
24          self.nodes[0].wait_for_rpc_connection()
25  
26      def run_test(self):
27          datadir = self.nodes[0].chain_path
28          blocksdir = self.nodes[0].blocks_path
29          self.log.info(f"Using datadir {datadir}")
30          self.log.info(f"Using blocksdir {blocksdir}")
31  
32          self.log.info("Check that we can't start a second bitcoind instance using the same datadir")
33          expected_msg = f"Error: Cannot obtain a lock on directory {datadir}. {self.config['environment']['CLIENT_NAME']} is probably already running."
34          self.nodes[1].assert_start_raises_init_error(extra_args=[f'-datadir={self.nodes[0].datadir_path}', '-noserver'], expected_msg=expected_msg)
35  
36          self.log.info("Check that we can't start a second bitcoind instance using the same blocksdir")
37          expected_msg = f"Error: Cannot obtain a lock on directory {blocksdir}. {self.config['environment']['CLIENT_NAME']} is probably already running."
38          self.nodes[1].assert_start_raises_init_error(extra_args=[f'-blocksdir={self.nodes[0].datadir_path}', '-noserver'], expected_msg=expected_msg)
39  
40          self.log.info("Check that cookie and PID file are not deleted when attempting to start a second bitcoind using the same datadir/blocksdir")
41          cookie_file = datadir / ".cookie"
42          assert cookie_file.exists()  # should not be deleted during the second bitcoind instance shutdown
43          pid_file = datadir / BITCOIN_PID_FILENAME_DEFAULT
44          assert pid_file.exists()
45  
46          if self.is_wallet_compiled():
47              wallet_name = ''.join([random.choice(string.ascii_lowercase) for _ in range(6)])
48              self.nodes[0].createwallet(wallet_name=wallet_name)
49              wallet_dir = self.nodes[0].wallets_path
50              self.log.info("Check that we can't start a second bitcoind instance using the same wallet")
51              expected_msg = f"Error: SQLiteDatabase: Unable to obtain an exclusive lock on the database, is it being used by another instance of {self.config['environment']['CLIENT_NAME']}?"
52              self.nodes[1].assert_start_raises_init_error(extra_args=[f'-walletdir={wallet_dir}', f'-wallet={wallet_name}', '-noserver'], expected_msg=expected_msg, match=ErrorMatch.PARTIAL_REGEX)
53  
54  if __name__ == '__main__':
55      FilelockTest(__file__).main()