This repository has been archived by the owner on Feb 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
test.sh
executable file
·302 lines (259 loc) · 6.92 KB
/
test.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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#!/usr/bin/bash
# Test docker image
set -eo pipefail
ENV_FILE=/tmp/mariadb_alpine_test_env
# https://mariadb.com/kb/en/library/mariadb-environment-variables/
export MYSQL_PWD=
echo_success() {
echo "$(tput setaf 10)$1$(tput sgr0)"
}
echo_error() {
echo >&2 "$(tput setaf 9)$1$(tput sgr0)"
}
# Execute MySQL statements
execute() {
if [ -n "$MYSQL_USER" ]; then
mysql --protocol=tcp --port=33060 --user="$MYSQL_USER" --password="$MYSQL_PASSWORD" -ss -e "$1"
else
# two -s to make it output only result
mysql --protocol=tcp --port=33060 -uroot -ss -e "$1"
fi
}
container_status() {
docker inspect --format '{{.State.Status}}' mariadb_alpine_test 2> /dev/null || true
}
start_container() {
if [ -z "$DOCKER_VOLUME" ]; then
docker run --detach --name mariadb_alpine_test -p 33060:3306 \
--env-file="$ENV_FILE" \
-e MYSQL_ROOT_PASSWORD="$MYSQL_PWD" \
mariadb-alpine:latest > /dev/null
else
docker run --detach --name mariadb_alpine_test -p 33060:3306 \
--env-file="$ENV_FILE" \
-e MYSQL_ROOT_PASSWORD="$MYSQL_PWD" \
-v "${DOCKER_VOLUME}" \
mariadb-alpine:latest > /dev/null
fi
}
remove_container() {
docker stop mariadb_alpine_test &> /dev/null || true
docker rm mariadb_alpine_test &> /dev/null || true
}
# Check whether container is running
is_container_running() {
# status: created running paused restarting removing exited dead
status=$(container_status)
[ "$status" = 'created' -o "$status" = 'running' ]
}
# Whether mysql is running correctly
is_mysql_running() {
execute 'SELECT 1' &> /dev/null
}
wait_running() {
for i in `seq 30 -1 0`; do
if ! is_container_running; then
echo_error 'Container failed to start'
exit 1
fi
if is_mysql_running; then
break
fi
sleep 1
done
if [ "$i" = 0 ]; then
echo_error 'Test failed'
exit 1
fi
}
check_running() {
wait_running
echo_success 'Test successful'
}
# Test MYSQL_ROOT_PASSWORD
test_root_password() {
export MYSQL_PWD='root'
echo "Test MYSQL_ROOT_PASSWORD='${MYSQL_PWD}'"
echo > "$ENV_FILE"
start_container
check_running
remove_container
# password with special characters
export MYSQL_PWD='a#F a$b~-'
echo "Test MYSQL_ROOT_PASSWORD='${MYSQL_PWD}'"
echo > "$ENV_FILE"
start_container
check_running
remove_container
}
# Test MYSQL_RANDOM_ROOT_PASSWORD
test_random_root_password() {
unset MYSQL_PWD
echo "Test MYSQL_RANDOM_ROOT_PASSWORD=yes"
echo "MYSQL_RANDOM_ROOT_PASSWORD=yes" > "$ENV_FILE"
start_container
for i in `seq 30 -1 0`; do
if ! is_container_running; then
echo_error 'Container failed to start'
exit 1
fi
password=$(docker logs mariadb_alpine_test 2>&1 | grep -m1 '^GENERATED ROOT PASSWORD:' | cut -d' ' -f4- || true)
if [ -n "$password" ]; then
export MYSQL_PWD="$password"
break
fi
sleep 1
done
if [ -z "$MYSQL_PWD" ]; then
echo_error 'Failed to get random root password'
exit 1
else
check_running
remove_container
fi
}
# Test MYSQL_ALLOW_EMPTY_PASSWORD
test_empty_root_password() {
unset MYSQL_PWD
echo 'Test MYSQL_ALLOW_EMPTY_PASSWORD=yes'
echo "MYSQL_ALLOW_EMPTY_PASSWORD=yes" > "$ENV_FILE"
start_container
check_running
remove_container
}
# Test MYSQL_ROOT_HOST
test_mysql_root_host() {
export MYSQL_PWD=mypassword
# Docker host ip
local host=$(docker network inspect bridge --format '{{(index .IPAM.Config 0).Gateway}}')
echo "Test MYSQL_ROOT_HOST=$host"
echo "MYSQL_ROOT_HOST=$host" > "$ENV_FILE"
start_container
check_running
remove_container
# host not owned by current machine
local host=example.com
echo "Test MYSQL_ROOT_HOST=$host"
echo "MYSQL_ROOT_HOST=$host" > "$ENV_FILE"
start_container
# Wait for mysqld to startup
for i in `seq 30 -1 0`; do
if ! is_container_running; then
echo_error 'Container failed to start'
exit 1
fi
if docker logs mariadb_alpine_test 2>&1 | grep 'mysqld_safe Starting mysqld daemon' &> /dev/null; then
sleep 3
break
fi
done
# Should not be allowed to access the
result=$(execute 'SELECT 1' 2>&1 || true)
if [ "$result" = "1" ]; then
echo_error 'Should not be allowed to connect'
exit 1
else
echo_success 'Test successful'
remove_container
fi
}
# Test MYSQL_DATABASE
test_mysql_database() {
export MYSQL_PWD=mypassword
local database=blog
echo "Test MYSQL_DATABASE=$database"
echo "MYSQL_DATABASE=$database" > "$ENV_FILE"
start_container
wait_running
if execute "SHOW CREATE DATABASE \`$database\`;" &> /dev/null; then
echo_success 'Test successful'
remove_container
else
echo_error "Database $database not exist"
exit 1
fi
}
# Test MYSQL_USER, MYSQL_PASSWORD
test_mysql_user() {
export MYSQL_PWD=mypassword
export MYSQL_USER=alice
export MYSQL_PASSWORD=alice_password
echo "Test MYSQL_USER=$MYSQL_USER, MYSQL_PASSWORD=$MYSQL_PASSWORD"
echo -e "MYSQL_USER=$MYSQL_USER\nMYSQL_PASSWORD=$MYSQL_PASSWORD" > "$ENV_FILE"
start_container
check_running
remove_container
unset MYSQL_USER
unset MYSQL_PASSWORD
}
# MYSQL_INITDB_SKIP_TZINFO
test_skip_tzinfo() {
export MYSQL_PWD=mypassword
echo 'Test MYSQL_INITDB_SKIP_TZINFO='
echo 'MYSQL_INITDB_SKIP_TZINFO=' > "$ENV_FILE"
start_container
wait_running
local count=$(execute 'SELECT COUNT(*) FROM mysql.time_zone' || true)
if [ "$count" = "0" ]; then
echo_error "No timezone records inserted"
exit 1
else
echo_success "Test successful"
remove_container
fi
echo 'Test MYSQL_INITDB_SKIP_TZINFO=yes'
echo 'MYSQL_INITDB_SKIP_TZINFO=yes' > "$ENV_FILE"
start_container
wait_running
local count=$(execute 'SELECT COUNT(*) FROM mysql.time_zone' || true)
if [ "$count" != "0" ]; then
echo_error "Timezone records inserted"
exit 1
else
echo_success "Test successful"
remove_container
fi
}
test_volume() {
export MYSQL_PWD=mypassword
export DOCKER_VOLUME="$(mktemp -p /tmp -d mariadb_alpine_test_volume.XXXXX):/var/lib/mysql"
echo 'Test volume'
echo > "$ENV_FILE"
start_container
wait_running
remove_container
# Use already initialized volume
start_container
check_running
remove_container
unset DOCKER_VOLUME
}
test_custom_initialization_script() {
export MYSQL_PWD='root'
export DOCKER_VOLUME="$(readlink -e test/initdb.d):/docker-entrypoint-initdb.d"
echo "Test custom initialization script"
echo > "$ENV_FILE"
start_container
wait_running
local result=$(execute "USE test_docker; SELECT name from users where name = 'admin' LIMIT 1" || true)
if [ "$result" = "Admin" ]; then
echo_success "Test successful"
remove_container
else
echo_error "No records found"
exit 1
fi
}
remove_container
test_root_password
test_empty_root_password
test_random_root_password
test_mysql_root_host
test_mysql_database
test_mysql_user
test_skip_tzinfo
test_volume
test_custom_initialization_script
echo 'Clean up temp files...'
rm -f "$ENV_FILE"
sudo rm -rf /tmp/mariadb_alpine_test_volume*