Skip to content

Commit

Permalink
test: Add/Fix unit tests for the check
Browse files Browse the repository at this point in the history
Signed-off-by: Noble Mittal <[email protected]>
  • Loading branch information
beingnoble03 committed Oct 2, 2024
1 parent e1bff4b commit 305bfd7
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 7 deletions.
6 changes: 6 additions & 0 deletions go/vt/vtctl/workflow/materializer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const (
mzGetCopyState = "select distinct table_name from _vt.copy_state cs, _vt.vreplication vr where vr.id = cs.vrepl_id and vr.id = 1"
mzGetLatestCopyState = "select vrepl_id, table_name, lastpk from _vt.copy_state where vrepl_id in (1) and id in (select max(id) from _vt.copy_state where vrepl_id in (1) group by vrepl_id, table_name)"
insertPrefix = `/insert into _vt.vreplication\(workflow, source, pos, max_tps, max_replication_lag, cell, tablet_types, time_updated, transaction_timestamp, state, db_name, workflow_type, workflow_sub_type, defer_secondary_keys, options\) values `
getNonEmptyTable = "select 1 from t1 limit 1"
)

var (
Expand Down Expand Up @@ -519,6 +520,7 @@ func TestMigrateVSchema(t *testing.T) {
defer env.close()

env.tmc.expectVRQuery(100, mzCheckJournal, &sqltypes.Result{})
env.tmc.expectVRQuery(200, getNonEmptyTable, &sqltypes.Result{})
env.tmc.expectVRQuery(200, mzGetCopyState, &sqltypes.Result{})
env.tmc.expectVRQuery(200, mzGetLatestCopyState, &sqltypes.Result{})

Expand Down Expand Up @@ -577,6 +579,7 @@ func TestMoveTablesDDLFlag(t *testing.T) {
// a circular dependency.
// The TabletManager portion is tested in rpc_vreplication_test.go.
env.tmc.expectVRQuery(100, mzCheckJournal, &sqltypes.Result{})
env.tmc.expectVRQuery(200, getNonEmptyTable, &sqltypes.Result{})
env.tmc.expectVRQuery(200, mzGetCopyState, &sqltypes.Result{})
env.tmc.expectVRQuery(200, mzGetLatestCopyState, &sqltypes.Result{})

Expand Down Expand Up @@ -626,6 +629,7 @@ func TestMoveTablesNoRoutingRules(t *testing.T) {
// a circular dependency.
// The TabletManager portion is tested in rpc_vreplication_test.go.
env.tmc.expectVRQuery(100, mzCheckJournal, &sqltypes.Result{})
env.tmc.expectVRQuery(200, getNonEmptyTable, &sqltypes.Result{})
env.tmc.expectVRQuery(200, mzGetCopyState, &sqltypes.Result{})
env.tmc.expectVRQuery(200, mzGetLatestCopyState, &sqltypes.Result{})

Expand Down Expand Up @@ -744,6 +748,7 @@ func TestCreateLookupVindexFull(t *testing.T) {
}

env.tmc.expectVRQuery(100, mzCheckJournal, &sqltypes.Result{})
env.tmc.expectVRQuery(200, "select 1 from lookup limit 1", &sqltypes.Result{})
env.tmc.expectVRQuery(200, "/CREATE TABLE `lookup`", &sqltypes.Result{})
env.tmc.expectVRQuery(200, mzGetCopyState, &sqltypes.Result{})
env.tmc.expectVRQuery(200, mzGetLatestCopyState, &sqltypes.Result{})
Expand Down Expand Up @@ -2691,6 +2696,7 @@ func TestKeyRangesEqualOptimization(t *testing.T) {
if tablet.Keyspace != targetKs || tablet.Type != topodatapb.TabletType_PRIMARY {
continue
}
env.tmc.expectVRQuery(int(tablet.Alias.Uid), getNonEmptyTable, &sqltypes.Result{})
// If we are doing a partial MoveTables, we will only perform the workflow
// stream creation / INSERT statment on the shard(s) we're migrating.
if len(tc.moveTablesReq.SourceShards) > 0 && !slices.Contains(tc.moveTablesReq.SourceShards, tablet.Shard) {
Expand Down
125 changes: 124 additions & 1 deletion go/vt/vtctl/workflow/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,21 @@ import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
clientv3 "go.etcd.io/etcd/client/v3"

"vitess.io/vitess/go/testfiles"
"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/proto/vtctldata"
"vitess.io/vitess/go/vt/topo"
"vitess.io/vitess/go/vt/topo/etcd2topo"
"vitess.io/vitess/go/vt/topo/memorytopo"
"vitess.io/vitess/go/vt/topotools"
"vitess.io/vitess/go/vt/vtctl/grpcvtctldserver/testutil"

querypb "vitess.io/vitess/go/vt/proto/query"
topodatapb "vitess.io/vitess/go/vt/proto/topodata"
"vitess.io/vitess/go/vt/proto/vtctldata"
)

// TestCreateDefaultShardRoutingRules confirms that the default shard routing rules are created correctly for sharded
Expand Down Expand Up @@ -243,3 +248,121 @@ func startEtcd(t *testing.T) string {

return clientAddr
}

func TestValidateEmptyTables(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ts := memorytopo.NewServer(ctx, "zone1")
defer ts.Close()

ks := "test_keyspace"
shard1 := "-40"
shard2 := "40-80"
shard3 := "80-"
err := ts.CreateKeyspace(ctx, ks, &topodatapb.Keyspace{})
require.NoError(t, err)

err = ts.CreateShard(ctx, ks, shard1)
require.NoError(t, err)
err = ts.CreateShard(ctx, ks, shard2)
require.NoError(t, err)
err = ts.CreateShard(ctx, ks, shard3)
require.NoError(t, err)

tablet1 := &topodatapb.Tablet{
Alias: &topodatapb.TabletAlias{
Cell: "zone1",
Uid: 100,
},
Keyspace: ks,
Shard: shard1,
}
tablet2 := &topodatapb.Tablet{
Alias: &topodatapb.TabletAlias{
Cell: "zone1",
Uid: 200,
},
Keyspace: ks,
Shard: shard2,
}
tablet3 := &topodatapb.Tablet{
Alias: &topodatapb.TabletAlias{
Cell: "zone1",
Uid: 300,
},
Keyspace: ks,
Shard: shard3,
}
err = ts.CreateTablet(ctx, tablet1)
require.NoError(t, err)
err = ts.CreateTablet(ctx, tablet2)
require.NoError(t, err)
err = ts.CreateTablet(ctx, tablet3)
require.NoError(t, err)

s1, err := ts.UpdateShardFields(ctx, ks, shard1, func(si *topo.ShardInfo) error {
si.Shard.PrimaryAlias = tablet1.Alias
return nil
})
require.NoError(t, err)
s2, err := ts.UpdateShardFields(ctx, ks, shard2, func(si *topo.ShardInfo) error {
si.Shard.PrimaryAlias = tablet2.Alias
return nil
})
require.NoError(t, err)
s3, err := ts.UpdateShardFields(ctx, ks, shard3, func(si *topo.ShardInfo) error {
si.Shard.PrimaryAlias = tablet3.Alias
return nil
})
require.NoError(t, err)

tmc := &testutil.TabletManagerClient{
ExecuteFetchAsDbaResults: map[string]struct {
Response *querypb.QueryResult
Error error
}{
"zone1-0000000100": {
Response: &querypb.QueryResult{
Rows: []*querypb.Row{{
Lengths: []int64{1},
Values: []byte("1"),
},
},
},
},
"zone1-0000000200": {
Response: &querypb.QueryResult{
Rows: []*querypb.Row{{
Lengths: []int64{1},
Values: []byte("1"),
},
},
},
},
"zone1-0000000300": {
Response: &querypb.QueryResult{
Rows: []*querypb.Row{{}},
},
},
},
}

tableSettings := []*vtctldata.TableMaterializeSettings{
{
TargetTable: "table1",
},
{
TargetTable: "table2",
},
{
TargetTable: "table3",
},
}
err = validateEmptyTables(ctx, ts, tmc, []*topo.ShardInfo{s1, s2, s3}, tableSettings)
assert.ErrorContains(t, err, "table1")
assert.ErrorContains(t, err, "table2")
assert.ErrorContains(t, err, "table3")

err = validateEmptyTables(ctx, ts, tmc, []*topo.ShardInfo{s1, s2, s3}, []*vtctldata.TableMaterializeSettings{})
assert.NoError(t, err, "should not throw any error for empty table settings slice")
}
4 changes: 2 additions & 2 deletions go/vt/vttablet/tabletmanager/framework_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -480,8 +480,8 @@ func (tmc *fakeTMClient) VReplicationExec(ctx context.Context, tablet *topodatap
}
for qry, res := range tmc.vreQueries[int(tablet.Alias.Uid)] {
if strings.HasPrefix(qry, "/") {
re := regexp.MustCompile(qry)
if re.MatchString(qry) {
re := regexp.MustCompile(qry[1:])
if re.MatchString(query) {
return res, nil
}
}
Expand Down
Loading

0 comments on commit 305bfd7

Please sign in to comment.