feature_framework_testshell.py raw

   1  #!/usr/bin/env python3
   2  # Copyright (c) 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 for the `TestShell` submodule."""
   6  
   7  from contextlib import redirect_stdout
   8  from decimal import Decimal
   9  from io import StringIO
  10  from pathlib import Path
  11  
  12  # Note that we need to import from functional test framework modules
  13  # *after* extending the Python path via sys.path.insert(0, ...) below,
  14  # in order to work with the full symlinked (unresolved) path within the
  15  # build directory (usually ./build/test/functional).
  16  
  17  
  18  # Test matching the minimal example from the documentation. Should be kept
  19  # in sync with the interactive shell instructions ('>>> ') in test-shell.md.
  20  def run_testshell_doc_example(functional_tests_dir):
  21      import sys
  22      sys.path.insert(0, functional_tests_dir)
  23      from test_framework.test_shell import TestShell
  24      from test_framework.util import assert_equal
  25  
  26      test = TestShell().setup(num_nodes=2, setup_clean_chain=True)
  27      try:
  28          assert test is not None
  29          stdout_buf = StringIO()
  30          with redirect_stdout(stdout_buf):
  31              test2 = TestShell().setup()
  32          assert test2 is None
  33          assert_equal(stdout_buf.getvalue().rstrip(), "TestShell is already running!")
  34          assert_equal(test.nodes[0].getblockchaininfo()["blocks"], 0)
  35          if test.is_wallet_compiled():
  36              res = test.nodes[0].createwallet('default')
  37              assert_equal(res, {'name': 'default'})
  38              address = test.nodes[0].getnewaddress()
  39              res = test.generatetoaddress(test.nodes[0], 101, address)
  40              assert_equal(len(res), 101)
  41              test.sync_blocks()
  42              assert_equal(test.nodes[1].getblockchaininfo()["blocks"], 101)
  43              assert_equal(test.nodes[0].getbalance(), Decimal('50.0'))
  44              test.nodes[0].log.info("Successfully mined regtest chain!")
  45      finally:
  46          test.shutdown()
  47          test.reset()
  48          assert test.num_nodes is None
  49  
  50  
  51  if __name__ == "__main__":
  52      run_testshell_doc_example(str(Path(__file__).parent))
  53