Some tests had to be skipped as there's some issues to address, and some of the result-types cannot be mocked / stubbed. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
90 lines
2.7 KiB
Go
90 lines
2.7 KiB
Go
package node
|
|
|
|
import (
|
|
"errors"
|
|
"io"
|
|
"testing"
|
|
|
|
"github.com/docker/cli/internal/test"
|
|
"github.com/docker/cli/internal/test/builders"
|
|
"github.com/moby/moby/api/types/swarm"
|
|
"github.com/moby/moby/client"
|
|
"gotest.tools/v3/assert"
|
|
)
|
|
|
|
func TestNodeDemoteErrors(t *testing.T) {
|
|
testCases := []struct {
|
|
args []string
|
|
nodeInspectFunc func() (client.NodeInspectResult, error)
|
|
nodeUpdateFunc func(nodeID string, options client.NodeUpdateOptions) (client.NodeUpdateResult, error)
|
|
expectedError string
|
|
}{
|
|
{
|
|
expectedError: "requires at least 1 argument",
|
|
},
|
|
{
|
|
args: []string{"nodeID"},
|
|
nodeInspectFunc: func() (client.NodeInspectResult, error) {
|
|
return client.NodeInspectResult{}, errors.New("error inspecting the node")
|
|
},
|
|
expectedError: "error inspecting the node",
|
|
},
|
|
{
|
|
args: []string{"nodeID"},
|
|
nodeUpdateFunc: func(nodeID string, options client.NodeUpdateOptions) (client.NodeUpdateResult, error) {
|
|
return client.NodeUpdateResult{}, errors.New("error updating the node")
|
|
},
|
|
expectedError: "error updating the node",
|
|
},
|
|
}
|
|
for _, tc := range testCases {
|
|
cmd := newDemoteCommand(
|
|
test.NewFakeCli(&fakeClient{
|
|
nodeInspectFunc: tc.nodeInspectFunc,
|
|
nodeUpdateFunc: tc.nodeUpdateFunc,
|
|
}))
|
|
cmd.SetArgs(tc.args)
|
|
cmd.SetOut(io.Discard)
|
|
cmd.SetErr(io.Discard)
|
|
assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
|
|
}
|
|
}
|
|
|
|
func TestNodeDemoteNoChange(t *testing.T) {
|
|
cmd := newDemoteCommand(
|
|
test.NewFakeCli(&fakeClient{
|
|
nodeInspectFunc: func() (client.NodeInspectResult, error) {
|
|
return client.NodeInspectResult{
|
|
Node: *builders.Node(),
|
|
}, nil
|
|
},
|
|
nodeUpdateFunc: func(nodeID string, options client.NodeUpdateOptions) (client.NodeUpdateResult, error) {
|
|
if options.Node.Role != swarm.NodeRoleWorker {
|
|
return client.NodeUpdateResult{}, errors.New("expected role worker, got " + string(options.Node.Role))
|
|
}
|
|
return client.NodeUpdateResult{}, nil
|
|
},
|
|
}))
|
|
cmd.SetArgs([]string{"nodeID"})
|
|
assert.NilError(t, cmd.Execute())
|
|
}
|
|
|
|
func TestNodeDemoteMultipleNode(t *testing.T) {
|
|
cmd := newDemoteCommand(
|
|
test.NewFakeCli(&fakeClient{
|
|
nodeInspectFunc: func() (client.NodeInspectResult, error) {
|
|
return client.NodeInspectResult{
|
|
Node: *builders.Node(builders.Manager()),
|
|
}, nil
|
|
},
|
|
nodeUpdateFunc: func(nodeID string, options client.NodeUpdateOptions) (client.NodeUpdateResult, error) {
|
|
if options.Node.Role != swarm.NodeRoleWorker {
|
|
return client.NodeUpdateResult{}, errors.New("expected role worker, got " + string(options.Node.Role))
|
|
}
|
|
return client.NodeUpdateResult{}, nil
|
|
},
|
|
}))
|
|
cmd.SetArgs([]string{"nodeID1", "nodeID2"})
|
|
assert.NilError(t, cmd.Execute())
|
|
}
|