|
| 1 | +package hcloud |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | +) |
| 9 | + |
| 10 | +func TestWaitFor(t *testing.T) { |
| 11 | + RunMockedTestCases(t, |
| 12 | + []MockedTestCase{ |
| 13 | + { |
| 14 | + Name: "succeed", |
| 15 | + WantRequests: []MockedRequest{ |
| 16 | + {"GET", "/actions?id=1509772237&page=1&sort=status&sort=id", nil, 200, |
| 17 | + `{ |
| 18 | + "actions": [ |
| 19 | + { "id": 1509772237, "status": "running", "progress": 0 } |
| 20 | + ], |
| 21 | + "meta": { "pagination": { "page": 1 }} |
| 22 | + }`}, |
| 23 | + {"GET", "/actions?id=1509772237&page=1&sort=status&sort=id", nil, 200, |
| 24 | + `{ |
| 25 | + "actions": [ |
| 26 | + { "id": 1509772237, "status": "success", "progress": 100 } |
| 27 | + ], |
| 28 | + "meta": { "pagination": { "page": 1 }} |
| 29 | + }`}, |
| 30 | + }, |
| 31 | + Run: func(env testEnv) { |
| 32 | + actions := []*Action{{ID: 1509772237, Status: ActionStatusRunning}} |
| 33 | + |
| 34 | + err := env.Client.Action.WaitFor(context.Background(), actions...) |
| 35 | + assert.NoError(t, err) |
| 36 | + }, |
| 37 | + }, |
| 38 | + { |
| 39 | + Name: "succeed with already succeeded action", |
| 40 | + Run: func(env testEnv) { |
| 41 | + actions := []*Action{{ID: 1509772237, Status: ActionStatusSuccess}} |
| 42 | + |
| 43 | + err := env.Client.Action.WaitFor(context.Background(), actions...) |
| 44 | + assert.NoError(t, err) |
| 45 | + }, |
| 46 | + }, |
| 47 | + { |
| 48 | + Name: "fail with unknown action", |
| 49 | + WantRequests: []MockedRequest{ |
| 50 | + {"GET", "/actions?id=1509772237&page=1&sort=status&sort=id", nil, 200, |
| 51 | + `{ |
| 52 | + "actions": [], |
| 53 | + "meta": { "pagination": { "page": 1 }} |
| 54 | + }`}, |
| 55 | + }, |
| 56 | + Run: func(env testEnv) { |
| 57 | + actions := []*Action{{ID: 1509772237, Status: ActionStatusRunning}} |
| 58 | + |
| 59 | + err := env.Client.Action.WaitFor(context.Background(), actions...) |
| 60 | + assert.Error(t, err) |
| 61 | + assert.Equal(t, "actions not found: [1509772237]", err.Error()) |
| 62 | + }, |
| 63 | + }, |
| 64 | + { |
| 65 | + Name: "fail with canceled context", |
| 66 | + Run: func(env testEnv) { |
| 67 | + actions := []*Action{{ID: 1509772237, Status: ActionStatusRunning}} |
| 68 | + |
| 69 | + ctx, cancelFunc := context.WithCancel(context.Background()) |
| 70 | + cancelFunc() |
| 71 | + err := env.Client.Action.WaitFor(ctx, actions...) |
| 72 | + assert.Error(t, err) |
| 73 | + }, |
| 74 | + }, |
| 75 | + { |
| 76 | + Name: "fail with api error", |
| 77 | + WantRequests: []MockedRequest{ |
| 78 | + {"GET", "/actions?id=1509772237&page=1&sort=status&sort=id", nil, 503, ""}, |
| 79 | + }, |
| 80 | + Run: func(env testEnv) { |
| 81 | + actions := []*Action{{ID: 1509772237, Status: ActionStatusRunning}} |
| 82 | + |
| 83 | + err := env.Client.Action.WaitFor(context.Background(), actions...) |
| 84 | + assert.Error(t, err) |
| 85 | + assert.Equal(t, "hcloud: server responded with status code 503", err.Error()) |
| 86 | + }, |
| 87 | + }, |
| 88 | + }, |
| 89 | + ) |
| 90 | +} |
| 91 | + |
| 92 | +func TestWaitForFunc(t *testing.T) { |
| 93 | + RunMockedTestCases(t, |
| 94 | + []MockedTestCase{ |
| 95 | + { |
| 96 | + Name: "succeed", |
| 97 | + WantRequests: []MockedRequest{ |
| 98 | + {"GET", "/actions?id=1509772237&id=1509772238&page=1&sort=status&sort=id", nil, 200, |
| 99 | + `{ |
| 100 | + "actions": [ |
| 101 | + { "id": 1509772237, "status": "running", "progress": 40 }, |
| 102 | + { "id": 1509772238, "status": "running", "progress": 0 } |
| 103 | + ], |
| 104 | + "meta": { "pagination": { "page": 1 }} |
| 105 | + }`}, |
| 106 | + {"GET", "/actions?id=1509772237&id=1509772238&page=1&sort=status&sort=id", nil, 200, |
| 107 | + `{ |
| 108 | + "actions": [ |
| 109 | + { "id": 1509772237, "status": "running", "progress": 60 }, |
| 110 | + { "id": 1509772238, "status": "running", "progress": 50 } |
| 111 | + ], |
| 112 | + "meta": { "pagination": { "page": 1 }} |
| 113 | + }`}, |
| 114 | + {"GET", "/actions?id=1509772237&id=1509772238&page=1&sort=status&sort=id", nil, 200, |
| 115 | + `{ |
| 116 | + "actions": [ |
| 117 | + { "id": 1509772237, "status": "success", "progress": 100 }, |
| 118 | + { "id": 1509772238, "status": "running", "progress": 75 } |
| 119 | + ], |
| 120 | + "meta": { "pagination": { "page": 1 }} |
| 121 | + }`}, |
| 122 | + {"GET", "/actions?id=1509772238&page=1&sort=status&sort=id", nil, 200, |
| 123 | + `{ |
| 124 | + "actions": [ |
| 125 | + { "id": 1509772238, "status": "error", "progress": 75, |
| 126 | + "error": { |
| 127 | + "code": "action_failed", |
| 128 | + "message": "Something went wrong with the action" |
| 129 | + } |
| 130 | + } |
| 131 | + ], |
| 132 | + "meta": { "pagination": { "page": 1 }} |
| 133 | + }`}, |
| 134 | + }, |
| 135 | + Run: func(env testEnv) { |
| 136 | + actions := []*Action{ |
| 137 | + {ID: 1509772236, Status: ActionStatusSuccess}, |
| 138 | + {ID: 1509772237, Status: ActionStatusRunning}, |
| 139 | + {ID: 1509772238, Status: ActionStatusRunning}, |
| 140 | + } |
| 141 | + progress := make([]int, 0) |
| 142 | + |
| 143 | + progressByAction := make(map[int]int, len(actions)) |
| 144 | + err := env.Client.Action.WaitForFunc(context.Background(), func(update *Action) error { |
| 145 | + switch update.Status { |
| 146 | + case ActionStatusRunning: |
| 147 | + progressByAction[update.ID] = update.Progress |
| 148 | + case ActionStatusSuccess: |
| 149 | + progressByAction[update.ID] = 100 |
| 150 | + case ActionStatusError: |
| 151 | + progressByAction[update.ID] = 100 |
| 152 | + } |
| 153 | + |
| 154 | + sum := 0 |
| 155 | + for _, value := range progressByAction { |
| 156 | + sum += value |
| 157 | + } |
| 158 | + progress = append(progress, sum/len(actions)) |
| 159 | + |
| 160 | + return nil |
| 161 | + }, actions...) |
| 162 | + |
| 163 | + assert.Nil(t, err) |
| 164 | + assert.Equal(t, []int{33, 46, 46, 53, 70, 83, 91, 100}, progress) |
| 165 | + }, |
| 166 | + }, |
| 167 | + }, |
| 168 | + ) |
| 169 | +} |
0 commit comments