Skip to content

Commit

Permalink
[release-18.0] VReplication: error on vtctldclient commands w/o table…
Browse files Browse the repository at this point in the history
…t types (#14294) (#14298)

Signed-off-by: Matt Lord <[email protected]>
Co-authored-by: vitess-bot[bot] <108069721+vitess-bot[bot]@users.noreply.github.com>
  • Loading branch information
vitess-bot[bot] authored Oct 17, 2023
1 parent 5cf1bd8 commit fe27a3f
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 4 deletions.
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

0 comments on commit fe27a3f

Please sign in to comment.