Skip to content

Commit

Permalink
Add more unit tests. Remove old tests kept previously in comments.
Browse files Browse the repository at this point in the history
Signed-off-by: Rohit Nayak <rohit@planetscale.com>
  • Loading branch information
rohit-nayak-ps committed May 22, 2024
1 parent 9b4e1dd commit d76d73c
Showing 1 changed file with 41 additions and 47 deletions.
88 changes: 41 additions & 47 deletions go/vt/topo/shard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,14 @@ func TestUpdateSourceDeniedTables(t *testing.T) {
wantTabletControl *topodatapb.Shard_TabletControl
}

// These tests update the state of the shard tablet controls, so subsequent tests
// depend on the cumulative state from the previous tests.
testCases := []testCase{
{
name: "enforce keyspace lock",
ctx: ctx,
tabletType: topodatapb.TabletType_RDONLY,
cells: []string{"first"},

wantError: "keyspace ks is not locked (no locksInfo)",
},
Expand Down Expand Up @@ -221,15 +224,51 @@ func TestUpdateSourceDeniedTables(t *testing.T) {
DeniedTables: []string{"t1", "t2"},
},
},
{
name: "add replica tablet type",
tabletType: topodatapb.TabletType_REPLICA,
cells: []string{"first"},
tables: []string{"t1", "t2"},
wantTabletControl: &topodatapb.Shard_TabletControl{
TabletType: topodatapb.TabletType_REPLICA,
Cells: []string{"first"},
DeniedTables: []string{"t1", "t2"},
},
},
{
name: "confirm rdonly still stays the same, after replica was added",
tabletType: topodatapb.TabletType_RDONLY,
wantTabletControl: &topodatapb.Shard_TabletControl{
TabletType: topodatapb.TabletType_RDONLY,
Cells: []string{"first", "third"},
DeniedTables: []string{"t1", "t2"},
},
},
{
name: "remove rdonly entry",
tabletType: topodatapb.TabletType_RDONLY,
cells: []string{"first", "third"},
remove: true,
tables: []string{"t1", "t2"},
},
{
name: "remove replica entry",
tabletType: topodatapb.TabletType_REPLICA,
cells: []string{"first", "third"},
remove: true,
tables: []string{"t1", "t2"},
},
}

for _, tcase := range testCases {
t.Run(tcase.name, func(t *testing.T) {
if tcase.ctx == nil {
tcase.ctx = ctxWithLock
}

err := si.UpdateDeniedTables(tcase.ctx, tcase.tabletType, tcase.cells, tcase.remove, tcase.tables)
var err error
if tcase.tables != nil || tcase.cells != nil {
err = si.UpdateDeniedTables(tcase.ctx, tcase.tabletType, tcase.cells, tcase.remove, tcase.tables)
}
if tcase.wantError != "" {
require.Error(t, err)
require.Contains(t, err.Error(), tcase.wantError)
Expand All @@ -244,51 +283,6 @@ func TestUpdateSourceDeniedTables(t *testing.T) {
}
})
}
/*
// re-add a cell, then another with different table list to
// make sure it fails
if err := si.UpdateDeniedTables(ctx, topodatapb.TabletType_RDONLY, []string{"first"}, false, []string{"t1", "t2"}); err != nil {
t.Fatalf("one cell add failed: %v", si)
}
if err := si.UpdateDeniedTables(ctx, topodatapb.TabletType_RDONLY, []string{"second"}, false, []string{"t2", "t3"}); err == nil || err.Error() != "trying to use two different sets of denied tables for shard ks/sh: [t1 t2] and [t2 t3]" {
t.Fatalf("different table list should fail: %v", err)
}
// add another cell, see the list grow
if err := si.UpdateDeniedTables(ctx, topodatapb.TabletType_RDONLY, []string{"second"}, false, []string{"t1", "t2"}); err != nil || !reflect.DeepEqual(si.TabletControls, []*topodatapb.Shard_TabletControl{
{
TabletType: topodatapb.TabletType_RDONLY,
Cells: []string{"first", "second"},
DeniedTables: []string{"t1", "t2"},
},
}) {
t.Fatalf("second cell add failed: %v", si)
}
// add all cells, see the list grow to all
if err := si.UpdateDeniedTables(ctx, topodatapb.TabletType_RDONLY, []string{"first", "second", "third"}, false, []string{"t1", "t2"}); err != nil || !reflect.DeepEqual(si.TabletControls, []*topodatapb.Shard_TabletControl{
{
TabletType: topodatapb.TabletType_RDONLY,
Cells: []string{"first", "second", "third"},
DeniedTables: []string{"t1", "t2"},
},
}) {
t.Fatalf("all cells add failed: %v", si)
}
// remove one cell from the full list
if err := si.UpdateDeniedTables(ctx, topodatapb.TabletType_RDONLY, []string{"second"}, true, []string{"t1", "t2"}); err != nil || !reflect.DeepEqual(si.TabletControls, []*topodatapb.Shard_TabletControl{
{
TabletType: topodatapb.TabletType_RDONLY,
Cells: []string{"first", "third"},
DeniedTables: []string{"t1", "t2"},
},
}) {
t.Fatalf("one cell removal from all failed: %v", si)
}
*/
}

func TestValidateShardName(t *testing.T) {
Expand Down

0 comments on commit d76d73c

Please sign in to comment.