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

[release-18.0] VReplication: error on vtctldclient commands w/o tablet types (#14294) #14298

Merged
merged 1 commit into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ go.sum @ajm188 @deepthi @harshit-gangal @mattlord @rohit-nayak-ps @systay @froui
/go/cache @vmg
/go/cmd @ajm188 @deepthi
/go/cmd/vtadmin @ajm188 @notfelineit
/go/cmd/vtctldclient @ajm188 @notfelineit
/go/cmd/vtctldclient @ajm188 @mattlord
/go/cmd/vtctldclient/command/vreplication @mattlord @rohit-nayak-ps
/go/internal/flag @ajm188 @rohit-nayak-ps
/go/mysql @harshit-gangal @systay @mattlord
/go/pools @deepthi @harshit-gangal
Expand Down
15 changes: 12 additions & 3 deletions go/cmd/vtctldclient/command/vreplication/common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,17 @@ func ParseCells(cmd *cobra.Command) {
}
}

func ParseTabletTypes(cmd *cobra.Command) {
if !cmd.Flags().Lookup("tablet-types").Changed {
func ParseTabletTypes(cmd *cobra.Command) error {
ttf := cmd.Flags().Lookup("tablet-types")
if ttf == nil {
return fmt.Errorf("no tablet-types flag found")
}
if !ttf.Changed {
CreateOptions.TabletTypes = tabletTypesDefault
} else if strings.TrimSpace(ttf.Value.String()) == "" {
return fmt.Errorf("invalid tablet-types value, at least one valid tablet type must be specified")
}
return nil
}

func validateOnDDL(cmd *cobra.Command) error {
Expand All @@ -124,7 +131,9 @@ func ParseAndValidateCreateOptions(cmd *cobra.Command) error {
return err
}
ParseCells(cmd)
ParseTabletTypes(cmd)
if err := ParseTabletTypes(cmd); err != nil {
return err
}
return nil
}

Expand Down
82 changes: 82 additions & 0 deletions go/cmd/vtctldclient/command/vreplication/common/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
Copyright 2023 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 common

import (
"testing"

"github.com/spf13/cobra"
)

func TestParseAndValidateCreateOptions(t *testing.T) {
tests := []struct {
name string
setFunc func(*cobra.Command) error
wantErr bool
}{
{
name: "invalid tablet type",
setFunc: func(cmd *cobra.Command) error {
tabletTypesFlag := cmd.Flags().Lookup("tablet-types")
err := tabletTypesFlag.Value.Set("invalid")
tabletTypesFlag.Changed = true
return err
},
wantErr: true,
},
{
name: "no tablet types",
setFunc: func(cmd *cobra.Command) error {
tabletTypesFlag := cmd.Flags().Lookup("tablet-types")
err := tabletTypesFlag.Value.Set("")
tabletTypesFlag.Changed = true
return err
},
wantErr: true,
},
{
name: "valid tablet types",
setFunc: func(cmd *cobra.Command) error {
tabletTypesFlag := cmd.Flags().Lookup("tablet-types")
err := tabletTypesFlag.Value.Set("rdonly,replica")
tabletTypesFlag.Changed = true
return err
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cmd := &cobra.Command{}
AddCommonCreateFlags(cmd)
test := func() error {
if tt.setFunc != nil {
if err := tt.setFunc(cmd); err != nil {
return err
}
}
if err := ParseAndValidateCreateOptions(cmd); err != nil {
return err
}
return nil
}
if err := test(); (err != nil) != tt.wantErr {
t.Errorf("ParseAndValidateCreateOptions() error = %v, wantErr %t", err, tt.wantErr)
}
})
}
}
3 changes: 3 additions & 0 deletions go/cmd/vtctldclient/command/vreplication/workflow/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ var (
updateOptions.Cells = textutil.SimulatedNullStringSlice
}
if cmd.Flags().Lookup("tablet-types").Changed {
if err := common.ParseTabletTypes(cmd); err != nil {
return err
}
changes = true
} else {
updateOptions.TabletTypes = []topodatapb.TabletType{topodatapb.TabletType(textutil.SimulatedNullInt)}
Expand Down
Loading