From d95798747153d76ff0cc1d4436948d515a43ebca Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Wed, 10 Jun 2020 10:42:48 +0200 Subject: [PATCH] Unit tests for cobra commands Signed-off-by: Nicolas De Loof --- ecs/cmd/commands/root.go | 32 +++++++++++++++++ ecs/cmd/commands/root_test.go | 13 +++++++ ecs/cmd/commands/setup.go | 6 ---- ecs/cmd/commands/setup_test.go | 32 +++++++++++++++++ ecs/cmd/commands/testdata/context.golden | 1 + ecs/cmd/commands/version.go | 20 +++++++++++ ecs/cmd/commands/version_test.go | 18 ++++++++++ ecs/cmd/main/main.go | 44 ++--------------------- ecs/pkg/amazon/check_test.go | 2 +- ecs/pkg/amazon/cloudformation_test.go | 2 +- ecs/tests/command_test.go | 18 ---------- ecs/tests/e2e_deploy_services_test.go | 2 +- ecs/tests/setup_command_test.go | 24 ------------- ecs/tests/testdata/context-inspect.golden | 16 --------- 14 files changed, 122 insertions(+), 108 deletions(-) create mode 100644 ecs/cmd/commands/root.go create mode 100644 ecs/cmd/commands/root_test.go create mode 100644 ecs/cmd/commands/setup_test.go create mode 100644 ecs/cmd/commands/testdata/context.golden create mode 100644 ecs/cmd/commands/version.go create mode 100644 ecs/cmd/commands/version_test.go delete mode 100644 ecs/tests/command_test.go delete mode 100644 ecs/tests/setup_command_test.go delete mode 100644 ecs/tests/testdata/context-inspect.golden diff --git a/ecs/cmd/commands/root.go b/ecs/cmd/commands/root.go new file mode 100644 index 000000000..a9c229218 --- /dev/null +++ b/ecs/cmd/commands/root.go @@ -0,0 +1,32 @@ +package commands + +import ( + "fmt" + + "github.com/docker/cli/cli/command" + "github.com/spf13/cobra" +) + +// NewRootCmd returns the base root command. +func NewRootCmd(dockerCli command.Cli) *cobra.Command { + cmd := &cobra.Command{ + Short: "Docker ECS", + Long: `run multi-container applications on Amazon ECS.`, + Use: "ecs", + Annotations: map[string]string{"experimentalCLI": "true"}, + RunE: func(cmd *cobra.Command, args []string) error { + if len(args) != 0 { + return fmt.Errorf("%q is not a docker ecs command\nSee 'docker ecs --help'", args[0]) + } + cmd.Help() + return nil + }, + } + cmd.AddCommand( + VersionCommand(), + ComposeCommand(dockerCli), + SecretCommand(dockerCli), + SetupCommand(), + ) + return cmd +} diff --git a/ecs/cmd/commands/root_test.go b/ecs/cmd/commands/root_test.go new file mode 100644 index 000000000..80f2a72d5 --- /dev/null +++ b/ecs/cmd/commands/root_test.go @@ -0,0 +1,13 @@ +package commands + +import ( + "testing" + + "gotest.tools/v3/assert" +) + +func TestUnknownCommand(t *testing.T) { + root := NewRootCmd(nil) + _, _, err := root.Find([]string{"unknown_command"}) + assert.Error(t, err, "unknown command \"unknown_command\" for \"ecs\"") +} diff --git a/ecs/cmd/commands/setup.go b/ecs/cmd/commands/setup.go index dd6c40f61..6d52664f9 100644 --- a/ecs/cmd/commands/setup.go +++ b/ecs/cmd/commands/setup.go @@ -9,7 +9,6 @@ import ( "github.com/aws/aws-sdk-go/aws/awserr" "github.com/aws/aws-sdk-go/aws/credentials" "github.com/aws/aws-sdk-go/aws/defaults" - "github.com/docker/cli/cli-plugins/plugin" contextStore "github.com/docker/ecs-plugin/pkg/docker" "github.com/manifoldco/promptui" "github.com/spf13/cobra" @@ -42,11 +41,6 @@ func SetupCommand() *cobra.Command { cmd := &cobra.Command{ Use: "setup", Short: "", - PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - //Override the root command PersistentPreRun - //We just need to initialize the top parent command - return plugin.PersistentPreRunE(cmd, args) - }, RunE: func(cmd *cobra.Command, args []string) error { if requiredFlag := opts.unsetRequiredArgs(); len(requiredFlag) > 0 { if err := interactiveCli(&opts); err != nil { diff --git a/ecs/cmd/commands/setup_test.go b/ecs/cmd/commands/setup_test.go new file mode 100644 index 000000000..97b7add38 --- /dev/null +++ b/ecs/cmd/commands/setup_test.go @@ -0,0 +1,32 @@ +package commands + +import ( + "io/ioutil" + "path/filepath" + "testing" + + "github.com/docker/cli/cli/config" + "gotest.tools/v3/assert" + "gotest.tools/v3/fs" + "gotest.tools/v3/golden" +) + +func TestDefaultAwsContextName(t *testing.T) { + dir := fs.NewDir(t, "setup") + defer dir.Remove() + cmd := NewRootCmd(nil) + dockerConfig := config.Dir() + config.SetDir(dir.Path()) + defer config.SetDir(dockerConfig) + + cmd.SetArgs([]string{"setup", "--cluster", "clusterName", "--profile", "profileName", "--region", "regionName"}) + err := cmd.Execute() + assert.NilError(t, err) + + files, err := filepath.Glob(dir.Join("contexts", "meta", "*", "meta.json")) + assert.NilError(t, err) + assert.Equal(t, len(files), 1) + b, err := ioutil.ReadFile(files[0]) + assert.NilError(t, err) + golden.Assert(t, string(b), "context.golden") +} diff --git a/ecs/cmd/commands/testdata/context.golden b/ecs/cmd/commands/testdata/context.golden new file mode 100644 index 000000000..891cb9cf8 --- /dev/null +++ b/ecs/cmd/commands/testdata/context.golden @@ -0,0 +1 @@ +{"Name":"aws","Metadata":{"Type":"aws"},"Endpoints":{"aws":{"Profile":"profileName","Cluster":"clusterName","Region":"regionName"},"docker":{"Profile":"profileName","Cluster":"clusterName","Region":"regionName"}}} \ No newline at end of file diff --git a/ecs/cmd/commands/version.go b/ecs/cmd/commands/version.go new file mode 100644 index 000000000..d3b44687c --- /dev/null +++ b/ecs/cmd/commands/version.go @@ -0,0 +1,20 @@ +package commands + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +const Version = "0.0.1" + +func VersionCommand() *cobra.Command { + return &cobra.Command{ + Use: "version", + Short: "Show version.", + RunE: func(cmd *cobra.Command, args []string) error { + fmt.Fprintf(cmd.OutOrStdout(), "Docker ECS plugin %s\n", Version) + return nil + }, + } +} diff --git a/ecs/cmd/commands/version_test.go b/ecs/cmd/commands/version_test.go new file mode 100644 index 000000000..4c0ed7e69 --- /dev/null +++ b/ecs/cmd/commands/version_test.go @@ -0,0 +1,18 @@ +package commands + +import ( + "bytes" + "strings" + "testing" + + "gotest.tools/v3/assert" +) + +func TestVersion(t *testing.T) { + root := NewRootCmd(nil) + var out bytes.Buffer + root.SetOut(&out) + root.SetArgs([]string{"version"}) + root.Execute() + assert.Check(t, strings.Contains(out.String(), Version)) +} diff --git a/ecs/cmd/main/main.go b/ecs/cmd/main/main.go index 9af648292..5dd1f4dd1 100644 --- a/ecs/cmd/main/main.go +++ b/ecs/cmd/main/main.go @@ -1,60 +1,22 @@ package main import ( - "fmt" + "github.com/docker/ecs-plugin/cmd/commands" "github.com/docker/cli/cli-plugins/manager" "github.com/docker/cli/cli-plugins/plugin" "github.com/docker/cli/cli/command" - commands "github.com/docker/ecs-plugin/cmd/commands" "github.com/spf13/cobra" ) -const version = "0.0.1" - func main() { plugin.Run(func(dockerCli command.Cli) *cobra.Command { - cmd := NewRootCmd("ecs", dockerCli) + cmd := commands.NewRootCmd(dockerCli) return cmd }, manager.Metadata{ SchemaVersion: "0.1.0", Vendor: "Docker Inc.", - Version: version, + Version: commands.Version, Experimental: true, }) } - -// NewRootCmd returns the base root command. -func NewRootCmd(name string, dockerCli command.Cli) *cobra.Command { - cmd := &cobra.Command{ - Short: "Docker ECS", - Long: `run multi-container applications on Amazon ECS.`, - Use: name, - Annotations: map[string]string{"experimentalCLI": "true"}, - RunE: func(cmd *cobra.Command, args []string) error { - if len(args) != 0 { - return fmt.Errorf("%q is not a docker ecs command\nSee 'docker ecs --help'", args[0]) - } - cmd.Help() - return nil - }, - } - cmd.AddCommand( - VersionCommand(), - commands.ComposeCommand(dockerCli), - commands.SecretCommand(dockerCli), - commands.SetupCommand(), - ) - return cmd -} - -func VersionCommand() *cobra.Command { - return &cobra.Command{ - Use: "version", - Short: "Show version.", - RunE: func(cmd *cobra.Command, args []string) error { - fmt.Printf("Docker ECS plugin %s\n", version) - return nil - }, - } -} diff --git a/ecs/pkg/amazon/check_test.go b/ecs/pkg/amazon/check_test.go index 3f1859548..3038eee86 100644 --- a/ecs/pkg/amazon/check_test.go +++ b/ecs/pkg/amazon/check_test.go @@ -3,7 +3,7 @@ package amazon import ( "testing" - "gotest.tools/assert" + "gotest.tools/v3/assert" ) func TestInvalidNetworkMode(t *testing.T) { diff --git a/ecs/pkg/amazon/cloudformation_test.go b/ecs/pkg/amazon/cloudformation_test.go index 84e12e761..4b525d8fc 100644 --- a/ecs/pkg/amazon/cloudformation_test.go +++ b/ecs/pkg/amazon/cloudformation_test.go @@ -13,7 +13,7 @@ import ( "github.com/compose-spec/compose-go/loader" "github.com/compose-spec/compose-go/types" "github.com/docker/ecs-plugin/pkg/compose" - "gotest.tools/assert" + "gotest.tools/v3/assert" "gotest.tools/v3/golden" ) diff --git a/ecs/tests/command_test.go b/ecs/tests/command_test.go deleted file mode 100644 index 3ca7433d4..000000000 --- a/ecs/tests/command_test.go +++ /dev/null @@ -1,18 +0,0 @@ -package tests - -import ( - "testing" - - "gotest.tools/v3/icmd" -) - -func TestExitErrorCode(t *testing.T) { - cmd, cleanup, _ := dockerCli.createTestCmd() - defer cleanup() - - cmd.Command = dockerCli.Command("ecs", "unknown_command") - icmd.RunCmd(cmd).Assert(t, icmd.Expected{ - ExitCode: 1, - Err: "\"unknown_command\" is not a docker ecs command\nSee 'docker ecs --help'", - }) -} diff --git a/ecs/tests/e2e_deploy_services_test.go b/ecs/tests/e2e_deploy_services_test.go index c7002a19f..c8a2242ce 100644 --- a/ecs/tests/e2e_deploy_services_test.go +++ b/ecs/tests/e2e_deploy_services_test.go @@ -10,7 +10,7 @@ import ( "github.com/aws/aws-sdk-go/aws/session" "github.com/docker/ecs-plugin/pkg/amazon" "github.com/docker/ecs-plugin/pkg/docker" - "gotest.tools/assert" + "gotest.tools/v3/assert" "gotest.tools/v3/fs" "gotest.tools/v3/golden" "gotest.tools/v3/icmd" diff --git a/ecs/tests/setup_command_test.go b/ecs/tests/setup_command_test.go deleted file mode 100644 index d36393f48..000000000 --- a/ecs/tests/setup_command_test.go +++ /dev/null @@ -1,24 +0,0 @@ -package tests - -import ( - "strings" - "testing" - - "gotest.tools/assert" - "gotest.tools/v3/golden" - "gotest.tools/v3/icmd" -) - -func TestDefaultAwsContextName(t *testing.T) { - cmd, cleanup, _ := dockerCli.createTestCmd() - defer cleanup() - - cmd.Command = dockerCli.Command("ecs", "setup", "--cluster", "clusterName", "--profile", "profileName", - "--region", "regionName") - icmd.RunCmd(cmd).Assert(t, icmd.Success) - - cmd.Command = dockerCli.Command("context", "inspect", "aws") - output := icmd.RunCmd(cmd).Assert(t, icmd.Success).Combined() - expected := golden.Get(t, "context-inspect.golden") - assert.Assert(t, strings.HasPrefix(output, string(expected))) -} diff --git a/ecs/tests/testdata/context-inspect.golden b/ecs/tests/testdata/context-inspect.golden deleted file mode 100644 index ff61b55fc..000000000 --- a/ecs/tests/testdata/context-inspect.golden +++ /dev/null @@ -1,16 +0,0 @@ -[ - { - "Name": "aws", - "Metadata": {}, - "Endpoints": { - "aws": { - "Cluster": "clusterName", - "Profile": "profileName", - "Region": "regionName" - }, - "docker": { - "SkipTLSVerify": false - } - }, - "TLSMaterial": {}, - "Storage": \ No newline at end of file