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"
|
|
|
|
|
2019-01-28 08:30:31 -05:00
|
|
|
"github.com/docker/cli/cli/streams"
|
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"
|
2023-07-14 17:42:40 -04:00
|
|
|
"github.com/docker/docker/api/types/system"
|
2017-03-09 13:23:45 -05:00
|
|
|
"github.com/pkg/errors"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/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
|
2023-07-14 17:42:40 -04:00
|
|
|
infoFunc func() (system.Info, error)
|
2016-12-25 16:23:35 -05:00
|
|
|
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",
|
2023-07-14 17:42:40 -04:00
|
|
|
infoFunc: func() (system.Info, error) {
|
|
|
|
return system.Info{
|
2016-12-25 16:23:35 -05:00
|
|
|
Swarm: swarm.Info{
|
|
|
|
LocalNodeState: swarm.LocalNodeStateInactive,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
expectedError: "This node is not part of a swarm",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "is-not-locked",
|
2023-07-14 17:42:40 -04:00
|
|
|
infoFunc: func() (system.Info, error) {
|
|
|
|
return system.Info{
|
2016-12-25 16:23:35 -05:00
|
|
|
Swarm: swarm.Info{
|
|
|
|
LocalNodeState: swarm.LocalNodeStateActive,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
expectedError: "Error: swarm is not locked",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "unlockrequest-failed",
|
2023-07-14 17:42:40 -04:00
|
|
|
infoFunc: func() (system.Info, error) {
|
|
|
|
return system.Info{
|
2016-12-25 16:23:35 -05:00
|
|
|
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)
|
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 TestSwarmUnlock(t *testing.T) {
|
|
|
|
input := "unlockKey"
|
2017-07-05 14:43:39 -04:00
|
|
|
dockerCli := test.NewFakeCli(&fakeClient{
|
2023-07-14 17:42:40 -04:00
|
|
|
infoFunc: func() (system.Info, error) {
|
|
|
|
return system.Info{
|
2016-12-25 16:23:35 -05:00
|
|
|
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
|
|
|
})
|
2022-02-25 08:32:11 -05:00
|
|
|
dockerCli.SetIn(streams.NewIn(io.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
|
|
|
}
|