github.com/justincormack/cli@v0.0.0-20201215022714-831ebeae9675/cli/command/node/list_test.go (about) 1 package node 2 3 import ( 4 "io/ioutil" 5 "testing" 6 7 "github.com/docker/cli/cli/config/configfile" 8 "github.com/docker/cli/internal/test" 9 . "github.com/docker/cli/internal/test/builders" // Import builders to get the builder function as package function 10 "github.com/docker/docker/api/types" 11 "github.com/docker/docker/api/types/swarm" 12 "github.com/pkg/errors" 13 "gotest.tools/v3/assert" 14 is "gotest.tools/v3/assert/cmp" 15 "gotest.tools/v3/golden" 16 ) 17 18 func TestNodeListErrorOnAPIFailure(t *testing.T) { 19 testCases := []struct { 20 nodeListFunc func() ([]swarm.Node, error) 21 infoFunc func() (types.Info, error) 22 expectedError string 23 }{ 24 { 25 nodeListFunc: func() ([]swarm.Node, error) { 26 return []swarm.Node{}, errors.Errorf("error listing nodes") 27 }, 28 expectedError: "error listing nodes", 29 }, 30 { 31 nodeListFunc: func() ([]swarm.Node, error) { 32 return []swarm.Node{ 33 { 34 ID: "nodeID", 35 }, 36 }, nil 37 }, 38 infoFunc: func() (types.Info, error) { 39 return types.Info{}, errors.Errorf("error asking for node info") 40 }, 41 expectedError: "error asking for node info", 42 }, 43 } 44 for _, tc := range testCases { 45 cli := test.NewFakeCli(&fakeClient{ 46 nodeListFunc: tc.nodeListFunc, 47 infoFunc: tc.infoFunc, 48 }) 49 cmd := newListCommand(cli) 50 cmd.SetOut(ioutil.Discard) 51 assert.Error(t, cmd.Execute(), tc.expectedError) 52 } 53 } 54 55 func TestNodeList(t *testing.T) { 56 cli := test.NewFakeCli(&fakeClient{ 57 nodeListFunc: func() ([]swarm.Node, error) { 58 return []swarm.Node{ 59 *Node(NodeID("nodeID1"), Hostname("node-2-foo"), Manager(Leader()), EngineVersion(".")), 60 *Node(NodeID("nodeID2"), Hostname("node-10-foo"), Manager(), EngineVersion("18.03.0-ce")), 61 *Node(NodeID("nodeID3"), Hostname("node-1-foo")), 62 }, nil 63 }, 64 infoFunc: func() (types.Info, error) { 65 return types.Info{ 66 Swarm: swarm.Info{ 67 NodeID: "nodeID1", 68 }, 69 }, nil 70 }, 71 }) 72 73 cmd := newListCommand(cli) 74 assert.NilError(t, cmd.Execute()) 75 golden.Assert(t, cli.OutBuffer().String(), "node-list-sort.golden") 76 } 77 78 func TestNodeListQuietShouldOnlyPrintIDs(t *testing.T) { 79 cli := test.NewFakeCli(&fakeClient{ 80 nodeListFunc: func() ([]swarm.Node, error) { 81 return []swarm.Node{ 82 *Node(NodeID("nodeID1")), 83 }, nil 84 }, 85 }) 86 cmd := newListCommand(cli) 87 cmd.Flags().Set("quiet", "true") 88 assert.NilError(t, cmd.Execute()) 89 assert.Check(t, is.Equal(cli.OutBuffer().String(), "nodeID1\n")) 90 } 91 92 func TestNodeListDefaultFormatFromConfig(t *testing.T) { 93 cli := test.NewFakeCli(&fakeClient{ 94 nodeListFunc: func() ([]swarm.Node, error) { 95 return []swarm.Node{ 96 *Node(NodeID("nodeID1"), Hostname("nodeHostname1"), Manager(Leader())), 97 *Node(NodeID("nodeID2"), Hostname("nodeHostname2"), Manager()), 98 *Node(NodeID("nodeID3"), Hostname("nodeHostname3")), 99 }, nil 100 }, 101 infoFunc: func() (types.Info, error) { 102 return types.Info{ 103 Swarm: swarm.Info{ 104 NodeID: "nodeID1", 105 }, 106 }, nil 107 }, 108 }) 109 cli.SetConfigFile(&configfile.ConfigFile{ 110 NodesFormat: "{{.ID}}: {{.Hostname}} {{.Status}}/{{.ManagerStatus}}", 111 }) 112 cmd := newListCommand(cli) 113 assert.NilError(t, cmd.Execute()) 114 golden.Assert(t, cli.OutBuffer().String(), "node-list-format-from-config.golden") 115 } 116 117 func TestNodeListFormat(t *testing.T) { 118 cli := test.NewFakeCli(&fakeClient{ 119 nodeListFunc: func() ([]swarm.Node, error) { 120 return []swarm.Node{ 121 *Node(NodeID("nodeID1"), Hostname("nodeHostname1"), Manager(Leader())), 122 *Node(NodeID("nodeID2"), Hostname("nodeHostname2"), Manager()), 123 }, nil 124 }, 125 infoFunc: func() (types.Info, error) { 126 return types.Info{ 127 Swarm: swarm.Info{ 128 NodeID: "nodeID1", 129 }, 130 }, nil 131 }, 132 }) 133 cli.SetConfigFile(&configfile.ConfigFile{ 134 NodesFormat: "{{.ID}}: {{.Hostname}} {{.Status}}/{{.ManagerStatus}}", 135 }) 136 cmd := newListCommand(cli) 137 cmd.Flags().Set("format", "{{.Hostname}}: {{.ManagerStatus}}") 138 assert.NilError(t, cmd.Execute()) 139 golden.Assert(t, cli.OutBuffer().String(), "node-list-format-flag.golden") 140 }