Changeset View
Changeset View
Standalone View
Standalone View
test/functional/test_framework/test_node.py
Show First 20 Lines • Show All 350 Lines • ▼ Show 20 Lines | class TestNodeCLIAttr: | ||||
def get_request(self, *args, **kwargs): | def get_request(self, *args, **kwargs): | ||||
return lambda: self(*args, **kwargs) | return lambda: self(*args, **kwargs) | ||||
class TestNodeCLI(): | class TestNodeCLI(): | ||||
"""Interface to bitcoin-cli for an individual node""" | """Interface to bitcoin-cli for an individual node""" | ||||
def __init__(self, binary, datadir): | def __init__(self, binary, datadir): | ||||
self.args = [] | self.options = [] | ||||
self.binary = binary | self.binary = binary | ||||
self.datadir = datadir | self.datadir = datadir | ||||
self.input = None | self.input = None | ||||
self.log = logging.getLogger('TestFramework.bitcoincli') | self.log = logging.getLogger('TestFramework.bitcoincli') | ||||
def __call__(self, *args, input=None): | def __call__(self, *options, input=None): | ||||
# TestNodeCLI is callable with bitcoin-cli command-line args | # TestNodeCLI is callable with bitcoin-cli command-line options | ||||
cli = TestNodeCLI(self.binary, self.datadir) | cli = TestNodeCLI(self.binary, self.datadir) | ||||
cli.args = [str(arg) for arg in args] | cli.options = [str(o) for o in options] | ||||
cli.input = input | cli.input = input | ||||
return cli | return cli | ||||
def __getattr__(self, command): | def __getattr__(self, command): | ||||
return TestNodeCLIAttr(self, command) | return TestNodeCLIAttr(self, command) | ||||
def batch(self, requests): | def batch(self, requests): | ||||
results = [] | results = [] | ||||
for request in requests: | for request in requests: | ||||
try: | try: | ||||
results.append(dict(result=request())) | results.append(dict(result=request())) | ||||
except JSONRPCException as e: | except JSONRPCException as e: | ||||
results.append(dict(error=e)) | results.append(dict(error=e)) | ||||
return results | return results | ||||
def send_cli(self, command, *args, **kwargs): | def send_cli(self, command, *args, **kwargs): | ||||
"""Run bitcoin-cli command. Deserializes returned string as python object.""" | """Run bitcoin-cli command. Deserializes returned string as python object.""" | ||||
pos_args = [str(arg) for arg in args] | pos_args = [str(arg) for arg in args] | ||||
named_args = [str(key) + "=" + str(value) | named_args = [str(key) + "=" + str(value) | ||||
for (key, value) in kwargs.items()] | for (key, value) in kwargs.items()] | ||||
assert not ( | assert not ( | ||||
pos_args and named_args), "Cannot use positional arguments and named arguments in the same bitcoin-cli call" | 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] + self.args | p_args = [self.binary, "-datadir=" + self.datadir] + self.options | ||||
if named_args: | if named_args: | ||||
p_args += ["-named"] | p_args += ["-named"] | ||||
p_args += [command] + pos_args + named_args | p_args += [command] + pos_args + named_args | ||||
self.log.debug("Running bitcoin-cli command: {}".format(command)) | self.log.debug("Running bitcoin-cli command: {}".format(command)) | ||||
process = subprocess.Popen(p_args, stdin=subprocess.PIPE, | process = subprocess.Popen(p_args, stdin=subprocess.PIPE, | ||||
stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True) | stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True) | ||||
cli_stdout, cli_stderr = process.communicate(input=self.input) | cli_stdout, cli_stderr = process.communicate(input=self.input) | ||||
returncode = process.poll() | returncode = process.poll() | ||||
Show All 13 Lines |