-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.sh
More file actions
executable file
·133 lines (117 loc) · 3.67 KB
/
Copy pathtest.sh
File metadata and controls
executable file
·133 lines (117 loc) · 3.67 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
#!/bin/sh
# automated smoke test for the mysql (mariadb) container
# builds the image, starts it standalone, waits for mariadb to accept
# connections and then does a REAL db round-trip (create/insert/select)
set -eu
IMAGE=mysql-test
NAME=mysql-test-run
ADMIN_USER=admin
ADMIN_PASSWORD=adminsecret
DB_NAME=testdb
DB_USER=testuser
DB_PASSWORD=testsecret
FAILED=0
fail() {
echo "FAIL: $*" >&2
FAILED=1
}
cleanup() {
echo ">> cleanup: removing container $NAME"
docker rm -f "$NAME" >/dev/null 2>&1 || true
}
trap cleanup EXIT INT TERM
# run a query as the given user, printing the result (no column headers)
# usage: q <user> <password> <sql> [db]
q() {
_u="$1"; _p="$2"; _sql="$3"; _db="${4:-}"
docker exec "$NAME" mysql -u"$_u" -p"$_p" -N -B ${_db:+"$_db"} -e "$_sql" 2>/dev/null
}
echo ">> building image $IMAGE"
docker build -t "$IMAGE" .
echo ">> (re)starting container $NAME"
docker rm -f "$NAME" >/dev/null 2>&1 || true
docker run -d --name "$NAME" \
-e ADMIN_USER="$ADMIN_USER" \
-e ADMIN_PASSWORD="$ADMIN_PASSWORD" \
-e DB_NAME="$DB_NAME" \
-e DB_USER="$DB_USER" \
-e DB_PASSWORD="$DB_PASSWORD" \
"$IMAGE"
# wait until the admin user can actually run a query (mariadb init takes a while)
echo ">> waiting for mariadb to accept connections (up to ~120s)"
READY=0
i=0
while [ "$i" -lt 60 ]; do
if ! docker ps --format '{{.Names}}' | grep -q "^${NAME}$"; then
echo "!! container is not running anymore, dumping logs:" >&2
docker logs "$NAME" >&2 2>&1 || true
fail "container exited during startup"
break
fi
if [ "$(q "$ADMIN_USER" "$ADMIN_PASSWORD" 'SELECT 1')" = "1" ]; then
READY=1
break
fi
i=$((i + 1))
sleep 2
done
if [ "$READY" -ne 1 ] && [ "$FAILED" -eq 0 ]; then
echo "!! mariadb did not accept connections in time, dumping logs:" >&2
docker logs "$NAME" >&2 2>&1 || true
fail "timed out waiting for mariadb"
fi
# only run the deeper assertions if the db is up
if [ "$READY" -eq 1 ]; then
echo ">> assert: admin user works"
if [ "$(q "$ADMIN_USER" "$ADMIN_PASSWORD" 'SELECT 42')" = "42" ]; then
echo "ok - admin user can query"
else
fail "admin user could not run a query"
fi
# the auto-created db/user is provisioned asynchronously by the entrypoint,
# so wait until the app user can connect to its database
echo ">> waiting for auto-created db '$DB_NAME' + user '$DB_USER'"
DBREADY=0
j=0
while [ "$j" -lt 30 ]; do
if [ "$(q "$DB_USER" "$DB_PASSWORD" 'SELECT 1' "$DB_NAME")" = "1" ]; then
DBREADY=1
break
fi
j=$((j + 1))
sleep 2
done
if [ "$DBREADY" -eq 1 ]; then
echo "ok - db user can connect to '$DB_NAME'"
else
fail "db user '$DB_USER' could not connect to '$DB_NAME'"
fi
if [ "$DBREADY" -eq 1 ]; then
echo ">> assert: real round-trip (CREATE TABLE / INSERT / SELECT) as db user"
q "$DB_USER" "$DB_PASSWORD" \
'CREATE TABLE roundtrip (id INT PRIMARY KEY, name VARCHAR(32));' "$DB_NAME"
q "$DB_USER" "$DB_PASSWORD" \
"INSERT INTO roundtrip (id, name) VALUES (1, 'servercontainers');" "$DB_NAME"
GOT=$(q "$DB_USER" "$DB_PASSWORD" \
'SELECT name FROM roundtrip WHERE id = 1;' "$DB_NAME")
if [ "$GOT" = "servercontainers" ]; then
echo "ok - round-trip returned expected row ('$GOT')"
else
fail "round-trip did not return expected row (got: '$GOT')"
fi
echo ">> assert: admin can see the app database"
if q "$ADMIN_USER" "$ADMIN_PASSWORD" 'SHOW DATABASES;' | grep -q "^${DB_NAME}$"; then
echo "ok - admin sees database '$DB_NAME'"
else
fail "admin does not see database '$DB_NAME'"
fi
fi
fi
echo
if [ "$FAILED" -eq 0 ]; then
echo "ALL TESTS PASSED"
exit 0
else
echo "SOME TESTS FAILED"
exit 1
fi