2016-12-25 16:23:35 -05:00
|
|
|
package node
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io/ioutil"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/swarm"
|
|
|
|
"github.com/docker/docker/cli/internal/test"
|
2017-03-09 13:23:45 -05:00
|
|
|
"github.com/pkg/errors"
|
2016-12-25 16:23:35 -05:00
|
|
|
// Import builders to get the builder function as package function
|
|
|
|
. "github.com/docker/docker/cli/internal/test/builders"
|
|
|
|
"github.com/docker/docker/pkg/testutil/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestNodeListErrorOnAPIFailure(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
nodeListFunc func() ([]swarm.Node, error)
|
|
|
|
infoFunc func() (types.Info, error)
|
|
|
|
expectedError string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
nodeListFunc: func() ([]swarm.Node, error) {
|
2017-03-09 13:23:45 -05:00
|
|
|
return []swarm.Node{}, errors.Errorf("error listing nodes")
|
2016-12-25 16:23:35 -05:00
|
|
|
},
|
|
|
|
expectedError: "error listing nodes",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
nodeListFunc: func() ([]swarm.Node, error) {
|
|
|
|
return []swarm.Node{
|
|
|
|
{
|
|
|
|
ID: "nodeID",
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
infoFunc: func() (types.Info, error) {
|
2017-03-09 13:23:45 -05:00
|
|
|
return types.Info{}, errors.Errorf("error asking for node info")
|
2016-12-25 16:23:35 -05:00
|
|
|
},
|
|
|
|
expectedError: "error asking for node info",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
cmd := newListCommand(
|
|
|
|
test.NewFakeCli(&fakeClient{
|
|
|
|
nodeListFunc: tc.nodeListFunc,
|
|
|
|
infoFunc: tc.infoFunc,
|
|
|
|
}, buf))
|
|
|
|
cmd.SetOutput(ioutil.Discard)
|
|
|
|
assert.Error(t, cmd.Execute(), tc.expectedError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNodeList(t *testing.T) {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
cmd := newListCommand(
|
|
|
|
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")),
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
infoFunc: func() (types.Info, error) {
|
|
|
|
return types.Info{
|
|
|
|
Swarm: swarm.Info{
|
|
|
|
NodeID: "nodeID1",
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
}, buf))
|
|
|
|
assert.NilError(t, cmd.Execute())
|
|
|
|
assert.Contains(t, buf.String(), `nodeID1 * nodeHostname1 Ready Active Leader`)
|
|
|
|
assert.Contains(t, buf.String(), `nodeID2 nodeHostname2 Ready Active Reachable`)
|
|
|
|
assert.Contains(t, buf.String(), `nodeID3 nodeHostname3 Ready Active`)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNodeListQuietShouldOnlyPrintIDs(t *testing.T) {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
cmd := newListCommand(
|
|
|
|
test.NewFakeCli(&fakeClient{
|
|
|
|
nodeListFunc: func() ([]swarm.Node, error) {
|
|
|
|
return []swarm.Node{
|
|
|
|
*Node(),
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
}, buf))
|
|
|
|
cmd.Flags().Set("quiet", "true")
|
|
|
|
assert.NilError(t, cmd.Execute())
|
|
|
|
assert.Contains(t, buf.String(), "nodeID")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test case for #24090
|
|
|
|
func TestNodeListContainsHostname(t *testing.T) {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
cmd := newListCommand(test.NewFakeCli(&fakeClient{}, buf))
|
|
|
|
assert.NilError(t, cmd.Execute())
|
|
|
|
assert.Contains(t, buf.String(), "HOSTNAME")
|
|
|
|
}
|