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.
7 Run Regression Test Suite
9 This module calls down into individual test cases via subprocess. It will
10 forward all unrecognized arguments onto the individual test scripts, other
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
16 - `--coverage`: this generates a basic coverage report for the RPC
19 For a description of arguments recognized by test scripts, see
20 `qa/pull-tester/test_framework/test_framework.py:BitcoinTestFramework.main`.
32 sys
.path
.append("qa/pull-tester/")
33 from tests_config
import *
36 if os
.name
== 'posix':
37 # primitive formatting on supported
38 # terminal via ANSI escape sequences:
39 BOLD
= ('\033[0m', '\033[1m')
41 RPC_TESTS_DIR
= SRCDIR
+ '/qa/rpc-tests/'
43 #If imported values are not defined then set to zero (or disabled)
44 if 'ENABLE_WALLET' not in vars():
46 if 'ENABLE_BITCOIND' not in vars():
48 if 'ENABLE_UTILS' not in vars():
50 if 'ENABLE_ZMQ' not in vars():
55 #Create a set to store arguments and create the passon string
58 PASSON_REGEX
= re
.compile("^--")
59 PARALLEL_REGEX
= re
.compile('^-parallel=')
64 for arg
in sys
.argv
[1:]:
65 if arg
== "--help" or arg
== "-h" or arg
== "-?":
68 if arg
== '--coverage':
70 elif PASSON_REGEX
.match(arg
):
71 passon_args
.append(arg
)
72 elif PARALLEL_REGEX
.match(arg
):
73 run_parallel
= int(arg
.split(sep
='=', maxsplit
=1)[1])
78 if "BITCOIND" not in os
.environ
:
79 os
.environ
["BITCOIND"] = BUILDDIR
+ '/src/bitcoind' + EXEEXT
81 if EXEEXT
== ".exe" and "-win" not in opts
:
82 # https://github.com/bitcoin/bitcoin/commit/d52802551752140cf41f0d9a225a43e84404d3e9
83 # https://github.com/bitcoin/bitcoin/pull/5677#issuecomment-136646964
84 print("Win tests currently disabled by default. Use -win option to enable")
87 if not (ENABLE_WALLET
== 1 and ENABLE_UTILS
== 1 and ENABLE_BITCOIND
== 1):
88 print("No rpc tests to run. Wallet, utils, and bitcoind must all be enabled")
91 # python3-zmq may not be installed. Handle this gracefully and with some helpful info
96 print("ERROR: \"import zmq\" failed. Set ENABLE_ZMQ=0 or "
97 "to run zmq tests, see dependency info in /qa/README.md.")
102 # longest test should go first, to favor running tests in parallel
103 'p2p-fullblocktest.py',
105 'bip68-112-113-p2p.py',
107 'wallet-accounts.py',
110 'listtransactions.py',
112 'mempool_resurrect_test.py',
113 'txn_doublespend.py --mineblock',
118 'rawtransactions.py',
120 'mempool_spendcoinbase.py',
128 'fundrawtransaction.py',
129 'signrawtransactions.py',
138 'prioritise_transaction.py',
139 'invalidblockrequest.py',
140 'invalidtxrequest.py',
141 'abandonconflict.py',
142 'p2p-versionbits-warning.py',
143 'importprunedfunds.py',
145 'p2p-compactblocks.py',
148 testScripts
.append('zmq_test.py')
157 'getblocktemplate_longpoll.py',
158 'getblocktemplate_proposals.py',
159 'txn_doublespend.py',
160 'txn_clone.py --mineblock',
162 'invalidateblock.py',
165 'maxblocksinflight.py',
166 'p2p-acceptblock.py',
167 'mempool_packages.py',
168 'maxuploadtarget.py',
171 'pruning.py', # leave pruning last as it takes a REALLY long time
177 if '-extended' in opts
:
178 test_list
= testScripts
+ testScriptsExt
179 elif len(opts
) == 0 or (len(opts
) == 1 and "-win" in opts
):
180 test_list
= testScripts
182 for t
in testScripts
+ testScriptsExt
:
183 if t
in opts
or re
.sub(".py$", "", t
) in opts
:
187 # Only print help of the first script and exit
188 subprocess
.check_call((RPC_TESTS_DIR
+ test_list
[0]).split() + ['-h'])
194 coverage
= RPCCoverage()
195 print("Initializing coverage directory at %s\n" % coverage
.dir)
196 flags
= ["--srcdir=%s/src" % BUILDDIR
] + passon_args
197 flags
.append("--cachedir=%s/qa/cache" % BUILDDIR
)
199 flags
.append(coverage
.flag
)
201 if len(test_list
) > 1 and run_parallel
> 1:
203 subprocess
.check_output([RPC_TESTS_DIR
+ 'create_cache.py'] + flags
)
206 max_len_name
= len(max(test_list
, key
=len))
209 job_queue
= RPCTestHandler(run_parallel
, test_list
, flags
)
210 results
= BOLD
[1] + "%s | %s | %s\n\n" % ("TEST".ljust(max_len_name
), "PASSED", "DURATION") + BOLD
[0]
212 for _
in range(len(test_list
)):
213 (name
, stdout
, stderr
, passed
, duration
) = job_queue
.get_next()
214 all_passed
= all_passed
and passed
217 print('\n' + BOLD
[1] + name
+ BOLD
[0] + ":")
219 print('stderr:\n' if not stderr
== '' else '', stderr
)
220 results
+= "%s | %s | %s s\n" % (name
.ljust(max_len_name
), str(passed
).ljust(6), duration
)
221 print("Pass: %s%s%s, Duration: %s s\n" % (BOLD
[1], passed
, BOLD
[0], duration
))
222 results
+= BOLD
[1] + "\n%s | %s | %s s (accumulated)" % ("ALL".ljust(max_len_name
), str(all_passed
).ljust(6), time_sum
) + BOLD
[0]
224 print("\nRuntime: %s s" % (int(time
.time() - time0
)))
227 coverage
.report_rpc_coverage()
229 print("Cleaning up coverage data")
232 sys
.exit(not all_passed
)
235 class RPCTestHandler
:
237 Trigger the testscrips passed in via the list.
240 def __init__(self
, num_tests_parallel
, test_list
=None, flags
=None):
241 assert(num_tests_parallel
>= 1)
242 self
.num_jobs
= num_tests_parallel
243 self
.test_list
= test_list
249 while self
.num_running
< self
.num_jobs
and self
.test_list
:
251 self
.num_running
+= 1
252 t
= self
.test_list
.pop(0)
253 port_seed
= ["--portseed=%s" % len(self
.test_list
)]
256 subprocess
.Popen((RPC_TESTS_DIR
+ t
).split() + self
.flags
+ port_seed
,
257 universal_newlines
=True,
258 stdout
=subprocess
.PIPE
,
259 stderr
=subprocess
.PIPE
)))
261 raise IndexError('pop from empty list')
263 # Return first proc that finishes
266 (name
, time0
, proc
) = j
267 if proc
.poll() is not None:
268 (stdout
, stderr
) = proc
.communicate(timeout
=3)
269 passed
= stderr
== "" and proc
.returncode
== 0
270 self
.num_running
-= 1
272 return name
, stdout
, stderr
, passed
, int(time
.time() - time0
)
273 print('.', end
='', flush
=True)
276 class RPCCoverage(object):
278 Coverage reporting utilities for pull-tester.
280 Coverage calculation works by having each test script subprocess write
281 coverage files into a particular directory. These files contain the RPC
282 commands invoked during testing, as well as a complete listing of RPC
283 commands per `bitcoin-cli help` (`rpc_interface.txt`).
285 After all tests complete, the commands run are combined and diff'd against
286 the complete list to calculate uncovered RPC commands.
288 See also: qa/rpc-tests/test_framework/coverage.py
292 self
.dir = tempfile
.mkdtemp(prefix
="coverage")
293 self
.flag
= '--coveragedir=%s' % self
.dir
295 def report_rpc_coverage(self
):
297 Print out RPC commands that were unexercised by tests.
300 uncovered
= self
._get
_uncovered
_rpc
_commands
()
303 print("Uncovered RPC commands:")
304 print("".join((" - %s\n" % i
) for i
in sorted(uncovered
)))
306 print("All RPC commands covered.")
309 return shutil
.rmtree(self
.dir)
311 def _get_uncovered_rpc_commands(self
):
313 Return a set of currently untested RPC commands.
316 # This is shared from `qa/rpc-tests/test-framework/coverage.py`
317 REFERENCE_FILENAME
= 'rpc_interface.txt'
318 COVERAGE_FILE_PREFIX
= 'coverage.'
320 coverage_ref_filename
= os
.path
.join(self
.dir, REFERENCE_FILENAME
)
321 coverage_filenames
= set()
325 if not os
.path
.isfile(coverage_ref_filename
):
326 raise RuntimeError("No coverage reference found")
328 with
open(coverage_ref_filename
, 'r') as f
:
329 all_cmds
.update([i
.strip() for i
in f
.readlines()])
331 for root
, dirs
, files
in os
.walk(self
.dir):
332 for filename
in files
:
333 if filename
.startswith(COVERAGE_FILE_PREFIX
):
334 coverage_filenames
.add(os
.path
.join(root
, filename
))
336 for filename
in coverage_filenames
:
337 with
open(filename
, 'r') as f
:
338 covered_cmds
.update([i
.strip() for i
in f
.readlines()])
340 return all_cmds
- covered_cmds
343 if __name__
== '__main__':