github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/etcd/etcdkey_test.go (about)

     1  // Copyright 2021 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 etcd
    15  
    16  import (
    17  	"fmt"
    18  	"testing"
    19  
    20  	"github.com/pingcap/tiflow/cdc/model"
    21  	"github.com/stretchr/testify/require"
    22  )
    23  
    24  func TestEtcdKey(t *testing.T) {
    25  	t.Parallel()
    26  
    27  	testcases := []struct {
    28  		key      string
    29  		expected *CDCKey
    30  	}{{
    31  		key: fmt.Sprintf("%s/owner/223176cb44d20a13", DefaultClusterAndMetaPrefix),
    32  		expected: &CDCKey{
    33  			Tp:           CDCKeyTypeOwner,
    34  			OwnerLeaseID: "223176cb44d20a13",
    35  			ClusterID:    DefaultCDCClusterID,
    36  		},
    37  	}, {
    38  		key: fmt.Sprintf("%s/owner", DefaultClusterAndMetaPrefix),
    39  		expected: &CDCKey{
    40  			Tp:           CDCKeyTypeOwner,
    41  			OwnerLeaseID: "",
    42  			ClusterID:    DefaultCDCClusterID,
    43  		},
    44  	}, {
    45  		key: fmt.Sprintf("%s/capture/6bbc01c8-0605-4f86-a0f9-b3119109b225",
    46  			DefaultClusterAndMetaPrefix),
    47  		expected: &CDCKey{
    48  			Tp:        CDCKeyTypeCapture,
    49  			CaptureID: "6bbc01c8-0605-4f86-a0f9-b3119109b225",
    50  			ClusterID: DefaultCDCClusterID,
    51  		},
    52  	}, {
    53  		key: DefaultClusterAndNamespacePrefix +
    54  			"/changefeed/info/test-_@#$%changefeed",
    55  		expected: &CDCKey{
    56  			Tp:           CDCKeyTypeChangefeedInfo,
    57  			ChangefeedID: model.DefaultChangeFeedID("test-_@#$%changefeed"),
    58  			ClusterID:    DefaultCDCClusterID,
    59  			Namespace:    model.DefaultNamespace,
    60  		},
    61  	}, {
    62  		key: DefaultClusterAndNamespacePrefix +
    63  			"/changefeed/info/test/changefeed",
    64  		expected: &CDCKey{
    65  			Tp:           CDCKeyTypeChangefeedInfo,
    66  			ChangefeedID: model.DefaultChangeFeedID("test/changefeed"),
    67  			ClusterID:    DefaultCDCClusterID,
    68  			Namespace:    model.DefaultNamespace,
    69  		},
    70  	}, {
    71  		key: DefaultClusterAndNamespacePrefix +
    72  			"/changefeed/status/test-changefeed",
    73  		expected: &CDCKey{
    74  			Tp:           CDCKeyTypeChangeFeedStatus,
    75  			ChangefeedID: model.DefaultChangeFeedID("test-changefeed"),
    76  			ClusterID:    DefaultCDCClusterID,
    77  			Namespace:    model.DefaultNamespace,
    78  		},
    79  	}, {
    80  		key: "/tidb/cdc/default/name/task" +
    81  			"/position/6bbc01c8-0605-4f86-a0f9-b3119109b225/test-changefeed",
    82  		expected: &CDCKey{
    83  			Tp: CDCKeyTypeTaskPosition,
    84  			ChangefeedID: model.ChangeFeedID{
    85  				Namespace: "name",
    86  				ID:        "test-changefeed",
    87  			},
    88  			CaptureID: "6bbc01c8-0605-4f86-a0f9-b3119109b225",
    89  			ClusterID: DefaultCDCClusterID,
    90  			Namespace: "name",
    91  		},
    92  	}, {
    93  		key: DefaultClusterAndNamespacePrefix +
    94  			"/task/position/6bbc01c8-0605-4f86-a0f9-b3119109b225/test/changefeed",
    95  		expected: &CDCKey{
    96  			Tp:           CDCKeyTypeTaskPosition,
    97  			ChangefeedID: model.DefaultChangeFeedID("test/changefeed"),
    98  			CaptureID:    "6bbc01c8-0605-4f86-a0f9-b3119109b225",
    99  			ClusterID:    DefaultCDCClusterID,
   100  			Namespace:    model.DefaultNamespace,
   101  		},
   102  	}, {
   103  		key: DefaultClusterAndNamespacePrefix + "/upstream/12345",
   104  		expected: &CDCKey{
   105  			Tp:         CDCKeyTypeUpStream,
   106  			ClusterID:  DefaultCDCClusterID,
   107  			Namespace:  model.DefaultNamespace,
   108  			UpstreamID: 12345,
   109  		},
   110  	}, {
   111  		key: fmt.Sprintf("%s%s", DefaultClusterAndMetaPrefix, metaVersionKey),
   112  		expected: &CDCKey{
   113  			Tp:        CDCKeyTypeMetaVersion,
   114  			ClusterID: DefaultCDCClusterID,
   115  		},
   116  	}}
   117  	for _, tc := range testcases {
   118  		k := new(CDCKey)
   119  		err := k.Parse(DefaultCDCClusterID, tc.key)
   120  		require.NoError(t, err)
   121  		require.Equal(t, k, tc.expected)
   122  		require.Equal(t, k.String(), tc.key)
   123  	}
   124  }
   125  
   126  func TestEtcdKeyParseError(t *testing.T) {
   127  	t.Parallel()
   128  
   129  	testCases := []struct {
   130  		key   string
   131  		error bool
   132  	}{{
   133  		key: DefaultClusterAndNamespacePrefix +
   134  			"/task/position/6bbc01c8-0605-4f86-a0f9-b3119109b225/test/changefeed",
   135  		error: false,
   136  	}, {
   137  		key: DefaultClusterAndNamespacePrefix +
   138  			"/task/position/6bbc01c8-0605-4f86-a0f9-b3119109b225/",
   139  		error: false,
   140  	}, {
   141  		key: DefaultClusterAndNamespacePrefix +
   142  			"/task/position/6bbc01c8-0605-4f86-a0f9-b3119109b225",
   143  		error: true,
   144  	}, {
   145  		key:   "/tidb/cd",
   146  		error: true,
   147  	}, {
   148  		key:   "/tidb/cdc/",
   149  		error: true,
   150  	}, {
   151  		key:   "/tidb/cdc/default/__meta_data__/abcd",
   152  		error: true,
   153  	}, {
   154  		key:   "/tidb/cdc/default/default/abcd",
   155  		error: true,
   156  	}}
   157  	for _, tc := range testCases {
   158  		k := new(CDCKey)
   159  		err := k.Parse(DefaultCDCClusterID, tc.key)
   160  		if tc.error {
   161  			require.NotNil(t, err)
   162  		} else {
   163  			require.Nil(t, err)
   164  		}
   165  	}
   166  	k := new(CDCKey)
   167  	k.Tp = CDCKeyTypeUpStream + 1
   168  	require.Panics(t, func() {
   169  		_ = k.String()
   170  	})
   171  }