Skip to content

Commit 24b6c8a

Browse files
author
Ian Campbell
committed
context: produce consistent output on context create.
Refactor `RunCreate` slightly so that all three paths always produce the same output, namely the name of the new context of `stdout` (for scripting) and the success log message on `stderr`. Validate by extending the existing unit tests to always check the output is as expected. Signed-off-by: Ian Campbell <ijc@docker.com>
1 parent 53fc257 commit 24b6c8a

3 files changed

Lines changed: 39 additions & 9 deletions

File tree

cli/command/context/create.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,19 @@ func RunCreate(cli command.Cli, o *CreateOptions) error {
7878
if err != nil {
7979
return errors.Wrap(err, "unable to parse default-stack-orchestrator")
8080
}
81-
if o.From == "" && o.Docker == nil && o.Kubernetes == nil {
82-
return createFromExistingContext(s, cli.CurrentContext(), stackOrchestrator, o)
83-
}
84-
if o.From != "" {
85-
return createFromExistingContext(s, o.From, stackOrchestrator, o)
86-
}
87-
return createNewContext(o, stackOrchestrator, cli, s)
81+
switch {
82+
case o.From == "" && o.Docker == nil && o.Kubernetes == nil:
83+
err = createFromExistingContext(s, cli.CurrentContext(), stackOrchestrator, o)
84+
case o.From != "":
85+
err = createFromExistingContext(s, o.From, stackOrchestrator, o)
86+
default:
87+
err = createNewContext(o, stackOrchestrator, cli, s)
88+
}
89+
if err == nil {
90+
fmt.Fprintln(cli.Out(), o.Name)
91+
fmt.Fprintf(cli.Err(), "Successfully created context %q\n", o.Name)
92+
}
93+
return err
8894
}
8995

9096
func createNewContext(o *CreateOptions, stackOrchestrator command.Orchestrator, cli command.Cli, s store.Writer) error {
@@ -127,8 +133,6 @@ func createNewContext(o *CreateOptions, stackOrchestrator command.Orchestrator,
127133
if err := s.ResetTLSMaterial(o.Name, &contextTLSData); err != nil {
128134
return err
129135
}
130-
fmt.Fprintln(cli.Out(), o.Name)
131-
fmt.Fprintf(cli.Err(), "Successfully created context %q\n", o.Name)
132136
return nil
133137
}
134138

cli/command/context/create_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package context
22

33
import (
4+
"fmt"
45
"io/ioutil"
56
"os"
67
"testing"
@@ -154,6 +155,8 @@ func TestCreateOrchestratorEmpty(t *testing.T) {
154155
Docker: map[string]string{},
155156
})
156157
assert.NilError(t, err)
158+
assert.Equal(t, "test\n", cli.OutBuffer().String())
159+
assert.Equal(t, "Successfully created context \"test\"\n", cli.ErrBuffer().String())
157160
}
158161

159162
func validateTestKubeEndpoint(t *testing.T, s store.Reader, name string) {
@@ -189,6 +192,8 @@ func TestCreateOrchestratorAllKubernetesEndpointFromCurrent(t *testing.T) {
189192
cli, cleanup := makeFakeCli(t)
190193
defer cleanup()
191194
createTestContextWithKube(t, cli)
195+
assert.Equal(t, "test\n", cli.OutBuffer().String())
196+
assert.Equal(t, "Successfully created context \"test\"\n", cli.ErrBuffer().String())
192197
validateTestKubeEndpoint(t, cli.ContextStore(), "test")
193198
}
194199

@@ -225,6 +230,7 @@ func TestCreateFromContext(t *testing.T) {
225230
defer cleanup()
226231
revert := env.Patch(t, "KUBECONFIG", "./testdata/test-kubeconfig")
227232
defer revert()
233+
cli.ResetOutputBuffers()
228234
assert.NilError(t, RunCreate(cli, &CreateOptions{
229235
Name: "original",
230236
Description: "original description",
@@ -236,6 +242,10 @@ func TestCreateFromContext(t *testing.T) {
236242
},
237243
DefaultStackOrchestrator: "swarm",
238244
}))
245+
assert.Equal(t, "original\n", cli.OutBuffer().String())
246+
assert.Equal(t, "Successfully created context \"original\"\n", cli.ErrBuffer().String())
247+
248+
cli.ResetOutputBuffers()
239249
assert.NilError(t, RunCreate(cli, &CreateOptions{
240250
Name: "dummy",
241251
Description: "dummy description",
@@ -247,11 +257,14 @@ func TestCreateFromContext(t *testing.T) {
247257
},
248258
DefaultStackOrchestrator: "swarm",
249259
}))
260+
assert.Equal(t, "dummy\n", cli.OutBuffer().String())
261+
assert.Equal(t, "Successfully created context \"dummy\"\n", cli.ErrBuffer().String())
250262

251263
cli.SetCurrentContext("dummy")
252264

253265
for _, c := range cases {
254266
t.Run(c.name, func(t *testing.T) {
267+
cli.ResetOutputBuffers()
255268
err := RunCreate(cli, &CreateOptions{
256269
From: "original",
257270
Name: c.name,
@@ -261,6 +274,8 @@ func TestCreateFromContext(t *testing.T) {
261274
Kubernetes: c.kubernetes,
262275
})
263276
assert.NilError(t, err)
277+
assert.Equal(t, c.name+"\n", cli.OutBuffer().String())
278+
assert.Equal(t, fmt.Sprintf("Successfully created context %q\n", c.name), cli.ErrBuffer().String())
264279
newContext, err := cli.ContextStore().GetMetadata(c.name)
265280
assert.NilError(t, err)
266281
newContextTyped, err := command.GetDockerContext(newContext)
@@ -308,6 +323,7 @@ func TestCreateFromCurrent(t *testing.T) {
308323
defer cleanup()
309324
revert := env.Patch(t, "KUBECONFIG", "./testdata/test-kubeconfig")
310325
defer revert()
326+
cli.ResetOutputBuffers()
311327
assert.NilError(t, RunCreate(cli, &CreateOptions{
312328
Name: "original",
313329
Description: "original description",
@@ -319,17 +335,22 @@ func TestCreateFromCurrent(t *testing.T) {
319335
},
320336
DefaultStackOrchestrator: "swarm",
321337
}))
338+
assert.Equal(t, "original\n", cli.OutBuffer().String())
339+
assert.Equal(t, "Successfully created context \"original\"\n", cli.ErrBuffer().String())
322340

323341
cli.SetCurrentContext("original")
324342

325343
for _, c := range cases {
326344
t.Run(c.name, func(t *testing.T) {
345+
cli.ResetOutputBuffers()
327346
err := RunCreate(cli, &CreateOptions{
328347
Name: c.name,
329348
Description: c.description,
330349
DefaultStackOrchestrator: c.orchestrator,
331350
})
332351
assert.NilError(t, err)
352+
assert.Equal(t, c.name+"\n", cli.OutBuffer().String())
353+
assert.Equal(t, fmt.Sprintf("Successfully created context %q\n", c.name), cli.ErrBuffer().String())
333354
newContext, err := cli.ContextStore().GetMetadata(c.name)
334355
assert.NilError(t, err)
335356
newContextTyped, err := command.GetDockerContext(newContext)

internal/test/cli.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,11 @@ func (c *FakeCli) ErrBuffer() *bytes.Buffer {
169169
return c.err
170170
}
171171

172+
func (c *FakeCli) ResetOutputBuffers() {
173+
c.outBuffer.Reset()
174+
c.err.Reset()
175+
}
176+
172177
// SetNotaryClient sets the internal getter for retrieving a NotaryClient
173178
func (c *FakeCli) SetNotaryClient(notaryClientFunc NotaryClientFuncType) {
174179
c.notaryClientFunc = notaryClientFunc

0 commit comments

Comments
 (0)