-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_test.go
89 lines (68 loc) · 2.32 KB
/
server_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
package quayd
import (
"bytes"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"reflect"
"testing"
)
func loadFixture(fixture string, t testing.TB) io.Reader {
body, err := ioutil.ReadFile("test-fixtures/quay.io/" + fixture + ".json")
if err != nil {
t.Fatalf("Unable to load fixture %s: %s", fixture, err)
}
return bytes.NewReader(body)
}
func TestWebhook(t *testing.T) {
r := DefaultStatusesRepository
s := NewServer(nil)
defer r.Reset()
tests := []struct {
status string
fixture string
expected Status
}{
{"pending", "pending_build", Status{Repo: "ejholmes/docker-statsd", Ref: "long-f1fb3b0", State: "pending", Context: "Docker Image", TargetURL: "https://quay.io/repository/ejholmes/docker-statsd/build?current=077f3664-35d3-48e6-9da7-889f9be73070", Description: "The Docker image is building"}},
{"success", "pending_build", Status{Repo: "ejholmes/docker-statsd", Ref: "long-f1fb3b0", State: "success", Context: "Docker Image", TargetURL: "https://quay.io/repository/ejholmes/docker-statsd/build?current=077f3664-35d3-48e6-9da7-889f9be73070", Description: "The Docker image was built"}},
}
for _, tt := range tests {
r.Reset()
resp := httptest.NewRecorder()
req, _ := http.NewRequest("POST", "/quay/"+tt.status, loadFixture(tt.fixture, t))
s.ServeHTTP(resp, req)
if len(r.statuses) != 1 {
t.Fatal("Expected 1 commit status")
}
if got, want := r.statuses[0], &tt.expected; !reflect.DeepEqual(got, want) {
t.Fatalf("Status => %q; want %q", got, want)
}
}
}
func TestWebhook_InvalidStatus(t *testing.T) {
r := DefaultStatusesRepository
s := NewServer(nil)
resp := httptest.NewRecorder()
req, _ := http.NewRequest("POST", "/quay/foo", loadFixture("pending_build", t))
s.ServeHTTP(resp, req)
if len(r.statuses) != 0 {
t.Fatal("Expected 0 commit statuses")
}
}
func TestWebhook_ManualTrigger(t *testing.T) {
r := DefaultStatusesRepository
s := NewServer(nil)
resp := httptest.NewRecorder()
req, _ := http.NewRequest("POST", "/quay/pending", loadFixture("pending_build.manual", t))
s.ServeHTTP(resp, req)
if len(r.statuses) != 0 {
t.Fatal("Expected 0 commit statuses")
}
}
func TestWebhook_TagsImageID(t *testing.T) {
s := NewServer(nil)
resp := httptest.NewRecorder()
req, _ := http.NewRequest("POST", "/quay/success", loadFixture("pending_build", t))
s.ServeHTTP(resp, req)
}