2016-12-25 16:23:35 -05:00
|
|
|
package swarm
|
|
|
|
|
|
|
|
import (
|
2022-02-25 08:32:11 -05:00
|
|
|
"io"
|
2016-12-25 16:23:35 -05:00
|
|
|
"strings"
|
|
|
|
"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"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2016-12-25 16:23:35 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSwarmJoinErrors(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
args []string
|
|
|
|
swarmJoinFunc func() error
|
|
|
|
infoFunc func() (types.Info, error)
|
|
|
|
expectedError string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "not-enough-args",
|
|
|
|
expectedError: "requires exactly 1 argument",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "too-many-args",
|
|
|
|
args: []string{"remote1", "remote2"},
|
|
|
|
expectedError: "requires exactly 1 argument",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "join-failed",
|
|
|
|
args: []string{"remote"},
|
|
|
|
swarmJoinFunc: func() error {
|
2017-03-09 13:23:45 -05:00
|
|
|
return errors.Errorf("error joining the swarm")
|
2016-12-25 16:23:35 -05:00
|
|
|
},
|
|
|
|
expectedError: "error joining the swarm",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "join-failed-on-init",
|
|
|
|
args: []string{"remote"},
|
|
|
|
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 {
|
|
|
|
cmd := newJoinCommand(
|
2017-08-16 13:48:31 -04:00
|
|
|
test.NewFakeCli(&fakeClient{
|
2016-12-25 16:23:35 -05:00
|
|
|
swarmJoinFunc: tc.swarmJoinFunc,
|
|
|
|
infoFunc: tc.infoFunc,
|
2017-08-16 13:48:31 -04:00
|
|
|
}))
|
2016-12-25 16:23:35 -05:00
|
|
|
cmd.SetArgs(tc.args)
|
2022-02-25 08:32:11 -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 TestSwarmJoin(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
infoFunc func() (types.Info, error)
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "join-as-manager",
|
|
|
|
infoFunc: func() (types.Info, error) {
|
|
|
|
return types.Info{
|
|
|
|
Swarm: swarm.Info{
|
|
|
|
ControlAvailable: true,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
expected: "This node joined a swarm as a manager.",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "join-as-worker",
|
|
|
|
infoFunc: func() (types.Info, error) {
|
|
|
|
return types.Info{
|
|
|
|
Swarm: swarm.Info{
|
|
|
|
ControlAvailable: false,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
expected: "This node joined a swarm as a worker.",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
2017-08-16 13:48:31 -04:00
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
|
|
|
infoFunc: tc.infoFunc,
|
|
|
|
})
|
|
|
|
cmd := newJoinCommand(cli)
|
2016-12-25 16:23:35 -05:00
|
|
|
cmd.SetArgs([]string{"remote"})
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, cmd.Execute())
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal(strings.TrimSpace(cli.OutBuffer().String()), tc.expected))
|
2016-12-25 16:23:35 -05:00
|
|
|
}
|
|
|
|
}
|