2016-12-25 16:23:35 -05:00
|
|
|
package node
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"testing"
|
|
|
|
|
2017-08-21 16:30:09 -04:00
|
|
|
"github.com/docker/cli/internal/test"
|
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"
|
2016-12-25 16:23:35 -05:00
|
|
|
// Import builders to get the builder function as package function
|
2017-08-21 16:30:09 -04:00
|
|
|
. "github.com/docker/cli/internal/test/builders"
|
2017-08-21 16:39:35 -04:00
|
|
|
"github.com/docker/cli/internal/test/testutil"
|
Remove pkg/testutil/assert in favor of testify
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>
2017-04-13 18:45:37 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
2016-12-25 16:23:35 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNodeUpdateErrors(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
args []string
|
|
|
|
flags map[string]string
|
|
|
|
nodeInspectFunc func() (swarm.Node, []byte, error)
|
|
|
|
nodeUpdateFunc func(nodeID string, version swarm.Version, node swarm.NodeSpec) error
|
|
|
|
expectedError string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
expectedError: "requires exactly 1 argument",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
args: []string{"node1", "node2"},
|
|
|
|
expectedError: "requires exactly 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",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
args: []string{"nodeID"},
|
|
|
|
nodeInspectFunc: func() (swarm.Node, []byte, error) {
|
|
|
|
return *Node(NodeLabels(map[string]string{
|
|
|
|
"key": "value",
|
|
|
|
})), []byte{}, nil
|
|
|
|
},
|
|
|
|
flags: map[string]string{
|
|
|
|
"label-rm": "notpresent",
|
|
|
|
},
|
|
|
|
expectedError: "key notpresent doesn't exist in node's labels",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
cmd := newUpdateCommand(
|
2017-07-05 14:43:39 -04:00
|
|
|
test.NewFakeCli(&fakeClient{
|
2016-12-25 16:23:35 -05:00
|
|
|
nodeInspectFunc: tc.nodeInspectFunc,
|
|
|
|
nodeUpdateFunc: tc.nodeUpdateFunc,
|
2017-07-05 14:43:39 -04:00
|
|
|
}))
|
2016-12-25 16:23:35 -05:00
|
|
|
cmd.SetArgs(tc.args)
|
|
|
|
for key, value := range tc.flags {
|
|
|
|
cmd.Flags().Set(key, value)
|
|
|
|
}
|
|
|
|
cmd.SetOutput(ioutil.Discard)
|
Remove pkg/testutil/assert in favor of testify
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>
2017-04-13 18:45:37 -04:00
|
|
|
testutil.ErrorContains(t, cmd.Execute(), tc.expectedError)
|
2016-12-25 16:23:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNodeUpdate(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
args []string
|
|
|
|
flags map[string]string
|
|
|
|
nodeInspectFunc func() (swarm.Node, []byte, error)
|
|
|
|
nodeUpdateFunc func(nodeID string, version swarm.Version, node swarm.NodeSpec) error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
args: []string{"nodeID"},
|
|
|
|
flags: map[string]string{
|
|
|
|
"role": "manager",
|
|
|
|
},
|
|
|
|
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 {
|
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
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
args: []string{"nodeID"},
|
|
|
|
flags: map[string]string{
|
|
|
|
"availability": "drain",
|
|
|
|
},
|
|
|
|
nodeInspectFunc: func() (swarm.Node, []byte, error) {
|
|
|
|
return *Node(), []byte{}, nil
|
|
|
|
},
|
|
|
|
nodeUpdateFunc: func(nodeID string, version swarm.Version, node swarm.NodeSpec) error {
|
|
|
|
if node.Availability != swarm.NodeAvailabilityDrain {
|
2017-03-09 13:23:45 -05:00
|
|
|
return errors.Errorf("expected drain availability, got %s", node.Availability)
|
2016-12-25 16:23:35 -05:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
args: []string{"nodeID"},
|
|
|
|
flags: map[string]string{
|
|
|
|
"label-add": "lbl",
|
|
|
|
},
|
|
|
|
nodeInspectFunc: func() (swarm.Node, []byte, error) {
|
|
|
|
return *Node(), []byte{}, nil
|
|
|
|
},
|
|
|
|
nodeUpdateFunc: func(nodeID string, version swarm.Version, node swarm.NodeSpec) error {
|
|
|
|
if _, present := node.Annotations.Labels["lbl"]; !present {
|
2017-03-09 13:23:45 -05:00
|
|
|
return errors.Errorf("expected 'lbl' label, got %v", node.Annotations.Labels)
|
2016-12-25 16:23:35 -05:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
args: []string{"nodeID"},
|
|
|
|
flags: map[string]string{
|
|
|
|
"label-add": "key=value",
|
|
|
|
},
|
|
|
|
nodeInspectFunc: func() (swarm.Node, []byte, error) {
|
|
|
|
return *Node(), []byte{}, nil
|
|
|
|
},
|
|
|
|
nodeUpdateFunc: func(nodeID string, version swarm.Version, node swarm.NodeSpec) error {
|
|
|
|
if value, present := node.Annotations.Labels["key"]; !present || value != "value" {
|
2017-03-09 13:23:45 -05:00
|
|
|
return errors.Errorf("expected 'key' label to be 'value', got %v", node.Annotations.Labels)
|
2016-12-25 16:23:35 -05:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
args: []string{"nodeID"},
|
|
|
|
flags: map[string]string{
|
|
|
|
"label-rm": "key",
|
|
|
|
},
|
|
|
|
nodeInspectFunc: func() (swarm.Node, []byte, error) {
|
|
|
|
return *Node(NodeLabels(map[string]string{
|
|
|
|
"key": "value",
|
|
|
|
})), []byte{}, nil
|
|
|
|
},
|
|
|
|
nodeUpdateFunc: func(nodeID string, version swarm.Version, node swarm.NodeSpec) error {
|
|
|
|
if len(node.Annotations.Labels) > 0 {
|
2017-03-09 13:23:45 -05:00
|
|
|
return errors.Errorf("expected no labels, got %v", node.Annotations.Labels)
|
2016-12-25 16:23:35 -05:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
cmd := newUpdateCommand(
|
2017-07-05 14:43:39 -04:00
|
|
|
test.NewFakeCli(&fakeClient{
|
2016-12-25 16:23:35 -05:00
|
|
|
nodeInspectFunc: tc.nodeInspectFunc,
|
|
|
|
nodeUpdateFunc: tc.nodeUpdateFunc,
|
2017-07-05 14:43:39 -04:00
|
|
|
}))
|
2016-12-25 16:23:35 -05:00
|
|
|
cmd.SetArgs(tc.args)
|
|
|
|
for key, value := range tc.flags {
|
|
|
|
cmd.Flags().Set(key, value)
|
|
|
|
}
|
Remove pkg/testutil/assert in favor of testify
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>
2017-04-13 18:45:37 -04:00
|
|
|
assert.NoError(t, cmd.Execute())
|
2016-12-25 16:23:35 -05:00
|
|
|
}
|
|
|
|
}
|