oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package thintest
2
3import (
4 "encoding/base64"
5 "net/url"
6 "testing"
7 "time"
8
9 "amadan.net/oficina/equipo/internal/wire"
10)
11
12// Mac runners slice F, plan task 4: a worker's candidate is measured by
13// the primary when it publishes. Its change files ride in the published
14// report and its diffs are uploaded bound to that publication; the
15// instance accepts them only from that publication's primary, for the
16// worker's task, step and attempt. The worker's outcome (unfinished,
17// no-shell) is kept from its committed report through the publication,
18// and Pushed is only ever the primary's word.
19
20// swipeDiff is one file's display diff of a worker's candidate.
21var swipeDiff = wire.FileDiff{Path: "Sources/Swipe.swift", Patch: "@@ -1 +1 @@\n-let back = false\n+let back = true\n"}
22
23// committedAs runs w's Engineering attempt d to a commit whose outcome is
24// outcome, as workerCommitted does; with done, the worker's final report
25// (done, not pushed, its reason for that outcome) comes before the pack.
26func (r *runnersRig) committedAs(w *peer, d *wire.DispatchCommand, cand, outcome string, done bool) *wire.PublishCommand {
27 r.t.Helper()
28 if !w.lease(d.StepID, d.Attempt) {
29 r.t.Fatal("start refused")
30 }
31 w.send(wire.FrameStep, wire.StepReport{TaskID: d.TaskID, StepID: d.StepID, Attempt: d.Attempt, State: wire.StepRunning, BaseSHA: d.BaseSHA, Findings: []wire.Finding{}})
32 w.send(wire.FrameStep, wire.StepReport{TaskID: d.TaskID, StepID: d.StepID, Attempt: d.Attempt, State: wire.StepRunning, CandidateSHA: cand, Phase: wire.OutcomeCommitted, OutcomePhase: outcome, Findings: []wire.Finding{}})
33 _, body := w.expect(wire.FrameSendPack)
34 h := body.(*wire.SendPackCommand).Transfer
35 if h.Direction != wire.TransferOutbound || h.CandidateSHA != cand {
36 r.t.Fatalf("outbound header %+v", h)
37 }
38 if done {
39 w.send(wire.FrameStep, workerDone(d, cand, outcome))
40 }
41 w.send(wire.FrameBlob, chunk(h, []byte("PACK commit")))
42 r.fromPrimary(wire.FrameBlob)
43 pub := r.fromPrimary(wire.FramePublish).(*wire.PublishCommand)
44 if pub.CandidateSHA != cand || pub.Transfer != h.ID || pub.StepID != d.StepID || pub.Attempt != d.Attempt || pub.OutcomePhase != outcome {
45 r.t.Fatalf("publish %+v, want outcome %q", pub, outcome)
46 }
47 return pub
48}
49
50// workerDone is a worker's final report for its commit: done, never
51// pushed, with the reason its outcome has on the worker.
52func workerDone(d *wire.DispatchCommand, cand, outcome string) wire.StepReport {
53 reason := ""
54 switch outcome {
55 case wire.OutcomeUnfinished:
56 reason = "step_timeout"
57 case wire.OutcomeNoShell:
58 reason = wire.ReasonShellUnavailable
59 }
60 return wire.StepReport{TaskID: d.TaskID, StepID: d.StepID, Attempt: d.Attempt, State: wire.StepDone, Reason: reason, CandidateSHA: cand, Termination: true, TerminationKind: wire.TerminationProcessGroup, OutcomePhase: outcome, Findings: []wire.Finding{}}
61}
62
63// publicationDiff is the primary's upload of one display diff bound to
64// pub.
65func publicationDiff(pub *wire.PublishCommand, base string, file wire.FileDiff) wire.ChangeDiffUpload {
66 return wire.ChangeDiffUpload{TaskID: pub.TaskID, StepID: pub.StepID, Attempt: pub.Attempt, Publication: pub.Publication, Base: base, Candidate: pub.CandidateSHA, Collection: wire.DiffCollection([]wire.FileDiff{file}), Total: 1, Path: file.Path, Size: len(file.Patch), SHA256: wire.DiffDigest([]byte(file.Patch)), Data: base64.StdEncoding.EncodeToString([]byte(file.Patch))}
67}
68
69func diffFacts(base, cand string, file wire.FileDiff) *wire.ChangeFiles {
70 return &wire.ChangeFiles{Base: base, SHA: cand, Files: 1, Additions: 1, Deletions: 1, Entries: []wire.ChangedFile{{Path: file.Path, Status: wire.ChangeFileModified, Additions: 1, Deletions: 1}}}
71}
72
73// publishedWith is the primary's published report for pub with its
74// measurements of the candidate.
75func publishedWith(pub *wire.PublishCommand, base string, file wire.FileDiff) wire.PublishedReport {
76 return wire.PublishedReport{Publication: pub.Publication, TaskID: pub.TaskID, StepID: pub.StepID, Attempt: pub.Attempt, CandidateSHA: pub.CandidateSHA, Pushed: true, ChangeFiles: diffFacts(base, pub.CandidateSHA, file), DiffCollection: wire.DiffCollection([]wire.FileDiff{file}), OutcomePhase: pub.OutcomePhase}
77}
78
79// diffRows counts the stored diff rows of a step, accepted or not.
80func (r *runnersRig) diffRows(step string) int {
81 r.t.Helper()
82 var n int
83 if err := r.inst.DB.Writer().QueryRow("SELECT count(*) FROM chat_change_diffs WHERE step_id = ?", step).Scan(&n); err != nil {
84 r.t.Fatal(err)
85 }
86 return n
87}
88
89// diffOf reads a file's diff of the task's candidate as the person's
90// device does.
91func (r *runnersRig) diffOf(task, cand, path string) wire.ChangeDiff {
92 r.t.Helper()
93 var out wire.ChangeDiff
94 if err := r.client.do(r.ctx, "GET", "/api/v1/tasks/"+task+"/changes/diff?candidate_sha="+cand+"&path="+url.QueryEscape(path), nil, &out); err != nil {
95 r.t.Fatal(err)
96 }
97 return out
98}
99
100// shown: the task shows the primary's measurement of the worker's
101// candidate, and the file's diff reads as uploaded.
102func (r *runnersRig) shown(task, base, cand string, file wire.FileDiff) {
103 r.t.Helper()
104 k := r.task(task)
105 f := k.Steps[0].ChangeFiles
106 if f == nil || f.Base != base || f.SHA != cand || len(f.Entries) != 1 || f.Entries[0].Path != file.Path || !k.Steps[0].Pushed {
107 r.t.Fatalf("change files on the task %+v (step %+v)", f, k.Steps[0])
108 }
109 if out := r.diffOf(task, cand, file.Path); out.Patch != file.Patch || out.StepID != k.Steps[0].ID || out.Reason != "" {
110 r.t.Fatalf("diff %+v", out)
111 }
112}
113
114// restartMac restarts the instance (a deploy) and reconnects the primary
115// and w, returning w's new connection.
116func (r *runnersRig) restartMac(w *peer) *peer {
117 r.t.Helper()
118 pgen, wgen := r.primary.fr.Welcome.Generation, w.fr.Welcome.Generation
119 c := r.creds[w.fr]
120 r.primary.fr.Close()
121 w.fr.Close()
122 r.inst.Restart(r.t)
123 h := primaryHello(pgen)
124 h.Protocol = wire.ProtocolMacEngineering
125 h.Catalog[0].NeedsMacOS = true
126 p, err := DialRunnerHello(r.ctx, r.inst.URL, r.rig.runner, h)
127 if err != nil {
128 r.t.Fatal(err)
129 }
130 r.t.Cleanup(p.Close)
131 r.runner, r.primary, r.held = p, &peer{t: r.t, r: r.toolRig, fr: p}, nil
132 r.primary.r = r.toolRig
133 wh := macEngineerHello(c[1], wire.EngineeringReady, wire.ProtocolMacEngineering, false)
134 wh.Generation = wgen
135 fr, err := DialRunnerHello(r.ctx, r.inst.URL, c[0], wh)
136 if err != nil {
137 r.t.Fatal(err)
138 }
139 r.t.Cleanup(fr.Close)
140 r.creds[fr] = c
141 return &peer{t: r.t, r: r.toolRig, fr: fr}
142}
143
144// The primary's diffs of a worker's candidate are accepted for its
145// publication whether the worker's final report comes before them or
146// after, and across an instance restart; they are shown once the
147// published report names their collection.
148func TestContractPublicationDiffs(t *testing.T) {
149 for _, order := range []string{"uploads before done", "done before uploads", "restart between"} {
150 t.Run(order, func(t *testing.T) {
151 r := newMacRig(t)
152 w := r.newMacEngineer("Mac A", wire.EngineeringReady)
153 base, cand := sha(1), sha(0xd1)
154 task, d := r.macDispatch(w, "iOS diffs", base)
155 pub := r.committedAs(w, d, cand, wire.OutcomeCommitted, order == "done before uploads")
156 r.primary.send(wire.FrameChangeDiff, publicationDiff(pub, base, swipeDiff))
157 if n := r.diffRows(d.StepID); n != 1 {
158 t.Fatalf("%d diff rows from the publication's primary", n)
159 }
160 if order == "restart between" {
161 w = r.restartMac(w)
162 }
163 r.primary.send(wire.FramePublished, publishedWith(pub, base, swipeDiff))
164 if order != "done before uploads" {
165 w.send(wire.FrameStep, workerDone(d, cand, wire.OutcomeCommitted))
166 }
167 r.shown(task, base, cand, swipeDiff)
168 })
169 }
170}
171
172// Diffs that name a publication are refused from anyone but that
173// publication's primary (here the worker), for another attempt, for a
174// publication that is not the step's own or that failed; a worker's own
175// diffs are never kept. Retry's new publication is accepted.
176func TestContractPublicationDiffsRefused(t *testing.T) {
177 r := newMacRig(t)
178 w := r.newMacEngineer("Mac A", wire.EngineeringReady)
179 base, cand := sha(1), sha(0xd2)
180 task, d := r.macDispatch(w, "iOS refused diffs", base)
181 pub := r.committedAs(w, d, cand, wire.OutcomeCommitted, false)
182 up := publicationDiff(pub, base, swipeDiff)
183 none := func(why string) {
184 t.Helper()
185 if n := r.diffRows(d.StepID); n != 0 {
186 t.Fatalf("%s: %d diff rows kept", why, n)
187 }
188 }
189 // The worker, naming the publication, and on its own.
190 w.send(wire.FrameChangeDiff, up)
191 none("from the worker")
192 own := up
193 own.Publication = ""
194 w.send(wire.FrameChangeDiff, own)
195 none("the worker's own diffs")
196 // The primary, for another attempt or another publication.
197 wrong := up
198 wrong.Attempt = d.Attempt + 1
199 r.primary.send(wire.FrameChangeDiff, wrong)
200 none("another attempt")
201 stale := up
202 stale.Publication = "pb_not_this_one"
203 r.primary.send(wire.FrameChangeDiff, stale)
204 none("another publication")
205 // A failed publication's, then Retry's new publication: the old one is
206 // stale, the new one is accepted.
207 w.send(wire.FrameStep, workerDone(d, cand, wire.OutcomeCommitted))
208 if !r.primary.leasePublication(d.StepID, d.Attempt, pub.Publication) {
209 t.Fatal("publication refused")
210 }
211 r.primary.send(wire.FramePublished, wire.PublishedReport{Publication: pub.Publication, TaskID: task, StepID: d.StepID, Attempt: d.Attempt, CandidateSHA: cand, Reason: "push_failed"})
212 r.primary.send(wire.FrameChangeDiff, up)
213 none("a failed publication")
214 if _, err := r.client.RetryTask(r.ctx, task); err != nil {
215 t.Fatal(err)
216 }
217 again := r.fromPrimary(wire.FramePublish).(*wire.PublishCommand)
218 r.primary.send(wire.FrameChangeDiff, up)
219 none("a stale publication")
220 r.primary.send(wire.FrameChangeDiff, publicationDiff(again, base, swipeDiff))
221 if n := r.diffRows(d.StepID); n != 1 {
222 t.Fatalf("the publication's primary, for the current publication: %d rows", n)
223 }
224}
225
226// diffRowsBy counts a step's stored diff rows uploaded by runner.
227func (r *runnersRig) diffRowsBy(step, runner string) int {
228 r.t.Helper()
229 var n int
230 if err := r.inst.DB.Writer().QueryRow("SELECT count(*) FROM chat_change_diffs WHERE step_id = ? AND runner_id = ?", step, runner).Scan(&n); err != nil {
231 r.t.Fatal(err)
232 }
233 return n
234}
235
236// A publication belongs to the primary it was given to. Replaced by
237// primary B (A removed while still connected), A's pending publication
238// fails, so the person can Retry; nothing B sends for A's publication
239// counts (diffs, its confirmation before the push, its published
240// report), and neither does anything A sends. Retry asks the worker for
241// its commit again, for B, and B's new publication is B's own.
242func TestContractPublicationBoundToItsPrimary(t *testing.T) {
243 r := newMacRig(t)
244 // Work A started, so its removal waits for that proof and it stays
245 // connected, removing.
246 plain := primaryHello(0)
247 plain.Protocol = wire.ProtocolMacEngineering
248 r.primary.heartbeat(plain)
249 started := r.macTask("Linux change")
250 d0 := r.fromPrimary(wire.FrameDispatch).(*wire.DispatchCommand)
251 if d0.TaskID != started || !r.primary.lease(d0.StepID, d0.Attempt) {
252 t.Fatalf("plain engineering %+v", d0)
253 }
254 mac := primaryHello(0)
255 mac.Protocol = wire.ProtocolMacEngineering
256 mac.Catalog[0].NeedsMacOS = true
257 r.primary.heartbeat(mac)
258 w := r.newMacEngineer("Mac A", wire.EngineeringReady)
259 base, cand := sha(1), sha(0xd5)
260 task, d := r.macDispatch(w, "iOS replaced primary", base)
261 pub := r.committedAs(w, d, cand, wire.OutcomeCommitted, true)
262 a := r.primary
263 a.send(wire.FrameChangeDiff, publicationDiff(pub, base, swipeDiff))
264 if n := r.diffRowsBy(d.StepID, a.fr.Welcome.RunnerID); n != 1 {
265 t.Fatalf("A's upload for its own publication: %d rows", n)
266 }
267 r.remove(a.fr.Welcome.RunnerID, false)
268 token := r.inst.EnrolRunner(t).Token
269 h := primaryHello(0)
270 h.Protocol, h.Label = wire.ProtocolMacEngineering, "docs-t2"
271 h.Catalog[0].NeedsMacOS = true
272 np, err := DialRunnerHello(r.ctx, r.inst.URL, token, h)
273 if err != nil || np.Welcome.Role != wire.RolePrimary {
274 t.Fatalf("replacement %+v %v", np, err)
275 }
276 t.Cleanup(np.Close)
277 b := &peer{t: t, r: r.toolRig, fr: np}
278 bID := np.Welcome.RunnerID
279 if s := r.task(task).Steps[0]; s.Publication != wire.PublicationFailed || s.Pushed || r.needsYou(task) != wire.NeedsRetry {
280 t.Fatalf("A's publication after B took over: %+v needs %q", s, r.needsYou(task))
281 }
282 // Each guard on its own: A's publication as if it were still pending,
283 // and without A's row (which a different uploader collides with).
284 if _, err := r.inst.DB.Writer().Exec("UPDATE chat_steps SET publication = ? WHERE id = ?", wire.PublicationPending, d.StepID); err != nil {
285 t.Fatal(err)
286 }
287 if _, err := r.inst.DB.Writer().Exec("DELETE FROM chat_change_diffs WHERE step_id = ?", d.StepID); err != nil {
288 t.Fatal(err)
289 }
290 b.send(wire.FrameChangeDiff, publicationDiff(pub, base, swipeDiff))
291 if n := r.diffRowsBy(d.StepID, bID); n != 0 {
292 t.Fatalf("B's upload for A's publication: %d rows", n)
293 }
294 if b.leasePublication(d.StepID, d.Attempt, pub.Publication) {
295 t.Fatal("B confirmed A's publication")
296 }
297 b.send(wire.FramePublished, publishedWith(pub, base, swipeDiff))
298 if s := r.task(task).Steps[0]; s.Pushed || s.Publication != wire.PublicationPending || s.ChangeFiles != nil {
299 t.Fatalf("B's published report for A's publication counted: %+v", s)
300 }
301 a.send(wire.FrameChangeDiff, publicationDiff(pub, base, swipeDiff))
302 if a.leasePublication(d.StepID, d.Attempt, pub.Publication) {
303 t.Fatal("the replaced primary confirmed its publication")
304 }
305 if _, err := r.inst.DB.Writer().Exec("UPDATE chat_steps SET publication = ? WHERE id = ?", wire.PublicationFailed, d.StepID); err != nil {
306 t.Fatal(err)
307 }
308 // Retry: the worker uploads its commit again, to B, and B publishes.
309 if _, err := r.client.RetryTask(r.ctx, task); err != nil {
310 t.Fatal(err)
311 }
312 _, body := w.expect(wire.FrameSendPack)
313 tr := body.(*wire.SendPackCommand).Transfer
314 if tr.Direction != wire.TransferOutbound || tr.CandidateSHA != cand || tr.BaseSHA != base || tr.Attempt != d.Attempt {
315 t.Fatalf("outbound again %+v", tr)
316 }
317 w.send(wire.FrameBlob, chunk(tr, []byte("PACK commit")))
318 b.expect(wire.FrameBlob)
319 _, body = b.expect(wire.FramePublish)
320 again := body.(*wire.PublishCommand)
321 if again.Publication == pub.Publication || again.Transfer != tr.ID || again.CandidateSHA != cand {
322 t.Fatalf("B's publication %+v", again)
323 }
324 b.send(wire.FrameChangeDiff, publicationDiff(again, base, swipeDiff))
325 if n, m := r.diffRowsBy(d.StepID, bID), r.diffRowsBy(d.StepID, a.fr.Welcome.RunnerID); n != 1 || m != 0 {
326 t.Fatalf("after B's publication: B %d rows, A %d", n, m)
327 }
328 if !b.leasePublication(d.StepID, d.Attempt, again.Publication) {
329 t.Fatal("B's own publication refused")
330 }
331 b.send(wire.FramePublished, publishedWith(again, base, swipeDiff))
332 r.shown(task, base, cand, swipeDiff)
333}
334
335// Slice F fields read from a runner count only at protocol 37. A worker
336// that reconnected below it has its Engineering reports refused whole,
337// before anything changes, and unacked: it keeps them in its outbox
338// (task 16 holds them there below 37) and they count once it is back at
339// 37. A primary below 37 cannot set change files, a diff collection or a
340// publication's diffs.
341func TestContractSliceFFieldsBelow37(t *testing.T) {
342 for _, protocol := range []int{36, wire.ProtocolMacEngineering} {
343 t.Run(itoa(protocol), func(t *testing.T) {
344 r := newMacRig(t)
345 w := r.newMacEngineer("Mac A", wire.EngineeringReady)
346 base, cand := sha(1), sha(0xf1)
347 task, d := r.macDispatch(w, "iOS fields", base)
348 if !w.lease(d.StepID, d.Attempt) {
349 t.Fatal("start refused")
350 }
351 // The worker comes back at protocol.
352 c := r.creds[w.fr]
353 reconnect := func(protocol int) *peer {
354 wh := macEngineerHello(c[1], wire.EngineeringReady, protocol, false)
355 wh.Generation = w.fr.Welcome.Generation
356 w.fr.Close()
357 fr, err := DialRunnerHello(r.ctx, r.inst.URL, c[0], wh)
358 if err != nil || fr.Welcome.Protocol != protocol {
359 t.Fatalf("worker at %d: %+v %v", protocol, fr, err)
360 }
361 t.Cleanup(fr.Close)
362 r.creds[fr] = c
363 return &peer{t: t, r: r.toolRig, fr: fr}
364 }
365 w = reconnect(protocol)
366 // Raw frames carrying the slice F keys, whatever Encode would do.
367 committed := wire.StepReport{TaskID: task, StepID: d.StepID, Attempt: d.Attempt, State: wire.StepRunning, CandidateSHA: cand, Phase: wire.OutcomeCommitted, OutcomePhase: wire.OutcomeUnfinished, XcodeAccess: wire.XcodeAccessSandboxed, SessionID: "sess-1", Findings: []wire.Finding{}}
368 done := workerDone(d, cand, wire.OutcomeUnfinished)
369 if protocol < wire.ProtocolMacEngineering {
370 for i, rep := range []wire.StepReport{committed, done} {
371 if w.sendHeld("held-"+itoa(i), wire.FrameStep, rep) {
372 t.Fatalf("at %d the worker's Engineering report %d was acked", protocol, i)
373 }
374 }
375 if s := r.engineeringStep(task); s.CandidateSHA != "" || wire.StepTerminal(s.State) || s.Terminated {
376 t.Fatalf("at %d the step changed: %+v", protocol, s)
377 }
378 var n int
379 if err := r.inst.DB.Writer().QueryRow("SELECT count(*) FROM runner_transfers WHERE step_id = ? AND direction = ?", d.StepID, wire.TransferOutbound).Scan(&n); err != nil || n != 0 {
380 t.Fatalf("at %d an outbound transfer opened: %d %v", protocol, n, err)
381 }
382 w.none(wire.FrameSendPack, 50*time.Millisecond)
383 // Back at 37, its outbox sends them again.
384 w = reconnect(wire.ProtocolMacEngineering)
385 w.sendAs("held-0", wire.FrameStep, committed)
386 } else {
387 w.send(wire.FrameStep, committed)
388 }
389 _, body := w.expect(wire.FrameSendPack)
390 h := body.(*wire.SendPackCommand).Transfer
391 if protocol < wire.ProtocolMacEngineering {
392 w.sendAs("held-1", wire.FrameStep, done)
393 } else {
394 w.send(wire.FrameStep, done)
395 }
396 w.send(wire.FrameBlob, chunk(h, []byte("PACK commit")))
397 r.fromPrimary(wire.FrameBlob)
398 pub := r.fromPrimary(wire.FramePublish).(*wire.PublishCommand)
399 if pub.OutcomePhase != wire.OutcomeUnfinished {
400 t.Fatalf("at %d the outcome is %q", protocol, pub.OutcomePhase)
401 }
402 // The primary comes back at protocol.
403 ph := primaryHello(r.primary.fr.Welcome.Generation)
404 ph.Protocol = protocol
405 ph.Catalog[0].NeedsMacOS = true
406 r.primary.fr.Close()
407 p, err := DialRunnerHello(r.ctx, r.inst.URL, r.rig.runner, ph)
408 if err != nil || p.Welcome.Protocol != protocol {
409 t.Fatalf("primary at %d: %+v %v", protocol, p, err)
410 }
411 t.Cleanup(p.Close)
412 r.runner, r.primary, r.held = p, &peer{t: t, r: r.toolRig, fr: p}, nil
413 r.primary.send(wire.FrameChangeDiff, publicationDiff(pub, base, swipeDiff))
414 rows := r.diffRows(d.StepID)
415 r.primary.send(wire.FramePublished, publishedWith(pub, base, swipeDiff))
416 s := r.task(task).Steps[0]
417 if !s.Pushed || s.Reason != "step_timeout" {
418 t.Fatalf("at %d not published as unfinished: %+v", protocol, s)
419 }
420 if protocol < wire.ProtocolMacEngineering {
421 if rows != 0 || s.ChangeFiles != nil {
422 t.Fatalf("at %d slice F fields counted: %d rows, files %+v", protocol, rows, s.ChangeFiles)
423 }
424 return
425 }
426 if rows != 1 {
427 t.Fatalf("at 37: %d rows", rows)
428 }
429 r.shown(task, base, cand, swipeDiff)
430 })
431 }
432}
433
434// The files and diffs the primary published are kept whichever of the
435// worker's done and the published report comes first, and after a
436// restart; the worker's own measurement never replaces or clears them.
437func TestContractPublishedFilesSurviveDone(t *testing.T) {
438 for _, order := range []string{"done first", "published first"} {
439 t.Run(order, func(t *testing.T) {
440 r := newMacRig(t)
441 w := r.newMacEngineer("Mac A", wire.EngineeringReady)
442 base, cand := sha(1), sha(0xd3)
443 task, d := r.macDispatch(w, "iOS files", base)
444 pub := r.committedAs(w, d, cand, wire.OutcomeCommitted, false)
445 // The worker's own final report names other files and its own
446 // collection: neither counts.
447 done := workerDone(d, cand, wire.OutcomeCommitted)
448 bogus := wire.FileDiff{Path: "Elsewhere.swift", Patch: "@@ -1 +1 @@\n-a\n+b\n"}
449 done.ChangeFiles, done.ChangeDiffs = diffFacts(base, cand, bogus), wire.DiffCollection([]wire.FileDiff{bogus})
450 if order == "done first" {
451 w.send(wire.FrameStep, done)
452 if k := r.task(task); k.Steps[0].ChangeFiles != nil {
453 t.Fatalf("the worker's measurement was kept: %+v", k.Steps[0].ChangeFiles)
454 }
455 }
456 r.primary.send(wire.FrameChangeDiff, publicationDiff(pub, base, swipeDiff))
457 r.primary.send(wire.FramePublished, publishedWith(pub, base, swipeDiff))
458 if order == "published first" {
459 w.send(wire.FrameStep, done)
460 }
461 r.shown(task, base, cand, swipeDiff)
462 r.restartMac(w)
463 r.shown(task, base, cand, swipeDiff)
464 })
465 }
466}
467
468// The worker's outcome survives its publication: unfinished work is
469// step_timeout and a fix round with no shell is shell_unavailable, but
470// only once the primary has published it, and Pushed only ever comes
471// from the primary's published report, through a failed publication and
472// Retry's republish too.
473func TestContractWorkerUnfinishedThroughPublication(t *testing.T) {
474 t.Run("timeout", func(t *testing.T) {
475 r := newMacRig(t)
476 w := r.newMacEngineer("Mac A", wire.EngineeringReady)
477 base, cand := sha(1), sha(0xe1)
478 task, d := r.macDispatch(w, "iOS timeout", base)
479 pub := r.committedAs(w, d, cand, wire.OutcomeUnfinished, false)
480 // The worker saying it pushed its unfinished work is refused.
481 pushed := workerDone(d, cand, wire.OutcomeUnfinished)
482 pushed.Pushed = true
483 w.send(wire.FrameStep, pushed)
484 w.send(wire.FrameStep, workerDone(d, cand, wire.OutcomeUnfinished))
485 if s := r.task(task).Steps[0]; s.Pushed || s.Reason == "step_timeout" || s.State != wire.StepDone {
486 t.Fatalf("before publication %+v", s)
487 }
488 r.primary.send(wire.FramePublished, wire.PublishedReport{Publication: pub.Publication, TaskID: task, StepID: d.StepID, Attempt: d.Attempt, CandidateSHA: cand, Pushed: true, OutcomePhase: wire.OutcomeUnfinished})
489 if s := r.task(task).Steps[0]; !s.Pushed || s.Reason != "step_timeout" || s.Publication != wire.PublicationPublished {
490 t.Fatalf("after publication %+v", s)
491 }
492 })
493 t.Run("published before done", func(t *testing.T) {
494 r := newMacRig(t)
495 w := r.newMacEngineer("Mac A", wire.EngineeringReady)
496 base, cand := sha(1), sha(0xe2)
497 task, d := r.macDispatch(w, "iOS timeout early", base)
498 pub := r.committedAs(w, d, cand, wire.OutcomeUnfinished, false)
499 r.primary.send(wire.FramePublished, wire.PublishedReport{Publication: pub.Publication, TaskID: task, StepID: d.StepID, Attempt: d.Attempt, CandidateSHA: cand, Pushed: true, OutcomePhase: wire.OutcomeUnfinished})
500 w.send(wire.FrameStep, workerDone(d, cand, wire.OutcomeUnfinished))
501 if s := r.task(task).Steps[0]; !s.Pushed || s.Reason != "step_timeout" || s.State != wire.StepDone {
502 t.Fatalf("after both %+v", s)
503 }
504 })
505 t.Run("no shell fix round", func(t *testing.T) {
506 r := newMacRig(t)
507 w := r.newMacEngineer("Mac A", wire.EngineeringReady)
508 base, c1, c2 := sha(1), sha(0xe3), sha(0xe4)
509 task, _ := r.macCandidate(w, "iOS no shell", base, c1)
510 cw, c := r.macChecked(task, w)
511 cw.send(wire.FrameStep, wire.StepReport{TaskID: task, StepID: c.StepID, Attempt: c.Attempt, State: wire.StepDone, CandidateSHA: c1, Checks: wire.ChecksFailed, Gate: "xcodebuild test", ExitCode: exit(65), Output: "FAILED", Termination: true, TerminationKind: wire.TerminationProcessGroup, Findings: []wire.Finding{}})
512 _, body := w.expect(wire.FrameDispatch)
513 fix := body.(*wire.DispatchCommand)
514 if fix.Specialist != wire.SpecialistEngineering || fix.Round != 1 {
515 t.Fatalf("fix round %+v", fix)
516 }
517 r.engineeringTransfer(w, fix)
518 pub := r.committedAs(w, fix, c2, wire.OutcomeNoShell, true)
519 step := func() wire.Step {
520 for _, s := range r.task(task).Steps {
521 if s.ID == fix.StepID {
522 return s
523 }
524 }
525 t.Fatal("no fix round")
526 return wire.Step{}
527 }
528 if s := step(); s.Pushed || s.Reason == wire.ReasonShellUnavailable {
529 t.Fatalf("before publication %+v", s)
530 }
531 r.primary.send(wire.FramePublished, wire.PublishedReport{Publication: pub.Publication, TaskID: task, StepID: fix.StepID, Attempt: fix.Attempt, CandidateSHA: c2, Pushed: true, OutcomePhase: wire.OutcomeNoShell})
532 if s := step(); !s.Pushed || s.Reason != wire.ReasonShellUnavailable {
533 t.Fatalf("after publication %+v", s)
534 }
535 })
536 t.Run("failed publication then republish", func(t *testing.T) {
537 r := newMacRig(t)
538 w := r.newMacEngineer("Mac A", wire.EngineeringReady)
539 base, cand := sha(1), sha(0xe5)
540 task, d := r.macDispatch(w, "iOS timeout again", base)
541 pub := r.committedAs(w, d, cand, wire.OutcomeUnfinished, true)
542 if !r.primary.leasePublication(d.StepID, d.Attempt, pub.Publication) {
543 t.Fatal("publication refused")
544 }
545 r.primary.send(wire.FramePublished, wire.PublishedReport{Publication: pub.Publication, TaskID: task, StepID: d.StepID, Attempt: d.Attempt, CandidateSHA: cand, Reason: "push_failed", OutcomePhase: wire.OutcomeUnfinished})
546 if s := r.task(task).Steps[0]; s.Pushed || s.Reason != "push_failed" {
547 t.Fatalf("failed publication %+v", s)
548 }
549 if _, err := r.client.RetryTask(r.ctx, task); err != nil {
550 t.Fatal(err)
551 }
552 again := r.fromPrimary(wire.FramePublish).(*wire.PublishCommand)
553 if again.Publication == pub.Publication || again.OutcomePhase != wire.OutcomeUnfinished {
554 t.Fatalf("republish %+v", again)
555 }
556 if s := r.task(task).Steps[0]; s.Pushed {
557 t.Fatalf("pushed before the republish %+v", s)
558 }
559 r.primary.send(wire.FramePublished, wire.PublishedReport{Publication: again.Publication, TaskID: task, StepID: d.StepID, Attempt: d.Attempt, CandidateSHA: cand, Pushed: true, OutcomePhase: wire.OutcomeUnfinished})
560 if s := r.task(task).Steps[0]; !s.Pushed || s.Reason != "step_timeout" {
561 t.Fatalf("republished %+v", s)
562 }
563 })
564}
565
566// A pack too large to relay (declared by its chunk count) ends a worker's
567// Engineering attempt workspace_too_large with Needs you, whether or not
568// the worker had started it; nothing reaches the worker and nothing runs
569// on Linux.
570func TestPackOversizeIsWorkspaceTooLarge(t *testing.T) {
571 for _, started := range []bool{false, true} {
572 name := "not started"
573 if started {
574 name = "started"
575 }
576 t.Run(name, func(t *testing.T) {
577 r := newMacRig(t)
578 w := r.newMacEngineer("Mac A", wire.EngineeringReady)
579 task := r.macTask("iOS huge")
580 r.based(r.prepared(task), sha(1))
581 _, body := w.expect(wire.FrameDispatch)
582 d := body.(*wire.DispatchCommand)
583 h := r.engineeringTransfer(w, d)
584 if started && !w.lease(d.StepID, d.Attempt) {
585 t.Fatal("start refused")
586 }
587 big := chunk(h, []byte("PACK huge"))
588 big.Total = wire.MaxTransferChunks + 1
589 r.primary.send(wire.FrameBlob, big)
590 if started {
591 _, body = w.expect(wire.FrameCancelStep)
592 if c := body.(*wire.CancelStepCommand); c.StepID != d.StepID || c.Attempt != d.Attempt {
593 t.Fatalf("cancel %+v", c)
594 }
595 w.send(wire.FrameStep, wire.StepReport{TaskID: task, StepID: d.StepID, Attempt: d.Attempt, State: wire.StepCancelled, Reason: "withdrawn", Termination: true, TerminationKind: wire.TerminationProcessGroup, Findings: []wire.Finding{}})
596 }
597 s := r.engineeringStep(task)
598 if s.State != wire.StepFailed || s.Reason != wire.ReasonWorkspaceTooLarge || !s.Terminated || r.needsYou(task) != wire.NeedsRetry {
599 t.Fatalf("oversize %+v needs %q", s, r.needsYou(task))
600 }
601 if n := r.relayed(w); n != 0 {
602 t.Fatalf("%d chunks reached the worker", n)
603 }
604 if n := r.mailboxHas(r.primary.fr.Welcome.RunnerID, `"specialist":"engineering"`); n != 0 {
605 t.Fatalf("%d engineering dispatches to the Linux primary", n)
606 }
607 w.none(wire.FrameDispatch, 50*time.Millisecond)
608 })
609 }
610}
611