forked from Kong/kong
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-upgrade-path.sh
More file actions
executable file
·200 lines (167 loc) · 6.69 KB
/
Copy pathtest-upgrade-path.sh
File metadata and controls
executable file
·200 lines (167 loc) · 6.69 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#!/bin/bash
# This script runs the database upgrade tests from the
# spec/05-migration directory. It uses docker compose to stand up a
# simple environment with postgres database server and
# two Kong nodes. One node contains the oldest supported version, the
# other has the current version of Kong. The testing is then done as
# described in https://docs.google.com/document/d/1Df-iq5tNyuPj1UNG7bkhecisJFPswOfFqlOS3V4wXSc/edit?usp=sharing
# Normally, the testing environment and the git worktree that is
# required by this script are removed when the tests have run. By
# setting the UPGRADE_ENV_PREFIX environment variable, the docker
# compose environment's prefix can be defined. The environment will
# then not be automatically cleaned, which is useful during test
# development as it greatly speeds up test runs.
# Optionally, the test to run can be specified as a command line
# option. If it is not specified, the script will determine the tests
# to run based on the migration steps that are performed during the
# database up migration from the base to the current version.
set -e
trap "echo exiting because of error" 0
function get_current_version() {
local image_tag=$1
local version_from_rockspec=$(perl -ne 'print "$1\n" if (/^\s*tag = "(.*)"/)' kong*.rockspec)
if docker pull $image_tag:$version_from_rockspec >/dev/null 2>/dev/null
then
echo $version_from_rockspec-ubuntu
else
echo master-ubuntu
fi
}
export OLD_KONG_VERSION=2.8.0
export OLD_KONG_IMAGE=kong:$OLD_KONG_VERSION-ubuntu
export NEW_KONG_IMAGE=kong/kong:$(get_current_version kong)
function usage() {
cat 1>&2 <<EOF
usage: $0 [ -i <new-kong-image> ] [ <test> ... ]
<new-kong-image> must be the name of a kong image to use as the base image for the
new kong version, based on this repository.
EOF
}
args=$(getopt i: $*)
if [ $? -ne 0 ]
then
usage
exit 1
fi
set -- $args
while :; do
case "$1" in
-i)
export NEW_KONG_IMAGE=$2
shift
shift
;;
--)
shift
break
;;
*)
usage
exit 1
;;
esac
done
TESTS=$*
ENV_PREFIX=${UPGRADE_ENV_PREFIX:-$(openssl rand -hex 8)}
COMPOSE="docker compose -p $ENV_PREFIX -f scripts/upgrade-tests/docker-compose.yml"
NETWORK_NAME=$ENV_PREFIX
OLD_CONTAINER=$ENV_PREFIX-kong_old-1
NEW_CONTAINER=$ENV_PREFIX-kong_new-1
function prepare_container() {
docker exec $1 apt-get update
docker exec $1 apt-get install -y build-essential curl m4 unzip git
docker exec $1 bash -c "ln -sf /usr/local/kong/include/* /usr/include"
docker exec $1 bash -c "ln -sf /usr/local/kong/lib/* /usr/lib"
}
function build_containers() {
echo "Building containers"
[ -d worktree/$OLD_KONG_VERSION ] || git worktree add worktree/$OLD_KONG_VERSION $OLD_KONG_VERSION
$COMPOSE up --wait
prepare_container $OLD_CONTAINER
prepare_container $NEW_CONTAINER
docker exec -w /kong $OLD_CONTAINER make dev CRYPTO_DIR=/usr/local/kong
# Kong version >= 3.3 moved non Bazel-built dev setup to make dev-legacy
docker exec -w /kong $NEW_CONTAINER make dev-legacy CRYPTO_DIR=/usr/local/kong
docker exec ${NEW_CONTAINER} ln -sf /kong/bin/kong /usr/local/bin/kong
}
function initialize_test_list() {
echo "Determining tests to run"
# Prepare list of tests to run
if [ -z "$TESTS" ]
then
all_tests_file=$(mktemp)
available_tests_file=$(mktemp)
docker exec $OLD_CONTAINER kong migrations reset --yes || true
docker exec $OLD_CONTAINER kong migrations bootstrap
docker exec $NEW_CONTAINER kong migrations status \
| jq -r '.new_migrations | .[] | (.namespace | gsub("[.]"; "/")) as $namespace | .migrations[] | "\($namespace)/\(.)_spec.lua" | gsub("^kong"; "spec/05-migration")' \
| sort > $all_tests_file
ls 2>/dev/null $(cat $all_tests_file) \
| sort > $available_tests_file
if [ "$IGNORE_MISSING_TESTS" = "1" ]
then
TESTS=$(cat $available_tests_file)
else
if ! cmp -s $available_tests_file $all_tests_file
then
echo "Not all migrations have corresponding tests, cannot continue. Missing test(s):"
echo
comm -13 $available_tests_file $all_tests_file \
| perl -pe 's/^/ /g'
echo
rm $available_tests_file $all_tests_file
exit 1
fi
TESTS=$(cat $all_tests_file)
fi
rm $available_tests_file $all_tests_file
fi
echo "Going to run:"
echo $TESTS | perl -pe 's/(^| )/\n /g'
# Make tests available in OLD container
TESTS_TAR=/tmp/upgrade-tests-$$.tar
tar cf ${TESTS_TAR} spec/upgrade_helpers.lua $TESTS
docker cp ${TESTS_TAR} ${OLD_CONTAINER}:${TESTS_TAR}
docker exec ${OLD_CONTAINER} mkdir -p /upgrade-test/bin /upgrade-test/spec
docker exec ${OLD_CONTAINER} ln -sf /kong/bin/kong /upgrade-test/bin
docker exec ${OLD_CONTAINER} bash -c "ln -sf /kong/spec/* /upgrade-test/spec"
docker exec ${OLD_CONTAINER} tar -xf ${TESTS_TAR} -C /upgrade-test
docker cp spec/helpers/http_mock ${OLD_CONTAINER}:/upgrade-test/spec/helpers
docker cp spec/helpers/http_mock.lua ${OLD_CONTAINER}:/upgrade-test/spec/helpers
rm ${TESTS_TAR}
}
function run_tests() {
# Run the tests
BUSTED="env KONG_DATABASE=$1 KONG_DNS_RESOLVER= KONG_TEST_PG_DATABASE=kong /kong/bin/busted"
shift
set $TESTS
for TEST in $TESTS
do
docker exec $OLD_CONTAINER kong migrations reset --yes || true
docker exec $OLD_CONTAINER kong migrations bootstrap
echo
echo --------------------------------------------------------------------------------
echo Running $TEST
echo ">> Setting up tests"
docker exec -w /upgrade-test $OLD_CONTAINER $BUSTED -t setup $TEST
echo ">> Running migrations"
docker exec $NEW_CONTAINER kong migrations up
echo ">> Testing old_after_up,all_phases"
docker exec -w /upgrade-test $OLD_CONTAINER $BUSTED -t old_after_up,all_phases $TEST
echo ">> Testing new_after_up,all_phases"
docker exec -w /kong $NEW_CONTAINER $BUSTED -t new_after_up,all_phases $TEST
echo ">> Finishing migrations"
docker exec $NEW_CONTAINER kong migrations finish
echo ">> Testing new_after_finish,all_phases"
docker exec -w /kong $NEW_CONTAINER $BUSTED -t new_after_finish,all_phases $TEST
done
}
function cleanup() {
git worktree remove worktree/$OLD_KONG_VERSION --force
$COMPOSE down
}
build_containers
initialize_test_list
run_tests postgres
[ -z "$UPGRADE_ENV_PREFIX" ] && cleanup
trap "" 0