mirror of https://github.com/docker/compose.git
Unit tests for cobra commands
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
parent
a0500799d0
commit
d957987471
|
@ -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
|
||||
}
|
|
@ -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\"")
|
||||
}
|
|
@ -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 {
|
||||
|
|
|
@ -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")
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
{"Name":"aws","Metadata":{"Type":"aws"},"Endpoints":{"aws":{"Profile":"profileName","Cluster":"clusterName","Region":"regionName"},"docker":{"Profile":"profileName","Cluster":"clusterName","Region":"regionName"}}}
|
|
@ -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
|
||||
},
|
||||
}
|
||||
}
|
|
@ -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))
|
||||
}
|
|
@ -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
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,7 +3,7 @@ package amazon
|
|||
import (
|
||||
"testing"
|
||||
|
||||
"gotest.tools/assert"
|
||||
"gotest.tools/v3/assert"
|
||||
)
|
||||
|
||||
func TestInvalidNetworkMode(t *testing.T) {
|
||||
|
|
|
@ -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"
|
||||
)
|
||||
|
||||
|
|
|
@ -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'",
|
||||
})
|
||||
}
|
|
@ -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"
|
||||
|
|
|
@ -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)))
|
||||
}
|
|
@ -1,16 +0,0 @@
|
|||
[
|
||||
{
|
||||
"Name": "aws",
|
||||
"Metadata": {},
|
||||
"Endpoints": {
|
||||
"aws": {
|
||||
"Cluster": "clusterName",
|
||||
"Profile": "profileName",
|
||||
"Region": "regionName"
|
||||
},
|
||||
"docker": {
|
||||
"SkipTLSVerify": false
|
||||
}
|
||||
},
|
||||
"TLSMaterial": {},
|
||||
"Storage":
|
Loading…
Reference in New Issue