Skip to content

Commit

Permalink
Move BuildCluster to vtadmin/testutil
Browse files Browse the repository at this point in the history
Signed-off-by: Sara Bee <[email protected]>
  • Loading branch information
doeg committed Feb 25, 2021
1 parent 3815581 commit ad990e4
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 74 deletions.
89 changes: 18 additions & 71 deletions go/vt/vtadmin/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,21 @@ package vtadmin

import (
"context"
"database/sql"
"errors"
"fmt"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"

"vitess.io/vitess/go/vt/grpcclient"
"vitess.io/vitess/go/vt/topo"
"vitess.io/vitess/go/vt/topo/memorytopo"
"vitess.io/vitess/go/vt/topo/topoproto"
"vitess.io/vitess/go/vt/vitessdriver"
"vitess.io/vitess/go/vt/vtadmin/cluster"
"vitess.io/vitess/go/vt/vtadmin/cluster/discovery/fakediscovery"
vtadminerrors "vitess.io/vitess/go/vt/vtadmin/errors"
"vitess.io/vitess/go/vt/vtadmin/grpcserver"
"vitess.io/vitess/go/vt/vtadmin/http"
vtadmintestutil "vitess.io/vitess/go/vt/vtadmin/testutil"
vtadminvtctldclient "vitess.io/vitess/go/vt/vtadmin/vtctldclient"
"vitess.io/vitess/go/vt/vtadmin/vtsql"
"vitess.io/vitess/go/vt/vtadmin/vtsql/fakevtsql"
"vitess.io/vitess/go/vt/vtctl/grpcvtctldserver"
"vitess.io/vitess/go/vt/vtctl/grpcvtctldserver/testutil"
"vitess.io/vitess/go/vt/vtctl/vtctldclient"
Expand Down Expand Up @@ -398,8 +390,8 @@ func TestGetKeyspaces(t *testing.T) {
clusterClients := []vtctldclient.VtctldClient{cluster0Client, cluster1Client}

clusters := []*cluster.Cluster{
buildCluster(0, clusterClients[0], nil, nil),
buildCluster(1, clusterClients[1], nil, nil),
vtadmintestutil.BuildCluster(0, clusterClients[0], nil, nil),
vtadmintestutil.BuildCluster(1, clusterClients[1], nil, nil),
}

api := NewAPI(clusters, grpcserver.Options{}, http.Options{})
Expand Down Expand Up @@ -900,7 +892,7 @@ func TestGetSchemas(t *testing.T) {
}
}

clusters[cdx] = buildCluster(cdx, clusterClients[cdx], cts, nil)
clusters[cdx] = vtadmintestutil.BuildCluster(cdx, clusterClients[cdx], cts, nil)
}

api := NewAPI(clusters, grpcserver.Options{}, http.Options{})
Expand All @@ -919,7 +911,7 @@ func TestGetTablets(t *testing.T) {
tests := []struct {
name string
clusterTablets [][]*vtadminpb.Tablet
dbconfigs map[string]*dbcfg
dbconfigs map[string]*vtadmintestutil.Dbcfg
req *vtadminpb.GetTabletsRequest
expected []*vtadminpb.Tablet
shouldErr bool
Expand All @@ -944,7 +936,7 @@ func TestGetTablets(t *testing.T) {
},
},
},
dbconfigs: map[string]*dbcfg{},
dbconfigs: map[string]*vtadmintestutil.Dbcfg{},
req: &vtadminpb.GetTabletsRequest{},
expected: []*vtadminpb.Tablet{
{
Expand Down Expand Up @@ -1003,8 +995,8 @@ func TestGetTablets(t *testing.T) {
},
},
},
dbconfigs: map[string]*dbcfg{
"c1": {shouldErr: true},
dbconfigs: map[string]*vtadmintestutil.Dbcfg{
"c1": {ShouldErr: true},
},
req: &vtadminpb.GetTabletsRequest{},
expected: nil,
Expand Down Expand Up @@ -1046,7 +1038,7 @@ func TestGetTablets(t *testing.T) {
},
},
},
dbconfigs: map[string]*dbcfg{},
dbconfigs: map[string]*vtadmintestutil.Dbcfg{},
req: &vtadminpb.GetTabletsRequest{ClusterIds: []string{"c0"}},
expected: []*vtadminpb.Tablet{
{
Expand Down Expand Up @@ -1076,7 +1068,7 @@ func TestGetTablets(t *testing.T) {
clusters := make([]*cluster.Cluster, len(tt.clusterTablets))

for i, tablets := range tt.clusterTablets {
cluster := buildCluster(i, nil, tablets, tt.dbconfigs)
cluster := vtadmintestutil.BuildCluster(i, nil, tablets, tt.dbconfigs)
clusters[i] = cluster
}

Expand All @@ -1097,7 +1089,7 @@ func TestGetTablet(t *testing.T) {
tests := []struct {
name string
clusterTablets [][]*vtadminpb.Tablet
dbconfigs map[string]*dbcfg
dbconfigs map[string]*vtadmintestutil.Dbcfg
req *vtadminpb.GetTabletRequest
expected *vtadminpb.Tablet
shouldErr bool
Expand All @@ -1122,7 +1114,7 @@ func TestGetTablet(t *testing.T) {
},
},
},
dbconfigs: map[string]*dbcfg{},
dbconfigs: map[string]*vtadmintestutil.Dbcfg{},
req: &vtadminpb.GetTabletRequest{
Hostname: "ks1-00-00-zone1-a",
},
Expand Down Expand Up @@ -1181,8 +1173,8 @@ func TestGetTablet(t *testing.T) {
},
},
},
dbconfigs: map[string]*dbcfg{
"c1": {shouldErr: true},
dbconfigs: map[string]*vtadmintestutil.Dbcfg{
"c1": {ShouldErr: true},
},
req: &vtadminpb.GetTabletRequest{
Hostname: "doesn't matter",
Expand Down Expand Up @@ -1226,7 +1218,7 @@ func TestGetTablet(t *testing.T) {
},
},
},
dbconfigs: map[string]*dbcfg{},
dbconfigs: map[string]*vtadmintestutil.Dbcfg{},
req: &vtadminpb.GetTabletRequest{
Hostname: "ks1-00-00-zone1-a",
ClusterIds: []string{"c0"},
Expand Down Expand Up @@ -1286,7 +1278,7 @@ func TestGetTablet(t *testing.T) {
},
},
},
dbconfigs: map[string]*dbcfg{},
dbconfigs: map[string]*vtadmintestutil.Dbcfg{},
req: &vtadminpb.GetTabletRequest{
Hostname: "ks1-00-00-zone1-a",
},
Expand All @@ -1299,7 +1291,7 @@ func TestGetTablet(t *testing.T) {
/* cluster 0 */
{},
},
dbconfigs: map[string]*dbcfg{},
dbconfigs: map[string]*vtadmintestutil.Dbcfg{},
req: &vtadminpb.GetTabletRequest{
Hostname: "ks1-00-00-zone1-a",
},
Expand All @@ -1313,7 +1305,7 @@ func TestGetTablet(t *testing.T) {
clusters := make([]*cluster.Cluster, len(tt.clusterTablets))

for i, tablets := range tt.clusterTablets {
cluster := buildCluster(i, nil, tablets, tt.dbconfigs)
cluster := vtadmintestutil.BuildCluster(i, nil, tablets, tt.dbconfigs)
clusters[i] = cluster
}

Expand Down Expand Up @@ -1588,7 +1580,7 @@ func TestVTExplain(t *testing.T) {
}
}

c := buildCluster(0, vtctldClient, tt.tablets, nil)
c := vtadmintestutil.BuildCluster(0, vtctldClient, tt.tablets, nil)
clusters := []*cluster.Cluster{c}

api := NewAPI(clusters, grpcserver.Options{}, http.Options{})
Expand All @@ -1608,51 +1600,6 @@ func TestVTExplain(t *testing.T) {
}
}

type dbcfg struct {
shouldErr bool
}

// shared helper for building a cluster that contains the given tablets and
// talking to the given vtctld server. dbconfigs contains an optional config
// for controlling the behavior of the cluster's DB at the package sql level.
func buildCluster(i int, vtctldClient vtctldclient.VtctldClient, tablets []*vtadminpb.Tablet, dbconfigs map[string]*dbcfg) *cluster.Cluster {
disco := fakediscovery.New()
disco.AddTaggedGates(nil, &vtadminpb.VTGate{Hostname: fmt.Sprintf("cluster%d-gate", i)})
disco.AddTaggedVtctlds(nil, &vtadminpb.Vtctld{Hostname: "doesn't matter"})

cluster := &cluster.Cluster{
ID: fmt.Sprintf("c%d", i),
Name: fmt.Sprintf("cluster%d", i),
Discovery: disco,
}

dbconfig, ok := dbconfigs[cluster.ID]
if !ok {
dbconfig = &dbcfg{shouldErr: false}
}

db := vtsql.New(&vtsql.Config{
Cluster: cluster.ToProto(),
Discovery: disco,
})
db.DialFunc = func(cfg vitessdriver.Configuration) (*sql.DB, error) {
return sql.OpenDB(&fakevtsql.Connector{Tablets: tablets, ShouldErr: dbconfig.shouldErr}), nil
}

vtctld := vtadminvtctldclient.New(&vtadminvtctldclient.Config{
Cluster: cluster.ToProto(),
Discovery: disco,
})
vtctld.DialFunc = func(addr string, ff grpcclient.FailFast, opts ...grpc.DialOption) (vtctldclient.VtctldClient, error) {
return vtctldClient, nil
}

cluster.DB = db
cluster.Vtctld = vtctld

return cluster
}

func init() {
// For tests that don't actually care about mocking the tmclient (i.e. they
// call grpcvtctldserver.NewVtctldServer to initialize the unit under test),
Expand Down
7 changes: 4 additions & 3 deletions go/vt/vtadmin/cluster/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package cluster
package cluster_test

import (
"context"
Expand All @@ -24,6 +24,7 @@ import (
"github.com/stretchr/testify/assert"

"vitess.io/vitess/go/vt/vitessdriver"
"vitess.io/vitess/go/vt/vtadmin/cluster"
"vitess.io/vitess/go/vt/vtadmin/cluster/discovery/fakediscovery"
"vitess.io/vitess/go/vt/vtadmin/vtsql"

Expand All @@ -45,7 +46,7 @@ func TestGetTablets(t *testing.T) {
return nil, assert.AnError
}

cluster := &Cluster{DB: db}
_, err := cluster.GetTablets(context.Background())
c := &cluster.Cluster{DB: db}
_, err := c.GetTablets(context.Background())
assert.Error(t, err)
}
80 changes: 80 additions & 0 deletions go/vt/vtadmin/testutil/cluster.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
Copyright 2021 The Vitess Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package testutil

import (
"database/sql"
"fmt"

"google.golang.org/grpc"

"vitess.io/vitess/go/vt/grpcclient"
"vitess.io/vitess/go/vt/vitessdriver"
"vitess.io/vitess/go/vt/vtadmin/cluster"
"vitess.io/vitess/go/vt/vtadmin/cluster/discovery/fakediscovery"
vtadminvtctldclient "vitess.io/vitess/go/vt/vtadmin/vtctldclient"
"vitess.io/vitess/go/vt/vtadmin/vtsql"
"vitess.io/vitess/go/vt/vtadmin/vtsql/fakevtsql"
"vitess.io/vitess/go/vt/vtctl/vtctldclient"

vtadminpb "vitess.io/vitess/go/vt/proto/vtadmin"
)

type Dbcfg struct {
ShouldErr bool
}

// shared helper for building a cluster that contains the given tablets and
// talking to the given vtctld server. dbconfigs contains an optional config
// for controlling the behavior of the cluster's DB at the package sql level.
func BuildCluster(i int, vtctldClient vtctldclient.VtctldClient, tablets []*vtadminpb.Tablet, dbconfigs map[string]*Dbcfg) *cluster.Cluster {
disco := fakediscovery.New()
disco.AddTaggedGates(nil, &vtadminpb.VTGate{Hostname: fmt.Sprintf("cluster%d-gate", i)})
disco.AddTaggedVtctlds(nil, &vtadminpb.Vtctld{Hostname: "doesn't matter"})

cluster := &cluster.Cluster{
ID: fmt.Sprintf("c%d", i),
Name: fmt.Sprintf("cluster%d", i),
Discovery: disco,
}

dbconfig, ok := dbconfigs[cluster.ID]
if !ok {
dbconfig = &Dbcfg{ShouldErr: false}
}

db := vtsql.New(&vtsql.Config{
Cluster: cluster.ToProto(),
Discovery: disco,
})
db.DialFunc = func(cfg vitessdriver.Configuration) (*sql.DB, error) {
return sql.OpenDB(&fakevtsql.Connector{Tablets: tablets, ShouldErr: dbconfig.ShouldErr}), nil
}

vtctld := vtadminvtctldclient.New(&vtadminvtctldclient.Config{
Cluster: cluster.ToProto(),
Discovery: disco,
})
vtctld.DialFunc = func(addr string, ff grpcclient.FailFast, opts ...grpc.DialOption) (vtctldclient.VtctldClient, error) {
return vtctldClient, nil
}

cluster.DB = db
cluster.Vtctld = vtctld

return cluster
}

0 comments on commit ad990e4

Please sign in to comment.