2016-12-25 16:23:35 -05:00
|
|
|
package swarm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"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"
|
|
|
|
"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"
|
2017-08-16 13:48:31 -04:00
|
|
|
"github.com/gotestyourself/gotestyourself/golden"
|
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 TestSwarmJoinTokenErrors(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
args []string
|
|
|
|
flags map[string]string
|
|
|
|
infoFunc func() (types.Info, error)
|
|
|
|
swarmInspectFunc func() (swarm.Swarm, error)
|
|
|
|
swarmUpdateFunc func(swarm swarm.Spec, flags swarm.UpdateFlags) error
|
|
|
|
nodeInspectFunc func() (swarm.Node, []byte, error)
|
|
|
|
expectedError string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "not-enough-args",
|
|
|
|
expectedError: "requires exactly 1 argument",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "too-many-args",
|
|
|
|
args: []string{"worker", "manager"},
|
|
|
|
expectedError: "requires exactly 1 argument",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid-args",
|
|
|
|
args: []string{"foo"},
|
|
|
|
expectedError: "unknown role foo",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "swarm-inspect-failed",
|
|
|
|
args: []string{"worker"},
|
|
|
|
swarmInspectFunc: func() (swarm.Swarm, error) {
|
2017-03-09 13:23:45 -05:00
|
|
|
return swarm.Swarm{}, errors.Errorf("error inspecting the swarm")
|
2016-12-25 16:23:35 -05:00
|
|
|
},
|
|
|
|
expectedError: "error inspecting the swarm",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "swarm-inspect-rotate-failed",
|
|
|
|
args: []string{"worker"},
|
|
|
|
flags: map[string]string{
|
|
|
|
flagRotate: "true",
|
|
|
|
},
|
|
|
|
swarmInspectFunc: func() (swarm.Swarm, error) {
|
2017-03-09 13:23:45 -05:00
|
|
|
return swarm.Swarm{}, errors.Errorf("error inspecting the swarm")
|
2016-12-25 16:23:35 -05:00
|
|
|
},
|
|
|
|
expectedError: "error inspecting the swarm",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "swarm-update-failed",
|
|
|
|
args: []string{"worker"},
|
|
|
|
flags: map[string]string{
|
|
|
|
flagRotate: "true",
|
|
|
|
},
|
|
|
|
swarmUpdateFunc: func(swarm swarm.Spec, flags swarm.UpdateFlags) error {
|
2017-03-09 13:23:45 -05:00
|
|
|
return errors.Errorf("error updating the swarm")
|
2016-12-25 16:23:35 -05:00
|
|
|
},
|
|
|
|
expectedError: "error updating the swarm",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "node-inspect-failed",
|
|
|
|
args: []string{"worker"},
|
|
|
|
nodeInspectFunc: func() (swarm.Node, []byte, error) {
|
2017-03-09 13:23:45 -05:00
|
|
|
return swarm.Node{}, []byte{}, errors.Errorf("error inspecting node")
|
2016-12-25 16:23:35 -05:00
|
|
|
},
|
|
|
|
expectedError: "error inspecting node",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "info-failed",
|
|
|
|
args: []string{"worker"},
|
|
|
|
infoFunc: func() (types.Info, error) {
|
2017-03-09 13:23:45 -05:00
|
|
|
return types.Info{}, errors.Errorf("error asking for node info")
|
2016-12-25 16:23:35 -05:00
|
|
|
},
|
|
|
|
expectedError: "error asking for node info",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
2017-08-16 13:48:31 -04:00
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
|
|
|
swarmInspectFunc: tc.swarmInspectFunc,
|
|
|
|
swarmUpdateFunc: tc.swarmUpdateFunc,
|
|
|
|
infoFunc: tc.infoFunc,
|
|
|
|
nodeInspectFunc: tc.nodeInspectFunc,
|
|
|
|
})
|
|
|
|
cmd := newJoinTokenCommand(cli)
|
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 TestSwarmJoinToken(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
args []string
|
|
|
|
flags map[string]string
|
|
|
|
infoFunc func() (types.Info, error)
|
|
|
|
swarmInspectFunc func() (swarm.Swarm, error)
|
|
|
|
nodeInspectFunc func() (swarm.Node, []byte, error)
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "worker",
|
|
|
|
args: []string{"worker"},
|
|
|
|
infoFunc: func() (types.Info, error) {
|
|
|
|
return types.Info{
|
|
|
|
Swarm: swarm.Info{
|
|
|
|
NodeID: "nodeID",
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
nodeInspectFunc: func() (swarm.Node, []byte, error) {
|
|
|
|
return *Node(Manager()), []byte{}, nil
|
|
|
|
},
|
|
|
|
swarmInspectFunc: func() (swarm.Swarm, error) {
|
|
|
|
return *Swarm(), nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "manager",
|
|
|
|
args: []string{"manager"},
|
|
|
|
infoFunc: func() (types.Info, error) {
|
|
|
|
return types.Info{
|
|
|
|
Swarm: swarm.Info{
|
|
|
|
NodeID: "nodeID",
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
nodeInspectFunc: func() (swarm.Node, []byte, error) {
|
|
|
|
return *Node(Manager()), []byte{}, nil
|
|
|
|
},
|
|
|
|
swarmInspectFunc: func() (swarm.Swarm, error) {
|
|
|
|
return *Swarm(), nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "manager-rotate",
|
|
|
|
args: []string{"manager"},
|
|
|
|
flags: map[string]string{
|
|
|
|
flagRotate: "true",
|
|
|
|
},
|
|
|
|
infoFunc: func() (types.Info, error) {
|
|
|
|
return types.Info{
|
|
|
|
Swarm: swarm.Info{
|
|
|
|
NodeID: "nodeID",
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
nodeInspectFunc: func() (swarm.Node, []byte, error) {
|
|
|
|
return *Node(Manager()), []byte{}, nil
|
|
|
|
},
|
|
|
|
swarmInspectFunc: func() (swarm.Swarm, error) {
|
|
|
|
return *Swarm(), nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "worker-quiet",
|
|
|
|
args: []string{"worker"},
|
|
|
|
flags: map[string]string{
|
|
|
|
flagQuiet: "true",
|
|
|
|
},
|
|
|
|
nodeInspectFunc: func() (swarm.Node, []byte, error) {
|
|
|
|
return *Node(Manager()), []byte{}, nil
|
|
|
|
},
|
|
|
|
swarmInspectFunc: func() (swarm.Swarm, error) {
|
|
|
|
return *Swarm(), nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "manager-quiet",
|
|
|
|
args: []string{"manager"},
|
|
|
|
flags: map[string]string{
|
|
|
|
flagQuiet: "true",
|
|
|
|
},
|
|
|
|
nodeInspectFunc: func() (swarm.Node, []byte, error) {
|
|
|
|
return *Node(Manager()), []byte{}, nil
|
|
|
|
},
|
|
|
|
swarmInspectFunc: func() (swarm.Swarm, error) {
|
|
|
|
return *Swarm(), nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
2017-08-16 13:48:31 -04:00
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
|
|
|
swarmInspectFunc: tc.swarmInspectFunc,
|
|
|
|
infoFunc: tc.infoFunc,
|
|
|
|
nodeInspectFunc: tc.nodeInspectFunc,
|
|
|
|
})
|
|
|
|
cmd := newJoinTokenCommand(cli)
|
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())
|
2017-08-16 13:48:31 -04:00
|
|
|
golden.Assert(t, cli.OutBuffer().String(), fmt.Sprintf("jointoken-%s.golden", tc.name))
|
2016-12-25 16:23:35 -05:00
|
|
|
}
|
|
|
|
}
|