Skip to content

Commit

Permalink
Merge pull request #7641 from tinyspeck/am_vtadmin_test_refactors
Browse files Browse the repository at this point in the history
[vtadmin] test refactors
  • Loading branch information
rohit-nayak-ps committed Mar 9, 2021
2 parents 7e1e105 + 3f7e006 commit a3fa48c
Show file tree
Hide file tree
Showing 13 changed files with 764 additions and 560 deletions.
422 changes: 240 additions & 182 deletions go/vt/vtadmin/api_test.go

Large diffs are not rendered by default.

724 changes: 367 additions & 357 deletions go/vt/vtadmin/cluster/cluster_test.go

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions go/vt/vtadmin/cluster/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
)

func TestMergeConfig(t *testing.T) {
t.Parallel()

tests := []struct {
name string
base Config
Expand Down Expand Up @@ -131,14 +133,20 @@ func TestMergeConfig(t *testing.T) {
}

for _, tt := range tests {
tt := tt

t.Run(tt.name, func(t *testing.T) {
t.Parallel()

actual := tt.base.Merge(tt.override)
assert.Equal(t, tt.expected, actual)
})
}
}

func TestConfigUnmarshalYAML(t *testing.T) {
t.Parallel()

tests := []struct {
name string
yaml string
Expand Down Expand Up @@ -187,7 +195,11 @@ discovery-zk-whatever: 5
}

for _, tt := range tests {
tt := tt

t.Run(tt.name, func(t *testing.T) {
t.Parallel()

cfg := Config{
DiscoveryFlagsByImpl: map[string]map[string]string{},
}
Expand Down
30 changes: 27 additions & 3 deletions go/vt/vtadmin/cluster/discovery/discovery_consul_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ func consulServiceEntry(name string, tags []string, meta map[string]string) *con
}

func TestConsulDiscoverVTGates(t *testing.T) {
t.Parallel()

tests := []struct {
name string
disco *ConsulDiscovery
Expand Down Expand Up @@ -228,15 +230,21 @@ func TestConsulDiscoverVTGates(t *testing.T) {
},
}

ctx := context.Background()

for _, tt := range tests {
tt := tt

t.Run(tt.name, func(t *testing.T) {
t.Parallel()

tt.disco.client = &fakeConsulClient{
health: &fakeConsulHealth{
entries: tt.entries,
},
}

gates, err := tt.disco.DiscoverVTGates(context.Background(), tt.tags)
gates, err := tt.disco.DiscoverVTGates(ctx, tt.tags)
if tt.shouldErr {
assert.Error(t, err, assert.AnError)
return
Expand All @@ -249,6 +257,8 @@ func TestConsulDiscoverVTGates(t *testing.T) {
}

func TestConsulDiscoverVTGate(t *testing.T) {
t.Parallel()

tests := []struct {
name string
disco *ConsulDiscovery
Expand Down Expand Up @@ -331,15 +341,21 @@ func TestConsulDiscoverVTGate(t *testing.T) {
},
}

ctx := context.Background()

for _, tt := range tests {
tt := tt

t.Run(tt.name, func(t *testing.T) {
t.Parallel()

tt.disco.client = &fakeConsulClient{
health: &fakeConsulHealth{
entries: tt.entries,
},
}

gate, err := tt.disco.DiscoverVTGate(context.Background(), tt.tags)
gate, err := tt.disco.DiscoverVTGate(ctx, tt.tags)
if tt.shouldErr {
assert.Error(t, err, assert.AnError)
return
Expand All @@ -352,6 +368,8 @@ func TestConsulDiscoverVTGate(t *testing.T) {
}

func TestConsulDiscoverVTGateAddr(t *testing.T) {
t.Parallel()

tests := []struct {
name string
disco *ConsulDiscovery
Expand Down Expand Up @@ -421,15 +439,21 @@ func TestConsulDiscoverVTGateAddr(t *testing.T) {
},
}

ctx := context.Background()

for _, tt := range tests {
tt := tt

t.Run(tt.name, func(t *testing.T) {
t.Parallel()

tt.disco.client = &fakeConsulClient{
health: &fakeConsulHealth{
entries: tt.entries,
},
}

addr, err := tt.disco.DiscoverVTGateAddr(context.Background(), tt.tags)
addr, err := tt.disco.DiscoverVTGateAddr(ctx, tt.tags)
if tt.shouldErr {
assert.Error(t, err, assert.AnError)
return
Expand Down
40 changes: 36 additions & 4 deletions go/vt/vtadmin/cluster/discovery/discovery_static_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import (
)

func TestDiscoverVTGate(t *testing.T) {
t.Parallel()

tests := []struct {
name string
contents []byte
Expand Down Expand Up @@ -89,13 +91,19 @@ func TestDiscoverVTGate(t *testing.T) {
},
}

ctx := context.Background()

for _, tt := range tests {
tt := tt

t.Run(tt.name, func(t *testing.T) {
t.Parallel()

disco := &StaticFileDiscovery{}
err := disco.parseConfig(tt.contents)
require.NoError(t, err)

gate, err := disco.DiscoverVTGate(context.Background(), tt.tags)
gate, err := disco.DiscoverVTGate(ctx, tt.tags)
if tt.shouldErr {
assert.Error(t, err)
return
Expand All @@ -108,6 +116,8 @@ func TestDiscoverVTGate(t *testing.T) {
}

func TestDiscoverVTGates(t *testing.T) {
t.Parallel()

tests := []struct {
name string
contents []byte
Expand Down Expand Up @@ -226,8 +236,14 @@ func TestDiscoverVTGates(t *testing.T) {
},
}

ctx := context.Background()

for _, tt := range tests {
tt := tt

t.Run(tt.name, func(t *testing.T) {
t.Parallel()

disco := &StaticFileDiscovery{}

err := disco.parseConfig(tt.contents)
Expand All @@ -237,7 +253,7 @@ func TestDiscoverVTGates(t *testing.T) {
require.NoError(t, err)
}

gates, err := disco.DiscoverVTGates(context.Background(), tt.tags)
gates, err := disco.DiscoverVTGates(ctx, tt.tags)
if tt.shouldErr {
assert.Error(t, err)
return
Expand All @@ -250,6 +266,8 @@ func TestDiscoverVTGates(t *testing.T) {
}

func TestDiscoverVtctld(t *testing.T) {
t.Parallel()

tests := []struct {
name string
contents []byte
Expand Down Expand Up @@ -311,13 +329,19 @@ func TestDiscoverVtctld(t *testing.T) {
},
}

ctx := context.Background()

for _, tt := range tests {
tt := tt

t.Run(tt.name, func(t *testing.T) {
t.Parallel()

disco := &StaticFileDiscovery{}
err := disco.parseConfig(tt.contents)
require.NoError(t, err)

vtctld, err := disco.DiscoverVtctld(context.Background(), tt.tags)
vtctld, err := disco.DiscoverVtctld(ctx, tt.tags)
if tt.shouldErr {
assert.Error(t, err)
return
Expand All @@ -330,6 +354,8 @@ func TestDiscoverVtctld(t *testing.T) {
}

func TestDiscoverVtctlds(t *testing.T) {
t.Parallel()

tests := []struct {
name string
contents []byte
Expand Down Expand Up @@ -448,8 +474,14 @@ func TestDiscoverVtctlds(t *testing.T) {
},
}

ctx := context.Background()

for _, tt := range tests {
tt := tt

t.Run(tt.name, func(t *testing.T) {
t.Parallel()

disco := &StaticFileDiscovery{}

err := disco.parseConfig(tt.contents)
Expand All @@ -459,7 +491,7 @@ func TestDiscoverVtctlds(t *testing.T) {
require.NoError(t, err)
}

vtctlds, err := disco.DiscoverVtctlds(context.Background(), tt.tags)
vtctlds, err := disco.DiscoverVtctlds(ctx, tt.tags)
if tt.shouldErr {
assert.Error(t, err)
return
Expand Down
18 changes: 16 additions & 2 deletions go/vt/vtadmin/cluster/discovery/discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,18 @@ limitations under the License.
package discovery

import (
"fmt"
"testing"
"time"

"github.com/stretchr/testify/assert"

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

func TestNew(t *testing.T) {
t.Parallel()

tests := []struct {
name string
impl string
Expand All @@ -46,7 +50,11 @@ func TestNew(t *testing.T) {
}

for _, tt := range tests {
tt := tt

t.Run(tt.name, func(t *testing.T) {
t.Parallel()

disco, err := New(tt.impl, &vtadminpb.Cluster{Id: "testid", Name: "testcluster"}, []string{})
if tt.err != nil {
assert.Error(t, err, tt.err.Error())
Expand All @@ -60,7 +68,13 @@ func TestNew(t *testing.T) {
}

func TestRegister(t *testing.T) {
Register("testfactory", nil)
t.Parallel()

// Use a timestamp to allow running tests with `-count=N`.
ts := time.Now().UnixNano()
factoryName := fmt.Sprintf("testfactory-%d", ts)

Register(factoryName, nil)

defer func() {
err := recover()
Expand All @@ -70,6 +84,6 @@ func TestRegister(t *testing.T) {
}()

// this one panics
Register("testfactory", nil)
Register(factoryName, nil)
assert.Equal(t, 1, 2, "double register should have panicked")
}
16 changes: 10 additions & 6 deletions go/vt/vtadmin/cluster/discovery/fakediscovery/discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import (
)

func TestDiscoverVTGates(t *testing.T) {
t.Parallel()

fake := New()
gates := []*vtadminpb.VTGate{
{
Expand All @@ -39,33 +41,35 @@ func TestDiscoverVTGates(t *testing.T) {
},
}

ctx := context.Background()

fake.AddTaggedGates(nil, gates...)
fake.AddTaggedGates([]string{"tag1:val1"}, gates[0], gates[1])
fake.AddTaggedGates([]string{"tag2:val2"}, gates[0], gates[2])

actual, err := fake.DiscoverVTGates(context.Background(), nil)
actual, err := fake.DiscoverVTGates(ctx, nil)
assert.NoError(t, err)
assert.ElementsMatch(t, gates, actual)

actual, err = fake.DiscoverVTGates(context.Background(), []string{"tag1:val1"})
actual, err = fake.DiscoverVTGates(ctx, []string{"tag1:val1"})
assert.NoError(t, err)
assert.ElementsMatch(t, []*vtadminpb.VTGate{gates[0], gates[1]}, actual)

actual, err = fake.DiscoverVTGates(context.Background(), []string{"tag2:val2"})
actual, err = fake.DiscoverVTGates(ctx, []string{"tag2:val2"})
assert.NoError(t, err)
assert.ElementsMatch(t, []*vtadminpb.VTGate{gates[0], gates[2]}, actual)

actual, err = fake.DiscoverVTGates(context.Background(), []string{"tag1:val1", "tag2:val2"})
actual, err = fake.DiscoverVTGates(ctx, []string{"tag1:val1", "tag2:val2"})
assert.NoError(t, err)
assert.ElementsMatch(t, []*vtadminpb.VTGate{gates[0]}, actual)

actual, err = fake.DiscoverVTGates(context.Background(), []string{"differentTag:val"})
actual, err = fake.DiscoverVTGates(ctx, []string{"differentTag:val"})
assert.NoError(t, err)
assert.Equal(t, []*vtadminpb.VTGate{}, actual)

fake.SetGatesError(true)

actual, err = fake.DiscoverVTGates(context.Background(), nil)
actual, err = fake.DiscoverVTGates(ctx, nil)
assert.Error(t, err)
assert.Nil(t, actual)
}

0 comments on commit a3fa48c

Please sign in to comment.