Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

rpc-tests.py 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. """
  6. Run Regression Test Suite
  7. This module calls down into individual test cases via subprocess. It will
  8. forward all unrecognized arguments onto the individual test scripts, other
  9. than:
  10. - `-extended`: run the "extended" test suite in addition to the basic one.
  11. - `-win`: signal that this is running in a Windows environment, and we
  12. should run the tests.
  13. - `--coverage`: this generates a basic coverage report for the RPC
  14. interface.
  15. For a description of arguments recognized by test scripts, see
  16. `qa/pull-tester/test_framework/test_framework.py:BitcoinTestFramework.main`.
  17. """
  18. import os
  19. import shutil
  20. import sys
  21. import subprocess
  22. import tempfile
  23. import re
  24. from tests_config import *
  25. #If imported values are not defined then set to zero (or disabled)
  26. if not vars().has_key('ENABLE_WALLET'):
  27. ENABLE_WALLET=0
  28. if not vars().has_key('ENABLE_BITCOIND'):
  29. ENABLE_BITCOIND=0
  30. if not vars().has_key('ENABLE_UTILS'):
  31. ENABLE_UTILS=0
  32. if not vars().has_key('ENABLE_ZMQ'):
  33. ENABLE_ZMQ=0
  34. ENABLE_COVERAGE=0
  35. #Create a set to store arguments and create the passOn string
  36. opts = set()
  37. passOn = ""
  38. p = re.compile("^--")
  39. for arg in sys.argv[1:]:
  40. if arg == '--coverage':
  41. ENABLE_COVERAGE = 1
  42. elif (p.match(arg) or arg == "-h"):
  43. passOn += " " + arg
  44. else:
  45. opts.add(arg)
  46. #Set env vars
  47. buildDir = BUILDDIR
  48. os.environ["BITCOIND"] = buildDir + '/src/bitcoind' + EXEEXT
  49. os.environ["BITCOINCLI"] = buildDir + '/src/bitcoin-cli' + EXEEXT
  50. #Disable Windows tests by default
  51. if EXEEXT == ".exe" and "-win" not in opts:
  52. print "Win tests currently disabled. Use -win option to enable"
  53. sys.exit(0)
  54. #Tests
  55. testScripts = [
  56. 'wallet.py',
  57. 'listtransactions.py',
  58. 'mempool_resurrect_test.py',
  59. 'txn_doublespend.py --mineblock',
  60. 'txn_clone.py',
  61. 'getchaintips.py',
  62. 'rawtransactions.py',
  63. 'rest.py',
  64. 'mempool_spendcoinbase.py',
  65. 'mempool_coinbase_spends.py',
  66. 'httpbasics.py',
  67. 'zapwallettxes.py',
  68. 'proxy_test.py',
  69. 'merkle_blocks.py',
  70. 'fundrawtransaction.py',
  71. 'signrawtransactions.py',
  72. 'walletbackup.py',
  73. 'nodehandling.py',
  74. 'reindex.py',
  75. 'decodescript.py',
  76. 'p2p-fullblocktest.py',
  77. 'blockchain.py',
  78. ]
  79. testScriptsExt = [
  80. 'bip65-cltv.py',
  81. 'bip65-cltv-p2p.py',
  82. 'bipdersig-p2p.py',
  83. 'bipdersig.py',
  84. 'getblocktemplate_longpoll.py',
  85. 'getblocktemplate_proposals.py',
  86. 'txn_doublespend.py',
  87. 'txn_clone.py --mineblock',
  88. 'pruning.py',
  89. 'forknotify.py',
  90. 'invalidateblock.py',
  91. 'keypool.py',
  92. 'receivedby.py',
  93. # 'rpcbind_test.py', #temporary, bug in libevent, see #6655
  94. # 'script_test.py', #used for manual comparison of 2 binaries
  95. 'smartfees.py',
  96. 'maxblocksinflight.py',
  97. 'invalidblockrequest.py',
  98. 'p2p-acceptblock.py',
  99. 'mempool_packages.py',
  100. 'maxuploadtarget.py',
  101. ]
  102. #Enable ZMQ tests
  103. if ENABLE_ZMQ == 1:
  104. testScripts.append('zmq_test.py')
  105. def runtests():
  106. coverage = None
  107. if ENABLE_COVERAGE:
  108. coverage = RPCCoverage()
  109. print("Initializing coverage directory at %s" % coverage.dir)
  110. if(ENABLE_WALLET == 1 and ENABLE_UTILS == 1 and ENABLE_BITCOIND == 1):
  111. rpcTestDir = buildDir + '/qa/rpc-tests/'
  112. run_extended = '-extended' in opts
  113. cov_flag = coverage.flag if coverage else ''
  114. flags = " --srcdir %s/src %s %s" % (buildDir, cov_flag, passOn)
  115. #Run Tests
  116. for i in range(len(testScripts)):
  117. if (len(opts) == 0
  118. or (len(opts) == 1 and "-win" in opts )
  119. or run_extended
  120. or testScripts[i] in opts
  121. or re.sub(".py$", "", testScripts[i]) in opts ):
  122. print("Running testscript " + testScripts[i] + "...")
  123. subprocess.check_call(
  124. rpcTestDir + testScripts[i] + flags, shell=True)
  125. # exit if help is called so we print just one set of
  126. # instructions
  127. p = re.compile(" -h| --help")
  128. if p.match(passOn):
  129. sys.exit(0)
  130. # Run Extended Tests
  131. for i in range(len(testScriptsExt)):
  132. if (run_extended or testScriptsExt[i] in opts
  133. or re.sub(".py$", "", testScriptsExt[i]) in opts):
  134. print(
  135. "Running 2nd level testscript "
  136. + testScriptsExt[i] + "...")
  137. subprocess.check_call(
  138. rpcTestDir + testScriptsExt[i] + flags, shell=True)
  139. if coverage:
  140. coverage.report_rpc_coverage()
  141. print("Cleaning up coverage data")
  142. coverage.cleanup()
  143. else:
  144. print "No rpc tests to run. Wallet, utils, and bitcoind must all be enabled"
  145. class RPCCoverage(object):
  146. """
  147. Coverage reporting utilities for pull-tester.
  148. Coverage calculation works by having each test script subprocess write
  149. coverage files into a particular directory. These files contain the RPC
  150. commands invoked during testing, as well as a complete listing of RPC
  151. commands per `bitcoin-cli help` (`rpc_interface.txt`).
  152. After all tests complete, the commands run are combined and diff'd against
  153. the complete list to calculate uncovered RPC commands.
  154. See also: qa/rpc-tests/test_framework/coverage.py
  155. """
  156. def __init__(self):
  157. self.dir = tempfile.mkdtemp(prefix="coverage")
  158. self.flag = '--coveragedir %s' % self.dir
  159. def report_rpc_coverage(self):
  160. """
  161. Print out RPC commands that were unexercised by tests.
  162. """
  163. uncovered = self._get_uncovered_rpc_commands()
  164. if uncovered:
  165. print("Uncovered RPC commands:")
  166. print("".join((" - %s\n" % i) for i in sorted(uncovered)))
  167. else:
  168. print("All RPC commands covered.")
  169. def cleanup(self):
  170. return shutil.rmtree(self.dir)
  171. def _get_uncovered_rpc_commands(self):
  172. """
  173. Return a set of currently untested RPC commands.
  174. """
  175. # This is shared from `qa/rpc-tests/test-framework/coverage.py`
  176. REFERENCE_FILENAME = 'rpc_interface.txt'
  177. COVERAGE_FILE_PREFIX = 'coverage.'
  178. coverage_ref_filename = os.path.join(self.dir, REFERENCE_FILENAME)
  179. coverage_filenames = set()
  180. all_cmds = set()
  181. covered_cmds = set()
  182. if not os.path.isfile(coverage_ref_filename):
  183. raise RuntimeError("No coverage reference found")
  184. with open(coverage_ref_filename, 'r') as f:
  185. all_cmds.update([i.strip() for i in f.readlines()])
  186. for root, dirs, files in os.walk(self.dir):
  187. for filename in files:
  188. if filename.startswith(COVERAGE_FILE_PREFIX):
  189. coverage_filenames.add(os.path.join(root, filename))
  190. for filename in coverage_filenames:
  191. with open(filename, 'r') as f:
  192. covered_cmds.update([i.strip() for i in f.readlines()])
  193. return all_cmds - covered_cmds
  194. if __name__ == '__main__':
  195. runtests()