Added counter tests as well

This commit is contained in:
Ethan Frey
2017-06-16 13:38:51 +02:00
parent 9341b8be5d
commit 81d6d2425f
3 changed files with 147 additions and 0 deletions
+72
View File
@@ -0,0 +1,72 @@
# this is not executable, but helper functions for the other scripts
# these are general accounts to be prepared
ACCOUNTS=(jae ethan bucky rigel igor)
RICH=${ACCOUNTS[0]}
POOR=${ACCOUNTS[4]}
prepareClient() {
echo "Preparing client keys..."
${CLIENT_EXE} reset_all
assertTrue $?
for i in "${!ACCOUNTS[@]}"; do
newKey ${ACCOUNTS[$i]}
done
}
# initServer takes two args - (root dir, chain_id)
# and optionally port prefix as a third arg (default is 4665{6,7,8})
# it grabs the first account to give it the money
initServer() {
echo "Setting up genesis..."
SERVE_DIR=$1/server
assertNotNull "no chain" $2
CHAIN=$2
SERVER_LOG=$1/${SERVER_EXE}.log
${SERVER_EXE} init --home=$SERVE_DIR >>$SERVER_LOG
#change the genesis to the first account
GENKEY=$(${CLIENT_EXE} keys get ${RICH} -o json | jq .pubkey.data)
GENJSON=$(cat $SERVE_DIR/genesis.json)
echo $GENJSON | jq '.app_options.accounts[0].pub_key.data='$GENKEY \
| jq ".chain_id=\"$2\"" > $SERVE_DIR/genesis.json
# optionally set the port
if [ -n "$3" ]; then
echo "setting port $3"
sed -ie "s/4665/$3/" $SERVE_DIR/config.toml
fi
echo "Starting ${SERVER_EXE} server..."
${SERVER_EXE} start --home=$SERVE_DIR >>$SERVER_LOG 2>&1 &
sleep 5
PID_SERVER=$!
}
# initClient requires chain_id arg, port is optional (default 4665{5,6,7})
initClient() {
echo "Attaching ${CLIENT_EXE} client..."
PORT=${2:-4665}
# hard-code the expected validator hash
${CLIENT_EXE} init --chain-id=$1 --node=tcp://localhost:${PORT}7 --valhash=EB168E17E45BAEB194D4C79067FFECF345C64DE6
assertTrue "initialized light-client" $?
}
# newKeys makes a key for a given username, second arg optional password
newKey(){
assertNotNull "keyname required" "$1"
KEYPASS=${2:-qwertyuiop}
(echo $KEYPASS; echo $KEYPASS) | ${CLIENT_EXE} keys new $1 >/dev/null 2>/dev/null
assertTrue "created $1" $?
assertTrue "$1 doesn't exist" "${CLIENT_EXE} keys get $1"
}
# getAddr gets the address for a key name
getAddr() {
assertNotNull "keyname required" "$1"
RAW=$(${CLIENT_EXE} keys get $1)
assertTrue "no key for $1" $?
# print the addr
echo $RAW | cut -d' ' -f2
}
+137
View File
@@ -0,0 +1,137 @@
#!/bin/bash
# these are two globals to control all scripts (can use eg. counter instead)
SERVER_EXE=counter
CLIENT_EXE=basecli # TODO: move to countercli
oneTimeSetUp() {
# these are passed in as args
BASE_DIR=$HOME/.basecoin_test_counter
CHAIN_ID="counter-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 1234
echo pid $PID_SERVER
initClient $CHAIN_ID 1234
echo "...Testing may begin!"
echo
echo
echo
}
oneTimeTearDown() {
echo
echo
echo "stopping $SERVER_EXE test server..."
kill -9 $PID_SERVER >/dev/null 2>&1
sleep 1
}
# blatently copied to make sure it works with counter as well
test00GetAccount() {
SENDER=$(getAddr $RICH)
RECV=$(getAddr $POOR)
assertFalse "requires arg" "${CLIENT_EXE} query account"
ACCT=$(${CLIENT_EXE} query account $SENDER)
assertTrue "must have proper genesis account" $?
assertEquals "no tx" "0" $(echo $ACCT | jq .data.sequence)
assertEquals "has money" "9007199254740992" $(echo $ACCT | jq .data.coins[0].amount)
ACCT2=$(${CLIENT_EXE} query account $RECV)
assertFalse "has no genesis account" $?
}
# blatently copied to make sure it works with counter as well
test01SendTx() {
SENDER=$(getAddr $RICH)
RECV=$(getAddr $POOR)
assertFalse "missing dest" "${CLIENT_EXE} tx send --amount=992mycoin --sequence=1 2>/dev/null"
assertFalse "bad password" "echo foo | ${CLIENT_EXE} tx send --amount=992mycoin --sequence=1 --to=$RECV --name=$RICH 2>/dev/null"
# we have to remove the password request from stdout, to just get the json
RES=$(echo qwertyuiop | ${CLIENT_EXE} tx send --amount=992mycoin --sequence=1 --to=$RECV --name=$RICH 2>/dev/null | tail -n +2)
assertTrue "sent tx" $?
HASH=$(echo $RES | jq .hash | tr -d \")
TX_HEIGHT=$(echo $RES | jq .height)
assertEquals "good check" "0" $(echo $RES | jq .check_tx.code)
assertEquals "good deliver" "0" $(echo $RES | jq .deliver_tx.code)
# make sure sender goes down
ACCT=$(${CLIENT_EXE} query account $SENDER)
assertTrue "must have genesis account" $?
assertEquals "one tx" "1" $(echo $ACCT | jq .data.sequence)
assertEquals "has money" "9007199254740000" $(echo $ACCT | jq .data.coins[0].amount)
# make sure recipient goes up
ACCT2=$(${CLIENT_EXE} query account $RECV)
assertTrue "must have new account" $?
assertEquals "no tx" "0" $(echo $ACCT2 | jq .data.sequence)
assertEquals "has money" "992" $(echo $ACCT2 | jq .data.coins[0].amount)
# make sure tx is indexed
TX=$(${CLIENT_EXE} query tx $HASH)
assertTrue "found tx" $?
assertEquals "proper height" $TX_HEIGHT $(echo $TX | jq .height)
assertEquals "type=send" '"send"' $(echo $TX | jq .data.type)
assertEquals "proper sender" "\"$SENDER\"" $(echo $TX | jq .data.data.inputs[0].address)
assertEquals "proper out amount" "992" $(echo $TX | jq .data.data.outputs[0].coins[0].amount)
}
test02GetCounter() {
COUNT=$(${CLIENT_EXE} query counter)
assertFalse "no default count" $?
}
test02AddCount() {
SENDER=$(getAddr $RICH)
assertFalse "bad password" "echo hi | ${CLIENT_EXE} tx counter --amount=1000mycoin --sequence=2 --name=${RICH} 2>/dev/null"
# we have to remove the password request from stdout, to just get the json
RES=$(echo qwertyuiop | ${CLIENT_EXE} tx counter --amount=10mycoin --sequence=2 --name=${RICH} --valid --countfee=5mycoin 2>/dev/null | tail -n +2)
assertTrue "sent tx" $?
HASH=$(echo $RES | jq .hash | tr -d \")
TX_HEIGHT=$(echo $RES | jq .height)
assertEquals "good check" "0" $(echo $RES | jq .check_tx.code)
assertEquals "good deliver" "0" $(echo $RES | jq .deliver_tx.code)
# check new state
COUNT=$(${CLIENT_EXE} query counter)
assertTrue "count now set" $?
assertEquals "one tx" "1" $(echo $COUNT | jq .data.Counter)
assertEquals "has money" "5" $(echo $COUNT | jq .data.TotalFees[0].amount)
# FIXME: cannot load apptx properly.
# Look at the stack trace
# This cannot be fixed with the current ugly apptx structure...
# Leave for refactoring
# make sure tx is indexed
# echo hash $HASH
# TX=$(${CLIENT_EXE} query tx $HASH --trace)
# echo tx $TX
# if [-z assertTrue "found tx" $?]; then
# assertEquals "proper height" $TX_HEIGHT $(echo $TX | jq .height)
# assertEquals "type=app" '"app"' $(echo $TX | jq .data.type)
# assertEquals "proper sender" "\"$SENDER\"" $(echo $TX | jq .data.data.input.address)
# fi
# echo $TX
}
# 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