2016-12-25 16:23:35 -05:00
|
|
|
package swarm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2017-08-21 16:30:09 -04:00
|
|
|
"github.com/docker/cli/internal/test"
|
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 TestSwarmLeaveErrors(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
args []string
|
|
|
|
swarmLeaveFunc func() error
|
|
|
|
expectedError string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "too-many-args",
|
|
|
|
args: []string{"foo"},
|
2017-08-12 12:25:38 -04:00
|
|
|
expectedError: "accepts no arguments",
|
2016-12-25 16:23:35 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "leave-failed",
|
|
|
|
swarmLeaveFunc: func() error {
|
2017-03-09 13:23:45 -05:00
|
|
|
return errors.Errorf("error leaving the swarm")
|
2016-12-25 16:23:35 -05:00
|
|
|
},
|
|
|
|
expectedError: "error leaving the swarm",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
cmd := newLeaveCommand(
|
2017-07-05 14:43:39 -04:00
|
|
|
test.NewFakeCli(&fakeClient{
|
2016-12-25 16:23:35 -05:00
|
|
|
swarmLeaveFunc: tc.swarmLeaveFunc,
|
2017-07-05 14:43:39 -04:00
|
|
|
}))
|
2016-12-25 16:23:35 -05:00
|
|
|
cmd.SetArgs(tc.args)
|
2020-05-07 08:25:59 -04:00
|
|
|
cmd.SetOut(ioutil.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 TestSwarmLeave(t *testing.T) {
|
2017-07-05 14:43:39 -04:00
|
|
|
cli := test.NewFakeCli(&fakeClient{})
|
|
|
|
cmd := newLeaveCommand(cli)
|
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("Node left the swarm.", strings.TrimSpace(cli.OutBuffer().String())))
|
2016-12-25 16:23:35 -05:00
|
|
|
}
|