github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/dm/worker/utils_test.go (about)

     1  // Copyright 2022 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package worker
    15  
    16  import (
    17  	"testing"
    18  
    19  	"github.com/pingcap/tiflow/dm/config"
    20  	"github.com/pingcap/tiflow/dm/pb"
    21  	"github.com/pingcap/tiflow/dm/pkg/ha"
    22  	"github.com/stretchr/testify/require"
    23  	"go.etcd.io/etcd/tests/v3/integration"
    24  )
    25  
    26  func TestGetExpectValidatorStage(t *testing.T) {
    27  	integration.BeforeTestExternal(t)
    28  	mockCluster := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
    29  	defer mockCluster.Terminate(t)
    30  
    31  	etcdTestCli := mockCluster.RandClient()
    32  	defer func() {
    33  		require.Nil(t, ha.ClearTestInfoOperation(etcdTestCli))
    34  	}()
    35  	cfg := config.SubTaskConfig{}
    36  	require.Nil(t, cfg.Decode(config.SampleSubtaskConfig, true))
    37  	source := cfg.SourceID
    38  	task := cfg.Name
    39  	stage := ha.NewSubTaskStage(pb.Stage_Running, source, task)
    40  
    41  	validatorStage, err := getExpectValidatorStage(cfg.ValidatorCfg, etcdTestCli, source, task, 0)
    42  	require.Nil(t, err)
    43  	require.Equal(t, pb.Stage_InvalidStage, validatorStage)
    44  
    45  	cfg.ValidatorCfg.Mode = config.ValidationFast
    46  	validatorStage, err = getExpectValidatorStage(cfg.ValidatorCfg, etcdTestCli, source, task, 0)
    47  	require.Nil(t, err)
    48  	require.Equal(t, pb.Stage_InvalidStage, validatorStage)
    49  
    50  	// put subtask config and stage at the same time
    51  	rev, err := ha.PutSubTaskCfgStage(etcdTestCli, []config.SubTaskConfig{cfg}, []ha.Stage{stage}, []ha.Stage{stage})
    52  	require.Nil(t, err)
    53  
    54  	validatorStage, err = getExpectValidatorStage(cfg.ValidatorCfg, etcdTestCli, source, task, 0)
    55  	require.Nil(t, err)
    56  	require.Equal(t, pb.Stage_Running, validatorStage)
    57  	validatorStage, err = getExpectValidatorStage(cfg.ValidatorCfg, etcdTestCli, source, task+"not exist", 0)
    58  	require.Nil(t, err)
    59  	require.Equal(t, pb.Stage_InvalidStage, validatorStage)
    60  
    61  	_, err = getExpectValidatorStage(cfg.ValidatorCfg, etcdTestCli, source, task+"not exist", rev+1)
    62  	require.NotNil(t, err)
    63  }