| 14 | ) |
| 15 | |
| 16 | func TestNodeUpdateErrors(t *testing.T) { |
| 17 | testCases := []struct { |
| 18 | args []string |
| 19 | flags map[string]string |
| 20 | nodeInspectFunc func() (client.NodeInspectResult, error) |
| 21 | nodeUpdateFunc func(nodeID string, options client.NodeUpdateOptions) (client.NodeUpdateResult, error) |
| 22 | expectedError string |
| 23 | }{ |
| 24 | { |
| 25 | expectedError: "requires 1 argument", |
| 26 | }, |
| 27 | { |
| 28 | args: []string{"node1", "node2"}, |
| 29 | expectedError: "requires 1 argument", |
| 30 | }, |
| 31 | { |
| 32 | args: []string{"nodeID"}, |
| 33 | nodeInspectFunc: func() (client.NodeInspectResult, error) { |
| 34 | return client.NodeInspectResult{}, errors.New("error inspecting the node") |
| 35 | }, |
| 36 | expectedError: "error inspecting the node", |
| 37 | }, |
| 38 | { |
| 39 | args: []string{"nodeID"}, |
| 40 | nodeUpdateFunc: func(nodeID string, options client.NodeUpdateOptions) (client.NodeUpdateResult, error) { |
| 41 | return client.NodeUpdateResult{}, errors.New("error updating the node") |
| 42 | }, |
| 43 | expectedError: "error updating the node", |
| 44 | }, |
| 45 | { |
| 46 | args: []string{"nodeID"}, |
| 47 | nodeInspectFunc: func() (client.NodeInspectResult, error) { |
| 48 | return client.NodeInspectResult{ |
| 49 | Node: *builders.Node(builders.NodeLabels(map[string]string{ |
| 50 | "key": "value", |
| 51 | })), |
| 52 | }, nil |
| 53 | }, |
| 54 | flags: map[string]string{ |
| 55 | "label-rm": "not-present", |
| 56 | }, |
| 57 | expectedError: "key not-present doesn't exist in node's labels", |
| 58 | }, |
| 59 | } |
| 60 | for _, tc := range testCases { |
| 61 | cmd := newUpdateCommand( |
| 62 | test.NewFakeCli(&fakeClient{ |
| 63 | nodeInspectFunc: tc.nodeInspectFunc, |
| 64 | nodeUpdateFunc: tc.nodeUpdateFunc, |
| 65 | })) |
| 66 | cmd.SetArgs(tc.args) |
| 67 | for key, value := range tc.flags { |
| 68 | assert.Check(t, cmd.Flags().Set(key, value)) |
| 69 | } |
| 70 | cmd.SetOut(io.Discard) |
| 71 | cmd.SetErr(io.Discard) |
| 72 | assert.ErrorContains(t, cmd.Execute(), tc.expectedError) |
| 73 | } |