github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/cdc/sink/validator/validator_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 validator
    15  
    16  import (
    17  	"context"
    18  	"testing"
    19  
    20  	"github.com/pingcap/tiflow/cdc/model"
    21  	"github.com/pingcap/tiflow/pkg/config"
    22  	"github.com/pingcap/tiflow/pkg/util"
    23  	"github.com/stretchr/testify/require"
    24  )
    25  
    26  func TestPreCheckSinkURI(t *testing.T) {
    27  	t.Parallel()
    28  
    29  	tests := []struct {
    30  		name string
    31  		uri  string
    32  		err  string
    33  	}{
    34  		{
    35  			name: "valid domain MySQL URI",
    36  			uri:  "mysql://root:111@baidu.com:3306/",
    37  			err:  "",
    38  		},
    39  		{
    40  			name: "valid IPv4 MySQL URI",
    41  			uri:  "mysql://root:111@127.0.0.1:3306/",
    42  			err:  "",
    43  		},
    44  		{
    45  			name: "valid IPv6 MySQL URI",
    46  			uri:  "mysql://root:111@[::1]:3306/",
    47  			err:  "",
    48  		},
    49  		{
    50  			name: "valid IPv4 Kafka URI",
    51  			uri:  "kafka://127.0.0.1:9092/topic1",
    52  			err:  "",
    53  		},
    54  		{
    55  			name: "valid IPv6 Kafka URI",
    56  			uri:  "kafka://[3333:10:9:101::204]:9092/topic1",
    57  			err:  "",
    58  		},
    59  		{
    60  			name: "blackhole URI",
    61  			uri:  "blackhole://",
    62  			err:  "",
    63  		},
    64  		{
    65  			name: "empty URI",
    66  			uri:  "",
    67  			err:  "sink uri is empty",
    68  		},
    69  		{
    70  			name: "invalid IPv6 MySQL URI",
    71  			uri:  "mysql://root:111@::1:3306/",
    72  			err:  "sink uri host is not valid IPv6 address",
    73  		},
    74  		{
    75  			name: "invalid IPv6 Kafka URI",
    76  			uri:  "kafka://3333:10:9:101::204:9092/topic1",
    77  			err:  "sink uri host is not valid IPv6 address",
    78  		},
    79  	}
    80  
    81  	for _, tt := range tests {
    82  		test := tt
    83  		t.Run(test.name, func(t *testing.T) {
    84  			t.Parallel()
    85  			_, err := preCheckSinkURI(test.uri)
    86  			if test.err != "" {
    87  				require.Contains(t, err.Error(), test.err)
    88  			} else {
    89  				require.NoError(t, err)
    90  			}
    91  		})
    92  	}
    93  }
    94  
    95  func TestValidateSink(t *testing.T) {
    96  	t.Parallel()
    97  	ctx := context.Background()
    98  	ctx, cancel := context.WithCancel(ctx)
    99  	defer cancel()
   100  	replicateConfig := config.GetDefaultReplicaConfig()
   101  
   102  	// test sink uri error
   103  	sinkURI := "mysql://root:111@127.0.0.1:3306/"
   104  	err := Validate(ctx, model.DefaultChangeFeedID("test"), sinkURI, replicateConfig, nil)
   105  	require.NotNil(t, err)
   106  	require.Contains(t, err.Error(), "fail to open MySQL connection")
   107  
   108  	// test sink uri right
   109  	sinkURI = "blackhole://"
   110  	err = Validate(ctx, model.DefaultChangeFeedID("test"), sinkURI, replicateConfig, nil)
   111  	require.Nil(t, err)
   112  
   113  	// test bdr mode error
   114  	replicateConfig.BDRMode = util.AddressOf(true)
   115  	sinkURI = "blackhole://"
   116  	err = Validate(ctx, model.DefaultChangeFeedID("test"), sinkURI, replicateConfig, nil)
   117  	require.NotNil(t, err)
   118  	require.Contains(t, err.Error(), "sink uri scheme is not supported in BDR mode")
   119  
   120  	// test sink-scheme/syncpoint error
   121  	replicateConfig.EnableSyncPoint = util.AddressOf(true)
   122  	sinkURI = "kafka://"
   123  	err = Validate(ctx, model.DefaultChangeFeedID("test"), sinkURI, replicateConfig, nil)
   124  	require.NotNil(t, err)
   125  	require.Contains(
   126  		t, err.Error(),
   127  		"sink uri scheme is not supported with syncpoint enabled",
   128  	)
   129  }