feature_includeconf.py raw

   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  """Tests the includeconf argument
   6  
   7  Verify that:
   8  
   9  1. adding includeconf to the configuration file causes the includeconf
  10     file to be loaded in the correct order.
  11  2. includeconf cannot be used as a command line argument.
  12  3. includeconf cannot be used recursively (ie includeconf can only
  13     be used from the base config file).
  14  4. multiple includeconf arguments can be specified in the main config
  15     file.
  16  """
  17  from test_framework.test_framework import BitcoinTestFramework
  18  
  19  
  20  class IncludeConfTest(BitcoinTestFramework):
  21      def set_test_params(self):
  22          self.num_nodes = 1
  23  
  24      def run_test(self):
  25          # Create additional config files
  26          # - tmpdir/node0/relative.conf
  27          with open(self.nodes[0].datadir_path / "relative.conf", "w") as f:
  28              f.write("uacomment=relative\n")
  29          # - tmpdir/node0/relative2.conf
  30          with open(self.nodes[0].datadir_path / "relative2.conf", "w") as f:
  31              f.write("uacomment=relative2\n")
  32          with open(self.nodes[0].datadir_path / "bitcoin.conf", "a") as f:
  33              f.write("uacomment=main\nincludeconf=relative.conf\n")
  34          self.restart_node(0)
  35  
  36          self.log.info("-includeconf works from config file. subversion should end with 'main; relative)/'")
  37  
  38          subversion = self.nodes[0].getnetworkinfo()["subversion"]
  39          assert subversion.endswith("main; relative)/")
  40  
  41          self.log.info("-includeconf cannot be used as command-line arg")
  42          self.stop_node(0)
  43          self.nodes[0].assert_start_raises_init_error(
  44              extra_args=['-noincludeconf=0'],
  45              expected_msg='Error: Error parsing command line arguments: -includeconf cannot be used from commandline; -includeconf=true',
  46          )
  47          self.nodes[0].assert_start_raises_init_error(
  48              extra_args=['-includeconf=relative2.conf', '-includeconf=no_warn.conf'],
  49              expected_msg='Error: Error parsing command line arguments: -includeconf cannot be used from commandline; -includeconf="relative2.conf"',
  50          )
  51  
  52          self.log.info("-includeconf cannot be used recursively. subversion should end with 'main; relative)/'")
  53          with open(self.nodes[0].datadir_path / "relative.conf", "a") as f:
  54              f.write("includeconf=relative2.conf\n")
  55          self.start_node(0)
  56  
  57          subversion = self.nodes[0].getnetworkinfo()["subversion"]
  58          assert subversion.endswith("main; relative)/")
  59          self.stop_node(0, expected_stderr="warning: -includeconf cannot be used from included files; ignoring -includeconf=relative2.conf")
  60  
  61          self.log.info("-includeconf cannot contain invalid arg")
  62  
  63          # Commented out as long as we ignore invalid arguments in configuration files
  64          #with open(self.nodes[0].datadir_path / "relative.conf", "w") as f:
  65          #    f.write("foo=bar\n")
  66          #self.nodes[0].assert_start_raises_init_error(expected_msg="Error: Error reading configuration file: Invalid configuration value foo")
  67  
  68          self.log.info("-includeconf cannot be invalid path")
  69          (self.nodes[0].datadir_path / "relative.conf").unlink()
  70          self.nodes[0].assert_start_raises_init_error(expected_msg="Error: Error reading configuration file: Failed to include configuration file relative.conf")
  71  
  72          self.log.info("multiple -includeconf args can be used from the base config file. subversion should end with 'main; relative; relative2)/'")
  73          with open(self.nodes[0].datadir_path / "relative.conf", "w") as f:
  74              # Restore initial file contents
  75              f.write("uacomment=relative\n")
  76  
  77          with open(self.nodes[0].datadir_path / "bitcoin.conf", "a") as f:
  78              f.write("includeconf=relative2.conf\n")
  79  
  80          self.start_node(0)
  81  
  82          subversion = self.nodes[0].getnetworkinfo()["subversion"]
  83          assert subversion.endswith("main; relative; relative2)/")
  84  
  85  if __name__ == '__main__':
  86      IncludeConfTest(__file__).main()
  87