-
Notifications
You must be signed in to change notification settings - Fork 37
/
local-api-tests.sh
executable file
·150 lines (128 loc) · 3.56 KB
/
local-api-tests.sh
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/bin/bash
set -e
# Use a custom port here to not haphazardly interfer with a running portal-api local
# instance. Which would not be good. Been there, done that (messes up your entire local
# environment).
apiPort=3301
if nc -z localhost ${apiPort}; then
echo "*** ERROR: Port ${apiPort} is already open."
exit 1
fi
echoPort=3309
if nc -z localhost ${echoPort}; then
echo "*** ERROR: Port ${echoPost} is already open (echo server)."
exit 1
fi
tmpDir=test-$(date +%Y%m%d%H%M%S)
mkdir -p ./tmp/$tmpDir
baseDir=$(pwd)/tmp/$tmpDir
thisDir=$(pwd)
echo "Test dir: $baseDir"
trap traperror ERR
apiPid=""
pgContainer=""
function killthings() {
if [ ! -z "$apiPid" ]; then
echo "===> Killing API"
if ! kill $apiPid; then
echo "Apparently the API was already dead."
fi
fi
if [ ! -z "$pgContainer" ]; then
echo "===> Killing Postgres container"
docker rm -f $pgContainer
fi
}
function traperror() {
echo "*********************************"
echo "Oh sh... killing all the things"
echo "*********************************"
killthings
exit 1
}
function waitFor() {
x=$3
while ! nc -z "$1" "$2";
do
sleep 1
echo "Waiting for ${1}:${2} for ${x} times"
x=$((x-1))
if [[ x -le 0 ]];
then
return 1 ## return failure
fi
done
return 0 ## return success
}
cp -r ./portal-api/test/test-config/static ./tmp/$tmpDir/static
mkdir -p ./tmp/$tmpDir/dynamic
export PORTAL_API_HOOK_INTERVAL=250
export PORTAL_API_AESKEY=ThisIsASecretSauceKeyWhichDoesNotMatterForTheUnitTests
export PORTAL_CONFIG_KEY=ThisIsUsedInDeploy
export DEBUG=portal-api:*,portal:*,kong-adapter:*,portal-env:*
export ALLOW_KILL=true
export ALLOW_RESYNC=true
export PORTAL_CONFIG_BASE=$baseDir
export PORTAL_API_STATIC_CONFIG=$baseDir/static
export PORTAL_API_DYNAMIC_CONFIG=$baseDir/dynamic
export NODE_ENV=test
export LOG_LEVEL=debug
# Do not log in JSON
export LOG_PLAIN=true
export SWAGGER_RESOURCE_URL=http://localhost:8080
export PORTAL_API_URL=http://localhost:${apiPort}
export PORTAL_PORTAL_URL=http://localhost:3000
export PORTAL_KONG_ADAPTER_URL=http://localhost:3002
export PORTAL_KONG_ADMIN_URL=http://localhost:8001
export PORTAL_MAILER_URL=http://localhost:3003
export PORTAL_CHATBOT_URL=http://localhost:3004
export ECHO_PORT=${echoPort}
export HOOK_PORT=3111
export HOOK_HOST=localhost
mode=""
grepFilter=""
while [[ -n "$1" ]]; do
case "$1" in
"--json")
mode="json"
;;
"--postgres")
mode="postgres"
;;
"--grep")
shift 1
grepFilter="$1"
echo "Filtering test cases for '${grepFilter}'"
;;
esac
shift 1
done
if [[ -z "${mode}" ]]; then
echo "Usage: $0 <--json|--postgres> [--grep <filter>]"
exit 1
fi
if [[ $mode == json ]]; then
echo "=== JSON mode"
export WICKED_STORAGE=json
else
echo "=== Postgres mode"
docker run -d --name $tmpDir -p 6543:5432 -e POSTGRES_USER=kong -e POSTGRES_PASSWORD=kong postgres:11-alpine
pgContainer=$tmpDir
# portal-api will wait for itself until Postgres is available,
# no need to do that from bash. We'll just give it a couple of seconds
# to gather itself.
export WICKED_STORAGE=postgres
fi
pushd ../api
PORT=${apiPort} node bin/api &> ${thisDir}/logs/api-test-local.log &
apiPid=$!
popd
pushd portal-api
node node_modules/portal-env/await.js http://localhost:${apiPort}/ping
if [[ -z "$grepFilter" ]]; then
mocha
else
mocha --grep "${grepFilter}"
fi
popd
killthings