Skip to content

Commit

Permalink
Refactor fake cli configuration
Browse files Browse the repository at this point in the history
Signed-off-by: Mathieu Champlon <mathieu.champlon@docker.com>
  • Loading branch information
mat007 committed May 15, 2018
1 parent 08fdab0 commit 685e0dd
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 31 deletions.
2 changes: 1 addition & 1 deletion cli/command/container/create_test.go
Expand Up @@ -156,7 +156,7 @@ func TestNewCreateCommandWithContentTrustErrors(t *testing.T) {
) (container.ContainerCreateCreatedBody, error) {
return container.ContainerCreateCreatedBody{}, fmt.Errorf("shouldn't try to pull image")
},
}, test.EnableContentTrust)
}).EnableContentTrust()
cli.SetNotaryClient(tc.notaryFunc)
cmd := NewCreateCommand(cli)
cmd.SetOutput(ioutil.Discard)
Expand Down
2 changes: 1 addition & 1 deletion cli/command/container/run_test.go
Expand Up @@ -62,7 +62,7 @@ func TestRunCommandWithContentTrustErrors(t *testing.T) {
) (container.ContainerCreateCreatedBody, error) {
return container.ContainerCreateCreatedBody{}, fmt.Errorf("shouldn't try to pull image")
},
}, test.EnableContentTrust)
}).EnableContentTrust()
cli.SetNotaryClient(tc.notaryFunc)
cmd := NewRunCommand(cli)
cmd.SetArgs(tc.args)
Expand Down
2 changes: 1 addition & 1 deletion cli/command/image/pull_test.go
Expand Up @@ -110,7 +110,7 @@ func TestNewPullCommandWithContentTrustErrors(t *testing.T) {
imagePullFunc: func(ref string, options types.ImagePullOptions) (io.ReadCloser, error) {
return ioutil.NopCloser(strings.NewReader("")), fmt.Errorf("shouldn't try to pull image")
},
}, test.EnableContentTrust)
}).EnableContentTrust()
cli.SetNotaryClient(tc.notaryFunc)
cmd := NewPullCommand(cli)
cmd.SetOutput(ioutil.Discard)
Expand Down
8 changes: 4 additions & 4 deletions cli/command/stack/list_test.go
Expand Up @@ -48,7 +48,7 @@ func TestListErrors(t *testing.T) {
for _, tc := range testCases {
cmd := newListCommand(test.NewFakeCli(&fakeClient{
serviceListFunc: tc.serviceListFunc,
}, test.OrchestratorSwarm))
}).WithOrchestratorSwarm())
cmd.SetArgs(tc.args)
cmd.SetOutput(ioutil.Discard)
for key, value := range tc.flags {
Expand All @@ -69,7 +69,7 @@ func TestListWithFormat(t *testing.T) {
}),
)}, nil
},
}, test.OrchestratorSwarm)
}).WithOrchestratorSwarm()
cmd := newListCommand(cli)
cmd.Flags().Set("format", "{{ .Name }}")
assert.NilError(t, cmd.Execute())
Expand All @@ -86,7 +86,7 @@ func TestListWithoutFormat(t *testing.T) {
}),
)}, nil
},
}, test.OrchestratorSwarm)
}).WithOrchestratorSwarm()
cmd := newListCommand(cli)
assert.NilError(t, cmd.Execute())
golden.Assert(t, cli.OutBuffer().String(), "stack-list-without-format.golden")
Expand Down Expand Up @@ -139,7 +139,7 @@ func TestListOrder(t *testing.T) {
serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) {
return uc.swarmServices, nil
},
}, test.OrchestratorSwarm)
}).WithOrchestratorSwarm()
cmd := newListCommand(cli)
assert.NilError(t, cmd.Execute())
golden.Assert(t, cli.OutBuffer().String(), uc.golden)
Expand Down
2 changes: 1 addition & 1 deletion cli/command/system/version_test.go
Expand Up @@ -38,7 +38,7 @@ func fakeServerVersion(_ context.Context) (types.Version, error) {
}

func TestVersionWithOrchestrator(t *testing.T) {
cli := test.NewFakeCli(&fakeClient{serverVersion: fakeServerVersion}, test.OrchestratorSwarm)
cli := test.NewFakeCli(&fakeClient{serverVersion: fakeServerVersion}).WithOrchestratorSwarm()
cmd := NewVersionCommand(cli)
assert.NilError(t, cmd.Execute())
assert.Check(t, is.Contains(cleanTabs(cli.OutBuffer().String()), "Orchestrator: swarm"))
Expand Down
30 changes: 7 additions & 23 deletions internal/test/cli.go
Expand Up @@ -38,7 +38,7 @@ type FakeCli struct {
}

// NewFakeCli returns a fake for the command.Cli interface
func NewFakeCli(client client.APIClient, opts ...func(*FakeCli)) *FakeCli {
func NewFakeCli(client client.APIClient) *FakeCli {
outBuffer := new(bytes.Buffer)
errBuffer := new(bytes.Buffer)
c := &FakeCli{
Expand All @@ -51,9 +51,6 @@ func NewFakeCli(client client.APIClient, opts ...func(*FakeCli)) *FakeCli {
// Set cli.ConfigFile().Filename to a tempfile to support Save.
configfile: configfile.New(""),
}
for _, opt := range opts {
opt(c)
}
return c
}

Expand Down Expand Up @@ -110,11 +107,6 @@ func (c *FakeCli) ClientInfo() command.ClientInfo {
return c.DockerCli.ClientInfo()
}

// SetClientInfo sets the internal getter for retrieving a ClientInfo
func (c *FakeCli) SetClientInfo(clientInfoFunc clientInfoFuncType) {
c.clientInfoFunc = clientInfoFunc
}

// OutBuffer returns the stdout buffer
func (c *FakeCli) OutBuffer() *bytes.Buffer {
return c.outBuffer
Expand Down Expand Up @@ -164,21 +156,13 @@ func (c *FakeCli) ContentTrustEnabled() bool {
}

// EnableContentTrust on the fake cli
func EnableContentTrust(c *FakeCli) {
func (c *FakeCli) EnableContentTrust() *FakeCli {
c.contentTrust = true
return c
}

// OrchestratorSwarm sets a command.ClientInfo with Swarm orchestrator
func OrchestratorSwarm(c *FakeCli) {
c.SetClientInfo(func() command.ClientInfo { return command.ClientInfo{Orchestrator: "swarm"} })
}

// OrchestratorKubernetes sets a command.ClientInfo with Kubernetes orchestrator
func OrchestratorKubernetes(c *FakeCli) {
c.SetClientInfo(func() command.ClientInfo { return command.ClientInfo{Orchestrator: "kubernetes"} })
}

// OrchestratorAll sets a command.ClientInfo with all orchestrator
func OrchestratorAll(c *FakeCli) {
c.SetClientInfo(func() command.ClientInfo { return command.ClientInfo{Orchestrator: "all"} })
// WithOrchestratorSwarm sets a command.ClientInfo with Swarm orchestrator
func (c *FakeCli) WithOrchestratorSwarm() *FakeCli {
c.clientInfoFunc = func() command.ClientInfo { return command.ClientInfo{Orchestrator: "swarm"} }
return c
}

0 comments on commit 685e0dd

Please sign in to comment.