-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommitstatus_test.go
More file actions
493 lines (454 loc) · 15.1 KB
/
commitstatus_test.go
File metadata and controls
493 lines (454 loc) · 15.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
package github_test
import (
"context"
"errors"
"fmt"
"net/http"
"net/http/httptest"
"slices"
"strconv"
"testing"
"time"
"github.com/Pix4D/go-kit/github"
"github.com/Pix4D/go-kit/internal/testutils"
)
type mockedResponse struct {
body string
status int
rateLimitRemaining string
rateLimitReset int64
}
const (
emptyRateRemaining = "0" // From the GitHub API.
fullRateRemaining = "5000" // From the GitHub API.
)
func TestGitHubStatusSuccessMockAPI(t *testing.T) {
type testCase struct {
name string
response []mockedResponse
// wantSleeps:
// - contains the durations we expect for the sleeps
// - its size is the number of times we expect the sleep function to be called
wantSleeps []time.Duration
}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
cfg := fakeTestCfg
context := "go-kit/test"
targetURL := "https://go-kit.example/builds/job/42"
now := time.Now()
desc := now.Format("15:04:05")
retryFirstDelay := github.DefaultRetry(nil).FirstDelay
test := func(t *testing.T, tc testCase) {
attempt := 0
handler := func(w http.ResponseWriter, r *http.Request) {
response := tc.response[attempt]
if response.body == "" { // default
response.body = "Anything goes..."
}
w.Header().Set("x-ratelimit-remaining", response.rateLimitRemaining)
w.Header().Set("x-ratelimit-reset", strconv.Itoa(int(response.rateLimitReset)))
w.WriteHeader(response.status)
_, err := fmt.Fprintln(w, response.body)
if err != nil {
t.Fatalf("writing response body: %s", err)
}
attempt++
}
ts := httptest.NewServer(http.HandlerFunc(handler))
defer ts.Close()
log := testutils.MakeTestLog()
sleepSpy := SleepSpy{}
rtr := github.DefaultRetry(log)
rtr.SleepFn = sleepSpy.Sleep
target := &github.Target{
Client: ts.Client(),
Server: ts.URL,
Retry: rtr,
}
ghStatus := github.NewCommitStatus(target, cfg.Token, cfg.Owner, cfg.Repo, context, log)
err := ghStatus.Add(ctx, cfg.SHA, "success", targetURL, desc)
if err != nil {
t.Fatal("Add:", err)
}
if have, want := sleepSpy.sleeps, tc.wantSleeps; slices.Compare(have, want) != 0 {
t.Errorf("%s:\nhave: %v\nwant: %v", "sleeps", have, want)
}
}
testCases := []testCase{
{
name: "Success at first attempt",
response: []mockedResponse{
{status: http.StatusCreated},
},
wantSleeps: nil,
},
{
name: "Rate limited at the first attempt, success at the second attempt",
response: []mockedResponse{
{
status: http.StatusForbidden,
rateLimitRemaining: emptyRateRemaining,
rateLimitReset: now.Add(42 * time.Second).Unix(),
},
{
status: http.StatusCreated,
rateLimitRemaining: fullRateRemaining,
rateLimitReset: now.Add(1 * time.Hour).Unix(),
},
},
wantSleeps: []time.Duration{42 * time.Second},
},
{
name: "retry also on server-side inconsistency (zero sleep time), repro of Pix4D/cogito#124",
response: []mockedResponse{
{
status: http.StatusForbidden,
rateLimitRemaining: emptyRateRemaining,
// This causes sleep time to be 0: it would be silly to fail,
// we should instead attempt once more. Depending on the problem
// server-side, the next request might also fail, but at least we
// did everything we could.
rateLimitReset: now.Unix(),
},
{
status: http.StatusCreated,
rateLimitRemaining: fullRateRemaining,
rateLimitReset: now.Add(1 * time.Hour).Unix(),
},
},
wantSleeps: []time.Duration{retryFirstDelay},
},
{
name: "retry also on server-side inconsistency (negative sleep time), repro of Pix4D/cogito#124",
response: []mockedResponse{
{
status: http.StatusForbidden,
rateLimitRemaining: emptyRateRemaining,
// This causes sleep time to be < 0.
rateLimitReset: now.Add(-30 * time.Minute).Unix(),
},
{
status: http.StatusForbidden,
rateLimitRemaining: emptyRateRemaining,
// This causes sleep time to be < 0.
rateLimitReset: now.Add(-30 * time.Minute).Unix(),
},
{
status: http.StatusCreated,
rateLimitRemaining: fullRateRemaining,
rateLimitReset: now.Add(1 * time.Hour).Unix(),
},
},
wantSleeps: []time.Duration{retryFirstDelay, 2 * retryFirstDelay},
},
{
name: "Github is flaky at the first attempt, success at 3rd attempt",
response: []mockedResponse{
{status: http.StatusGatewayTimeout},
{status: http.StatusGatewayTimeout},
{status: http.StatusCreated},
},
wantSleeps: []time.Duration{retryFirstDelay, 2 * retryFirstDelay},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) { test(t, tc) })
}
}
func TestGitHubStatusFailureMockAPI(t *testing.T) {
type testCase struct {
name string
response []mockedResponse
// wantSleeps:
// - contains the durations we expect for the sleeps
// - its size is the number of times we expect the sleep function to be called
wantSleeps []time.Duration
wantErr string
}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
cfg := fakeTestCfg
context := "go-kit/test"
targetURL := "https://go-kit.example/builds/job/42"
now := time.Now()
desc := now.Format("15:04:05")
upTo := 5 * time.Minute
retryFirstDelay := github.DefaultRetry(nil).FirstDelay
run := func(t *testing.T, tc testCase) {
attempt := 0
handler := func(w http.ResponseWriter, r *http.Request) {
response := tc.response[attempt]
if response.body == "" {
response.body = "fake body"
}
w.Header().Set("x-ratelimit-remaining", response.rateLimitRemaining)
w.Header().Set("x-ratelimit-reset", strconv.Itoa(int(response.rateLimitReset)))
// The Date header is set automatically by default, but we override it
// for better control.
w.Header().Set("Date", now.Format(time.RFC1123))
w.WriteHeader(response.status)
_, err := fmt.Fprintln(w, response.body)
if err != nil {
t.Fatalf("writing response body: %s", err)
}
attempt++
}
ts := httptest.NewServer(http.HandlerFunc(handler))
defer ts.Close()
wantErr := fmt.Sprintf(tc.wantErr, ts.URL)
log := testutils.MakeTestLog()
sleepSpy := SleepSpy{}
rtr := github.DefaultRetry(log)
rtr.UpTo = upTo
rtr.SleepFn = sleepSpy.Sleep
target := &github.Target{
Client: ts.Client(),
Server: ts.URL,
Retry: rtr,
}
ghStatus := github.NewCommitStatus(target, cfg.Token, cfg.Owner, cfg.Repo, context, log)
err := ghStatus.Add(ctx, cfg.SHA, "success", targetURL, desc)
if err == nil {
t.Fatalf("%s\nhave: %v\nwant: %v", "Add", "<no error>", wantErr)
}
if diff := diff(err.Error(), wantErr); diff != "" {
t.Fatalf("Add: error mismatch:\n%s", diff)
}
{
var ghError *github.StatusError
if !errors.As(err, &ghError) {
t.Errorf("%s\nhave: %T\nwant: %T", "Add: error type", err, ghError)
} else {
wantStatus := tc.response[len(tc.response)-1].status
if have, want := ghError.StatusCode, wantStatus; have != want {
t.Errorf("%s\nhave: %v\nwant: %v", "StatusCode", have, want)
}
}
}
if have, want := sleepSpy.sleeps, tc.wantSleeps; slices.Compare(have, want) != 0 {
t.Errorf("%s:\nhave: %v\nwant: %v", "sleeps", have, want)
}
}
testCases := []testCase{
{
name: "non transient error, stop at first attempt",
response: []mockedResponse{
{
body: "fake body",
status: http.StatusNotFound,
},
},
wantSleeps: nil,
wantErr: `failed to add state "success" for commit 0123456: 404 Not Found
Body: fake body
Hint: one of the following happened:
1. The repo https://github.com/fakeOwner/fakeRepo doesn't exist
2. The user who issued the token doesn't have write access to the repo
3. The token doesn't have scope repo:status
Action: POST %s/repos/fakeOwner/fakeRepo/statuses/0123456789012345678901234567890123456789
OAuth: X-Accepted-Oauth-Scopes: , X-Oauth-Scopes: `,
},
{
name: "transient error, consume all attempts",
response: []mockedResponse{
// cumulative
{status: http.StatusInternalServerError}, // 2s 2s
{status: http.StatusInternalServerError}, // 4s 6s
{status: http.StatusInternalServerError}, // 8s 14s
{status: http.StatusInternalServerError}, // 16s 30s
{status: http.StatusInternalServerError}, // 32s 1m2s
{status: http.StatusInternalServerError}, // 1m 2m2s
{status: http.StatusInternalServerError}, // 1m 3m2s
{status: http.StatusInternalServerError}, // 1m 4m2s
{status: http.StatusInternalServerError}, // 1m 5m2s too long
},
wantSleeps: []time.Duration{
retryFirstDelay,
4 * time.Second,
8 * time.Second,
16 * time.Second,
32 * time.Second,
1 * time.Minute,
1 * time.Minute,
1 * time.Minute,
},
wantErr: `failed to add state "success" for commit 0123456: 500 Internal Server Error
Body: fake body
Hint: Github API is down
Action: POST %s/repos/fakeOwner/fakeRepo/statuses/0123456789012345678901234567890123456789
OAuth: X-Accepted-Oauth-Scopes: , X-Oauth-Scopes: `,
},
{
name: "Rate limited: wait time too long (> Retry.UpTo)",
response: []mockedResponse{
{
body: "API rate limit exceeded for user ID 123456789. [rate reset in XXmXXs]",
status: http.StatusForbidden,
rateLimitRemaining: emptyRateRemaining,
rateLimitReset: now.Add(upTo + time.Second).Unix(),
},
},
wantSleeps: nil,
wantErr: `failed to add state "success" for commit 0123456: 403 Forbidden
Body: API rate limit exceeded for user ID 123456789. [rate reset in XXmXXs]
Hint: Rate limited but the wait time to reset would be longer than 5m0s (Retry.UpTo)
Action: POST %s/repos/fakeOwner/fakeRepo/statuses/0123456789012345678901234567890123456789
OAuth: X-Accepted-Oauth-Scopes: , X-Oauth-Scopes: `,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) { run(t, tc) })
}
}
func TestGitHubStatusSuccessIntegration(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
cfg := gitHubSecretsOrFail(t)
context := "go-kit/test"
targetURL := "https://go-kit.example/builds/job/42"
desc := time.Now().Format("15:04:05")
state := "success"
log := testutils.MakeTestLog()
target := &github.Target{
Client: &http.Client{},
Server: github.ApiRoot(github.GhDefaultHostname),
Retry: github.DefaultRetry(log),
}
ghStatus := github.NewCommitStatus(target, cfg.Token, cfg.Owner, cfg.Repo, context, log)
err := ghStatus.Add(ctx, cfg.SHA, state, targetURL, desc)
if err != nil {
t.Fatal("Add:", err)
}
}
func TestGitHubStatusFailureIntegration(t *testing.T) {
type testCase struct {
name string
token string // default: cfg.Token
owner string // default: cfg.Owner
repo string // default: cfg.Repo
sha string // default: cfg.SHA
wantErr string
wantStatus int
}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
cfg := gitHubSecretsOrFail(t)
state := "success"
log := testutils.MakeTestLog()
run := func(t *testing.T, tc testCase) {
// zero values are defaults
if tc.token == "" {
tc.token = cfg.Token
}
if tc.owner == "" {
tc.owner = cfg.Owner
}
if tc.repo == "" {
tc.repo = cfg.Repo
}
if tc.sha == "" {
tc.sha = cfg.SHA
}
target := &github.Target{
Client: &http.Client{},
Server: github.ApiRoot(github.GhDefaultHostname),
Retry: github.DefaultRetry(log),
}
ghStatus := github.NewCommitStatus(target, tc.token, tc.owner, tc.repo, "dummy-context", log)
err := ghStatus.Add(ctx, tc.sha, state, "dummy-url", "dummy-desc")
if err == nil {
t.Fatalf("%s\nhave: %v\nwant: %v", "Add", "<no error>", tc.wantErr)
}
if diff := diff(err.Error(), tc.wantErr); diff != "" {
t.Fatalf("Add: error mismatch:\n%s", diff)
}
{
var ghError *github.StatusError
if !errors.As(err, &ghError) {
t.Errorf("%s\nhave: %T\nwant: %T", "Add: error type", err, ghError)
} else {
if have, want := ghError.StatusCode, tc.wantStatus; have != want {
t.Errorf("%s\nhave: %v\nwant: %v", "StatusCode", have, want)
}
}
}
}
testCases := []testCase{
{
name: "bad token: Unauthorized",
token: "bad-token",
wantErr: `failed to add state "success" for commit 751affd: 401 Unauthorized
Body: {"documentation_url":"https://docs.github.com/rest","message":"Bad credentials","status":"401"}
Hint: Either wrong credentials or PAT expired (check your email for expiration notice)
Action: POST https://api.github.com/repos/pix4d/go-kit-test-read-write/statuses/751affd155db7a00d936ee6e9f483deee69c5922
OAuth: X-Accepted-Oauth-Scopes: , X-Oauth-Scopes: `,
wantStatus: http.StatusUnauthorized,
},
{
name: "non existing repo: Not Found",
repo: "non-existing-really",
wantErr: `failed to add state "success" for commit 751affd: 404 Not Found
Body: {"documentation_url":"https://docs.github.com/rest/commits/statuses#create-a-commit-status","message":"Not Found","status":"404"}
Hint: one of the following happened:
1. The repo https://github.com/pix4d/non-existing-really doesn't exist
2. The user who issued the token doesn't have write access to the repo
3. The token doesn't have scope repo:status
Action: POST https://api.github.com/repos/pix4d/non-existing-really/statuses/751affd155db7a00d936ee6e9f483deee69c5922
OAuth: X-Accepted-Oauth-Scopes: repo, X-Oauth-Scopes: repo:status`,
wantStatus: http.StatusNotFound,
},
{
name: "non existing SHA: Unprocessable Entity",
sha: "e576e3aa7aaaa048b396e2f34fa24c9cf4d1e822",
wantErr: `failed to add state "success" for commit e576e3a: 422 Unprocessable Entity
Body: {"documentation_url":"https://docs.github.com/rest/commits/statuses#create-a-commit-status","message":"No commit found for SHA: e576e3aa7aaaa048b396e2f34fa24c9cf4d1e822","status":"422"}
Hint: none
Action: POST https://api.github.com/repos/pix4d/go-kit-test-read-write/statuses/e576e3aa7aaaa048b396e2f34fa24c9cf4d1e822
OAuth: X-Accepted-Oauth-Scopes: , X-Oauth-Scopes: repo:status`,
wantStatus: http.StatusUnprocessableEntity,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) { run(t, tc) })
}
}
type SleepSpy struct {
sleeps []time.Duration
}
func (spy *SleepSpy) Sleep(d time.Duration) {
spy.sleeps = append(spy.sleeps, d)
}
func TestApiRoot(t *testing.T) {
type testCase struct {
name string
hostname string
wantAPI string
}
run := func(t *testing.T, tc testCase) {
root := github.ApiRoot(tc.hostname)
if have, want := root, tc.wantAPI; have != want {
t.Errorf("%s\nhave: %v\nwant: %v", "ApiRoot", have, want)
}
}
testCases := []testCase{
{
name: "hostname is localhost from http testserver",
hostname: "127.0.0.1:5678",
wantAPI: "http://127.0.0.1:5678",
},
{
name: "default GitHub hostname",
hostname: github.GhDefaultHostname,
wantAPI: "https://api.github.com",
},
{
name: "Github Enterprise hostname",
hostname: "github.mycompany.org",
wantAPI: "https://github.mycompany.org/api/v3",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) { run(t, tc) })
}
}