| new file mode 100644 |
| index 00000000..8fad4a6f |
| --- /dev/null |
| +++ b/internal/thintest/wbfix1_publication_contract_test.go |
| @@ -0,0 +1,145 @@ |
| +package thintest |
| + |
| +import ( |
| + "crypto/sha256" |
| + "encoding/base64" |
| + "encoding/hex" |
| + "testing" |
| + "time" |
| + |
| + "amadan.net/oficina/equipo/internal/wire" |
| +) |
| + |
| +// Whole-branch fix 1, #4: a worker's commit is published only by the |
| +// primary its pack went to. A primary replaced while the outbound |
| +// transfer is open (or once it completed with no primary to publish) |
| +// has that transfer closed as superseded, and the worker is asked for |
| +// its retained candidate again, to the new primary. |
| + |
| +// chunksOf is pack split into n chunks of h. |
| +func chunksOf(h wire.TransferHeader, pack []byte, n int) []wire.Blob { |
| + sum := sha256.Sum256(pack) |
| + var out []wire.Blob |
| + for i := range n { |
| + part := pack[i*len(pack)/n : (i+1)*len(pack)/n] |
| + out = append(out, wire.Blob{Transfer: h, Index: i, Total: n, SHA256: hex.EncodeToString(sum[:]), Data: base64.StdEncoding.EncodeToString(part)}) |
| + } |
| + return out |
| +} |
| + |
| +func (r *runnersRig) transferTo(id string) (to, state string) { |
| + r.t.Helper() |
| + if err := r.inst.DB.Writer().QueryRow("SELECT to_runner, state FROM runner_transfers WHERE id = ?", id).Scan(&to, &state); err != nil { |
| + r.t.Fatal(err) |
| + } |
| + return to, state |
| +} |
| + |
| +func TestContractPublicationPrimaryReplacedMidTransfer(t *testing.T) { |
| + for _, when := range []string{"open", "complete"} { |
| + t.Run(when, func(t *testing.T) { |
| + r := newMacRig(t) |
| + w := r.newMacEngineer("Mac A", wire.EngineeringReady) |
| + base, cand := sha(1), sha(0xa4) |
| + task, d := r.macDispatch(w, "iOS primary replaced", base) |
| + if !w.lease(d.StepID, d.Attempt) { |
| + t.Fatal("start refused") |
| + } |
| + w.send(wire.FrameStep, wire.StepReport{TaskID: task, StepID: d.StepID, Attempt: d.Attempt, State: wire.StepRunning, CandidateSHA: cand, Phase: wire.OutcomeCommitted, OutcomePhase: wire.OutcomeCommitted, Findings: []wire.Finding{}}) |
| + _, body := w.expect(wire.FrameSendPack) |
| + old := body.(*wire.SendPackCommand).Transfer |
| + a := r.primary.fr.Welcome.RunnerID |
| + chunks := chunksOf(old, []byte("PACK commit, two chunks"), 2) |
| + w.send(wire.FrameBlob, chunks[0]) |
| + r.remove(a, false) |
| + if when == "complete" { |
| + // The last chunk lands with no primary to publish it. |
| + w.send(wire.FrameBlob, chunks[1]) |
| + } |
| + 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 |
| + // The open transfer to A is superseded; the worker is asked again, |
| + // for B. |
| + if to, state := r.transferTo(old.ID); to != a || state != "closed" { |
| + t.Fatalf("A's transfer: to %s, %s", to, state) |
| + } |
| + _, body = w.expect(wire.FrameSendPack) |
| + again := body.(*wire.SendPackCommand).Transfer |
| + if again.ID == old.ID || again.Direction != wire.TransferOutbound || again.CandidateSHA != cand || again.BaseSHA != base || again.Attempt != d.Attempt { |
| + t.Fatalf("outbound again %+v", again) |
| + } |
| + if to, state := r.transferTo(again.ID); to != bID || state != "open" { |
| + t.Fatalf("the new transfer: to %s, %s", to, state) |
| + } |
| + if when == "open" { |
| + // A's last chunk now has nowhere to go. |
| + w.send(wire.FrameBlob, chunks[1]) |
| + b.none(wire.FramePublish, 50*time.Millisecond) |
| + } |
| + w.send(wire.FrameStep, workerDone(d, cand, wire.OutcomeCommitted)) |
| + w.send(wire.FrameBlob, chunk(again, []byte("PACK commit"))) |
| + b.expect(wire.FrameBlob) |
| + _, body = b.expect(wire.FramePublish) |
| + pub := body.(*wire.PublishCommand) |
| + if pub.Transfer != again.ID || pub.CandidateSHA != cand { |
| + t.Fatalf("B's publication %+v", pub) |
| + } |
| + if !b.leasePublication(d.StepID, d.Attempt, pub.Publication) { |
| + t.Fatal("B's own publication refused") |
| + } |
| + b.send(wire.FramePublished, publishedWith(pub, base, swipeDiff)) |
| + if s := r.task(task).Steps[0]; !s.Pushed || s.Publication != wire.PublicationPublished { |
| + t.Fatalf("after B published %+v", s) |
| + } |
| + }) |
| + } |
| +} |
| + |
| +// A transfer that completes while it names a primary since replaced |
| +// (the new one enrolled, not yet connected) is never published by the |
| +// new primary: the worker is asked again, for it. |
| +func TestContractPublicationNeedsItsTransfersPrimary(t *testing.T) { |
| + r := newMacRig(t) |
| + w := r.newMacEngineer("Mac A", wire.EngineeringReady) |
| + base, cand := sha(1), sha(0xa5) |
| + task, d := r.macDispatch(w, "iOS primary enrolled", base) |
| + if !w.lease(d.StepID, d.Attempt) { |
| + t.Fatal("start refused") |
| + } |
| + w.send(wire.FrameStep, wire.StepReport{TaskID: task, StepID: d.StepID, Attempt: d.Attempt, State: wire.StepRunning, CandidateSHA: cand, Phase: wire.OutcomeCommitted, OutcomePhase: wire.OutcomeCommitted, Findings: []wire.Finding{}}) |
| + _, body := w.expect(wire.FrameSendPack) |
| + old := body.(*wire.SendPackCommand).Transfer |
| + a := r.primary.fr.Welcome.RunnerID |
| + r.remove(a, false) |
| + token := r.inst.EnrolRunner(t).Token |
| + w.send(wire.FrameBlob, chunk(old, []byte("PACK commit"))) |
| + if n := r.mailboxHas(a, `"publication"`); n != 0 { |
| + t.Fatalf("%d publications for the replaced primary", n) |
| + } |
| + 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 { |
| + t.Fatal(err) |
| + } |
| + t.Cleanup(np.Close) |
| + b := &peer{t: t, r: r.toolRig, fr: np} |
| + b.none(wire.FramePublish, 50*time.Millisecond) |
| + _, body = w.expect(wire.FrameSendPack) |
| + if again := body.(*wire.SendPackCommand).Transfer; again.ID == old.ID { |
| + t.Fatalf("not asked again %+v", again) |
| + } |
| + if s := r.task(task).Steps[0]; s.Pushed || s.Publication == wire.PublicationPending { |
| + t.Fatalf("published without its pack %+v", s) |
| + } |
| +} |