github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/dm/pkg/conn/utils_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 conn
    15  
    16  import (
    17  	"context"
    18  	"testing"
    19  
    20  	"github.com/DATA-DOG/go-sqlmock"
    21  	gmysql "github.com/go-mysql-org/go-mysql/mysql"
    22  	tcontext "github.com/pingcap/tiflow/dm/pkg/context"
    23  	"github.com/pingcap/tiflow/dm/pkg/log"
    24  	"github.com/stretchr/testify/require"
    25  )
    26  
    27  func TestGetBinlogDB(t *testing.T) {
    28  	ctx, cancel := context.WithTimeout(context.Background(), DefaultDBTimeout)
    29  	defer cancel()
    30  
    31  	tctx := tcontext.NewContext(ctx, log.L())
    32  
    33  	db, mock, err := sqlmock.New()
    34  	require.NoError(t, err)
    35  	baseDB := NewBaseDBForTest(db)
    36  
    37  	// 5 columns for MySQL
    38  	rows := mock.NewRows([]string{"File", "Position", "Binlog_Do_DB", "Binlog_Ignore_DB", "Executed_Gtid_Set"}).AddRow(
    39  		"mysql-bin.000009", 11232, "do_db", "ignore_db", "074be7f4-f0f1-11ea-95bd-0242ac120002:1-699",
    40  	)
    41  	mock.ExpectQuery(`SHOW MASTER STATUS`).WillReturnRows(rows)
    42  
    43  	binlogDoDB, binlogIgnoreDB, err := GetBinlogDB(tctx, baseDB, "mysql")
    44  	require.Nil(t, err)
    45  	require.Equal(t, binlogDoDB, "do_db")
    46  	require.Equal(t, binlogIgnoreDB, "ignore_db")
    47  	require.Nil(t, mock.ExpectationsWereMet())
    48  
    49  	// 4 columns for MariaDB
    50  	rows = mock.NewRows([]string{"File", "Position", "Binlog_Do_DB", "Binlog_Ignore_DB"}).AddRow(
    51  		"mysql-bin.000009", 11232, "do_db", "ignore_db",
    52  	)
    53  	mock.ExpectQuery(`SHOW MASTER STATUS`).WillReturnRows(rows)
    54  	rows = mock.NewRows([]string{"Variable_name", "Value"}).AddRow("gtid_binlog_pos", "1-2-100")
    55  	mock.ExpectQuery(`SHOW GLOBAL VARIABLES LIKE 'gtid_binlog_pos'`).WillReturnRows(rows)
    56  
    57  	binlogDoDB, binlogIgnoreDB, err = GetBinlogDB(tctx, baseDB, "mariadb")
    58  	require.Nil(t, err)
    59  	require.Equal(t, binlogDoDB, "do_db")
    60  	require.Equal(t, binlogIgnoreDB, "ignore_db")
    61  	require.Nil(t, mock.ExpectationsWereMet())
    62  }
    63  
    64  func TestGetMasterStatus(t *testing.T) {
    65  	ctx := context.Background()
    66  	tctx := tcontext.NewContext(ctx, log.L())
    67  
    68  	db, mock, err := sqlmock.New()
    69  	require.NoError(t, err)
    70  	baseDB := NewBaseDBForTest(db)
    71  
    72  	cases := []struct {
    73  		rows           *sqlmock.Rows
    74  		binlogName     string
    75  		pos            uint64
    76  		binlogDoDB     string
    77  		binlogIgnoreDB string
    78  		gtidStr        string
    79  		err            error
    80  		flavor         string
    81  	}{
    82  		// For MySQL
    83  		{
    84  			sqlmock.NewRows([]string{"File", "Position", "Binlog_Do_DB", "Binlog_Ignore_DB", "Executed_Gtid_Set"}).
    85  				AddRow("ON.000001", 4822, "", "", "85ab69d1-b21f-11e6-9c5e-64006a8978d2:1-46"),
    86  			"ON.000001",
    87  			4822,
    88  			"",
    89  			"",
    90  			"85ab69d1-b21f-11e6-9c5e-64006a8978d2:1-46",
    91  			nil,
    92  			gmysql.MySQLFlavor,
    93  		},
    94  		// test unit64 position for MySQL
    95  		{
    96  			sqlmock.NewRows([]string{"File", "Position", "Binlog_Do_DB", "Binlog_Ignore_DB", "Executed_Gtid_Set"}).
    97  				AddRow("ON.000002", 429496729500, "", "", "85ab69d1-b21f-11e6-9c5e-64006a8978d2:1-500"),
    98  			"ON.000002",
    99  			429496729500,
   100  			"",
   101  			"",
   102  			"85ab69d1-b21f-11e6-9c5e-64006a8978d2:1-500",
   103  			nil,
   104  			gmysql.MySQLFlavor,
   105  		},
   106  		// For MariaDB
   107  		{
   108  			sqlmock.NewRows([]string{"File", "Position", "Binlog_Do_DB", "Binlog_Ignore_DB"}).
   109  				AddRow("mariadb-bin.000016", 475, "", ""),
   110  			"mariadb-bin.000016",
   111  			475,
   112  			"",
   113  			"",
   114  			"0-1-2",
   115  			nil,
   116  			gmysql.MariaDBFlavor,
   117  		},
   118  	}
   119  	for _, ca := range cases {
   120  		mock.ExpectQuery("SHOW MASTER STATUS").WillReturnRows(ca.rows)
   121  		// For MariaDB
   122  		if ca.flavor == gmysql.MariaDBFlavor {
   123  			mock.ExpectQuery("SHOW GLOBAL VARIABLES LIKE 'gtid_binlog_pos'").WillReturnRows(
   124  				sqlmock.NewRows([]string{"Variable_name", "Value"}).
   125  					AddRow("gtid_binlog_pos", "0-1-2"),
   126  			)
   127  		}
   128  		binlogName, pos, binlogDoDB, binlogIgnoreDB, gtidStr, err := GetMasterStatus(tctx, baseDB, ca.flavor)
   129  		require.IsType(t, uint64(0), pos)
   130  		require.NoError(t, err)
   131  		require.Equal(t, ca.binlogName, binlogName)
   132  		require.Equal(t, ca.pos, pos)
   133  		require.Equal(t, ca.binlogDoDB, binlogDoDB)
   134  		require.Equal(t, ca.binlogIgnoreDB, binlogIgnoreDB)
   135  		require.Equal(t, ca.gtidStr, gtidStr)
   136  		require.NoError(t, mock.ExpectationsWereMet())
   137  	}
   138  }