|
| 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 "$@" |
0 commit comments