I noticed that we're using a homegrown package for assertions. The functions are extremely similar to testify, but with enough slight differences to be confusing (for example, Equal takes its arguments in a different order). We already vendor testify, and it's used in a few places by tests. I also found some problems with pkg/testutil/assert. For example, the NotNil function seems to be broken. It checks the argument against "nil", which only works for an interface. If you pass in a nil map or slice, the equality check will fail. In the interest of avoiding NIH, I'm proposing replacing pkg/testutil/assert with testify. The test code looks almost the same, but we avoid the confusion of having two similar but slightly different assertion packages, and having to maintain our own package instead of using a commonly-used one. In the process, I found a few places where the tests should halt if an assertion fails, so I've made those cases (that I noticed) use "require" instead of "assert", and I've vendored the "require" package from testify alongside the already-present "assert" package. Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com> Upstream-commit: 6052f2b3969feadb01662d8e2f30337d9c7f61af Component: engine
90 lines
2.5 KiB
Go
90 lines
2.5 KiB
Go
package node
|
|
|
|
import (
|
|
"bytes"
|
|
"io/ioutil"
|
|
"testing"
|
|
|
|
"github.com/docker/docker/api/types/swarm"
|
|
"github.com/docker/docker/cli/internal/test"
|
|
"github.com/pkg/errors"
|
|
// Import builders to get the builder function as package function
|
|
. "github.com/docker/docker/cli/internal/test/builders"
|
|
"github.com/docker/docker/pkg/testutil"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestNodeDemoteErrors(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{}, errors.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 errors.Errorf("error updating the node")
|
|
},
|
|
expectedError: "error updating the node",
|
|
},
|
|
}
|
|
for _, tc := range testCases {
|
|
buf := new(bytes.Buffer)
|
|
cmd := newDemoteCommand(
|
|
test.NewFakeCli(&fakeClient{
|
|
nodeInspectFunc: tc.nodeInspectFunc,
|
|
nodeUpdateFunc: tc.nodeUpdateFunc,
|
|
}, buf))
|
|
cmd.SetArgs(tc.args)
|
|
cmd.SetOutput(ioutil.Discard)
|
|
testutil.ErrorContains(t, cmd.Execute(), tc.expectedError)
|
|
}
|
|
}
|
|
|
|
func TestNodeDemoteNoChange(t *testing.T) {
|
|
buf := new(bytes.Buffer)
|
|
cmd := newDemoteCommand(
|
|
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.NodeRoleWorker {
|
|
return errors.Errorf("expected role worker, got %s", node.Role)
|
|
}
|
|
return nil
|
|
},
|
|
}, buf))
|
|
cmd.SetArgs([]string{"nodeID"})
|
|
assert.NoError(t, cmd.Execute())
|
|
}
|
|
|
|
func TestNodeDemoteMultipleNode(t *testing.T) {
|
|
buf := new(bytes.Buffer)
|
|
cmd := newDemoteCommand(
|
|
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.NodeRoleWorker {
|
|
return errors.Errorf("expected role worker, got %s", node.Role)
|
|
}
|
|
return nil
|
|
},
|
|
}, buf))
|
|
cmd.SetArgs([]string{"nodeID1", "nodeID2"})
|
|
assert.NoError(t, cmd.Execute())
|
|
}
|