github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/dm/pkg/binlog/status_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 binlog 15 16 import ( 17 "testing" 18 19 "github.com/DATA-DOG/go-sqlmock" 20 gmysql "github.com/go-mysql-org/go-mysql/mysql" 21 "github.com/go-sql-driver/mysql" 22 "github.com/pingcap/tiflow/dm/pkg/conn" 23 tcontext "github.com/pingcap/tiflow/dm/pkg/context" 24 "github.com/stretchr/testify/require" 25 ) 26 27 func TestGetBinaryLogs(t *testing.T) { 28 t.Parallel() 29 30 db, mock, err := sqlmock.New() 31 require.NoError(t, err) 32 33 ctx := tcontext.Background() 34 baseDB := conn.NewBaseDBForTest(db) 35 36 cases := []struct { 37 rows *sqlmock.Rows 38 sizes FileSizes 39 }{ 40 { 41 sqlmock.NewRows([]string{"Log_name", "File_size"}). 42 AddRow("mysql-bin.000001", 52119). 43 AddRow("mysql-bin.000002", 114), 44 []binlogSize{ 45 { 46 "mysql-bin.000001", 52119, 47 }, 48 { 49 "mysql-bin.000002", 114, 50 }, 51 }, 52 }, 53 { 54 sqlmock.NewRows([]string{"Log_name", "File_size", "Encrypted"}). 55 AddRow("mysql-bin.000001", 52119, "No"). 56 AddRow("mysql-bin.000002", 114, "No"), 57 []binlogSize{ 58 { 59 "mysql-bin.000001", 52119, 60 }, 61 { 62 "mysql-bin.000002", 114, 63 }, 64 }, 65 }, 66 } 67 68 for _, ca := range cases { 69 mock.ExpectQuery("SHOW BINARY LOGS").WillReturnRows(ca.rows) 70 sizes, err2 := GetBinaryLogs(ctx, baseDB) 71 require.NoError(t, err2) 72 require.Equal(t, ca.sizes, sizes) 73 require.NoError(t, mock.ExpectationsWereMet()) 74 } 75 76 mock.ExpectQuery("SHOW BINARY LOGS").WillReturnError(&mysql.MySQLError{ 77 Number: 1227, 78 Message: "Access denied; you need (at least one of) the SUPER, REPLICATION CLIENT privilege(s) for this operation", 79 }) 80 _, err2 := GetBinaryLogs(ctx, baseDB) 81 require.Error(t, err2) 82 require.NoError(t, mock.ExpectationsWereMet()) 83 } 84 85 func TestBinlogSizesAfter(t *testing.T) { 86 t.Parallel() 87 sizes := FileSizes{ 88 {name: "mysql-bin.999999", size: 1}, 89 {name: "mysql-bin.1000000", size: 2}, 90 {name: "mysql-bin.1000001", size: 4}, 91 } 92 93 cases := []struct { 94 position gmysql.Position 95 expected int64 96 }{ 97 { 98 gmysql.Position{Name: "mysql-bin.999999", Pos: 0}, 99 7, 100 }, 101 { 102 gmysql.Position{Name: "mysql-bin.1000000", Pos: 1}, 103 5, 104 }, 105 { 106 gmysql.Position{Name: "mysql-bin.1000001", Pos: 3}, 107 1, 108 }, 109 } 110 111 for _, ca := range cases { 112 require.Equal(t, ca.expected, sizes.After(ca.position)) 113 } 114 }