diff --git a/clitest/common.sh b/clitest/common.sh new file mode 100644 index 000000000..3294548a0 --- /dev/null +++ b/clitest/common.sh @@ -0,0 +1,67 @@ +# this is not executable, but helper functions for the other scripts + +prepareClient() { + echo "Preparing client keys..." + basecli 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/basecoin.log + basecoin init --home=$SERVE_DIR >>$SERVER_LOG + + #change the genesis to the first account + GENKEY=$(basecli keys get ${ACCOUNTS[0]} -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..." + basecoin 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..." + PORT=${2:-4665} + # hard-code the expected validator hash + basecli 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) | basecli keys new $1 >/dev/null 2>/dev/null + assertTrue "created $1" $? + assertTrue "$1 doesn't exist" "basecli keys get $1" +} + +# getAddr gets the address for a key name +getAddr() { + assertNotNull "keyname required" "$1" + RAW=$(basecli keys get $1) + assertTrue "no key for $1" $? + # print the addr + echo $RAW | cut -d' ' -f2 +} diff --git a/tests/cli/basictx.sh b/tests/cli/basictx.sh index cbb15b434..8584535e5 100755 --- a/tests/cli/basictx.sh +++ b/tests/cli/basictx.sh @@ -1,11 +1,11 @@ #!/bin/bash + oneTimeSetUp() { BASE_DIR=$HOME/.basecoin_test_basictx - LOG=$BASE_DIR/test.log - SERVER_LOG=$BASE_DIR/basecoin.log + CHAIN_ID=my-test-chain - rm -rf $BASE_DIR + rm -rf $BASE_DIR 2>/dev/null mkdir -p $BASE_DIR ACCOUNTS=(jae ethan bucky rigel igor) @@ -13,13 +13,14 @@ oneTimeSetUp() { POOR=${ACCOUNTS[1]} # set up client + export BC_HOME=${BASE_DIR}/client prepareClient # start basecoin server (with counter) - initServer + initServer $BASE_DIR $CHAIN_ID 3456 echo pid $PID_SERVER - initClient + initClient $CHAIN_ID 3456 echo "...Testing may begin!" echo @@ -35,59 +36,6 @@ oneTimeTearDown() { sleep 1 } -prepareClient() { - echo "Preparing client keys..." - export BC_HOME=$BASE_DIR/client - basecli reset_all - assertTrue $? - - for i in "${!ACCOUNTS[@]}"; do - newKey ${ACCOUNTS[$i]} - done -} - -initServer() { - echo "Setting up genesis..." - SERVE_DIR=$BASE_DIR/server - rm -rf $SERVE_DIR 2>/dev/null - basecoin init --home=$SERVE_DIR >>$SERVER_LOG - - #change the genesis to the first account - GENKEY=$(basecli 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 > $SERVE_DIR/genesis.json - - echo "Starting server..." - basecoin start --home=$SERVE_DIR >>$SERVER_LOG 2>&1 & - sleep 5 - PID_SERVER=$! -} - -initClient() { - echo "Attaching client..." - # hard-code the expected validator hash - basecli init --chain-id=test_chain_id --node=tcp://localhost:46657 --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) | basecli keys new $1 >>$LOG 2>/dev/null - assertTrue "created $1" $? - assertTrue "$1 doesn't exist" "basecli keys get $1" -} - -# getAddr gets the address for a key name -getAddr() { - assertNotNull "keyname required" "$1" - RAW=$(basecli keys get $1) - assertTrue "no key for $1" $? - # print the addr - echo $RAW | cut -d' ' -f2 -} - test00GetAccount() { SENDER=$(getAddr $RICH) RECV=$(getAddr $POOR) @@ -140,4 +88,8 @@ test01SendTx() { # 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