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)
source $HERE/testutil.sh
# log commands
# set -x
cosmos-testd tx cosmostest new-bid 0 800 \
-y --from alice \
| expect_fail "Can't create bids for nonexistent auctions"
log_test "Typical Auction Flow"
out=$(cosmos-testd tx cosmostest new-auction asdf asdf utoken -y --from bob --chain-id cosmostest)
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
cosmos-testd tx cosmostest new-auction asdf asdf 100 token 1662155879 \
-y --from bob \
| expect_success "New auction is created"

View File

@ -1,21 +1,39 @@
#!/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
function assert_eq {
if [[ $1 = $2 ]]; then
log_fail
echo Expected $3 to equal $2, instead got $1.
exit 1
else
echo $3 has correct value.
log_ok
echo $3 correctly equals $2
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.
function get_block_time {
return \
$(
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}'
}
function filter_out {
out=$1
filter=$2
function get_key {
local out=$1
local filter=$2
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 {
out=$1
read out
local msg=${1:-Expect transaction to succeed}
# if we want to pipe:
# read out
res=$(echo $out | grep code: | awk -F ': ' '{print $2}')
raw_log=$(echo $out | grep raw_log: | awk -F ': ' '{print $2}')
# local res=$(echo $out | grep code: | 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
log_ok
echo Transaction succeeded.
echo $msg
else
log_fail
echo Transaction unexpectedly reverted.
echo
echo $msg
echo $raw_log
exit 1
fi
}
function expect_fail {
out=$1
res=$(echo $out | grep code: | awk -F ': ' '{print $2}')
read out
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
log_fail
echo Transaction unexpectedly succeeded.
echo $msg
exit 1
else
log_ok
echo Transaction reverted.
echo $msg
fi
}