From 4c62d7288f50bad15674431944cd1b6a5bc3fb8c Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Wed, 16 Aug 2017 12:53:41 -0400 Subject: [PATCH] Update service and secret command tests to new golden Signed-off-by: Daniel Nephin --- cli/command/secret/create_test.go | 20 +++----- cli/command/secret/inspect_test.go | 49 +++++++----------- cli/command/secret/ls_test.go | 51 +++++++------------ cli/command/secret/remove_test.go | 18 +++---- .../secret-inspect-pretty.simple.golden | 10 ++-- ...format.multiple-secrets-with-labels.golden | 4 +- ...nspect-without-format.single-secret.golden | 6 +-- .../secret-list-with-config-format.golden | 2 +- .../testdata/secret-list-with-format.golden | 2 +- cli/command/service/list_test.go | 10 ++-- cli/command/service/ps_test.go | 5 +- 11 files changed, 67 insertions(+), 110 deletions(-) diff --git a/cli/command/secret/create_test.go b/cli/command/secret/create_test.go index 592ede0fe5..0887601ab6 100644 --- a/cli/command/secret/create_test.go +++ b/cli/command/secret/create_test.go @@ -1,7 +1,6 @@ package secret import ( - "bytes" "io/ioutil" "path/filepath" "reflect" @@ -12,7 +11,7 @@ import ( "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/swarm" "github.com/docker/docker/pkg/testutil" - "github.com/docker/docker/pkg/testutil/golden" + "github.com/gotestyourself/gotestyourself/golden" "github.com/pkg/errors" "github.com/stretchr/testify/assert" ) @@ -54,9 +53,8 @@ func TestSecretCreateErrors(t *testing.T) { func TestSecretCreateWithName(t *testing.T) { name := "foo" - buf := new(bytes.Buffer) var actual []byte - cli := test.NewFakeCliWithOutput(&fakeClient{ + cli := test.NewFakeCli(&fakeClient{ secretCreateFunc: func(spec swarm.SecretSpec) (types.SecretCreateResponse, error) { if spec.Name != 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, }, nil }, - }, buf) + }) cmd := newSecretCreateCommand(cli) cmd.SetArgs([]string{name, filepath.Join("testdata", secretDataFile)}) assert.NoError(t, cmd.Execute()) - expected := golden.Get(t, actual, secretDataFile) - assert.Equal(t, expected, actual) - assert.Equal(t, "ID-"+name, strings.TrimSpace(buf.String())) + golden.Assert(t, string(actual), secretDataFile) + assert.Equal(t, "ID-"+name, strings.TrimSpace(cli.OutBuffer().String())) } func TestSecretCreateWithLabels(t *testing.T) { @@ -85,8 +82,7 @@ func TestSecretCreateWithLabels(t *testing.T) { } name := "foo" - buf := new(bytes.Buffer) - cli := test.NewFakeCliWithOutput(&fakeClient{ + cli := test.NewFakeCli(&fakeClient{ secretCreateFunc: func(spec swarm.SecretSpec) (types.SecretCreateResponse, error) { if spec.Name != 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, }, nil }, - }, buf) + }) cmd := newSecretCreateCommand(cli) cmd.SetArgs([]string{name, filepath.Join("testdata", secretDataFile)}) cmd.Flags().Set("label", "lbl1=Label-foo") cmd.Flags().Set("label", "lbl2=Label-bar") assert.NoError(t, cmd.Execute()) - assert.Equal(t, "ID-"+name, strings.TrimSpace(buf.String())) + assert.Equal(t, "ID-"+name, strings.TrimSpace(cli.OutBuffer().String())) } diff --git a/cli/command/secret/inspect_test.go b/cli/command/secret/inspect_test.go index 093624ddfe..eef789d80b 100644 --- a/cli/command/secret/inspect_test.go +++ b/cli/command/secret/inspect_test.go @@ -1,7 +1,6 @@ package secret import ( - "bytes" "fmt" "io/ioutil" "testing" @@ -13,7 +12,7 @@ import ( // Import builders to get the builder function as package function . "github.com/docker/cli/cli/internal/test/builders" "github.com/docker/docker/pkg/testutil" - "github.com/docker/docker/pkg/testutil/golden" + "github.com/gotestyourself/gotestyourself/golden" "github.com/stretchr/testify/assert" ) @@ -53,11 +52,10 @@ func TestSecretInspectErrors(t *testing.T) { }, } for _, tc := range testCases { - buf := new(bytes.Buffer) cmd := newSecretInspectCommand( - test.NewFakeCliWithOutput(&fakeClient{ + test.NewFakeCli(&fakeClient{ secretInspectFunc: tc.secretInspectFunc, - }, buf), + }), ) cmd.SetArgs(tc.args) for key, value := range tc.flags { @@ -95,17 +93,13 @@ func TestSecretInspectWithoutFormat(t *testing.T) { }, } for _, tc := range testCases { - buf := new(bytes.Buffer) - cmd := newSecretInspectCommand( - test.NewFakeCliWithOutput(&fakeClient{ - secretInspectFunc: tc.secretInspectFunc, - }, buf), - ) + cli := test.NewFakeCli(&fakeClient{ + secretInspectFunc: tc.secretInspectFunc, + }) + cmd := newSecretInspectCommand(cli) cmd.SetArgs(tc.args) assert.NoError(t, cmd.Execute()) - actual := buf.String() - expected := golden.Get(t, []byte(actual), fmt.Sprintf("secret-inspect-without-format.%s.golden", tc.name)) - testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected)) + golden.Assert(t, cli.OutBuffer().String(), fmt.Sprintf("secret-inspect-without-format.%s.golden", tc.name)) } } @@ -135,18 +129,14 @@ func TestSecretInspectWithFormat(t *testing.T) { }, } for _, tc := range testCases { - buf := new(bytes.Buffer) - cmd := newSecretInspectCommand( - test.NewFakeCliWithOutput(&fakeClient{ - secretInspectFunc: tc.secretInspectFunc, - }, buf), - ) + cli := test.NewFakeCli(&fakeClient{ + secretInspectFunc: tc.secretInspectFunc, + }) + cmd := newSecretInspectCommand(cli) cmd.SetArgs(tc.args) cmd.Flags().Set("format", tc.format) assert.NoError(t, cmd.Execute()) - actual := buf.String() - expected := golden.Get(t, []byte(actual), fmt.Sprintf("secret-inspect-with-format.%s.golden", tc.name)) - testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected)) + golden.Assert(t, cli.OutBuffer().String(), fmt.Sprintf("secret-inspect-with-format.%s.golden", tc.name)) } } @@ -171,16 +161,13 @@ func TestSecretInspectPretty(t *testing.T) { }, } for _, tc := range testCases { - buf := new(bytes.Buffer) - cmd := newSecretInspectCommand( - test.NewFakeCliWithOutput(&fakeClient{ - secretInspectFunc: tc.secretInspectFunc, - }, buf)) + cli := test.NewFakeCli(&fakeClient{ + secretInspectFunc: tc.secretInspectFunc, + }) + cmd := newSecretInspectCommand(cli) cmd.SetArgs([]string{"secretID"}) cmd.Flags().Set("pretty", "true") assert.NoError(t, cmd.Execute()) - actual := buf.String() - expected := golden.Get(t, []byte(actual), fmt.Sprintf("secret-inspect-pretty.%s.golden", tc.name)) - testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected)) + golden.Assert(t, cli.OutBuffer().String(), fmt.Sprintf("secret-inspect-pretty.%s.golden", tc.name)) } } diff --git a/cli/command/secret/ls_test.go b/cli/command/secret/ls_test.go index fb033efea6..cced136106 100644 --- a/cli/command/secret/ls_test.go +++ b/cli/command/secret/ls_test.go @@ -14,7 +14,7 @@ import ( // Import builders to get the builder function as package function . "github.com/docker/cli/cli/internal/test/builders" "github.com/docker/docker/pkg/testutil" - "github.com/docker/docker/pkg/testutil/golden" + "github.com/gotestyourself/gotestyourself/golden" "github.com/stretchr/testify/assert" ) @@ -36,11 +36,10 @@ func TestSecretListErrors(t *testing.T) { }, } for _, tc := range testCases { - buf := new(bytes.Buffer) cmd := newSecretListCommand( - test.NewFakeCliWithOutput(&fakeClient{ + test.NewFakeCli(&fakeClient{ secretListFunc: tc.secretListFunc, - }, buf), + }), ) cmd.SetArgs(tc.args) cmd.SetOutput(ioutil.Discard) @@ -50,7 +49,7 @@ func TestSecretListErrors(t *testing.T) { func TestSecretList(t *testing.T) { buf := new(bytes.Buffer) - cli := test.NewFakeCliWithOutput(&fakeClient{ + cli := test.NewFakeCli(&fakeClient{ secretListFunc: func(options types.SecretListOptions) ([]swarm.Secret, error) { return []swarm.Secret{ *Secret(SecretID("ID-foo"), @@ -67,18 +66,15 @@ func TestSecretList(t *testing.T) { ), }, nil }, - }, buf) + }) cmd := newSecretListCommand(cli) cmd.SetOutput(buf) assert.NoError(t, cmd.Execute()) - actual := buf.String() - expected := golden.Get(t, []byte(actual), "secret-list.golden") - testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected)) + golden.Assert(t, cli.OutBuffer().String(), "secret-list.golden") } func TestSecretListWithQuietOption(t *testing.T) { - buf := new(bytes.Buffer) - cli := test.NewFakeCliWithOutput(&fakeClient{ + cli := test.NewFakeCli(&fakeClient{ secretListFunc: func(options types.SecretListOptions) ([]swarm.Secret, error) { return []swarm.Secret{ *Secret(SecretID("ID-foo"), SecretName("foo")), @@ -87,18 +83,15 @@ func TestSecretListWithQuietOption(t *testing.T) { })), }, nil }, - }, buf) + }) cmd := newSecretListCommand(cli) cmd.Flags().Set("quiet", "true") assert.NoError(t, cmd.Execute()) - actual := buf.String() - expected := golden.Get(t, []byte(actual), "secret-list-with-quiet-option.golden") - testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected)) + golden.Assert(t, cli.OutBuffer().String(), "secret-list-with-quiet-option.golden") } func TestSecretListWithConfigFormat(t *testing.T) { - buf := new(bytes.Buffer) - cli := test.NewFakeCliWithOutput(&fakeClient{ + cli := test.NewFakeCli(&fakeClient{ secretListFunc: func(options types.SecretListOptions) ([]swarm.Secret, error) { return []swarm.Secret{ *Secret(SecretID("ID-foo"), SecretName("foo")), @@ -107,20 +100,17 @@ func TestSecretListWithConfigFormat(t *testing.T) { })), }, nil }, - }, buf) + }) cli.SetConfigFile(&configfile.ConfigFile{ SecretFormat: "{{ .Name }} {{ .Labels }}", }) cmd := newSecretListCommand(cli) assert.NoError(t, cmd.Execute()) - actual := buf.String() - expected := golden.Get(t, []byte(actual), "secret-list-with-config-format.golden") - testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected)) + golden.Assert(t, cli.OutBuffer().String(), "secret-list-with-config-format.golden") } func TestSecretListWithFormat(t *testing.T) { - buf := new(bytes.Buffer) - cli := test.NewFakeCliWithOutput(&fakeClient{ + cli := test.NewFakeCli(&fakeClient{ secretListFunc: func(options types.SecretListOptions) ([]swarm.Secret, error) { return []swarm.Secret{ *Secret(SecretID("ID-foo"), SecretName("foo")), @@ -129,18 +119,15 @@ func TestSecretListWithFormat(t *testing.T) { })), }, nil }, - }, buf) + }) cmd := newSecretListCommand(cli) cmd.Flags().Set("format", "{{ .Name }} {{ .Labels }}") assert.NoError(t, cmd.Execute()) - actual := buf.String() - expected := golden.Get(t, []byte(actual), "secret-list-with-format.golden") - testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected)) + golden.Assert(t, cli.OutBuffer().String(), "secret-list-with-format.golden") } func TestSecretListWithFilter(t *testing.T) { - buf := new(bytes.Buffer) - cli := test.NewFakeCliWithOutput(&fakeClient{ + cli := test.NewFakeCli(&fakeClient{ secretListFunc: func(options types.SecretListOptions) ([]swarm.Secret, error) { assert.Equal(t, "foo", options.Filters.Get("name")[0], "foo") assert.Equal(t, "lbl1=Label-bar", options.Filters.Get("label")[0]) @@ -159,12 +146,10 @@ func TestSecretListWithFilter(t *testing.T) { ), }, nil }, - }, buf) + }) cmd := newSecretListCommand(cli) cmd.Flags().Set("filter", "name=foo") cmd.Flags().Set("filter", "label=lbl1=Label-bar") assert.NoError(t, cmd.Execute()) - actual := buf.String() - expected := golden.Get(t, []byte(actual), "secret-list-with-filter.golden") - testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected)) + golden.Assert(t, cli.OutBuffer().String(), "secret-list-with-filter.golden") } diff --git a/cli/command/secret/remove_test.go b/cli/command/secret/remove_test.go index ad479c0a80..f3e427f7d7 100644 --- a/cli/command/secret/remove_test.go +++ b/cli/command/secret/remove_test.go @@ -1,7 +1,6 @@ package secret import ( - "bytes" "io/ioutil" "strings" "testing" @@ -31,11 +30,10 @@ func TestSecretRemoveErrors(t *testing.T) { }, } for _, tc := range testCases { - buf := new(bytes.Buffer) cmd := newSecretRemoveCommand( - test.NewFakeCliWithOutput(&fakeClient{ + test.NewFakeCli(&fakeClient{ secretRemoveFunc: tc.secretRemoveFunc, - }, buf), + }), ) cmd.SetArgs(tc.args) cmd.SetOutput(ioutil.Discard) @@ -45,27 +43,25 @@ func TestSecretRemoveErrors(t *testing.T) { func TestSecretRemoveWithName(t *testing.T) { names := []string{"foo", "bar"} - buf := new(bytes.Buffer) var removedSecrets []string - cli := test.NewFakeCliWithOutput(&fakeClient{ + cli := test.NewFakeCli(&fakeClient{ secretRemoveFunc: func(name string) error { removedSecrets = append(removedSecrets, name) return nil }, - }, buf) + }) cmd := newSecretRemoveCommand(cli) cmd.SetArgs(names) 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) } func TestSecretRemoveContinueAfterError(t *testing.T) { names := []string{"foo", "bar"} - buf := new(bytes.Buffer) var removedSecrets []string - cli := test.NewFakeCliWithOutput(&fakeClient{ + cli := test.NewFakeCli(&fakeClient{ secretRemoveFunc: func(name string) error { removedSecrets = append(removedSecrets, name) if name == "foo" { @@ -73,7 +69,7 @@ func TestSecretRemoveContinueAfterError(t *testing.T) { } return nil }, - }, buf) + }) cmd := newSecretRemoveCommand(cli) cmd.SetOutput(ioutil.Discard) diff --git a/cli/command/secret/testdata/secret-inspect-pretty.simple.golden b/cli/command/secret/testdata/secret-inspect-pretty.simple.golden index cc14091e8b..4e2df1fb4c 100644 --- a/cli/command/secret/testdata/secret-inspect-pretty.simple.golden +++ b/cli/command/secret/testdata/secret-inspect-pretty.simple.golden @@ -1,6 +1,6 @@ -ID: secretID -Name: secretName +ID: secretID +Name: secretName Labels: - - lbl1=value1 -Created at: 0001-01-01 00:00:00+0000 utc -Updated at: 0001-01-01 00:00:00+0000 utc + - lbl1=value1 +Created at: 0001-01-01 00:00:00 +0000 utc +Updated at: 0001-01-01 00:00:00 +0000 utc diff --git a/cli/command/secret/testdata/secret-inspect-without-format.multiple-secrets-with-labels.golden b/cli/command/secret/testdata/secret-inspect-without-format.multiple-secrets-with-labels.golden index 6887c185f1..b01a400c57 100644 --- a/cli/command/secret/testdata/secret-inspect-without-format.multiple-secrets-with-labels.golden +++ b/cli/command/secret/testdata/secret-inspect-without-format.multiple-secrets-with-labels.golden @@ -1,7 +1,7 @@ [ { "ID": "ID-foo", - "Version": {}, + "Version": {}, "CreatedAt": "0001-01-01T00:00:00Z", "UpdatedAt": "0001-01-01T00:00:00Z", "Spec": { @@ -13,7 +13,7 @@ }, { "ID": "ID-bar", - "Version": {}, + "Version": {}, "CreatedAt": "0001-01-01T00:00:00Z", "UpdatedAt": "0001-01-01T00:00:00Z", "Spec": { diff --git a/cli/command/secret/testdata/secret-inspect-without-format.single-secret.golden b/cli/command/secret/testdata/secret-inspect-without-format.single-secret.golden index ea42ec6f4f..c4f41c1067 100644 --- a/cli/command/secret/testdata/secret-inspect-without-format.single-secret.golden +++ b/cli/command/secret/testdata/secret-inspect-without-format.single-secret.golden @@ -1,9 +1,9 @@ [ { "ID": "ID-foo", - "Version": {}, - "CreatedAt": "0001-01-01T00:00:00Z", - "UpdatedAt": "0001-01-01T00:00:00Z", + "Version": {}, + "CreatedAt": "0001-01-01T00:00:00Z", + "UpdatedAt": "0001-01-01T00:00:00Z", "Spec": { "Name": "foo", "Labels": null diff --git a/cli/command/secret/testdata/secret-list-with-config-format.golden b/cli/command/secret/testdata/secret-list-with-config-format.golden index 9a47538804..11c39229b8 100644 --- a/cli/command/secret/testdata/secret-list-with-config-format.golden +++ b/cli/command/secret/testdata/secret-list-with-config-format.golden @@ -1,2 +1,2 @@ -foo +foo bar label=label-bar diff --git a/cli/command/secret/testdata/secret-list-with-format.golden b/cli/command/secret/testdata/secret-list-with-format.golden index 9a47538804..11c39229b8 100644 --- a/cli/command/secret/testdata/secret-list-with-format.golden +++ b/cli/command/secret/testdata/secret-list-with-format.golden @@ -1,2 +1,2 @@ -foo +foo bar label=label-bar diff --git a/cli/command/service/list_test.go b/cli/command/service/list_test.go index 85ccd1fb79..297f5f9b17 100644 --- a/cli/command/service/list_test.go +++ b/cli/command/service/list_test.go @@ -3,14 +3,12 @@ package service import ( "testing" - "golang.org/x/net/context" - "github.com/docker/cli/cli/internal/test" "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/swarm" - "github.com/docker/docker/pkg/testutil" - "github.com/docker/docker/pkg/testutil/golden" + "github.com/gotestyourself/gotestyourself/golden" "github.com/stretchr/testify/assert" + "golang.org/x/net/context" ) func TestServiceListOrder(t *testing.T) { @@ -26,7 +24,5 @@ func TestServiceListOrder(t *testing.T) { cmd := newListCommand(cli) cmd.Flags().Set("format", "{{.Name}}") assert.NoError(t, cmd.Execute()) - actual := cli.OutBuffer().String() - expected := golden.Get(t, []byte(actual), "service-list-sort.golden") - testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected)) + golden.Assert(t, cli.OutBuffer().String(), "service-list-sort.golden") } diff --git a/cli/command/service/ps_test.go b/cli/command/service/ps_test.go index dac79ab50a..083c58f91f 100644 --- a/cli/command/service/ps_test.go +++ b/cli/command/service/ps_test.go @@ -3,8 +3,6 @@ package service import ( "testing" - "bytes" - "github.com/docker/cli/cli/internal/test" "github.com/docker/cli/opts" "github.com/docker/docker/api/types" @@ -82,8 +80,7 @@ func TestRunPSWarnsOnNotFound(t *testing.T) { }, } - out := new(bytes.Buffer) - cli := test.NewFakeCliWithOutput(client, out) + cli := test.NewFakeCli(client) options := psOptions{ services: []string{"foo", "bar"}, filter: opts.NewFilterOpt(),