Skip to content

Commit

Permalink
cli validate cluster name in cmd and config
Browse files Browse the repository at this point in the history
  • Loading branch information
ddjjia committed Oct 10, 2023
1 parent 5a7934c commit e924589
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
10 changes: 8 additions & 2 deletions cmd/eksctl-anywhere/cmd/upgradecluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,14 @@ var upgradeClusterCmd = &cobra.Command{
Long: "This command is used to upgrade workload clusters",
PreRunE: bindFlagsToViper,
SilenceUsage: true,
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
if uc.forceClean {
logger.MarkFail(forceCleanupDeprecationMessageForUpgrade)
return errors.New("please remove the --force-cleanup flag")
}

if err := uc.upgradeCluster(cmd); err != nil {
if err := uc.upgradeCluster(cmd, args); err != nil {

Check warning on line 49 in cmd/eksctl-anywhere/cmd/upgradecluster.go

View check run for this annotation

Codecov / codecov/patch

cmd/eksctl-anywhere/cmd/upgradecluster.go#L49

Added line #L49 was not covered by tests
return fmt.Errorf("failed to upgrade cluster: %v", err)
}
return nil
Expand All @@ -65,7 +66,8 @@ func init() {
flags.MarkRequired(createClusterCmd.Flags(), flags.ClusterConfig.Name)
}

func (uc *upgradeClusterOptions) upgradeCluster(cmd *cobra.Command) error {
// nolint:gocyclo
func (uc *upgradeClusterOptions) upgradeCluster(cmd *cobra.Command, args []string) error {

Check warning on line 70 in cmd/eksctl-anywhere/cmd/upgradecluster.go

View check run for this annotation

Codecov / codecov/patch

cmd/eksctl-anywhere/cmd/upgradecluster.go#L70

Added line #L70 was not covered by tests
ctx := cmd.Context()

clusterConfigFileExist := validations.FileExists(uc.fileName)
Expand All @@ -91,6 +93,10 @@ func (uc *upgradeClusterOptions) upgradeCluster(cmd *cobra.Command) error {
if _, err := uc.commonValidations(ctx); err != nil {
return fmt.Errorf("common validations failed due to: %v", err)
}

if err := validations.ValidateClusterNameFromCommandAndConfig(args, clusterConfig.Name); err != nil {
return err
}

Check warning on line 99 in cmd/eksctl-anywhere/cmd/upgradecluster.go

View check run for this annotation

Codecov / codecov/patch

cmd/eksctl-anywhere/cmd/upgradecluster.go#L97-L99

Added lines #L97 - L99 were not covered by tests
clusterSpec, err := newClusterSpec(uc.clusterOptions)
if err != nil {
return err
Expand Down
15 changes: 15 additions & 0 deletions pkg/validations/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package validations

import (
"errors"
"fmt"
"os"

"github.com/aws/eks-anywhere/pkg/api/v1alpha1"
Expand Down Expand Up @@ -31,3 +32,17 @@ func FileExistsAndIsNotEmpty(filename string) bool {
info, err := os.Stat(filename)
return err == nil && info.Size() > 0
}

// ValidateClusterNameFromCommandAndConfig validates if cluster name provided in command matches with cluster name in config file.
func ValidateClusterNameFromCommandAndConfig(args []string, clusterNameConfig string) error {
if len(args) != 0 {
clusterNameCli, err := ValidateClusterNameArg(args)
if err != nil {
return fmt.Errorf("please provide a valid <cluster-name>")
}
if clusterNameCli != clusterNameConfig {
return fmt.Errorf("please make sure cluster name provided in command matches with cluster name in config file")
}
}
return nil
}
43 changes: 43 additions & 0 deletions pkg/validations/input_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,46 @@ func TestValidateClusterNameArg(t *testing.T) {
})
}
}

func TestValidateClusterNameFromCommandAndConfig(t *testing.T) {
tests := []struct {
name string
args []string
clusterNameConfig string
expectedError error
}{
{
name: "Success cluster name match",
args: []string{"test-cluster"},
clusterNameConfig: "test-cluster",
expectedError: nil,
},
{
name: "Success empty Arguments",
args: []string{},
clusterNameConfig: "test-cluster",
expectedError: nil,
},
{
name: "Failure invalid cluster name",
args: []string{"123test-Cluster"},
clusterNameConfig: "test-cluster",
expectedError: errors.New("please provide a valid <cluster-name>"),
},
{
name: "Failure cluster name not match",
args: []string{"test-cluster-1"},
clusterNameConfig: "test-cluster",
expectedError: errors.New("please make sure cluster name provided in command matches with cluster name in config file"),
},
}

for _, tc := range tests {
t.Run(tc.name, func(tt *testing.T) {
gotError := validations.ValidateClusterNameFromCommandAndConfig(tc.args, tc.clusterNameConfig)
if !reflect.DeepEqual(tc.expectedError, gotError) {
t.Errorf("\n%v got Error = %v, want Error %v", tc.name, gotError, tc.expectedError)
}
})
}
}

0 comments on commit e924589

Please sign in to comment.