2016-12-25 16:23:35 -05:00
|
|
|
package swarm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2017-03-30 20:21:14 -04:00
|
|
|
"github.com/docker/cli/cli/command"
|
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"
|
2018-06-08 12:24:26 -04:00
|
|
|
"gotest.tools/assert"
|
2016-12-25 16:23:35 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSwarmUnlockErrors(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
args []string
|
|
|
|
swarmUnlockFunc func(req swarm.UnlockRequest) error
|
|
|
|
infoFunc func() (types.Info, 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: "is-not-part-of-a-swarm",
|
|
|
|
infoFunc: func() (types.Info, error) {
|
|
|
|
return types.Info{
|
|
|
|
Swarm: swarm.Info{
|
|
|
|
LocalNodeState: swarm.LocalNodeStateInactive,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
expectedError: "This node is not part of a swarm",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "is-not-locked",
|
|
|
|
infoFunc: func() (types.Info, error) {
|
|
|
|
return types.Info{
|
|
|
|
Swarm: swarm.Info{
|
|
|
|
LocalNodeState: swarm.LocalNodeStateActive,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
expectedError: "Error: swarm is not locked",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "unlockrequest-failed",
|
|
|
|
infoFunc: func() (types.Info, error) {
|
|
|
|
return types.Info{
|
|
|
|
Swarm: swarm.Info{
|
|
|
|
LocalNodeState: swarm.LocalNodeStateLocked,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
swarmUnlockFunc: func(req swarm.UnlockRequest) error {
|
2017-03-09 13:23:45 -05:00
|
|
|
return errors.Errorf("error unlocking the swarm")
|
2016-12-25 16:23:35 -05:00
|
|
|
},
|
|
|
|
expectedError: "error unlocking the swarm",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
cmd := newUnlockCommand(
|
2017-07-05 14:43:39 -04:00
|
|
|
test.NewFakeCli(&fakeClient{
|
2016-12-25 16:23:35 -05:00
|
|
|
infoFunc: tc.infoFunc,
|
|
|
|
swarmUnlockFunc: tc.swarmUnlockFunc,
|
2017-07-05 14:43:39 -04:00
|
|
|
}))
|
2016-12-25 16:23:35 -05:00
|
|
|
cmd.SetArgs(tc.args)
|
|
|
|
cmd.SetOutput(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 TestSwarmUnlock(t *testing.T) {
|
|
|
|
input := "unlockKey"
|
2017-07-05 14:43:39 -04:00
|
|
|
dockerCli := test.NewFakeCli(&fakeClient{
|
2016-12-25 16:23:35 -05:00
|
|
|
infoFunc: func() (types.Info, error) {
|
|
|
|
return types.Info{
|
|
|
|
Swarm: swarm.Info{
|
|
|
|
LocalNodeState: swarm.LocalNodeStateLocked,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
swarmUnlockFunc: func(req swarm.UnlockRequest) error {
|
|
|
|
if req.UnlockKey != input {
|
2017-03-09 13:23:45 -05:00
|
|
|
return errors.Errorf("Invalid unlock key")
|
2016-12-25 16:23:35 -05:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
2017-07-05 14:43:39 -04:00
|
|
|
})
|
2017-03-30 20:21:14 -04:00
|
|
|
dockerCli.SetIn(command.NewInStream(ioutil.NopCloser(strings.NewReader(input))))
|
2016-12-25 16:23:35 -05:00
|
|
|
cmd := newUnlockCommand(dockerCli)
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, cmd.Execute())
|
2016-12-25 16:23:35 -05:00
|
|
|
}
|