diff --git a/tests/cli/common.sh b/tests/cli/common.sh index edc7c0e125..12ca5d5a6f 100644 --- a/tests/cli/common.sh +++ b/tests/cli/common.sh @@ -1,13 +1,13 @@ # This is not executable, but helper functions for the other scripts # XXX XXX XXX XXX XXX -# The following global variables must be defined before calling common functions: +# The following global variables must be defined before calling common functions: # SERVER_EXE=foobar # Server binary name # CLIENT_EXE=foobarcli # Client binary name -# ACCOUNTS=(foo bar) # List of accounts for initialization +# ACCOUNTS=(foo bar) # List of accounts for initialization # RICH=${ACCOUNTS[0]} # Account to assign genesis balance -# XXX Ex Usage: quickSetup $WORK_NAME $CHAIN_ID +# XXX Ex Usage: quickSetup $WORK_NAME $CHAIN_ID # Desc: Start the program, use with shunit2 OneTimeSetUp() quickSetup() { # These are passed in as args @@ -73,17 +73,30 @@ initServer() { fi echo "Starting ${SERVER_EXE} server..." - ${SERVER_EXE} start --home=$SERVE_DIR >>$SERVER_LOG 2>&1 & + startServer $SERVE_DIR $SERVER_LOG + return $? +} + +# usage: startServer SERVE_DIR SERVER_LOG +startServer() { + ${SERVER_EXE} start --home=$1 >>$2 2>&1 & sleep 5 PID_SERVER=$! disown if ! ps $PID_SERVER >/dev/null; then echo "**FAILED**" - # cat $SERVER_LOG - # return 1 + cat $SERVER_LOG + return 1 fi } +# XXX Ex Usage: stopServer $PID_SERVER +stopServer() { + echo "stopping $SERVER_EXE test server..." + kill -9 $1 >/dev/null 2>&1 + sleep 1 +} + # XXX Ex Usage1: initClient $CHAINID # XXX Ex Usage2: initClient $CHAINID $PORTPREFIX # Desc: Initialize the client program @@ -121,7 +134,11 @@ getAddr() { checkAccount() { # make sure sender goes down ACCT=$(${CLIENT_EXE} query account $1) - assertTrue "must have genesis account" $? + if ! assertTrue "account must exist: $ACCT" $?; then + return 1 + fi + + if [ -n "$DEBUG" ]; then echo $ACCT; echo; fi assertEquals "proper sequence" "$2" $(echo $ACCT | jq .data.sequence) assertEquals "proper money" "$3" $(echo $ACCT | jq .data.coins[0].amount) return $? @@ -145,6 +162,8 @@ txSucceeded() { checkSendTx() { TX=$(${CLIENT_EXE} query tx $1) assertTrue "found tx" $? + if [ -n "$DEBUG" ]; then echo $TX; echo; fi + assertEquals "proper height" $2 $(echo $TX | jq .height) assertEquals "type=send" '"send"' $(echo $TX | jq .data.type) assertEquals "proper sender" "\"$3\"" $(echo $TX | jq .data.data.inputs[0].address) diff --git a/tests/cli/restart.sh b/tests/cli/restart.sh new file mode 100755 index 0000000000..dfdfe7c547 --- /dev/null +++ b/tests/cli/restart.sh @@ -0,0 +1,106 @@ +#!/bin/bash + +# these are two globals to control all scripts (can use eg. counter instead) +SERVER_EXE=basecoin +CLIENT_EXE=basecli + +oneTimeSetUp() { + # these are passed in as args + BASE_DIR=$HOME/.basecoin_test_restart + CHAIN_ID=restart-chain + + rm -rf $BASE_DIR 2>/dev/null + mkdir -p $BASE_DIR + + # set up client - make sure you use the proper prefix if you set + # a custom CLIENT_EXE + export BC_HOME=${BASE_DIR}/client + prepareClient + + # start basecoin server (with counter) + initServer $BASE_DIR $CHAIN_ID 3456 + if [ $? != 0 ]; then return 1; fi + + initClient $CHAIN_ID 34567 + + echo "...Testing may begin!" + echo + echo + echo +} + +oneTimeTearDown() { + echo + echo + stopServer $PID_SERVER +} + +test00PreRestart() { + SENDER=$(getAddr $RICH) + RECV=$(getAddr $POOR) + + RES=$(echo qwertyuiop | ${CLIENT_EXE} tx send --amount=992mycoin --sequence=1 --to=$RECV --name=$RICH 2>/dev/null) + txSucceeded $? "$RES" + TX=`echo $RES | cut -d: -f2-` + HASH=$(echo $TX | jq .hash | tr -d \") + TX_HEIGHT=$(echo $TX | jq .height) + + checkAccount $SENDER "1" "9007199254740000" + checkAccount $RECV "0" "992" + + # make sure tx is indexed + checkSendTx $HASH $TX_HEIGHT $SENDER "992" + +} + + +test01OnRestart() { + SENDER=$(getAddr $RICH) + RECV=$(getAddr $POOR) + + RES=$(echo qwertyuiop | ${CLIENT_EXE} tx send --amount=10000mycoin --sequence=2 --to=$RECV --name=$RICH 2>/dev/null) + txSucceeded $? "$RES" + if [ $? != 0 ]; then echo "can't make tx!"; return 1; fi + + TX=`echo $RES | cut -d: -f2-` + HASH=$(echo $TX | jq .hash | tr -d \") + TX_HEIGHT=$(echo $TX | jq .height) + + # wait til we have quite a few blocks... like at least 20, + # so the query command won't just wait for the next eg. 7 blocks to verify the result + echo "waiting to generate lots of blocks..." + sleep 20 + echo "done waiting!" + + # last minute tx just at the block cut-off... + RES=$(echo qwertyuiop | ${CLIENT_EXE} tx send --amount=20000mycoin --sequence=3 --to=$RECV --name=$RICH 2>/dev/null) + txSucceeded $? "$RES" + if [ $? != 0 ]; then echo "can't make second tx!"; return 1; fi + + # now we do a restart... + stopServer $PID_SERVER + startServer $BASE_DIR/server $BASE_DIR/${SERVER_EXE}.log + if [ $? != 0 ]; then echo "can't restart server!"; return 1; fi + + # make sure queries still work properly, with all 3 tx now executed + checkAccount $SENDER "3" "9007199254710000" + checkAccount $RECV "0" "30992" + + # make sure tx is indexed + checkSendTx $HASH $TX_HEIGHT $SENDER "10000" + + # for double-check of logs + if [ -n "$DEBUG" ]; then + cat $BASE_DIR/${SERVER_EXE}.log; + fi +} + + +# load and run these tests with shunit2! +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" #get this files directory + +# load common helpers +. $DIR/common.sh + +. $DIR/shunit2 +