Move e2e test on version command to unit tests

* Refactor a little version command

Signed-off-by: Silvin Lubecki <silvin.lubecki@docker.com>
This commit is contained in:
Silvin Lubecki 2017-12-05 15:41:03 +01:00
parent 61713c42a4
commit ba5d0d8ff5
6 changed files with 70 additions and 78 deletions

View File

@ -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.

View File

@ -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 {

View File

@ -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

View File

@ -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), " ")
}

View File

@ -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())
}

View File

@ -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}
}