diff options
author | John Newbery <john@johnnewbery.com> | 2017-07-11 13:01:44 -0400 |
---|---|---|
committer | MarcoFalke <falke.marco@gmail.com> | 2017-10-03 18:31:40 +0200 |
commit | 016b9ada2111aaf41f3336c0179a1347355bc9e6 (patch) | |
tree | e16642ab0b6e3ff9c3a2adfd89416a2d83322b3e /test/functional/test_framework/test_node.py | |
parent | 5398f205e3ba1f7542deeba420f9e198bcb635f5 (diff) | |
download | bitcoin-016b9ada2111aaf41f3336c0179a1347355bc9e6.tar.xz |
[tests] add TestNodeCLI class for calling bitcoin-cli for a node
Github-Pull: #10798
Rebased-From: b23549f6e677a8e22953568704eac7ea0c2c1289
Diffstat (limited to 'test/functional/test_framework/test_node.py')
-rwxr-xr-x | test/functional/test_framework/test_node.py | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/test/functional/test_framework/test_node.py b/test/functional/test_framework/test_node.py index a803df5b49..7c1325e691 100755 --- a/test/functional/test_framework/test_node.py +++ b/test/functional/test_framework/test_node.py @@ -4,8 +4,10 @@ # file COPYING or http://www.opensource.org/licenses/mit-license.php. """Class for bitcoind node under test""" +import decimal import errno import http.client +import json import logging import os import subprocess @@ -49,6 +51,8 @@ class TestNode(): self.extra_args = extra_args 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] + self.cli = TestNodeCLI(os.getenv("BITCOINCLI", "bitcoin-cli"), self.datadir) + self.running = False self.process = None self.rpc_connected = False @@ -136,3 +140,28 @@ class TestNode(): time.sleep(0.1) self.rpc = None self.rpc_connected = False + +class TestNodeCLI(): + """Interface to bitcoin-cli for an individual node""" + + def __init__(self, binary, datadir): + self.binary = binary + self.datadir = datadir + + def __getattr__(self, command): + def dispatcher(*args, **kwargs): + return self.send_cli(command, *args, **kwargs) + return dispatcher + + def send_cli(self, command, *args, **kwargs): + """Run bitcoin-cli command. Deserializes returned string as python object.""" + + pos_args = [str(arg) for arg in args] + named_args = [str(key) + "=" + str(value) for (key, value) in kwargs.items()] + assert not (pos_args and named_args), "Cannot use positional arguments and named arguments in the same bitcoin-cli call" + p_args = [self.binary, "-datadir=" + self.datadir] + if named_args: + p_args += ["-named"] + p_args += [command] + pos_args + named_args + cli_output = subprocess.check_output(p_args, universal_newlines=True) + return json.loads(cli_output, parse_float=decimal.Decimal) |