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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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_nodes(self):
  27. return start_nodes(4, self.options.tmpdir)
  28. def setup_network(self, split = False):
  29. self.nodes = self.setup_nodes()
  30. # Connect the nodes as a "chain". This allows us
  31. # to split the network between nodes 1 and 2 to get
  32. # two halves that can work on competing chains.
  33. # If we joined network halves, connect the nodes from the joint
  34. # on outward. This ensures that chains are properly reorganised.
  35. if not split:
  36. connect_nodes_bi(self.nodes, 1, 2)
  37. sync_blocks(self.nodes[1:3])
  38. sync_mempools(self.nodes[1:3])
  39. connect_nodes_bi(self.nodes, 0, 1)
  40. connect_nodes_bi(self.nodes, 2, 3)
  41. self.is_network_split = split
  42. self.sync_all()
  43. def split_network(self):
  44. """
  45. Split the network of four nodes into nodes 0/1 and 2/3.
  46. """
  47. assert not self.is_network_split
  48. stop_nodes(self.nodes)
  49. wait_bitcoinds()
  50. self.setup_network(True)
  51. def sync_all(self):
  52. if self.is_network_split:
  53. sync_blocks(self.nodes[:2])
  54. sync_blocks(self.nodes[2:])
  55. sync_mempools(self.nodes[:2])
  56. sync_mempools(self.nodes[2:])
  57. else:
  58. sync_blocks(self.nodes)
  59. sync_mempools(self.nodes)
  60. def join_network(self):
  61. """
  62. Join the (previously split) network halves together.
  63. """
  64. assert self.is_network_split
  65. stop_nodes(self.nodes)
  66. wait_bitcoinds()
  67. self.setup_network(False)
  68. def main(self):
  69. import optparse
  70. parser = optparse.OptionParser(usage="%prog [options]")
  71. parser.add_option("--nocleanup", dest="nocleanup", default=False, action="store_true",
  72. help="Leave bitcoinds and test.* datadir on exit or error")
  73. parser.add_option("--srcdir", dest="srcdir", default="../../src",
  74. help="Source directory containing bitcoind/bitcoin-cli (default: %default%)")
  75. parser.add_option("--tmpdir", dest="tmpdir", default=tempfile.mkdtemp(prefix="test"),
  76. help="Root directory for datadirs")
  77. parser.add_option("--tracerpc", dest="trace_rpc", default=False, action="store_true",
  78. help="Print out all RPC calls as they are made")
  79. self.add_options(parser)
  80. (self.options, self.args) = parser.parse_args()
  81. if self.options.trace_rpc:
  82. import logging
  83. logging.basicConfig(level=logging.DEBUG)
  84. os.environ['PATH'] = self.options.srcdir+":"+os.environ['PATH']
  85. check_json_precision()
  86. success = False
  87. try:
  88. if not os.path.isdir(self.options.tmpdir):
  89. os.makedirs(self.options.tmpdir)
  90. self.setup_chain()
  91. self.setup_network()
  92. self.run_test()
  93. success = True
  94. except JSONRPCException as e:
  95. print("JSONRPC error: "+e.error['message'])
  96. traceback.print_tb(sys.exc_info()[2])
  97. except AssertionError as e:
  98. print("Assertion failed: "+e.message)
  99. traceback.print_tb(sys.exc_info()[2])
  100. except Exception as e:
  101. print("Unexpected exception caught during testing: "+str(e))
  102. traceback.print_tb(sys.exc_info()[2])
  103. print("Stopping nodes")
  104. stop_nodes(self.nodes)
  105. wait_bitcoinds()
  106. if not self.options.nocleanup:
  107. print("Cleaning up")
  108. shutil.rmtree(self.options.tmpdir)
  109. if success:
  110. print("Tests successful")
  111. sys.exit(0)
  112. else:
  113. print("Failed")
  114. sys.exit(1)