github.com/pingcap/ticdc@v0.0.0-20220526033649-485a10ef2652/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 "github.com/pingcap/check" 18 "github.com/pingcap/ticdc/pkg/util/testleak" 19 ) 20 21 type etcdkeySuite struct{} 22 23 var _ = check.Suite(&etcdkeySuite{}) 24 25 func (s *etcdkeySuite) TestEtcdKey(c *check.C) { 26 defer testleak.AfterTest(c)() 27 testcases := []struct { 28 key string 29 expected *CDCKey 30 }{{ 31 key: "/tidb/cdc/owner/223176cb44d20a13", 32 expected: &CDCKey{ 33 Tp: CDCKeyTypeOwner, 34 OwnerLeaseID: "223176cb44d20a13", 35 }, 36 }, { 37 key: "/tidb/cdc/owner", 38 expected: &CDCKey{ 39 Tp: CDCKeyTypeOwner, 40 OwnerLeaseID: "", 41 }, 42 }, { 43 key: "/tidb/cdc/capture/6bbc01c8-0605-4f86-a0f9-b3119109b225", 44 expected: &CDCKey{ 45 Tp: CDCKeyTypeCapture, 46 CaptureID: "6bbc01c8-0605-4f86-a0f9-b3119109b225", 47 }, 48 }, { 49 key: "/tidb/cdc/changefeed/info/test-_@#$%changefeed", 50 expected: &CDCKey{ 51 Tp: CDCKeyTypeChangefeedInfo, 52 ChangefeedID: "test-_@#$%changefeed", 53 }, 54 }, { 55 key: "/tidb/cdc/changefeed/info/test/changefeed", 56 expected: &CDCKey{ 57 Tp: CDCKeyTypeChangefeedInfo, 58 ChangefeedID: "test/changefeed", 59 }, 60 }, { 61 key: "/tidb/cdc/job/test-changefeed", 62 expected: &CDCKey{ 63 Tp: CDCKeyTypeChangeFeedStatus, 64 ChangefeedID: "test-changefeed", 65 }, 66 }, { 67 key: "/tidb/cdc/task/position/6bbc01c8-0605-4f86-a0f9-b3119109b225/test-changefeed", 68 expected: &CDCKey{ 69 Tp: CDCKeyTypeTaskPosition, 70 ChangefeedID: "test-changefeed", 71 CaptureID: "6bbc01c8-0605-4f86-a0f9-b3119109b225", 72 }, 73 }, { 74 key: "/tidb/cdc/task/position/6bbc01c8-0605-4f86-a0f9-b3119109b225/test/changefeed", 75 expected: &CDCKey{ 76 Tp: CDCKeyTypeTaskPosition, 77 ChangefeedID: "test/changefeed", 78 CaptureID: "6bbc01c8-0605-4f86-a0f9-b3119109b225", 79 }, 80 }, { 81 key: "/tidb/cdc/task/status/6bbc01c8-0605-4f86-a0f9-b3119109b225/test-changefeed", 82 expected: &CDCKey{ 83 Tp: CDCKeyTypeTaskStatus, 84 ChangefeedID: "test-changefeed", 85 CaptureID: "6bbc01c8-0605-4f86-a0f9-b3119109b225", 86 }, 87 }, { 88 key: "/tidb/cdc/task/workload/6bbc01c8-0605-4f86-a0f9-b3119109b225/test-changefeed", 89 expected: &CDCKey{ 90 Tp: CDCKeyTypeTaskWorkload, 91 ChangefeedID: "test-changefeed", 92 CaptureID: "6bbc01c8-0605-4f86-a0f9-b3119109b225", 93 }, 94 }} 95 for _, tc := range testcases { 96 k := new(CDCKey) 97 err := k.Parse(tc.key) 98 c.Assert(err, check.IsNil) 99 c.Assert(k, check.DeepEquals, tc.expected) 100 c.Assert(k.String(), check.Equals, tc.key) 101 } 102 } 103 104 func (s *etcdkeySuite) TestEtcdKeyParseError(c *check.C) { 105 defer testleak.AfterTest(c)() 106 testCases := []struct { 107 key string 108 error bool 109 }{{ 110 key: "/tidb/cdc/task/position/6bbc01c8-0605-4f86-a0f9-b3119109b225/test/changefeed", 111 error: false, 112 }, { 113 key: "/tidb/cdc/task/position/6bbc01c8-0605-4f86-a0f9-b3119109b225/", 114 error: false, 115 }, { 116 key: "/tidb/cdc/task/position/6bbc01c8-0605-4f86-a0f9-b3119109b225", 117 error: true, 118 }, { 119 key: "/tidb/cdc/task/status/6bbc01c8-0605-4f86-a0f9-b3119109b225", 120 error: true, 121 }, { 122 key: "/tidb/cdc/task/workload/6bbc01c8-0605-4f86-a0f9-b3119109b225", 123 error: true, 124 }, { 125 key: "/tidb/cd", 126 error: true, 127 }, { 128 key: "/tidb/cdc/", 129 error: true, 130 }} 131 for _, tc := range testCases { 132 k := new(CDCKey) 133 err := k.Parse(tc.key) 134 if tc.error { 135 c.Assert(err, check.NotNil) 136 } else { 137 c.Assert(err, check.IsNil) 138 } 139 } 140 }