Skip to content

Commit

Permalink
mysql: add the err handle to the CreateReplUserWithoutBinlog
Browse files Browse the repository at this point in the history
  • Loading branch information
andyli029 authored and BohuTANG committed Dec 23, 2018
1 parent 334dc26 commit f6aeea8
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 1 deletion.
29 changes: 29 additions & 0 deletions src/mysql/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion src/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}

Expand Down
14 changes: 14 additions & 0 deletions src/mysql/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 29 additions & 0 deletions src/mysql/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package mysql

import (
"config"
"fmt"
"model"
"testing"
"xbase/xlog"
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit f6aeea8

Please sign in to comment.