From f6aeea8369db8d143ba48f5ed797c0303aba0173 Mon Sep 17 00:00:00 2001 From: andy Date: Sat, 22 Dec 2018 14:07:25 +0800 Subject: [PATCH] mysql: add the err handle to the CreateReplUserWithoutBinlog --- src/mysql/mock.go | 29 +++++++++++++++++++++++++++++ src/mysql/mysql.go | 4 +++- src/mysql/mysql_test.go | 14 ++++++++++++++ src/mysql/user_test.go | 29 +++++++++++++++++++++++++++++ 4 files changed, 75 insertions(+), 1 deletion(-) diff --git a/src/mysql/mock.go b/src/mysql/mock.go index 208b092..d1fc4a2 100644 --- a/src/mysql/mock.go +++ b/src/mysql/mock.go @@ -661,6 +661,35 @@ func MockMysql(log *xlog.Log, port int, h ReplHandler) (string, *Mysql, func()) } } +// MockMysqlReplUser mock. +func MockMysqlReplUser(log *xlog.Log, port int, h ReplHandler) (string, *Mysql, func()) { + id := fmt.Sprintf("127.0.0.1:%d", port) + conf := config.DefaultMysqlConfig() + mysql := NewMysql(conf, log) + + // setup rpc + rpc, err := xrpc.NewService(xrpc.Log(log), + xrpc.ConnectionStr(id)) + if err != nil { + log.Panic("mysqlRPC.NewService.error[%v]", err) + } + setupRPC(rpc, mysql) + rpc.Start() + + // Set Repl functions + mysql.SetReplHandler(h) + + // Set User functions + mysql.SetUserHandler(new(MockUserA)) + + // start ping + mysql.PingStart() + return id, mysql, func() { + mysql.PingStop() + rpc.Stop() + } +} + // MockGetClient mock. func MockGetClient(t *testing.T, svrConn string) (*xrpc.Client, func()) { client, err := xrpc.NewClient(svrConn, 100) diff --git a/src/mysql/mysql.go b/src/mysql/mysql.go index 0c89613..cb0b200 100644 --- a/src/mysql/mysql.go +++ b/src/mysql/mysql.go @@ -118,7 +118,9 @@ func (m *Mysql) Ping() { // check replication users if exists, err := m.userHandler.CheckUserExists(db, m.conf.ReplUser); err == nil { if !exists { - m.userHandler.CreateReplUserWithoutBinlog(db, m.conf.ReplUser, m.conf.ReplPasswd) + if err = m.userHandler.CreateReplUserWithoutBinlog(db, m.conf.ReplUser, m.conf.ReplPasswd); err != nil { + log.Error("server.mysql.create.replication.user[%v].error[%+v]", m.conf.ReplUser, err) + } } } diff --git a/src/mysql/mysql_test.go b/src/mysql/mysql_test.go index 25cab04..82e61db 100644 --- a/src/mysql/mysql_test.go +++ b/src/mysql/mysql_test.go @@ -47,6 +47,20 @@ func TestStateDead(t *testing.T) { mysql.PingStop() } +func TestCreateReplUser (t *testing.T) { + // log + log := xlog.NewStdLog(xlog.Level(xlog.PANIC)) + port := common.RandomPort(8000, 9000) + _, mysql, cleanup := MockMysqlReplUser(log, port, NewMockGTIDA()) + defer cleanup() + + time.Sleep(time.Duration(config.DefaultMysqlConfig().PingTimeout*2) * time.Millisecond) + got := mysql.GetState() + want := MysqlAlive + assert.Equal(t, want, got) + mysql.PingStop() +} + /* // TEST EFFECTS: // test GTIDGreaterThan function diff --git a/src/mysql/user_test.go b/src/mysql/user_test.go index 9679409..502b9e3 100644 --- a/src/mysql/user_test.go +++ b/src/mysql/user_test.go @@ -10,6 +10,7 @@ package mysql import ( "config" + "fmt" "model" "testing" "xbase/xlog" @@ -153,6 +154,34 @@ func TestCreateReplUserWithoutBinlog(t *testing.T) { assert.Nil(t, err) } +func TestCreateReplUserWithoutBinlogErr(t *testing.T) { + db, mock, err := sqlmock.New() + assert.Nil(t, err) + defer db.Close() + + // log + log := xlog.NewStdLog(xlog.Level(xlog.DEBUG)) + conf := config.DefaultMysqlConfig() + mysql := NewMysql(conf, log) + mysql.db = db + + queryList := []string{ + "SET sql_log_bin=0", + "CREATE USER `repl` IDENTIFIED BY 'replpwd'", + "GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO `repl`", + "SET sql_log_bin=1", + } + + mock.ExpectExec(queryList[0]).WillReturnResult(sqlmock.NewResult(1, 1)) + mock.ExpectExec(queryList[1]).WillReturnResult(sqlmock.NewResult(1, 1)) + mock.ExpectExec(queryList[2]).WillReturnError(fmt.Errorf("ERROR 1045 (28000): Access denied for user 'repl'@'%%'")) + mock.ExpectExec(queryList[3]).WillReturnResult(sqlmock.NewResult(1, 1)) + err = mysql.CreateReplUserWithoutBinlog("repl", "replpwd") + want := "ERROR 1045 (28000): Access denied for user 'repl'@'%'" + got := err.Error() + assert.Equal(t, want, got) +} + func TestChangeUserPasswd(t *testing.T) { db, mock, err := sqlmock.New() assert.Nil(t, err)