Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 128 lines (107 sloc) 3.9 KB
#!/bin/bash
set -e
: "${NGROK_TOKEN:?NGROK_TOKEN env var is required}"
: "${GI_SUITE:?GI_SUITE env var is required}"
: "${GI_API_KEY:?GI_API_KEY env var is required}"
: "${APP_PORT:?APP_PORT env var is required}"
: ${STARTUP_DELAY:=3}
# ability to provide extra params via JSON
: ${GI_PARAMS_JSON:=''}
# set TERM if not present, ngrok will fail with missing TERM
: ${TERM:=xterm}
# obscure sensitive tokens from output
PRINT_NGROK_TOKEN="$(echo $NGROK_TOKEN | head -c4)****************"
PRINT_GI_API_KEY="$(echo $GI_API_KEY | head -c4)****************"
echo "NGROK_TOKEN: $PRINT_NGROK_TOKEN"
echo "GI_API_KEY: $PRINT_GI_API_KEY"
echo "GI_SUITE: $GI_SUITE"
echo "APP_PORT: $APP_PORT"
# check for additional URL parameters in the environment
set +e
GI_ENV_PARAMS=$(env | grep "^GI_PARAM_.*")
set -e
GI_EXTRA_PARAMS=''
# parse the extra URL parameters into usable format
for param in $GI_ENV_PARAMS; do
GI_PARAM=$(echo $param | sed 's/GI_PARAM_//g')
echo "Extra API parameter: $GI_PARAM"
GI_EXTRA_PARAMS="&$GI_PARAM$GI_EXTRA_PARAMS"
done
# server command will be all script params after $0
SERVER="${@:1}"
# check if we need to start the application
if [ "$SERVER" != "" ]; then
echo "Running application with args: $SERVER"
node $SERVER &
APP_PID=$!
echo "Application started with PID: $APP_PID"
# give the application a second to warm up
echo "Waiting $STARTUP_DELAY seconds for application to start..."
sleep $STARTUP_DELAY
# double-check the app is running before we continue
if ! kill -0 $APP_PID > /dev/null 2>&1; then
echo "FATAL: Unable to signal application process ($SERVER), is it running?" >&2
echo "Exiting..."
exit 1
fi
fi
# Intialize ngrok and open tunnel to our application
ngrok authtoken $NGROK_TOKEN
ngrok http $APP_PORT &
NGROK_PID=$!
# give ngrok a second to register URLs
echo "Waiting $STARTUP_DELAY seconds for ngrok to register..."
sleep $STARTUP_DELAY
# double-check the ngrok is running before we continue
if ! kill -0 $NGROK_PID > /dev/null 2>&1; then
echo "FATAL: Unable to signal ngrok process, is the token valid?" >&2
echo "Exiting..."
exit 1
fi
# Grab the ngrok url to send to the API, coerce to HTTPS
START_URL=$(curl -s 'http://localhost:4040/api/tunnels' | jq -r '.tunnels[1].public_url' | sed 's/http:/https:/')
if [ "$START_URL" = "" ]; then
echo "ERROR: did not get a start URL from the ngrok daemon, exiting..."
exit 1
fi
echo "Using start URL: $START_URL"
# Set up a couple variables to monitor result state
STATUS='null'
SUITE_RESULT=
PASSING=
# Execute Ghost Inspector suite via API and grab the result ID
EXECUTE_URL="https://api.ghostinspector.com/v1/suites/$GI_SUITE/execute/?startUrl=$START_URL&immediate=1$GI_EXTRA_PARAMS"
if [ $GI_PARAMS_JSON != '' ]; then
echo "Executing suite (POST): $EXECUTE_URL&apiKey=$PRINT_GI_API_KEY ($GI_PARAMS_JSON)"
RESULT_ID=$(curl -s -XPOST -H'Content-Type: application/json' -d''$GI_PARAMS_JSON'' "$EXECUTE_URL&apiKey=$GI_API_KEY" | jq -r '.data._id')
else
echo "Executing suite (GET): $EXECUTE_URL&apiKey=$PRINT_GI_API_KEY"
RESULT_ID=$(curl -s$POST_OPTION "$EXECUTE_URL&apiKey=$GI_API_KEY" | jq -r '.data._id')
fi
# for the suite result, sleep for a few seconds if it hasn't changed
echo "Polling for suite results (ID: $RESULT_ID)"
while [ "$STATUS" = 'null' ]; do
sleep 5
SUITE_RESULT=$(curl -s "https://api.ghostinspector.com/v1/suite-results/$RESULT_ID/?apiKey=$GI_API_KEY")
STATUS=$(echo $SUITE_RESULT | jq -r '.data.passing')
if [ "$STATUS" = 'null' ]; then
echo " - status: running..."
else
echo " - status: done, passing: $STATUS"
fi
done
# status has been updated, check results for "passing"
if [ "$(echo $SUITE_RESULT | jq -r '.data.passing')" != 'true' ]; then
echo "Suite failed! ¯\_(ツ)_/¯"
PASSING=1
else
echo "Suite passed! \o/"
PASSING=0
fi
# clean up our processes
if [ "$SERVER" != "" ]; then
kill -9 $APP_PID
fi
kill -9 $NGROK_PID
# return our passing status
exit $PASSING