REST API test framework and tests. (#2492)

* REST API test framework and tests.

* Fix ValidatorIndex tests to properly handle int32, but not uint32 values.

* Fix tests to follow latest REST fixes.

* refactor restapi.sh

and add it to the test suite

* Fix issues.
Add delay timeout which is required.

* Fix restapi.sh script for Windows.

Co-authored-by: Ștefan Talpalaru <stefantalpalaru@yahoo.com>
This commit is contained in:
Eugene Kabanov 2021-04-27 23:46:24 +03:00 committed by GitHub
parent 7dba1b37dd
commit 0c3302b826
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 4426 additions and 17 deletions

View File

@ -41,6 +41,7 @@ endif
TOOLS := \
nimbus_beacon_node \
deposit_contract \
resttest \
inspector \
logtrace \
nbench \
@ -268,6 +269,7 @@ test: | $(TEST_BINARIES)
ifeq ($(DISABLE_TEST_FIXTURES_SCRIPT), 0)
V=$(V) scripts/setup_official_tests.sh
endif
tests/simulation/restapi.sh
for TEST_BINARY in $(TEST_BINARIES); do \
PARAMS=""; \
if [[ "$${TEST_BINARY}" == "state_sim" ]]; then PARAMS="--validators=6000 --slots=128"; \

View File

@ -478,7 +478,7 @@ proc getCurrentHead*(node: BeaconNode,
proc getCurrentHead*(node: BeaconNode,
epoch: Epoch): Result[BlockRef, cstring] =
if epoch >= MaxEpoch:
if epoch > MaxEpoch:
return err("Requesting epoch for which slot would overflow")
node.getCurrentHead(compute_start_slot_at_epoch(epoch))

View File

@ -83,11 +83,7 @@ proc installValidatorApiHandlers*(router: var RestRouter, node: BeaconNode) =
block:
let res = node.getCurrentHead(qepoch)
if res.isErr():
if not(node.isSynced(node.chainDag.head)):
return RestApiResponse.jsonError(Http503, BeaconNodeInSyncError)
else:
return RestApiResponse.jsonError(Http400, NoHeadForSlotError,
$res.error())
return RestApiResponse.jsonError(Http503, BeaconNodeInSyncError)
res.get()
let droot =
if qepoch >= Epoch(2):
@ -148,11 +144,7 @@ proc installValidatorApiHandlers*(router: var RestRouter, node: BeaconNode) =
block:
let res = node.getCurrentHead(qepoch)
if res.isErr():
if not(node.isSynced(node.chainDag.head)):
return RestApiResponse.jsonError(Http503, BeaconNodeInSyncError)
else:
return RestApiResponse.jsonError(Http400, NoHeadForSlotError,
$res.error())
return RestApiResponse.jsonError(Http503, BeaconNodeInSyncError)
res.get()
let droot =
if qepoch >= Epoch(2):
@ -266,11 +258,7 @@ proc installValidatorApiHandlers*(router: var RestRouter, node: BeaconNode) =
block:
let res = node.getCurrentHead(qslot)
if res.isErr():
if not(node.isSynced(node.chainDag.head)):
return RestApiResponse.jsonError(Http503, BeaconNodeInSyncError)
else:
return RestApiResponse.jsonError(Http400, NoHeadForSlotError,
$res.error())
return RestApiResponse.jsonError(Http503, BeaconNodeInSyncError)
res.get()
let epochRef = node.chainDag.getEpochRef(qhead, qslot.epoch)
makeAttestationData(epochRef, qhead.atSlot(qslot), qindex)

2
env.sh
View File

@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env bash
# We use ${BASH_SOURCE[0]} instead of $0 to allow sourcing this file
# and we fall back to a Zsh-specific special var to also support Zsh.

3149
ncli/resttest-rules.json Normal file

File diff suppressed because it is too large Load Diff

1128
ncli/resttest.nim Normal file

File diff suppressed because it is too large Load Diff

142
tests/simulation/restapi.sh Executable file
View File

@ -0,0 +1,142 @@
#!/usr/bin/env bash
NUM_VALIDATORS=${VALIDATORS:-32}
TOTAL_NODES=${NODES:-1}
GIT_ROOT="$(git rev-parse --show-toplevel)"
TEST_DIR="${GIT_ROOT}/build/resttest_sim"
LOG_FILE="${TEST_DIR}/resttest_node.log"
VALIDATORS_DIR="${TEST_DIR}/validators"
SECRETS_DIR="${TEST_DIR}/secrets"
SNAPSHOT_FILE="${TEST_DIR}/state_snapshot.ssz"
NETWORK_BOOTSTRAP_FILE="${TEST_DIR}/bootstrap_hidden_nodes.txt"
RESTTEST_RULES="${GIT_ROOT}/ncli/resttest-rules.json"
DEPOSIT_CONTRACT_BIN="${GIT_ROOT}/build/deposit_contract"
BOOTSTRAP_ENR_FILE="${TEST_DIR}/beacon_node.enr"
NETWORK_METADATA_FILE="${TEST_DIR}/network.json"
DEPOSITS_FILE="${TEST_DIR}/deposits.json"
REST_ADDRESS="127.0.0.1"
REST_PORT="5052"
MKDIR_SCRIPT="${GIT_ROOT}/scripts/makedir.sh"
$MKDIR_SCRIPT "${TEST_DIR}"
cd "${TEST_DIR}"
# Windows detection
if uname | grep -qiE "mingw|msys"; then
MAKE="mingw32-make"
else
MAKE="make"
fi
# number of CPU cores
if uname | grep -qi darwin; then
NPROC="$(sysctl -n hw.logicalcpu)"
else
NPROC="$(nproc)"
fi
build_if_missing () {
if [[ ! -e "${GIT_ROOT}/build/${1}" ]]; then
${MAKE} -C "${GIT_ROOT}" -j ${NPROC} ${1}
fi
}
EXISTING_VALIDATORS=0
if [[ -f "${DEPOSITS_FILE}" ]]; then
# We count the number of deposits by counting the number of
# occurrences of the 'deposit_data_root' field:
EXISTING_VALIDATORS=$(grep -o -i deposit_data_root "${DEPOSITS_FILE}" | wc -l)
fi
if [[ ${EXISTING_VALIDATORS} -ne ${NUM_VALIDATORS} ]]; then
build_if_missing deposit_contract
rm -rf "${VALIDATORS_DIR}" "${SECRETS_DIR}"
../deposit_contract generateSimulationDeposits \
--count="${NUM_VALIDATORS}" \
--out-validators-dir="${VALIDATORS_DIR}" \
--out-secrets-dir="${SECRETS_DIR}" \
--out-deposits-file="${DEPOSITS_FILE}"
echo "All deposits prepared"
fi
build_if_missing nimbus_beacon_node
build_if_missing resttest
if [[ ! -f "${SNAPSHOT_FILE}" ]]; then
echo "Creating testnet genesis..."
../nimbus_beacon_node \
--data-dir="${TEST_DIR}" \
createTestnet \
--deposits-file="${DEPOSITS_FILE}" \
--total-validators="${NUM_VALIDATORS}" \
--output-genesis="${SNAPSHOT_FILE}" \
--output-bootstrap-file="${NETWORK_BOOTSTRAP_FILE}" \
--netkey-file=network_key.json \
--insecure-netkey-password=true \
--genesis-offset=0 # Delay in seconds
fi
DEPOSIT_CONTRACT_ADDRESS="0x0000000000000000000000000000000000000000"
DEPOSIT_CONTRACT_BLOCK="0x0000000000000000000000000000000000000000000000000000000000000000"
echo "Writing ${NETWORK_METADATA_FILE}:"
tee "${NETWORK_METADATA_FILE}" <<EOF
{
"runtimePreset": {
"MIN_GENESIS_ACTIVE_VALIDATOR_COUNT": ${NUM_VALIDATORS},
"MIN_GENESIS_TIME": 0,
"GENESIS_DELAY": 0,
"GENESIS_FORK_VERSION": "0x00000000",
"ETH1_FOLLOW_DISTANCE": 1,
},
"depositContractAddress": "${DEPOSIT_CONTRACT_ADDRESS}",
"depositContractDeployedAt": "${DEPOSIT_CONTRACT_BLOCK}"
}
EOF
SNAPSHOT_ARG=""
if [[ -f "${SNAPSHOT_FILE}" ]]; then
SNAPSHOT_ARG="--finalized-checkpoint-state=${SNAPSHOT_FILE}"
fi
../nimbus_beacon_node \
--log-level=${LOG_LEVEL:-DEBUG} \
--network="${NETWORK_METADATA_FILE}" \
--data-dir="${TEST_DIR}" \
--secrets-dir="${SECRETS_DIR}" \
${SNAPSHOT_ARG} \
--doppelganger-detection=off \
--nat=none \
--rest=true \
--rest-address=${REST_ADDRESS} \
--rest-port= ${REST_PORT} \
${ADDITIONAL_BEACON_NODE_ARGS} \
"$@" > ${LOG_FILE} 2>&1 &
BEACON_NODE_STATUS=$?
if [[ ${BEACON_NODE_STATUS} -eq 0 ]]; then
echo "nimbus_beacon_node has been successfully started"
BEACON_NODE_PID="$(jobs -p)"
../resttest \
--delay=30 \
--timeout=60 \
--skip-topic=slow \
--connections=4 \
--rules-file="${RESTTEST_RULES}" \
http://${REST_ADDRESS}:${REST_PORT}/api
RESTTEST_STATUS=$?
kill -SIGINT ${BEACON_NODE_PID}
if [[ ${RESTTEST_STATUS} -eq 0 ]]; then
echo "All tests were completed successfully!"
else
echo "Some of the tests failed!"
tail -n 100 ${LOG_FILE}
exit 1
fi
else
echo "nimbus_beacon_node failed to start"
fi