package thintest import ( "encoding/base64" "net/url" "testing" "time" "amadan.net/oficina/equipo/internal/wire" ) // Mac runners slice F, plan task 4: a worker's candidate is measured by // the primary when it publishes. Its change files ride in the published // report and its diffs are uploaded bound to that publication; the // instance accepts them only from that publication's primary, for the // worker's task, step and attempt. The worker's outcome (unfinished, // no-shell) is kept from its committed report through the publication, // and Pushed is only ever the primary's word. // swipeDiff is one file's display diff of a worker's candidate. var swipeDiff = wire.FileDiff{Path: "Sources/Swipe.swift", Patch: "@@ -1 +1 @@\n-let back = false\n+let back = true\n"} // committedAs runs w's Engineering attempt d to a commit whose outcome is // outcome, as workerCommitted does; with done, the worker's final report // (done, not pushed, its reason for that outcome) comes before the pack. func (r *runnersRig) committedAs(w *peer, d *wire.DispatchCommand, cand, outcome string, done bool) *wire.PublishCommand { r.t.Helper() if !w.lease(d.StepID, d.Attempt) { r.t.Fatal("start refused") } w.send(wire.FrameStep, wire.StepReport{TaskID: d.TaskID, StepID: d.StepID, Attempt: d.Attempt, State: wire.StepRunning, BaseSHA: d.BaseSHA, Findings: []wire.Finding{}}) 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{}}) _, body := w.expect(wire.FrameSendPack) h := body.(*wire.SendPackCommand).Transfer if h.Direction != wire.TransferOutbound || h.CandidateSHA != cand { r.t.Fatalf("outbound header %+v", h) } if done { w.send(wire.FrameStep, workerDone(d, cand, outcome)) } w.send(wire.FrameBlob, chunk(h, []byte("PACK commit"))) r.fromPrimary(wire.FrameBlob) pub := r.fromPrimary(wire.FramePublish).(*wire.PublishCommand) if pub.CandidateSHA != cand || pub.Transfer != h.ID || pub.StepID != d.StepID || pub.Attempt != d.Attempt || pub.OutcomePhase != outcome { r.t.Fatalf("publish %+v, want outcome %q", pub, outcome) } return pub } // workerDone is a worker's final report for its commit: done, never // pushed, with the reason its outcome has on the worker. func workerDone(d *wire.DispatchCommand, cand, outcome string) wire.StepReport { reason := "" switch outcome { case wire.OutcomeUnfinished: reason = "step_timeout" case wire.OutcomeNoShell: reason = wire.ReasonShellUnavailable } 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{}} } // publicationDiff is the primary's upload of one display diff bound to // pub. func publicationDiff(pub *wire.PublishCommand, base string, file wire.FileDiff) wire.ChangeDiffUpload { 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))} } func diffFacts(base, cand string, file wire.FileDiff) *wire.ChangeFiles { 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}}} } // publishedWith is the primary's published report for pub with its // measurements of the candidate. func publishedWith(pub *wire.PublishCommand, base string, file wire.FileDiff) wire.PublishedReport { 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} } // diffRows counts the stored diff rows of a step, accepted or not. func (r *runnersRig) diffRows(step string) int { r.t.Helper() var n int if err := r.inst.DB.Writer().QueryRow("SELECT count(*) FROM chat_change_diffs WHERE step_id = ?", step).Scan(&n); err != nil { r.t.Fatal(err) } return n } // diffOf reads a file's diff of the task's candidate as the person's // device does. func (r *runnersRig) diffOf(task, cand, path string) wire.ChangeDiff { r.t.Helper() var out wire.ChangeDiff 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 { r.t.Fatal(err) } return out } // shown: the task shows the primary's measurement of the worker's // candidate, and the file's diff reads as uploaded. func (r *runnersRig) shown(task, base, cand string, file wire.FileDiff) { r.t.Helper() k := r.task(task) f := k.Steps[0].ChangeFiles if f == nil || f.Base != base || f.SHA != cand || len(f.Entries) != 1 || f.Entries[0].Path != file.Path || !k.Steps[0].Pushed { r.t.Fatalf("change files on the task %+v (step %+v)", f, k.Steps[0]) } if out := r.diffOf(task, cand, file.Path); out.Patch != file.Patch || out.StepID != k.Steps[0].ID || out.Reason != "" { r.t.Fatalf("diff %+v", out) } } // restartMac restarts the instance (a deploy) and reconnects the primary // and w, returning w's new connection. func (r *runnersRig) restartMac(w *peer) *peer { r.t.Helper() pgen, wgen := r.primary.fr.Welcome.Generation, w.fr.Welcome.Generation c := r.creds[w.fr] r.primary.fr.Close() w.fr.Close() r.inst.Restart(r.t) h := primaryHello(pgen) h.Protocol = wire.ProtocolMacEngineering h.Catalog[0].NeedsMacOS = true p, err := DialRunnerHello(r.ctx, r.inst.URL, r.rig.runner, h) if err != nil { r.t.Fatal(err) } r.t.Cleanup(p.Close) r.runner, r.primary, r.held = p, &peer{t: r.t, r: r.toolRig, fr: p}, nil r.primary.r = r.toolRig wh := macEngineerHello(c[1], wire.EngineeringReady, wire.ProtocolMacEngineering, false) wh.Generation = wgen fr, err := DialRunnerHello(r.ctx, r.inst.URL, c[0], wh) if err != nil { r.t.Fatal(err) } r.t.Cleanup(fr.Close) r.creds[fr] = c return &peer{t: r.t, r: r.toolRig, fr: fr} } // The primary's diffs of a worker's candidate are accepted for its // publication whether the worker's final report comes before them or // after, and across an instance restart; they are shown once the // published report names their collection. func TestContractPublicationDiffs(t *testing.T) { for _, order := range []string{"uploads before done", "done before uploads", "restart between"} { t.Run(order, func(t *testing.T) { r := newMacRig(t) w := r.newMacEngineer("Mac A", wire.EngineeringReady) base, cand := sha(1), sha(0xd1) task, d := r.macDispatch(w, "iOS diffs", base) pub := r.committedAs(w, d, cand, wire.OutcomeCommitted, order == "done before uploads") r.primary.send(wire.FrameChangeDiff, publicationDiff(pub, base, swipeDiff)) if n := r.diffRows(d.StepID); n != 1 { t.Fatalf("%d diff rows from the publication's primary", n) } if order == "restart between" { w = r.restartMac(w) } r.primary.send(wire.FramePublished, publishedWith(pub, base, swipeDiff)) if order != "done before uploads" { w.send(wire.FrameStep, workerDone(d, cand, wire.OutcomeCommitted)) } r.shown(task, base, cand, swipeDiff) }) } } // Diffs that name a publication are refused from anyone but that // publication's primary (here the worker), for another attempt, for a // publication that is not the step's own or that failed; a worker's own // diffs are never kept. Retry's new publication is accepted. func TestContractPublicationDiffsRefused(t *testing.T) { r := newMacRig(t) w := r.newMacEngineer("Mac A", wire.EngineeringReady) base, cand := sha(1), sha(0xd2) task, d := r.macDispatch(w, "iOS refused diffs", base) pub := r.committedAs(w, d, cand, wire.OutcomeCommitted, false) up := publicationDiff(pub, base, swipeDiff) none := func(why string) { t.Helper() if n := r.diffRows(d.StepID); n != 0 { t.Fatalf("%s: %d diff rows kept", why, n) } } // The worker, naming the publication, and on its own. w.send(wire.FrameChangeDiff, up) none("from the worker") own := up own.Publication = "" w.send(wire.FrameChangeDiff, own) none("the worker's own diffs") // The primary, for another attempt or another publication. wrong := up wrong.Attempt = d.Attempt + 1 r.primary.send(wire.FrameChangeDiff, wrong) none("another attempt") stale := up stale.Publication = "pb_not_this_one" r.primary.send(wire.FrameChangeDiff, stale) none("another publication") // A failed publication's, then Retry's new publication: the old one is // stale, the new one is accepted. w.send(wire.FrameStep, workerDone(d, cand, wire.OutcomeCommitted)) if !r.primary.leasePublication(d.StepID, d.Attempt, pub.Publication) { t.Fatal("publication refused") } r.primary.send(wire.FramePublished, wire.PublishedReport{Publication: pub.Publication, TaskID: task, StepID: d.StepID, Attempt: d.Attempt, CandidateSHA: cand, Reason: "push_failed"}) r.primary.send(wire.FrameChangeDiff, up) none("a failed publication") if _, err := r.client.RetryTask(r.ctx, task); err != nil { t.Fatal(err) } again := r.fromPrimary(wire.FramePublish).(*wire.PublishCommand) r.primary.send(wire.FrameChangeDiff, up) none("a stale publication") r.primary.send(wire.FrameChangeDiff, publicationDiff(again, base, swipeDiff)) if n := r.diffRows(d.StepID); n != 1 { t.Fatalf("the publication's primary, for the current publication: %d rows", n) } } // diffRowsBy counts a step's stored diff rows uploaded by runner. func (r *runnersRig) diffRowsBy(step, runner string) int { r.t.Helper() var n int 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 { r.t.Fatal(err) } return n } // A publication belongs to the primary it was given to. Replaced by // primary B (A removed while still connected), A's pending publication // fails, so the person can Retry; nothing B sends for A's publication // counts (diffs, its confirmation before the push, its published // report), and neither does anything A sends. Retry asks the worker for // its commit again, for B, and B's new publication is B's own. func TestContractPublicationBoundToItsPrimary(t *testing.T) { r := newMacRig(t) // Work A started, so its removal waits for that proof and it stays // connected, removing. plain := primaryHello(0) plain.Protocol = wire.ProtocolMacEngineering r.primary.heartbeat(plain) started := r.macTask("Linux change") d0 := r.fromPrimary(wire.FrameDispatch).(*wire.DispatchCommand) if d0.TaskID != started || !r.primary.lease(d0.StepID, d0.Attempt) { t.Fatalf("plain engineering %+v", d0) } mac := primaryHello(0) mac.Protocol = wire.ProtocolMacEngineering mac.Catalog[0].NeedsMacOS = true r.primary.heartbeat(mac) w := r.newMacEngineer("Mac A", wire.EngineeringReady) base, cand := sha(1), sha(0xd5) task, d := r.macDispatch(w, "iOS replaced primary", base) pub := r.committedAs(w, d, cand, wire.OutcomeCommitted, true) a := r.primary a.send(wire.FrameChangeDiff, publicationDiff(pub, base, swipeDiff)) if n := r.diffRowsBy(d.StepID, a.fr.Welcome.RunnerID); n != 1 { t.Fatalf("A's upload for its own publication: %d rows", n) } r.remove(a.fr.Welcome.RunnerID, false) token := r.inst.EnrolRunner(t).Token h := primaryHello(0) h.Protocol, h.Label = wire.ProtocolMacEngineering, "docs-t2" h.Catalog[0].NeedsMacOS = true np, err := DialRunnerHello(r.ctx, r.inst.URL, token, h) if err != nil || np.Welcome.Role != wire.RolePrimary { t.Fatalf("replacement %+v %v", np, err) } t.Cleanup(np.Close) b := &peer{t: t, r: r.toolRig, fr: np} bID := np.Welcome.RunnerID if s := r.task(task).Steps[0]; s.Publication != wire.PublicationFailed || s.Pushed || r.needsYou(task) != wire.NeedsRetry { t.Fatalf("A's publication after B took over: %+v needs %q", s, r.needsYou(task)) } // Each guard on its own: A's publication as if it were still pending, // and without A's row (which a different uploader collides with). if _, err := r.inst.DB.Writer().Exec("UPDATE chat_steps SET publication = ? WHERE id = ?", wire.PublicationPending, d.StepID); err != nil { t.Fatal(err) } if _, err := r.inst.DB.Writer().Exec("DELETE FROM chat_change_diffs WHERE step_id = ?", d.StepID); err != nil { t.Fatal(err) } b.send(wire.FrameChangeDiff, publicationDiff(pub, base, swipeDiff)) if n := r.diffRowsBy(d.StepID, bID); n != 0 { t.Fatalf("B's upload for A's publication: %d rows", n) } if b.leasePublication(d.StepID, d.Attempt, pub.Publication) { t.Fatal("B confirmed A's publication") } b.send(wire.FramePublished, publishedWith(pub, base, swipeDiff)) if s := r.task(task).Steps[0]; s.Pushed || s.Publication != wire.PublicationPending || s.ChangeFiles != nil { t.Fatalf("B's published report for A's publication counted: %+v", s) } a.send(wire.FrameChangeDiff, publicationDiff(pub, base, swipeDiff)) if a.leasePublication(d.StepID, d.Attempt, pub.Publication) { t.Fatal("the replaced primary confirmed its publication") } if _, err := r.inst.DB.Writer().Exec("UPDATE chat_steps SET publication = ? WHERE id = ?", wire.PublicationFailed, d.StepID); err != nil { t.Fatal(err) } // Retry: the worker uploads its commit again, to B, and B publishes. if _, err := r.client.RetryTask(r.ctx, task); err != nil { t.Fatal(err) } _, body := w.expect(wire.FrameSendPack) tr := body.(*wire.SendPackCommand).Transfer if tr.Direction != wire.TransferOutbound || tr.CandidateSHA != cand || tr.BaseSHA != base || tr.Attempt != d.Attempt { t.Fatalf("outbound again %+v", tr) } w.send(wire.FrameBlob, chunk(tr, []byte("PACK commit"))) b.expect(wire.FrameBlob) _, body = b.expect(wire.FramePublish) again := body.(*wire.PublishCommand) if again.Publication == pub.Publication || again.Transfer != tr.ID || again.CandidateSHA != cand { t.Fatalf("B's publication %+v", again) } b.send(wire.FrameChangeDiff, publicationDiff(again, base, swipeDiff)) if n, m := r.diffRowsBy(d.StepID, bID), r.diffRowsBy(d.StepID, a.fr.Welcome.RunnerID); n != 1 || m != 0 { t.Fatalf("after B's publication: B %d rows, A %d", n, m) } if !b.leasePublication(d.StepID, d.Attempt, again.Publication) { t.Fatal("B's own publication refused") } b.send(wire.FramePublished, publishedWith(again, base, swipeDiff)) r.shown(task, base, cand, swipeDiff) } // Slice F fields read from a runner count only at protocol 37. A worker // that reconnected below it has its Engineering reports refused whole, // before anything changes, and unacked: it keeps them in its outbox // (task 16 holds them there below 37) and they count once it is back at // 37. A primary below 37 cannot set change files, a diff collection or a // publication's diffs. func TestContractSliceFFieldsBelow37(t *testing.T) { for _, protocol := range []int{36, wire.ProtocolMacEngineering} { t.Run(itoa(protocol), func(t *testing.T) { r := newMacRig(t) w := r.newMacEngineer("Mac A", wire.EngineeringReady) base, cand := sha(1), sha(0xf1) task, d := r.macDispatch(w, "iOS fields", base) if !w.lease(d.StepID, d.Attempt) { t.Fatal("start refused") } // The worker comes back at protocol. c := r.creds[w.fr] reconnect := func(protocol int) *peer { wh := macEngineerHello(c[1], wire.EngineeringReady, protocol, false) wh.Generation = w.fr.Welcome.Generation w.fr.Close() fr, err := DialRunnerHello(r.ctx, r.inst.URL, c[0], wh) if err != nil || fr.Welcome.Protocol != protocol { t.Fatalf("worker at %d: %+v %v", protocol, fr, err) } t.Cleanup(fr.Close) r.creds[fr] = c return &peer{t: t, r: r.toolRig, fr: fr} } w = reconnect(protocol) // Raw frames carrying the slice F keys, whatever Encode would do. 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{}} done := workerDone(d, cand, wire.OutcomeUnfinished) if protocol < wire.ProtocolMacEngineering { for i, rep := range []wire.StepReport{committed, done} { if w.sendHeld("held-"+itoa(i), wire.FrameStep, rep) { t.Fatalf("at %d the worker's Engineering report %d was acked", protocol, i) } } if s := r.engineeringStep(task); s.CandidateSHA != "" || wire.StepTerminal(s.State) || s.Terminated { t.Fatalf("at %d the step changed: %+v", protocol, s) } var n int 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 { t.Fatalf("at %d an outbound transfer opened: %d %v", protocol, n, err) } w.none(wire.FrameSendPack, 50*time.Millisecond) // Back at 37, its outbox sends them again. w = reconnect(wire.ProtocolMacEngineering) w.sendAs("held-0", wire.FrameStep, committed) } else { w.send(wire.FrameStep, committed) } _, body := w.expect(wire.FrameSendPack) h := body.(*wire.SendPackCommand).Transfer if protocol < wire.ProtocolMacEngineering { w.sendAs("held-1", wire.FrameStep, done) } else { w.send(wire.FrameStep, done) } w.send(wire.FrameBlob, chunk(h, []byte("PACK commit"))) r.fromPrimary(wire.FrameBlob) pub := r.fromPrimary(wire.FramePublish).(*wire.PublishCommand) if pub.OutcomePhase != wire.OutcomeUnfinished { t.Fatalf("at %d the outcome is %q", protocol, pub.OutcomePhase) } // The primary comes back at protocol. ph := primaryHello(r.primary.fr.Welcome.Generation) ph.Protocol = protocol ph.Catalog[0].NeedsMacOS = true r.primary.fr.Close() p, err := DialRunnerHello(r.ctx, r.inst.URL, r.rig.runner, ph) if err != nil || p.Welcome.Protocol != protocol { t.Fatalf("primary at %d: %+v %v", protocol, p, err) } t.Cleanup(p.Close) r.runner, r.primary, r.held = p, &peer{t: t, r: r.toolRig, fr: p}, nil r.primary.send(wire.FrameChangeDiff, publicationDiff(pub, base, swipeDiff)) rows := r.diffRows(d.StepID) r.primary.send(wire.FramePublished, publishedWith(pub, base, swipeDiff)) s := r.task(task).Steps[0] if !s.Pushed || s.Reason != "step_timeout" { t.Fatalf("at %d not published as unfinished: %+v", protocol, s) } if protocol < wire.ProtocolMacEngineering { if rows != 0 || s.ChangeFiles != nil { t.Fatalf("at %d slice F fields counted: %d rows, files %+v", protocol, rows, s.ChangeFiles) } return } if rows != 1 { t.Fatalf("at 37: %d rows", rows) } r.shown(task, base, cand, swipeDiff) }) } } // The files and diffs the primary published are kept whichever of the // worker's done and the published report comes first, and after a // restart; the worker's own measurement never replaces or clears them. func TestContractPublishedFilesSurviveDone(t *testing.T) { for _, order := range []string{"done first", "published first"} { t.Run(order, func(t *testing.T) { r := newMacRig(t) w := r.newMacEngineer("Mac A", wire.EngineeringReady) base, cand := sha(1), sha(0xd3) task, d := r.macDispatch(w, "iOS files", base) pub := r.committedAs(w, d, cand, wire.OutcomeCommitted, false) // The worker's own final report names other files and its own // collection: neither counts. done := workerDone(d, cand, wire.OutcomeCommitted) bogus := wire.FileDiff{Path: "Elsewhere.swift", Patch: "@@ -1 +1 @@\n-a\n+b\n"} done.ChangeFiles, done.ChangeDiffs = diffFacts(base, cand, bogus), wire.DiffCollection([]wire.FileDiff{bogus}) if order == "done first" { w.send(wire.FrameStep, done) if k := r.task(task); k.Steps[0].ChangeFiles != nil { t.Fatalf("the worker's measurement was kept: %+v", k.Steps[0].ChangeFiles) } } r.primary.send(wire.FrameChangeDiff, publicationDiff(pub, base, swipeDiff)) r.primary.send(wire.FramePublished, publishedWith(pub, base, swipeDiff)) if order == "published first" { w.send(wire.FrameStep, done) } r.shown(task, base, cand, swipeDiff) r.restartMac(w) r.shown(task, base, cand, swipeDiff) }) } } // The worker's outcome survives its publication: unfinished work is // step_timeout and a fix round with no shell is shell_unavailable, but // only once the primary has published it, and Pushed only ever comes // from the primary's published report, through a failed publication and // Retry's republish too. func TestContractWorkerUnfinishedThroughPublication(t *testing.T) { t.Run("timeout", func(t *testing.T) { r := newMacRig(t) w := r.newMacEngineer("Mac A", wire.EngineeringReady) base, cand := sha(1), sha(0xe1) task, d := r.macDispatch(w, "iOS timeout", base) pub := r.committedAs(w, d, cand, wire.OutcomeUnfinished, false) // The worker saying it pushed its unfinished work is refused. pushed := workerDone(d, cand, wire.OutcomeUnfinished) pushed.Pushed = true w.send(wire.FrameStep, pushed) w.send(wire.FrameStep, workerDone(d, cand, wire.OutcomeUnfinished)) if s := r.task(task).Steps[0]; s.Pushed || s.Reason == "step_timeout" || s.State != wire.StepDone { t.Fatalf("before publication %+v", s) } 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}) if s := r.task(task).Steps[0]; !s.Pushed || s.Reason != "step_timeout" || s.Publication != wire.PublicationPublished { t.Fatalf("after publication %+v", s) } }) t.Run("published before done", func(t *testing.T) { r := newMacRig(t) w := r.newMacEngineer("Mac A", wire.EngineeringReady) base, cand := sha(1), sha(0xe2) task, d := r.macDispatch(w, "iOS timeout early", base) pub := r.committedAs(w, d, cand, wire.OutcomeUnfinished, false) 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}) w.send(wire.FrameStep, workerDone(d, cand, wire.OutcomeUnfinished)) if s := r.task(task).Steps[0]; !s.Pushed || s.Reason != "step_timeout" || s.State != wire.StepDone { t.Fatalf("after both %+v", s) } }) t.Run("no shell fix round", func(t *testing.T) { r := newMacRig(t) w := r.newMacEngineer("Mac A", wire.EngineeringReady) base, c1, c2 := sha(1), sha(0xe3), sha(0xe4) task, _ := r.macCandidate(w, "iOS no shell", base, c1) cw, c := r.macChecked(task, w) 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{}}) _, body := w.expect(wire.FrameDispatch) fix := body.(*wire.DispatchCommand) if fix.Specialist != wire.SpecialistEngineering || fix.Round != 1 { t.Fatalf("fix round %+v", fix) } r.engineeringTransfer(w, fix) pub := r.committedAs(w, fix, c2, wire.OutcomeNoShell, true) step := func() wire.Step { for _, s := range r.task(task).Steps { if s.ID == fix.StepID { return s } } t.Fatal("no fix round") return wire.Step{} } if s := step(); s.Pushed || s.Reason == wire.ReasonShellUnavailable { t.Fatalf("before publication %+v", s) } 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}) if s := step(); !s.Pushed || s.Reason != wire.ReasonShellUnavailable { t.Fatalf("after publication %+v", s) } }) t.Run("failed publication then republish", func(t *testing.T) { r := newMacRig(t) w := r.newMacEngineer("Mac A", wire.EngineeringReady) base, cand := sha(1), sha(0xe5) task, d := r.macDispatch(w, "iOS timeout again", base) pub := r.committedAs(w, d, cand, wire.OutcomeUnfinished, true) if !r.primary.leasePublication(d.StepID, d.Attempt, pub.Publication) { t.Fatal("publication refused") } 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}) if s := r.task(task).Steps[0]; s.Pushed || s.Reason != "push_failed" { t.Fatalf("failed publication %+v", s) } if _, err := r.client.RetryTask(r.ctx, task); err != nil { t.Fatal(err) } again := r.fromPrimary(wire.FramePublish).(*wire.PublishCommand) if again.Publication == pub.Publication || again.OutcomePhase != wire.OutcomeUnfinished { t.Fatalf("republish %+v", again) } if s := r.task(task).Steps[0]; s.Pushed { t.Fatalf("pushed before the republish %+v", s) } 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}) if s := r.task(task).Steps[0]; !s.Pushed || s.Reason != "step_timeout" { t.Fatalf("republished %+v", s) } }) } // A pack too large to relay (declared by its chunk count) ends a worker's // Engineering attempt workspace_too_large with Needs you, whether or not // the worker had started it; nothing reaches the worker and nothing runs // on Linux. func TestPackOversizeIsWorkspaceTooLarge(t *testing.T) { for _, started := range []bool{false, true} { name := "not started" if started { name = "started" } t.Run(name, func(t *testing.T) { r := newMacRig(t) w := r.newMacEngineer("Mac A", wire.EngineeringReady) task := r.macTask("iOS huge") r.based(r.prepared(task), sha(1)) _, body := w.expect(wire.FrameDispatch) d := body.(*wire.DispatchCommand) h := r.engineeringTransfer(w, d) if started && !w.lease(d.StepID, d.Attempt) { t.Fatal("start refused") } big := chunk(h, []byte("PACK huge")) big.Total = wire.MaxTransferChunks + 1 r.primary.send(wire.FrameBlob, big) if started { _, body = w.expect(wire.FrameCancelStep) if c := body.(*wire.CancelStepCommand); c.StepID != d.StepID || c.Attempt != d.Attempt { t.Fatalf("cancel %+v", c) } 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{}}) } s := r.engineeringStep(task) if s.State != wire.StepFailed || s.Reason != wire.ReasonWorkspaceTooLarge || !s.Terminated || r.needsYou(task) != wire.NeedsRetry { t.Fatalf("oversize %+v needs %q", s, r.needsYou(task)) } if n := r.relayed(w); n != 0 { t.Fatalf("%d chunks reached the worker", n) } if n := r.mailboxHas(r.primary.fr.Welcome.RunnerID, `"specialist":"engineering"`); n != 0 { t.Fatalf("%d engineering dispatches to the Linux primary", n) } w.none(wire.FrameDispatch, 50*time.Millisecond) }) } }