Skip to content

Commit

Permalink
Merge pull request pingcap#12 from NingLin-P/add-integration-tests-dev
Browse files Browse the repository at this point in the history
add integration tests
  • Loading branch information
NingLin-P authored Oct 12, 2019
2 parents 5293cb4 + 2250a8a commit 501e3c9
Show file tree
Hide file tree
Showing 14 changed files with 449 additions and 1 deletion.
12 changes: 11 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,21 @@ GOCHECKER := awk '{ print } END { if (NR > 0) { exit 1 } }'
all: check build test

build:
GO111MODULE=on go build -race
GO111MODULE=on go build -race -o bin/br

test:
GO111MODULE=on go test -race ./...

integration_test: build
@which bin/tidb-server
@which bin/tikv-server
@which bin/pd-server
@which bin/pd-ctl
@which bin/tikv-importer
@which bin/go-ycsb
@which bin/br
tests/run.sh

tools:
@echo "install tools..."
@GO111MODULE=off go get github.com/twitchtv/retool
Expand Down
49 changes: 49 additions & 0 deletions tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
This folder contains all tests which relies on external processes such as TiDB.

Unit tests (the `*_test.go` files inside the source directory) should *never* rely on external
programs.

## Preparations

1. The following 6 executables must be copied or linked into these locations:
* `bin/tidb-server`
* `bin/tikv-server`
* `bin/pd-server`
* `bin/pd-ctl`
* `bin/tikv-importer`
* `bin/go-ycsb`

The versions must be ≥2.1.0 as usual.

2. The following programs must be installed:

* `mysql` (the CLI client)
* `curl`

3. The user executing the tests must have permission to create the folder
`/tmp/backup_restore_test`. All test artifacts will be written into this folder.

## Running

Make sure the path is `br/`

Run `make integration_test` to execute the integration tests. This command will

1. Build `br`
2. Check that all 6 required executables and `br` executable exist
3. Execute `tests/run.sh`

If the first tow steps are done before, you could also run `tests/run.sh` directly.
This script will

1. Start PD, TiKV, TiDB and TiKV-Importer in background with local storage
2. Find out all `tests/*/run.sh` and run it

## Writing new tests

New integration tests can be written as shell scripts in `tests/TEST_NAME/run.sh`.
The script should exit with a nonzero error code on failure.

Several convenient commands are provided:

* `run_sql <SQL>` — Executes an SQL query on the TiDB database
19 changes: 19 additions & 0 deletions tests/_utils/run_sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/sh
#
# Copyright 2019 PingCAP, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# See the License for the specific language governing permissions and
# limitations under the License.

set -eu

echo "[$(date)] Executing SQL: $1" > "$TEST_DIR/sql_res.$TEST_NAME.txt"
mysql -uroot -h127.0.0.1 -P4000 --default-character-set utf8 -E -e "$1"
58 changes: 58 additions & 0 deletions tests/br_full/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/bin/sh
#
# Copyright 2019 PingCAP, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# See the License for the specific language governing permissions and
# limitations under the License.

set -eu
DB="$TEST_NAME"
TABLE="usertable"
DB_COUNT=3

for i in $(seq $DB_COUNT); do
run_sql "CREATE DATABASE $DB${i};"
go-ycsb load mysql -P tests/$TEST_NAME/workload -p mysql.host=$TIDB_IP -p mysql.port=$TIDB_PORT -p mysql.user=root -p mysql.db=$DB${i}
done

for i in $(seq $DB_COUNT); do
row_count_ori[${i}]=$(run_sql "SELECT COUNT(*) FROM $DB${i}.$TABLE;" | awk '/COUNT/{print $2}')
done

# backup full
br --pd $PD_ADDR backup full -s "local://$TEST_DIR/$DB/backupdata" --ratelimit 100 --concurrency 4

for i in $(seq $DB_COUNT); do
run_sql "DROP DATABASE $DB${i};"
done

# restore full
br restore full --connect "root@tcp($TIDB_ADDR)/" --importer $IMPORTER_ADDR --meta backupmeta --status $TIDB_IP:10080 --pd $PD_ADDR

for i in $(seq $DB_COUNT); do
row_count_new[${i}]=$(run_sql "SELECT COUNT(*) FROM $DB${i}.$TABLE;" | awk '/COUNT/{print $2}')
done

fail=false
for i in $(seq $DB_COUNT); do
if [ "${row_count_ori[i]}" != "${row_count_new[i]}" ];then
fail=true
echo "TEST: [$TEST_NAME] fail on database $DB${i}"
fi
echo "[original] row count: ${row_count_ori[i]}, [after br] row count: ${row_count_new[i]}"
done

if $fail; then
echo "TEST: [$TEST_NAME] failed!"
exit 1
else
echo "TEST: [$TEST_NAME] successed!"
fi
12 changes: 12 additions & 0 deletions tests/br_full/workload
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
recordcount=2000
operationcount=0
workload=core

readallfields=true

readproportion=0
updateproportion=0
scanproportion=0
insertproportion=0

requestdistribution=uniform
49 changes: 49 additions & 0 deletions tests/br_shuffle_leader/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/bin/sh
#
# Copyright 2019 PingCAP, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# See the License for the specific language governing permissions and
# limitations under the License.

set -eu
DB="$TEST_NAME"
TABLE="usertable"

run_sql "CREATE DATABASE $DB;"

go-ycsb load mysql -P tests/$TEST_NAME/workload -p mysql.host=$TIDB_IP -p mysql.port=$TIDB_PORT -p mysql.user=root -p mysql.db=$DB

row_count_ori=$(run_sql "SELECT COUNT(*) FROM $DB.$TABLE;" | awk '/COUNT/{print $2}')

# add shuffle leader scheduler
pd-ctl -u "http://$PD_ADDR" -d sched add shuffle-leader-scheduler

# backup with shuffle leader
br --pd $PD_ADDR backup table -s "local://$TEST_DIR/$DB/backupdata" --db $DB -t $TABLE --ratelimit 100 --concurrency 4

run_sql "DELETE FROM $DB.$TABLE;"

# restore with shuffle leader
br restore table --db $DB --table $TABLE --connect "root@tcp($TIDB_ADDR)/" --importer $IMPORTER_ADDR --meta backupmeta --status $TIDB_IP:10080 --pd $PD_ADDR

# remove shuffle leader scheduler
pd-ctl -u "http://$PD_ADDR" -d sched remove shuffle-leader-scheduler

row_count_new=$(run_sql "SELECT COUNT(*) FROM $DB.$TABLE;" | awk '/COUNT/{print $2}')

echo "[original] row count: $row_count_ori, [after br] row count: $row_count_new"

if [ "$row_count_ori" -eq "$row_count_new" ];then
echo "TEST: [$TEST_NAME] successed!"
else
echo "TEST: [$TEST_NAME] failed!"
exit 1
fi
12 changes: 12 additions & 0 deletions tests/br_shuffle_leader/workload
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
recordcount=2000
operationcount=0
workload=core

readallfields=true

readproportion=0
updateproportion=0
scanproportion=0
insertproportion=0

requestdistribution=uniform
49 changes: 49 additions & 0 deletions tests/br_shuffle_region/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/bin/sh
#
# Copyright 2019 PingCAP, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# See the License for the specific language governing permissions and
# limitations under the License.

set -eu
DB="$TEST_NAME"
TABLE="usertable"

run_sql "CREATE DATABASE $DB;"

go-ycsb load mysql -P tests/$TEST_NAME/workload -p mysql.host=$TIDB_IP -p mysql.port=$TIDB_PORT -p mysql.user=root -p mysql.db=$DB

row_count_ori=$(run_sql "SELECT COUNT(*) FROM $DB.$TABLE;" | awk '/COUNT/{print $2}')

# add shuffle region scheduler
pd-ctl -u "http://$PD_ADDR" -d sched add shuffle-region-scheduler

# backup with shuffle region
br --pd $PD_ADDR backup table -s "local://$TEST_DIR/$DB/backupdata" --db $DB -t $TABLE --ratelimit 100 --concurrency 4

run_sql "DELETE FROM $DB.$TABLE;"

# restore with shuffle region
br restore table --db $DB --table $TABLE --connect "root@tcp($TIDB_ADDR)/" --importer $IMPORTER_ADDR --meta backupmeta --status $TIDB_IP:10080 --pd $PD_ADDR

# remove shuffle region scheduler
pd-ctl -u "http://$PD_ADDR" -d sched remove shuffle-region-scheduler

row_count_new=$(run_sql "SELECT COUNT(*) FROM $DB.$TABLE;" | awk '/COUNT/{print $2}')

echo "[original] row count: $row_count_ori, [after br] row count: $row_count_new"

if [ "$row_count_ori" -eq "$row_count_new" ];then
echo "TEST: [$TEST_NAME] successed!"
else
echo "TEST: [$TEST_NAME] failed!"
exit 1
fi
12 changes: 12 additions & 0 deletions tests/br_shuffle_region/workload
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
recordcount=2000
operationcount=0
workload=core

readallfields=true

readproportion=0
updateproportion=0
scanproportion=0
insertproportion=0

requestdistribution=uniform
43 changes: 43 additions & 0 deletions tests/br_single_table/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/bin/sh
#
# Copyright 2019 PingCAP, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# See the License for the specific language governing permissions and
# limitations under the License.

set -eu
DB="$TEST_NAME"
TABLE="usertable"

run_sql "CREATE DATABASE $DB;"

go-ycsb load mysql -P tests/$TEST_NAME/workload -p mysql.host=$TIDB_IP -p mysql.port=$TIDB_PORT -p mysql.user=root -p mysql.db=$DB

row_count_ori=$(run_sql "SELECT COUNT(*) FROM $DB.$TABLE;" | awk '/COUNT/{print $2}')

# backup table
br --pd $PD_ADDR backup table -s "local://$TEST_DIR/$DB/backupdata" --db $DB -t $TABLE --ratelimit 100 --concurrency 4

run_sql "DELETE FROM $DB.$TABLE;"

# restore table
br restore table --db $DB --table $TABLE --connect "root@tcp($TIDB_ADDR)/" --importer $IMPORTER_ADDR --meta backupmeta --status $TIDB_IP:10080 --pd $PD_ADDR

row_count_new=$(run_sql "SELECT COUNT(*) FROM $DB.$TABLE;" | awk '/COUNT/{print $2}')

echo "[original] row count: $row_count_ori, [after br] row count: $row_count_new"

if [ "$row_count_ori" -eq "$row_count_new" ];then
echo "TEST: [$TEST_NAME] successed!"
else
echo "TEST: [$TEST_NAME] failed!"
exit 1
fi
12 changes: 12 additions & 0 deletions tests/br_single_table/workload
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
recordcount=2000
operationcount=0
workload=core

readallfields=true

readproportion=0
updateproportion=0
scanproportion=0
insertproportion=0

requestdistribution=uniform
6 changes: 6 additions & 0 deletions tests/config/tidb.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# config of tidb

# Schema lease duration
# There are lot of ddl in the tests, setting this
# to 0s to accelerate the tests
lease = "0s"
5 changes: 5 additions & 0 deletions tests/config/tikv.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# config of tikv

[coprocessor]
region-max-keys = 20
region-split-keys = 12
Loading

0 comments on commit 501e3c9

Please sign in to comment.