gh-ost/localtests/test.sh

224 lines
6.4 KiB
Bash
Raw Normal View History

2016-08-22 07:20:17 +00:00
#!/bin/bash
2016-08-24 10:51:01 +00:00
# Local integration tests. To be used by CI.
# See https://github.com/github/gh-ost/tree/doc/local-tests.md
#
2016-08-22 07:20:17 +00:00
2016-09-06 07:46:11 +00:00
# Usage: localtests/test/sh [filter]
# By default, runs all tests. Given filter, will only run tests matching given regep
2016-08-22 07:20:17 +00:00
tests_path=$(dirname $0)
2016-08-22 09:13:18 +00:00
test_logfile=/tmp/gh-ost-test.log
2018-02-26 12:32:58 +00:00
default_ghost_binary=/tmp/gh-ost-test
ghost_binary=""
2016-08-22 10:18:02 +00:00
exec_command_file=/tmp/gh-ost-test.bash
2018-02-11 13:03:42 +00:00
orig_content_output_file=/tmp/gh-ost-test.orig.content.csv
ghost_content_output_file=/tmp/gh-ost-test.ghost.content.csv
2016-09-06 07:36:29 +00:00
2016-08-22 09:13:18 +00:00
master_host=
master_port=
replica_host=
replica_port=
2016-08-22 07:20:17 +00:00
2018-02-26 12:32:58 +00:00
OPTIND=1
while getopts "b:" OPTION
do
case $OPTION in
b)
ghost_binary="$OPTARG"
;;
esac
done
shift $((OPTIND-1))
test_pattern="${1:-.}"
2016-08-22 09:13:18 +00:00
verify_master_and_replica() {
if [ "$(gh-ost-test-mysql-master -e "select 1" -ss)" != "1" ] ; then
echo "Cannot verify gh-ost-test-mysql-master"
exit 1
fi
read master_host master_port <<< $(gh-ost-test-mysql-master -e "select @@hostname, @@port" -ss)
2018-02-11 12:58:16 +00:00
[ "$master_host" == "$(hostname)" ] && master_host="127.0.0.1"
2018-02-11 12:08:39 +00:00
echo "# master verified at $master_host:$master_port"
2016-08-22 09:13:18 +00:00
if [ "$(gh-ost-test-mysql-replica -e "select 1" -ss)" != "1" ] ; then
echo "Cannot verify gh-ost-test-mysql-replica"
exit 1
fi
if [ "$(gh-ost-test-mysql-replica -e "select @@global.binlog_format" -ss)" != "ROW" ] ; then
echo "Expecting test replica to have binlog_format=ROW"
exit 1
fi
2016-08-22 09:13:18 +00:00
read replica_host replica_port <<< $(gh-ost-test-mysql-replica -e "select @@hostname, @@port" -ss)
2018-02-11 12:58:16 +00:00
[ "$replica_host" == "$(hostname)" ] && replica_host="127.0.0.1"
2018-02-11 12:08:39 +00:00
echo "# replica verified at $replica_host:$replica_port"
2016-08-22 09:13:18 +00:00
}
2016-08-22 09:51:03 +00:00
exec_cmd() {
echo "$@"
command "$@" 1> $test_logfile 2>&1
return $?
}
2016-09-06 07:36:29 +00:00
echo_dot() {
echo -n "."
}
start_replication() {
gh-ost-test-mysql-replica -e "stop slave; start slave;"
num_attempts=0
while gh-ost-test-mysql-replica -e "show slave status\G" | grep Seconds_Behind_Master | grep -q NULL ; do
((num_attempts=num_attempts+1))
if [ $num_attempts -gt 10 ] ; then
echo
echo "ERROR replication failure"
exit 1
fi
echo_dot
sleep 1
done
}
2016-08-22 09:13:18 +00:00
test_single() {
local test_name
test_name="$1"
2016-08-22 07:20:17 +00:00
2016-09-06 07:36:29 +00:00
echo -n "Testing: $test_name"
2016-08-22 09:13:18 +00:00
2016-09-06 07:36:29 +00:00
echo_dot
start_replication
2016-09-06 07:36:29 +00:00
echo_dot
gh-ost-test-mysql-master --default-character-set=utf8mb4 test < $tests_path/$test_name/create.sql
2016-08-22 09:13:18 +00:00
2016-08-22 09:51:03 +00:00
extra_args=""
2016-08-22 09:13:18 +00:00
if [ -f $tests_path/$test_name/extra_args ] ; then
2016-08-22 10:18:02 +00:00
extra_args=$(cat $tests_path/$test_name/extra_args)
2016-08-22 09:13:18 +00:00
fi
2016-09-20 14:00:07 +00:00
orig_columns="*"
ghost_columns="*"
order_by=""
2016-09-20 14:00:07 +00:00
if [ -f $tests_path/$test_name/orig_columns ] ; then
orig_columns=$(cat $tests_path/$test_name/orig_columns)
fi
if [ -f $tests_path/$test_name/ghost_columns ] ; then
ghost_columns=$(cat $tests_path/$test_name/ghost_columns)
2016-08-22 09:13:18 +00:00
fi
if [ -f $tests_path/$test_name/order_by ] ; then
order_by="order by $(cat $tests_path/$test_name/order_by)"
fi
2016-08-24 12:18:49 +00:00
# graceful sleep for replica to catch up
2016-09-06 07:36:29 +00:00
echo_dot
2016-08-24 12:18:49 +00:00
sleep 1
#
2016-10-13 11:13:28 +00:00
cmd="$ghost_binary \
2016-08-22 09:13:18 +00:00
--user=gh-ost \
--password=gh-ost \
--host=$replica_host \
--port=$replica_port \
2018-02-11 12:58:16 +00:00
--assume-master-host=${master_host}:${master_port}
2016-08-22 09:13:18 +00:00
--database=test \
--table=gh_ost_test \
2016-08-22 10:18:02 +00:00
--alter='engine=innodb' \
2016-08-22 09:13:18 +00:00
--exact-rowcount \
--assume-rbr \
2016-08-22 09:13:18 +00:00
--initially-drop-old-table \
--initially-drop-ghost-table \
2016-08-22 10:18:02 +00:00
--throttle-query='select timestampdiff(second, min(last_update), now()) < 5 from _gh_ost_test_ghc' \
2016-08-22 09:13:18 +00:00
--serve-socket-file=/tmp/gh-ost.test.sock \
--initially-drop-socket-file \
--test-on-replica \
2016-08-22 13:43:00 +00:00
--default-retries=1 \
2017-08-21 05:20:06 +00:00
--chunk-size=10 \
2016-08-22 09:13:18 +00:00
--verbose \
--debug \
--stack \
2016-08-22 10:18:02 +00:00
--execute ${extra_args[@]}"
2016-09-06 07:36:29 +00:00
echo_dot
2016-08-22 10:18:02 +00:00
echo $cmd > $exec_command_file
2016-09-06 07:36:29 +00:00
echo_dot
2016-08-22 13:43:00 +00:00
bash $exec_command_file 1> $test_logfile 2>&1
2016-08-22 09:13:18 +00:00
execution_result=$?
2016-10-14 07:05:38 +00:00
if [ -f $tests_path/$test_name/destroy.sql ] ; then
gh-ost-test-mysql-master --default-character-set=utf8mb4 test < $tests_path/$test_name/destroy.sql
fi
if [ -f $tests_path/$test_name/expect_failure ] ; then
if [ $execution_result -eq 0 ] ; then
echo
echo "ERROR $test_name execution was expected to exit on error but did not. cat $test_logfile"
return 1
fi
if [ -s $tests_path/$test_name/expect_failure ] ; then
# 'expect_failure' file has content. We expect to find this content in the log.
expected_error_message="$(cat $tests_path/$test_name/expect_failure)"
if grep -q "$expected_error_message" $test_logfile ; then
return 0
fi
echo
echo "ERROR $test_name execution was expected to exit with error message '${expected_error_message}' but did not. cat $test_logfile"
return 1
fi
# 'expect_failure' file has no content. We generally agree that the failure is correct
return 0
fi
if [ $execution_result -ne 0 ] ; then
2016-09-06 07:36:29 +00:00
echo
2018-02-12 09:14:40 +00:00
echo "ERROR $test_name execution failure. cat $test_logfile:"
cat $test_logfile
2016-08-22 10:18:02 +00:00
return 1
fi
2016-08-22 09:13:18 +00:00
2016-09-06 07:36:29 +00:00
echo_dot
2017-10-24 12:24:31 +00:00
gh-ost-test-mysql-replica --default-character-set=utf8mb4 test -e "select ${orig_columns} from gh_ost_test ${order_by}" -ss > $orig_content_output_file
gh-ost-test-mysql-replica --default-character-set=utf8mb4 test -e "select ${ghost_columns} from _gh_ost_test_gho ${order_by}" -ss > $ghost_content_output_file
orig_checksum=$(cat $orig_content_output_file | md5sum)
ghost_checksum=$(cat $ghost_content_output_file | md5sum)
2016-08-22 09:13:18 +00:00
2016-08-22 10:18:02 +00:00
if [ "$orig_checksum" != "$ghost_checksum" ] ; then
echo "ERROR $test_name: checksum mismatch"
2016-08-22 13:43:00 +00:00
echo "---"
2017-10-24 12:24:31 +00:00
diff $orig_content_output_file $ghost_content_output_file
echo "diff $orig_content_output_file $ghost_content_output_file"
2016-08-22 10:18:02 +00:00
return 1
fi
2016-08-22 09:13:18 +00:00
}
2016-10-13 11:13:28 +00:00
build_binary() {
echo "Building"
2018-02-26 12:32:58 +00:00
rm -f $default_ghost_binary
[ "$ghost_binary" == "" ] && ghost_binary="$default_ghost_binary"
if [ -f "$ghost_binary" ] ; then
echo "Using binary: $ghost_binary"
return 0
fi
2016-10-13 11:13:28 +00:00
go build -o $ghost_binary go/cmd/gh-ost/main.go
2017-11-08 09:11:17 +00:00
if [ $? -ne 0 ] ; then
echo "Build failure"
exit 1
fi
2016-10-13 11:13:28 +00:00
}
2016-08-22 09:13:18 +00:00
test_all() {
2016-10-13 11:13:28 +00:00
build_binary
2016-09-06 07:36:29 +00:00
find $tests_path ! -path . -type d -mindepth 1 -maxdepth 1 | cut -d "/" -f 3 | egrep "$test_pattern" | while read test_name ; do
2016-08-22 09:13:18 +00:00
test_single "$test_name"
2016-08-22 09:51:03 +00:00
if [ $? -ne 0 ] ; then
create_statement=$(gh-ost-test-mysql-replica test -t -e "show create table _gh_ost_test_gho \G")
echo "$create_statement" >> $test_logfile
2016-08-22 09:51:03 +00:00
echo "+ FAIL"
return 1
else
echo
2016-08-22 09:51:03 +00:00
echo "+ pass"
fi
2016-08-22 13:43:00 +00:00
gh-ost-test-mysql-replica -e "start slave"
2016-08-22 09:13:18 +00:00
done
}
verify_master_and_replica
test_all