From 0e2bf7420aba3ad77397f0c7f1c3f78bbd2021ae Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Wed, 16 Aug 2017 12:42:19 -0400 Subject: [PATCH] Update node command tests to the new golden Also remove some superfluous tests that are now covered by a strict golden.Assert Signed-off-by: Daniel Nephin --- cli/command/node/inspect_test.go | 21 +++--- cli/command/node/list_test.go | 71 +++++-------------- cli/command/node/promote_test.go | 16 ++--- cli/command/node/ps_test.go | 38 +++++----- cli/command/node/remove_test.go | 9 +-- .../node-inspect-pretty.manager-leader.golden | 15 ++-- .../node-inspect-pretty.manager.golden | 15 ++-- .../node-inspect-pretty.simple.golden | 17 +++-- .../testdata/node-list-format-flag.golden | 2 + .../node-list-format-from-config.golden | 3 + .../node/testdata/node-list-sort.golden | 7 +- .../node/testdata/node-ps.simple.golden | 4 +- .../node/testdata/node-ps.with-errors.golden | 8 +-- 13 files changed, 86 insertions(+), 140 deletions(-) create mode 100644 cli/command/node/testdata/node-list-format-flag.golden create mode 100644 cli/command/node/testdata/node-list-format-from-config.golden diff --git a/cli/command/node/inspect_test.go b/cli/command/node/inspect_test.go index ca88371f8a..b841b0639d 100644 --- a/cli/command/node/inspect_test.go +++ b/cli/command/node/inspect_test.go @@ -1,7 +1,6 @@ package node 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" ) @@ -67,12 +66,11 @@ func TestNodeInspectErrors(t *testing.T) { }, } for _, tc := range testCases { - buf := new(bytes.Buffer) cmd := newInspectCommand( - test.NewFakeCliWithOutput(&fakeClient{ + test.NewFakeCli(&fakeClient{ nodeInspectFunc: tc.nodeInspectFunc, infoFunc: tc.infoFunc, - }, buf)) + })) cmd.SetArgs(tc.args) for key, value := range tc.flags { cmd.Flags().Set(key, value) @@ -109,16 +107,13 @@ func TestNodeInspectPretty(t *testing.T) { }, } for _, tc := range testCases { - buf := new(bytes.Buffer) - cmd := newInspectCommand( - test.NewFakeCliWithOutput(&fakeClient{ - nodeInspectFunc: tc.nodeInspectFunc, - }, buf)) + cli := test.NewFakeCli(&fakeClient{ + nodeInspectFunc: tc.nodeInspectFunc, + }) + cmd := newInspectCommand(cli) cmd.SetArgs([]string{"nodeID"}) cmd.Flags().Set("pretty", "true") assert.NoError(t, cmd.Execute()) - actual := buf.String() - expected := golden.Get(t, []byte(actual), fmt.Sprintf("node-inspect-pretty.%s.golden", tc.name)) - testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected)) + golden.Assert(t, cli.OutBuffer().String(), fmt.Sprintf("node-inspect-pretty.%s.golden", tc.name)) } } diff --git a/cli/command/node/list_test.go b/cli/command/node/list_test.go index f579ebc88a..fee29e284b 100644 --- a/cli/command/node/list_test.go +++ b/cli/command/node/list_test.go @@ -1,7 +1,6 @@ package node import ( - "bytes" "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" // Import builders to get the builder function as package function . "github.com/docker/cli/cli/internal/test/builders" @@ -58,9 +56,9 @@ func TestNodeList(t *testing.T) { cli := test.NewFakeCli(&fakeClient{ nodeListFunc: func() ([]swarm.Node, error) { return []swarm.Node{ - *Node(NodeID("nodeID1"), Hostname("nodeHostname1"), Manager(Leader())), - *Node(NodeID("nodeID2"), Hostname("nodeHostname2"), Manager()), - *Node(NodeID("nodeID3"), Hostname("nodeHostname3")), + *Node(NodeID("nodeID1"), Hostname("node-2-foo"), Manager(Leader())), + *Node(NodeID("nodeID2"), Hostname("node-10-foo"), Manager()), + *Node(NodeID("nodeID3"), Hostname("node-1-foo")), }, nil }, infoFunc: func() (types.Info, error) { @@ -74,39 +72,25 @@ func TestNodeList(t *testing.T) { cmd := newListCommand(cli) assert.NoError(t, cmd.Execute()) - out := cli.OutBuffer().String() - assert.Contains(t, out, `nodeID1 * nodeHostname1 Ready Active Leader`) - assert.Contains(t, out, `nodeID2 nodeHostname2 Ready Active Reachable`) - assert.Contains(t, out, `nodeID3 nodeHostname3 Ready Active`) + golden.Assert(t, cli.OutBuffer().String(), "node-list-sort.golden") } func TestNodeListQuietShouldOnlyPrintIDs(t *testing.T) { - buf := new(bytes.Buffer) - cli := test.NewFakeCliWithOutput(&fakeClient{ + cli := test.NewFakeCli(&fakeClient{ nodeListFunc: func() ([]swarm.Node, error) { return []swarm.Node{ - *Node(), + *Node(NodeID("nodeID1")), }, nil }, - }, buf) + }) cmd := newListCommand(cli) cmd.Flags().Set("quiet", "true") assert.NoError(t, cmd.Execute()) - assert.Contains(t, buf.String(), "nodeID") + assert.Equal(t, cli.OutBuffer().String(), "nodeID1\n") } -// Test case for #24090 -func TestNodeListContainsHostname(t *testing.T) { - buf := new(bytes.Buffer) - cli := test.NewFakeCliWithOutput(&fakeClient{}, buf) - cmd := newListCommand(cli) - assert.NoError(t, cmd.Execute()) - assert.Contains(t, buf.String(), "HOSTNAME") -} - -func TestNodeListDefaultFormat(t *testing.T) { - buf := new(bytes.Buffer) - cli := test.NewFakeCliWithOutput(&fakeClient{ +func TestNodeListDefaultFormatFromConfig(t *testing.T) { + cli := test.NewFakeCli(&fakeClient{ nodeListFunc: func() ([]swarm.Node, error) { return []swarm.Node{ *Node(NodeID("nodeID1"), Hostname("nodeHostname1"), Manager(Leader())), @@ -121,20 +105,17 @@ func TestNodeListDefaultFormat(t *testing.T) { }, }, nil }, - }, buf) + }) cli.SetConfigFile(&configfile.ConfigFile{ NodesFormat: "{{.ID}}: {{.Hostname}} {{.Status}}/{{.ManagerStatus}}", }) cmd := newListCommand(cli) assert.NoError(t, cmd.Execute()) - assert.Contains(t, buf.String(), `nodeID1: nodeHostname1 Ready/Leader`) - assert.Contains(t, buf.String(), `nodeID2: nodeHostname2 Ready/Reachable`) - assert.Contains(t, buf.String(), `nodeID3: nodeHostname3 Ready`) + golden.Assert(t, cli.OutBuffer().String(), "node-list-format-from-config.golden") } func TestNodeListFormat(t *testing.T) { - buf := new(bytes.Buffer) - cli := test.NewFakeCliWithOutput(&fakeClient{ + cli := test.NewFakeCli(&fakeClient{ nodeListFunc: func() ([]swarm.Node, error) { return []swarm.Node{ *Node(NodeID("nodeID1"), Hostname("nodeHostname1"), Manager(Leader())), @@ -148,32 +129,12 @@ func TestNodeListFormat(t *testing.T) { }, }, nil }, - }, buf) + }) cli.SetConfigFile(&configfile.ConfigFile{ NodesFormat: "{{.ID}}: {{.Hostname}} {{.Status}}/{{.ManagerStatus}}", }) cmd := newListCommand(cli) cmd.Flags().Set("format", "{{.Hostname}}: {{.ManagerStatus}}") assert.NoError(t, cmd.Execute()) - assert.Contains(t, buf.String(), `nodeHostname1: Leader`) - assert.Contains(t, buf.String(), `nodeHostname2: Reachable`) -} - -func TestNodeListOrder(t *testing.T) { - cli := test.NewFakeCli(&fakeClient{ - nodeListFunc: func() ([]swarm.Node, error) { - return []swarm.Node{ - *Node(Hostname("node-2-foo"), Manager(Leader())), - *Node(Hostname("node-10-foo"), Manager()), - *Node(Hostname("node-1-foo")), - }, nil - - }, - }) - cmd := newListCommand(cli) - cmd.Flags().Set("format", "{{.Hostname}}: {{.ManagerStatus}}") - assert.NoError(t, cmd.Execute()) - actual := cli.OutBuffer().String() - expected := golden.Get(t, []byte(actual), "node-list-sort.golden") - testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected)) + golden.Assert(t, cli.OutBuffer().String(), "node-list-format-flag.golden") } diff --git a/cli/command/node/promote_test.go b/cli/command/node/promote_test.go index 95e067eb15..2399b2ee8c 100644 --- a/cli/command/node/promote_test.go +++ b/cli/command/node/promote_test.go @@ -1,7 +1,6 @@ package node import ( - "bytes" "io/ioutil" "testing" @@ -40,12 +39,11 @@ func TestNodePromoteErrors(t *testing.T) { }, } for _, tc := range testCases { - buf := new(bytes.Buffer) cmd := newPromoteCommand( - test.NewFakeCliWithOutput(&fakeClient{ + test.NewFakeCli(&fakeClient{ nodeInspectFunc: tc.nodeInspectFunc, nodeUpdateFunc: tc.nodeUpdateFunc, - }, buf)) + })) cmd.SetArgs(tc.args) cmd.SetOutput(ioutil.Discard) testutil.ErrorContains(t, cmd.Execute(), tc.expectedError) @@ -53,9 +51,8 @@ func TestNodePromoteErrors(t *testing.T) { } func TestNodePromoteNoChange(t *testing.T) { - buf := new(bytes.Buffer) cmd := newPromoteCommand( - test.NewFakeCliWithOutput(&fakeClient{ + test.NewFakeCli(&fakeClient{ nodeInspectFunc: func() (swarm.Node, []byte, error) { return *Node(Manager()), []byte{}, nil }, @@ -65,15 +62,14 @@ func TestNodePromoteNoChange(t *testing.T) { } return nil }, - }, buf)) + })) cmd.SetArgs([]string{"nodeID"}) assert.NoError(t, cmd.Execute()) } func TestNodePromoteMultipleNode(t *testing.T) { - buf := new(bytes.Buffer) cmd := newPromoteCommand( - test.NewFakeCliWithOutput(&fakeClient{ + test.NewFakeCli(&fakeClient{ nodeInspectFunc: func() (swarm.Node, []byte, error) { return *Node(), []byte{}, nil }, @@ -83,7 +79,7 @@ func TestNodePromoteMultipleNode(t *testing.T) { } return nil }, - }, buf)) + })) cmd.SetArgs([]string{"nodeID1", "nodeID2"}) assert.NoError(t, cmd.Execute()) } diff --git a/cli/command/node/ps_test.go b/cli/command/node/ps_test.go index d3ef6f30a2..8733e5e0f4 100644 --- a/cli/command/node/ps_test.go +++ b/cli/command/node/ps_test.go @@ -1,7 +1,6 @@ package node import ( - "bytes" "fmt" "io/ioutil" "testing" @@ -13,8 +12,7 @@ import ( "github.com/pkg/errors" // 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" ) @@ -50,14 +48,13 @@ func TestNodePsErrors(t *testing.T) { }, } for _, tc := range testCases { - buf := new(bytes.Buffer) - cmd := newPsCommand( - test.NewFakeCliWithOutput(&fakeClient{ - infoFunc: tc.infoFunc, - nodeInspectFunc: tc.nodeInspectFunc, - taskInspectFunc: tc.taskInspectFunc, - taskListFunc: tc.taskListFunc, - }, buf)) + cli := test.NewFakeCli(&fakeClient{ + infoFunc: tc.infoFunc, + nodeInspectFunc: tc.nodeInspectFunc, + taskInspectFunc: tc.taskInspectFunc, + taskListFunc: tc.taskListFunc, + }) + cmd := newPsCommand(cli) cmd.SetArgs(tc.args) for key, value := range tc.flags { cmd.Flags().Set(key, value) @@ -114,21 +111,18 @@ func TestNodePs(t *testing.T) { }, } for _, tc := range testCases { - buf := new(bytes.Buffer) - cmd := newPsCommand( - test.NewFakeCliWithOutput(&fakeClient{ - infoFunc: tc.infoFunc, - nodeInspectFunc: tc.nodeInspectFunc, - taskInspectFunc: tc.taskInspectFunc, - taskListFunc: tc.taskListFunc, - }, buf)) + cli := test.NewFakeCli(&fakeClient{ + infoFunc: tc.infoFunc, + nodeInspectFunc: tc.nodeInspectFunc, + taskInspectFunc: tc.taskInspectFunc, + taskListFunc: tc.taskListFunc, + }) + cmd := newPsCommand(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("node-ps.%s.golden", tc.name)) - testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected)) + golden.Assert(t, cli.OutBuffer().String(), fmt.Sprintf("node-ps.%s.golden", tc.name)) } } diff --git a/cli/command/node/remove_test.go b/cli/command/node/remove_test.go index 81daa2f025..e0f3f238e6 100644 --- a/cli/command/node/remove_test.go +++ b/cli/command/node/remove_test.go @@ -1,7 +1,6 @@ package node import ( - "bytes" "io/ioutil" "testing" @@ -29,11 +28,10 @@ func TestNodeRemoveErrors(t *testing.T) { }, } for _, tc := range testCases { - buf := new(bytes.Buffer) cmd := newRemoveCommand( - test.NewFakeCliWithOutput(&fakeClient{ + test.NewFakeCli(&fakeClient{ nodeRemoveFunc: tc.nodeRemoveFunc, - }, buf)) + })) cmd.SetArgs(tc.args) cmd.SetOutput(ioutil.Discard) testutil.ErrorContains(t, cmd.Execute(), tc.expectedError) @@ -41,8 +39,7 @@ func TestNodeRemoveErrors(t *testing.T) { } func TestNodeRemoveMultiple(t *testing.T) { - buf := new(bytes.Buffer) - cmd := newRemoveCommand(test.NewFakeCliWithOutput(&fakeClient{}, buf)) + cmd := newRemoveCommand(test.NewFakeCli(&fakeClient{})) cmd.SetArgs([]string{"nodeID1", "nodeID2"}) assert.NoError(t, cmd.Execute()) } diff --git a/cli/command/node/testdata/node-inspect-pretty.manager-leader.golden b/cli/command/node/testdata/node-inspect-pretty.manager-leader.golden index 461fc46ea2..5cd95c5b91 100644 --- a/cli/command/node/testdata/node-inspect-pretty.manager-leader.golden +++ b/cli/command/node/testdata/node-inspect-pretty.manager-leader.golden @@ -1,10 +1,10 @@ ID: nodeID Name: defaultNodeName -Hostname: defaultNodeHostname -Joined at: 2009-11-10 23:00:00 +0000 utc +Hostname: defaultNodeHostname +Joined at: 2009-11-10 23:00:00 +0000 utc Status: State: Ready - Availability: Active + Availability: Active Address: 127.0.0.1 Manager Status: Address: 127.0.0.1 @@ -15,11 +15,10 @@ Platform: Architecture: x86_64 Resources: CPUs: 0 - Memory: 20 MiB + Memory: 20MiB Plugins: - Network: bridge, overlay - Volume: local + Network: bridge, overlay + Volume: local Engine Version: 1.13.0 Engine Labels: - - engine = label - + - engine=label diff --git a/cli/command/node/testdata/node-inspect-pretty.manager.golden b/cli/command/node/testdata/node-inspect-pretty.manager.golden index 2c660188d5..a63718293c 100644 --- a/cli/command/node/testdata/node-inspect-pretty.manager.golden +++ b/cli/command/node/testdata/node-inspect-pretty.manager.golden @@ -1,10 +1,10 @@ ID: nodeID Name: defaultNodeName -Hostname: defaultNodeHostname -Joined at: 2009-11-10 23:00:00 +0000 utc +Hostname: defaultNodeHostname +Joined at: 2009-11-10 23:00:00 +0000 utc Status: State: Ready - Availability: Active + Availability: Active Address: 127.0.0.1 Manager Status: Address: 127.0.0.1 @@ -15,11 +15,10 @@ Platform: Architecture: x86_64 Resources: CPUs: 0 - Memory: 20 MiB + Memory: 20MiB Plugins: - Network: bridge, overlay - Volume: local + Network: bridge, overlay + Volume: local Engine Version: 1.13.0 Engine Labels: - - engine = label - + - engine=label diff --git a/cli/command/node/testdata/node-inspect-pretty.simple.golden b/cli/command/node/testdata/node-inspect-pretty.simple.golden index e63bc12596..8aaf90899e 100644 --- a/cli/command/node/testdata/node-inspect-pretty.simple.golden +++ b/cli/command/node/testdata/node-inspect-pretty.simple.golden @@ -1,23 +1,22 @@ ID: nodeID Name: defaultNodeName Labels: - - lbl1 = value1 -Hostname: defaultNodeHostname -Joined at: 2009-11-10 23:00:00 +0000 utc + - lbl1=value1 +Hostname: defaultNodeHostname +Joined at: 2009-11-10 23:00:00 +0000 utc Status: State: Ready - Availability: Active + Availability: Active Address: 127.0.0.1 Platform: Operating System: linux Architecture: x86_64 Resources: CPUs: 0 - Memory: 20 MiB + Memory: 20MiB Plugins: - Network: bridge, overlay - Volume: local + Network: bridge, overlay + Volume: local Engine Version: 1.13.0 Engine Labels: - - engine = label - + - engine=label diff --git a/cli/command/node/testdata/node-list-format-flag.golden b/cli/command/node/testdata/node-list-format-flag.golden new file mode 100644 index 0000000000..c898df13e6 --- /dev/null +++ b/cli/command/node/testdata/node-list-format-flag.golden @@ -0,0 +1,2 @@ +nodeHostname1: Leader +nodeHostname2: Reachable diff --git a/cli/command/node/testdata/node-list-format-from-config.golden b/cli/command/node/testdata/node-list-format-from-config.golden new file mode 100644 index 0000000000..91beb4a29b --- /dev/null +++ b/cli/command/node/testdata/node-list-format-from-config.golden @@ -0,0 +1,3 @@ +nodeID1: nodeHostname1 Ready/Leader +nodeID2: nodeHostname2 Ready/Reachable +nodeID3: nodeHostname3 Ready/ diff --git a/cli/command/node/testdata/node-list-sort.golden b/cli/command/node/testdata/node-list-sort.golden index e2f2811994..ad8c34a813 100644 --- a/cli/command/node/testdata/node-list-sort.golden +++ b/cli/command/node/testdata/node-list-sort.golden @@ -1,3 +1,4 @@ -node-1-foo: -node-2-foo: Leader -node-10-foo: Reachable +ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS +nodeID3 node-1-foo Ready Active +nodeID1 * node-2-foo Ready Active Leader +nodeID2 node-10-foo Ready Active Reachable diff --git a/cli/command/node/testdata/node-ps.simple.golden b/cli/command/node/testdata/node-ps.simple.golden index f9555d8792..b1818b96b4 100644 --- a/cli/command/node/testdata/node-ps.simple.golden +++ b/cli/command/node/testdata/node-ps.simple.golden @@ -1,2 +1,2 @@ -ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS -taskID rl02d5gwz6chzu7il5fhtb8be.1 myimage:mytag defaultNodeName Ready Ready 2 hours ago *:80->80/tcp +ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS +taskID rl02d5gwz6chzu7il5fhtb8be.1 myimage:mytag defaultNodeName Ready Ready 2 hours ago *:80->80/tcp diff --git a/cli/command/node/testdata/node-ps.with-errors.golden b/cli/command/node/testdata/node-ps.with-errors.golden index 273b30fa11..99e34931a6 100644 --- a/cli/command/node/testdata/node-ps.with-errors.golden +++ b/cli/command/node/testdata/node-ps.with-errors.golden @@ -1,4 +1,4 @@ -ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS -taskID1 failure.1 myimage:mytag defaultNodeName Ready Ready 2 hours ago "a task error" -taskID2 \_ failure.1 myimage:mytag defaultNodeName Ready Ready 3 hours ago "a task error" -taskID3 \_ failure.1 myimage:mytag defaultNodeName Ready Ready 4 hours ago "a task error" +ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS +taskID1 failure.1 myimage:mytag defaultNodeName Ready Ready 2 hours ago "a task error" +taskID2 \_ failure.1 myimage:mytag defaultNodeName Ready Ready 3 hours ago "a task error" +taskID3 \_ failure.1 myimage:mytag defaultNodeName Ready Ready 4 hours ago "a task error"