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 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2014-2016 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. import logging
  7. import optparse
  8. import os
  9. import sys
  10. import shutil
  11. import tempfile
  12. import traceback
  13. from .util import (
  14. initialize_chain,
  15. start_nodes,
  16. connect_nodes_bi,
  17. sync_blocks,
  18. sync_mempools,
  19. stop_nodes,
  20. stop_node,
  21. enable_coverage,
  22. check_json_precision,
  23. initialize_chain_clean,
  24. PortSeed,
  25. )
  26. from .authproxy import JSONRPCException
  27. class BitcoinTestFramework(object):
  28. def __init__(self):
  29. self.num_nodes = 4
  30. self.setup_clean_chain = False
  31. self.nodes = None
  32. def run_test(self):
  33. raise NotImplementedError
  34. def add_options(self, parser):
  35. pass
  36. def setup_chain(self):
  37. self.log.info("Initializing test directory "+self.options.tmpdir)
  38. if self.setup_clean_chain:
  39. initialize_chain_clean(self.options.tmpdir, self.num_nodes)
  40. else:
  41. initialize_chain(self.options.tmpdir, self.num_nodes, self.options.cachedir)
  42. def stop_node(self, num_node):
  43. stop_node(self.nodes[num_node], num_node)
  44. def setup_nodes(self):
  45. return start_nodes(self.num_nodes, self.options.tmpdir)
  46. def setup_network(self, split = False):
  47. self.nodes = self.setup_nodes()
  48. # Connect the nodes as a "chain". This allows us
  49. # to split the network between nodes 1 and 2 to get
  50. # two halves that can work on competing chains.
  51. # If we joined network halves, connect the nodes from the joint
  52. # on outward. This ensures that chains are properly reorganised.
  53. if not split:
  54. connect_nodes_bi(self.nodes, 1, 2)
  55. sync_blocks(self.nodes[1:3])
  56. sync_mempools(self.nodes[1:3])
  57. connect_nodes_bi(self.nodes, 0, 1)
  58. connect_nodes_bi(self.nodes, 2, 3)
  59. self.is_network_split = split
  60. self.sync_all()
  61. def split_network(self):
  62. """
  63. Split the network of four nodes into nodes 0/1 and 2/3.
  64. """
  65. assert not self.is_network_split
  66. stop_nodes(self.nodes)
  67. self.setup_network(True)
  68. def sync_all(self):
  69. if self.is_network_split:
  70. sync_blocks(self.nodes[:2])
  71. sync_blocks(self.nodes[2:])
  72. sync_mempools(self.nodes[:2])
  73. sync_mempools(self.nodes[2:])
  74. else:
  75. sync_blocks(self.nodes)
  76. sync_mempools(self.nodes)
  77. def join_network(self):
  78. """
  79. Join the (previously split) network halves together.
  80. """
  81. assert self.is_network_split
  82. stop_nodes(self.nodes)
  83. self.setup_network(False)
  84. def main(self):
  85. parser = optparse.OptionParser(usage="%prog [options]")
  86. parser.add_option("--nocleanup", dest="nocleanup", default=False, action="store_true",
  87. help="Leave bitcoinds and test.* datadir on exit or error")
  88. parser.add_option("--noshutdown", dest="noshutdown", default=False, action="store_true",
  89. help="Don't stop bitcoinds after the test execution")
  90. parser.add_option("--srcdir", dest="srcdir", default=os.path.normpath(os.path.dirname(os.path.realpath(__file__))+"/../../../src"),
  91. help="Source directory containing bitcoind/bitcoin-cli (default: %default)")
  92. parser.add_option("--cachedir", dest="cachedir", default=os.path.normpath(os.path.dirname(os.path.realpath(__file__))+"/../../cache"),
  93. help="Directory for caching pregenerated datadirs")
  94. parser.add_option("--tmpdir", dest="tmpdir", default=tempfile.mkdtemp(prefix="test"),
  95. help="Root directory for datadirs")
  96. parser.add_option("-l", "--loglevel", dest="loglevel", default="INFO",
  97. help="log events at this level and higher to the console. Can be set to DEBUG, INFO, WARNING, ERROR or CRITICAL. Passing --loglevel DEBUG will output all logs to console. Note that logs at all levels are always written to the test_framework.log file in the temporary test directory.")
  98. parser.add_option("--tracerpc", dest="trace_rpc", default=False, action="store_true",
  99. help="Print out all RPC calls as they are made")
  100. parser.add_option("--portseed", dest="port_seed", default=os.getpid(), type='int',
  101. help="The seed to use for assigning port numbers (default: current process id)")
  102. parser.add_option("--coveragedir", dest="coveragedir",
  103. help="Write tested RPC commands into this directory")
  104. self.add_options(parser)
  105. (self.options, self.args) = parser.parse_args()
  106. # backup dir variable for removal at cleanup
  107. self.options.root, self.options.tmpdir = self.options.tmpdir, self.options.tmpdir + '/' + str(self.options.port_seed)
  108. if self.options.coveragedir:
  109. enable_coverage(self.options.coveragedir)
  110. PortSeed.n = self.options.port_seed
  111. os.environ['PATH'] = self.options.srcdir+":"+self.options.srcdir+"/qt:"+os.environ['PATH']
  112. check_json_precision()
  113. # Set up temp directory and start logging
  114. os.makedirs(self.options.tmpdir, exist_ok=False)
  115. self._start_logging()
  116. success = False
  117. try:
  118. self.setup_chain()
  119. self.setup_network()
  120. self.run_test()
  121. success = True
  122. except JSONRPCException as e:
  123. self.log.exception("JSONRPC error")
  124. except AssertionError as e:
  125. self.log.exception("Assertion failed")
  126. except KeyError as e:
  127. self.log.exception("Key error")
  128. except Exception as e:
  129. self.log.exception("Unexpected exception caught during testing")
  130. except KeyboardInterrupt as e:
  131. self.log.warning("Exiting after keyboard interrupt")
  132. if not self.options.noshutdown:
  133. self.log.info("Stopping nodes")
  134. stop_nodes(self.nodes)
  135. else:
  136. self.log.info("Note: bitcoinds were not stopped and may still be running")
  137. if not self.options.nocleanup and not self.options.noshutdown and success:
  138. self.log.info("Cleaning up")
  139. shutil.rmtree(self.options.tmpdir)
  140. if not os.listdir(self.options.root):
  141. os.rmdir(self.options.root)
  142. else:
  143. self.log.warning("Not cleaning up dir %s" % self.options.tmpdir)
  144. if os.getenv("PYTHON_DEBUG", ""):
  145. # Dump the end of the debug logs, to aid in debugging rare
  146. # travis failures.
  147. import glob
  148. filenames = glob.glob(self.options.tmpdir + "/node*/regtest/debug.log")
  149. MAX_LINES_TO_PRINT = 1000
  150. for f in filenames:
  151. print("From" , f, ":")
  152. from collections import deque
  153. print("".join(deque(open(f), MAX_LINES_TO_PRINT)))
  154. if success:
  155. self.log.info("Tests successful")
  156. sys.exit(0)
  157. else:
  158. self.log.error("Test failed. Test logging available at %s/test_framework.log", self.options.tmpdir)
  159. logging.shutdown()
  160. sys.exit(1)
  161. def _start_logging(self):
  162. # Add logger and logging handlers
  163. self.log = logging.getLogger('TestFramework')
  164. self.log.setLevel(logging.DEBUG)
  165. # Create file handler to log all messages
  166. fh = logging.FileHandler(self.options.tmpdir + '/test_framework.log')
  167. fh.setLevel(logging.DEBUG)
  168. # Create console handler to log messages to stderr. By default this logs only error messages, but can be configured with --loglevel.
  169. ch = logging.StreamHandler(sys.stdout)
  170. # User can provide log level as a number or string (eg DEBUG). loglevel was caught as a string, so try to convert it to an int
  171. ll = int(self.options.loglevel) if self.options.loglevel.isdigit() else self.options.loglevel.upper()
  172. ch.setLevel(ll)
  173. # Format logs the same as bitcoind's debug.log with microprecision (so log files can be concatenated and sorted)
  174. formatter = logging.Formatter(fmt = '%(asctime)s.%(msecs)03d000 %(name)s (%(levelname)s): %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
  175. fh.setFormatter(formatter)
  176. ch.setFormatter(formatter)
  177. # add the handlers to the logger
  178. self.log.addHandler(fh)
  179. self.log.addHandler(ch)
  180. if self.options.trace_rpc:
  181. rpc_logger = logging.getLogger("BitcoinRPC")
  182. rpc_logger.setLevel(logging.DEBUG)
  183. rpc_handler = logging.StreamHandler(sys.stdout)
  184. rpc_handler.setLevel(logging.DEBUG)
  185. rpc_logger.addHandler(rpc_handler)
  186. # Test framework for doing p2p comparison testing, which sets up some bitcoind
  187. # binaries:
  188. # 1 binary: test binary
  189. # 2 binaries: 1 test binary, 1 ref binary
  190. # n>2 binaries: 1 test binary, n-1 ref binaries
  191. class ComparisonTestFramework(BitcoinTestFramework):
  192. def __init__(self):
  193. super().__init__()
  194. self.num_nodes = 2
  195. self.setup_clean_chain = True
  196. def add_options(self, parser):
  197. parser.add_option("--testbinary", dest="testbinary",
  198. default=os.getenv("BITCOIND", "bitcoind"),
  199. help="bitcoind binary to test")
  200. parser.add_option("--refbinary", dest="refbinary",
  201. default=os.getenv("BITCOIND", "bitcoind"),
  202. help="bitcoind binary to use for reference nodes (if any)")
  203. def setup_network(self):
  204. self.nodes = start_nodes(
  205. self.num_nodes, self.options.tmpdir,
  206. extra_args=[['-whitelist=127.0.0.1']] * self.num_nodes,
  207. binary=[self.options.testbinary] +
  208. [self.options.refbinary]*(self.num_nodes-1))