/
runghostinspectorsuite
executable file
·131 lines (106 loc) · 3.77 KB
/
runghostinspectorsuite
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/bin/bash
# Flag bash to fail the script if it encounters an error exit code
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}
: ${APP_WAIT_TIMEOUT:=}
# ability to provide extra params via JSON
: ${GI_PARAMS_JSON:='{}'}
# ability to use screenshot to pass/fail test
: ${GI_PASSING_STATUS_KEY:='passing'}
# set TERM if not present, ngrok will fail with missing TERM
: ${TERM:=xterm}
# prepend hostname to APP_PORT if not specified
if [ "$APP_PORT" -ge 0 ] 2>/dev/null; then
APP_PORT="localhost:$APP_PORT"
fi
# 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"
echo "PASSING_STATUS_KEY: $GI_PASSING_STATUS_KEY"
# check for additional ENV parameters in the environment
set +e
GI_ENV_PARAMS=$(env | grep "^GI_PARAM_.*")
set -e
GI_EXTRA_PARAMS=''
# parse the extra ENV parameters into usable format
for param in $GI_ENV_PARAMS; do
GI_PARAM=$(echo $param | sed 's/GI_PARAM_//g')
echo "Extra ENV parameter: $GI_PARAM"
VARNAME=$(echo $GI_PARAM | cut -d'=' -f1)
VALUE=$(echo $GI_PARAM | cut -d'=' -f2)
GI_PARAMS_JSON=$(echo "$GI_PARAMS_JSON" | jq --arg value "$VALUE" '. + {'$VARNAME': $value}')
done
# pass the status key in as a param
GI_PARAMS_JSON=$(echo "$GI_PARAMS_JSON" | jq --arg value "$GI_PASSING_STATUS_KEY" '. + {passingStatusKey: $value}')
# 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"
# Inject the start url
GI_PARAMS_JSON=$(echo "$GI_PARAMS_JSON" | jq --arg startUrl "$START_URL" '. + {startUrl: $startUrl}')
echo "Using JSON params: $GI_PARAMS_JSON"
# Wait for application startup, if specified
if [ -n "$APP_WAIT_TIMEOUT" ]; then
/usr/bin/wait-for-it $APP_PORT -t $APP_WAIT_TIMEOUT
echo "$APP_PORT is up, continuing..."
fi
# Execute the suite
set +e
OUTPUT=$(GI_API_KEY=$GI_API_KEY & /bin/run-suite.js $GI_SUITE "$GI_PARAMS_JSON")
PASSING=$?
# echo $OUTPUT
echo "Passing: $PASSING"
if [ $PASSING -eq 0 ]; then
echo "Suite passed! \o/"
else
echo "Suite failed! ¯\_(ツ)_/¯"
fi
# clean up our processes
if [ "$SERVER" != "" ]; then
kill -9 $APP_PID
fi
kill -9 $NGROK_PID
# return our passing status
exit $PASSING