Fix stream for 'nothing found in stack' message

Signed-off-by: Daniel Nephin <dnephin@docker.com>
This commit is contained in:
Daniel Nephin 2017-07-05 13:32:54 -04:00
parent 30933b516b
commit fb6deb1077
6 changed files with 36 additions and 27 deletions

View File

@ -58,7 +58,7 @@ func runPS(dockerCli command.Cli, options psOptions) error {
}
if len(tasks) == 0 {
fmt.Fprintf(dockerCli.Out(), "Nothing found in stack: %s\n", namespace)
fmt.Fprintf(dockerCli.Err(), "Nothing found in stack: %s\n", namespace)
return nil
}

View File

@ -53,15 +53,20 @@ func TestStackPsErrors(t *testing.T) {
}
func TestStackPsEmptyStack(t *testing.T) {
buf := new(bytes.Buffer)
cmd := newPsCommand(test.NewFakeCli(&fakeClient{
out := new(bytes.Buffer)
stderr := new(bytes.Buffer)
fakeCli := test.NewFakeCli(&fakeClient{
taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) {
return []swarm.Task{}, nil
},
}, buf))
}, out)
fakeCli.SetErr(stderr)
cmd := newPsCommand(fakeCli)
cmd.SetArgs([]string{"foo"})
assert.NoError(t, cmd.Execute())
testutil.EqualNormalizedString(t, testutil.RemoveSpace, buf.String(), "Nothing found in stack: foo")
assert.Equal(t, "", out.String())
assert.Equal(t, "Nothing found in stack: foo\n", stderr.String())
}
func TestStackPsWithQuietOption(t *testing.T) {

View File

@ -72,7 +72,7 @@ func runRemove(dockerCli command.Cli, opts removeOptions) error {
}
if len(services)+len(networks)+len(secrets)+len(configs) == 0 {
fmt.Fprintf(dockerCli.Out(), "Nothing found in stack: %s\n", namespace)
fmt.Fprintf(dockerCli.Err(), "Nothing found in stack: %s\n", namespace)
continue
}

View File

@ -56,8 +56,9 @@ func TestRemoveStack(t *testing.T) {
assert.Equal(t, allConfigIDs, cli.removedConfigs)
}
func TestSkipEmptyStack(t *testing.T) {
buf := new(bytes.Buffer)
func TestRemoveStackSkipEmpty(t *testing.T) {
out := new(bytes.Buffer)
stderr := new(bytes.Buffer)
allServices := []string{objectName("bar", "service1"), objectName("bar", "service2")}
allServiceIDs := buildObjectIDs(allServices)
@ -70,21 +71,24 @@ func TestSkipEmptyStack(t *testing.T) {
allConfigs := []string{objectName("bar", "config1")}
allConfigIDs := buildObjectIDs(allConfigs)
cli := &fakeClient{
fakeClient := &fakeClient{
services: allServices,
networks: allNetworks,
secrets: allSecrets,
configs: allConfigs,
}
cmd := newRemoveCommand(test.NewFakeCli(cli, buf))
fakeCli := test.NewFakeCli(fakeClient, out)
fakeCli.SetErr(stderr)
cmd := newRemoveCommand(fakeCli)
cmd.SetArgs([]string{"foo", "bar"})
assert.NoError(t, cmd.Execute())
assert.Contains(t, buf.String(), "Nothing found in stack: foo")
assert.Equal(t, allServiceIDs, cli.removedServices)
assert.Equal(t, allNetworkIDs, cli.removedNetworks)
assert.Equal(t, allSecretIDs, cli.removedSecrets)
assert.Equal(t, allConfigIDs, cli.removedConfigs)
assert.Equal(t, "", out.String())
assert.Contains(t, stderr.String(), "Nothing found in stack: foo\n")
assert.Equal(t, allServiceIDs, fakeClient.removedServices)
assert.Equal(t, allNetworkIDs, fakeClient.removedNetworks)
assert.Equal(t, allSecretIDs, fakeClient.removedSecrets)
assert.Equal(t, allConfigIDs, fakeClient.removedConfigs)
}
func TestRemoveContinueAfterError(t *testing.T) {

View File

@ -51,11 +51,9 @@ func runServices(dockerCli command.Cli, options servicesOptions) error {
return err
}
out := dockerCli.Out()
// if no services in this stack, print message and exit 0
if len(services) == 0 {
fmt.Fprintf(out, "Nothing found in stack: %s\n", options.namespace)
fmt.Fprintf(dockerCli.Err(), "Nothing found in stack: %s\n", options.namespace)
return nil
}

View File

@ -83,17 +83,19 @@ func TestStackServicesErrors(t *testing.T) {
}
func TestStackServicesEmptyServiceList(t *testing.T) {
buf := new(bytes.Buffer)
cmd := newServicesCommand(
test.NewFakeCli(&fakeClient{
serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) {
return []swarm.Service{}, nil
},
}, buf),
)
out := new(bytes.Buffer)
stderr := new(bytes.Buffer)
fakeCli := test.NewFakeCli(&fakeClient{
serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) {
return []swarm.Service{}, nil
},
}, out)
fakeCli.SetErr(stderr)
cmd := newServicesCommand(fakeCli)
cmd.SetArgs([]string{"foo"})
assert.NoError(t, cmd.Execute())
testutil.EqualNormalizedString(t, testutil.RemoveSpace, buf.String(), "Nothing found in stack: foo")
assert.Equal(t, "", out.String())
assert.Equal(t, "Nothing found in stack: foo\n", stderr.String())
}
func TestStackServicesWithQuietOption(t *testing.T) {