Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vtctldclient: Fix Apply (Shard | Keyspace| Table) Routing Rules commands #16096

Merged
merged 11 commits into from
Jun 12, 2024
2 changes: 1 addition & 1 deletion go/cmd/vtctldclient/command/routing_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func commandApplyRoutingRules(cmd *cobra.Command, args []string) error {
}

rr := &vschemapb.RoutingRules{}
if err := json2.Unmarshal(rulesBytes, &rr); err != nil {
if err := json2.Unmarshal(rulesBytes, rr); err != nil {
rohit-nayak-ps marked this conversation as resolved.
Show resolved Hide resolved
return err
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ package vreplication
import (
"encoding/json"
"fmt"
"os"
"slices"
"strings"
"testing"

"vitess.io/vitess/go/json2"

"github.com/stretchr/testify/require"
"golang.org/x/exp/maps"
"google.golang.org/protobuf/encoding/protojson"
Expand Down Expand Up @@ -61,6 +64,9 @@ func TestVtctldclientCLI(t *testing.T) {
workflowName := "wf1"
targetTabs := setupMinimalCustomerKeyspace(t)

t.Run("RoutingRulesCommands", func(t *testing.T) {
testRoutingRulesCommands(t)
})
t.Run("WorkflowList", func(t *testing.T) {
testWorkflowList(t, sourceKeyspaceName, targetKeyspaceName)
})
Expand Down Expand Up @@ -88,6 +94,76 @@ func TestVtctldclientCLI(t *testing.T) {
})
}

func testRoutingRulesCommands(t *testing.T) {
rr := &vschemapb.RoutingRules{
Rules: []*vschemapb.RoutingRule{
{
FromTable: "from1",
ToTables: []string{"to1", "to2"},
},
},
}
rules, err := json2.MarshalPB(rr)
require.NoError(t, err)

type routingRulesTest struct {
name string
typ string
rules string
useFile bool
}
tests := []routingRulesTest{
{
name: "inline",
typ: "RoutingRules",
rules: string(rules),
},
{
name: "file",
typ: "RoutingRules",
rules: string(rules),
useFile: true,
},
{
name: "empty",
typ: "RoutingRules",
rules: "{}",
},
}
for _, tt := range tests {
t.Run(tt.typ+"/"+tt.name, func(t *testing.T) {
var args []string
apply := fmt.Sprintf("Apply%s", tt.typ)
get := fmt.Sprintf("Get%s", tt.typ)
args = append(args, apply)
if tt.useFile {
tmpFile, err := os.CreateTemp("", fmt.Sprintf("%s_rules.json", tt.name))
require.NoError(t, err)
defer os.Remove(tmpFile.Name())
_, err = tmpFile.WriteString(tt.rules)
require.NoError(t, err)
args = append(args, "--rules-file", tmpFile.Name())
} else {
args = append(args, "--rules", tt.rules)
}
var output string
var err error
if output, err = vc.VtctldClient.ExecuteCommandWithOutput(args...); err != nil {
require.FailNowf(t, "failed action", apply, "%v: %s", err, output)
}
if output, err = vc.VtctldClient.ExecuteCommandWithOutput(get); err != nil {
require.FailNowf(t, "failed action", get, "%v: %s", err, output)
}
var want = &vschemapb.RoutingRules{}
require.NoError(t, json2.Unmarshal([]byte(tt.rules), want))
var got = &vschemapb.RoutingRules{}
require.NoError(t, json2.Unmarshal([]byte(output), got))
require.EqualValues(t, want, got)
})
}

}

// Tests several create flags and some complete flags and validates that some of them are set correctly for the workflow.
func testMoveTablesFlags1(t *testing.T, mt *iMoveTables, sourceKeyspace, targetKeyspace, workflowName string, targetTabs map[string]*cluster.VttabletProcess) {
tables := "customer,customer2"
Expand Down
Loading