github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/dm/tests/openapi/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  export PATH=$PATH:$cur/client/
     8  WORK_DIR=$TEST_DIR/$TEST_NAME
     9  
    10  function prepare_database() {
    11  	run_sql_source1 'DROP DATABASE if exists openapi;'
    12  	run_sql_source1 'CREATE DATABASE openapi;'
    13  
    14  	run_sql_source2 'DROP DATABASE if exists openapi;'
    15  	run_sql_source2 'CREATE DATABASE openapi;'
    16  }
    17  
    18  function init_noshard_data() {
    19  
    20  	run_sql_source1 "CREATE TABLE openapi.t1(i TINYINT, j INT UNIQUE KEY);"
    21  	run_sql_source1 "INSERT INTO openapi.t1(i,j) VALUES (1, 2);"
    22  
    23  	run_sql_source2 "CREATE TABLE openapi.t2(i TINYINT, j INT UNIQUE KEY);"
    24  	run_sql_source2 "INSERT INTO openapi.t2(i,j) VALUES (3, 4);"
    25  }
    26  
    27  function init_shard_data() {
    28  	run_sql_source1 "CREATE TABLE openapi.t(i TINYINT, j INT UNIQUE KEY);"
    29  	run_sql_source2 "CREATE TABLE openapi.t(i TINYINT, j INT UNIQUE KEY);"
    30  
    31  	run_sql_source1 "INSERT INTO openapi.t(i,j) VALUES (1, 2);"
    32  	run_sql_source2 "INSERT INTO openapi.t(i,j) VALUES (3, 4);"
    33  }
    34  
    35  function init_data_with_auto_id() {
    36  	run_sql_source1 "CREATE TABLE openapi.t(id bigint primary key auto_increment, i TINYINT, j INT UNIQUE KEY);"
    37  	run_sql_source2 "CREATE TABLE openapi.t(id bigint primary key auto_increment, i TINYINT, j INT UNIQUE KEY);"
    38  
    39  	run_sql_source1 "INSERT INTO openapi.t(i,j) VALUES (1, 2);"
    40  	run_sql_source2 "INSERT INTO openapi.t(i,j) VALUES (3, 4);"
    41  }
    42  
    43  function init_data_with_diff_column() {
    44  	run_sql_source1 "CREATE TABLE openapi.t(id bigint, i TINYINT, j INT UNIQUE KEY);"
    45  	run_sql_source2 "CREATE TABLE openapi.t(i TINYINT, j INT UNIQUE KEY);"
    46  
    47  	run_sql_source1 "INSERT INTO openapi.t(i,j) VALUES (1, 2);"
    48  	run_sql_source2 "INSERT INTO openapi.t(i,j) VALUES (3, 4);"
    49  }
    50  
    51  function clean_cluster_sources_and_tasks() {
    52  	openapi_source_check "delete_source_with_force_success" "mysql-01"
    53  	openapi_source_check "delete_source_with_force_success" "mysql-02"
    54  	openapi_source_check "list_source_success" 0
    55  	openapi_task_check "get_task_list" 0
    56  	run_sql_tidb "DROP DATABASE if exists openapi;"
    57  }
    58  
    59  function test_source() {
    60  	echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>START TEST OPENAPI: SOURCE"
    61  	prepare_database
    62  	# create source successfully
    63  	openapi_source_check "create_source1_success"
    64  
    65  	# recreate source will failed
    66  	openapi_source_check "create_source_failed"
    67  
    68  	# update source1 without password success
    69  	openapi_source_check "update_source1_without_password_success"
    70  
    71  	# get source list success
    72  	openapi_source_check "list_source_success" 1
    73  
    74  	# get source list with status
    75  	openapi_source_check "list_source_with_status_success" 1 1
    76  
    77  	# transfer source
    78  	openapi_source_check "transfer_source_success" "mysql-01" "worker2"
    79  	run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
    80  		"query-status -s mysql-01" \
    81  		"\"worker\": \"worker2\"" 1
    82  
    83  	init_noshard_data # init table in database openapi for test get schema and table
    84  
    85  	# test get source schemas and tables
    86  	openapi_source_check "get_source_schemas_and_tables_success" "mysql-01" "openapi" "t1"
    87  
    88  	# test the db name that must be quoted are working properly
    89  	must_quote_db_name="\`db-name\`"
    90  	run_sql_source1 "create database if not exists $must_quote_db_name"
    91  	run_sql_source1 "create table $must_quote_db_name.t1 (i TINYINT, j INT UNIQUE KEY)"
    92  	openapi_source_check "get_source_schemas_and_tables_success" "mysql-01" "db-name" "t1"
    93  	run_sql_source1 "drop database $must_quote_db_name"
    94  
    95  	# delete source success
    96  	openapi_source_check "delete_source_success" "mysql-01"
    97  
    98  	# after delete source, source list should be empty
    99  	openapi_source_check "list_source_success" 0
   100  
   101  	# re delete source failed
   102  	openapi_source_check "delete_source_failed" "mysql-01"
   103  
   104  	# send request to not leader node
   105  	openapi_source_check "list_source_with_reverse" 0
   106  
   107  	echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>TEST OPENAPI: SOURCE SUCCESS"
   108  }
   109  
   110  function test_relay() {
   111  	echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>START TEST OPENAPI: RELAY"
   112  	prepare_database
   113  	# create source successfully
   114  	openapi_source_check "create_source1_success"
   115  
   116  	# we need make sure that source is bound by worker1 because we will start relay on worker1
   117  	openapi_source_check "transfer_source_success" "mysql-01" "worker1"
   118  
   119  	run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   120  		"query-status -s mysql-01" \
   121  		"\"result\": true" 2 \
   122  		"\"worker\": \"worker1\"" 1
   123  
   124  	# start relay failed
   125  	openapi_source_check "enable_relay_failed" "mysql-01" "no-worker"
   126  
   127  	# enable relay success
   128  	openapi_source_check "enable_relay_success" "mysql-01" "worker1"
   129  	run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   130  		"query-status -s mysql-01" \
   131  		"\"worker\": \"worker1\"" 1 \
   132  		"\"relayCatchUpMaster\": true" 1
   133  
   134  	# get source status failed
   135  	openapi_source_check "get_source_status_failed" "no-mysql"
   136  
   137  	# get source status success
   138  	openapi_source_check "get_source_status_success" "mysql-01"
   139  	openapi_source_check "get_source_status_success_with_relay" "mysql-01"
   140  
   141  	# disable relay failed: not pass worker name
   142  	openapi_source_check "disable_relay_failed" "mysql-01" "no-worker"
   143  
   144  	# purge relay success
   145  	openapi_source_check "purge_relay_success" "mysql-01" "mysql-bin.000001"
   146  
   147  	# disable relay success
   148  	openapi_source_check "disable_relay_success" "mysql-01" "worker1"
   149  
   150  	run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   151  		"query-status -s mysql-01" \
   152  		"\"worker\": \"worker1\"" 1 \
   153  		"\"relayStatus\": null" 1
   154  
   155  	openapi_source_check "get_source_status_success_no_relay" "mysql-01"
   156  
   157  	openapi_source_check "enable_relay_success_with_two_worker" "mysql-01" "worker1" "worker2"
   158  	openapi_source_check "list_source_with_status_success" 1 2               # source 1 status_list will have two items
   159  	openapi_source_check "get_source_status_success" "mysql-01" 2            # have two source status
   160  	openapi_source_check "get_source_status_success_with_relay" "mysql-01" 0 # check worker1 relay status
   161  	openapi_source_check "get_source_status_success_with_relay" "mysql-01" 1 # check worker2 relay status
   162  
   163  	run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   164  		"query-status -s mysql-01" \
   165  		"\"worker\": \"worker1\"" 1 \
   166  		"\"worker\": \"worker2\"" 1
   167  
   168  	# stop relay on two worker success
   169  	openapi_source_check "disable_relay_success" "mysql-01" "worker1"
   170  	openapi_source_check "disable_relay_success" "mysql-01" "worker2"
   171  
   172  	# delete source success
   173  	openapi_source_check "delete_source_success" "mysql-01"
   174  
   175  	echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>TEST OPENAPI: RELAY SUCCESS"
   176  
   177  }
   178  
   179  function test_shard_task() {
   180  	echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>START TEST OPENAPI: SHARD TASK"
   181  	prepare_database
   182  
   183  	task_name="test-shard"
   184  
   185  	# create source successfully
   186  	openapi_source_check "create_source1_success"
   187  	openapi_source_check "list_source_success" 1
   188  	# get source status success
   189  	openapi_source_check "get_source_status_success" "mysql-01"
   190  
   191  	# create source successfully
   192  	openapi_source_check "create_source2_success"
   193  	# get source list success
   194  	openapi_source_check "list_source_success" 2
   195  	# get source status success
   196  	openapi_source_check "get_source_status_success" "mysql-02"
   197  
   198  	# create task success: not valid task create request
   199  	openapi_task_check "create_task_failed"
   200  
   201  	# create success
   202  	openapi_task_check "create_shard_task_success"
   203  	run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   204  		"query-status $task_name" \
   205  		"\"stage\": \"Stopped\"" 2
   206  
   207  	# start success
   208  	openapi_task_check "start_task_success" $task_name ""
   209  	run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   210  		"query-status $task_name" \
   211  		"\"stage\": \"Running\"" 2
   212  
   213  	init_shard_data
   214  	check_sync_diff $WORK_DIR $cur/conf/diff_config_shard.toml
   215  
   216  	# test binlog event filter, this delete will ignored in source-1
   217  	run_sql_source1 "DELETE FROM openapi.t;"
   218  	run_sql_tidb_with_retry "select count(1) from openapi.t;" "count(1): 2"
   219  
   220  	# test binlog event filter, this ddl will be ignored in source-2
   221  	run_sql "alter table openapi.t add column aaa int;" $MYSQL_PORT2 $MYSQL_PASSWORD2
   222  	# ddl will be ignored, so no ddl locks and the task will work normally.
   223  	run_sql "INSERT INTO openapi.t(i,j) VALUES (5, 5);" $MYSQL_PORT1 $MYSQL_PASSWORD1
   224  	run_sql_tidb_with_retry "select count(1) from openapi.t;" "count(1): 3"
   225  
   226  	# get task status failed
   227  	openapi_task_check "get_task_status_failed" "not a task name"
   228  
   229  	# get illegal char task_status failed
   230  	openapi_task_check get_illegal_char_task_status_failed
   231  
   232  	# get task status success
   233  	openapi_task_check "get_task_status_success" "$task_name" 2
   234  
   235  	# get task list
   236  	openapi_task_check "get_task_list" 1
   237  
   238  	# stop task success
   239  	openapi_task_check "stop_task_success" "$task_name" ""
   240  
   241  	openapi_task_check "get_task_list" 1
   242  
   243  	# delete task success
   244  	openapi_task_check "delete_task_success" "$task_name"
   245  
   246  	# get task list
   247  	openapi_task_check "get_task_list" 0
   248  
   249  	# delete source success
   250  	clean_cluster_sources_and_tasks
   251  	echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>TEST OPENAPI: SHARD TASK SUCCESS"
   252  
   253  }
   254  
   255  function test_noshard_task() {
   256  	echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>START TEST OPENAPI: NO SHARD TASK"
   257  	prepare_database
   258  
   259  	task_name="test-no-shard"
   260  	target_table_name="" # empty means no route
   261  
   262  	# create source successfully
   263  	openapi_source_check "create_source1_success"
   264  	openapi_source_check "list_source_success" 1
   265  
   266  	# get source status success
   267  	openapi_source_check "get_source_status_success" "mysql-01"
   268  
   269  	# create source successfully
   270  	openapi_source_check "create_source2_success"
   271  	# get source list success
   272  	openapi_source_check "list_source_success" 2
   273  
   274  	# get source status success
   275  	openapi_source_check "get_source_status_success" "mysql-02"
   276  
   277  	# create no shard task success
   278  	openapi_task_check "create_noshard_task_success" $task_name $target_table_name
   279  	run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   280  		"query-status $task_name" \
   281  		"\"stage\": \"Stopped\"" 2
   282  
   283  	openapi_task_check "start_task_success" $task_name ""
   284  	run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   285  		"query-status $task_name" \
   286  		"\"stage\": \"Running\"" 2
   287  	init_noshard_data
   288  	check_sync_diff $WORK_DIR $cur/conf/diff_config_no_shard.toml
   289  
   290  	# get task status failed
   291  	openapi_task_check "get_task_status_failed" "not a task name"
   292  
   293  	# get task status success
   294  	openapi_task_check "get_task_status_success" "$task_name" 2
   295  
   296  	# delete source with force
   297  	openapi_source_check "delete_source_with_force_success" "mysql-01"
   298  
   299  	# after delete source-1, there is only one subtask status
   300  	openapi_task_check "get_task_status_success" "$task_name" 1
   301  
   302  	# get task list
   303  	openapi_task_check "get_task_list" 1
   304  
   305  	# stop task first for operate schema
   306  	openapi_task_check "stop_task_success" "$task_name" "mysql-02"
   307  
   308  	# operate schema
   309  	openapi_task_check "operate_schema_and_table_success" "$task_name" "mysql-02" "openapi" "t2"
   310  
   311  	# start task again
   312  	openapi_task_check "start_task_success" "$task_name" "mysql-02"
   313  
   314  	# delete task failed because there is a running task
   315  	openapi_task_check "delete_task_failed" "$task_name"
   316  
   317  	# delete task success with force
   318  	openapi_task_check "delete_task_with_force_success" "$task_name"
   319  
   320  	openapi_task_check "get_task_list" 0
   321  
   322  	# delete source success
   323  	openapi_source_check "delete_source_success" "mysql-02"
   324  	openapi_source_check "list_source_success" 0
   325  	run_sql_tidb "DROP DATABASE if exists openapi;"
   326  	echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>TEST OPENAPI: NO SHARD TASK SUCCESS"
   327  }
   328  
   329  function test_complex_operations_of_source_and_task() {
   330  	echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>START TEST OPENAPI: COMPLEX OPERATION"
   331  	prepare_database
   332  
   333  	task_name="test-complex"
   334  	target_table_name="" # empty means no route
   335  
   336  	# create source successfully
   337  	openapi_source_check "create_source1_success"
   338  	openapi_source_check "list_source_success" 1
   339  	openapi_source_check "create_source2_success"
   340  	openapi_source_check "list_source_success" 2
   341  
   342  	# create and check task
   343  	openapi_task_check "create_noshard_task_success" $task_name $target_table_name
   344  	run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   345  		"query-status $task_name" \
   346  		"\"stage\": \"Stopped\"" 2
   347  	openapi_task_check "get_task_list" 1
   348  
   349  	openapi_task_check "start_task_success" $task_name ""
   350  	run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   351  		"query-status $task_name" \
   352  		"\"stage\": \"Running\"" 2
   353  	init_noshard_data
   354  	check_sync_diff $WORK_DIR $cur/conf/diff_config_no_shard.toml
   355  	openapi_task_check "get_task_status_success" "$task_name" 2
   356  
   357  	# do some complex operations
   358  	openapi_task_check "do_complex_operations" "$task_name"
   359  
   360  	# incr more data
   361  	run_sql_source1 "INSERT INTO openapi.t1(i,j) VALUES (3,4);"
   362  	run_sql_source2 "INSERT INTO openapi.t2(i,j) VALUES (5,6);"
   363  	check_sync_diff $WORK_DIR $cur/conf/diff_config_no_shard.toml
   364  
   365  	clean_cluster_sources_and_tasks
   366  	echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>TEST OPENAPI: COMPLEX OPERATION SUCCESS"
   367  }
   368  
   369  function test_multi_tasks() {
   370  	echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>START TEST OPENAPI: MULTI TASK"
   371  	prepare_database
   372  
   373  	task1="test-1"
   374  	task1_target_table_name="task1_target_table"
   375  
   376  	task2="test-2"
   377  	task2_target_table_name="task2_target_table"
   378  
   379  	# create and check source
   380  	openapi_source_check "create_source1_success"
   381  	openapi_source_check "create_source2_success"
   382  	openapi_source_check "list_source_success" 2
   383  
   384  	init_noshard_data
   385  
   386  	openapi_task_check "create_noshard_task_success" $task1 $task1_target_table_name
   387  	run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   388  		"query-status $task1" \
   389  		"\"stage\": \"Stopped\"" 2
   390  
   391  	openapi_task_check "start_task_success" $task1 ""
   392  	run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   393  		"query-status $task1" \
   394  		"\"stage\": \"Running\"" 2
   395  
   396  	# test get task list with status, now we have 1 task with two status
   397  	openapi_task_check "get_task_list_with_status" 1 $task1 2
   398  
   399  	openapi_task_check "create_noshard_task_success" $task2 $task2_target_table_name
   400  	run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   401  		"query-status $task2" \
   402  		"\"stage\": \"Stopped\"" 2
   403  
   404  	openapi_task_check "start_task_success" $task2 ""
   405  	run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   406  		"query-status $task2" \
   407  		"\"stage\": \"Running\"" 2
   408  
   409  	# now we have 2 task and each one has two status
   410  	openapi_task_check "get_task_list_with_status" 2 $task2 2
   411  
   412  	# delete source success and clean data for other test
   413  	clean_cluster_sources_and_tasks
   414  	echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>TEST OPENAPI: MULTI TASK SUCCESS"
   415  
   416  }
   417  
   418  function test_task_templates() {
   419  	echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>START TEST OPENAPI: TASK TEMPLATES"
   420  	prepare_database
   421  
   422  	task_name="test-1"
   423  
   424  	# create and check source
   425  	openapi_source_check "create_source1_success"
   426  	openapi_source_check "create_source2_success"
   427  	openapi_source_check "list_source_success" 2
   428  
   429  	# crud task template
   430  	openapi_task_check "create_task_template_failed"
   431  	openapi_task_check "create_task_template_success" $task_name
   432  	openapi_task_check "list_task_template" 1
   433  	openapi_task_check "get_task_template" $task_name
   434  	openapi_task_check "update_task_template_success" $task_name "full"
   435  	openapi_task_check "delete_task_template" $task_name
   436  	openapi_task_check "list_task_template" 0
   437  
   438  	# import from tasks and get from dmctl
   439  	init_noshard_data
   440  
   441  	openapi_task_check "create_noshard_task_success" $task_name
   442  	run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   443  		"query-status $task_name" \
   444  		"\"stage\": \"Stopped\"" 2
   445  	openapi_task_check "start_task_success" $task_name ""
   446  	run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   447  		"query-status $task_name" \
   448  		"\"stage\": \"Running\"" 2
   449  
   450  	openapi_task_check "import_task_template" 1 0
   451  	openapi_task_check "list_task_template" 1
   452  
   453  	run_dm_ctl $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   454  		"config task $task_name --path $WORK_DIR/get_task_from_task.yaml" \
   455  		"\"result\": true" 1
   456  
   457  	run_dm_ctl $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   458  		"config task-template $task_name --path $WORK_DIR/get_task_from_task_template.yaml" \
   459  		"\"result\": true" 1
   460  
   461  	diff $WORK_DIR/get_task_from_task.yaml $WORK_DIR/get_task_from_task_template.yaml || exit 1
   462  
   463  	# delete source success and clean data for other test
   464  	clean_cluster_sources_and_tasks
   465  	echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>TEST OPENAPI: TASK TEMPLATES"
   466  }
   467  
   468  function test_noshard_task_dump_status() {
   469  	echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>START TEST OPENAPI: NO SHARD TASK DUMP STATUS"
   470  
   471  	export GO_FAILPOINTS="github.com/pingcap/tiflow/dm/dumpling/dumpUnitProcessForever=return()"
   472  	kill_dm_worker
   473  	check_port_offline $WORKER1_PORT 20
   474  	check_port_offline $WORKER2_PORT 20
   475  
   476  	# run dm-worker1
   477  	run_dm_worker $WORK_DIR/worker1 $WORKER1_PORT $cur/conf/dm-worker1.toml
   478  	check_rpc_alive $cur/../bin/check_worker_online 127.0.0.1:$WORKER1_PORT
   479  	# run dm-worker2
   480  	run_dm_worker $WORK_DIR/worker2 $WORKER2_PORT $cur/conf/dm-worker2.toml
   481  	check_rpc_alive $cur/../bin/check_worker_online 127.0.0.1:$WORKER2_PORT
   482  
   483  	prepare_database
   484  
   485  	task_name="test-no-shard-dump-status"
   486  	target_table_name="" # empty means no route
   487  
   488  	openapi_source_check "create_source1_success"
   489  	openapi_source_check "list_source_success" 1
   490  	openapi_source_check "get_source_status_success" "mysql-01"
   491  	openapi_source_check "create_source2_success"
   492  	openapi_source_check "list_source_success" 2
   493  	openapi_source_check "get_source_status_success" "mysql-02"
   494  
   495  	openapi_task_check "create_noshard_task_success" $task_name $target_table_name
   496  	run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   497  		"query-status $task_name" \
   498  		"\"stage\": \"Stopped\"" 2
   499  	openapi_task_check "start_task_success" $task_name ""
   500  	run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   501  		"query-status $task_name" \
   502  		"\"unit\": \"Dump\"" 2 \
   503  		"\"stage\": \"Running\"" 2
   504  
   505  	# check noshard task dump status success
   506  	openapi_task_check "check_noshard_task_dump_status_success" "$task_name" 0
   507  
   508  	kill_dm_worker
   509  	check_port_offline $WORKER1_PORT 20
   510  	check_port_offline $WORKER2_PORT 20
   511  
   512  	openapi_task_check "get_task_status_success_but_worker_meet_error" "$task_name" 2
   513  
   514  	export GO_FAILPOINTS=""
   515  	kill_dm_worker
   516  	check_port_offline $WORKER1_PORT 20
   517  	check_port_offline $WORKER2_PORT 20
   518  
   519  	# run dm-worker1
   520  	run_dm_worker $WORK_DIR/worker1 $WORKER1_PORT $cur/conf/dm-worker1.toml
   521  	check_rpc_alive $cur/../bin/check_worker_online 127.0.0.1:$WORKER1_PORT
   522  	# run dm-worker2
   523  	run_dm_worker $WORK_DIR/worker2 $WORKER2_PORT $cur/conf/dm-worker2.toml
   524  	check_rpc_alive $cur/../bin/check_worker_online 127.0.0.1:$WORKER2_PORT
   525  	openapi_source_check "list_source_success" 2
   526  	clean_cluster_sources_and_tasks
   527  	echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>TEST OPENAPI: NO SHARD TASK DUMP STATUS SUCCESS"
   528  }
   529  
   530  function test_task_with_ignore_check_items() {
   531  	echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>START TEST OPENAPI: TEST TASK WITH IGNORE CHECK ITEMS"
   532  	prepare_database
   533  	init_shard_data
   534  
   535  	# create source successfully
   536  	openapi_source_check "create_source1_success"
   537  	openapi_source_check "list_source_success" 1
   538  
   539  	# get source status success
   540  	openapi_source_check "get_source_status_success" "mysql-01"
   541  
   542  	# create source successfully
   543  	openapi_source_check "create_source2_success"
   544  	# get source list success
   545  	openapi_source_check "list_source_success" 2
   546  
   547  	# get source status success
   548  	openapi_source_check "get_source_status_success" "mysql-02"
   549  
   550  	# no ignore precheck and no warn or error
   551  	task_name="test-no-ignore-no-error"
   552  	ignore_check=""
   553  	is_success="success"
   554  	check_res="pre-check is passed"
   555  	openapi_task_check "create_task_with_precheck" "$task_name" "$ignore_check" "$is_success" "$check_res"
   556  	run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   557  		"query-status $task_name" \
   558  		"\"stage\": \"Stopped\"" 2
   559  	# delete task
   560  	openapi_task_check "delete_task_success" "$task_name"
   561  	openapi_task_check "get_task_list" 0
   562  	run_sql_tidb "DROP DATABASE if exists dm_mata;"
   563  
   564  	# no ignore precheck and has warn
   565  	prepare_database
   566  	init_data_with_auto_id
   567  	task_name="test-no-ignore-has-warn"
   568  	ignore_check=""
   569  	is_success="success"
   570  	check_res="have auto-increment key"
   571  	openapi_task_check "create_task_with_precheck" "$task_name" "$ignore_check" "$is_success" "$check_res"
   572  	run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   573  		"query-status $task_name" \
   574  		"\"stage\": \"Stopped\"" 2
   575  	# delete task
   576  	openapi_task_check "delete_task_success" "$task_name"
   577  	openapi_task_check "get_task_list" 0
   578  	run_sql_tidb "DROP DATABASE if exists dm_mata;"
   579  
   580  	# no ignore precheck and has error
   581  	prepare_database
   582  	init_data_with_diff_column
   583  	task_name="test-no-ignore-has-error"
   584  	ignore_check=""
   585  	is_success="failed"
   586  	check_res="column length mismatch"
   587  	openapi_task_check "create_task_with_precheck" "$task_name" "$ignore_check" "$is_success" "$check_res"
   588  	run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   589  		"query-status $task_name" \
   590  		"\"result\": false" 1
   591  	run_sql_tidb "DROP DATABASE if exists dm_mata;"
   592  
   593  	# # has ignore precheck and no error
   594  	prepare_database
   595  	init_data_with_diff_column
   596  	task_name="test-has-ignore-without-error"
   597  	ignore_check="schema_of_shard_tables"
   598  	is_success="success"
   599  	check_res="pre-check is passed"
   600  	openapi_task_check "create_task_with_precheck" "$task_name" "$ignore_check" "$is_success" "$check_res"
   601  	run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   602  		"query-status $task_name" \
   603  		"\"stage\": \"Stopped\"" 2
   604  	# delete task
   605  	openapi_task_check "delete_task_success" "$task_name"
   606  	openapi_task_check "get_task_list" 0
   607  
   608  	clean_cluster_sources_and_tasks
   609  	echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>TEST OPENAPI: TEST TASK WITH IGNORE CHECK ITEMS SUCCESS"
   610  }
   611  
   612  function test_delete_task_with_stopped_downstream() {
   613  	echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>START TEST OPENAPI: DELETE TASK WITH STOPPED DOWNSTREAM"
   614  	prepare_database
   615  
   616  	task_name="test-no-shard"
   617  	target_table_name="" # empty means no route
   618  
   619  	# create source successfully
   620  	openapi_source_check "create_source1_success"
   621  
   622  	# create source successfully
   623  	openapi_source_check "create_source2_success"
   624  	# get source list success
   625  	openapi_source_check "list_source_success" 2
   626  
   627  	# create no shard task success
   628  	openapi_task_check "create_noshard_task_success" $task_name $target_table_name
   629  	run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   630  		"query-status $task_name" \
   631  		"\"stage\": \"Stopped\"" 2
   632  
   633  	# stop downstream
   634  	cleanup_tidb_server
   635  
   636  	# delete task failed because downstream is stopped.
   637  	openapi_task_check "delete_task_failed" "$task_name"
   638  
   639  	# delete task success with force
   640  	openapi_task_check "delete_task_with_force_success" "$task_name"
   641  	openapi_task_check "get_task_list" 0
   642  
   643  	# restart downstream
   644  	run_tidb_server 4000 $TIDB_PASSWORD
   645  	sleep 2
   646  	clean_cluster_sources_and_tasks
   647  	echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>TEST OPENAPI: DELETE TASK WITH STOPPED DOWNSTREAM SUCCESS"
   648  }
   649  
   650  function test_start_task_with_condition() {
   651  	echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>START TEST OPENAPI: START TASK WITH CONDITION"
   652  	prepare_database
   653  	run_sql_tidb "DROP DATABASE if exists openapi;"
   654  
   655  	# create source successfully
   656  	openapi_source_check "create_source1_success"
   657  	openapi_source_check "list_source_success" 1
   658  
   659  	# get source status success
   660  	openapi_source_check "get_source_status_success" "mysql-01"
   661  	# create source successfully
   662  	openapi_source_check "create_source2_success"
   663  	# get source list success
   664  	openapi_source_check "list_source_success" 2
   665  
   666  	# get source status success
   667  	openapi_source_check "get_source_status_success" "mysql-02"
   668  
   669  	# incremental task no source meta and start time, still error
   670  	task_name="incremental_task_no_source_meta"
   671  	run_sql_source1 "CREATE TABLE openapi.t1(i TINYINT, j INT UNIQUE KEY);"
   672  	run_sql_source2 "CREATE TABLE openapi.t2(i TINYINT, j INT UNIQUE KEY);"
   673  
   674  	openapi_task_check "create_incremental_task_with_gtid_success" $task_name "" "" "" "" "" ""
   675  	run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   676  		"query-status $task_name" \
   677  		"\"stage\": \"Stopped\"" 2
   678  
   679  	check_result="must set meta for task-mode incremental"
   680  	openapi_task_check "start_task_failed" $task_name "" "$check_result"
   681  	openapi_task_check "delete_task_with_force_success" "$task_name"
   682  	openapi_task_check "get_task_list" 0
   683  
   684  	# incremental task use gtid
   685  	prepare_database
   686  	run_sql_tidb "DROP DATABASE if exists openapi;"
   687  	task_name="incremental_task_use_gtid"
   688  	run_sql_source1 "CREATE TABLE openapi.t1(i TINYINT, j INT UNIQUE KEY);"
   689  	run_sql_source2 "CREATE TABLE openapi.t2(i TINYINT, j INT UNIQUE KEY);"
   690  
   691  	master_status1=($(get_master_status $MYSQL_HOST1 $MYSQL_PORT1))
   692  	master_status2=($(get_master_status $MYSQL_HOST2 $MYSQL_PORT2))
   693  	openapi_task_check "create_incremental_task_with_gtid_success" $task_name ${master_status1[0]} ${master_status1[1]} ${master_status1[2]} ${master_status2[0]} ${master_status2[1]} ${master_status2[2]}
   694  	run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   695  		"query-status $task_name" \
   696  		"\"stage\": \"Stopped\"" 2
   697  	openapi_task_check "start_task_success" $task_name ""
   698  	run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   699  		"query-status $task_name" \
   700  		"\"stage\": \"Running\"" 2
   701  
   702  	run_sql_tidb 'CREATE DATABASE openapi;'
   703  	run_sql_source1 "CREATE TABLE openapi.t3(i TINYINT, j INT UNIQUE KEY);"
   704  	run_sql_source2 "CREATE TABLE openapi.t4(i TINYINT, j INT UNIQUE KEY);"
   705  
   706  	run_sql_tidb_with_retry "show tables in openapi;" "t3"
   707  	run_sql_tidb_with_retry "show tables in openapi;" "t4"
   708  	run_sql_tidb_with_retry "SELECT count(1) FROM information_schema.tables WHERE table_schema = 'openapi';" "count(1): 2"
   709  
   710  	openapi_task_check "stop_task_success" "$task_name" ""
   711  	openapi_task_check "delete_task_with_force_success" "$task_name"
   712  	openapi_task_check "get_task_list" 0
   713  
   714  	# incremental task use start_time
   715  	prepare_database
   716  	run_sql_tidb "DROP DATABASE if exists openapi;"
   717  	task_name="incremental_task_use_start_time"
   718  	run_sql_source1 "CREATE TABLE openapi.t1(i TINYINT, j INT UNIQUE KEY);"
   719  	run_sql_source2 "CREATE TABLE openapi.t2(i TINYINT, j INT UNIQUE KEY);"
   720  
   721  	openapi_task_check "create_incremental_task_with_gtid_success" $task_name "" "" "" "" "" ""
   722  	run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   723  		"query-status $task_name" \
   724  		"\"stage\": \"Stopped\"" 2
   725  	sleep 2
   726  	start_time=$(TZ='UTC' date '+%Y-%m-%d %T') # mysql 3306 and 3307 is in UTC
   727  	sleep 2
   728  	duration=""
   729  	is_success="success"
   730  	check_result=""
   731  	run_sql_tidb 'CREATE DATABASE openapi;'
   732  	run_sql_source1 "CREATE TABLE openapi.t3(i TINYINT, j INT UNIQUE KEY);"
   733  	run_sql_source2 "CREATE TABLE openapi.t4(i TINYINT, j INT UNIQUE KEY);"
   734  	openapi_task_check "start_task_with_condition" $task_name "$start_time" "$duration" "$is_success" "$check_result"
   735  	run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   736  		"query-status $task_name" \
   737  		"\"stage\": \"Running\"" 2
   738  
   739  	run_sql_tidb_with_retry "show tables in openapi;" "t3"
   740  	run_sql_tidb_with_retry "show tables in openapi;" "t4"
   741  	run_sql_tidb_with_retry "SELECT count(1) FROM information_schema.tables WHERE table_schema = 'openapi';" "count(1): 2"
   742  
   743  	openapi_task_check "stop_task_success" "$task_name" ""
   744  	openapi_task_check "delete_task_with_force_success" "$task_name"
   745  	openapi_task_check "get_task_list" 0
   746  
   747  	# incremental task use start_time, but time is after create table
   748  	prepare_database
   749  	run_sql_tidb "DROP DATABASE if exists openapi;"
   750  	task_name="incremental_task_use_start_time_after_create"
   751  	run_sql_source1 "CREATE TABLE openapi.t1(i TINYINT, j INT UNIQUE KEY);"
   752  	run_sql_source2 "CREATE TABLE openapi.t2(i TINYINT, j INT UNIQUE KEY);"
   753  
   754  	openapi_task_check "create_incremental_task_with_gtid_success" $task_name "" "" "" "" "" ""
   755  	curl http://127.0.0.1:8261/api/v1/tasks/incremental_task_use_start_time_after_create/sources/mysql-01/migrate_targets
   756  	run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   757  		"query-status $task_name" \
   758  		"\"stage\": \"Stopped\"" 2
   759  	sleep 2
   760  	start_time=$(TZ='UTC' date '+%Y-%m-%d %T') # mysql 3306 and 3307 is in UTC
   761  	sleep 2
   762  	duration=""
   763  	is_success="success"
   764  	check_result=""
   765  	run_sql_tidb 'CREATE DATABASE openapi;'
   766  	run_sql_source1 "INSERT INTO openapi.t1(i,j) VALUES (1, 2);"
   767  	run_sql_source2 "INSERT INTO openapi.t2(i,j) VALUES (3, 4);"
   768  	openapi_task_check "start_task_with_condition" $task_name "$start_time" "$duration" "$is_success" "$check_result"
   769  	run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   770  		"query-status $task_name" \
   771  		"Table 'openapi.*' doesn't exist" 2
   772  
   773  	openapi_task_check "stop_task_success" "$task_name" ""
   774  	openapi_task_check "delete_task_with_force_success" "$task_name"
   775  	openapi_task_check "get_task_list" 0
   776  
   777  	# incremental task both gtid and start_time, start_time first
   778  	prepare_database
   779  	run_sql_tidb "DROP DATABASE if exists openapi;"
   780  	task_name="incremental_task_both_gtid_start_time"
   781  	run_sql_source1 "CREATE TABLE openapi.t1(i TINYINT, j INT UNIQUE KEY);"
   782  	run_sql_source2 "CREATE TABLE openapi.t2(i TINYINT, j INT UNIQUE KEY);"
   783  	master_status1=($(get_master_status $MYSQL_HOST1 $MYSQL_PORT1))
   784  	master_status2=($(get_master_status $MYSQL_HOST2 $MYSQL_PORT2))
   785  	openapi_task_check "create_incremental_task_with_gtid_success" $task_name ${master_status1[0]} ${master_status1[1]} ${master_status1[2]} ${master_status2[0]} ${master_status2[1]} ${master_status2[2]}
   786  	run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   787  		"query-status $task_name" \
   788  		"\"stage\": \"Stopped\"" 2
   789  	run_sql_source1 "CREATE TABLE openapi.t3(i TINYINT, j INT UNIQUE KEY);"
   790  	run_sql_source2 "CREATE TABLE openapi.t4(i TINYINT, j INT UNIQUE KEY);"
   791  	sleep 2
   792  	start_time=$(TZ='UTC' date '+%Y-%m-%d %T') # mysql 3306 and 3307 is in UTC
   793  	sleep 2
   794  	duration=""
   795  	is_success="success"
   796  	check_result=""
   797  	run_sql_tidb 'CREATE DATABASE openapi;'
   798  	run_sql_source1 "CREATE TABLE openapi.t5(i TINYINT, j INT UNIQUE KEY);"
   799  	run_sql_source2 "CREATE TABLE openapi.t6(i TINYINT, j INT UNIQUE KEY);"
   800  	openapi_task_check "start_task_with_condition" $task_name "$start_time" "$duration" "$is_success" "$check_result"
   801  	run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   802  		"query-status $task_name" \
   803  		"\"stage\": \"Running\"" 2
   804  
   805  	run_sql_tidb_with_retry "show tables in openapi;" "t5"
   806  	run_sql_tidb_with_retry "show tables in openapi;" "t6"
   807  	run_sql_tidb_with_retry "SELECT count(1) FROM information_schema.tables WHERE table_schema = 'openapi';" "count(1): 2"
   808  
   809  	openapi_task_check "stop_task_success" "$task_name" ""
   810  	openapi_task_check "delete_task_with_force_success" "$task_name"
   811  	openapi_task_check "get_task_list" 0
   812  
   813  	# incremental task no duration has error
   814  	export GO_FAILPOINTS="github.com/pingcap/tiflow/dm/syncer/SafeModeInitPhaseSeconds=return(\"0s\")"
   815  	kill_dm_worker
   816  	check_port_offline $WORKER1_PORT 20
   817  	check_port_offline $WORKER2_PORT 20
   818  
   819  	# run dm-worker1
   820  	run_dm_worker $WORK_DIR/worker1 $WORKER1_PORT $cur/conf/dm-worker1.toml
   821  	check_rpc_alive $cur/../bin/check_worker_online 127.0.0.1:$WORKER1_PORT
   822  	# run dm-worker2
   823  	run_dm_worker $WORK_DIR/worker2 $WORKER2_PORT $cur/conf/dm-worker2.toml
   824  	check_rpc_alive $cur/../bin/check_worker_online 127.0.0.1:$WORKER2_PORT
   825  	openapi_source_check "list_source_success" 2
   826  
   827  	prepare_database
   828  	run_sql_tidb "DROP DATABASE if exists openapi;"
   829  	task_name="incremental_task_no_duration_but_error"
   830  	run_sql_source1 "CREATE TABLE openapi.t1(i TINYINT, j INT UNIQUE KEY);"
   831  	run_sql_source2 "CREATE TABLE openapi.t2(i TINYINT, j INT UNIQUE KEY);"
   832  
   833  	sleep 2
   834  	start_time=$(TZ='UTC' date '+%Y-%m-%d %T') # mysql 3306 and 3307 is in UTC
   835  	sleep 2
   836  	duration=""
   837  	is_success="success"
   838  	check_result=""
   839  
   840  	run_sql_source1 "INSERT INTO openapi.t1(i,j) VALUES (1, 2);"
   841  	run_sql_source2 "INSERT INTO openapi.t2(i,j) VALUES (1, 2);"
   842  	run_sql_source1 "INSERT INTO openapi.t1(i,j) VALUES (3, 4);"
   843  	run_sql_source2 "INSERT INTO openapi.t2(i,j) VALUES (3, 4);"
   844  	# mock already sync data to downstream
   845  	run_sql_tidb 'CREATE DATABASE openapi;'
   846  	run_sql_tidb "CREATE TABLE openapi.t1(i TINYINT, j INT UNIQUE KEY);"
   847  	run_sql_tidb "CREATE TABLE openapi.t2(i TINYINT, j INT UNIQUE KEY);"
   848  	run_sql_tidb "INSERT INTO openapi.t1(i,j) VALUES (1, 2);"
   849  	run_sql_tidb "INSERT INTO openapi.t2(i,j) VALUES (1, 2);"
   850  
   851  	openapi_task_check "create_incremental_task_with_gtid_success" $task_name "" "" "" "" "" ""
   852  	run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   853  		"query-status $task_name" \
   854  		"\"stage\": \"Stopped\"" 2
   855  	openapi_task_check "start_task_with_condition" $task_name "$start_time" "$duration" "$is_success" "$check_result"
   856  	run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   857  		"query-status $task_name" \
   858  		"Duplicate entry" 2
   859  
   860  	# set duration and start again
   861  	openapi_task_check "stop_task_success" "$task_name" ""
   862  	duration="600s"
   863  	openapi_task_check "start_task_with_condition" $task_name "$start_time" "$duration" "$is_success" "$check_result"
   864  
   865  	run_sql_tidb_with_retry "SELECT count(1) FROM openapi.t1;" "count(1): 2"
   866  	run_sql_tidb_with_retry "SELECT count(1) FROM openapi.t2;" "count(1): 2"
   867  
   868  	openapi_task_check "stop_task_success" "$task_name" ""
   869  	openapi_task_check "delete_task_with_force_success" "$task_name"
   870  	openapi_task_check "get_task_list" 0
   871  
   872  	clean_cluster_sources_and_tasks
   873  	echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>TEST OPENAPI: START TASK WITH CONDITION SUCCESS"
   874  }
   875  
   876  function test_stop_task_with_condition() {
   877  	echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>START TEST OPENAPI: STOP TASK WITH CONDITION"
   878  	prepare_database
   879  	run_sql_tidb "DROP DATABASE if exists openapi;"
   880  
   881  	# create source successfully
   882  	openapi_source_check "create_source1_success"
   883  	openapi_source_check "list_source_success" 1
   884  
   885  	# get source status success
   886  	openapi_source_check "get_source_status_success" "mysql-01"
   887  	# create source successfully
   888  	openapi_source_check "create_source2_success"
   889  	# get source list success
   890  	openapi_source_check "list_source_success" 2
   891  
   892  	# get source status success
   893  	openapi_source_check "get_source_status_success" "mysql-02"
   894  
   895  	# test wait_time_on_stop
   896  	export GO_FAILPOINTS='github.com/pingcap/tiflow/dm/syncer/recordAndIgnorePrepareTime=return();github.com/pingcap/tiflow/dm/syncer/checkWaitDuration=return("200s")'
   897  	kill_dm_worker
   898  	check_port_offline $WORKER1_PORT 20
   899  	check_port_offline $WORKER2_PORT 20
   900  
   901  	# run dm-worker1
   902  	run_dm_worker $WORK_DIR/worker1 $WORKER1_PORT $cur/conf/dm-worker1.toml
   903  	check_rpc_alive $cur/../bin/check_worker_online 127.0.0.1:$WORKER1_PORT
   904  	# run dm-worker2
   905  	run_dm_worker $WORK_DIR/worker2 $WORKER2_PORT $cur/conf/dm-worker2.toml
   906  	check_rpc_alive $cur/../bin/check_worker_online 127.0.0.1:$WORKER2_PORT
   907  
   908  	task_name="test_wait_time_on_stop"
   909  	# create no shard task success
   910  	openapi_task_check "create_noshard_task_success" $task_name ""
   911  	run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   912  		"query-status $task_name" \
   913  		"\"stage\": \"Stopped\"" 2
   914  
   915  	timeout_duration="200s"
   916  
   917  	openapi_task_check "start_task_success" $task_name ""
   918  	run_dm_ctl_with_retry $WORK_DIR "127.0.0.1:$MASTER_PORT" \
   919  		"query-status $task_name" \
   920  		"\"stage\": \"Running\"" 2
   921  	init_noshard_data
   922  	check_sync_diff $WORK_DIR $cur/conf/diff_config_no_shard.toml
   923  	openapi_task_check "stop_task_with_condition" "$task_name" "" "$timeout_duration"
   924  	echo "error check"
   925  	check_log_contain_with_retry 'panic: success check wait_time_on_stop !!!' $WORK_DIR/worker1/log/stdout.log
   926  	check_log_contain_with_retry 'panic: success check wait_time_on_stop !!!' $WORK_DIR/worker2/log/stdout.log
   927  
   928  	# clean
   929  	export GO_FAILPOINTS=''
   930  	kill_dm_worker
   931  	check_port_offline $WORKER1_PORT 20
   932  	check_port_offline $WORKER2_PORT 20
   933  
   934  	# run dm-worker1
   935  	run_dm_worker $WORK_DIR/worker1 $WORKER1_PORT $cur/conf/dm-worker1.toml
   936  	check_rpc_alive $cur/../bin/check_worker_online 127.0.0.1:$WORKER1_PORT
   937  	# run dm-worker2
   938  	run_dm_worker $WORK_DIR/worker2 $WORKER2_PORT $cur/conf/dm-worker2.toml
   939  	check_rpc_alive $cur/../bin/check_worker_online 127.0.0.1:$WORKER2_PORT
   940  	openapi_task_check "delete_task_with_force_success" "$task_name"
   941  	openapi_task_check "get_task_list" 0
   942  
   943  	clean_cluster_sources_and_tasks
   944  	echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>TEST OPENAPI: START TASK WITH CONDITION SUCCESS"
   945  }
   946  
   947  function test_reverse_https() {
   948  	echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>START TEST OPENAPI: REVERSE HTTPS"
   949  	cleanup_data openapi
   950  	cleanup_process
   951  
   952  	cp $cur/tls_conf/dm-master1.toml $WORK_DIR/
   953  	cp $cur/tls_conf/dm-master2.toml $WORK_DIR/
   954  	cp $cur/tls_conf/dm-worker1.toml $WORK_DIR/
   955  	cp $cur/tls_conf/dm-worker2.toml $WORK_DIR/
   956  	sed -i "s%dir-placeholer%$cur\/tls_conf%g" $WORK_DIR/dm-master1.toml
   957  	sed -i "s%dir-placeholer%$cur\/tls_conf%g" $WORK_DIR/dm-master2.toml
   958  	sed -i "s%dir-placeholer%$cur\/tls_conf%g" $WORK_DIR/dm-worker1.toml
   959  	sed -i "s%dir-placeholer%$cur\/tls_conf%g" $WORK_DIR/dm-worker2.toml
   960  
   961  	# run dm-master1
   962  	run_dm_master $WORK_DIR/master1 $MASTER_PORT1 $WORK_DIR/dm-master1.toml
   963  	check_rpc_alive $cur/../bin/check_master_online 127.0.0.1:$MASTER_PORT1 "$cur/tls_conf/ca.pem" "$cur/tls_conf/dm.pem" "$cur/tls_conf/dm.key"
   964  	# join master2
   965  	run_dm_master $WORK_DIR/master2 $MASTER_PORT2 $WORK_DIR/dm-master2.toml
   966  	check_rpc_alive $cur/../bin/check_master_online 127.0.0.1:$MASTER_PORT2 "$cur/tls_conf/ca.pem" "$cur/tls_conf/dm.pem" "$cur/tls_conf/dm.key"
   967  	# run dm-worker1
   968  	run_dm_worker $WORK_DIR/worker1 $WORKER1_PORT $WORK_DIR/dm-worker1.toml
   969  	check_rpc_alive $cur/../bin/check_worker_online 127.0.0.1:$WORKER1_PORT "$cur/tls_conf/ca.pem" "$cur/tls_conf/dm.pem" "$cur/tls_conf/dm.key"
   970  	# run dm-worker2
   971  	run_dm_worker $WORK_DIR/worker2 $WORKER2_PORT $WORK_DIR/dm-worker2.toml
   972  	check_rpc_alive $cur/../bin/check_worker_online 127.0.0.1:$WORKER2_PORT "$cur/tls_conf/ca.pem" "$cur/tls_conf/dm.pem" "$cur/tls_conf/dm.key"
   973  
   974  	prepare_database
   975  	# create source successfully
   976  	openapi_source_check "create_source_success_https" "$cur/tls_conf/ca.pem" "$cur/tls_conf/dm.pem" "$cur/tls_conf/dm.key"
   977  
   978  	# get source list success
   979  	openapi_source_check "list_source_success_https" 1 "$cur/tls_conf/ca.pem" "$cur/tls_conf/dm.pem" "$cur/tls_conf/dm.key"
   980  
   981  	# send request to not leader node
   982  	openapi_source_check "list_source_with_reverse_https" 1 "$cur/tls_conf/ca.pem" "$cur/tls_conf/dm.pem" "$cur/tls_conf/dm.key"
   983  
   984  	cleanup_data openapi
   985  	cleanup_process
   986  
   987  	# run dm-master1
   988  	run_dm_master $WORK_DIR/master1 $MASTER_PORT1 $cur/conf/dm-master1.toml
   989  	check_rpc_alive $cur/../bin/check_master_online 127.0.0.1:$MASTER_PORT1
   990  	# join master2
   991  	run_dm_master $WORK_DIR/master2 $MASTER_PORT2 $cur/conf/dm-master2.toml
   992  	check_rpc_alive $cur/../bin/check_master_online 127.0.0.1:$MASTER_PORT2
   993  	# run dm-worker1
   994  	run_dm_worker $WORK_DIR/worker1 $WORKER1_PORT $cur/conf/dm-worker1.toml
   995  	check_rpc_alive $cur/../bin/check_worker_online 127.0.0.1:$WORKER1_PORT
   996  	# run dm-worker2
   997  	run_dm_worker $WORK_DIR/worker2 $WORKER2_PORT $cur/conf/dm-worker2.toml
   998  	check_rpc_alive $cur/../bin/check_worker_online 127.0.0.1:$WORKER2_PORT
   999  
  1000  	echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>TEST OPENAPI: REVERSE HTTPS"
  1001  }
  1002  
  1003  function test_cluster() {
  1004  	# list master and worker node
  1005  	openapi_cluster_check "list_master_success" 2
  1006  
  1007  	openapi_cluster_check "list_worker_success" 2
  1008  
  1009  	# delete master node
  1010  	openapi_cluster_check "delete_master_with_retry_success" "master2"
  1011  	openapi_cluster_check "list_master_success" 1
  1012  
  1013  	# delete worker node failed because of worker is still online
  1014  	openapi_cluster_check "delete_worker_failed" "worker1"
  1015  	kill_dm_worker
  1016  	check_port_offline $WORKER1_PORT 20
  1017  	check_port_offline $WORKER2_PORT 20
  1018  
  1019  	openapi_cluster_check "delete_worker_with_retry_success" "worker1"
  1020  	openapi_cluster_check "list_worker_success" 1
  1021  }
  1022  
  1023  function run() {
  1024  	# run dm-master1
  1025  	run_dm_master $WORK_DIR/master1 $MASTER_PORT1 $cur/conf/dm-master1.toml
  1026  	check_rpc_alive $cur/../bin/check_master_online 127.0.0.1:$MASTER_PORT1
  1027  	# join master2
  1028  	run_dm_master $WORK_DIR/master2 $MASTER_PORT2 $cur/conf/dm-master2.toml
  1029  	check_rpc_alive $cur/../bin/check_master_online 127.0.0.1:$MASTER_PORT2
  1030  	# run dm-worker1
  1031  	run_dm_worker $WORK_DIR/worker1 $WORKER1_PORT $cur/conf/dm-worker1.toml
  1032  	check_rpc_alive $cur/../bin/check_worker_online 127.0.0.1:$WORKER1_PORT
  1033  	# run dm-worker2
  1034  	run_dm_worker $WORK_DIR/worker2 $WORKER2_PORT $cur/conf/dm-worker2.toml
  1035  	check_rpc_alive $cur/../bin/check_worker_online 127.0.0.1:$WORKER2_PORT
  1036  
  1037  	test_relay
  1038  	test_source
  1039  
  1040  	test_shard_task
  1041  	test_multi_tasks
  1042  	test_noshard_task
  1043  	test_task_templates
  1044  	test_noshard_task_dump_status
  1045  	test_complex_operations_of_source_and_task
  1046  	test_task_with_ignore_check_items
  1047  	test_delete_task_with_stopped_downstream
  1048  	test_start_task_with_condition
  1049  	test_stop_task_with_condition
  1050  	test_reverse_https
  1051  
  1052  	# NOTE: this test case MUST running at last, because it will offline some members of cluster
  1053  	test_cluster
  1054  }
  1055  
  1056  cleanup_data openapi
  1057  cleanup_process
  1058  
  1059  run
  1060  
  1061  cleanup_process
  1062  
  1063  echo "[$(date)] <<<<<< test case $TEST_NAME success! >>>>>>"