github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/dm/pkg/utils/relay_test.go (about) 1 // Copyright 2019 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 utils 15 16 import ( 17 "os" 18 "strings" 19 "testing" 20 21 "github.com/go-mysql-org/go-mysql/replication" 22 "github.com/stretchr/testify/require" 23 ) 24 25 func TestParseUUIDIndex(t *testing.T) { 26 t.Parallel() 27 28 f, err := os.CreateTemp("", "server-uuid.index") 29 require.NoError(t, err) 30 defer os.Remove(f.Name()) 31 32 uuids := []string{ 33 "c65525fa-c7a3-11e8-a878-0242ac130005.000001", 34 "c6ae5afe-c7a3-11e8-a19d-0242ac130006.000002", 35 "c65525fa-c7a3-11e8-a878-0242ac130005.000003", 36 } 37 38 err = os.WriteFile(f.Name(), []byte(strings.Join(uuids, "\n")), 0o644) 39 require.NoError(t, err) 40 41 obtainedUUIDs, err := ParseUUIDIndex(f.Name()) 42 require.NoError(t, err) 43 require.Equal(t, uuids, obtainedUUIDs) 44 45 // test GetUUIDBySuffix 46 uuid := uuids[1] 47 uuidWS := GetUUIDBySuffix(uuids, uuid[len(uuid)-6:]) 48 require.Equal(t, uuid, uuidWS) 49 50 uuidWS = GetUUIDBySuffix(uuids, "100000") 51 require.Equal(t, "", uuidWS) 52 } 53 54 func TestSuffixForUUID(t *testing.T) { 55 t.Parallel() 56 57 cases := []struct { 58 uuid string 59 ID int 60 uuidWithSuffix string 61 }{ 62 {"c65525fa-c7a3-11e8-a878-0242ac130005", 1, "c65525fa-c7a3-11e8-a878-0242ac130005.000001"}, 63 {"c6ae5afe-c7a3-11e8-a19d-0242ac130006", 2, "c6ae5afe-c7a3-11e8-a19d-0242ac130006.000002"}, 64 } 65 66 for _, cs := range cases { 67 uuidWS := AddSuffixForUUID(cs.uuid, cs.ID) 68 require.Equal(t, cs.uuidWithSuffix, uuidWS) 69 70 uuidWOS, id, err := ParseRelaySubDir(cs.uuidWithSuffix) 71 require.NoError(t, err) 72 require.Equal(t, cs.uuid, uuidWOS) 73 require.Equal(t, cs.ID, id) 74 75 suffix := SuffixIntToStr(cs.ID) 76 hasSuffix := strings.HasSuffix(cs.uuidWithSuffix, suffix) 77 require.Equal(t, true, hasSuffix) 78 } 79 80 _, _, err := ParseRelaySubDir("uuid-with-out-suffix") 81 require.Error(t, err) 82 83 _, _, err = ParseRelaySubDir("uuid-invalid-suffix-len.01") 84 require.Error(t, err) 85 86 _, _, err = ParseRelaySubDir("uuid-invalid-suffix-fmt.abc") 87 require.Error(t, err) 88 89 _, _, err = ParseRelaySubDir("uuid-invalid-fmt.abc.000001") 90 require.Error(t, err) 91 } 92 93 func TestGenFakeRotateEvent(t *testing.T) { 94 t.Parallel() 95 96 var ( 97 nextLogName = "mysql-bin.000123" 98 logPos = uint64(456) 99 serverID = uint32(101) 100 ) 101 102 ev, err := GenFakeRotateEvent(nextLogName, logPos, serverID) 103 require.NoError(t, err) 104 require.Equal(t, serverID, ev.Header.ServerID) 105 require.Equal(t, uint32(0), ev.Header.Timestamp) 106 require.Equal(t, uint32(0), ev.Header.LogPos) 107 require.Equal(t, replication.ROTATE_EVENT, ev.Header.EventType) 108 109 evR, ok := ev.Event.(*replication.RotateEvent) 110 require.True(t, ok) 111 require.Equal(t, nextLogName, string(evR.NextLogName)) 112 require.Equal(t, logPos, evR.Position) 113 }