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

Exit with non-zero codes when user input is invalid #2090

Merged
merged 2 commits into from
Mar 2, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
58 changes: 58 additions & 0 deletions cmd/kind/app/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package app

import (
"testing"

"sigs.k8s.io/kind/pkg/cmd"
)

func TestCheckQuiet(t *testing.T) {
Expand Down Expand Up @@ -78,3 +80,59 @@ func TestCheckQuiet(t *testing.T) {
})
}
}

func Test_CommandErrReturn(t *testing.T) {
t.Parallel()
cases := []struct {
Name string
Command string
Subcommand string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we add a expectErr field and do some positive testing? i..e commands that we know can't fail

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

only if we turn this into an integration test, which can be in a follow-up (let's not be running real commands in unit tests)

}{
{
Name: "misspelled subcommand for build",
Command: "build",
Subcommand: "nod-image",
},
{
Name: "misspelled subcommand for completion",
Command: "completion",
Subcommand: "zzsh",
},
{
Name: "misspelled subcommand for create",
Command: "create",
Subcommand: "clunster",
},
{
Name: "misspelled subcommand for delete",
Command: "delete",
Subcommand: "clust",
},
{
Name: "misspelled subcommand for export",
Command: "export",
Subcommand: "kubecfg",
},
{
Name: "misspelled subcommand for get",
Command: "get",
Subcommand: "nods",
},
{
Name: "misspelled subcommand for load",
Command: "load",
Subcommand: "dokker-image",
},
}

for _, tc := range cases {
tc := tc
t.Run(tc.Name, func(t *testing.T) {
t.Parallel()
err := Run(cmd.NewLogger(), cmd.StandardIOStreams(), []string{tc.Command, tc.Subcommand})
if err == nil {
t.Errorf("Subcommand should raise an error if not called with correct params")
}
})
}
}
9 changes: 9 additions & 0 deletions pkg/cmd/kind/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ limitations under the License.
package build

import (
"errors"

"github.com/spf13/cobra"

"sigs.k8s.io/kind/pkg/cmd"
Expand All @@ -33,6 +35,13 @@ func NewCommand(logger log.Logger, streams cmd.IOStreams) *cobra.Command {
Use: "build",
Short: "Build one of [node-image]",
Long: "Build one of [node-image]",
RunE: func(cmd *cobra.Command, args []string) error {
err := cmd.Help()
if err != nil {
return err
}
return errors.New("Subcommand is required")
},
}
// add subcommands
cmd.AddCommand(nodeimage.NewCommand(logger, streams))
Expand Down
9 changes: 9 additions & 0 deletions pkg/cmd/kind/completion/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ limitations under the License.
package completion

import (
"errors"

"github.com/spf13/cobra"

"sigs.k8s.io/kind/pkg/cmd"
Expand All @@ -34,6 +36,13 @@ func NewCommand(logger log.Logger, streams cmd.IOStreams) *cobra.Command {
Use: "completion",
Short: "Output shell completion code for the specified shell (bash, zsh or fish)",
Long: longDescription,
RunE: func(cmd *cobra.Command, args []string) error {
err := cmd.Help()
if err != nil {
return err
}
return errors.New("Subcommand is required")
},
}
cmd.AddCommand(zsh.NewCommand(logger, streams))
cmd.AddCommand(bash.NewCommand(logger, streams))
Expand Down
9 changes: 9 additions & 0 deletions pkg/cmd/kind/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ limitations under the License.
package create

import (
"errors"

"github.com/spf13/cobra"

"sigs.k8s.io/kind/pkg/cmd"
Expand All @@ -32,6 +34,13 @@ func NewCommand(logger log.Logger, streams cmd.IOStreams) *cobra.Command {
Use: "create",
Short: "Creates one of [cluster]",
Long: "Creates one of local Kubernetes cluster (cluster)",
RunE: func(cmd *cobra.Command, args []string) error {
err := cmd.Help()
if err != nil {
return err
}
return errors.New("Subcommand is required")
},
}
cmd.AddCommand(createcluster.NewCommand(logger, streams))
return cmd
Expand Down
9 changes: 9 additions & 0 deletions pkg/cmd/kind/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ limitations under the License.
package delete

import (
"errors"

"github.com/spf13/cobra"

"sigs.k8s.io/kind/pkg/cmd"
Expand All @@ -34,6 +36,13 @@ func NewCommand(logger log.Logger, streams cmd.IOStreams) *cobra.Command {
Use: "delete",
Short: "Deletes one of [cluster]",
Long: "Deletes one of [cluster]",
RunE: func(cmd *cobra.Command, args []string) error {
err := cmd.Help()
if err != nil {
return err
}
return errors.New("Subcommand is required")
},
}
cmd.AddCommand(deletecluster.NewCommand(logger, streams))
cmd.AddCommand(deleteclusters.NewCommand(logger, streams))
Expand Down
9 changes: 9 additions & 0 deletions pkg/cmd/kind/export/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ limitations under the License.
package export

import (
"errors"

"github.com/spf13/cobra"

"sigs.k8s.io/kind/pkg/cmd"
Expand All @@ -34,6 +36,13 @@ func NewCommand(logger log.Logger, streams cmd.IOStreams) *cobra.Command {
Use: "export",
Short: "Exports one of [kubeconfig, logs]",
Long: "Exports one of [kubeconfig, logs]",
RunE: func(cmd *cobra.Command, args []string) error {
err := cmd.Help()
if err != nil {
return err
}
return errors.New("Subcommand is required")
},
}
// add subcommands
cmd.AddCommand(logs.NewCommand(logger, streams))
Expand Down
9 changes: 9 additions & 0 deletions pkg/cmd/kind/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ limitations under the License.
package get

import (
"errors"

"github.com/spf13/cobra"

"sigs.k8s.io/kind/pkg/cmd"
Expand All @@ -35,6 +37,13 @@ func NewCommand(logger log.Logger, streams cmd.IOStreams) *cobra.Command {
Use: "get",
Short: "Gets one of [clusters, nodes, kubeconfig]",
Long: "Gets one of [clusters, nodes, kubeconfig]",
RunE: func(cmd *cobra.Command, args []string) error {
err := cmd.Help()
if err != nil {
return err
}
return errors.New("Subcommand is required")
},
}
// add subcommands
cmd.AddCommand(clusters.NewCommand(logger, streams))
Expand Down
9 changes: 9 additions & 0 deletions pkg/cmd/kind/load/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ limitations under the License.
package load

import (
"errors"

"github.com/spf13/cobra"

"sigs.k8s.io/kind/pkg/cmd"
Expand All @@ -33,6 +35,13 @@ func NewCommand(logger log.Logger, streams cmd.IOStreams) *cobra.Command {
Use: "load",
Short: "Loads images into nodes",
Long: "Loads images into node from an archive or image on host",
RunE: func(cmd *cobra.Command, args []string) error {
err := cmd.Help()
if err != nil {
return err
}
return errors.New("Subcommand is required")
},
}
// add subcommands
cmd.AddCommand(dockerimage.NewCommand(logger, streams))
Expand Down