| 17 | ) |
| 18 | |
| 19 | func TestSwarmInitErrorOnAPIFailure(t *testing.T) { |
| 20 | testCases := []struct { |
| 21 | name string |
| 22 | flags map[string]string |
| 23 | swarmInitFunc func(client.SwarmInitOptions) (client.SwarmInitResult, error) |
| 24 | swarmInspectFunc func() (client.SwarmInspectResult, error) |
| 25 | swarmGetUnlockKeyFunc func() (client.SwarmGetUnlockKeyResult, error) |
| 26 | nodeInspectFunc func() (client.NodeInspectResult, error) |
| 27 | expectedError string |
| 28 | }{ |
| 29 | { |
| 30 | name: "init-failed", |
| 31 | swarmInitFunc: func(client.SwarmInitOptions) (client.SwarmInitResult, error) { |
| 32 | return client.SwarmInitResult{}, errors.New("error initializing the swarm") |
| 33 | }, |
| 34 | expectedError: "error initializing the swarm", |
| 35 | }, |
| 36 | { |
| 37 | name: "init-failed-with-ip-choice", |
| 38 | swarmInitFunc: func(client.SwarmInitOptions) (client.SwarmInitResult, error) { |
| 39 | return client.SwarmInitResult{}, errors.New("could not choose an IP address to advertise") |
| 40 | }, |
| 41 | expectedError: "could not choose an IP address to advertise - specify one with --advertise-addr", |
| 42 | }, |
| 43 | { |
| 44 | name: "swarm-inspect-after-init-failed", |
| 45 | swarmInspectFunc: func() (client.SwarmInspectResult, error) { |
| 46 | return client.SwarmInspectResult{}, errors.New("error inspecting the swarm") |
| 47 | }, |
| 48 | expectedError: "error inspecting the swarm", |
| 49 | }, |
| 50 | { |
| 51 | name: "node-inspect-after-init-failed", |
| 52 | nodeInspectFunc: func() (client.NodeInspectResult, error) { |
| 53 | return client.NodeInspectResult{}, errors.New("error inspecting the node") |
| 54 | }, |
| 55 | expectedError: "error inspecting the node", |
| 56 | }, |
| 57 | { |
| 58 | name: "swarm-get-unlock-key-after-init-failed", |
| 59 | flags: map[string]string{ |
| 60 | flagAutolock: "true", |
| 61 | }, |
| 62 | swarmGetUnlockKeyFunc: func() (client.SwarmGetUnlockKeyResult, error) { |
| 63 | return client.SwarmGetUnlockKeyResult{}, errors.New("error getting swarm unlock key") |
| 64 | }, |
| 65 | expectedError: "could not fetch unlock key: error getting swarm unlock key", |
| 66 | }, |
| 67 | } |
| 68 | for _, tc := range testCases { |
| 69 | t.Run(tc.name, func(t *testing.T) { |
| 70 | cmd := newInitCommand( |
| 71 | test.NewFakeCli(&fakeClient{ |
| 72 | swarmInitFunc: tc.swarmInitFunc, |
| 73 | swarmInspectFunc: tc.swarmInspectFunc, |
| 74 | swarmGetUnlockKeyFunc: tc.swarmGetUnlockKeyFunc, |
| 75 | nodeInspectFunc: tc.nodeInspectFunc, |
| 76 | })) |