diff --git a/qa/rpc-tests/test_framework/tests/README.md b/qa/rpc-tests/test_framework/tests/README.md new file mode 100644 --- /dev/null +++ b/qa/rpc-tests/test_framework/tests/README.md @@ -0,0 +1,18 @@ +Test framework sanity tests +=========================== + +This folder is intended for sanity tests (self tests) of the test framework. + +Tests here are not intended to test Bitcoin functionality, but rather to +test aspects of the Python test framework. + +As such, they are not bound into the regular test suites embedded in the +rpc-tests.py test runner, but have to be executed in a special way through +the `run-self-tests.sh` script in this folder, or executed manually. + +There is also not yet an associated build system (Make) target for running +these self-tests. + +To execute, they need the PYTHONPATH to include the qa/rpc-tests/ folder, +so that they can import the `test_framework` module and friends. +The `run-self-tests.sh` wrapper takes care of that. diff --git a/qa/rpc-tests/test_framework/tests/initialize_direct.py b/qa/rpc-tests/test_framework/tests/initialize_direct.py new file mode 100755 --- /dev/null +++ b/qa/rpc-tests/test_framework/tests/initialize_direct.py @@ -0,0 +1,44 @@ +#!/usr/bin/env python3 +# Copyright (c) 2017 The Bitcoin developers +# Distributed under the MIT software license, see the accompanying +# file COPYING or http://www.opensource.org/licenses/mit-license.php. +""" +Test the initialize_chain function. +""" + +from test_framework.test_framework import BitcoinTestFramework +from test_framework.util import start_node, stop_nodes, initialize_chain + + +# number of nodes to launch +NUM_NODES = 4 + + +class Direct_Initialize_Test (BitcoinTestFramework): + ''' + This tests the direct use of the initialize function. + ''' + + def __init__(self): + super(Direct_Initialize_Test, self).__init__() + self.num_nodes = NUM_NODES + # Do not set up chain - we will call initialize_chain to do it. + self.setup_clean_chain = True + + def setup_chain(self): + print("Setting up chain") + initialize_chain(self.options.tmpdir, self.num_nodes, + self.options.cachedir) + + def run_test(self): + # Stop nodes which have been started by default + stop_nodes(self.nodes) + + # Start them up again because test framework tries to stop + # nodes at end of test, and will deliver error messages + # if none are running. + for i in range(NUM_NODES): + self.nodes[i] = start_node(i, self.options.tmpdir) + +if __name__ == '__main__': + Direct_Initialize_Test().main() diff --git a/qa/rpc-tests/test_framework/tests/run_self_tests.sh b/qa/rpc-tests/test_framework/tests/run_self_tests.sh new file mode 100644 --- /dev/null +++ b/qa/rpc-tests/test_framework/tests/run_self_tests.sh @@ -0,0 +1,53 @@ +#!/bin/sh +# Copyright (c) 2017 The Bitcoin developers +# Distributed under the MIT software license, see the accompanying +# file COPYING or http://www.opensource.org/licenses/mit-license.php. +# +# Executes the test framework self-tests located in +# qa/rpc-tests/test_framework/tests. +# +# You should be able to call this script from pretty much anywhere. +# It takes care of setting up the PYTHONPATH so tests can run. + +# Enable exit on any error. +set -e + +# Save folder from which script was called. +# We may need to return after looking for the base folder. +start_folder="$(pwd)" + +if [ -d qa/rpc-tests ] +then + # called from top level + RPC_TESTS_DIR=$(pwd)/qa/rpc-tests +elif [ -d ../qa/rpc-tests ] +then + # called from out of tree build + RPC_TESTS_DIR=$(cd ../qa/rpc-tests && pwd) +else + # chop off the script's filename to get path + scriptpath=$(dirname $0) + RPC_TESTS_DIR=$(cd $scriptpath/../.. && pwd) +fi + +export PYTHONPATH="$PYTHONPATH:$RPC_TESTS_DIR" + +# Go to where the tests are located +cd $RPC_TESTS_DIR/test_framework/tests + +echo "Running test framework self-tests..." + +# Run all the Python tests we find there. +for t in *.py +do + echo "`date -u`: Starting self-test: $t" + SRCDIR=$RPC_TESTS_DIR/../.. python3 ./$t + echo "`date -u`: Finished self-test: $t" + echo +done + +# Return to calling folder + +cd "$start_folder" + +echo "Completed test framework self-tests." diff --git a/qa/rpc-tests/test_framework/tests/simple-initialize-chain.py b/qa/rpc-tests/test_framework/tests/simple-initialize-chain.py new file mode 100755 --- /dev/null +++ b/qa/rpc-tests/test_framework/tests/simple-initialize-chain.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python3 +# Copyright (c) 2017 The Bitcoin developers +# Distributed under the MIT software license, see the accompanying +# file COPYING or http://www.opensource.org/licenses/mit-license.php. +""" +Test the initialize_chain function by setting up a normal chain. +This is just to exercise initialize_chain() and some of the node +start/stop machinery. +""" + +from test_framework.test_framework import BitcoinTestFramework +from test_framework.util import start_node, stop_nodes + + +# number of nodes to launch +NUM_NODES = 4 + + +class Simple_Initialize_Test (BitcoinTestFramework): + + def __init__(self): + super(Simple_Initialize_Test, self).__init__() + self.num_nodes = NUM_NODES + self.setup_clean_chain = True + + def run_test(self): + # Stop nodes which have been started by default + stop_nodes(self.nodes) + + # Start them up again because test framework tries to stop + # nodes at end of test, and will deliver error messages + # if none are running. + for i in range(NUM_NODES): + self.nodes[i] = start_node(i, self.options.tmpdir) + +if __name__ == '__main__': + Simple_Initialize_Test().main()