moby/cli/command/node/promote_test.go
Vincent Demeester f151c297eb
Add some unit tests to the node and swarm cli code
Start work on adding unit tests to our cli code in order to have to
write less costly integration test.

Signed-off-by: Vincent Demeester <vincent@sbr.pm>
2017-01-09 18:30:15 +01:00

88 lines
2.5 KiB
Go

package node
import (
"bytes"
"fmt"
"io/ioutil"
"testing"
"github.com/docker/docker/api/types/swarm"
"github.com/docker/docker/cli/internal/test"
// 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 TestNodePromoteErrors(t *testing.T) {
testCases := []struct {
args []string
nodeInspectFunc func() (swarm.Node, []byte, error)
nodeUpdateFunc func(nodeID string, version swarm.Version, node swarm.NodeSpec) error
expectedError string
}{
{
expectedError: "requires at least 1 argument",
},
{
args: []string{"nodeID"},
nodeInspectFunc: func() (swarm.Node, []byte, error) {
return swarm.Node{}, []byte{}, fmt.Errorf("error inspecting the node")
},
expectedError: "error inspecting the node",
},
{
args: []string{"nodeID"},
nodeUpdateFunc: func(nodeID string, version swarm.Version, node swarm.NodeSpec) error {
return fmt.Errorf("error updating the node")
},
expectedError: "error updating the node",
},
}
for _, tc := range testCases {
buf := new(bytes.Buffer)
cmd := newPromoteCommand(
test.NewFakeCli(&fakeClient{
nodeInspectFunc: tc.nodeInspectFunc,
nodeUpdateFunc: tc.nodeUpdateFunc,
}, buf))
cmd.SetArgs(tc.args)
cmd.SetOutput(ioutil.Discard)
assert.Error(t, cmd.Execute(), tc.expectedError)
}
}
func TestNodePromoteNoChange(t *testing.T) {
buf := new(bytes.Buffer)
cmd := newPromoteCommand(
test.NewFakeCli(&fakeClient{
nodeInspectFunc: func() (swarm.Node, []byte, error) {
return *Node(Manager()), []byte{}, nil
},
nodeUpdateFunc: func(nodeID string, version swarm.Version, node swarm.NodeSpec) error {
if node.Role != swarm.NodeRoleManager {
return fmt.Errorf("expected role manager, got %s", node.Role)
}
return nil
},
}, buf))
cmd.SetArgs([]string{"nodeID"})
assert.NilError(t, cmd.Execute())
}
func TestNodePromoteMultipleNode(t *testing.T) {
buf := new(bytes.Buffer)
cmd := newPromoteCommand(
test.NewFakeCli(&fakeClient{
nodeInspectFunc: func() (swarm.Node, []byte, error) {
return *Node(), []byte{}, nil
},
nodeUpdateFunc: func(nodeID string, version swarm.Version, node swarm.NodeSpec) error {
if node.Role != swarm.NodeRoleManager {
return fmt.Errorf("expected role manager, got %s", node.Role)
}
return nil
},
}, buf))
cmd.SetArgs([]string{"nodeID1", "nodeID2"})
assert.NilError(t, cmd.Execute())
}