github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/cdc/sink/dmlsink/mq/dispatcher/topic/expression_pulsar_test.go (about) 1 // Copyright 2023 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 topic 15 16 import ( 17 "fmt" 18 "testing" 19 20 "github.com/stretchr/testify/require" 21 ) 22 23 func TestPulsarValidate(t *testing.T) { 24 t.Parallel() 25 schema := "schema" 26 table := "table" 27 testTopicCases := []struct { 28 name string // test case name 29 expression string 30 wantErr string 31 expectedTopic string 32 }{ 33 // invalid cases 34 { 35 name: "like a full topic ,no {schema}", 36 expression: "persistent://", 37 wantErr: "invalid topic expression", 38 }, 39 { 40 name: "like a full topic", 41 expression: "persistent://{schema}", 42 wantErr: "invalid topic expression", 43 }, 44 { 45 name: "like a full topic ,no {schema}", 46 expression: "persistent://public", 47 wantErr: "invalid topic expression", 48 }, 49 { 50 name: "like a full topic ,no {schema}", 51 expression: "persistent://public_test-table", 52 wantErr: "invalid topic expression", 53 }, 54 { 55 name: "like a full topic ,need '/' after 'test-table'", 56 expression: "persistent://public/_test-table", 57 wantErr: "invalid topic expression", 58 }, 59 //{ 60 // // if the {schema} is a not exist namespace in pulsar server, pulsar client will get an error, 61 // // but cdc can not check it 62 // expression: "persistent://public/{schema}/_test-table", 63 // expectedResult: false, 64 //}, 65 { 66 name: "like a full topic", 67 expression: "persistent_public/test__{table}", 68 wantErr: "invalid topic expression", 69 }, 70 { 71 name: "like a full topic", 72 expression: "persistent://{schema}_{table}", 73 wantErr: "invalid topic expression", 74 }, 75 { 76 name: "like a full topic, but more '/' ", 77 expression: "persistent://{schema}/{table}/test/name", 78 wantErr: "invalid topic expression", 79 }, 80 { 81 name: "like a full topic, but more '/' ", 82 expression: "persistent://test/{table}/test/name/admin", 83 wantErr: "invalid topic expression", 84 }, 85 { 86 name: "like a full topic, but less '/' ", 87 expression: "non-persistent://public/test_{schema}_{table}", 88 wantErr: "invalid topic expression", 89 }, 90 { 91 name: "like a full topic, but less '/' ", 92 expression: "non-persistent://public/test {table}_123456aaaa", 93 wantErr: "invalid topic expression", 94 }, 95 // valid cases 96 { 97 name: "simple topic ,no {schema}", 98 expression: "public", // no {schema} 99 expectedTopic: "public", 100 }, 101 { 102 name: "simple topic ,no {schema}", 103 expression: "_xyz", 104 expectedTopic: "_xyz", 105 }, 106 { 107 name: "simple topic ,no {schema}", 108 expression: "123456", 109 expectedTopic: "123456", 110 }, 111 { 112 name: "simple topic ,no {schema}", 113 expression: "ABCD", 114 expectedTopic: "ABCD", 115 }, 116 { 117 name: "like a full topic ,no {schema}", 118 expression: "persistent:public_test-table", 119 expectedTopic: "persistent:public_test-table", 120 }, 121 { 122 name: "simple topic", 123 expression: "{schema}", 124 expectedTopic: "schema", 125 }, 126 { 127 name: "simple topic", 128 expression: "AZ_{schema}", 129 expectedTopic: "AZ_schema", 130 }, 131 { 132 name: "simple topic", 133 expression: "{table}_{schema}", 134 expectedTopic: "table_schema", 135 }, 136 { 137 name: "simple topic", 138 expression: "123_{schema}_non-persistenttest__{table})", 139 expectedTopic: "123_schema_non-persistenttest__table)", 140 }, 141 { 142 name: "simple topic", 143 expression: "persistent_public_test_{schema}_{table}", 144 expectedTopic: "persistent_public_test_schema_table", 145 }, 146 { 147 name: "simple topic", 148 expression: "persistent{schema}_{table}", 149 expectedTopic: "persistentschema_table", 150 }, 151 { 152 name: "full topic", 153 expression: "persistent://public/default/{schema}_{table}", 154 expectedTopic: "persistent://public/default/schema_table", 155 }, 156 { 157 name: "full topic", 158 expression: "persistent://public/default/2342-{schema}_abc234", 159 expectedTopic: "persistent://public/default/2342-schema_abc234", 160 }, 161 { 162 name: "full topic", 163 expression: "persistent://{schema}/{schema}/2342-{schema}_abc234", 164 expectedTopic: "persistent://schema/schema/2342-schema_abc234", 165 }, 166 { 167 name: "full topic", 168 expression: "persistent://{schema}/dev/2342-{schema}_abc234", 169 expectedTopic: "persistent://schema/dev/2342-schema_abc234", 170 }, 171 { 172 name: "full topic", 173 expression: "non-persistent://public/default/test_{schema}_{table}_back_up", 174 expectedTopic: "non-persistent://public/default/test_schema_table_back_up", 175 }, 176 { 177 name: "full topic", 178 expression: "123_{schema}_non-persistenttest__{table}", 179 expectedTopic: "123_schema_non-persistenttest__table", 180 }, 181 } 182 183 for i, tc := range testTopicCases { 184 topicExpr := Expression(tc.expression) 185 err := topicExpr.PulsarValidate() 186 t.Logf("case %d: %s", i, tc.name) 187 if err != nil { 188 require.Contains(t, err.Error(), tc.wantErr, fmt.Sprintf("case:%s", tc.name)) 189 } else { 190 require.Nil(t, err, fmt.Sprintf("case:%s", tc.name)) 191 topicName := topicExpr.Substitute(schema, table) 192 require.Equal(t, topicName, tc.expectedTopic, fmt.Sprintf("case:%s", tc.name)) 193 } 194 } 195 }