From ba5d0d8ff5f0efa9b5284c451f268ecdfd802ade Mon Sep 17 00:00:00 2001 From: Silvin Lubecki Date: Tue, 5 Dec 2017 15:41:03 +0100 Subject: [PATCH] Move e2e test on version command to unit tests * Refactor a little version command Signed-off-by: Silvin Lubecki --- cli/command/cli.go | 1 + cli/command/system/client_test.go | 9 ++++- cli/command/system/version.go | 21 ++++-------- cli/command/system/version_test.go | 55 ++++++++++++++++++++++++++++++ e2e/orchestrator/main_test.go | 17 --------- e2e/orchestrator/version_test.go | 45 ------------------------ 6 files changed, 70 insertions(+), 78 deletions(-) create mode 100644 cli/command/system/version_test.go delete mode 100644 e2e/orchestrator/main_test.go delete mode 100644 e2e/orchestrator/version_test.go diff --git a/cli/command/cli.go b/cli/command/cli.go index d4d9fd8198..b34d9e7f3d 100644 --- a/cli/command/cli.go +++ b/cli/command/cli.go @@ -44,6 +44,7 @@ type Cli interface { ServerInfo() ServerInfo ClientInfo() ClientInfo NotaryClient(imgRefAndAuth trust.ImageRefAndAuth, actions []string) (notaryclient.Repository, error) + DefaultVersion() string } // DockerCli is an instance the docker command line client. diff --git a/cli/command/system/client_test.go b/cli/command/system/client_test.go index ed0e803ea8..2500ddfeac 100644 --- a/cli/command/system/client_test.go +++ b/cli/command/system/client_test.go @@ -1,13 +1,20 @@ package system import ( + "github.com/docker/docker/api/types" "github.com/docker/docker/client" + "golang.org/x/net/context" ) type fakeClient struct { client.Client - version string + version string + serverVersion func(ctx context.Context) (types.Version, error) +} + +func (cli *fakeClient) ServerVersion(ctx context.Context) (types.Version, error) { + return cli.serverVersion(ctx) } func (cli *fakeClient) ClientVersion() string { diff --git a/cli/command/system/version.go b/cli/command/system/version.go index 2e676a0c14..31f441e53f 100644 --- a/cli/command/system/version.go +++ b/cli/command/system/version.go @@ -24,7 +24,7 @@ Client:{{if ne .Platform.Name ""}} {{.Platform.Name}}{{end}} Built: {{.BuildTime}} OS/Arch: {{.Os}}/{{.Arch}} Experimental: {{.Experimental}} - Orchestrator: {{.Client.Orchestrator}} + Orchestrator: {{.Orchestrator}} {{- end}} {{- if .ServerOK}}{{with .Server}} @@ -82,7 +82,7 @@ func (v versionInfo) ServerOK() bool { } // NewVersionCommand creates a new cobra.Command for `docker version` -func NewVersionCommand(dockerCli *command.DockerCli) *cobra.Command { +func NewVersionCommand(dockerCli command.Cli) *cobra.Command { var opts versionOptions cmd := &cobra.Command{ @@ -109,9 +109,7 @@ func reformatDate(buildTime string) string { return buildTime } -func runVersion(dockerCli *command.DockerCli, opts *versionOptions) error { - ctx := context.Background() - +func runVersion(dockerCli command.Cli, opts *versionOptions) error { templateFormat := versionTemplate tmpl := templates.New("version") if opts.format != "" { @@ -129,28 +127,21 @@ func runVersion(dockerCli *command.DockerCli, opts *versionOptions) error { vd := versionInfo{ Client: clientVersion{ + Platform: struct{ Name string }{cli.PlatformName}, Version: cli.Version, APIVersion: dockerCli.Client().ClientVersion(), DefaultAPIVersion: dockerCli.DefaultVersion(), GoVersion: runtime.Version(), GitCommit: cli.GitCommit, - BuildTime: cli.BuildTime, + BuildTime: reformatDate(cli.BuildTime), Os: runtime.GOOS, Arch: runtime.GOARCH, -<<<<<<< HEAD Experimental: dockerCli.ClientInfo().HasExperimental, - Orchestrator: string(command.GetOrchestrator(dockerCli)), -======= Orchestrator: string(command.GetOrchestrator(dockerCli.ConfigFile().Orchestrator)), ->>>>>>> Refactor stack command }, } - vd.Client.Platform.Name = cli.PlatformName - // first we need to make BuildTime more human friendly - vd.Client.BuildTime = reformatDate(vd.Client.BuildTime) - - sv, err := dockerCli.Client().ServerVersion(ctx) + sv, err := dockerCli.Client().ServerVersion(context.Background()) if err == nil { vd.Server = &sv foundEngine := false diff --git a/cli/command/system/version_test.go b/cli/command/system/version_test.go new file mode 100644 index 0000000000..32b5452c96 --- /dev/null +++ b/cli/command/system/version_test.go @@ -0,0 +1,55 @@ +package system + +import ( + "fmt" + "strings" + "testing" + + "github.com/docker/cli/cli/config/configfile" + "github.com/docker/cli/internal/test" + "github.com/docker/docker/api" + "github.com/docker/docker/api/types" + "github.com/stretchr/testify/assert" + "golang.org/x/net/context" +) + +func TestVersionWithoutServer(t *testing.T) { + cli := test.NewFakeCli(&fakeClient{ + serverVersion: func(ctx context.Context) (types.Version, error) { + return types.Version{}, fmt.Errorf("no server") + }, + }) + cmd := NewVersionCommand(cli) + cmd.SetOutput(cli.Err()) + assert.Error(t, cmd.Execute()) + assert.Contains(t, cleanTabs(cli.OutBuffer().String()), "Client:") + assert.NotContains(t, cleanTabs(cli.OutBuffer().String()), "Server:") +} + +func fakeServerVersion(ctx context.Context) (types.Version, error) { + return types.Version{ + Version: "docker-dev", + APIVersion: api.DefaultVersion, + }, nil +} + +func TestVersionWithDefaultOrchestrator(t *testing.T) { + cli := test.NewFakeCli(&fakeClient{serverVersion: fakeServerVersion}) + cmd := NewVersionCommand(cli) + assert.NoError(t, cmd.Execute()) + assert.Contains(t, cleanTabs(cli.OutBuffer().String()), "Orchestrator: swarm") +} + +func TestVersionWithOverridenOrchestrator(t *testing.T) { + cli := test.NewFakeCli(&fakeClient{serverVersion: fakeServerVersion}) + config := configfile.New("configfile") + config.Orchestrator = "Kubernetes" + cli.SetConfigFile(config) + cmd := NewVersionCommand(cli) + assert.NoError(t, cmd.Execute()) + assert.Contains(t, cleanTabs(cli.OutBuffer().String()), "Orchestrator: kubernetes") +} + +func cleanTabs(line string) string { + return strings.Join(strings.Fields(line), " ") +} diff --git a/e2e/orchestrator/main_test.go b/e2e/orchestrator/main_test.go deleted file mode 100644 index 17890aa521..0000000000 --- a/e2e/orchestrator/main_test.go +++ /dev/null @@ -1,17 +0,0 @@ -package orchestrator - -import ( - "fmt" - "os" - "testing" - - "github.com/docker/cli/internal/test/environment" -) - -func TestMain(m *testing.M) { - if err := environment.Setup(); err != nil { - fmt.Println(err.Error()) - os.Exit(3) - } - os.Exit(m.Run()) -} diff --git a/e2e/orchestrator/version_test.go b/e2e/orchestrator/version_test.go deleted file mode 100644 index 1bd8b62393..0000000000 --- a/e2e/orchestrator/version_test.go +++ /dev/null @@ -1,45 +0,0 @@ -package orchestrator - -import ( - "fmt" - "os" - "testing" - - shlex "github.com/flynn-archive/go-shlex" - "github.com/gotestyourself/gotestyourself/fs" - "github.com/gotestyourself/gotestyourself/icmd" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" -) - -func TestVersionWithDefaultOrchestrator(t *testing.T) { - // Orchestrator by default - result := icmd.RunCmd(shell(t, "docker version")) - result.Assert(t, icmd.Success) - assert.Contains(t, result.Stdout(), "Orchestrator: swarm") -} - -func TestVersionWithOverridenEnvOrchestrator(t *testing.T) { - // Override orchestrator using environment variable - result := icmd.RunCmd(shell(t, "docker version"), func(cmd *icmd.Cmd) { - cmd.Env = append(cmd.Env, append(os.Environ(), "DOCKER_ORCHESTRATOR=kubernetes")...) - }) - result.Assert(t, icmd.Success) - assert.Contains(t, result.Stdout(), "Orchestrator: kubernetes") -} - -func TestVersionWithOverridenConfigOrchestrator(t *testing.T) { - // Override orchestrator using configuration file - configDir := fs.NewDir(t, "config", fs.WithFile("config.json", `{"orchestrator": "kubernetes"}`)) - defer configDir.Remove() - result := icmd.RunCmd(shell(t, fmt.Sprintf("docker --config %s version", configDir.Path()))) - result.Assert(t, icmd.Success) - assert.Contains(t, result.Stdout(), "Orchestrator: kubernetes") -} - -// TODO: move to gotestyourself -func shell(t *testing.T, format string, args ...interface{}) icmd.Cmd { - cmd, err := shlex.Split(fmt.Sprintf(format, args...)) - require.NoError(t, err) - return icmd.Cmd{Command: cmd} -}