8000 context: produce consistent output on `context create`. · docker/cli@ff44305 · GitHub
[go: up one dir, main page]

Skip to content

Commit ff44305

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 ff44305

File tree

3 files changed

+39
-11
lines changed

3 files changed

+39
-11
lines changed

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: 20 additions & 2 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"
@@ -131,6 +132,11 @@ func TestCreateInvalids(t *testing.T) {
131132
}
132133
}
133134

135+
func assertContextCreateLogging(t *testing.T, cli *test.FakeCli, n string) {
136+
assert.Equal(t, n+"\n", cli.OutBuffer().String())
137+
assert.Equal(t, fmt.Sprintf("Successfully created context %q\n", n), cli.ErrBuffer().String())
138+
}
139+
134140
func TestCreateOrchestratorSwarm(t *testing.T) {
135141
cli, cleanup := makeFakeCli(t)
136142
defer cleanup()
@@ -141,8 +147,7 @@ func TestCreateOrchestratorSwarm(t *testing.T) {
141147
Docker: map[string]string{},
142148
})
143149
assert.NilError(t, err)
144-
assert.Equal(t, "test\n", cli.OutBuffer().String())
145-
assert.Equal(t, "Successfully created context \"test\"\n", cli.ErrBuffer().String())
150+
assertContextCreateLogging(t, cli, "test")
146151
}
147152

148153
func TestCreateOrchestratorEmpty(t *testing.T) {
@@ -154,6 +159,7 @@ func TestCreateOrchestratorEmpty(t *testing.T) {
154159
Docker: map[string]string{},
155160
})
156161
assert.NilError(t, err)
162+
assertContextCreateLogging(t, cli, "test")
157163
}
158164

159165
func validateTestKubeEndpoint(t *testing.T, s store.Reader, name string) {
@@ -189,6 +195,7 @@ func TestCreateOrchestratorAllKubernetesEndpointFromCurrent(t *testing.T) {
189195
cli, cleanup := makeFakeCli(t)
190196
defer cleanup()
191197
createTestContextWithKube(t, cli)
198+
assertContextCreateLogging(t, cli, "test")
192199
validateTestKubeEndpoint(t, cli.ContextStore(), "test")
193200
}
194201

@@ -225,6 +232,7 @@ func TestCreateFromContext(t *testing.T) {
225232
defer cleanup()
226233
revert := env.Patch(t, "KUBECONFIG", "./testdata/test-kubeconfig")
227234
defer revert()
235+
cli.ResetOutputBuffers()
228236
assert.NilError(t, RunCreate(cli, &CreateOptions{
229237
Name: "original",
230238
Description: "original description",
@@ -236,6 +244,9 @@ func TestCreateFromContext(t *testing.T) {
236244
},
237245
DefaultStackOrchestrator: "swarm",
238246
}))
247+
assertContextCreateLogging(t, cli, "original")
248+
249+
cli.ResetOutputBuffers()
239250
assert.NilError(t, RunCreate(cli, &CreateOptions{
240251
Name: "dummy",
241252
Description: "dummy description",
@@ -247,11 +258,13 @@ func TestCreateFromContext(t *testing.T) {
247258
},
248259
DefaultStackOrchestrator: "swarm",
249260
}))
261+
assertContextCreateLogging(t, cli, "dummy")
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,7 @@ func TestCreateFromContext(t *testing.T) {
261274
Kubernetes: c.kubernetes,
262275
})
263276
assert.NilError(t, err)
277+
assertContextCreateLogging(t, cli, c.name)
264278
newContext, err := cli.ContextStore().GetMetadata(c.name)
265279
assert.NilError(t, err)
266280
newContextTyped, err := command.GetDockerContext(newContext)
@@ -308,6 +322,7 @@ func TestCreateFromCurrent(t *testing.T) {
308322
defer cleanup()
309323
revert := env.Patch(t, "KUBECONFIG", "./testdata/test-kubeconfig")
310324
defer revert()
325+
cli.ResetOutputBuffers()
311326
assert.NilError(t, RunCreate(cli, &CreateOptions{
312327
Name: "original",
313328
Description: "original description",
@@ -319,17 +334,20 @@ func TestCreateFromCurrent(t *testing.T) {
319334
},
320335
DefaultStackOrchestrator: "swarm",
321336
}))
337+
assertContextCreateLogging(t, cli, "original")
322338

323339
cli.SetCurrentContext("original")
324340

325341
for _, c := range cases {
326342
t.Run(c.name, func(t *testing.T) {
343+
cli.ResetOutputBuffers()
327344
err := RunCreate(cli, &CreateOptions{
328345
Name: c.name,
329346
Description: c.description,
330347
DefaultStackOrchestrator: c.orchestrator,
331348
})
332349
assert.NilError(t, err)
350+
assertContextCreateLogging(t, cli, c.name)
333351
newContext, err := cli.ContextStore().GetMetadata(c.name)
334352
assert.NilError(t, err)
335353
newContextTyped, err := command.GetDockerContext(newContext)

internal/test/cli.go

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

172+
// ResetOutputBuffers resets the .OutBuffer() and.ErrBuffer() back to empty
173+
func (c *FakeCli) ResetOutputBuffers() {
174+
c.outBuffer.Reset()
175+
c.err.Reset()
176+
}
177+
172178
// SetNotaryClient sets the internal getter for retrieving a NotaryClient
173179
func (c *FakeCli) SetNotaryClient(notaryClientFunc NotaryClientFuncType) {
174180
c.notaryClientFunc = notaryClientFunc

0 commit comments

Comments
 (0)
0