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 "crypto/sha256"
5 "encoding/base64"
6 "encoding/hex"
7 "testing"
8 "time"
9
10 "amadan.net/oficina/equipo/internal/wire"
11)
12
13// Whole-branch fix 1, #4: a worker's commit is published only by the
14// primary its pack went to. A primary replaced while the outbound
15// transfer is open (or once it completed with no primary to publish)
16// has that transfer closed as superseded, and the worker is asked for
17// its retained candidate again, to the new primary.
18
19// chunksOf is pack split into n chunks of h.
20func chunksOf(h wire.TransferHeader, pack []byte, n int) []wire.Blob {
21 sum := sha256.Sum256(pack)
22 var out []wire.Blob
23 for i := range n {
24 part := pack[i*len(pack)/n : (i+1)*len(pack)/n]
25 out = append(out, wire.Blob{Transfer: h, Index: i, Total: n, SHA256: hex.EncodeToString(sum[:]), Data: base64.StdEncoding.EncodeToString(part)})
26 }
27 return out
28}
29
30func (r *runnersRig) transferTo(id string) (to, state string) {
31 r.t.Helper()
32 if err := r.inst.DB.Writer().QueryRow("SELECT to_runner, state FROM runner_transfers WHERE id = ?", id).Scan(&to, &state); err != nil {
33 r.t.Fatal(err)
34 }
35 return to, state
36}
37
38func TestContractPublicationPrimaryReplacedMidTransfer(t *testing.T) {
39 for _, when := range []string{"open", "complete"} {
40 t.Run(when, func(t *testing.T) {
41 r := newMacRig(t)
42 w := r.newMacEngineer("Mac A", wire.EngineeringReady)
43 base, cand := sha(1), sha(0xa4)
44 task, d := r.macDispatch(w, "iOS primary replaced", base)
45 if !w.lease(d.StepID, d.Attempt) {
46 t.Fatal("start refused")
47 }
48 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{}})
49 _, body := w.expect(wire.FrameSendPack)
50 old := body.(*wire.SendPackCommand).Transfer
51 a := r.primary.fr.Welcome.RunnerID
52 chunks := chunksOf(old, []byte("PACK commit, two chunks"), 2)
53 w.send(wire.FrameBlob, chunks[0])
54 r.remove(a, false)
55 if when == "complete" {
56 // The last chunk lands with no primary to publish it.
57 w.send(wire.FrameBlob, chunks[1])
58 }
59 token := r.inst.EnrolRunner(t).Token
60 h := primaryHello(0)
61 h.Protocol, h.Label = wire.ProtocolMacEngineering, "docs-t2"
62 h.Catalog[0].NeedsMacOS = true
63 np, err := DialRunnerHello(r.ctx, r.inst.URL, token, h)
64 if err != nil || np.Welcome.Role != wire.RolePrimary {
65 t.Fatalf("replacement %+v %v", np, err)
66 }
67 t.Cleanup(np.Close)
68 b := &peer{t: t, r: r.toolRig, fr: np}
69 bID := np.Welcome.RunnerID
70 // The open transfer to A is superseded; the worker is asked again,
71 // for B.
72 if to, state := r.transferTo(old.ID); to != a || state != "closed" {
73 t.Fatalf("A's transfer: to %s, %s", to, state)
74 }
75 _, body = w.expect(wire.FrameSendPack)
76 again := body.(*wire.SendPackCommand).Transfer
77 if again.ID == old.ID || again.Direction != wire.TransferOutbound || again.CandidateSHA != cand || again.BaseSHA != base || again.Attempt != d.Attempt {
78 t.Fatalf("outbound again %+v", again)
79 }
80 if to, state := r.transferTo(again.ID); to != bID || state != "open" {
81 t.Fatalf("the new transfer: to %s, %s", to, state)
82 }
83 if when == "open" {
84 // A's last chunk now has nowhere to go.
85 w.send(wire.FrameBlob, chunks[1])
86 b.none(wire.FramePublish, 50*time.Millisecond)
87 }
88 w.send(wire.FrameStep, workerDone(d, cand, wire.OutcomeCommitted))
89 w.send(wire.FrameBlob, chunk(again, []byte("PACK commit")))
90 b.expect(wire.FrameBlob)
91 _, body = b.expect(wire.FramePublish)
92 pub := body.(*wire.PublishCommand)
93 if pub.Transfer != again.ID || pub.CandidateSHA != cand {
94 t.Fatalf("B's publication %+v", pub)
95 }
96 if !b.leasePublication(d.StepID, d.Attempt, pub.Publication) {
97 t.Fatal("B's own publication refused")
98 }
99 b.send(wire.FramePublished, publishedWith(pub, base, swipeDiff))
100 if s := r.task(task).Steps[0]; !s.Pushed || s.Publication != wire.PublicationPublished {
101 t.Fatalf("after B published %+v", s)
102 }
103 })
104 }
105}
106
107// A transfer that completes while it names a primary since replaced
108// (the new one enrolled, not yet connected) is never published by the
109// new primary: the worker is asked again, for it.
110func TestContractPublicationNeedsItsTransfersPrimary(t *testing.T) {
111 r := newMacRig(t)
112 w := r.newMacEngineer("Mac A", wire.EngineeringReady)
113 base, cand := sha(1), sha(0xa5)
114 task, d := r.macDispatch(w, "iOS primary enrolled", base)
115 if !w.lease(d.StepID, d.Attempt) {
116 t.Fatal("start refused")
117 }
118 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{}})
119 _, body := w.expect(wire.FrameSendPack)
120 old := body.(*wire.SendPackCommand).Transfer
121 a := r.primary.fr.Welcome.RunnerID
122 r.remove(a, false)
123 token := r.inst.EnrolRunner(t).Token
124 w.send(wire.FrameBlob, chunk(old, []byte("PACK commit")))
125 if n := r.mailboxHas(a, `"publication"`); n != 0 {
126 t.Fatalf("%d publications for the replaced primary", n)
127 }
128 h := primaryHello(0)
129 h.Protocol, h.Label = wire.ProtocolMacEngineering, "docs-t2"
130 h.Catalog[0].NeedsMacOS = true
131 np, err := DialRunnerHello(r.ctx, r.inst.URL, token, h)
132 if err != nil {
133 t.Fatal(err)
134 }
135 t.Cleanup(np.Close)
136 b := &peer{t: t, r: r.toolRig, fr: np}
137 b.none(wire.FramePublish, 50*time.Millisecond)
138 _, body = w.expect(wire.FrameSendPack)
139 if again := body.(*wire.SendPackCommand).Transfer; again.ID == old.ID {
140 t.Fatalf("not asked again %+v", again)
141 }
142 if s := r.task(task).Steps[0]; s.Pushed || s.Publication == wire.PublicationPending {
143 t.Fatalf("published without its pack %+v", s)
144 }
145}
146