Changeset View
Changeset View
Standalone View
Standalone View
test/functional/test_framework/test_framework.py
Show First 20 Lines • Show All 69 Lines • ▼ Show 20 Lines | class BitcoinTestFramework(): | ||||
- setup_nodes() | - setup_nodes() | ||||
The __init__() and main() methods should not be overridden. | The __init__() and main() methods should not be overridden. | ||||
This class also contains various public and private helper methods.""" | This class also contains various public and private helper methods.""" | ||||
def __init__(self): | def __init__(self): | ||||
"""Sets test framework defaults. Do not override this method. Instead, override the set_test_params() method""" | """Sets test framework defaults. Do not override this method. Instead, override the set_test_params() method""" | ||||
self.chain = 'regtest' | |||||
self.setup_clean_chain = False | self.setup_clean_chain = False | ||||
self.nodes = [] | self.nodes = [] | ||||
self.network_thread = None | self.network_thread = None | ||||
self.mocktime = 0 | self.mocktime = 0 | ||||
# Wait for up to 60 seconds for the RPC server to respond | # Wait for up to 60 seconds for the RPC server to respond | ||||
self.rpc_timeout = 60 | self.rpc_timeout = 60 | ||||
self.supports_cli = False | self.supports_cli = False | ||||
self.bind_to_localhost_only = True | self.bind_to_localhost_only = True | ||||
▲ Show 20 Lines • Show All 189 Lines • ▼ Show 20 Lines | class BitcoinTestFramework(): | ||||
def run_test(self): | def run_test(self): | ||||
"""Tests must override this method to define test logic""" | """Tests must override this method to define test logic""" | ||||
raise NotImplementedError | raise NotImplementedError | ||||
# Public helper methods. These can be accessed by the subclass test | # Public helper methods. These can be accessed by the subclass test | ||||
# scripts. | # scripts. | ||||
def add_nodes(self, num_nodes, extra_args=None, | def add_nodes(self, num_nodes, extra_args=None, | ||||
*, rpchost=None, binary=None): | *, host=None, binary=None): | ||||
"""Instantiate TestNode objects. | """Instantiate TestNode objects. | ||||
Should only be called once after the nodes have been specified in | Should only be called once after the nodes have been specified in | ||||
set_test_params().""" | set_test_params().""" | ||||
if self.bind_to_localhost_only: | if self.bind_to_localhost_only: | ||||
extra_confs = [["bind=127.0.0.1"]] * num_nodes | extra_confs = [["bind=127.0.0.1"]] * num_nodes | ||||
else: | else: | ||||
extra_confs = [[]] * num_nodes | extra_confs = [[]] * num_nodes | ||||
if extra_args is None: | if extra_args is None: | ||||
extra_args = [[]] * num_nodes | extra_args = [[]] * num_nodes | ||||
if binary is None: | if binary is None: | ||||
binary = [self.options.bitcoind] * num_nodes | binary = [self.options.bitcoind] * num_nodes | ||||
assert_equal(len(extra_confs), num_nodes) | assert_equal(len(extra_confs), num_nodes) | ||||
assert_equal(len(extra_args), num_nodes) | assert_equal(len(extra_args), num_nodes) | ||||
assert_equal(len(binary), num_nodes) | assert_equal(len(binary), num_nodes) | ||||
for i in range(num_nodes): | for i in range(num_nodes): | ||||
self.nodes.append(TestNode( | self.nodes.append(TestNode( | ||||
i, | i, | ||||
get_datadir_path(self.options.tmpdir, i), | get_datadir_path(self.options.tmpdir, i), | ||||
host=rpchost, | chain=self.chain, | ||||
host=host, | |||||
rpc_port=rpc_port(i), | rpc_port=rpc_port(i), | ||||
p2p_port=p2p_port(i), | p2p_port=p2p_port(i), | ||||
timewait=self.rpc_timeout, | timewait=self.rpc_timeout, | ||||
bitcoind=binary[i], | bitcoind=binary[i], | ||||
bitcoin_cli=self.options.bitcoincli, | bitcoin_cli=self.options.bitcoincli, | ||||
mocktime=self.mocktime, | mocktime=self.mocktime, | ||||
coverage_dir=self.options.coveragedir, | coverage_dir=self.options.coveragedir, | ||||
extra_conf=extra_confs[i], | extra_conf=extra_confs[i], | ||||
▲ Show 20 Lines • Show All 138 Lines • ▼ Show 20 Lines | def _initialize_chain(self): | ||||
# find and delete old cache directories if any exist | # find and delete old cache directories if any exist | ||||
for i in range(MAX_NODES): | for i in range(MAX_NODES): | ||||
if os.path.isdir(get_datadir_path(self.options.cachedir, i)): | if os.path.isdir(get_datadir_path(self.options.cachedir, i)): | ||||
shutil.rmtree(get_datadir_path(self.options.cachedir, i)) | shutil.rmtree(get_datadir_path(self.options.cachedir, i)) | ||||
# Create cache directories, run bitcoinds: | # Create cache directories, run bitcoinds: | ||||
for i in range(MAX_NODES): | for i in range(MAX_NODES): | ||||
datadir = initialize_datadir(self.options.cachedir, i) | datadir = initialize_datadir( | ||||
self.options.cachedir, i, self.chain) | |||||
self.nodes.append(TestNode( | self.nodes.append(TestNode( | ||||
i, | i, | ||||
get_datadir_path(self.options.cachedir, i), | get_datadir_path(self.options.cachedir, i), | ||||
chain=self.chain, | |||||
extra_conf=["bind=127.0.0.1"], | extra_conf=["bind=127.0.0.1"], | ||||
extra_args=[], | extra_args=[], | ||||
host=None, | host=None, | ||||
rpc_port=rpc_port(i), | rpc_port=rpc_port(i), | ||||
p2p_port=p2p_port(i), | p2p_port=p2p_port(i), | ||||
timewait=self.rpc_timeout, | timewait=self.rpc_timeout, | ||||
bitcoind=self.options.bitcoind, | bitcoind=self.options.bitcoind, | ||||
bitcoin_cli=self.options.bitcoincli, | bitcoin_cli=self.options.bitcoincli, | ||||
▲ Show 20 Lines • Show All 41 Lines • ▼ Show 20 Lines | def _initialize_chain(self): | ||||
# Shut them down, and clean up cache directories: | # Shut them down, and clean up cache directories: | ||||
self.stop_nodes() | self.stop_nodes() | ||||
self.nodes = [] | self.nodes = [] | ||||
self.mocktime = 0 | self.mocktime = 0 | ||||
def cache_path(n, *paths): | def cache_path(n, *paths): | ||||
return os.path.join(get_datadir_path( | return os.path.join(get_datadir_path( | ||||
self.options.cachedir, n), "regtest", *paths) | self.options.cachedir, n), self.chain, *paths) | ||||
for i in range(MAX_NODES): | for i in range(MAX_NODES): | ||||
# Remove empty wallets dir | # Remove empty wallets dir | ||||
os.rmdir(cache_path(i, 'wallets')) | os.rmdir(cache_path(i, 'wallets')) | ||||
for entry in os.listdir(cache_path(i)): | for entry in os.listdir(cache_path(i)): | ||||
if entry not in ['chainstate', 'blocks']: | if entry not in ['chainstate', 'blocks']: | ||||
os.remove(cache_path(i, entry)) | os.remove(cache_path(i, entry)) | ||||
for i in range(self.num_nodes): | for i in range(self.num_nodes): | ||||
from_dir = get_datadir_path(self.options.cachedir, i) | from_dir = get_datadir_path(self.options.cachedir, i) | ||||
to_dir = get_datadir_path(self.options.tmpdir, i) | to_dir = get_datadir_path(self.options.tmpdir, i) | ||||
shutil.copytree(from_dir, to_dir) | shutil.copytree(from_dir, to_dir) | ||||
# Overwrite port/rpcport in bitcoin.conf | # Overwrite port/rpcport in bitcoin.conf | ||||
initialize_datadir(self.options.tmpdir, i) | initialize_datadir(self.options.tmpdir, i, self.chain) | ||||
def _initialize_chain_clean(self): | def _initialize_chain_clean(self): | ||||
"""Initialize empty blockchain for use by the test. | """Initialize empty blockchain for use by the test. | ||||
Create an empty blockchain and num_nodes wallets. | Create an empty blockchain and num_nodes wallets. | ||||
Useful if a test case wants complete control over initialization.""" | Useful if a test case wants complete control over initialization.""" | ||||
for i in range(self.num_nodes): | for i in range(self.num_nodes): | ||||
initialize_datadir(self.options.tmpdir, i) | initialize_datadir(self.options.tmpdir, i, self.chain) | ||||
def skip_if_no_py3_zmq(self): | def skip_if_no_py3_zmq(self): | ||||
"""Attempt to import the zmq package and skip the test if the import fails.""" | """Attempt to import the zmq package and skip the test if the import fails.""" | ||||
try: | try: | ||||
import zmq # noqa | import zmq # noqa | ||||
except ImportError: | except ImportError: | ||||
raise SkipTest("python3-zmq module not available.") | raise SkipTest("python3-zmq module not available.") | ||||
▲ Show 20 Lines • Show All 44 Lines • Show Last 20 Lines |