forked from sourcegraph/checkup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
github_test.go
437 lines (387 loc) · 13.7 KB
/
github_test.go
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
package checkup
import (
"bytes"
"crypto/sha1"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"net/url"
"path/filepath"
"strings"
"testing"
"time"
"github.com/google/go-github/github"
)
var (
results = []Result{{Title: "Testing"}}
resultsBytes = []byte(`[{"title":"Testing"}]`)
)
func mustWriteJSON(w io.Writer, data interface{}) {
if err := json.NewEncoder(w).Encode(data); err != nil {
panic(err)
}
}
func base64Encoded(input []byte) string {
return base64.StdEncoding.EncodeToString(input)
}
func toJSON(data interface{}) []byte {
encoded, err := json.Marshal(data)
if err != nil {
panic(err)
}
return encoded
}
func sha(data []byte) string {
return fmt.Sprintf("%x", sha1.Sum(data))
}
func pathForGitRepo(path string) string {
return strings.TrimPrefix(path, "/")
}
func pathForIndex(path string) string {
return filepath.Base(path)
}
func repositoryContent(path, serverSHAForRepo string, data interface{}) *github.RepositoryContent {
return &github.RepositoryContent{
Type: github.String("file"),
Encoding: github.String("base64"),
Size: github.Int(len(base64Encoded(toJSON(data)))),
Name: github.String(filepath.Base(path)),
Path: github.String(path),
Content: github.String(base64Encoded(toJSON(data))),
SHA: github.String(serverSHAForRepo),
}
}
func withGitHubServer(t *testing.T, specimen GitHub, f func(*github.Client)) {
// test server
mux := http.NewServeMux()
server := httptest.NewServer(mux)
// github client configured to use test server
client := github.NewClient(nil)
url, _ := url.Parse(server.URL + "/")
client.BaseURL = url
client.UploadURL = url
defer server.Close()
// files := map[string]string{}
index := map[string]int64{
"1501523631505010894-check.json": time.Now().UnixNano(),
"1501525202306053005-check.json": time.Now().UnixNano(),
}
gitRepo := struct {
LastUpdated int64
Files map[string]bool
}{
LastUpdated: time.Now().UnixNano(),
Files: map[string]bool{},
}
fixtureFiles := []string{"1501523631505010894-check.json", "1501525202306053005-check.json", "index.json"}
for _, file := range fixtureFiles {
gitRepo.Files[filepath.Join(specimen.Dir, file)] = true
}
serverSHAForRepo := sha(toJSON(gitRepo))
mux.HandleFunc("/repos/o/r/git/refs/heads/"+specimen.Branch, func(w http.ResponseWriter, r *http.Request) {
fmt.Printf("Processing %s %s\n", r.Method, r.URL.Path)
if got := r.Method; got != "GET" {
t.Errorf("Request method: %v, want %v", got, "GET")
}
mustWriteJSON(w, github.Reference{
Ref: github.String("refs/heads/" + specimen.Branch),
Object: &github.GitObject{
Type: github.String("commit"),
SHA: github.String(serverSHAForRepo),
},
})
})
mux.HandleFunc("/repos/o/r/git/trees/", func(w http.ResponseWriter, r *http.Request) {
fmt.Printf("Processing %s %s\n", r.Method, r.URL.Path)
if got := r.FormValue("recursive"); got != "1" {
t.Errorf("Expected recursive flag to be 1, got %v", got)
}
if sha := strings.TrimPrefix(r.URL.Path, "/repos/o/r/git/trees/"); sha != serverSHAForRepo {
t.Errorf("Expected to fetch tree %s, got: %v", serverSHAForRepo, sha)
}
entries := []github.TreeEntry{
{
SHA: github.String(sha(toJSON(index))),
Path: github.String(filepath.Join(specimen.Dir, "index.json")),
Content: github.String(base64Encoded(toJSON(index))),
},
}
for filename, _ := range index {
entries = append(entries, github.TreeEntry{
SHA: github.String(sha(resultsBytes)),
Path: github.String(filepath.Join(specimen.Dir, filename)),
Content: github.String(string(resultsBytes)),
})
}
mustWriteJSON(w, github.Tree{
SHA: github.String(serverSHAForRepo),
Entries: entries,
})
})
mux.HandleFunc("/repos/o/r/contents/", func(w http.ResponseWriter, r *http.Request) {
fmt.Printf("Processing %s %s\n", r.Method, r.URL.Path)
path := strings.TrimPrefix(r.URL.Path, "/repos/o/r/contents")
if got := r.Method; !(got == "GET" || got == "PUT" || got == "DELETE") {
t.Errorf("Request method: %v, want GET or PUT", got)
}
if got := r.FormValue("ref"); got != "" && got != "heads/b" {
t.Errorf("Expected heads/b, got %v", got)
}
if r.Method == "GET" && strings.HasSuffix(path, "/index.json") {
mustWriteJSON(w, repositoryContent(path, serverSHAForRepo, index))
return
}
if r.Method == "PUT" && strings.HasSuffix(path, "/index.json") {
// 1. Enforce body contains message, committer, content (base64-encoded), sha
stuff := struct {
Message string `json:"message"`
Content string `json:"content"`
SHA string `json:"sha"`
Committer map[string]string `json:"committer"`
}{}
if err := json.NewDecoder(r.Body).Decode(&stuff); err != nil {
t.Errorf("Expected body to decode fine, but got %+v", err)
}
if expected := fmt.Sprintf("[checkup] store %s [ci skip]", strings.TrimPrefix(path, "/")); stuff.Message != expected {
t.Errorf("Expected commit message '%s', got '%s'", expected, stuff.Message)
}
if stuff.SHA != serverSHAForRepo {
t.Errorf("Expected SHA to be %s, got '%s'", serverSHAForRepo, stuff.SHA)
}
if stuff.Committer["email"] != specimen.CommitterEmail {
t.Errorf("Expected email to be %s, got %s", specimen.CommitterEmail, stuff.Committer["email"])
}
if stuff.Committer["name"] != specimen.CommitterName {
t.Errorf("Expected name to be %s, got %s", specimen.CommitterName, stuff.Committer["name"])
}
// 2. Return object based on https://developer.github.com/v3/repos/contents/#update-a-file
gitRepo.LastUpdated = time.Now().UnixNano()
serverSHAForRepo = sha(toJSON(gitRepo))
mustWriteJSON(w, github.RepositoryContentResponse{
Content: repositoryContent(path, serverSHAForRepo, index),
Commit: github.Commit{SHA: github.String(serverSHAForRepo)},
})
return
}
if r.Method == "GET" && strings.HasSuffix(path, "-check.json") {
_, ok := index[pathForIndex(path)]
if ok {
mustWriteJSON(w, repositoryContent(path, serverSHAForRepo, results))
} else {
http.Error(w, filepath.Base(path)+" does not exist", 404)
}
return
}
if r.Method == "PUT" && strings.HasSuffix(path, "-check.json") {
index[pathForIndex(path)] = time.Now().UnixNano()
gitRepo.Files[pathForGitRepo(path)] = true
gitRepo.LastUpdated = time.Now().UnixNano()
serverSHAForRepo = sha(toJSON(gitRepo))
// 1. Enforce body contains message, committer, content (base64-encoded), sha
stuff := struct {
Message string `json:"message"`
Content string `json:"content"`
SHA string `json:"sha"`
Committer map[string]string `json:"committer"`
}{}
if err := json.NewDecoder(r.Body).Decode(&stuff); err != nil {
t.Errorf("Expected body to decode fine, but got %+v", err)
}
if expected := fmt.Sprintf("[checkup] store %s [ci skip]", strings.TrimPrefix(path, "/")); stuff.Message != expected {
t.Errorf("Expected commit message '%s', got '%s'", expected, stuff.Message)
}
if stuff.SHA != "" {
t.Errorf("Expected SHA to be empty, got '%s'", stuff.SHA)
}
if stuff.Committer["email"] != specimen.CommitterEmail {
t.Errorf("Expected email to be %s, got %s", specimen.CommitterEmail, stuff.Committer["email"])
}
if stuff.Committer["name"] != specimen.CommitterName {
t.Errorf("Expected name to be %s, got %s", specimen.CommitterName, stuff.Committer["name"])
}
// Response is the same if updating or creating.
mustWriteJSON(w, github.RepositoryContentResponse{
Content: repositoryContent(path, serverSHAForRepo, results),
Commit: github.Commit{
SHA: github.String(serverSHAForRepo),
},
})
return
}
if r.Method == "DELETE" && strings.HasSuffix(path, "-check.json") {
// 1. Enforce body contains message, committer, content (base64-encoded), sha
stuff := struct {
Message string `json:"message"`
Content string `json:"content"`
SHA string `json:"sha"`
Committer map[string]string `json:"committer"`
}{}
if err := json.NewDecoder(r.Body).Decode(&stuff); err != nil {
t.Errorf("Expected body to decode fine, but got %+v", err)
}
if expected := fmt.Sprintf("[checkup] delete %s [ci skip]", strings.TrimPrefix(path, "/")); stuff.Message != expected {
t.Errorf("Expected commit message to be '%s', got '%s'", expected, stuff.Message)
}
if stuff.SHA != sha(resultsBytes) {
t.Errorf("Expected SHA to be %s, got '%s'", sha(resultsBytes), stuff.SHA)
}
if stuff.Committer["email"] != specimen.CommitterEmail {
t.Errorf("Expected email to be %s, got %s", specimen.CommitterEmail, stuff.Committer["email"])
}
if stuff.Committer["name"] != specimen.CommitterName {
t.Errorf("Expected name to be %s, got %s", specimen.CommitterName, stuff.Committer["name"])
}
// Ok, start modifying the in-memory state.
if _, ok := index[pathForIndex(path)]; !ok {
http.Error(w, "no such file: "+filepath.Base(path), 500)
}
delete(index, filepath.Base(path))
if _, ok := gitRepo.Files[pathForGitRepo(path)]; !ok {
fmt.Printf("path=%s index=%+v repo: %+v\n", path, index, gitRepo.Files)
http.Error(w, "file was deleted", 401)
return
}
gitRepo.Files[pathForGitRepo(path)] = false
gitRepo.LastUpdated = time.Now().UnixNano()
// Don't update the server SHA.
// Response is the same if updating or creating.
mustWriteJSON(w, github.RepositoryContentResponse{
Commit: github.Commit{
SHA: github.String(serverSHAForRepo),
},
})
return
}
http.Error(w, path+" is not handled", 403)
t.Errorf("Cannot handle %s %s (path=%s)", r.Method, r.URL.Path, path)
})
f(client)
}
func TestGitHubWithoutSubdir(t *testing.T) {
// Our subject, our specimen.
specimen := &GitHub{
RepositoryOwner: "o",
RepositoryName: "r",
CommitterName: "John Appleseed",
CommitterEmail: "[email protected]",
Branch: "b",
Dir: "",
}
withGitHubServer(t, *specimen, func(client *github.Client) {
specimen.client = client
if err := specimen.Store(results); err != nil {
t.Fatalf("Expected no error from Store(), got: %v", err)
}
fmt.Println("Done with Store()")
// Make sure index has been created
index, _, err := specimen.readIndex()
if err != nil {
t.Fatalf("Cannot read index: %v", err)
}
if len(index) != 3 {
t.Fatalf("Expected length of index to be 3, but got %v", len(index))
}
var (
name string
nsec int64
)
for name, nsec = range index {
break
}
// Make sure index has timestamp of check
ts := time.Unix(0, nsec)
if time.Since(ts) > 1*time.Second {
t.Errorf("Timestamp of check is %s but expected something very recent", ts)
}
// Make sure check file bytes are correct
fmt.Printf("checking %s\n", name)
b, _, err := specimen.readFile(name)
if err != nil {
t.Fatalf("Expected no error reading body, got: %v", err)
}
if bytes.Compare(b, resultsBytes) != 0 {
t.Errorf("Contents of file are wrong\nExpected %s\nGot %s", resultsBytes, b)
}
// Make sure check file is not deleted after maintain with CheckExpiry == 0
if err := specimen.Maintain(); err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if _, _, err := specimen.readFile(name); err != nil {
t.Fatalf("Expected not error calling Stat() on checkfile, got: %v", err)
}
// Make sure checkfile is deleted after maintain with CheckExpiry > 0
specimen.CheckExpiry = 1 * time.Nanosecond
if err := specimen.Maintain(); err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if _, _, err := specimen.readFile(name); err != nil && err != errFileNotFound {
t.Fatalf("Expected checkfile to be deleted, but Stat() returned error: %v", err)
}
})
}
func TestGitHubWithSubdir(t *testing.T) {
// Our subject, our specimen.
specimen := &GitHub{
RepositoryOwner: "o",
RepositoryName: "r",
CommitterName: "John Appleseed",
CommitterEmail: "[email protected]",
Branch: "b",
Dir: "subdir/",
}
withGitHubServer(t, *specimen, func(client *github.Client) {
specimen.client = client
if err := specimen.Store(results); err != nil {
t.Fatalf("Expected no error from Store(), got: %v", err)
}
fmt.Println("Done with Store()")
// Make sure index has been created
index, _, err := specimen.readIndex()
if err != nil {
t.Fatalf("Cannot read index: %v", err)
}
if len(index) != 3 {
t.Fatalf("Expected length of index to be 3, but got %v", len(index))
}
var (
name string
nsec int64
)
for name, nsec = range index {
break
}
// Make sure index has timestamp of check
ts := time.Unix(0, nsec)
if time.Since(ts) > 1*time.Second {
t.Errorf("Timestamp of check is %s but expected something very recent", ts)
}
// Make sure check file bytes are correct
checkfile := filepath.Join(specimen.Dir, name)
fmt.Printf("checking %s\n", checkfile)
b, _, err := specimen.readFile(checkfile)
if err != nil {
t.Fatalf("Expected no error reading body, got: %v", err)
}
if bytes.Compare(b, resultsBytes) != 0 {
t.Errorf("Contents of file are wrong\nExpected %s\nGot %s", resultsBytes, b)
}
// Make sure check file is not deleted after maintain with CheckExpiry == 0
if err := specimen.Maintain(); err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if _, _, err := specimen.readFile(checkfile); err != nil {
t.Fatalf("Expected not error calling Stat() on checkfile, got: %v", err)
}
// Make sure checkfile is deleted after maintain with CheckExpiry > 0
specimen.CheckExpiry = 1 * time.Nanosecond
if err := specimen.Maintain(); err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if _, _, err := specimen.readFile(checkfile); err != nil && err != errFileNotFound {
t.Fatalf("Expected checkfile to be deleted, but Stat() returned error: %v", err)
}
})
}