Merge pull request #8665 from aj-2000/user/aj-2000/validate-from-schedule-flag
Validate `--from-schedule` flag in create backup commandpull/8342/merge
commit
d9721fddb5
|
@ -0,0 +1 @@
|
||||||
|
Fixes issue #8214, validate `--from-schedule` flag in create backup command to prevent empty or whitespace-only values.
|
|
@ -179,6 +179,11 @@ func (o *CreateOptions) Validate(c *cobra.Command, args []string, f client.Facto
|
||||||
return fmt.Errorf("either a 'selector' or an 'or-selector' can be specified, but not both")
|
return fmt.Errorf("either a 'selector' or an 'or-selector' can be specified, but not both")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Ensure if FromSchedule is set, it has a non-empty value
|
||||||
|
if err := o.validateFromScheduleFlag(c); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// Ensure that unless FromSchedule is set, args contains a backup name
|
// Ensure that unless FromSchedule is set, args contains a backup name
|
||||||
if o.FromSchedule == "" && len(args) != 1 {
|
if o.FromSchedule == "" && len(args) != 1 {
|
||||||
return fmt.Errorf("a backup name is required, unless you are creating based on a schedule")
|
return fmt.Errorf("a backup name is required, unless you are creating based on a schedule")
|
||||||
|
@ -215,6 +220,17 @@ func (o *CreateOptions) Validate(c *cobra.Command, args []string, f client.Facto
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (o *CreateOptions) validateFromScheduleFlag(c *cobra.Command) error {
|
||||||
|
trimmed := strings.TrimSpace(o.FromSchedule)
|
||||||
|
if c.Flags().Changed("from-schedule") && trimmed == "" {
|
||||||
|
return fmt.Errorf("flag must have a non-empty value: --from-schedule")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Assign the trimmed value back
|
||||||
|
o.FromSchedule = trimmed
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (o *CreateOptions) Complete(args []string, f client.Factory) error {
|
func (o *CreateOptions) Complete(args []string, f client.Factory) error {
|
||||||
// If an explicit name is specified, use that name
|
// If an explicit name is specified, use that name
|
||||||
if len(args) > 0 {
|
if len(args) > 0 {
|
||||||
|
|
|
@ -24,6 +24,7 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
flag "github.com/spf13/pflag"
|
flag "github.com/spf13/pflag"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/mock"
|
"github.com/stretchr/testify/mock"
|
||||||
|
@ -86,6 +87,42 @@ func TestCreateOptions_BuildBackup(t *testing.T) {
|
||||||
}, backup.Spec.OrderedResources)
|
}, backup.Spec.OrderedResources)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCreateOptions_ValidateFromScheduleFlag(t *testing.T) {
|
||||||
|
cmd := &cobra.Command{}
|
||||||
|
o := NewCreateOptions()
|
||||||
|
o.BindFromSchedule(cmd.Flags())
|
||||||
|
|
||||||
|
t.Run("from-schedule with empty or no value", func(t *testing.T) {
|
||||||
|
cmd.Flags().Set("from-schedule", "")
|
||||||
|
err := o.validateFromScheduleFlag(cmd)
|
||||||
|
require.True(t, cmd.Flags().Changed("from-schedule"))
|
||||||
|
require.Error(t, err)
|
||||||
|
require.Equal(t, "flag must have a non-empty value: --from-schedule", err.Error())
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("from-schedule with spaces only", func(t *testing.T) {
|
||||||
|
cmd.Flags().Set("from-schedule", " ")
|
||||||
|
err := o.validateFromScheduleFlag(cmd)
|
||||||
|
require.True(t, cmd.Flags().Changed("from-schedule"))
|
||||||
|
require.Error(t, err)
|
||||||
|
require.Equal(t, "flag must have a non-empty value: --from-schedule", err.Error())
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("from-schedule with valid value", func(t *testing.T) {
|
||||||
|
cmd.Flags().Set("from-schedule", "daily")
|
||||||
|
err := o.validateFromScheduleFlag(cmd)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, "daily", o.FromSchedule)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("from-schedule with leading and trailing spaces", func(t *testing.T) {
|
||||||
|
cmd.Flags().Set("from-schedule", " daily ")
|
||||||
|
err := o.validateFromScheduleFlag(cmd)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, "daily", o.FromSchedule)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func TestCreateOptions_BuildBackupFromSchedule(t *testing.T) {
|
func TestCreateOptions_BuildBackupFromSchedule(t *testing.T) {
|
||||||
o := NewCreateOptions()
|
o := NewCreateOptions()
|
||||||
o.FromSchedule = "test"
|
o.FromSchedule = "test"
|
||||||
|
|
Loading…
Reference in New Issue