2019-02-27 18:12:47 +03:00
|
|
|
#! /usr/bin/env bash
|
|
|
|
|
2020-02-13 12:14:02 +03:00
|
|
|
# If no arguments are provided to this script, all the server upgrade tests will be run
|
|
|
|
# With arguments, you can specify which server upgrade pytests should be run
|
|
|
|
# Any options provided to this script will be applied to the
|
|
|
|
# pytest command collecting server upgrade tests
|
|
|
|
|
2019-02-27 18:12:47 +03:00
|
|
|
set -euo pipefail
|
|
|
|
|
|
|
|
# # keep track of the last executed command
|
|
|
|
# trap 'last_command=$current_command; current_command=$BASH_COMMAND' DEBUG
|
|
|
|
# # echo an error message before exiting
|
|
|
|
# trap 'echo "\"${last_command}\" command filed with exit code $?."' EXIT
|
|
|
|
|
|
|
|
ROOT="${BASH_SOURCE[0]%/*}"
|
|
|
|
|
2020-02-13 12:14:02 +03:00
|
|
|
download_with_etag_check() {
|
|
|
|
URL="$1"
|
|
|
|
FILE="$2"
|
|
|
|
ETAG="$(curl -I $URL | grep etag: | awk '{print $2}' | sed 's/\r$//')"
|
|
|
|
set -x
|
|
|
|
if ! ( [ -f "$FILE" ] && [ "$(cat "$FILE.etag" 2>/dev/null)" == "$ETAG" ] ) ; then
|
|
|
|
curl -Lo "$FILE" "$URL"
|
|
|
|
chmod +x "$FILE"
|
|
|
|
echo -e -n "$ETAG" > "$FILE.etag"
|
|
|
|
fi
|
|
|
|
set +x
|
|
|
|
}
|
|
|
|
|
|
|
|
fail_if_port_busy() {
|
|
|
|
local PORT=$1
|
|
|
|
if nc -z localhost $PORT ; then
|
|
|
|
echo "Port $PORT is busy. Exiting"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
# wait_for_port PORT [PID] [LOG_FILE]
|
2019-02-27 18:12:47 +03:00
|
|
|
wait_for_port() {
|
|
|
|
local PORT=$1
|
2020-02-13 12:14:02 +03:00
|
|
|
local PIDMSG=""
|
|
|
|
local PID=${2:-}
|
|
|
|
if [ -n "$PID" ] ; then
|
|
|
|
PIDMSG=", PID ($PID)"
|
|
|
|
fi
|
|
|
|
echo "waiting for ${PORT}${PIDMSG}"
|
2019-02-27 18:12:47 +03:00
|
|
|
for i in `seq 1 60`;
|
|
|
|
do
|
|
|
|
nc -z localhost $PORT && echo "port $PORT is ready" && return
|
|
|
|
echo -n .
|
|
|
|
sleep 1
|
2020-02-13 12:14:02 +03:00
|
|
|
if [ -n "$PID" ] && ! ps $PID >/dev/null ; then
|
|
|
|
echo "Process $PID has exited"
|
|
|
|
if [ -n "${3:-}" ] ; then
|
|
|
|
cat $3
|
|
|
|
fi
|
|
|
|
exit 1
|
|
|
|
fi
|
2019-02-27 18:12:47 +03:00
|
|
|
done
|
|
|
|
echo "Failed waiting for $PORT" && exit 1
|
|
|
|
}
|
|
|
|
|
2020-02-13 12:14:02 +03:00
|
|
|
log() { echo $'\e[1;33m'"--> $*"$'\e[0m'; }
|
|
|
|
|
2019-02-27 18:12:47 +03:00
|
|
|
|
|
|
|
: ${HASURA_GRAPHQL_SERVER_PORT:=8080}
|
|
|
|
: ${API_SERVER_PORT:=3000}
|
|
|
|
: ${HASURA_PROJECT_DIR:=$ROOT/hasura}
|
|
|
|
: ${API_SERVER_DIR:=$ROOT/api-server}
|
|
|
|
: ${SERVER_OUTPUT_DIR:=/build/_server_output}
|
|
|
|
: ${SERVER_BINARY:=/build/_server_output/graphql-engine}
|
|
|
|
: ${LATEST_SERVER_BINARY:=/bin/graphql-engine-latest}
|
2020-02-13 12:14:02 +03:00
|
|
|
: ${HASURA_GRAPHQL_STRINGIFY_NUMERIC_TYPES:=true}
|
2019-02-27 18:12:47 +03:00
|
|
|
|
|
|
|
LATEST_SERVER_LOG=$SERVER_OUTPUT_DIR/upgrade-test-latest-release-server.log
|
|
|
|
CURRENT_SERVER_LOG=$SERVER_OUTPUT_DIR/upgrade-test-current-server.log
|
|
|
|
|
|
|
|
HGE_ENDPOINT=http://localhost:$HASURA_GRAPHQL_SERVER_PORT
|
2020-02-13 12:14:02 +03:00
|
|
|
PYTEST_DIR="${ROOT}/../../server/tests-py"
|
2019-02-27 18:12:47 +03:00
|
|
|
|
2020-02-13 12:14:02 +03:00
|
|
|
pip3 -q install -r "${PYTEST_DIR}/requirements.txt"
|
2019-02-27 18:12:47 +03:00
|
|
|
|
|
|
|
# export them so that GraphQL Engine can use it
|
2020-02-13 12:14:02 +03:00
|
|
|
export HASURA_GRAPHQL_STRINGIFY_NUMERIC_TYPES="$HASURA_GRAPHQL_STRINGIFY_NUMERIC_TYPES"
|
|
|
|
# Required for testing caching
|
|
|
|
export GHCRTS='-N1'
|
|
|
|
# Required for event trigger tests
|
|
|
|
export WEBHOOK_FROM_ENV="http://127.0.0.1:5592"
|
|
|
|
export EVENT_WEBHOOK_HEADER="MyEnvValue"
|
|
|
|
|
|
|
|
# graphql-engine will be run on this port
|
|
|
|
fail_if_port_busy ${HASURA_GRAPHQL_SERVER_PORT}
|
|
|
|
|
|
|
|
# Remote graphql server of pytests run on this port
|
|
|
|
fail_if_port_busy 5000
|
2019-02-27 18:12:47 +03:00
|
|
|
|
|
|
|
log "setting up directories"
|
|
|
|
mkdir -p $SERVER_OUTPUT_DIR
|
|
|
|
touch $LATEST_SERVER_LOG
|
|
|
|
touch $CURRENT_SERVER_LOG
|
|
|
|
|
|
|
|
# download latest graphql engine release
|
|
|
|
log "downloading latest release of graphql engine"
|
2020-02-13 12:14:02 +03:00
|
|
|
download_with_etag_check 'https://graphql-engine-cdn.hasura.io/server/latest/linux-amd64' "$LATEST_SERVER_BINARY"
|
2019-02-27 18:12:47 +03:00
|
|
|
|
2020-02-13 12:14:02 +03:00
|
|
|
cur_server_version(){
|
|
|
|
echo "$(curl http://localhost:${HASURA_GRAPHQL_SERVER_PORT}/v1/version -q 2>/dev/null)"
|
|
|
|
}
|
2019-02-27 18:12:47 +03:00
|
|
|
|
2020-02-13 12:14:02 +03:00
|
|
|
log "Run pytests with server upgrade"
|
2019-02-27 18:12:47 +03:00
|
|
|
|
2020-02-13 12:14:02 +03:00
|
|
|
WORKTREE_DIR="$(mktemp -d)"
|
2020-04-10 03:57:59 +03:00
|
|
|
RELEASE_PYTEST_DIR="${WORKTREE_DIR}/server/tests-py"
|
|
|
|
RELEASE_VERSION="$( $LATEST_SERVER_BINARY version | cut -d':' -f2 | awk '{print $1}' )"
|
2020-02-13 12:14:02 +03:00
|
|
|
rm_worktree(){
|
|
|
|
rm -rf "$WORKTREE_DIR"
|
|
|
|
}
|
|
|
|
trap rm_worktree ERR
|
2019-02-27 18:12:47 +03:00
|
|
|
|
2020-02-13 12:14:02 +03:00
|
|
|
make_latest_release_worktree() {
|
2020-04-10 03:57:59 +03:00
|
|
|
git worktree add --detach "$WORKTREE_DIR" "$RELEASE_VERSION"
|
2020-02-13 12:14:02 +03:00
|
|
|
}
|
2019-02-27 18:12:47 +03:00
|
|
|
|
2020-04-10 03:57:59 +03:00
|
|
|
cleanup_hasura_metadata_if_present() {
|
|
|
|
set -x
|
|
|
|
psql "$HASURA_GRAPHQL_DATABASE_URL" -c 'drop schema if exists hdb_catalog cascade;
|
|
|
|
drop schema if exists hdb_views cascade' >/dev/null 2>/dev/null
|
|
|
|
set +x
|
2020-02-13 12:14:02 +03:00
|
|
|
}
|
2019-02-27 18:12:47 +03:00
|
|
|
|
2020-04-10 03:57:59 +03:00
|
|
|
get_tables_of_interest() {
|
|
|
|
psql $HASURA_GRAPHQL_DATABASE_URL -P pager=off -c "
|
|
|
|
select table_schema as schema, table_name as name
|
|
|
|
from information_schema.tables
|
|
|
|
where table_schema not in ('hdb_catalog','hdb_views', 'pg_catalog', 'information_schema','topology', 'tiger')
|
|
|
|
and (table_schema <> 'public'
|
|
|
|
or table_name not in ('geography_columns','geometry_columns','spatial_ref_sys','raster_columns','raster_overviews')
|
|
|
|
);
|
|
|
|
"
|
|
|
|
}
|
|
|
|
|
|
|
|
get_current_catalog_version() {
|
|
|
|
psql $HASURA_GRAPHQL_DATABASE_URL -P pager=off -c "SELECT version FROM hdb_catalog.hdb_version"
|
|
|
|
}
|
2019-02-27 18:12:47 +03:00
|
|
|
|
2020-02-13 12:14:02 +03:00
|
|
|
args=("$@")
|
|
|
|
get_server_upgrade_tests() {
|
2020-04-10 03:57:59 +03:00
|
|
|
cd $RELEASE_PYTEST_DIR
|
2020-02-13 12:14:02 +03:00
|
|
|
tmpfile="$(mktemp --dry-run)"
|
|
|
|
set -x
|
|
|
|
python3 -m pytest -q --collect-only --collect-upgrade-tests-to-file "$tmpfile" -m 'allow_server_upgrade_test and not skip_server_upgrade_test' "${args[@]}" 1>/dev/null 2>/dev/null
|
|
|
|
set +x
|
|
|
|
cat "$tmpfile"
|
|
|
|
cd - >/dev/null
|
|
|
|
rm "$tmpfile"
|
|
|
|
}
|
2019-02-27 18:12:47 +03:00
|
|
|
|
2020-02-13 12:14:02 +03:00
|
|
|
run_server_upgrade_pytest() {
|
2020-04-10 03:57:59 +03:00
|
|
|
HGE_PID=""
|
|
|
|
cleanup_hge(){
|
|
|
|
kill $HGE_PID || true
|
|
|
|
wait $HGE_PID || true
|
|
|
|
# cleanup_hasura_metadata_if_present
|
|
|
|
rm_worktree
|
2020-02-13 12:14:02 +03:00
|
|
|
}
|
2020-04-10 03:57:59 +03:00
|
|
|
trap cleanup_hge ERR
|
|
|
|
local HGE_URL="http://localhost:${HASURA_GRAPHQL_SERVER_PORT}"
|
|
|
|
local tests_to_run="$1"
|
2020-02-13 12:14:02 +03:00
|
|
|
|
2020-04-10 03:57:59 +03:00
|
|
|
[ -n "$tests_to_run" ] || ( echo "Got no test as input" && false )
|
2020-02-13 12:14:02 +03:00
|
|
|
|
2020-04-10 03:57:59 +03:00
|
|
|
run_pytest(){
|
|
|
|
cd $RELEASE_PYTEST_DIR
|
|
|
|
set -x
|
2020-02-13 12:14:02 +03:00
|
|
|
|
2020-04-10 03:57:59 +03:00
|
|
|
# With --avoid-error-message-checks, we are only going to throw warnings if the error message has changed between releases
|
2020-04-29 17:56:16 +03:00
|
|
|
# FIX ME: Remove the deselect below after the next stable release
|
2020-04-10 03:57:59 +03:00
|
|
|
pytest --hge-urls "${HGE_URL}" --pg-urls "$HASURA_GRAPHQL_DATABASE_URL" \
|
|
|
|
--avoid-error-message-checks "$@" \
|
2020-04-29 17:56:16 +03:00
|
|
|
-m 'allow_server_upgrade_test and not skip_server_upgrade_test' \
|
|
|
|
--deselect test_graphql_mutations.py::TestGraphqlUpdateBasic::test_numerics_inc \
|
|
|
|
-v $tests_to_run
|
2020-04-10 03:57:59 +03:00
|
|
|
set +x
|
|
|
|
cd -
|
|
|
|
}
|
2020-02-13 12:14:02 +03:00
|
|
|
|
2020-04-10 03:57:59 +03:00
|
|
|
############## Tests for latest release GraphQL engine #########################
|
|
|
|
|
|
|
|
# Start the old (latest release) GraphQL Engine
|
2020-02-13 12:14:02 +03:00
|
|
|
log "starting latest graphql engine release"
|
|
|
|
$LATEST_SERVER_BINARY serve > $LATEST_SERVER_LOG 2>&1 &
|
|
|
|
HGE_PID=$!
|
2020-04-10 03:57:59 +03:00
|
|
|
|
2020-02-13 12:14:02 +03:00
|
|
|
|
|
|
|
# Wait for server start
|
|
|
|
wait_for_port $HASURA_GRAPHQL_SERVER_PORT $HGE_PID $LATEST_SERVER_LOG
|
|
|
|
|
2020-04-10 03:57:59 +03:00
|
|
|
log "Catalog version for $(cur_server_version)"
|
|
|
|
get_current_catalog_version
|
|
|
|
|
2020-02-13 12:14:02 +03:00
|
|
|
log "Run pytest for latest graphql-engine release $(cur_server_version) while skipping schema teardown"
|
2020-04-10 03:57:59 +03:00
|
|
|
run_pytest --skip-schema-teardown
|
2020-02-13 12:14:02 +03:00
|
|
|
|
|
|
|
log "kill the api server $(cur_server_version)"
|
|
|
|
kill $HGE_PID || true
|
|
|
|
wait $HGE_PID || true
|
|
|
|
|
|
|
|
log "the tables of interest in the database are: "
|
2020-04-10 03:57:59 +03:00
|
|
|
get_tables_of_interest
|
|
|
|
|
|
|
|
############## Tests for the current build GraphQL engine #########################
|
2020-02-13 12:14:02 +03:00
|
|
|
|
|
|
|
if [[ "$1" =~ "test_schema_stitching" ]] ; then
|
2020-04-10 03:57:59 +03:00
|
|
|
# In this case, Hasura metadata will have GraphQL servers defined as remote.
|
|
|
|
# We need to have remote GraphQL server running for the graphql-engine to avoid
|
|
|
|
# inconsistent metadata error
|
|
|
|
cd $RELEASE_PYTEST_DIR
|
2020-02-13 12:14:02 +03:00
|
|
|
python3 graphql_server.py & REMOTE_GQL_PID=$!
|
|
|
|
wait_for_port 5000
|
|
|
|
cd -
|
|
|
|
fi
|
|
|
|
|
|
|
|
log "start the current build"
|
2020-04-10 03:57:59 +03:00
|
|
|
set -x
|
|
|
|
rm -f graphql-engine.tix
|
2020-02-13 12:14:02 +03:00
|
|
|
$SERVER_BINARY serve > $CURRENT_SERVER_LOG 2>&1 &
|
|
|
|
HGE_PID=$!
|
2020-04-10 03:57:59 +03:00
|
|
|
set +x
|
2020-02-13 12:14:02 +03:00
|
|
|
|
|
|
|
# Wait for server start
|
|
|
|
wait_for_port $HASURA_GRAPHQL_SERVER_PORT $HGE_PID $CURRENT_SERVER_LOG
|
|
|
|
|
2020-04-10 03:57:59 +03:00
|
|
|
log "Catalog version for $(cur_server_version)"
|
|
|
|
get_current_catalog_version
|
|
|
|
|
2020-02-13 12:14:02 +03:00
|
|
|
if [[ "$1" =~ "test_schema_stitching" ]] ; then
|
|
|
|
kill $REMOTE_GQL_PID || true
|
|
|
|
wait $REMOTE_GQL_PID || true
|
|
|
|
fi
|
|
|
|
|
2020-04-10 03:57:59 +03:00
|
|
|
log "Run pytest for the current build $(cur_server_version) without modifying schema"
|
|
|
|
run_pytest --skip-schema-setup --skip-schema-teardown
|
|
|
|
|
|
|
|
log "kill the api server $(cur_server_version)"
|
|
|
|
kill $HGE_PID || true
|
|
|
|
wait $HGE_PID || true
|
|
|
|
|
|
|
|
|
|
|
|
#################### Downgrade to release version ##########################
|
|
|
|
|
|
|
|
log "Downgrade graphql-engine to $RELEASE_VERSION"
|
|
|
|
$SERVER_BINARY downgrade "--to-$RELEASE_VERSION"
|
|
|
|
|
|
|
|
|
|
|
|
############## Tests for latest release GraphQL engine once more after downgrade #########################
|
|
|
|
|
|
|
|
if [[ "$1" =~ "test_schema_stitching" ]] ; then
|
|
|
|
cd $RELEASE_PYTEST_DIR
|
|
|
|
python3 graphql_server.py & REMOTE_GQL_PID=$!
|
|
|
|
wait_for_port 5000
|
|
|
|
cd -
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Start the old (latest release) GraphQL Engine
|
|
|
|
log "starting latest graphql engine release"
|
|
|
|
$LATEST_SERVER_BINARY serve > $LATEST_SERVER_LOG 2>&1 &
|
|
|
|
HGE_PID=$!
|
|
|
|
|
|
|
|
# Wait for server start
|
|
|
|
wait_for_port $HASURA_GRAPHQL_SERVER_PORT $HGE_PID $LATEST_SERVER_LOG
|
|
|
|
|
|
|
|
log "Catalog version for $(cur_server_version)"
|
|
|
|
get_current_catalog_version
|
|
|
|
|
|
|
|
if [[ "$1" =~ "test_schema_stitching" ]] ; then
|
|
|
|
kill $REMOTE_GQL_PID || true
|
|
|
|
wait $REMOTE_GQL_PID || true
|
|
|
|
fi
|
|
|
|
|
|
|
|
log "Run pytest for latest graphql-engine release $(cur_server_version) (once more) while skipping schema setup"
|
|
|
|
run_pytest --skip-schema-setup
|
2020-02-13 12:14:02 +03:00
|
|
|
|
|
|
|
log "kill the api server $(cur_server_version)"
|
|
|
|
kill $HGE_PID || true
|
|
|
|
wait $HGE_PID || true
|
2020-04-10 03:57:59 +03:00
|
|
|
|
2020-02-13 12:14:02 +03:00
|
|
|
}
|
2019-02-27 18:12:47 +03:00
|
|
|
|
2020-02-13 12:14:02 +03:00
|
|
|
make_latest_release_worktree
|
2019-02-27 18:12:47 +03:00
|
|
|
|
2020-04-10 03:57:59 +03:00
|
|
|
cleanup_hasura_metadata_if_present
|
|
|
|
|
2020-02-13 12:14:02 +03:00
|
|
|
for pytest in $(get_server_upgrade_tests) ; do
|
|
|
|
log "Running pytest $pytest"
|
|
|
|
run_server_upgrade_pytest "$pytest"
|
|
|
|
done
|
2020-04-10 03:57:59 +03:00
|
|
|
|
|
|
|
cleanup_hasura_metadata_if_present
|
2019-02-27 18:12:47 +03:00
|
|
|
|
|
|
|
exit 0
|