From 3d68aa84167411c53a04df53130c533791ce55a5 Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Wed, 16 Aug 2017 13:48:31 -0400 Subject: [PATCH] Update swarm command tests to use the new golden Signed-off-by: Daniel Nephin --- cli/command/swarm/init_test.go | 28 ++++++--------- cli/command/swarm/join_test.go | 17 ++++----- cli/command/swarm/join_token_test.go | 35 ++++++++----------- .../testdata/jointoken-manager-rotate.golden | 1 + .../swarm/testdata/jointoken-manager.golden | 1 + .../swarm/testdata/jointoken-worker.golden | 1 + cli/command/swarm/unlock_key_test.go | 6 ++-- cli/command/swarm/update_test.go | 6 ++-- 8 files changed, 40 insertions(+), 55 deletions(-) diff --git a/cli/command/swarm/init_test.go b/cli/command/swarm/init_test.go index f6969d1b0e..5991c3dd54 100644 --- a/cli/command/swarm/init_test.go +++ b/cli/command/swarm/init_test.go @@ -1,7 +1,6 @@ package swarm import ( - "bytes" "fmt" "io/ioutil" "testing" @@ -9,8 +8,7 @@ import ( "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/pkg/errors" "github.com/stretchr/testify/assert" ) @@ -65,14 +63,13 @@ func TestSwarmInitErrorOnAPIFailure(t *testing.T) { }, } for _, tc := range testCases { - buf := new(bytes.Buffer) cmd := newInitCommand( - test.NewFakeCliWithOutput(&fakeClient{ + test.NewFakeCli(&fakeClient{ swarmInitFunc: tc.swarmInitFunc, swarmInspectFunc: tc.swarmInspectFunc, swarmGetUnlockKeyFunc: tc.swarmGetUnlockKeyFunc, nodeInspectFunc: tc.nodeInspectFunc, - }, buf)) + })) for key, value := range tc.flags { cmd.Flags().Set(key, value) } @@ -112,20 +109,17 @@ func TestSwarmInit(t *testing.T) { }, } for _, tc := range testCases { - buf := new(bytes.Buffer) - cmd := newInitCommand( - test.NewFakeCliWithOutput(&fakeClient{ - swarmInitFunc: tc.swarmInitFunc, - swarmInspectFunc: tc.swarmInspectFunc, - swarmGetUnlockKeyFunc: tc.swarmGetUnlockKeyFunc, - nodeInspectFunc: tc.nodeInspectFunc, - }, buf)) + cli := test.NewFakeCli(&fakeClient{ + swarmInitFunc: tc.swarmInitFunc, + swarmInspectFunc: tc.swarmInspectFunc, + swarmGetUnlockKeyFunc: tc.swarmGetUnlockKeyFunc, + nodeInspectFunc: tc.nodeInspectFunc, + }) + cmd := newInitCommand(cli) for key, value := range tc.flags { cmd.Flags().Set(key, value) } assert.NoError(t, cmd.Execute()) - actual := buf.String() - expected := golden.Get(t, []byte(actual), fmt.Sprintf("init-%s.golden", tc.name)) - testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected)) + golden.Assert(t, cli.OutBuffer().String(), fmt.Sprintf("init-%s.golden", tc.name)) } } diff --git a/cli/command/swarm/join_test.go b/cli/command/swarm/join_test.go index 331f2c1753..d9f5215b7b 100644 --- a/cli/command/swarm/join_test.go +++ b/cli/command/swarm/join_test.go @@ -1,7 +1,6 @@ package swarm import ( - "bytes" "io/ioutil" "strings" "testing" @@ -49,12 +48,11 @@ func TestSwarmJoinErrors(t *testing.T) { }, } for _, tc := range testCases { - buf := new(bytes.Buffer) cmd := newJoinCommand( - test.NewFakeCliWithOutput(&fakeClient{ + test.NewFakeCli(&fakeClient{ swarmJoinFunc: tc.swarmJoinFunc, infoFunc: tc.infoFunc, - }, buf)) + })) cmd.SetArgs(tc.args) cmd.SetOutput(ioutil.Discard) testutil.ErrorContains(t, cmd.Execute(), tc.expectedError) @@ -91,13 +89,12 @@ func TestSwarmJoin(t *testing.T) { }, } for _, tc := range testCases { - buf := new(bytes.Buffer) - cmd := newJoinCommand( - test.NewFakeCliWithOutput(&fakeClient{ - infoFunc: tc.infoFunc, - }, buf)) + cli := test.NewFakeCli(&fakeClient{ + infoFunc: tc.infoFunc, + }) + cmd := newJoinCommand(cli) cmd.SetArgs([]string{"remote"}) assert.NoError(t, cmd.Execute()) - assert.Equal(t, strings.TrimSpace(buf.String()), tc.expected) + assert.Equal(t, strings.TrimSpace(cli.OutBuffer().String()), tc.expected) } } diff --git a/cli/command/swarm/join_token_test.go b/cli/command/swarm/join_token_test.go index f8619a4bd6..da1b795321 100644 --- a/cli/command/swarm/join_token_test.go +++ b/cli/command/swarm/join_token_test.go @@ -1,7 +1,6 @@ package swarm 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" ) @@ -90,14 +89,13 @@ func TestSwarmJoinTokenErrors(t *testing.T) { }, } for _, tc := range testCases { - buf := new(bytes.Buffer) - cmd := newJoinTokenCommand( - test.NewFakeCliWithOutput(&fakeClient{ - swarmInspectFunc: tc.swarmInspectFunc, - swarmUpdateFunc: tc.swarmUpdateFunc, - infoFunc: tc.infoFunc, - nodeInspectFunc: tc.nodeInspectFunc, - }, buf)) + cli := test.NewFakeCli(&fakeClient{ + swarmInspectFunc: tc.swarmInspectFunc, + swarmUpdateFunc: tc.swarmUpdateFunc, + infoFunc: tc.infoFunc, + nodeInspectFunc: tc.nodeInspectFunc, + }) + cmd := newJoinTokenCommand(cli) cmd.SetArgs(tc.args) for key, value := range tc.flags { cmd.Flags().Set(key, value) @@ -198,20 +196,17 @@ func TestSwarmJoinToken(t *testing.T) { }, } for _, tc := range testCases { - buf := new(bytes.Buffer) - cmd := newJoinTokenCommand( - test.NewFakeCliWithOutput(&fakeClient{ - swarmInspectFunc: tc.swarmInspectFunc, - infoFunc: tc.infoFunc, - nodeInspectFunc: tc.nodeInspectFunc, - }, buf)) + cli := test.NewFakeCli(&fakeClient{ + swarmInspectFunc: tc.swarmInspectFunc, + infoFunc: tc.infoFunc, + nodeInspectFunc: tc.nodeInspectFunc, + }) + cmd := newJoinTokenCommand(cli) cmd.SetArgs(tc.args) for key, value := range tc.flags { cmd.Flags().Set(key, value) } assert.NoError(t, cmd.Execute()) - actual := buf.String() - expected := golden.Get(t, []byte(actual), fmt.Sprintf("jointoken-%s.golden", tc.name)) - testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected)) + golden.Assert(t, cli.OutBuffer().String(), fmt.Sprintf("jointoken-%s.golden", tc.name)) } } diff --git a/cli/command/swarm/testdata/jointoken-manager-rotate.golden b/cli/command/swarm/testdata/jointoken-manager-rotate.golden index b4d0a48f66..4a978e7624 100644 --- a/cli/command/swarm/testdata/jointoken-manager-rotate.golden +++ b/cli/command/swarm/testdata/jointoken-manager-rotate.golden @@ -3,3 +3,4 @@ Successfully rotated manager join token. To add a manager to this swarm, run the following command: docker swarm join --token manager-join-token 127.0.0.1 + diff --git a/cli/command/swarm/testdata/jointoken-manager.golden b/cli/command/swarm/testdata/jointoken-manager.golden index 522b2968fe..7bcb73373c 100644 --- a/cli/command/swarm/testdata/jointoken-manager.golden +++ b/cli/command/swarm/testdata/jointoken-manager.golden @@ -1,3 +1,4 @@ To add a manager to this swarm, run the following command: docker swarm join --token manager-join-token 127.0.0.1 + diff --git a/cli/command/swarm/testdata/jointoken-worker.golden b/cli/command/swarm/testdata/jointoken-worker.golden index 899df703fd..e6c3ab9af8 100644 --- a/cli/command/swarm/testdata/jointoken-worker.golden +++ b/cli/command/swarm/testdata/jointoken-worker.golden @@ -1,3 +1,4 @@ To add a worker to this swarm, run the following command: docker swarm join --token worker-join-token 127.0.0.1 + diff --git a/cli/command/swarm/unlock_key_test.go b/cli/command/swarm/unlock_key_test.go index bb3935ceda..0dc9fb5f08 100644 --- a/cli/command/swarm/unlock_key_test.go +++ b/cli/command/swarm/unlock_key_test.go @@ -12,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" ) @@ -167,8 +167,6 @@ func TestSwarmUnlockKey(t *testing.T) { cmd.Flags().Set(key, value) } assert.NoError(t, cmd.Execute()) - actual := cli.OutBuffer().String() - expected := golden.Get(t, []byte(actual), fmt.Sprintf("unlockkeys-%s.golden", tc.name)) - testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected)) + golden.Assert(t, cli.OutBuffer().String(), fmt.Sprintf("unlockkeys-%s.golden", tc.name)) } } diff --git a/cli/command/swarm/update_test.go b/cli/command/swarm/update_test.go index b3cb02f25f..3c928a2e0d 100644 --- a/cli/command/swarm/update_test.go +++ b/cli/command/swarm/update_test.go @@ -13,7 +13,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" ) @@ -174,8 +174,6 @@ func TestSwarmUpdate(t *testing.T) { } cmd.SetOutput(cli.OutBuffer()) assert.NoError(t, cmd.Execute()) - actual := cli.OutBuffer().String() - expected := golden.Get(t, []byte(actual), fmt.Sprintf("update-%s.golden", tc.name)) - testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected)) + golden.Assert(t, cli.OutBuffer().String(), fmt.Sprintf("update-%s.golden", tc.name)) } }