Browse Source

Improve TestNodeCLI output parsing

Parse JSONRPCException errors, and avoid JSON decode exception if RPC method
returns a plain string.
0.16
Russell Yanofsky 7 years ago committed by John Newbery
parent
commit
fcfb952bca
  1. 13
      test/functional/test_framework/test_node.py

13
test/functional/test_framework/test_node.py

@ -10,6 +10,7 @@ import http.client
import json import json
import logging import logging
import os import os
import re
import subprocess import subprocess
import time import time
@ -22,6 +23,9 @@ from .util import (
p2p_port, p2p_port,
) )
# For Python 3.4 compatibility
JSONDecodeError = getattr(json, "JSONDecodeError", ValueError)
BITCOIND_PROC_WAIT_TIMEOUT = 60 BITCOIND_PROC_WAIT_TIMEOUT = 60
class TestNode(): class TestNode():
@ -222,6 +226,13 @@ class TestNodeCLI():
cli_stdout, cli_stderr = process.communicate(input=self.input) cli_stdout, cli_stderr = process.communicate(input=self.input)
returncode = process.poll() returncode = process.poll()
if returncode: if returncode:
match = re.match(r'error code: ([-0-9]+)\nerror message:\n(.*)', cli_stderr)
if match:
code, message = match.groups()
raise JSONRPCException(dict(code=int(code), message=message))
# Ignore cli_stdout, raise with cli_stderr # Ignore cli_stdout, raise with cli_stderr
raise subprocess.CalledProcessError(returncode, self.binary, output=cli_stderr) raise subprocess.CalledProcessError(returncode, self.binary, output=cli_stderr)
return json.loads(cli_stdout, parse_float=decimal.Decimal) try:
return json.loads(cli_stdout, parse_float=decimal.Decimal)
except JSONDecodeError:
return cli_stdout.rstrip("\n")

Loading…
Cancel
Save