Skip to content

Commit dbd3d71

Browse files
authored
Docker setup for localtests (#1479)
* add docker localtest * add MYSQL_ROOT_HOST
1 parent 0676116 commit dbd3d71

6 files changed

+135
-5
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
/libexec/
44
/.vendor/
55
.idea/
6+
*.tmp

Diff for: localtests/docker-compose.yml

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
services:
2+
mysql-primary:
3+
image: $TEST_MYSQL_IMAGE
4+
container_name: mysql-primary
5+
command: --server-id=1 --log-bin=mysql-bin --binlog-format=row --gtid-mode=ON --enforce-gtid-consistency=ON
6+
environment:
7+
MYSQL_ROOT_PASSWORD: opensesame
8+
MYSQL_ROOT_HOST: '%'
9+
MYSQL_DATABASE: test
10+
MYSQL_TCP_PORT: 3307
11+
ports:
12+
- '3307:3307'
13+
expose:
14+
- '3307'
15+
mysql-replica:
16+
image: $TEST_MYSQL_IMAGE
17+
container_name: mysql-replica
18+
command: --server-id=2 --log-bin=mysql-bin --binlog-format=row --gtid-mode=ON --enforce-gtid-consistency=ON --log-slave-updates=ON
19+
environment:
20+
MYSQL_ROOT_PASSWORD: opensesame
21+
MYSQL_ROOT_HOST: '%'
22+
MYSQL_DATABASE: test
23+
MYSQL_TCP_PORT: 3308
24+
ports:
25+
- '3308:3308'
26+
expose:
27+
- '3308'

Diff for: localtests/test.sh

+16-5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ tests_path=$(dirname $0)
1111
test_logfile=/tmp/gh-ost-test.log
1212
default_ghost_binary=/tmp/gh-ost-test
1313
ghost_binary=""
14+
docker=false
1415
storage_engine=innodb
1516
exec_command_file=/tmp/gh-ost-test.bash
1617
ghost_structure_output_file=/tmp/gh-ost-test.ghost.structure.sql
@@ -25,13 +26,15 @@ replica_port=
2526
original_sql_mode=
2627

2728
OPTIND=1
28-
while getopts "b:s:" OPTION
29+
while getopts "b:s:d" OPTION
2930
do
3031
case $OPTION in
3132
b)
3233
ghost_binary="$OPTARG";;
3334
s)
3435
storage_engine="$OPTARG";;
36+
d)
37+
docker=true;;
3538
esac
3639
done
3740
shift $((OPTIND-1))
@@ -98,6 +101,13 @@ test_single() {
98101
local test_name
99102
test_name="$1"
100103

104+
if [ "$docker" = true ]; then
105+
master_host="0.0.0.0"
106+
master_port="3307"
107+
replica_host="0.0.0.0"
108+
replica_port="3308"
109+
fi
110+
101111
if [ -f $tests_path/$test_name/ignore_versions ] ; then
102112
ignore_versions=$(cat $tests_path/$test_name/ignore_versions)
103113
mysql_version=$(gh-ost-test-mysql-master -s -s -e "select @@version")
@@ -270,9 +280,10 @@ build_binary() {
270280

271281
test_all() {
272282
build_binary
273-
find $tests_path ! -path . -type d -mindepth 1 -maxdepth 1 | cut -d "/" -f 3 | egrep "$test_pattern" | sort | while read test_name ; do
274-
test_single "$test_name"
275-
if [ $? -ne 0 ] ; then
283+
test_dirs=$(find "$tests_path" -mindepth 1 -maxdepth 1 ! -path . -type d | grep "$test_pattern" | sort)
284+
while read -r test_dir; do
285+
test_name=$(basename "$test_dir")
286+
if ! test_single "$test_name" ; then
276287
create_statement=$(gh-ost-test-mysql-replica test -t -e "show create table _gh_ost_test_gho \G")
277288
echo "$create_statement" >> $test_logfile
278289
echo "+ FAIL"
@@ -282,7 +293,7 @@ test_all() {
282293
echo "+ pass"
283294
fi
284295
gh-ost-test-mysql-replica -e "start slave"
285-
done
296+
done <<< "$test_dirs"
286297
}
287298

288299
verify_master_and_replica

Diff for: script/docker-gh-ost-replica-tests

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#!/bin/bash
2+
3+
# This script starts two MySQL docker containers in a primary-replica setup
4+
# which can be used for running the replica tests in localtests/ .
5+
# Set the environment var TEST_MYSQL_IMAGE to change the docker image.
6+
#
7+
# Usage:
8+
# docker-gh-ost-replica-tests up start the containers
9+
# docker-gh-ost-replica-tests down remove the containers
10+
# docker-gh-ost-replica-tests run run replica tests on the containers
11+
12+
set -e
13+
14+
GH_OST_ROOT=$(git rev-parse --show-toplevel)
15+
if [[ ":$PATH:" != *":$GH_OST_ROOT:"* ]]; then
16+
export PATH="${PATH}:${GH_OST_ROOT}/script"
17+
fi
18+
19+
poll_mysql() {
20+
CTR=0
21+
cmd="gh-ost-test-mysql-$1"
22+
while ! $cmd -e "select 1;" > /dev/null 2>&1
23+
do
24+
sleep 1
25+
CTR=$((CTR + 1))
26+
if [ $CTR -gt 30 ]; then
27+
echo " ❌ MySQL $1 failed to start"
28+
return 1
29+
fi
30+
done
31+
echo " ✔ MySQL $1 OK"
32+
return 0
33+
}
34+
35+
setup() {
36+
[ -z "$TEST_MYSQL_IMAGE" ] && TEST_MYSQL_IMAGE="mysql:8.0.39"
37+
38+
echo "Starting MySQL $TEST_MYSQL_IMAGE containers..."
39+
compose_file="$GH_OST_ROOT/localtests/docker-compose.yml"
40+
(TEST_MYSQL_IMAGE="$TEST_MYSQL_IMAGE" envsubst < "$compose_file") > "$compose_file.tmp"
41+
docker compose -f "$compose_file.tmp" up -d --wait
42+
43+
echo "Waiting for MySQL..."
44+
poll_mysql "master" || exit 1
45+
poll_mysql "replica" || exit 1
46+
47+
echo -n "Setting up replication..."
48+
gh-ost-test-mysql-master -e "create user if not exists 'repl'@'%' identified with 'mysql_native_password' by 'repl';"
49+
gh-ost-test-mysql-master -e "grant replication slave on *.* to 'repl'@'%'; flush privileges;"
50+
gh-ost-test-mysql-master -e "create user if not exists 'gh-ost'@'%' identified by 'gh-ost';"
51+
gh-ost-test-mysql-master -e "grant all on *.* to 'gh-ost'@'%';"
52+
53+
sleep 1
54+
gh-ost-test-mysql-replica -e "change master to master_host='mysql-primary', master_port=3307, master_user='repl', master_password='repl', master_auto_position=1;"
55+
gh-ost-test-mysql-replica -e "start slave;"
56+
echo "OK"
57+
}
58+
59+
teardown() {
60+
echo "Stopping containers..."
61+
docker stop mysql-replica
62+
docker stop mysql-primary
63+
echo "Removing containers..."
64+
docker rm mysql-replica
65+
docker rm mysql-primary
66+
}
67+
68+
main() {
69+
if [[ "$1" == "up" ]]; then
70+
setup
71+
elif [[ "$1" == "down" ]]; then
72+
teardown
73+
elif [[ "$1" == "run" ]]; then
74+
shift 1
75+
"$GH_OST_ROOT/localtests/test.sh" -d "$@"
76+
fi
77+
}
78+
79+
main "$@"

Diff for: script/gh-ost-test-mysql-master

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
#
3+
# This executes a command on the mysql-primary docker container created
4+
# from localtests/docker-compose.yml. It's used by localtests/test.sh.
5+
6+
MYSQL_PWD=opensesame mysql -uroot -h0.0.0.0 -P3307 "$@"

Diff for: script/gh-ost-test-mysql-replica

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
#
3+
# This executes a command on the mysql-replica docker container created
4+
# from localtests/docker-compose.yml. It's used by localtests/test.sh.
5+
6+
MYSQL_PWD=opensesame mysql -uroot -h0.0.0.0 -P3308 "$@"

0 commit comments

Comments
 (0)