viable command-based testing framework

master
michael 2022-09-03 07:41:54 +00:00
parent c1ee2ec7a8
commit 61a037ee3c
2 changed files with 53 additions and 28 deletions

View File

@ -4,18 +4,11 @@
HERE=$(cd $(dirname $BASH_SOURCE) && pwd) HERE=$(cd $(dirname $BASH_SOURCE) && pwd)
source $HERE/testutil.sh source $HERE/testutil.sh
# log commands cosmos-testd tx cosmostest new-bid 0 800 \
# set -x -y --from alice \
| expect_fail "Can't create bids for nonexistent auctions"
log_test "Typical Auction Flow" cosmos-testd tx cosmostest new-auction asdf asdf 100 token 1662155879 \
-y --from bob \
out=$(cosmos-testd tx cosmostest new-auction asdf asdf utoken -y --from bob --chain-id cosmostest) | expect_success "New auction is created"
expect_success $out
log_test "Failed Bidding Edge Cases"
# out=$(cosmos-testd tx cosmostest new-bid 0 800 -y --from bob --chain-id cosmostest | expect_fail)
# expect_success $out
# cosmos-testd tx cosmostest new-bid 0 800 --from bob --chain-id cosmostest

View File

@ -1,21 +1,39 @@
#!/bin/bash #!/bin/bash
# Configuration options that all tests must include
set -e
cosmos-testd config chain-id cosmostest
cosmos-testd config output json
# Compares $1 & $2, exiting with $3 (label) if not eq # Compares $1 & $2, exiting with $3 (label) if not eq
function assert_eq { function assert_eq {
if [[ $1 = $2 ]]; then if [[ $1 = $2 ]]; then
log_fail
echo Expected $3 to equal $2, instead got $1. echo Expected $3 to equal $2, instead got $1.
exit 1 exit 1
else else
echo $3 has correct value. log_ok
echo $3 correctly equals $2
fi fi
} }
# Get balance of an account.
function get_balance {
addr=$1
denom=$2
return \
$(
curl http://0.0.0.0:1317/cosmos/bank/v1beta1/balances/$addr/by_denom?denom=$denom \
| jq -M ".balance.amount"
)
}
# Get current block time from chain. # Get current block time from chain.
function get_block_time { function get_block_time {
return \ return \
$( $(
curl http://0.0.0.0:1317/cosmos/base/tendermint/v1beta1/blocks/latest \ curl http://0.0.0.0:1317/cosmos/base/tendermint/v1beta1/blocks/latest \
| jq ".blocks.header.height" | jq -M ".blocks.header.height"
) )
} }
@ -24,40 +42,54 @@ function get_code {
return $out | grep code: | awk -F ': ' '{print $2}' return $out | grep code: | awk -F ': ' '{print $2}'
} }
function filter_out { function get_key {
out=$1 local out=$1
filter=$2 local filter=$2
echo $out | jq -M $filter echo $out | jq -M $filter
} }
function now {
local op=$1
local amount=$2
if [[ op = "" ]]; then
date +%s
else
echo "$(date +%s) $op $amount" | bc
fi
}
function expect_success { function expect_success {
out=$1 read out
local msg=${1:-Expect transaction to succeed}
# if we want to pipe: # if we want to pipe:
# read out # read out
res=$(echo $out | grep code: | awk -F ': ' '{print $2}') # local res=$(echo $out | grep code: | awk -F ': ' '{print $2}')
raw_log=$(echo $out | grep raw_log: | awk -F ': ' '{print $2}') # local raw_log=$(echo $out | awk -F ': ' '{print $2}')
local res=$(echo $out | jq -M ".code")
local raw_log=$(echo $out | jq -M ".raw_log")
if [[ $res = 0 ]]; then if [[ $res = 0 ]]; then
log_ok log_ok
echo Transaction succeeded. echo $msg
else else
log_fail log_fail
echo Transaction unexpectedly reverted. echo $msg
echo
echo $raw_log echo $raw_log
exit 1 exit 1
fi fi
} }
function expect_fail { function expect_fail {
out=$1 read out
res=$(echo $out | grep code: | awk -F ': ' '{print $2}') local msg=${1:-Expect transaction to revert}
# local res=$(echo $out | grep code: | awk -F ': ' '{print $2}')
local res=$(echo $out | jq -M ".code")
if [[ $res = 0 ]]; then if [[ $res = 0 ]]; then
log_fail log_fail
echo Transaction unexpectedly succeeded. echo $msg
exit 1 exit 1
else else
log_ok log_ok
echo Transaction reverted. echo $msg
fi fi
} }