Changeset View
Changeset View
Standalone View
Standalone View
test/functional/test_framework/util.py
#!/usr/bin/env python3 | #!/usr/bin/env python3 | ||||
# Copyright (c) 2014-2016 The Bitcoin Core developers | # Copyright (c) 2014-2016 The Bitcoin Core developers | ||||
# Distributed under the MIT software license, see the accompanying | # Distributed under the MIT software license, see the accompanying | ||||
# file COPYING or http://www.opensource.org/licenses/mit-license.php. | # file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
from base64 import b64encode | from base64 import b64encode | ||||
from binascii import hexlify, unhexlify | from binascii import hexlify, unhexlify | ||||
from decimal import Decimal, ROUND_DOWN | from decimal import Decimal, ROUND_DOWN | ||||
import json | import json | ||||
import logging | import logging | ||||
import os | import os | ||||
import random | import random | ||||
import re | import re | ||||
from subprocess import CalledProcessError | |||||
import time | import time | ||||
from . import coverage | from . import coverage | ||||
from .authproxy import AuthServiceProxy, JSONRPCException | from .authproxy import AuthServiceProxy, JSONRPCException | ||||
logger = logging.getLogger("TestFramework.utils") | logger = logging.getLogger("TestFramework.utils") | ||||
# Assert functions | # Assert functions | ||||
▲ Show 20 Lines • Show All 41 Lines • ▼ Show 20 Lines | except exc as e: | ||||
"Expected substring not found:" + e.error['message']) | "Expected substring not found:" + e.error['message']) | ||||
except Exception as e: | except Exception as e: | ||||
raise AssertionError( | raise AssertionError( | ||||
"Unexpected exception raised: " + type(e).__name__) | "Unexpected exception raised: " + type(e).__name__) | ||||
else: | else: | ||||
raise AssertionError("No exception raised") | raise AssertionError("No exception raised") | ||||
def assert_raises_process_error(returncode, output, fun, *args, **kwds): | |||||
"""Execute a process and asserts the process return code and output. | |||||
Calls function `fun` with arguments `args` and `kwds`. Catches a CalledProcessError | |||||
and verifies that the return code and output are as expected. Throws AssertionError if | |||||
no CalledProcessError was raised or if the return code and output are not as expected. | |||||
Args: | |||||
returncode (int): the process return code. | |||||
output (string): [a substring of] the process output. | |||||
fun (function): the function to call. This should execute a process. | |||||
args*: positional arguments for the function. | |||||
kwds**: named arguments for the function. | |||||
""" | |||||
try: | |||||
fun(*args, **kwds) | |||||
except CalledProcessError as e: | |||||
if returncode != e.returncode: | |||||
raise AssertionError("Unexpected returncode %i" % e.returncode) | |||||
if output not in e.output: | |||||
raise AssertionError("Expected substring not found:" + e.output) | |||||
else: | |||||
raise AssertionError("No exception raised") | |||||
def assert_raises_jsonrpc(code, message, fun, *args, **kwds): | def assert_raises_jsonrpc(code, message, fun, *args, **kwds): | ||||
"""Run an RPC and verify that a specific JSONRPC exception code and message is raised. | """Run an RPC and verify that a specific JSONRPC exception code and message is raised. | ||||
Calls function `fun` with arguments `args` and `kwds`. Catches a JSONRPCException | Calls function `fun` with arguments `args` and `kwds`. Catches a JSONRPCException | ||||
and verifies that the error code and message are as expected. Throws AssertionError if | and verifies that the error code and message are as expected. Throws AssertionError if | ||||
no JSONRPCException was returned or if the error code/message are not as expected. | no JSONRPCException was raised or if the error code/message are not as expected. | ||||
Args: | Args: | ||||
code (int), optional: the error code returned by the RPC call (defined | code (int), optional: the error code returned by the RPC call (defined | ||||
in src/rpc/protocol.h). Set to None if checking the error code is not required. | in src/rpc/protocol.h). Set to None if checking the error code is not required. | ||||
message (string), optional: [a substring of] the error string returned by the | message (string), optional: [a substring of] the error string returned by the | ||||
RPC call. Set to None if checking the error string is not required | RPC call. Set to None if checking the error string is not required. | ||||
fun (function): the function to call. This should be the name of an RPC. | fun (function): the function to call. This should be the name of an RPC. | ||||
args*: positional arguments for the function. | args*: positional arguments for the function. | ||||
kwds**: named arguments for the function. | kwds**: named arguments for the function. | ||||
""" | """ | ||||
try: | try: | ||||
fun(*args, **kwds) | fun(*args, **kwds) | ||||
except JSONRPCException as e: | except JSONRPCException as e: | ||||
# JSONRPCException was thrown as expected. Check the code and message values are correct. | # JSONRPCException was thrown as expected. Check the code and message values are correct. | ||||
▲ Show 20 Lines • Show All 564 Lines • Show Last 20 Lines |