gpu-compute-chain/tests/testutil.sh

118 lines
2.3 KiB
Bash
Raw Normal View History

2022-09-02 13:48:12 -07:00
#!/bin/bash
2022-09-03 00:41:54 -07:00
# Configuration options that all tests must include
set -e
cosmos-testd config chain-id cosmostest
cosmos-testd config output json
2022-09-02 13:48:12 -07:00
# Compares $1 & $2, exiting with $3 (label) if not eq
function assert_eq {
if [[ $1 = $2 ]]; then
2022-09-03 00:41:54 -07:00
log_fail
2022-09-02 13:48:12 -07:00
echo Expected $3 to equal $2, instead got $1.
exit 1
else
2022-09-03 00:41:54 -07:00
log_ok
echo $3 correctly equals $2
2022-09-02 13:48:12 -07:00
fi
}
2022-09-03 00:41:54 -07:00
# 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"
)
}
2022-09-02 13:48:12 -07:00
# Get current block time from chain.
function get_block_time {
return \
$(
curl http://0.0.0.0:1317/cosmos/base/tendermint/v1beta1/blocks/latest \
2022-09-03 00:41:54 -07:00
| jq -M ".blocks.header.height"
2022-09-02 13:48:12 -07:00
)
}
function get_code {
read out
return $out | grep code: | awk -F ': ' '{print $2}'
}
2022-09-03 00:41:54 -07:00
function get_key {
local out=$1
local filter=$2
2022-09-02 13:48:12 -07:00
echo $out | jq -M $filter
}
2022-09-03 00:41:54 -07:00
function now {
local op=$1
local amount=$2
if [[ op = "" ]]; then
date +%s
else
echo "$(date +%s) $op $amount" | bc
fi
}
2022-09-02 13:48:12 -07:00
function expect_success {
2022-09-03 00:41:54 -07:00
read out
local msg=${1:-Expect transaction to succeed}
2022-09-02 13:48:12 -07:00
# if we want to pipe:
# read out
2022-09-03 00:41:54 -07:00
# 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")
2022-09-02 13:48:12 -07:00
if [[ $res = 0 ]]; then
log_ok
2022-09-03 00:41:54 -07:00
echo $msg
2022-09-02 13:48:12 -07:00
else
log_fail
2022-09-03 00:41:54 -07:00
echo $msg
2022-09-02 13:48:12 -07:00
echo $raw_log
exit 1
fi
}
function expect_fail {
2022-09-03 00:41:54 -07:00
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")
2022-09-02 13:48:12 -07:00
if [[ $res = 0 ]]; then
log_fail
2022-09-03 00:41:54 -07:00
echo $msg
2022-09-02 13:48:12 -07:00
exit 1
else
log_ok
2022-09-03 00:41:54 -07:00
echo $msg
2022-09-02 13:48:12 -07:00
fi
}
function log_test {
printf "$(fmt_cyan [TEST])\t$1\n"
}
function log_ok {
printf "$(fmt_green [OK])\t"
}
function log_fail {
printf "$(fmt_red [FAIL])\t"
}
function fmt_red {
printf "\e[31m$1\e[0m"
}
function fmt_green {
printf "\e[32m$1\e[0m"
}
function fmt_cyan {
printf "\e[96m$1\e[0m"
}