github.com/justincormack/cli@v0.0.0-20201215022714-831ebeae9675/cli/command/swarm/init_test.go (about) 1 package swarm 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "testing" 7 8 "github.com/docker/cli/internal/test" 9 "github.com/docker/docker/api/types" 10 "github.com/docker/docker/api/types/swarm" 11 "github.com/pkg/errors" 12 "gotest.tools/v3/assert" 13 "gotest.tools/v3/golden" 14 ) 15 16 func TestSwarmInitErrorOnAPIFailure(t *testing.T) { 17 testCases := []struct { 18 name string 19 flags map[string]string 20 swarmInitFunc func() (string, error) 21 swarmInspectFunc func() (swarm.Swarm, error) 22 swarmGetUnlockKeyFunc func() (types.SwarmUnlockKeyResponse, error) 23 nodeInspectFunc func() (swarm.Node, []byte, error) 24 expectedError string 25 }{ 26 { 27 name: "init-failed", 28 swarmInitFunc: func() (string, error) { 29 return "", errors.Errorf("error initializing the swarm") 30 }, 31 expectedError: "error initializing the swarm", 32 }, 33 { 34 name: "init-failed-with-ip-choice", 35 swarmInitFunc: func() (string, error) { 36 return "", errors.Errorf("could not choose an IP address to advertise") 37 }, 38 expectedError: "could not choose an IP address to advertise - specify one with --advertise-addr", 39 }, 40 { 41 name: "swarm-inspect-after-init-failed", 42 swarmInspectFunc: func() (swarm.Swarm, error) { 43 return swarm.Swarm{}, errors.Errorf("error inspecting the swarm") 44 }, 45 expectedError: "error inspecting the swarm", 46 }, 47 { 48 name: "node-inspect-after-init-failed", 49 nodeInspectFunc: func() (swarm.Node, []byte, error) { 50 return swarm.Node{}, []byte{}, errors.Errorf("error inspecting the node") 51 }, 52 expectedError: "error inspecting the node", 53 }, 54 { 55 name: "swarm-get-unlock-key-after-init-failed", 56 flags: map[string]string{ 57 flagAutolock: "true", 58 }, 59 swarmGetUnlockKeyFunc: func() (types.SwarmUnlockKeyResponse, error) { 60 return types.SwarmUnlockKeyResponse{}, errors.Errorf("error getting swarm unlock key") 61 }, 62 expectedError: "could not fetch unlock key: error getting swarm unlock key", 63 }, 64 } 65 for _, tc := range testCases { 66 cmd := newInitCommand( 67 test.NewFakeCli(&fakeClient{ 68 swarmInitFunc: tc.swarmInitFunc, 69 swarmInspectFunc: tc.swarmInspectFunc, 70 swarmGetUnlockKeyFunc: tc.swarmGetUnlockKeyFunc, 71 nodeInspectFunc: tc.nodeInspectFunc, 72 })) 73 for key, value := range tc.flags { 74 cmd.Flags().Set(key, value) 75 } 76 cmd.SetOut(ioutil.Discard) 77 assert.Error(t, cmd.Execute(), tc.expectedError) 78 } 79 } 80 81 func TestSwarmInit(t *testing.T) { 82 testCases := []struct { 83 name string 84 flags map[string]string 85 swarmInitFunc func() (string, error) 86 swarmInspectFunc func() (swarm.Swarm, error) 87 swarmGetUnlockKeyFunc func() (types.SwarmUnlockKeyResponse, error) 88 nodeInspectFunc func() (swarm.Node, []byte, error) 89 }{ 90 { 91 name: "init", 92 swarmInitFunc: func() (string, error) { 93 return "nodeID", nil 94 }, 95 }, 96 { 97 name: "init-autolock", 98 flags: map[string]string{ 99 flagAutolock: "true", 100 }, 101 swarmInitFunc: func() (string, error) { 102 return "nodeID", nil 103 }, 104 swarmGetUnlockKeyFunc: func() (types.SwarmUnlockKeyResponse, error) { 105 return types.SwarmUnlockKeyResponse{ 106 UnlockKey: "unlock-key", 107 }, nil 108 }, 109 }, 110 } 111 for _, tc := range testCases { 112 cli := test.NewFakeCli(&fakeClient{ 113 swarmInitFunc: tc.swarmInitFunc, 114 swarmInspectFunc: tc.swarmInspectFunc, 115 swarmGetUnlockKeyFunc: tc.swarmGetUnlockKeyFunc, 116 nodeInspectFunc: tc.nodeInspectFunc, 117 }) 118 cmd := newInitCommand(cli) 119 for key, value := range tc.flags { 120 cmd.Flags().Set(key, value) 121 } 122 assert.NilError(t, cmd.Execute()) 123 golden.Assert(t, cli.OutBuffer().String(), fmt.Sprintf("init-%s.golden", tc.name)) 124 } 125 }