Skip to content

Commit e0c6d64

Browse files
authored
Add more tests for new replication statements (pingcap#12540)
ref pingcap#11020
1 parent 5a78616 commit e0c6d64

File tree

3 files changed

+61
-7
lines changed

3 files changed

+61
-7
lines changed

dm/pkg/conn/basedb.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,19 @@ func NewBaseDBForTest(db *sql.DB, doFuncInClose ...func()) *BaseDB {
229229
}
230230
}
231231

232+
// NewBaseDBForTestWithVersion returns *BaseDB object for test.
233+
func NewBaseDBForTestWithVersion(db *sql.DB, version string, doFuncInClose ...func()) *BaseDB {
234+
conns := make(map[*BaseConn]struct{})
235+
return &BaseDB{
236+
DB: db,
237+
conns: conns,
238+
Retry: &retry.FiniteRetryStrategy{},
239+
Scope: terror.ScopeNotSet,
240+
doFuncInClose: doFuncInClose,
241+
version: version,
242+
}
243+
}
244+
232245
// NewMockDB returns *BaseDB object for mock.
233246
func NewMockDB(db *sql.DB, doFuncInClose ...func()) *BaseDB {
234247
baseDB := NewBaseDBForTest(db, doFuncInClose...)

dm/pkg/conn/db_test.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -436,11 +436,26 @@ func TestGetSlaveServerID(t *testing.T) {
436436
require.NoError(t, err)
437437

438438
cases := []struct {
439+
version string
440+
stmt string
439441
rows *sqlmock.Rows
440442
results map[uint32]struct{}
441443
}{
442-
// For MySQL
444+
// For MySQL 8.4
445+
{
446+
"8.4.8",
447+
"SHOW REPLICAS",
448+
sqlmock.NewRows([]string{"Server_id", "Host", "Port", "Source_id", "Replica_UUID"}).
449+
AddRow(192168010, "iconnect2", 3306, 192168011, "14cb6624-7f93-11e0-b2c0-c80aa9429562").
450+
AddRow(1921680101, "athena", 3306, 192168011, "07af4990-f41f-11df-a566-7ac56fdaf645"),
451+
map[uint32]struct{}{
452+
192168010: {}, 1921680101: {},
453+
},
454+
},
455+
// For MySQL 8.0
443456
{
457+
"8.0.45",
458+
"SHOW SLAVE HOSTS",
444459
sqlmock.NewRows([]string{"Server_id", "Host", "Port", "Master_id", "Slave_UUID"}).
445460
AddRow(192168010, "iconnect2", 3306, 192168011, "14cb6624-7f93-11e0-b2c0-c80aa9429562").
446461
AddRow(1921680101, "athena", 3306, 192168011, "07af4990-f41f-11df-a566-7ac56fdaf645"),
@@ -450,6 +465,8 @@ func TestGetSlaveServerID(t *testing.T) {
450465
},
451466
// For MariaDB
452467
{
468+
"10.4.7-MariaDB",
469+
"SHOW SLAVE HOSTS",
453470
sqlmock.NewRows([]string{"Server_id", "Host", "Port", "Master_id"}).
454471
AddRow(192168010, "iconnect2", 3306, 192168011).
455472
AddRow(1921680101, "athena", 3306, 192168011),
@@ -459,6 +476,8 @@ func TestGetSlaveServerID(t *testing.T) {
459476
},
460477
// For MariaDB, with Server_id greater than 2^31, to test uint conversion
461478
{
479+
"10.4.7-MariaDB",
480+
"SHOW SLAVE HOSTS",
462481
sqlmock.NewRows([]string{"Server_id", "Host", "Port", "Master_id"}).
463482
AddRow(2147483649, "iconnect2", 3306, 192168011).
464483
AddRow(2147483650, "athena", 3306, 192168011),
@@ -470,8 +489,8 @@ func TestGetSlaveServerID(t *testing.T) {
470489

471490
tctx := tcontext.NewContext(context.Background(), log.L())
472491
for _, ca := range cases {
473-
mock.ExpectQuery("SHOW SLAVE HOSTS").WillReturnRows(ca.rows)
474-
results, err2 := GetSlaveServerID(tctx, NewBaseDBForTest(db))
492+
mock.ExpectQuery(ca.stmt).WillReturnRows(ca.rows)
493+
results, err2 := GetSlaveServerID(tctx, NewBaseDBForTestWithVersion(db, ca.version))
475494
require.NoError(t, err2)
476495
require.Equal(t, ca.results, results)
477496
}

dm/pkg/conn/utils_test.go

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,10 @@ func TestGetMasterStatus(t *testing.T) {
6767

6868
db, mock, err := sqlmock.New()
6969
require.NoError(t, err)
70-
baseDB := NewBaseDBForTest(db)
7170

7271
cases := []struct {
72+
version string
73+
stmt string
7374
rows *sqlmock.Rows
7475
binlogName string
7576
pos uint64
@@ -79,8 +80,24 @@ func TestGetMasterStatus(t *testing.T) {
7980
err error
8081
flavor string
8182
}{
82-
// For MySQL
83+
// For MySQL 8.4
84+
{
85+
"8.4.8",
86+
"SHOW BINARY LOG STATUS",
87+
sqlmock.NewRows([]string{"File", "Position", "Binlog_Do_DB", "Binlog_Ignore_DB", "Executed_Gtid_Set"}).
88+
AddRow("ON.000001", 4822, "", "", "85ab69d1-b21f-11e6-9c5e-64006a8978d2:1-46"),
89+
"ON.000001",
90+
4822,
91+
"",
92+
"",
93+
"85ab69d1-b21f-11e6-9c5e-64006a8978d2:1-46",
94+
nil,
95+
gmysql.MySQLFlavor,
96+
},
97+
// For MySQL 8.0
8398
{
99+
"8.0.45",
100+
"SHOW MASTER STATUS",
84101
sqlmock.NewRows([]string{"File", "Position", "Binlog_Do_DB", "Binlog_Ignore_DB", "Executed_Gtid_Set"}).
85102
AddRow("ON.000001", 4822, "", "", "85ab69d1-b21f-11e6-9c5e-64006a8978d2:1-46"),
86103
"ON.000001",
@@ -91,8 +108,10 @@ func TestGetMasterStatus(t *testing.T) {
91108
nil,
92109
gmysql.MySQLFlavor,
93110
},
94-
// test unit64 position for MySQL
111+
// test unit64 position for MySQL 8.0
95112
{
113+
"8.0.45",
114+
"SHOW MASTER STATUS",
96115
sqlmock.NewRows([]string{"File", "Position", "Binlog_Do_DB", "Binlog_Ignore_DB", "Executed_Gtid_Set"}).
97116
AddRow("ON.000002", 429496729500, "", "", "85ab69d1-b21f-11e6-9c5e-64006a8978d2:1-500"),
98117
"ON.000002",
@@ -105,6 +124,8 @@ func TestGetMasterStatus(t *testing.T) {
105124
},
106125
// For MariaDB
107126
{
127+
"10.4.7-MariaDB",
128+
"SHOW MASTER STATUS",
108129
sqlmock.NewRows([]string{"File", "Position", "Binlog_Do_DB", "Binlog_Ignore_DB"}).
109130
AddRow("mariadb-bin.000016", 475, "", ""),
110131
"mariadb-bin.000016",
@@ -117,14 +138,15 @@ func TestGetMasterStatus(t *testing.T) {
117138
},
118139
}
119140
for _, ca := range cases {
120-
mock.ExpectQuery("SHOW MASTER STATUS").WillReturnRows(ca.rows)
141+
mock.ExpectQuery(ca.stmt).WillReturnRows(ca.rows)
121142
// For MariaDB
122143
if ca.flavor == gmysql.MariaDBFlavor {
123144
mock.ExpectQuery("SHOW GLOBAL VARIABLES LIKE 'gtid_binlog_pos'").WillReturnRows(
124145
sqlmock.NewRows([]string{"Variable_name", "Value"}).
125146
AddRow("gtid_binlog_pos", "0-1-2"),
126147
)
127148
}
149+
baseDB := NewBaseDBForTestWithVersion(db, ca.version)
128150
binlogName, pos, binlogDoDB, binlogIgnoreDB, gtidStr, err := GetMasterStatus(tctx, baseDB, ca.flavor)
129151
require.IsType(t, uint64(0), pos)
130152
require.NoError(t, err)

0 commit comments

Comments
 (0)