ligo/test/test_utils.sh

259 lines
8.0 KiB
Bash
Raw Normal View History

2017-08-09 18:09:41 +04:00
#!/usr/bin/env bash
2017-07-24 17:06:56 +04:00
2017-08-09 18:09:41 +04:00
# If the TZPATH environment variable is not set,
# assume that we're in the test directory
if [ -z "$TZPATH" ]; then
export TZPATH="../"
fi
2017-07-24 17:06:56 +04:00
2017-08-09 18:09:41 +04:00
# Global arrays for cleanup
2017-08-14 13:06:37 +04:00
if [ -z "${CLEANUP_DIRS}" ]; then
export CLEANUP_DIRS=()
2017-08-09 18:09:41 +04:00
fi
if [ -z "${CLEANUP_PROCESSES}" ]; then
export CLEANUP_PROCESSES=()
fi
2017-07-24 17:06:56 +04:00
cleanup() {
2017-08-09 18:09:41 +04:00
for ps in "${CLEANUP_PROCESSES[@]}"; do
kill -9 $ps
done
2017-08-14 13:06:37 +04:00
CLEANUP_PROCESSES=();
2017-08-09 18:09:41 +04:00
sleep 2
2017-08-14 13:06:37 +04:00
for node_dir in "${CLEANUP_DIRS[@]}"; do
printf "\nNode's log:\n" 1>&2
[ ! -f $node_dir/LOG ] || cat $node_dir/LOG 1>&2
2017-08-09 18:09:41 +04:00
rm -rf $node_dir
done
2017-08-14 13:06:37 +04:00
CLEANUP_DIRS=()
2017-08-09 18:09:41 +04:00
for client_dir in ${CLIENT_DIRS[@]}; do
2017-08-14 13:06:37 +04:00
rm -rf $client_dir;
2017-08-09 18:09:41 +04:00
done
2017-08-14 13:06:37 +04:00
CLIENT_DIRS=();
2017-07-24 17:06:56 +04:00
}
2017-08-09 18:09:41 +04:00
trap cleanup EXIT
2017-07-24 17:06:56 +04:00
2017-08-09 18:09:41 +04:00
register_dir() {
2017-08-14 13:06:37 +04:00
CLEANUP_DIRS+=("$1")
2017-08-09 18:09:41 +04:00
}
2017-07-24 17:06:56 +04:00
2017-08-09 18:09:41 +04:00
make_client () {
client_dir="$(mktemp -d -t tezos_client.XXXXXXXXXX)"
echo "${TZPATH}/tezos-client -base-dir ${client_dir}"
}
2017-08-14 13:06:37 +04:00
TZCLIENT_DIR="$(mktemp -d -t tezos_client.XXXXXXXXXX)"
register_dir "${TZCLIENT_DIR}"
export TZCLIENT="${TZPATH}/tezos-client -base-dir ${TZCLIENT_DIR}"
export TZNODE="${TZPATH}/tezos-node"
2017-07-24 17:06:56 +04:00
2017-08-09 18:09:41 +04:00
CUSTOM_PARAM="--sandbox=${TZPATH}/test/sandbox.json"
start_sandboxed_node() {
if [ "$#" == "0" ]; then
default_args=--rpc-addr=[::]:8732
else
default_args=""
fi
data_dir="$(mktemp -d -t tezos_node.XXXXXXXXXX)"
register_dir "$data_dir"
${TZNODE} identity generate 0 --data-dir "${data_dir}" 2>&1| sed 's/^/## /' 1>&2
${TZNODE} config init --data-dir="${data_dir}" --connections=2 --expected-pow=0.0 2>&1| sed 's/^/## /' 1>&2
2017-08-09 18:09:41 +04:00
${TZNODE} run --data-dir "${data_dir}" ${CUSTOM_PARAM} "$@" $default_args > "$data_dir"/LOG 2>&1 &
node_pid="$!"
CLEANUP_PROCESSES+=($node_pid)
2017-08-14 13:06:37 +04:00
export CLEANUP_PROCESSES
echo alias tezos-client=\"${TZCLIENT} \"\;
echo alias "tezos-sandbox-stop=\"kill -9 ${node_pid}; sleep 1; rm -rf ${data_dir} ${TZCLIENT_DIR};\""
echo "## Created node, pid: ${node_pid}, log: $data_dir/LOG" 1>&2
2017-08-09 18:09:41 +04:00
}
activate_alpha() {
addr=${1:-[::]}
port=${2:-8732}
${TZCLIENT} -port $port -addr $addr \
-block genesis \
activate \
protocol ProtoALphaALphaALphaALphaALphaALphaALphaALphaDdp3zK \
with fitness 1 \
and passes 1 \
2017-08-14 13:06:37 +04:00
and key edskRhxswacLW6jF6ULavDdzwqnKJVS4UcDTNiCyiH6H8ZNnn2pmNviL7pRNz9kRxxaWQFzEQEcZExGHKbwmuaAcoMegj5T99z \
> /dev/stderr
2017-08-09 18:09:41 +04:00
}
2017-07-24 17:06:56 +04:00
run_contract_file () {
2017-08-09 18:09:41 +04:00
local contract=$1;
local storage=$2;
local input=$3;
${TZCLIENT} run program "$contract" on storage "$storage" and input "$input";
2017-07-24 17:06:56 +04:00
}
assert_output () {
2017-08-09 18:09:41 +04:00
local contract=$1;
local input=$2;
local storage=$3;
local expected=$4;
echo "Testing [$contract]"
local output=$(run_contract_file "$contract" "$input" "$storage" | sed '1,/output/d' |
sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' ||
{ printf '\nTest failed with error at line %s\n' "$(caller)" > /dev/stderr;
exit 1; });
if [ "$expected" != "$output" ]; then
2017-08-16 16:26:45 +04:00
echo "Test at " `caller` failed > /dev/stderr;
2017-08-09 18:09:41 +04:00
printf "Expected %s but got %s" "$expected" "$output" > /dev/stderr;
exit 1;
fi
2017-07-24 17:06:56 +04:00
}
assert_balance () {
local KEY="$1"
local EXPECTED_BALANCE="$2"
local RESULT=$(${TZCLIENT} get balance for ${KEY})
if [ "${RESULT}" != "${EXPECTED_BALANCE}" ]; then
printf "Balance assertion failed for ${KEY} on line '%s'. Expected %s but got %s.\n" \
"$(caller)" "${EXPECTED_BALANCE}" "${RESULT}"
exit 2
fi
}
contract_name_of_file () {
basename ${FILE} ".tz"
}
init_contract_from_file () {
local FILE="$1"
local NAME=$(contract_name_of_file ${FILE})
${TZCLIENT} remember program "${NAME}" "file:${FILE}"
}
init_with_transfer () {
local FILE="$1"
local NAME=$(contract_name_of_file ${FILE})
local KEY="$2"
local INITIAL_STORAGE="$3"
local TRANSFER_AMT="$4"
local TRANSFER_SRC=${5-bootstrap1}
echo "Originating [$NAME]"
2017-07-24 17:06:56 +04:00
${TZCLIENT} originate contract ${NAME} \
for ${KEY} transferring "${TRANSFER_AMT}" \
from ${TRANSFER_SRC} running "${FILE}" -init "${INITIAL_STORAGE}"
}
# Takes a grep regexp and fails with an error message if command does not include
# the regexp
assert_in_output () {
local MATCHING="$1"
local INPUT=${2-/dev/stdin}
if ! grep -q "${MATCHING}" ${INPUT}; then
2017-08-09 18:09:41 +04:00
printf "\nFailure on line %s. Expected to find %s in output." \
2017-07-24 17:06:56 +04:00
"$(caller)" "${MATCHING}"
exit 1
2017-08-09 18:09:41 +04:00
else
echo "[Assertion succeeded]"
2017-07-24 17:06:56 +04:00
fi
}
get_contract_addr () {
local CONTRACT_NAME="$1"
${TZCLIENT} show known contract "${CONTRACT_NAME}"
}
contract_storage () {
local CONTRACT_NAME="$1" # Can be either an alias or hash
${TZCLIENT} get storage for ${CONTRACT_NAME}
}
assert_storage_contains () {
local CONTRACT_NAME="$1"
local EXPECTED_STORAGE="$2"
contract_storage ${CONTRACT_NAME} | assert_in_output ${EXPECTED_STORAGE}
}
assert() {
local expected="$1"
local result="$(cat)"
if [ "${result}" != "${expected}" ]; then
echo "Unexpected result: \"${result}\""
echo "Expected: \"${expected}\""
exit 2
fi
}
2017-08-16 16:26:45 +04:00
assert_fails() {
printf "[Asserting failure]\n"
if "$@" 2> /dev/null; then
printf "Expected command line to fail, but succeeded:\n"
echo "$@"
exit 1
else
return 0
fi
}
2017-07-24 17:06:56 +04:00
BOOTSTRAP1_IDENTITY=tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx
BOOTSTRAP1_PUBLIC=edpkuBknW28nW72KG6RoHtYW7p12T6GKc7nAbwYX5m8Wd9sDVC9yav
BOOTSTRAP1_SECRET=edskRuR1azSfboG86YPTyxrQgosh5zChf5bVDmptqLTb5EuXAm9rsnDYfTKhq7rDQujdn5WWzwUMeV3agaZ6J2vPQT58jJAJPi
BOOTSTRAP2_IDENTITY=tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN
2017-08-09 18:09:41 +04:00
BOOTSTRAP2_PUBLIC=edpktzNbDAUjUk697W7gYg2CRuBQjyPxbEg8dLccYYwKSKvkPvjtV9
BOOTSTRAP2_SECRET=edskRkJz4Rw2rM5NtabEWMbbg2bF4b1nfFajaqEuEk4SgU7eeDbym9gVQtBTbYo32WUg2zb5sNBkD1whRN7zX43V9bftBbtaKc
2017-07-24 17:06:56 +04:00
BOOTSTRAP3_IDENTITY=tz1faswCTDciRzE4oJ9jn2Vm2dvjeyA9fUzU
2017-08-09 18:09:41 +04:00
BOOTSTRAP3_PUBLIC=edpkuTXkJDGcFd5nh6VvMz8phXxU3Bi7h6hqgywNFi1vZTfQNnS1RV
BOOTSTRAP3_SECRET=edskS3qsqsNgdjUqeMsVcEwBn8dkZ5iDRz6aF21KhcCtRiAkWBypUSbicccR4Vgqm9UdW2Vabuos6seezqgbXTrmcbLUG4rdAC
2017-07-24 17:06:56 +04:00
BOOTSTRAP4_IDENTITY=tz1b7tUupMgCNw2cCLpKTkSD1NZzB5TkP2sv
2017-08-09 18:09:41 +04:00
BOOTSTRAP4_PUBLIC=edpkuFrRoDSEbJYgxRtLx2ps82UdaYc1WwfS9sE11yhauZt5DgCHbU
BOOTSTRAP4_SECRET=edskRg9qcPqaVQa6jXWNMU5p71tseSuR7NzozgqZ9URsVDi81wTyPJdFSBdeakobyHUi4Xgu61jgKRQvkhXrPmEdEUfiqfiJFL
2017-07-24 17:06:56 +04:00
BOOTSTRAP5_IDENTITY=tz1ddb9NMYHZi5UzPdzTZMYQQZoMub195zgv
2017-08-09 18:09:41 +04:00
BOOTSTRAP5_PUBLIC=edpkv8EUUH68jmo3f7Um5PezmfGrRF24gnfLpH3sVNwJnV5bVCxL2n
BOOTSTRAP5_SECRET=edskS7rLN2Df3nbS1EYvwJbWo4umD7yPM1SUeX7gp1WhCVpMFXjcCyM58xs6xsnTsVqHQmJQ2RxoAjJGedWfvFmjQy6etA3dgZ
2017-07-24 17:06:56 +04:00
KEY1=foo
KEY2=bar
2017-08-09 18:09:41 +04:00
add_bootstrap_identities() {
client=${1:-${TZCLIENT}}
2017-08-16 00:24:40 +04:00
# ${client} add identity bootstrap1 ${BOOTSTRAP1_IDENTITY}
2017-08-09 18:09:41 +04:00
${client} add public key bootstrap1 ${BOOTSTRAP1_PUBLIC}
${client} add secret key bootstrap1 ${BOOTSTRAP1_SECRET}
2017-08-16 00:24:40 +04:00
# ${client} add identity bootstrap2 ${BOOTSTRAP2_IDENTITY}
2017-08-09 18:09:41 +04:00
${client} add public key bootstrap2 ${BOOTSTRAP2_PUBLIC}
${client} add secret key bootstrap2 ${BOOTSTRAP2_SECRET}
2017-08-16 00:24:40 +04:00
# ${client} add identity bootstrap3 ${BOOTSTRAP3_IDENTITY}
2017-08-09 18:09:41 +04:00
${client} add public key bootstrap3 ${BOOTSTRAP3_PUBLIC}
${client} add secret key bootstrap3 ${BOOTSTRAP3_SECRET}
2017-07-24 17:06:56 +04:00
2017-08-16 00:24:40 +04:00
# ${client} add identity bootstrap4 ${BOOTSTRAP4_IDENTITY}
2017-08-09 18:09:41 +04:00
${client} add public key bootstrap4 ${BOOTSTRAP4_PUBLIC}
${client} add secret key bootstrap4 ${BOOTSTRAP4_SECRET}
2017-07-24 17:06:56 +04:00
2017-08-16 00:24:40 +04:00
# ${client} add identity bootstrap5 ${BOOTSTRAP5_IDENTITY}
2017-08-09 18:09:41 +04:00
${client} add public key bootstrap5 ${BOOTSTRAP5_PUBLIC}
${client} add secret key bootstrap5 ${BOOTSTRAP5_SECRET}
2017-07-24 17:06:56 +04:00
2017-08-09 18:09:41 +04:00
sleep 2
${client} gen keys ${KEY1}
${client} gen keys ${KEY2}
}
extract_operation_hash() {
grep "Operation hash is" | grep -o "'.*'" | tr -d "'"
}
2017-08-14 13:06:37 +04:00
display_aliases() {
echo <<EOF
alias tezos-client="${TZCLIENT} "\;
alias tezos-sandbox-stop="kill -9 ${node_pid}; sleep 1; rm -rf ${CLEANUP_DIRS[@]}; unalias tezos-client tezos-sandbox-stop"
EOF
}