62 lines
1.6 KiB
Go
62 lines
1.6 KiB
Go
package mysqlproxy
|
|
|
|
import (
|
|
"context"
|
|
"github.com/go-mysql-org/go-mysql/mysql"
|
|
"github.com/go-mysql-org/go-mysql/server"
|
|
"linkport/internal/model"
|
|
"net"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
type databaseBackend struct {
|
|
server.EmptyHandler
|
|
selected chan string
|
|
}
|
|
|
|
func (b *databaseBackend) UseDB(db string) error { b.selected <- db; return nil }
|
|
func (b *databaseBackend) HandleQuery(string) (*mysql.Result, error) {
|
|
return &mysql.Result{Status: mysql.SERVER_STATUS_AUTOCOMMIT}, nil
|
|
}
|
|
|
|
func TestDefaultDatabaseAndClientOverride(t *testing.T) {
|
|
for _, explicit := range []string{"", "client_db"} {
|
|
selected := make(chan string, 1)
|
|
p := model.Proxy{Type: "mysql", TargetHost: "fixture", TargetPort: 3306, TargetUser: "target", TargetPassword: "secret", Database: "default_db", Cluster: model.ClusterConfig{Mode: "single"}}
|
|
dial := func(context.Context, string, string) (net.Conn, error) {
|
|
c, s := net.Pipe()
|
|
go func() {
|
|
defer s.Close()
|
|
srv := server.NewServer("8.0.11", 45, mysql.AUTH_NATIVE_PASSWORD, nil, nil)
|
|
conn, err := newNativeConn(srv, s, "target", "secret", &databaseBackend{selected: selected})
|
|
if err == nil {
|
|
for conn.HandleCommand() == nil {
|
|
}
|
|
}
|
|
}()
|
|
return c, nil
|
|
}
|
|
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
|
c, err := open(ctx, p, dial, explicit)
|
|
if err != nil {
|
|
cancel()
|
|
t.Fatal(err)
|
|
}
|
|
c.Close()
|
|
cancel()
|
|
want := explicit
|
|
if want == "" {
|
|
want = p.Database
|
|
}
|
|
select {
|
|
case got := <-selected:
|
|
if got != want {
|
|
t.Fatalf("database=%q want=%q", got, want)
|
|
}
|
|
case <-time.After(time.Second):
|
|
t.Fatal("database not selected")
|
|
}
|
|
}
|
|
}
|