Changeset View
Changeset View
Standalone View
Standalone View
test/functional/test_framework/test_framework.py
Show First 20 Lines • Show All 44 Lines • ▼ Show 20 Lines | |||||
TEST_EXIT_PASSED = 0 | TEST_EXIT_PASSED = 0 | ||||
TEST_EXIT_FAILED = 1 | TEST_EXIT_FAILED = 1 | ||||
TEST_EXIT_SKIPPED = 77 | TEST_EXIT_SKIPPED = 77 | ||||
# Timestamp is Dec. 1st, 2019 at 00:00:00 | # Timestamp is Dec. 1st, 2019 at 00:00:00 | ||||
TIMESTAMP_IN_THE_PAST = 1575158400 | TIMESTAMP_IN_THE_PAST = 1575158400 | ||||
class SkipTest(Exception): | |||||
"""This exception is raised to skip a test""" | |||||
def __init__(self, message): | |||||
self.message = message | |||||
class BitcoinTestFramework(): | class BitcoinTestFramework(): | ||||
"""Base class for a bitcoin test script. | """Base class for a bitcoin test script. | ||||
Individual bitcoin test scripts should subclass this class and override the set_test_params() and run_test() methods. | Individual bitcoin test scripts should subclass this class and override the set_test_params() and run_test() methods. | ||||
Individual tests can also override the following methods to customize the test setup: | Individual tests can also override the following methods to customize the test setup: | ||||
- add_options() | - add_options() | ||||
▲ Show 20 Lines • Show All 81 Lines • ▼ Show 20 Lines | def main(self): | ||||
self.network_thread.start() | self.network_thread.start() | ||||
success = TestStatus.FAILED | success = TestStatus.FAILED | ||||
try: | try: | ||||
if self.options.usecli and not self.supports_cli: | if self.options.usecli and not self.supports_cli: | ||||
raise SkipTest( | raise SkipTest( | ||||
"--usecli specified but test does not support using CLI") | "--usecli specified but test does not support using CLI") | ||||
self.skip_test_if_missing_module() | |||||
self.setup_chain() | self.setup_chain() | ||||
self.setup_network() | self.setup_network() | ||||
self.import_deterministic_coinbase_privkeys() | self.import_deterministic_coinbase_privkeys() | ||||
self.run_test() | self.run_test() | ||||
success = TestStatus.PASSED | success = TestStatus.PASSED | ||||
except JSONRPCException: | except JSONRPCException: | ||||
self.log.exception("JSONRPC error") | self.log.exception("JSONRPC error") | ||||
except SkipTest as e: | except SkipTest as e: | ||||
▲ Show 20 Lines • Show All 53 Lines • ▼ Show 20 Lines | class BitcoinTestFramework(): | ||||
def set_test_params(self): | def set_test_params(self): | ||||
"""Tests must this method to change default values for number of nodes, topology, etc""" | """Tests must this method to change default values for number of nodes, topology, etc""" | ||||
raise NotImplementedError | raise NotImplementedError | ||||
def add_options(self, parser): | def add_options(self, parser): | ||||
"""Override this method to add command-line options to the test""" | """Override this method to add command-line options to the test""" | ||||
pass | pass | ||||
def skip_test_if_missing_module(self): | |||||
"""Override this method to skip a test if a module is not compiled""" | |||||
pass | |||||
def setup_chain(self): | def setup_chain(self): | ||||
"""Override this method to customize blockchain setup""" | """Override this method to customize blockchain setup""" | ||||
self.log.info("Initializing test directory " + self.options.tmpdir) | self.log.info("Initializing test directory " + self.options.tmpdir) | ||||
if self.setup_clean_chain: | if self.setup_clean_chain: | ||||
self._initialize_chain_clean() | self._initialize_chain_clean() | ||||
else: | else: | ||||
self._initialize_chain() | self._initialize_chain() | ||||
▲ Show 20 Lines • Show All 285 Lines • ▼ Show 20 Lines | class BitcoinTestFramework(): | ||||
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) | ||||
def skip_if_no_py3_zmq(self): | |||||
class SkipTest(Exception): | |||||
"""This exception is raised to skip a test""" | |||||
def __init__(self, message): | |||||
self.message = message | |||||
def skip_if_no_py3_zmq(): | |||||
"""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.") | ||||
def skip_if_no_bitcoind_zmq(self): | |||||
def skip_if_no_bitcoind_zmq(test_instance): | |||||
"""Skip the running test if bitcoind has not been compiled with zmq support.""" | """Skip the running test if bitcoind has not been compiled with zmq support.""" | ||||
if not is_zmq_enabled(test_instance): | if not self.is_zmq_compiled(): | ||||
raise SkipTest("bitcoind has not been built with zmq enabled.") | raise SkipTest("bitcoind has not been built with zmq enabled.") | ||||
def skip_if_no_wallet(self): | |||||
"""Skip the running test if wallet has not been compiled.""" | |||||
if not self.is_wallet_compiled(): | |||||
raise SkipTest("wallet has not been compiled.") | |||||
def skip_if_no_cli(self): | |||||
"""Skip the running test if bitcoin-cli has not been compiled.""" | |||||
if not self.is_cli_compiled(): | |||||
raise SkipTest("bitcoin-cli has not been compiled.") | |||||
def is_zmq_enabled(test_instance): | def is_cli_compiled(self): | ||||
"""Checks whether zmq is enabled or not.""" | """Checks whether bitcoin-cli was compiled.""" | ||||
config = configparser.ConfigParser() | config = configparser.ConfigParser() | ||||
config.read_file(open(test_instance.options.configfile, encoding='utf-8')) | config.read_file(open(self.options.configfile, encoding='utf-8')) | ||||
return config["components"].getboolean("ENABLE_UTILS") | |||||
def is_wallet_compiled(self): | |||||
"""Checks whether the wallet module was compiled.""" | |||||
config = configparser.ConfigParser() | |||||
config.read_file(open(self.options.configfile, encoding='utf-8')) | |||||
return config["components"].getboolean("ENABLE_WALLET") | |||||
def is_zmq_compiled(self): | |||||
"""Checks whether the zmq module was compiled.""" | |||||
config = configparser.ConfigParser() | |||||
config.read_file(open(self.options.configfile, encoding='utf-8')) | |||||
return config["components"].getboolean("ENABLE_ZMQ") | return config["components"].getboolean("ENABLE_ZMQ") |