github.com/flavio/docker@v0.1.3-0.20170117145210-f63d1a6eec47/cli/command/swarm/init_test.go (about) 1 package swarm 2 3 import ( 4 "bytes" 5 "fmt" 6 "io/ioutil" 7 "testing" 8 9 "github.com/docker/docker/api/types" 10 "github.com/docker/docker/api/types/swarm" 11 "github.com/docker/docker/cli/internal/test" 12 "github.com/docker/docker/pkg/testutil/assert" 13 "github.com/docker/docker/pkg/testutil/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 "", fmt.Errorf("error initializing the swarm") 30 }, 31 expectedError: "error initializing the swarm", 32 }, 33 { 34 name: "init-faild-with-ip-choice", 35 swarmInitFunc: func() (string, error) { 36 return "", fmt.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{}, fmt.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{}, fmt.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{}, fmt.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 buf := new(bytes.Buffer) 67 cmd := newInitCommand( 68 test.NewFakeCli(&fakeClient{ 69 swarmInitFunc: tc.swarmInitFunc, 70 swarmInspectFunc: tc.swarmInspectFunc, 71 swarmGetUnlockKeyFunc: tc.swarmGetUnlockKeyFunc, 72 nodeInspectFunc: tc.nodeInspectFunc, 73 }, buf)) 74 for key, value := range tc.flags { 75 cmd.Flags().Set(key, value) 76 } 77 cmd.SetOutput(ioutil.Discard) 78 assert.Error(t, cmd.Execute(), tc.expectedError) 79 } 80 } 81 82 func TestSwarmInit(t *testing.T) { 83 testCases := []struct { 84 name string 85 flags map[string]string 86 swarmInitFunc func() (string, error) 87 swarmInspectFunc func() (swarm.Swarm, error) 88 swarmGetUnlockKeyFunc func() (types.SwarmUnlockKeyResponse, error) 89 nodeInspectFunc func() (swarm.Node, []byte, error) 90 }{ 91 { 92 name: "init", 93 swarmInitFunc: func() (string, error) { 94 return "nodeID", nil 95 }, 96 }, 97 { 98 name: "init-autolock", 99 flags: map[string]string{ 100 flagAutolock: "true", 101 }, 102 swarmInitFunc: func() (string, error) { 103 return "nodeID", nil 104 }, 105 swarmGetUnlockKeyFunc: func() (types.SwarmUnlockKeyResponse, error) { 106 return types.SwarmUnlockKeyResponse{ 107 UnlockKey: "unlock-key", 108 }, nil 109 }, 110 }, 111 } 112 for _, tc := range testCases { 113 buf := new(bytes.Buffer) 114 cmd := newInitCommand( 115 test.NewFakeCli(&fakeClient{ 116 swarmInitFunc: tc.swarmInitFunc, 117 swarmInspectFunc: tc.swarmInspectFunc, 118 swarmGetUnlockKeyFunc: tc.swarmGetUnlockKeyFunc, 119 nodeInspectFunc: tc.nodeInspectFunc, 120 }, buf)) 121 for key, value := range tc.flags { 122 cmd.Flags().Set(key, value) 123 } 124 assert.NilError(t, cmd.Execute()) 125 actual := buf.String() 126 expected := golden.Get(t, []byte(actual), fmt.Sprintf("init-%s.golden", tc.name)) 127 assert.EqualNormalizedString(t, assert.RemoveSpace, actual, string(expected)) 128 } 129 }