Skip to content

Commit f6ade9c

Browse files
committed
[tests] allow tests to be run with --usecli
test_framework accepts a new --usecli parameter. Running the test with this parameter will cause all RPCs to be sent through bitcoin-cli rather than directly over http. By default, individual test cases do not support --usecli, and self.supports_cli must be set to True in the set_test_params method. We can make supports_cli default to True in future once we know which tests will fail with use_cli.
1 parent ff9a363 commit f6ade9c

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

test/functional/create_cache.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class CreateCache(BitcoinTestFramework):
1616

1717
def set_test_params(self):
1818
self.num_nodes = 0
19+
self.supports_cli = True
1920

2021
def setup_network(self):
2122
pass

test/functional/test_framework/test_framework.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ def __init__(self):
6262
self.setup_clean_chain = False
6363
self.nodes = []
6464
self.mocktime = 0
65+
self.supports_cli = False
6566
self.set_test_params()
6667

6768
assert hasattr(self, "num_nodes"), "Test must set self.num_nodes in set_test_params()"
@@ -91,6 +92,8 @@ def main(self):
9192
help="Location of the test framework config file")
9293
parser.add_option("--pdbonfailure", dest="pdbonfailure", default=False, action="store_true",
9394
help="Attach a python debugger if test fails")
95+
parser.add_option("--usecli", dest="usecli", default=False, action="store_true",
96+
help="use bitcoin-cli instead of RPC for all commands")
9497
self.add_options(parser)
9598
(self.options, self.args) = parser.parse_args()
9699

@@ -113,6 +116,8 @@ def main(self):
113116
success = TestStatus.FAILED
114117

115118
try:
119+
if self.options.usecli and not self.supports_cli:
120+
raise SkipTest("--usecli specified but test does not support using CLI")
116121
self.setup_chain()
117122
self.setup_network()
118123
self.run_test()
@@ -213,7 +218,7 @@ def add_nodes(self, num_nodes, extra_args=None, rpchost=None, timewait=None, bin
213218
assert_equal(len(extra_args), num_nodes)
214219
assert_equal(len(binary), num_nodes)
215220
for i in range(num_nodes):
216-
self.nodes.append(TestNode(i, self.options.tmpdir, extra_args[i], rpchost, timewait=timewait, binary=binary[i], stderr=None, mocktime=self.mocktime, coverage_dir=self.options.coveragedir))
221+
self.nodes.append(TestNode(i, self.options.tmpdir, extra_args[i], rpchost, timewait=timewait, binary=binary[i], stderr=None, mocktime=self.mocktime, coverage_dir=self.options.coveragedir, use_cli=self.options.usecli))
217222

218223
def start_node(self, i, extra_args=None, stderr=None):
219224
"""Start a bitcoind"""

test/functional/test_framework/test_node.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class TestNode():
4242
To make things easier for the test writer, any unrecognised messages will
4343
be dispatched to the RPC connection."""
4444

45-
def __init__(self, i, dirname, extra_args, rpchost, timewait, binary, stderr, mocktime, coverage_dir):
45+
def __init__(self, i, dirname, extra_args, rpchost, timewait, binary, stderr, mocktime, coverage_dir, use_cli=False):
4646
self.index = i
4747
self.datadir = os.path.join(dirname, "node" + str(i))
4848
self.rpchost = rpchost
@@ -62,6 +62,7 @@ def __init__(self, i, dirname, extra_args, rpchost, timewait, binary, stderr, mo
6262
self.args = [self.binary, "-datadir=" + self.datadir, "-server", "-keypool=1", "-discover=0", "-rest", "-logtimemicros", "-debug", "-debugexclude=libevent", "-debugexclude=leveldb", "-mocktime=" + str(mocktime), "-uacomment=testnode%d" % i]
6363

6464
self.cli = TestNodeCLI(os.getenv("BITCOINCLI", "bitcoin-cli"), self.datadir)
65+
self.use_cli = use_cli
6566

6667
self.running = False
6768
self.process = None
@@ -73,9 +74,12 @@ def __init__(self, i, dirname, extra_args, rpchost, timewait, binary, stderr, mo
7374
self.p2ps = []
7475

7576
def __getattr__(self, name):
76-
"""Dispatches any unrecognised messages to the RPC connection."""
77-
assert self.rpc_connected and self.rpc is not None, "Error: no RPC connection"
78-
return getattr(self.rpc, name)
77+
"""Dispatches any unrecognised messages to the RPC connection or a CLI instance."""
78+
if self.use_cli:
79+
return getattr(self.cli, name)
80+
else:
81+
assert self.rpc_connected and self.rpc is not None, "Error: no RPC connection"
82+
return getattr(self.rpc, name)
7983

8084
def start(self, extra_args=None, stderr=None):
8185
"""Start the node."""
@@ -114,10 +118,13 @@ def wait_for_rpc_connection(self):
114118
raise AssertionError("Unable to connect to bitcoind")
115119

116120
def get_wallet_rpc(self, wallet_name):
117-
assert self.rpc_connected
118-
assert self.rpc
119-
wallet_path = "wallet/%s" % wallet_name
120-
return self.rpc / wallet_path
121+
if self.use_cli:
122+
return self.cli("-rpcwallet={}".format(wallet_name))
123+
else:
124+
assert self.rpc_connected
125+
assert self.rpc
126+
wallet_path = "wallet/%s" % wallet_name
127+
return self.rpc / wallet_path
121128

122129
def stop_node(self):
123130
"""Stop the node."""
@@ -210,6 +217,7 @@ def __init__(self, binary, datadir):
210217
self.binary = binary
211218
self.datadir = datadir
212219
self.input = None
220+
self.log = logging.getLogger('TestFramework.bitcoincli')
213221

214222
def __call__(self, *args, input=None):
215223
# TestNodeCLI is callable with bitcoin-cli command-line args
@@ -240,6 +248,7 @@ def send_cli(self, command, *args, **kwargs):
240248
if named_args:
241249
p_args += ["-named"]
242250
p_args += [command] + pos_args + named_args
251+
self.log.debug("Running bitcoin-cli command: %s" % command)
243252
process = subprocess.Popen(p_args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
244253
cli_stdout, cli_stderr = process.communicate(input=self.input)
245254
returncode = process.poll()

0 commit comments

Comments
 (0)