Skip to content

Commit

Permalink
cover mysql_test for unix domain socket
Browse files Browse the repository at this point in the history
Signed-off-by: kpango <kpango@vdaas.org>
  • Loading branch information
kpango committed Feb 19, 2021
1 parent 764ac42 commit 8a20261
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 21 deletions.
35 changes: 16 additions & 19 deletions internal/db/rdb/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,25 +113,22 @@ func (m *mySQLClient) Open(ctx context.Context) (err error) {
addParam += "&tls=" + tlsConfName
}

conn, err := m.dbr.Open(
m.db,
fmt.Sprintf(
"%s:%s@%s(%s)/%s?charset=%s&parseTime=true&loc=%s%s",
m.user, m.pass, m.network,
func() string {
switch net.NetworkTypeFromString(m.network) {
case net.UNIX, net.UNIXGRAM, net.UNIXPACKET:
if len(m.socketPath) != 0 {
return m.socketPath
}
}
return net.JoinHostPort(m.host, m.port)
}(),
m.name,
m.charset, m.timezone, addParam,
),
m.eventReceiver,
)
var addr, network string
switch net.NetworkTypeFromString(m.network) {
case net.UNIX, net.UNIXGRAM, net.UNIXPACKET:
if len(m.socketPath) != 0 {
addr = m.socketPath
} else {
network = net.TCP.String()
}
default:
network = net.TCP.String()
addr = net.JoinHostPort(m.host, m.port)
}
conn, err := m.dbr.Open(m.db, fmt.Sprintf(
"%s:%s@%s(%s)/%s?charset=%s&parseTime=true&loc=%s%s",
m.user, m.pass, network, addr, m.name, m.charset, m.timezone, addParam),
m.eventReceiver)
if err != nil {
return err
}
Expand Down
10 changes: 8 additions & 2 deletions internal/db/rdb/mysql/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ func Test_mySQLClient_Open(t *testing.T) {
db string
host string
port uint16
network string
socketPath string
user string
pass string
name string
Expand Down Expand Up @@ -291,6 +293,7 @@ func Test_mySQLClient_Open(t *testing.T) {
},
fields: fields{
db: "vdaas",
network: "unixgram",
host: "vald.com",
port: 3306,
user: "vdaas",
Expand Down Expand Up @@ -327,8 +330,10 @@ func Test_mySQLClient_Open(t *testing.T) {
},
fields: fields{
db: "vdaas",
host: "vald.com",
port: 3306,
network: "unix",
socketPath: "/tmp/mysql.sock",
host: "",
port: 0,
user: "vdaas",
pass: "vald",
name: "vald-user",
Expand Down Expand Up @@ -388,6 +393,7 @@ func Test_mySQLClient_Open(t *testing.T) {
}
m := &mySQLClient{
db: test.fields.db,
network: test.fields.network,
host: test.fields.host,
port: test.fields.port,
user: test.fields.user,
Expand Down

0 comments on commit 8a20261

Please sign in to comment.