2016-10-11 14:42:19 +00:00
|
|
|
/*
|
2022-08-12 16:52:22 +00:00
|
|
|
Copyright 2022 GitHub Inc.
|
2016-10-11 14:42:19 +00:00
|
|
|
See https://github.com/github/gh-ost/blob/master/LICENSE
|
|
|
|
*/
|
|
|
|
|
|
|
|
package mysql
|
|
|
|
|
|
|
|
import (
|
2019-01-31 16:03:48 +00:00
|
|
|
"crypto/tls"
|
2016-10-11 14:42:19 +00:00
|
|
|
"testing"
|
|
|
|
|
2021-06-24 18:19:37 +00:00
|
|
|
"github.com/openark/golib/log"
|
|
|
|
test "github.com/openark/golib/tests"
|
2016-10-11 14:42:19 +00:00
|
|
|
)
|
|
|
|
|
2022-11-23 09:32:53 +00:00
|
|
|
const (
|
|
|
|
transactionIsolation = "REPEATABLE-READ"
|
|
|
|
)
|
|
|
|
|
2016-10-11 14:42:19 +00:00
|
|
|
func init() {
|
|
|
|
log.SetLevel(log.ERROR)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewConnectionConfig(t *testing.T) {
|
2022-11-25 17:38:12 +00:00
|
|
|
c := NewConnectionConfig()
|
2016-10-11 14:42:19 +00:00
|
|
|
test.S(t).ExpectEquals(c.Key.Hostname, "")
|
|
|
|
test.S(t).ExpectEquals(c.Key.Port, 0)
|
|
|
|
test.S(t).ExpectEquals(c.ImpliedKey.Hostname, "")
|
|
|
|
test.S(t).ExpectEquals(c.ImpliedKey.Port, 0)
|
|
|
|
test.S(t).ExpectEquals(c.User, "")
|
|
|
|
test.S(t).ExpectEquals(c.Password, "")
|
2022-11-25 17:38:12 +00:00
|
|
|
test.S(t).ExpectEquals(c.TransactionIsolation, "")
|
2016-10-11 14:42:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDuplicateCredentials(t *testing.T) {
|
2022-11-25 17:38:12 +00:00
|
|
|
c := NewConnectionConfig()
|
2016-10-11 14:42:19 +00:00
|
|
|
c.Key = InstanceKey{Hostname: "myhost", Port: 3306}
|
|
|
|
c.User = "gromit"
|
|
|
|
c.Password = "penguin"
|
2019-01-31 16:03:48 +00:00
|
|
|
c.tlsConfig = &tls.Config{
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
ServerName: "feathers",
|
|
|
|
}
|
2022-11-25 17:38:12 +00:00
|
|
|
c.TransactionIsolation = transactionIsolation
|
2016-10-11 14:42:19 +00:00
|
|
|
|
|
|
|
dup := c.DuplicateCredentials(InstanceKey{Hostname: "otherhost", Port: 3310})
|
|
|
|
test.S(t).ExpectEquals(dup.Key.Hostname, "otherhost")
|
|
|
|
test.S(t).ExpectEquals(dup.Key.Port, 3310)
|
|
|
|
test.S(t).ExpectEquals(dup.ImpliedKey.Hostname, "otherhost")
|
|
|
|
test.S(t).ExpectEquals(dup.ImpliedKey.Port, 3310)
|
|
|
|
test.S(t).ExpectEquals(dup.User, "gromit")
|
|
|
|
test.S(t).ExpectEquals(dup.Password, "penguin")
|
2019-01-31 16:03:48 +00:00
|
|
|
test.S(t).ExpectEquals(dup.tlsConfig, c.tlsConfig)
|
2022-11-25 17:38:12 +00:00
|
|
|
test.S(t).ExpectEquals(dup.TransactionIsolation, c.TransactionIsolation)
|
2016-10-11 14:42:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDuplicate(t *testing.T) {
|
2022-11-25 17:38:12 +00:00
|
|
|
c := NewConnectionConfig()
|
2016-10-11 14:42:19 +00:00
|
|
|
c.Key = InstanceKey{Hostname: "myhost", Port: 3306}
|
|
|
|
c.User = "gromit"
|
|
|
|
c.Password = "penguin"
|
2022-11-25 17:38:12 +00:00
|
|
|
c.TransactionIsolation = transactionIsolation
|
2016-10-11 14:42:19 +00:00
|
|
|
|
|
|
|
dup := c.Duplicate()
|
|
|
|
test.S(t).ExpectEquals(dup.Key.Hostname, "myhost")
|
|
|
|
test.S(t).ExpectEquals(dup.Key.Port, 3306)
|
|
|
|
test.S(t).ExpectEquals(dup.ImpliedKey.Hostname, "myhost")
|
|
|
|
test.S(t).ExpectEquals(dup.ImpliedKey.Port, 3306)
|
|
|
|
test.S(t).ExpectEquals(dup.User, "gromit")
|
|
|
|
test.S(t).ExpectEquals(dup.Password, "penguin")
|
2022-11-25 17:38:12 +00:00
|
|
|
test.S(t).ExpectEquals(dup.TransactionIsolation, transactionIsolation)
|
2016-10-11 14:42:19 +00:00
|
|
|
}
|
2018-03-12 14:20:46 +00:00
|
|
|
|
2018-01-08 15:43:10 +00:00
|
|
|
func TestGetDBUri(t *testing.T) {
|
2022-11-25 17:38:12 +00:00
|
|
|
c := NewConnectionConfig()
|
2018-01-08 15:43:10 +00:00
|
|
|
c.Key = InstanceKey{Hostname: "myhost", Port: 3306}
|
|
|
|
c.User = "gromit"
|
|
|
|
c.Password = "penguin"
|
2022-08-12 16:52:22 +00:00
|
|
|
c.Timeout = 1.2345
|
2022-11-25 17:38:12 +00:00
|
|
|
c.TransactionIsolation = transactionIsolation
|
2018-01-08 15:43:10 +00:00
|
|
|
|
2018-03-12 14:17:53 +00:00
|
|
|
uri := c.GetDBUri("test")
|
2022-08-12 16:52:22 +00:00
|
|
|
test.S(t).ExpectEquals(uri, `gromit:penguin@tcp(myhost:3306)/test?autocommit=true&charset=utf8mb4,utf8,latin1&interpolateParams=true&tls=false&transaction_isolation="REPEATABLE-READ"&timeout=1.234500s&readTimeout=1.234500s&writeTimeout=1.234500s`)
|
2019-01-31 16:03:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetDBUriWithTLSSetup(t *testing.T) {
|
2022-11-25 17:38:12 +00:00
|
|
|
c := NewConnectionConfig()
|
2019-01-31 16:03:48 +00:00
|
|
|
c.Key = InstanceKey{Hostname: "myhost", Port: 3306}
|
|
|
|
c.User = "gromit"
|
|
|
|
c.Password = "penguin"
|
2022-08-12 16:52:22 +00:00
|
|
|
c.Timeout = 1.2345
|
2019-01-31 16:03:48 +00:00
|
|
|
c.tlsConfig = &tls.Config{}
|
2022-11-25 17:38:12 +00:00
|
|
|
c.TransactionIsolation = transactionIsolation
|
2019-01-31 16:03:48 +00:00
|
|
|
|
|
|
|
uri := c.GetDBUri("test")
|
2022-08-12 16:52:22 +00:00
|
|
|
test.S(t).ExpectEquals(uri, `gromit:penguin@tcp(myhost:3306)/test?autocommit=true&charset=utf8mb4,utf8,latin1&interpolateParams=true&tls=ghost&transaction_isolation="REPEATABLE-READ"&timeout=1.234500s&readTimeout=1.234500s&writeTimeout=1.234500s`)
|
2018-01-08 15:43:10 +00:00
|
|
|
}
|