Skip to content

Commit b23549f

Browse files
committed
[tests] add TestNodeCLI class for calling bitcoin-cli for a node
1 parent 85aec87 commit b23549f

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

test/functional/test_framework/test_node.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55
"""Class for bitcoind node under test"""
66

7+
import decimal
78
import errno
89
import http.client
10+
import json
911
import logging
1012
import os
1113
import subprocess
@@ -45,6 +47,8 @@ def __init__(self, i, dirname, extra_args, rpchost, timewait, binary, stderr, mo
4547
self.extra_args = extra_args
4648
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]
4749

50+
self.cli = TestNodeCLI(os.getenv("BITCOINCLI", "bitcoin-cli"), self.datadir)
51+
4852
self.running = False
4953
self.process = None
5054
self.rpc_connected = False
@@ -132,3 +136,28 @@ def node_encrypt_wallet(self, passphrase):
132136
time.sleep(0.1)
133137
self.rpc = None
134138
self.rpc_connected = False
139+
140+
class TestNodeCLI():
141+
"""Interface to bitcoin-cli for an individual node"""
142+
143+
def __init__(self, binary, datadir):
144+
self.binary = binary
145+
self.datadir = datadir
146+
147+
def __getattr__(self, command):
148+
def dispatcher(*args, **kwargs):
149+
return self.send_cli(command, *args, **kwargs)
150+
return dispatcher
151+
152+
def send_cli(self, command, *args, **kwargs):
153+
"""Run bitcoin-cli command. Deserializes returned string as python object."""
154+
155+
pos_args = [str(arg) for arg in args]
156+
named_args = [str(key) + "=" + str(value) for (key, value) in kwargs.items()]
157+
assert not (pos_args and named_args), "Cannot use positional arguments and named arguments in the same bitcoin-cli call"
158+
p_args = [self.binary, "-datadir=" + self.datadir]
159+
if named_args:
160+
p_args += ["-named"]
161+
p_args += [command] + pos_args + named_args
162+
cli_output = subprocess.check_output(p_args, universal_newlines=True)
163+
return json.loads(cli_output, parse_float=decimal.Decimal)

test/functional/test_runner.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ def run_tests(test_list, src_dir, build_dir, exeext, tmpdir, jobs=1, enable_cove
279279
#Set env vars
280280
if "BITCOIND" not in os.environ:
281281
os.environ["BITCOIND"] = build_dir + '/src/bitcoind' + exeext
282+
os.environ["BITCOINCLI"] = build_dir + '/src/bitcoin-cli' + exeext
282283

283284
tests_dir = src_dir + '/test/functional/'
284285

0 commit comments

Comments
 (0)