From 4c66cf9c289a4e36400066a0a3e823aebdc16f41 Mon Sep 17 00:00:00 2001 From: Boris Savelev Date: Sat, 20 Aug 2022 15:07:15 +0300 Subject: [PATCH] Improve script for TiDB tests * use isolated docker network instead of magic with MYIP * init script can accept TiDB verion * minor changes in usage help --- GNUmakefile | 2 +- scripts/tidb-test-cluster.sh | 160 +++++++++++------------------------ 2 files changed, 49 insertions(+), 113 deletions(-) diff --git a/GNUmakefile b/GNUmakefile index f068ef5a..efd51203 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -53,7 +53,7 @@ testtidb%: $(MAKE) MYSQL_VERSION=$* MYSQL_PORT=34$(shell echo "$*" | tr -d '.') testtidb testtidb: - @sh -c "'$(CURDIR)/scripts/tidb-test-cluster.sh' --init --port $(MYSQL_PORT)" + @sh -c "'$(CURDIR)/scripts/tidb-test-cluster.sh' --init --port $(MYSQL_PORT) --version $(MYSQL_VERSION)" @echo 'Waiting for TiDB...' @while ! mysql -h 127.0.0.1 -P $(MYSQL_PORT) -u "$(TEST_USER)" -e 'SELECT 1' >/dev/null 2>&1; do printf '.'; sleep 1; done ; echo ; echo "Connected!" MYSQL_USERNAME="$(TEST_USER)" MYSQL_PASSWORD="" MYSQL_ENDPOINT=127.0.0.1:$(MYSQL_PORT) $(MAKE) testacc diff --git a/scripts/tidb-test-cluster.sh b/scripts/tidb-test-cluster.sh index f5e8a1b8..2641db7d 100755 --- a/scripts/tidb-test-cluster.sh +++ b/scripts/tidb-test-cluster.sh @@ -1,4 +1,5 @@ #!/usr/bin/env bash + # This file creates minimal TiDB cluster for tests REALPATH=$(which realpath) @@ -10,13 +11,12 @@ fi # Set up constants SCRIPT_PATH=$(realpath $(dirname "$0")) -TEMPDIR=$(mktemp -d) DOCKER=$(which docker) -TAG_VERSION="v6.1.0" SCRIPT_INIT=false -MYIP="127.0.0.1" +DOCKER_NETWORK="mysql_provider_test_network" RUNNING_CONTAINERS="" export MYSQL_PORT=${MYSQL_PORT:-4000} +export TAG_VERSION="v${MYSQL_VERSION:-6.1.0}" # Sanity checks if [ -z "$DOCKER" ]; then @@ -31,12 +31,13 @@ set -o nounset # Disallow expansion of unset variables set -o pipefail # Use last non-zero exit code in a pipeline function script_usage() { - cat < TiDB Listen port - --init Init TiDB cluster - --destroy Destroy resources - -h|--help Displays this help + --init ^ Init TiDB cluster + --destroy ^ Destroy resources + --port ^ TiDB Listen port (default: 4000) + --version ^ TiDB version (default: 6.1.0) + -h|--help ^ Displays this help EOF } @@ -46,20 +47,31 @@ function parse_params() { param="$1" case $param in --port) - if [ -z "$2" ]; then - echo missing param + shift + if [ -z "$1" ]; then + echo "Missing port" + exit 1 fi - export MYSQL_PORT=${2:-4000} - shift # past argument - shift # past value + export MYSQL_PORT=${1:-4000} + shift ;; + --version) + shift + if [ -z "$1" ]; then + echo "Missing version" + exit 1 + fi + export TAG_VERSION="v${1:-6.1.0}" + shift + ;; + --init) export SCRIPT_INIT=true shift ;; --destroy) if [ -z "$SCRIPT_INIT" ]; then - echo "Cant destroy and init at once" + echo "Can't destroy and init at once" exit 1 fi destroy_cluster @@ -80,81 +92,13 @@ function parse_params() { done } - - -# My IP detection source code -function get_my_ip() { - if [ ! -d "$SCRIPT_PATH/../bin" ]; then - mkdir -p $SCRIPT_PATH/../bin - fi -cat <<\__END__ > $SCRIPT_PATH/../bin/myip.go -package main - -import ( - "errors" - "fmt" - "net" - "os" -) - -func main() { - - ip, err := externalIP() - if err != nil { - fmt.Println(err) - os.Exit(1) - } - fmt.Println(ip) - -} - -func externalIP() (string, error) { - ifaces, err := net.Interfaces() - if err != nil { - return "", err - } - for _, iface := range ifaces { - if iface.Flags&net.FlagUp == 0 { - continue // interface down - } - if iface.Flags&net.FlagLoopback != 0 { - continue // loopback interface - } - addrs, err := iface.Addrs() - if err != nil { - return "", err - } - for _, addr := range addrs { - var ip net.IP - switch v := addr.(type) { - case *net.IPNet: - ip = v.IP - case *net.IPAddr: - ip = v.IP - } - if ip == nil || ip.IsLoopback() { - continue - } - ip = ip.To4() - if ip == nil { - continue // not an ipv4 address - } - return ip.String(), nil - } - } - return "", errors.New("are you connected to the network?") -} -__END__ -gofmt -w $SCRIPT_PATH/../bin/myip.go || return 1 -MYIP=$(go run $SCRIPT_PATH/../bin/myip.go) -} - function destroy_cluster() { RUNNING_CONTAINERS=$(${DOCKER} ps -a -q -f name=tidb -f name=tikv -f name=pd) echo "==> Removing any existing TiDB cluster components" if [ ! -z "$RUNNING_CONTAINERS" ]; then ${DOCKER} rm -f $RUNNING_CONTAINERS >/dev/null 2>&1 fi + ${DOCKER} network rm ${DOCKER_NETWORK} >/dev/null 2>&1 || true } function show_docker_logs_and_exit() { @@ -165,66 +109,58 @@ function show_docker_logs_and_exit() { } function run_pd() { - local _myip=$1 echo "==> Pulling up PD component" - ${DOCKER} run -d --name pd1 \ - -p 2379:2379 \ - -p 2380:2380 \ + ${DOCKER} run -d --name pd \ -v /etc/localtime:/etc/localtime:ro \ - -v /data:/data \ - -h pd1 \ + -h pd \ + --network "$DOCKER_NETWORK" \ pingcap/pd:$TAG_VERSION \ - --name="pd1" \ - --data-dir="$TEMPDIR/pd1" \ + --name="pd" \ + --data-dir="/data" \ --client-urls="http://0.0.0.0:2379" \ - --advertise-client-urls="http://$_myip:2379" \ + --advertise-client-urls="http://pd:2379" \ --peer-urls="http://0.0.0.0:2380" \ - --advertise-peer-urls="http://$_myip:2380" \ - --initial-cluster="pd1=http://$_myip:2380" >/dev/null 2>&1 || show_docker_logs_and_exit pd + --advertise-peer-urls="http://pd:2380" \ + --initial-cluster="pd=http://pd:2380" >/dev/null 2>&1 || show_docker_logs_and_exit pd } function run_tikv() { - local _myip=$1 echo "==> Pulling up TiKV component" - ${DOCKER} run -d --name tikv1 \ - -p 20160:20160 \ - -p 20180:20180 \ + ${DOCKER} run -d --name tikv \ -v /etc/localtime:/etc/localtime:ro \ - -v /data:/data \ - -h tikv1 \ + -h tikv \ + --network "$DOCKER_NETWORK" \ pingcap/tikv:v4.0.0 \ --addr="0.0.0.0:20160" \ - --advertise-addr="$_myip:20160" \ + --advertise-addr="tikv:20160" \ --status-addr="0.0.0.0:20180" \ - --data-dir="$TEMPDIR/tikv1" \ - --pd="$_myip:2379" >/dev/null 2>&1 || show_docker_logs_and_exit tikv + --data-dir="/data" \ + --pd="pd:2379" >/dev/null 2>&1 || show_docker_logs_and_exit tikv } function run_tidb() { - local _myip=$1 - local _mysql_port=$2 + local _mysql_port=$1 echo "==> Pulling up TiDB component" ${DOCKER} run -d --name tidb \ -p $_mysql_port:$_mysql_port \ - -p 10080:10080 \ -v /etc/localtime:/etc/localtime:ro \ -h tidb \ + --network "$DOCKER_NETWORK" \ pingcap/tidb:$TAG_VERSION \ --store=tikv \ -P $_mysql_port \ - --path="$_myip:2379" >/dev/null 2>&1 || show_docker_logs_and_exit tidb + --path="pd:2379" >/dev/null 2>&1 || show_docker_logs_and_exit tidb } function main() { - local _myip parse_params "$@" if [[ "$SCRIPT_INIT" = "true" ]]; then echo "==> Pulling up TiDB cluster with TiKV and TB components" - get_my_ip && \ destroy_cluster && \ - run_pd $MYIP && \ - run_tikv $MYIP && \ - run_tidb $MYIP $MYSQL_PORT + ${DOCKER} network create ${DOCKER_NETWORK} && \ + run_pd && \ + run_tikv && \ + run_tidb $MYSQL_PORT else script_usage