You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_framework.py 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #!/usr/bin/env python2
  2. # Copyright (c) 2014 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. # Base class for RPC testing
  6. # Add python-bitcoinrpc to module search path:
  7. import os
  8. import sys
  9. sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), "python-bitcoinrpc"))
  10. import shutil
  11. import tempfile
  12. import traceback
  13. from bitcoinrpc.authproxy import AuthServiceProxy, JSONRPCException
  14. from util import *
  15. class BitcoinTestFramework(object):
  16. # These may be over-ridden by subclasses:
  17. def run_test(self):
  18. for node in self.nodes:
  19. assert_equal(node.getblockcount(), 200)
  20. assert_equal(node.getbalance(), 25*50)
  21. def add_options(self, parser):
  22. pass
  23. def setup_chain(self):
  24. print("Initializing test directory "+self.options.tmpdir)
  25. initialize_chain(self.options.tmpdir)
  26. def setup_network(self, split = False):
  27. self.nodes = start_nodes(4, self.options.tmpdir)
  28. # Connect the nodes as a "chain". This allows us
  29. # to split the network between nodes 1 and 2 to get
  30. # two halves that can work on competing chains.
  31. # If we joined network halves, connect the nodes from the joint
  32. # on outward. This ensures that chains are properly reorganised.
  33. if not split:
  34. connect_nodes_bi(self.nodes, 1, 2)
  35. sync_blocks(self.nodes[1:3])
  36. sync_mempools(self.nodes[1:3])
  37. connect_nodes_bi(self.nodes, 0, 1)
  38. connect_nodes_bi(self.nodes, 2, 3)
  39. self.is_network_split = split
  40. self.sync_all()
  41. def split_network(self):
  42. """
  43. Split the network of four nodes into nodes 0/1 and 2/3.
  44. """
  45. assert not self.is_network_split
  46. stop_nodes(self.nodes)
  47. wait_bitcoinds()
  48. self.setup_network(True)
  49. def sync_all(self):
  50. if self.is_network_split:
  51. sync_blocks(self.nodes[:2])
  52. sync_blocks(self.nodes[2:])
  53. sync_mempools(self.nodes[:2])
  54. sync_mempools(self.nodes[2:])
  55. else:
  56. sync_blocks(self.nodes)
  57. sync_mempools(self.nodes)
  58. def join_network(self):
  59. """
  60. Join the (previously split) network halves together.
  61. """
  62. assert self.is_network_split
  63. stop_nodes(self.nodes)
  64. wait_bitcoinds()
  65. self.setup_network(False)
  66. def main(self):
  67. import optparse
  68. parser = optparse.OptionParser(usage="%prog [options]")
  69. parser.add_option("--nocleanup", dest="nocleanup", default=False, action="store_true",
  70. help="Leave bitcoinds and test.* datadir on exit or error")
  71. parser.add_option("--srcdir", dest="srcdir", default="../../src",
  72. help="Source directory containing bitcoind/bitcoin-cli (default: %default%)")
  73. parser.add_option("--tmpdir", dest="tmpdir", default=tempfile.mkdtemp(prefix="test"),
  74. help="Root directory for datadirs")
  75. parser.add_option("--tracerpc", dest="trace_rpc", default=False, action="store_true",
  76. help="Print out all RPC calls as they are made")
  77. self.add_options(parser)
  78. (self.options, self.args) = parser.parse_args()
  79. if self.options.trace_rpc:
  80. import logging
  81. logging.basicConfig(level=logging.DEBUG)
  82. os.environ['PATH'] = self.options.srcdir+":"+os.environ['PATH']
  83. check_json_precision()
  84. success = False
  85. try:
  86. if not os.path.isdir(self.options.tmpdir):
  87. os.makedirs(self.options.tmpdir)
  88. self.setup_chain()
  89. self.setup_network()
  90. self.run_test()
  91. success = True
  92. except JSONRPCException as e:
  93. print("JSONRPC error: "+e.error['message'])
  94. traceback.print_tb(sys.exc_info()[2])
  95. except AssertionError as e:
  96. print("Assertion failed: "+e.message)
  97. traceback.print_tb(sys.exc_info()[2])
  98. except Exception as e:
  99. print("Unexpected exception caught during testing: "+str(e))
  100. traceback.print_tb(sys.exc_info()[2])
  101. if not self.options.nocleanup:
  102. print("Cleaning up")
  103. stop_nodes(self.nodes)
  104. wait_bitcoinds()
  105. shutil.rmtree(self.options.tmpdir)
  106. if success:
  107. print("Tests successful")
  108. sys.exit(0)
  109. else:
  110. print("Failed")
  111. sys.exit(1)