2016-12-25 16:23:35 -05:00
|
|
|
package node
|
|
|
|
|
|
|
|
import (
|
2022-02-25 07:20:55 -05:00
|
|
|
"io"
|
2016-12-25 16:23:35 -05:00
|
|
|
"testing"
|
|
|
|
|
2017-08-21 16:30:09 -04:00
|
|
|
"github.com/docker/cli/internal/test"
|
2023-10-23 08:51:01 -04:00
|
|
|
"github.com/docker/cli/internal/test/builders"
|
2016-12-25 16:23:35 -05:00
|
|
|
"github.com/docker/docker/api/types/swarm"
|
2017-03-09 13:23:45 -05:00
|
|
|
"github.com/pkg/errors"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
2016-12-25 16:23:35 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
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) {
|
2017-03-09 13:23:45 -05:00
|
|
|
return swarm.Node{}, []byte{}, errors.Errorf("error inspecting the node")
|
2016-12-25 16:23:35 -05:00
|
|
|
},
|
|
|
|
expectedError: "error inspecting the node",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
args: []string{"nodeID"},
|
|
|
|
nodeUpdateFunc: func(nodeID string, version swarm.Version, node swarm.NodeSpec) error {
|
2017-03-09 13:23:45 -05:00
|
|
|
return errors.Errorf("error updating the node")
|
2016-12-25 16:23:35 -05:00
|
|
|
},
|
|
|
|
expectedError: "error updating the node",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
cmd := newPromoteCommand(
|
2017-08-16 12:42:19 -04:00
|
|
|
test.NewFakeCli(&fakeClient{
|
2016-12-25 16:23:35 -05:00
|
|
|
nodeInspectFunc: tc.nodeInspectFunc,
|
|
|
|
nodeUpdateFunc: tc.nodeUpdateFunc,
|
2017-08-16 12:42:19 -04:00
|
|
|
}))
|
2016-12-25 16:23:35 -05:00
|
|
|
cmd.SetArgs(tc.args)
|
2022-02-25 07:20:55 -05:00
|
|
|
cmd.SetOut(io.Discard)
|
2018-03-06 14:03:47 -05:00
|
|
|
assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
|
2016-12-25 16:23:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNodePromoteNoChange(t *testing.T) {
|
|
|
|
cmd := newPromoteCommand(
|
2017-08-16 12:42:19 -04:00
|
|
|
test.NewFakeCli(&fakeClient{
|
2016-12-25 16:23:35 -05:00
|
|
|
nodeInspectFunc: func() (swarm.Node, []byte, error) {
|
2023-10-23 08:51:01 -04:00
|
|
|
return *builders.Node(builders.Manager()), []byte{}, nil
|
2016-12-25 16:23:35 -05:00
|
|
|
},
|
|
|
|
nodeUpdateFunc: func(nodeID string, version swarm.Version, node swarm.NodeSpec) error {
|
|
|
|
if node.Role != swarm.NodeRoleManager {
|
2017-03-09 13:23:45 -05:00
|
|
|
return errors.Errorf("expected role manager, got %s", node.Role)
|
2016-12-25 16:23:35 -05:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
2017-08-16 12:42:19 -04:00
|
|
|
}))
|
2016-12-25 16:23:35 -05:00
|
|
|
cmd.SetArgs([]string{"nodeID"})
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, cmd.Execute())
|
2016-12-25 16:23:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNodePromoteMultipleNode(t *testing.T) {
|
|
|
|
cmd := newPromoteCommand(
|
2017-08-16 12:42:19 -04:00
|
|
|
test.NewFakeCli(&fakeClient{
|
2016-12-25 16:23:35 -05:00
|
|
|
nodeInspectFunc: func() (swarm.Node, []byte, error) {
|
2023-10-23 08:51:01 -04:00
|
|
|
return *builders.Node(), []byte{}, nil
|
2016-12-25 16:23:35 -05:00
|
|
|
},
|
|
|
|
nodeUpdateFunc: func(nodeID string, version swarm.Version, node swarm.NodeSpec) error {
|
|
|
|
if node.Role != swarm.NodeRoleManager {
|
2017-03-09 13:23:45 -05:00
|
|
|
return errors.Errorf("expected role manager, got %s", node.Role)
|
2016-12-25 16:23:35 -05:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
2017-08-16 12:42:19 -04:00
|
|
|
}))
|
2016-12-25 16:23:35 -05:00
|
|
|
cmd.SetArgs([]string{"nodeID1", "nodeID2"})
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, cmd.Execute())
|
2016-12-25 16:23:35 -05:00
|
|
|
}
|