github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/tests/integration_tests/simple/run.sh (about)

     1  #!/bin/bash
     2  
     3  set -eu
     4  
     5  CUR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
     6  source $CUR/../_utils/test_prepare
     7  WORK_DIR=$OUT_DIR/$TEST_NAME
     8  CDC_BINARY=cdc.test
     9  SINK_TYPE=$1
    10  
    11  function prepare() {
    12  	rm -rf $WORK_DIR && mkdir -p $WORK_DIR
    13  
    14  	start_tidb_cluster --workdir $WORK_DIR
    15  
    16  	cd $WORK_DIR
    17  
    18  	# record tso before we create tables to skip the system table DDLs
    19  	start_ts=$(run_cdc_cli_tso_query ${UP_PD_HOST_1} ${UP_PD_PORT_1})
    20  
    21  	run_sql "CREATE table test.simple1(id int primary key, val int);"
    22  	run_sql "CREATE table test.simple2(id int primary key, val int);"
    23  
    24  	run_cdc_server --workdir $WORK_DIR --binary $CDC_BINARY
    25  
    26  	TOPIC_NAME="ticdc-simple-test-$RANDOM"
    27  	case $SINK_TYPE in
    28  	kafka) SINK_URI="kafka+ssl://127.0.0.1:9092/$TOPIC_NAME?protocol=open-protocol&partition-num=4&kafka-client-id=cdc_test_simple&kafka-version=${KAFKA_VERSION}&max-message-bytes=10485760" ;;
    29  	storage) SINK_URI="file://$WORK_DIR/storage_test/$TOPIC_NAME?protocol=canal-json&enable-tidb-extension=true" ;;
    30  	pulsar)
    31  		run_pulsar_cluster $WORK_DIR normal
    32  		SINK_URI="pulsar://127.0.0.1:6650/$TOPIC_NAME?protocol=canal-json&enable-tidb-extension=true"
    33  		;;
    34  	*) SINK_URI="mysql+ssl://normal:123456@127.0.0.1:3306/" ;;
    35  	esac
    36  	run_cdc_cli changefeed create --start-ts=$start_ts --sink-uri="$SINK_URI"
    37  	case $SINK_TYPE in
    38  	kafka) run_kafka_consumer $WORK_DIR "kafka://127.0.0.1:9092/$TOPIC_NAME?protocol=open-protocol&partition-num=4&version=${KAFKA_VERSION}&max-message-bytes=10485760" ;;
    39  	storage) run_storage_consumer $WORK_DIR $SINK_URI "" "" ;;
    40  	pulsar) run_pulsar_consumer --upstream-uri $SINK_URI ;;
    41  	esac
    42  }
    43  
    44  function sql_check() {
    45  	# run check in sequence and short circuit principle, if error hanppens,
    46  	# the following statement will be not executed
    47  
    48  	# check table simple1.
    49  	run_sql "SELECT id, val FROM test.simple1;" ${DOWN_TIDB_HOST} ${DOWN_TIDB_PORT} &&
    50  		check_contains "id: 1" &&
    51  		check_contains "val: 1" &&
    52  		check_contains "id: 2" &&
    53  		check_contains "val: 22" &&
    54  		check_not_contains "id: 3" &&
    55  
    56  		# check table simple2.
    57  		run_sql "SELECT id, val FROM test.simple2;" ${DOWN_TIDB_HOST} ${DOWN_TIDB_PORT} &&
    58  		check_contains "id: 1" &&
    59  		check_contains "val: 1" &&
    60  		check_contains "id: 2" &&
    61  		check_contains "val: 22" &&
    62  		check_not_contains "id: 3"
    63  }
    64  
    65  function sql_test() {
    66  	# test insert/update/delete for two table in the same way.
    67  	run_sql "INSERT INTO test.simple1(id, val) VALUES (1, 1);"
    68  	run_sql "INSERT INTO test.simple1(id, val) VALUES (2, 2);"
    69  	run_sql "INSERT INTO test.simple1(id, val) VALUES (3, 3);"
    70  
    71  	# update id = 2 and delete id = 3
    72  	run_sql "UPDATE test.simple1 set val = 22 where id = 2;"
    73  	run_sql "DELETE from test.simple1 where id = 3;"
    74  
    75  	# same dml for table simple2
    76  	run_sql "INSERT INTO test.simple2(id, val) VALUES (1, 1);"
    77  	run_sql "INSERT INTO test.simple2(id, val) VALUES (2, 2);"
    78  	run_sql "INSERT INTO test.simple2(id, val) VALUES (3, 3);"
    79  
    80  	run_sql "UPDATE test.simple2 set val = 22 where id = 2;"
    81  	run_sql "DELETE from test.simple2 where id = 3;"
    82  
    83  	i=0
    84  	check_time=50
    85  	set +e
    86  	while [ $i -lt $check_time ]; do
    87  		sql_check
    88  		ret=$?
    89  		if [ "$ret" == 0 ]; then
    90  			echo "check data successfully"
    91  			break
    92  		fi
    93  		((i++))
    94  		echo "check data failed $i-th time, retry later"
    95  		sleep 2
    96  	done
    97  	set -e
    98  
    99  	if [ $i -ge $check_time ]; then
   100  		echo "check data failed at last"
   101  		exit 1
   102  	fi
   103  
   104  	cleanup_process $CDC_BINARY
   105  }
   106  
   107  function region_label_test() {
   108  	i=0
   109  	while [ -z "$(curl -X GET http://127.0.0.1:2379/pd/api/v1/config/region-label/rules 2>/dev/null | grep 'meta')" ]; do
   110  		i=$((i + 1))
   111  		if [ "$i" -gt 5 ]; then
   112  			echo 'Failed to verify meta region labels'
   113  			exit 1
   114  		fi
   115  		sleep 1
   116  	done
   117  	echo 'succeed to verify meta placement rules'
   118  }
   119  
   120  trap stop_tidb_cluster EXIT
   121  prepare $*
   122  region_label_test $*
   123  sql_test $*
   124  check_logs $WORK_DIR
   125  echo "[$(date)] <<<<<< run test case $TEST_NAME success! >>>>>>"