Update service and secret command tests to new golden

Signed-off-by: Daniel Nephin <dnephin@docker.com>
This commit is contained in:
Daniel Nephin 2017-08-16 12:53:41 -04:00
parent 0e2bf7420a
commit 4c62d7288f
11 changed files with 67 additions and 110 deletions

View File

@ -1,7 +1,6 @@
package secret package secret
import ( import (
"bytes"
"io/ioutil" "io/ioutil"
"path/filepath" "path/filepath"
"reflect" "reflect"
@ -12,7 +11,7 @@ import (
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/swarm" "github.com/docker/docker/api/types/swarm"
"github.com/docker/docker/pkg/testutil" "github.com/docker/docker/pkg/testutil"
"github.com/docker/docker/pkg/testutil/golden" "github.com/gotestyourself/gotestyourself/golden"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -54,9 +53,8 @@ func TestSecretCreateErrors(t *testing.T) {
func TestSecretCreateWithName(t *testing.T) { func TestSecretCreateWithName(t *testing.T) {
name := "foo" name := "foo"
buf := new(bytes.Buffer)
var actual []byte var actual []byte
cli := test.NewFakeCliWithOutput(&fakeClient{ cli := test.NewFakeCli(&fakeClient{
secretCreateFunc: func(spec swarm.SecretSpec) (types.SecretCreateResponse, error) { secretCreateFunc: func(spec swarm.SecretSpec) (types.SecretCreateResponse, error) {
if spec.Name != name { if spec.Name != name {
return types.SecretCreateResponse{}, errors.Errorf("expected name %q, got %q", name, spec.Name) return types.SecretCreateResponse{}, errors.Errorf("expected name %q, got %q", name, spec.Name)
@ -68,14 +66,13 @@ func TestSecretCreateWithName(t *testing.T) {
ID: "ID-" + spec.Name, ID: "ID-" + spec.Name,
}, nil }, nil
}, },
}, buf) })
cmd := newSecretCreateCommand(cli) cmd := newSecretCreateCommand(cli)
cmd.SetArgs([]string{name, filepath.Join("testdata", secretDataFile)}) cmd.SetArgs([]string{name, filepath.Join("testdata", secretDataFile)})
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())
expected := golden.Get(t, actual, secretDataFile) golden.Assert(t, string(actual), secretDataFile)
assert.Equal(t, expected, actual) assert.Equal(t, "ID-"+name, strings.TrimSpace(cli.OutBuffer().String()))
assert.Equal(t, "ID-"+name, strings.TrimSpace(buf.String()))
} }
func TestSecretCreateWithLabels(t *testing.T) { func TestSecretCreateWithLabels(t *testing.T) {
@ -85,8 +82,7 @@ func TestSecretCreateWithLabels(t *testing.T) {
} }
name := "foo" name := "foo"
buf := new(bytes.Buffer) cli := test.NewFakeCli(&fakeClient{
cli := test.NewFakeCliWithOutput(&fakeClient{
secretCreateFunc: func(spec swarm.SecretSpec) (types.SecretCreateResponse, error) { secretCreateFunc: func(spec swarm.SecretSpec) (types.SecretCreateResponse, error) {
if spec.Name != name { if spec.Name != name {
return types.SecretCreateResponse{}, errors.Errorf("expected name %q, got %q", name, spec.Name) return types.SecretCreateResponse{}, errors.Errorf("expected name %q, got %q", name, spec.Name)
@ -100,12 +96,12 @@ func TestSecretCreateWithLabels(t *testing.T) {
ID: "ID-" + spec.Name, ID: "ID-" + spec.Name,
}, nil }, nil
}, },
}, buf) })
cmd := newSecretCreateCommand(cli) cmd := newSecretCreateCommand(cli)
cmd.SetArgs([]string{name, filepath.Join("testdata", secretDataFile)}) cmd.SetArgs([]string{name, filepath.Join("testdata", secretDataFile)})
cmd.Flags().Set("label", "lbl1=Label-foo") cmd.Flags().Set("label", "lbl1=Label-foo")
cmd.Flags().Set("label", "lbl2=Label-bar") cmd.Flags().Set("label", "lbl2=Label-bar")
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())
assert.Equal(t, "ID-"+name, strings.TrimSpace(buf.String())) assert.Equal(t, "ID-"+name, strings.TrimSpace(cli.OutBuffer().String()))
} }

View File

@ -1,7 +1,6 @@
package secret package secret
import ( import (
"bytes"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"testing" "testing"
@ -13,7 +12,7 @@ import (
// Import builders to get the builder function as package function // Import builders to get the builder function as package function
. "github.com/docker/cli/cli/internal/test/builders" . "github.com/docker/cli/cli/internal/test/builders"
"github.com/docker/docker/pkg/testutil" "github.com/docker/docker/pkg/testutil"
"github.com/docker/docker/pkg/testutil/golden" "github.com/gotestyourself/gotestyourself/golden"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -53,11 +52,10 @@ func TestSecretInspectErrors(t *testing.T) {
}, },
} }
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer)
cmd := newSecretInspectCommand( cmd := newSecretInspectCommand(
test.NewFakeCliWithOutput(&fakeClient{ test.NewFakeCli(&fakeClient{
secretInspectFunc: tc.secretInspectFunc, secretInspectFunc: tc.secretInspectFunc,
}, buf), }),
) )
cmd.SetArgs(tc.args) cmd.SetArgs(tc.args)
for key, value := range tc.flags { for key, value := range tc.flags {
@ -95,17 +93,13 @@ func TestSecretInspectWithoutFormat(t *testing.T) {
}, },
} }
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer) cli := test.NewFakeCli(&fakeClient{
cmd := newSecretInspectCommand( secretInspectFunc: tc.secretInspectFunc,
test.NewFakeCliWithOutput(&fakeClient{ })
secretInspectFunc: tc.secretInspectFunc, cmd := newSecretInspectCommand(cli)
}, buf),
)
cmd.SetArgs(tc.args) cmd.SetArgs(tc.args)
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())
actual := buf.String() golden.Assert(t, cli.OutBuffer().String(), fmt.Sprintf("secret-inspect-without-format.%s.golden", tc.name))
expected := golden.Get(t, []byte(actual), fmt.Sprintf("secret-inspect-without-format.%s.golden", tc.name))
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected))
} }
} }
@ -135,18 +129,14 @@ func TestSecretInspectWithFormat(t *testing.T) {
}, },
} }
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer) cli := test.NewFakeCli(&fakeClient{
cmd := newSecretInspectCommand( secretInspectFunc: tc.secretInspectFunc,
test.NewFakeCliWithOutput(&fakeClient{ })
secretInspectFunc: tc.secretInspectFunc, cmd := newSecretInspectCommand(cli)
}, buf),
)
cmd.SetArgs(tc.args) cmd.SetArgs(tc.args)
cmd.Flags().Set("format", tc.format) cmd.Flags().Set("format", tc.format)
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())
actual := buf.String() golden.Assert(t, cli.OutBuffer().String(), fmt.Sprintf("secret-inspect-with-format.%s.golden", tc.name))
expected := golden.Get(t, []byte(actual), fmt.Sprintf("secret-inspect-with-format.%s.golden", tc.name))
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected))
} }
} }
@ -171,16 +161,13 @@ func TestSecretInspectPretty(t *testing.T) {
}, },
} }
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer) cli := test.NewFakeCli(&fakeClient{
cmd := newSecretInspectCommand( secretInspectFunc: tc.secretInspectFunc,
test.NewFakeCliWithOutput(&fakeClient{ })
secretInspectFunc: tc.secretInspectFunc, cmd := newSecretInspectCommand(cli)
}, buf))
cmd.SetArgs([]string{"secretID"}) cmd.SetArgs([]string{"secretID"})
cmd.Flags().Set("pretty", "true") cmd.Flags().Set("pretty", "true")
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())
actual := buf.String() golden.Assert(t, cli.OutBuffer().String(), fmt.Sprintf("secret-inspect-pretty.%s.golden", tc.name))
expected := golden.Get(t, []byte(actual), fmt.Sprintf("secret-inspect-pretty.%s.golden", tc.name))
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected))
} }
} }

View File

@ -14,7 +14,7 @@ import (
// Import builders to get the builder function as package function // Import builders to get the builder function as package function
. "github.com/docker/cli/cli/internal/test/builders" . "github.com/docker/cli/cli/internal/test/builders"
"github.com/docker/docker/pkg/testutil" "github.com/docker/docker/pkg/testutil"
"github.com/docker/docker/pkg/testutil/golden" "github.com/gotestyourself/gotestyourself/golden"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -36,11 +36,10 @@ func TestSecretListErrors(t *testing.T) {
}, },
} }
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer)
cmd := newSecretListCommand( cmd := newSecretListCommand(
test.NewFakeCliWithOutput(&fakeClient{ test.NewFakeCli(&fakeClient{
secretListFunc: tc.secretListFunc, secretListFunc: tc.secretListFunc,
}, buf), }),
) )
cmd.SetArgs(tc.args) cmd.SetArgs(tc.args)
cmd.SetOutput(ioutil.Discard) cmd.SetOutput(ioutil.Discard)
@ -50,7 +49,7 @@ func TestSecretListErrors(t *testing.T) {
func TestSecretList(t *testing.T) { func TestSecretList(t *testing.T) {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
cli := test.NewFakeCliWithOutput(&fakeClient{ cli := test.NewFakeCli(&fakeClient{
secretListFunc: func(options types.SecretListOptions) ([]swarm.Secret, error) { secretListFunc: func(options types.SecretListOptions) ([]swarm.Secret, error) {
return []swarm.Secret{ return []swarm.Secret{
*Secret(SecretID("ID-foo"), *Secret(SecretID("ID-foo"),
@ -67,18 +66,15 @@ func TestSecretList(t *testing.T) {
), ),
}, nil }, nil
}, },
}, buf) })
cmd := newSecretListCommand(cli) cmd := newSecretListCommand(cli)
cmd.SetOutput(buf) cmd.SetOutput(buf)
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())
actual := buf.String() golden.Assert(t, cli.OutBuffer().String(), "secret-list.golden")
expected := golden.Get(t, []byte(actual), "secret-list.golden")
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected))
} }
func TestSecretListWithQuietOption(t *testing.T) { func TestSecretListWithQuietOption(t *testing.T) {
buf := new(bytes.Buffer) cli := test.NewFakeCli(&fakeClient{
cli := test.NewFakeCliWithOutput(&fakeClient{
secretListFunc: func(options types.SecretListOptions) ([]swarm.Secret, error) { secretListFunc: func(options types.SecretListOptions) ([]swarm.Secret, error) {
return []swarm.Secret{ return []swarm.Secret{
*Secret(SecretID("ID-foo"), SecretName("foo")), *Secret(SecretID("ID-foo"), SecretName("foo")),
@ -87,18 +83,15 @@ func TestSecretListWithQuietOption(t *testing.T) {
})), })),
}, nil }, nil
}, },
}, buf) })
cmd := newSecretListCommand(cli) cmd := newSecretListCommand(cli)
cmd.Flags().Set("quiet", "true") cmd.Flags().Set("quiet", "true")
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())
actual := buf.String() golden.Assert(t, cli.OutBuffer().String(), "secret-list-with-quiet-option.golden")
expected := golden.Get(t, []byte(actual), "secret-list-with-quiet-option.golden")
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected))
} }
func TestSecretListWithConfigFormat(t *testing.T) { func TestSecretListWithConfigFormat(t *testing.T) {
buf := new(bytes.Buffer) cli := test.NewFakeCli(&fakeClient{
cli := test.NewFakeCliWithOutput(&fakeClient{
secretListFunc: func(options types.SecretListOptions) ([]swarm.Secret, error) { secretListFunc: func(options types.SecretListOptions) ([]swarm.Secret, error) {
return []swarm.Secret{ return []swarm.Secret{
*Secret(SecretID("ID-foo"), SecretName("foo")), *Secret(SecretID("ID-foo"), SecretName("foo")),
@ -107,20 +100,17 @@ func TestSecretListWithConfigFormat(t *testing.T) {
})), })),
}, nil }, nil
}, },
}, buf) })
cli.SetConfigFile(&configfile.ConfigFile{ cli.SetConfigFile(&configfile.ConfigFile{
SecretFormat: "{{ .Name }} {{ .Labels }}", SecretFormat: "{{ .Name }} {{ .Labels }}",
}) })
cmd := newSecretListCommand(cli) cmd := newSecretListCommand(cli)
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())
actual := buf.String() golden.Assert(t, cli.OutBuffer().String(), "secret-list-with-config-format.golden")
expected := golden.Get(t, []byte(actual), "secret-list-with-config-format.golden")
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected))
} }
func TestSecretListWithFormat(t *testing.T) { func TestSecretListWithFormat(t *testing.T) {
buf := new(bytes.Buffer) cli := test.NewFakeCli(&fakeClient{
cli := test.NewFakeCliWithOutput(&fakeClient{
secretListFunc: func(options types.SecretListOptions) ([]swarm.Secret, error) { secretListFunc: func(options types.SecretListOptions) ([]swarm.Secret, error) {
return []swarm.Secret{ return []swarm.Secret{
*Secret(SecretID("ID-foo"), SecretName("foo")), *Secret(SecretID("ID-foo"), SecretName("foo")),
@ -129,18 +119,15 @@ func TestSecretListWithFormat(t *testing.T) {
})), })),
}, nil }, nil
}, },
}, buf) })
cmd := newSecretListCommand(cli) cmd := newSecretListCommand(cli)
cmd.Flags().Set("format", "{{ .Name }} {{ .Labels }}") cmd.Flags().Set("format", "{{ .Name }} {{ .Labels }}")
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())
actual := buf.String() golden.Assert(t, cli.OutBuffer().String(), "secret-list-with-format.golden")
expected := golden.Get(t, []byte(actual), "secret-list-with-format.golden")
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected))
} }
func TestSecretListWithFilter(t *testing.T) { func TestSecretListWithFilter(t *testing.T) {
buf := new(bytes.Buffer) cli := test.NewFakeCli(&fakeClient{
cli := test.NewFakeCliWithOutput(&fakeClient{
secretListFunc: func(options types.SecretListOptions) ([]swarm.Secret, error) { secretListFunc: func(options types.SecretListOptions) ([]swarm.Secret, error) {
assert.Equal(t, "foo", options.Filters.Get("name")[0], "foo") assert.Equal(t, "foo", options.Filters.Get("name")[0], "foo")
assert.Equal(t, "lbl1=Label-bar", options.Filters.Get("label")[0]) assert.Equal(t, "lbl1=Label-bar", options.Filters.Get("label")[0])
@ -159,12 +146,10 @@ func TestSecretListWithFilter(t *testing.T) {
), ),
}, nil }, nil
}, },
}, buf) })
cmd := newSecretListCommand(cli) cmd := newSecretListCommand(cli)
cmd.Flags().Set("filter", "name=foo") cmd.Flags().Set("filter", "name=foo")
cmd.Flags().Set("filter", "label=lbl1=Label-bar") cmd.Flags().Set("filter", "label=lbl1=Label-bar")
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())
actual := buf.String() golden.Assert(t, cli.OutBuffer().String(), "secret-list-with-filter.golden")
expected := golden.Get(t, []byte(actual), "secret-list-with-filter.golden")
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected))
} }

View File

@ -1,7 +1,6 @@
package secret package secret
import ( import (
"bytes"
"io/ioutil" "io/ioutil"
"strings" "strings"
"testing" "testing"
@ -31,11 +30,10 @@ func TestSecretRemoveErrors(t *testing.T) {
}, },
} }
for _, tc := range testCases { for _, tc := range testCases {
buf := new(bytes.Buffer)
cmd := newSecretRemoveCommand( cmd := newSecretRemoveCommand(
test.NewFakeCliWithOutput(&fakeClient{ test.NewFakeCli(&fakeClient{
secretRemoveFunc: tc.secretRemoveFunc, secretRemoveFunc: tc.secretRemoveFunc,
}, buf), }),
) )
cmd.SetArgs(tc.args) cmd.SetArgs(tc.args)
cmd.SetOutput(ioutil.Discard) cmd.SetOutput(ioutil.Discard)
@ -45,27 +43,25 @@ func TestSecretRemoveErrors(t *testing.T) {
func TestSecretRemoveWithName(t *testing.T) { func TestSecretRemoveWithName(t *testing.T) {
names := []string{"foo", "bar"} names := []string{"foo", "bar"}
buf := new(bytes.Buffer)
var removedSecrets []string var removedSecrets []string
cli := test.NewFakeCliWithOutput(&fakeClient{ cli := test.NewFakeCli(&fakeClient{
secretRemoveFunc: func(name string) error { secretRemoveFunc: func(name string) error {
removedSecrets = append(removedSecrets, name) removedSecrets = append(removedSecrets, name)
return nil return nil
}, },
}, buf) })
cmd := newSecretRemoveCommand(cli) cmd := newSecretRemoveCommand(cli)
cmd.SetArgs(names) cmd.SetArgs(names)
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())
assert.Equal(t, names, strings.Split(strings.TrimSpace(buf.String()), "\n")) assert.Equal(t, names, strings.Split(strings.TrimSpace(cli.OutBuffer().String()), "\n"))
assert.Equal(t, names, removedSecrets) assert.Equal(t, names, removedSecrets)
} }
func TestSecretRemoveContinueAfterError(t *testing.T) { func TestSecretRemoveContinueAfterError(t *testing.T) {
names := []string{"foo", "bar"} names := []string{"foo", "bar"}
buf := new(bytes.Buffer)
var removedSecrets []string var removedSecrets []string
cli := test.NewFakeCliWithOutput(&fakeClient{ cli := test.NewFakeCli(&fakeClient{
secretRemoveFunc: func(name string) error { secretRemoveFunc: func(name string) error {
removedSecrets = append(removedSecrets, name) removedSecrets = append(removedSecrets, name)
if name == "foo" { if name == "foo" {
@ -73,7 +69,7 @@ func TestSecretRemoveContinueAfterError(t *testing.T) {
} }
return nil return nil
}, },
}, buf) })
cmd := newSecretRemoveCommand(cli) cmd := newSecretRemoveCommand(cli)
cmd.SetOutput(ioutil.Discard) cmd.SetOutput(ioutil.Discard)

View File

@ -1,6 +1,6 @@
ID: secretID ID: secretID
Name: secretName Name: secretName
Labels: Labels:
- lbl1=value1 - lbl1=value1
Created at: 0001-01-01 00:00:00+0000 utc Created at: 0001-01-01 00:00:00 +0000 utc
Updated at: 0001-01-01 00:00:00+0000 utc Updated at: 0001-01-01 00:00:00 +0000 utc

View File

@ -1,7 +1,7 @@
[ [
{ {
"ID": "ID-foo", "ID": "ID-foo",
"Version": {}, "Version": {},
"CreatedAt": "0001-01-01T00:00:00Z", "CreatedAt": "0001-01-01T00:00:00Z",
"UpdatedAt": "0001-01-01T00:00:00Z", "UpdatedAt": "0001-01-01T00:00:00Z",
"Spec": { "Spec": {
@ -13,7 +13,7 @@
}, },
{ {
"ID": "ID-bar", "ID": "ID-bar",
"Version": {}, "Version": {},
"CreatedAt": "0001-01-01T00:00:00Z", "CreatedAt": "0001-01-01T00:00:00Z",
"UpdatedAt": "0001-01-01T00:00:00Z", "UpdatedAt": "0001-01-01T00:00:00Z",
"Spec": { "Spec": {

View File

@ -1,9 +1,9 @@
[ [
{ {
"ID": "ID-foo", "ID": "ID-foo",
"Version": {}, "Version": {},
"CreatedAt": "0001-01-01T00:00:00Z", "CreatedAt": "0001-01-01T00:00:00Z",
"UpdatedAt": "0001-01-01T00:00:00Z", "UpdatedAt": "0001-01-01T00:00:00Z",
"Spec": { "Spec": {
"Name": "foo", "Name": "foo",
"Labels": null "Labels": null

View File

@ -1,2 +1,2 @@
foo foo
bar label=label-bar bar label=label-bar

View File

@ -1,2 +1,2 @@
foo foo
bar label=label-bar bar label=label-bar

View File

@ -3,14 +3,12 @@ package service
import ( import (
"testing" "testing"
"golang.org/x/net/context"
"github.com/docker/cli/cli/internal/test" "github.com/docker/cli/cli/internal/test"
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/swarm" "github.com/docker/docker/api/types/swarm"
"github.com/docker/docker/pkg/testutil" "github.com/gotestyourself/gotestyourself/golden"
"github.com/docker/docker/pkg/testutil/golden"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"golang.org/x/net/context"
) )
func TestServiceListOrder(t *testing.T) { func TestServiceListOrder(t *testing.T) {
@ -26,7 +24,5 @@ func TestServiceListOrder(t *testing.T) {
cmd := newListCommand(cli) cmd := newListCommand(cli)
cmd.Flags().Set("format", "{{.Name}}") cmd.Flags().Set("format", "{{.Name}}")
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())
actual := cli.OutBuffer().String() golden.Assert(t, cli.OutBuffer().String(), "service-list-sort.golden")
expected := golden.Get(t, []byte(actual), "service-list-sort.golden")
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected))
} }

View File

@ -3,8 +3,6 @@ package service
import ( import (
"testing" "testing"
"bytes"
"github.com/docker/cli/cli/internal/test" "github.com/docker/cli/cli/internal/test"
"github.com/docker/cli/opts" "github.com/docker/cli/opts"
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
@ -82,8 +80,7 @@ func TestRunPSWarnsOnNotFound(t *testing.T) {
}, },
} }
out := new(bytes.Buffer) cli := test.NewFakeCli(client)
cli := test.NewFakeCliWithOutput(client, out)
options := psOptions{ options := psOptions{
services: []string{"foo", "bar"}, services: []string{"foo", "bar"},
filter: opts.NewFilterOpt(), filter: opts.NewFilterOpt(),