Dash Core  0.12.2.1
P2P Digital Currency
rpc-tests.py
Go to the documentation of this file.
1 #!/usr/bin/env python2
2 # Copyright (c) 2014-2015 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 """
7 Run Regression Test Suite
8 
9 This module calls down into individual test cases via subprocess. It will
10 forward all unrecognized arguments onto the individual test scripts, other
11 than:
12 
13  - `-extended`: run the "extended" test suite in addition to the basic one.
14  - `-win`: signal that this is running in a Windows environment, and we
15  should run the tests.
16  - `--coverage`: this generates a basic coverage report for the RPC
17  interface.
18 
19 For a description of arguments recognized by test scripts, see
20 `qa/pull-tester/test_framework/test_framework.py:BitcoinTestFramework.main`.
21 
22 """
23 
24 import os
25 import time
26 import shutil
27 import sys
28 import subprocess
29 import tempfile
30 import re
31 
32 from tests_config import *
33 
34 #If imported values are not defined then set to zero (or disabled)
35 if 'ENABLE_WALLET' not in vars():
36  ENABLE_WALLET=0
37 if 'ENABLE_BITCOIND' not in vars():
38  ENABLE_BITCOIND=0
39 if 'ENABLE_UTILS' not in vars():
40  ENABLE_UTILS=0
41 if 'ENABLE_ZMQ' not in vars():
42  ENABLE_ZMQ=0
43 
44 ENABLE_COVERAGE=0
45 
46 #Create a set to store arguments and create the passOn string
47 opts = set()
48 passOn = ""
49 p = re.compile("^--")
50 
51 bold = ("","")
52 if (os.name == 'posix'):
53  bold = ('\033[0m', '\033[1m')
54 
55 for arg in sys.argv[1:]:
56  if arg == '--coverage':
57  ENABLE_COVERAGE = 1
58  elif (p.match(arg) or arg == "-h"):
59  passOn += " " + arg
60  else:
61  opts.add(arg)
62 
63 #Set env vars
64 buildDir = BUILDDIR
65 if "DASHD" not in os.environ:
66  os.environ["DASHD"] = buildDir + '/src/dashd' + EXEEXT
67 if "DASHCLI" not in os.environ:
68  os.environ["DASHCLI"] = buildDir + '/src/dash-cli' + EXEEXT
69 
70 if EXEEXT == ".exe" and "-win" not in opts:
71  # https://github.com/bitcoin/bitcoin/commit/d52802551752140cf41f0d9a225a43e84404d3e9
72  # https://github.com/bitcoin/bitcoin/pull/5677#issuecomment-136646964
73  print "Win tests currently disabled by default. Use -win option to enable"
74  sys.exit(0)
75 
76 if not (ENABLE_WALLET == 1 and ENABLE_UTILS == 1 and ENABLE_BITCOIND == 1):
77  print "No rpc tests to run. Wallet, utils, and bitcoind must all be enabled"
78  sys.exit(0)
79 
80 # python-zmq may not be installed. Handle this gracefully and with some helpful info
81 if ENABLE_ZMQ:
82  try:
83  import zmq
84  except ImportError as e:
85  print("ERROR: \"import zmq\" failed. Set ENABLE_ZMQ=0 or " \
86  "to run zmq tests, see dependency info in /qa/README.md.")
87  raise e
88 
89 #Tests
90 testScripts = [
91  'bip68-112-113-p2p.py',
92  'wallet.py',
93  'wallet-hd.py',
94  'listtransactions.py',
95  'receivedby.py',
96  'mempool_resurrect_test.py',
97  'txn_doublespend.py --mineblock',
98  'txn_clone.py',
99  'getchaintips.py',
100  'rawtransactions.py',
101  'rest.py',
102  'mempool_spendcoinbase.py',
103  'mempool_reorg.py',
104  'mempool_limit.py',
105  'httpbasics.py',
106  'multi_rpc.py',
107  'zapwallettxes.py',
108  'proxy_test.py',
109  'merkle_blocks.py',
110  'fundrawtransaction.py',
111  'fundrawtransaction-hd.py',
112  'signrawtransactions.py',
113  'walletbackup.py',
114  'nodehandling.py',
115  'reindex.py',
116  'addressindex.py',
117  'timestampindex.py',
118  'spentindex.py',
119  'decodescript.py',
120  'p2p-fullblocktest.py', # NOTE: needs dash_hash to pass
121  'blockchain.py',
122  'disablewallet.py',
123  'sendheaders.py', # NOTE: needs dash_hash to pass
124  'keypool.py',
125  'keypool-hd.py',
126  'prioritise_transaction.py',
127  'invalidblockrequest.py', # NOTE: needs dash_hash to pass
128  'invalidtxrequest.py', # NOTE: needs dash_hash to pass
129  'abandonconflict.py',
130  'p2p-versionbits-warning.py',
131 ]
132 if ENABLE_ZMQ:
133  testScripts.append('zmq_test.py')
134 
135 testScriptsExt = [
136  'bip9-softforks.py',
137  'bip65-cltv.py',
138  'bip65-cltv-p2p.py', # NOTE: needs dash_hash to pass
139  'bip68-sequence.py',
140  'bipdersig-p2p.py', # NOTE: needs dash_hash to pass
141  'bipdersig.py',
142  'getblocktemplate_longpoll.py', # FIXME: "socket.error: [Errno 54] Connection reset by peer" on my Mac, same as https://github.com/bitcoin/bitcoin/issues/6651
143  'getblocktemplate_proposals.py',
144  'txn_doublespend.py',
145  'txn_clone.py --mineblock',
146  # 'pruning.py', # Prune mode is incompatible with -txindex.
147  'forknotify.py',
148  'invalidateblock.py',
149 # 'rpcbind_test.py', #temporary, bug in libevent, see #6655
150  'smartfees.py',
151  'maxblocksinflight.py',
152  'p2p-acceptblock.py', # NOTE: needs dash_hash to pass
153  'mempool_packages.py',
154  'maxuploadtarget.py',
155  # 'replace-by-fee.py', # RBF is disabled in Dash Core
156 ]
157 
158 def runtests():
159  coverage = None
160 
161  if ENABLE_COVERAGE:
162  coverage = RPCCoverage()
163  print("Initializing coverage directory at %s\n" % coverage.dir)
164 
165  rpcTestDir = buildDir + '/qa/rpc-tests/'
166  run_extended = '-extended' in opts
167  cov_flag = coverage.flag if coverage else ''
168  flags = " --srcdir %s/src %s %s" % (buildDir, cov_flag, passOn)
169 
170  #Run Tests
171  for i in range(len(testScripts)):
172  if (len(opts) == 0
173  or (len(opts) == 1 and "-win" in opts )
174  or run_extended
175  or testScripts[i] in opts
176  or re.sub(".py$", "", testScripts[i]) in opts ):
177 
178  print("Running testscript %s%s%s ..." % (bold[1], testScripts[i], bold[0]))
179  time0 = time.time()
180  subprocess.check_call(
181  rpcTestDir + testScripts[i] + flags, shell=True)
182  print("Duration: %s s\n" % (int(time.time() - time0)))
183 
184  # exit if help is called so we print just one set of
185  # instructions
186  p = re.compile(" -h| --help")
187  if p.match(passOn):
188  sys.exit(0)
189 
190  # Run Extended Tests
191  for i in range(len(testScriptsExt)):
192  if (run_extended or testScriptsExt[i] in opts
193  or re.sub(".py$", "", testScriptsExt[i]) in opts):
194 
195  print(
196  "Running 2nd level testscript "
197  + "%s%s%s ..." % (bold[1], testScriptsExt[i], bold[0]))
198  time0 = time.time()
199  subprocess.check_call(
200  rpcTestDir + testScriptsExt[i] + flags, shell=True)
201  print("Duration: %s s\n" % (int(time.time() - time0)))
202 
203  if coverage:
204  coverage.report_rpc_coverage()
205 
206  print("Cleaning up coverage data")
207  coverage.cleanup()
208 
209 
210 class RPCCoverage(object):
211  """
212  Coverage reporting utilities for pull-tester.
213 
214  Coverage calculation works by having each test script subprocess write
215  coverage files into a particular directory. These files contain the RPC
216  commands invoked during testing, as well as a complete listing of RPC
217  commands per `bitcoin-cli help` (`rpc_interface.txt`).
218 
219  After all tests complete, the commands run are combined and diff'd against
220  the complete list to calculate uncovered RPC commands.
221 
222  See also: qa/rpc-tests/test_framework/coverage.py
223 
224  """
225  def __init__(self):
226  self.dir = tempfile.mkdtemp(prefix="coverage")
227  self.flag = '--coveragedir %s' % self.dir
228 
230  """
231  Print out RPC commands that were unexercised by tests.
232 
233  """
234  uncovered = self._get_uncovered_rpc_commands()
235 
236  if uncovered:
237  print("Uncovered RPC commands:")
238  print("".join((" - %s\n" % i) for i in sorted(uncovered)))
239  else:
240  print("All RPC commands covered.")
241 
242  def cleanup(self):
243  return shutil.rmtree(self.dir)
244 
246  """
247  Return a set of currently untested RPC commands.
248 
249  """
250  # This is shared from `qa/rpc-tests/test-framework/coverage.py`
251  REFERENCE_FILENAME = 'rpc_interface.txt'
252  COVERAGE_FILE_PREFIX = 'coverage.'
253 
254  coverage_ref_filename = os.path.join(self.dir, REFERENCE_FILENAME)
255  coverage_filenames = set()
256  all_cmds = set()
257  covered_cmds = set()
258 
259  if not os.path.isfile(coverage_ref_filename):
260  raise RuntimeError("No coverage reference found")
261 
262  with open(coverage_ref_filename, 'r') as f:
263  all_cmds.update([i.strip() for i in f.readlines()])
264 
265  for root, dirs, files in os.walk(self.dir):
266  for filename in files:
267  if filename.startswith(COVERAGE_FILE_PREFIX):
268  coverage_filenames.add(os.path.join(root, filename))
269 
270  for filename in coverage_filenames:
271  with open(filename, 'r') as f:
272  covered_cmds.update([i.strip() for i in f.readlines()])
273 
274  return all_cmds - covered_cmds
275 
276 
277 if __name__ == '__main__':
278  runtests()
def report_rpc_coverage(self)
Definition: rpc-tests.py:229
def runtests()
Definition: rpc-tests.py:158
def _get_uncovered_rpc_commands(self)
Definition: rpc-tests.py:245