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"
|
2018-06-08 12:24:26 -04:00
|
|
|
"gotest.tools/assert"
|
|
|
|
"gotest.tools/golden"
|
2016-12-25 16:23:35 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSwarmInitErrorOnAPIFailure(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
flags map[string]string
|
|
|
|
swarmInitFunc func() (string, error)
|
|
|
|
swarmInspectFunc func() (swarm.Swarm, error)
|
|
|
|
swarmGetUnlockKeyFunc func() (types.SwarmUnlockKeyResponse, error)
|
|
|
|
nodeInspectFunc func() (swarm.Node, []byte, error)
|
|
|
|
expectedError string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "init-failed",
|
|
|
|
swarmInitFunc: func() (string, error) {
|
2017-03-09 13:23:45 -05:00
|
|
|
return "", errors.Errorf("error initializing the swarm")
|
2016-12-25 16:23:35 -05:00
|
|
|
},
|
|
|
|
expectedError: "error initializing the swarm",
|
|
|
|
},
|
|
|
|
{
|
2017-02-16 10:56:53 -05:00
|
|
|
name: "init-failed-with-ip-choice",
|
2016-12-25 16:23:35 -05:00
|
|
|
swarmInitFunc: func() (string, error) {
|
2017-03-09 13:23:45 -05:00
|
|
|
return "", errors.Errorf("could not choose an IP address to advertise")
|
2016-12-25 16:23:35 -05:00
|
|
|
},
|
|
|
|
expectedError: "could not choose an IP address to advertise - specify one with --advertise-addr",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "swarm-inspect-after-init-failed",
|
|
|
|
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: "node-inspect-after-init-failed",
|
|
|
|
nodeInspectFunc: func() (swarm.Node, []byte, error) {
|
2017-03-09 13:23:45 -05:00
|
|
|
return swarm.Node{}, []byte{}, errors.Errorf("error inspecting the node")
|
2016-12-25 16:23:35 -05:00
|
|
|
},
|
|
|
|
expectedError: "error inspecting the node",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "swarm-get-unlock-key-after-init-failed",
|
|
|
|
flags: map[string]string{
|
|
|
|
flagAutolock: "true",
|
|
|
|
},
|
|
|
|
swarmGetUnlockKeyFunc: func() (types.SwarmUnlockKeyResponse, error) {
|
2017-03-09 13:23:45 -05:00
|
|
|
return types.SwarmUnlockKeyResponse{}, errors.Errorf("error getting swarm unlock key")
|
2016-12-25 16:23:35 -05:00
|
|
|
},
|
|
|
|
expectedError: "could not fetch unlock key: error getting swarm unlock key",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
cmd := newInitCommand(
|
2017-08-16 13:48:31 -04:00
|
|
|
test.NewFakeCli(&fakeClient{
|
2016-12-25 16:23:35 -05:00
|
|
|
swarmInitFunc: tc.swarmInitFunc,
|
|
|
|
swarmInspectFunc: tc.swarmInspectFunc,
|
|
|
|
swarmGetUnlockKeyFunc: tc.swarmGetUnlockKeyFunc,
|
|
|
|
nodeInspectFunc: tc.nodeInspectFunc,
|
2017-08-16 13:48:31 -04:00
|
|
|
}))
|
2016-12-25 16:23:35 -05:00
|
|
|
for key, value := range tc.flags {
|
|
|
|
cmd.Flags().Set(key, value)
|
|
|
|
}
|
|
|
|
cmd.SetOutput(ioutil.Discard)
|
2018-03-06 15:54:24 -05:00
|
|
|
assert.Error(t, cmd.Execute(), tc.expectedError)
|
2016-12-25 16:23:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSwarmInit(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
flags map[string]string
|
|
|
|
swarmInitFunc func() (string, error)
|
|
|
|
swarmInspectFunc func() (swarm.Swarm, error)
|
|
|
|
swarmGetUnlockKeyFunc func() (types.SwarmUnlockKeyResponse, error)
|
|
|
|
nodeInspectFunc func() (swarm.Node, []byte, error)
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "init",
|
|
|
|
swarmInitFunc: func() (string, error) {
|
|
|
|
return "nodeID", nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "init-autolock",
|
|
|
|
flags: map[string]string{
|
|
|
|
flagAutolock: "true",
|
|
|
|
},
|
|
|
|
swarmInitFunc: func() (string, error) {
|
|
|
|
return "nodeID", nil
|
|
|
|
},
|
|
|
|
swarmGetUnlockKeyFunc: func() (types.SwarmUnlockKeyResponse, error) {
|
|
|
|
return types.SwarmUnlockKeyResponse{
|
|
|
|
UnlockKey: "unlock-key",
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
2017-08-16 13:48:31 -04:00
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
|
|
|
swarmInitFunc: tc.swarmInitFunc,
|
|
|
|
swarmInspectFunc: tc.swarmInspectFunc,
|
|
|
|
swarmGetUnlockKeyFunc: tc.swarmGetUnlockKeyFunc,
|
|
|
|
nodeInspectFunc: tc.nodeInspectFunc,
|
|
|
|
})
|
|
|
|
cmd := newInitCommand(cli)
|
2016-12-25 16:23:35 -05:00
|
|
|
for key, value := range tc.flags {
|
|
|
|
cmd.Flags().Set(key, value)
|
|
|
|
}
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, cmd.Execute())
|
2017-08-16 13:48:31 -04:00
|
|
|
golden.Assert(t, cli.OutBuffer().String(), fmt.Sprintf("init-%s.golden", tc.name))
|
2016-12-25 16:23:35 -05:00
|
|
|
}
|
|
|
|
}
|