-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_test.go
339 lines (308 loc) · 12.7 KB
/
main_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
package main_test
import (
"bytes"
"encoding/json"
"fmt"
"net"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"testing"
"time"
"github.com/google/go-cmp/cmp"
)
const ms = int64(time.Millisecond)
func TestOK(t *testing.T) {
t.Parallel()
res := test(t, testcase{
env: []string{"BUILD_MANIFEST=not-a-file"},
docker: []dockerEvent{
{TimeNano: 1 * ms, Type: "container", Action: "start", Actor: dockerActor{Attributes: dockerAttr{Name: "step_0"}}},
{TimeNano: 5 * ms, Type: "container", Action: "die", Actor: dockerActor{Attributes: dockerAttr{Name: "step_0", ExitCode: "0"}}},
{TimeNano: 50 * ms, Type: "container", Action: "start", Actor: dockerActor{Attributes: dockerAttr{Name: "step_1"}}},
{TimeNano: 55 * ms, Type: "container", Action: "start", Actor: dockerActor{Attributes: dockerAttr{Name: "step_2"}}},
{TimeNano: 80 * ms, Type: "container", Action: "start", Actor: dockerActor{Attributes: dockerAttr{Name: "step_3"}}},
{TimeNano: 10_200 * ms, Type: "container", Action: "die", Actor: dockerActor{Attributes: dockerAttr{Name: "step_1", ExitCode: "0"}}},
{TimeNano: 10_300 * ms, Type: "container", Action: "die", Actor: dockerActor{Attributes: dockerAttr{Name: "step_3", ExitCode: "1"}}},
{TimeNano: 10_301 * ms, Type: "container", Action: "kill", Actor: dockerActor{Attributes: dockerAttr{Name: "step_2", Signal: "9"}}},
{TimeNano: 10_302 * ms, Type: "container", Action: "die", Actor: dockerActor{Attributes: dockerAttr{Name: "step_2", ExitCode: "1"}}},
},
})
exp := []commitStatus{
{Context: "gcb", State: "success", Description: "Done: step_0", TargetURL: "https://console.cloud.google.com/cloud-build/builds/build-123;step=0?project=gcb-project"},
{Context: "gcb", State: "pending", Description: "Running: step_2, step_1; Done: step_0", TargetURL: "https://console.cloud.google.com/cloud-build/builds/build-123;step=2?project=gcb-project"},
{Context: "gcb", State: "pending", Description: "Running: step_3, step_2, step_1; Done: step_0", TargetURL: "https://console.cloud.google.com/cloud-build/builds/build-123;step=3?project=gcb-project"},
{Context: "gcb", State: "pending", Description: "Running: step_3 10s, step_2 10s, step_1 10s; Done: step_0", TargetURL: "https://console.cloud.google.com/cloud-build/builds/build-123;step=3?project=gcb-project"},
{Context: "gcb", State: "pending", Description: "Running: step_3 10s, step_2 10s; Done: step_1 10s, step_0", TargetURL: "https://console.cloud.google.com/cloud-build/builds/build-123;step=3?project=gcb-project"},
{Context: "gcb", State: "error", Description: "Error: step_3 10s; Cancelled: step_2 10s; Done: step_1 10s, step_0", TargetURL: "https://console.cloud.google.com/cloud-build/builds/build-123;step=3?project=gcb-project"},
}
if diff := cmp.Diff(exp, res.statuses); diff != "" {
t.Errorf("Expected GitHub updates (-) but got (+):\n%s", diff)
}
}
func TestOKManifest(t *testing.T) {
t.Parallel()
wd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
res := test(t, testcase{
env: []string{"BUILD_MANIFEST=" + filepath.Join(wd, "testdata/gcbtest.yaml")},
docker: []dockerEvent{
{TimeNano: 1 * ms, Type: "container", Action: "start", Actor: dockerActor{Attributes: dockerAttr{Name: "step_0"}}},
{TimeNano: 5 * ms, Type: "container", Action: "die", Actor: dockerActor{Attributes: dockerAttr{Name: "step_0", ExitCode: "0"}}},
{TimeNano: 50 * ms, Type: "container", Action: "start", Actor: dockerActor{Attributes: dockerAttr{Name: "step_1"}}},
{TimeNano: 55 * ms, Type: "container", Action: "start", Actor: dockerActor{Attributes: dockerAttr{Name: "step_2"}}},
{TimeNano: 80 * ms, Type: "container", Action: "start", Actor: dockerActor{Attributes: dockerAttr{Name: "step_3"}}},
{TimeNano: 10_200 * ms, Type: "container", Action: "die", Actor: dockerActor{Attributes: dockerAttr{Name: "step_1", ExitCode: "0"}}},
{TimeNano: 10_300 * ms, Type: "container", Action: "die", Actor: dockerActor{Attributes: dockerAttr{Name: "step_3", ExitCode: "1"}}},
{TimeNano: 10_400 * ms, Type: "container", Action: "kill", Actor: dockerActor{Attributes: dockerAttr{Name: "step_2", Signal: "9"}}},
{TimeNano: 10_401 * ms, Type: "container", Action: "die", Actor: dockerActor{Attributes: dockerAttr{Name: "step_2", ExitCode: "1"}}},
},
})
exp := []commitStatus{
{Context: "gcb", State: "pending", Description: "Done: quick", TargetURL: "https://console.cloud.google.com/cloud-build/builds/build-123;step=0?project=gcb-project"},
{Context: "gcb", State: "pending", Description: "Running: incomplete, slow; Done: quick", TargetURL: "https://console.cloud.google.com/cloud-build/builds/build-123;step=2?project=gcb-project"},
{Context: "gcb", State: "pending", Description: "Running: failure, incomplete, slow; Done: quick", TargetURL: "https://console.cloud.google.com/cloud-build/builds/build-123;step=3?project=gcb-project"},
{Context: "gcb", State: "pending", Description: "Running: failure 10s, incomplete 10s, slow 10s; Done: quick", TargetURL: "https://console.cloud.google.com/cloud-build/builds/build-123;step=3?project=gcb-project"},
{Context: "gcb", State: "pending", Description: "Running: failure 10s, incomplete 10s; Done: slow 10s, quick", TargetURL: "https://console.cloud.google.com/cloud-build/builds/build-123;step=3?project=gcb-project"},
{Context: "gcb", State: "error", Description: "Error: failure 10s; Cancelled: incomplete 10s; Done: slow 10s, quick", TargetURL: "https://console.cloud.google.com/cloud-build/builds/build-123;step=3?project=gcb-project"},
}
if diff := cmp.Diff(exp, res.statuses); diff != "" {
t.Errorf("Expected GitHub updates (-) but got (+):\n%s", diff)
}
}
func TestContextName(t *testing.T) {
t.Parallel()
res := test(t, testcase{
env: []string{"STATUS_CONTEXT=gcb-test"}, // As opposed to "user:pass".
docker: []dockerEvent{
{TimeNano: 1 * ms, Type: "container", Action: "start", Actor: dockerActor{Attributes: dockerAttr{Name: "step_0"}}},
},
})
exp := []commitStatus{
{Context: "gcb-test", State: "pending", Description: "Running: step_0", TargetURL: "https://console.cloud.google.com/cloud-build/builds/build-123;step=0?project=gcb-project"},
}
if diff := cmp.Diff(exp, res.statuses); diff != "" {
t.Errorf("Expected GitHub updates (-) but got (+):\n%s", diff)
}
}
func TestGitHubShortToken(t *testing.T) {
test(t, testcase{
env: []string{"GITHUB_TOKEN=token"}, // As opposed to "user:pass".
docker: []dockerEvent{
{TimeNano: 1 * ms, Type: "container", Action: "start", Actor: dockerActor{Attributes: dockerAttr{Name: "step_0"}}},
},
})
}
func TestBadGitHubRepo(t *testing.T) {
t.Parallel()
res := test(t, testcase{
fail: true,
env: []string{"GITHUB_REPO=unknown"},
docker: []dockerEvent{
{TimeNano: 1 * ms, Type: "container", Action: "start", Actor: dockerActor{Attributes: dockerAttr{Name: "step_0"}}},
},
})
if res.err == nil {
t.Fatal("Expected error but received none.")
}
requireLogsContain(t, res.logs, `404 Not Found`)
}
func TestBadGitHubToken(t *testing.T) {
t.Parallel()
res := test(t, testcase{
fail: true,
env: []string{"GITHUB_TOKEN=bad-token"},
docker: []dockerEvent{
{TimeNano: 1 * ms, Type: "container", Action: "start", Actor: dockerActor{Attributes: dockerAttr{Name: "step_0"}}},
},
})
if res.err == nil {
t.Fatal("Expected error but received none.")
}
requireLogsContain(t, res.logs, `Expected token "token" but got "bad-token".`)
}
func TestBadDockerHost(t *testing.T) {
t.Parallel()
res := test(t, testcase{
fail: true,
env: []string{"DOCKER_HOST=unix:///dev/null"},
docker: []dockerEvent{
{TimeNano: 1 * ms, Type: "container", Action: "start", Actor: dockerActor{Attributes: dockerAttr{Name: "step_0"}}},
},
})
if res.err == nil {
t.Fatal("Expected error but received none.")
}
requireLogsContain(t, res.logs, `dial unix /dev/null: connect: connection refused`)
}
type testcase struct {
fail bool
env []string
docker []dockerEvent
}
type testres struct {
err error
statuses []commitStatus
logs bytes.Buffer
}
func test(t *testing.T, tc testcase) (tr testres) {
// Catch failures and abort the test.
defer func() {
t.Cleanup(func() {
if t.Failed() {
t.Logf("Logs:\n%s", tr.logs.Bytes())
}
})
if !tc.fail && tr.err != nil {
t.Fatalf("Error running gcb2gh: %s", tr.err)
}
}()
// Create a fake GitHub API that logs updates.
var updLock sync.Mutex
var updates []commitStatus
gmux := http.NewServeMux()
gmux.HandleFunc("/repos/unravelin/gcb2gh-test/statuses/abc123", func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, fmt.Sprintf("Expected a POST request but got %s.", r.Method), http.StatusMethodNotAllowed)
return
}
// Validate the token.
const expTok = "token"
if _, tok, ok := r.BasicAuth(); !ok || tok != expTok {
http.Error(w, fmt.Sprintf("Expected token %q but got %q.", expTok, tok), http.StatusUnauthorized)
return
}
// Parse the update.
var upd commitStatus
err := json.NewDecoder(r.Body).Decode(&upd)
if err != nil {
http.Error(w, fmt.Sprintf("Error decoding request: %s", err), http.StatusBadRequest)
return
}
if len(upd.Description) > 140 {
http.Error(w, "Description too long.", http.StatusUnprocessableEntity)
return
}
// Respond with success.
w.WriteHeader(http.StatusCreated)
// Store the update.
updLock.Lock()
updates = append(updates, upd)
updLock.Unlock()
})
gh := httptest.NewServer(gmux)
defer gh.Close()
// Fake a Docker daemon to produce our test set of events.
dmux := http.NewServeMux()
dmux.HandleFunc("/events", func(w http.ResponseWriter, r *http.Request) {
// Validate we've got the required filters.
q := r.URL.Query()
if exp, act := "10", q.Get("since"); exp != act {
t.Errorf("Expected docker query param since=%q but got %q.", exp, act)
}
if exp, act := "container", q.Get("type"); exp != act {
t.Errorf("Expected docker query param since=%q but got %q.", exp, act)
}
// Send back the events.
w.Header().Set("Content-Type", "application/json")
s := json.NewEncoder(w)
now := time.Now().UnixNano()
ts := now
for _, e := range tc.docker {
// Sleep between events.
e.TimeNano += now
time.Sleep(time.Duration(e.TimeNano - ts))
ts = e.TimeNano
// Send.
err := s.Encode(e)
if err != nil {
t.Errorf("Error sending back docker event: %s", err)
return
}
w.(http.Flusher).Flush()
}
})
dsock := filepath.Join(t.TempDir(), "docker.sock")
serveSocket(t, dsock, dmux)
// Run gcb2gh.
run := exec.Command("go", "run", ".")
run.Stderr = &tr.logs
run.Stdout = os.Stdout
run.Env = append(
os.Environ(),
"PROJECT_ID=gcb-project",
"BUILD_ID=build-123",
"COMMIT_SHA=abc123",
"DOCKER_HOST=unix://"+dsock,
"GITHUB_API="+gh.URL,
"GITHUB_TOKEN=user:token",
"GITHUB_USER=unravelin",
"GITHUB_REPO=gcb2gh-test",
)
run.Env = append(run.Env, tc.env...)
tr.err = run.Run()
tr.statuses = updates
return tr
}
func requireLogsContain(t *testing.T, logs bytes.Buffer, find string) {
s := logs.String()
if !strings.Contains(s, find) {
t.Fatalf("Expected logs to contain %q but couldn't find it.", find)
// t.Fatalf("Logs:\n%s", s)
}
}
func serveSocket(t *testing.T, sockfile string, h http.Handler) {
sock, err := net.Listen("unix", sockfile)
if err != nil {
t.Fatalf("Failed to listen on socket: %s", err)
}
s := &http.Server{Handler: h}
t.Cleanup(func() {
s.Close()
})
go func() {
err := s.Serve(sock)
if err != nil && err != http.ErrServerClosed {
t.Errorf("Error serving socket: %s", err)
}
}()
}
type commitStatus struct {
State string `json:"state,omitempty"`
TargetURL string `json:"target_url,omitempty"`
Description string `json:"description,omitempty"`
Context string `json:"context,omitempty"`
}
type dockerEvent struct {
Type string `json:"Type,omitempty"`
Action string `json:"Action,omitempty"`
Actor dockerActor `json:"Actor,omitempty"`
Scope string `json:"scope,omitempty"`
Time int64 `json:"time,omitempty"`
TimeNano int64 `json:"timeNano,omitempty"`
Status string `json:"status,omitempty"`
ID string `json:"id,omitempty"`
From string `json:"from,omitempty"`
}
type dockerActor struct {
ID string `json:"ID,omitempty"`
Attributes dockerAttr `json:"Attributes"`
}
type dockerAttr struct {
Driver string `json:"driver,omitempty"`
Image string `json:"image,omitempty"`
Name string `json:"name,omitempty"`
Container string `json:"container,omitempty"`
Type string `json:"type,omitempty"`
Destination string `json:"destination,omitempty"`
Propagation string `json:"propagation,omitempty"`
ReadWrite string `json:"read/write,omitempty"`
ExitCode string `json:"exitCode,omitempty"`
Signal string `json:"signal,omitempty"`
}