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 "strings"
8 "testing"
9 "time"
10
11 "amadan.net/oficina/equipo/internal/wire"
12)
13
14func chunk(h wire.TransferHeader, data []byte) wire.Blob {
15 sum := sha256.Sum256(data)
16 return wire.Blob{Transfer: h, Index: 0, Total: 1, SHA256: hex.EncodeToString(sum[:]), Data: base64.StdEncoding.EncodeToString(data)}
17}
18
19// relayed counts the blob commands that reached a runner's mailbox.
20func (r *runnersRig) relayed(p *peer) int {
21 r.t.Helper()
22 var n int
23 if err := r.inst.DB.Writer().QueryRow("SELECT count(*) FROM runner_commands WHERE runner_id = ? AND kind = ?", p.fr.Welcome.RunnerID, wire.FrameBlob).Scan(&n); err != nil {
24 r.t.Fatal(err)
25 }
26 return n
27}
28
29// Every chunk is authorised against its transfer before a byte is kept:
30// a chunk from another runner, under another task's transfer, for
31// another candidate, in the wrong direction, or for an attempt Retry has
32// replaced, is refused and nothing reaches the receiver.
33func TestContractTransferAuthorization(t *testing.T) {
34 r := newRunnersRig(t)
35 w, _ := r.newWorker("Mac A")
36 other, _ := r.newWorker("Mac B")
37 taskA, _ := r.candidate("iOS one", true)
38 _, body := r.primary.expect(wire.FrameSendPack)
39 ha := body.(*wire.SendPackCommand).Transfer
40 _, body = w.expect(wire.FrameDispatch)
41 da := body.(*wire.DispatchCommand)
42 taskB, _ := r.candidate("iOS two", true)
43 _, body = r.primary.expect(wire.FrameSendPack)
44 hb := body.(*wire.SendPackCommand).Transfer
45 if ha.TaskID != taskA || hb.TaskID != taskB || ha.Direction != wire.TransferInbound {
46 t.Fatalf("headers %+v %+v", ha, hb)
47 }
48 before := r.relayed(w) + r.relayed(other)
49 data := []byte("PACK")
50 // A second worker uploading for a transfer not its own.
51 other.send(wire.FrameBlob, chunk(ha, data))
52 // Chunks for task A sent under task B's transfer.
53 mixed := hb
54 mixed.TaskID, mixed.StepID = ha.TaskID, ha.StepID
55 r.primary.send(wire.FrameBlob, chunk(mixed, data))
56 // A candidate that does not match the opened transfer.
57 wrong := ha
58 wrong.CandidateSHA = strings.Repeat("9", 40)
59 r.primary.send(wire.FrameBlob, chunk(wrong, data))
60 // An inbound transfer answered in the outbound direction.
61 back := ha
62 back.Direction = wire.TransferOutbound
63 w.send(wire.FrameBlob, chunk(back, data))
64 // Out of bounds: a chunk index past the total.
65 bad := chunk(ha, data)
66 bad.Index = 3
67 r.primary.send(wire.FrameBlob, bad)
68 if n := r.relayed(w) + r.relayed(other); n != before {
69 t.Fatalf("%d refused chunks were relayed", n-before)
70 }
71 // A stale attempt's chunks after Retry: the worker refuses, the
72 // attempt is replaced, and the old transfer's chunk is refused.
73 w.send(wire.FrameStep, wire.StepReport{TaskID: taskA, StepID: da.StepID, Attempt: 1, State: wire.StepFailed, Reason: "placement_refused", Termination: true, Findings: []wire.Finding{}})
74 r.primary.send(wire.FrameBlob, chunk(ha, data))
75 if n := r.relayed(w) + r.relayed(other); n != before {
76 t.Fatalf("a stale attempt's chunk was relayed")
77 }
78 // The new attempt's transfer is open, and its chunk goes through.
79 _, body = r.primary.expect(wire.FrameSendPack)
80 h2 := body.(*wire.SendPackCommand).Transfer
81 if h2.Attempt != 2 || h2.StepID != ha.StepID || h2.ID == ha.ID {
82 t.Fatalf("second transfer %+v", h2)
83 }
84 r.primary.send(wire.FrameBlob, chunk(h2, data))
85 _, got := other.expect(wire.FrameBlob)
86 if got.(*wire.Blob).Transfer != h2 {
87 t.Fatalf("relayed %+v", got)
88 }
89}
90
91// Oversize (M7): a transfer past 64 MB is refused, and checks that never
92// started end as not run (checks_unavailable), never a pass.
93func TestContractTransferTooLarge(t *testing.T) {
94 r := newRunnersRig(t)
95 w, _ := r.newWorker("Mac A")
96 task, _ := r.candidate("iOS big", true)
97 _, body := r.primary.expect(wire.FrameSendPack)
98 h := body.(*wire.SendPackCommand).Transfer
99 w.expect(wire.FrameDispatch)
100 if _, err := r.inst.DB.Writer().Exec("UPDATE runner_transfers SET bytes = ? WHERE id = ?", wire.MaxTransferBytes-2, h.ID); err != nil {
101 t.Fatal(err)
102 }
103 r.primary.send(wire.FrameBlob, chunk(h, []byte("PACK")))
104 s := r.checksStep(task)
105 if s.State != wire.StepDone || s.Checks != wire.ChecksCannotRun || s.Reason != "checks_unavailable" {
106 t.Fatalf("oversized candidate: %+v", s)
107 }
108 if n := r.relayed(w); n != 0 {
109 t.Fatalf("oversized chunk relayed")
110 }
111}
112
113// workerCommit runs a Mac task's Engineering step on the worker (placed
114// there by Mac Engineering, its base prepared by the primary) to a commit
115// and its pack, which reaches the primary with a publish command.
116func (r *runnersRig) workerCommit(w *peer, title string) (task string, d *wire.DispatchCommand, sha string, pub *wire.PublishCommand) {
117 r.t.Helper()
118 task, d = r.macDispatch(w, title, strings.Repeat("1", 40))
119 if !w.lease(d.StepID, d.Attempt) {
120 r.t.Fatal("start refused")
121 }
122 sha = strings.Repeat("c", 40)
123 w.send(wire.FrameStep, wire.StepReport{TaskID: task, StepID: d.StepID, Attempt: d.Attempt, State: wire.StepRunning, BaseSHA: strings.Repeat("1", 40), Findings: []wire.Finding{}})
124 w.send(wire.FrameStep, wire.StepReport{TaskID: task, StepID: d.StepID, Attempt: d.Attempt, State: wire.StepRunning, CandidateSHA: sha, Phase: "committed", Findings: []wire.Finding{}})
125 _, body := w.expect(wire.FrameSendPack)
126 h := body.(*wire.SendPackCommand).Transfer
127 if h.Direction != wire.TransferOutbound || h.CandidateSHA != sha {
128 r.t.Fatalf("outbound header %+v", h)
129 }
130 // A worker claiming it pushed is refused: the step does not end.
131 w.send(wire.FrameStep, wire.StepReport{TaskID: task, StepID: d.StepID, Attempt: d.Attempt, State: wire.StepDone, CandidateSHA: sha, Pushed: true, Termination: true, Findings: []wire.Finding{}})
132 if k, _ := r.client.Task(r.ctx, task); k.Steps[0].State != wire.StepRunning || k.Steps[0].Pushed {
133 r.t.Fatalf("a worker's pushed was accepted: %+v", k.Steps[0])
134 }
135 w.send(wire.FrameStep, wire.StepReport{TaskID: task, StepID: d.StepID, Attempt: d.Attempt, State: wire.StepDone, CandidateSHA: sha, Termination: true, TerminationKind: wire.TerminationProcessGroup, Findings: []wire.Finding{}})
136 w.send(wire.FrameBlob, chunk(h, []byte("PACK commit")))
137 r.fromPrimary(wire.FrameBlob)
138 pub = r.fromPrimary(wire.FramePublish).(*wire.PublishCommand)
139 if pub.CandidateSHA != sha || pub.Transfer != h.ID || pub.StepID != d.StepID {
140 r.t.Fatalf("publish %+v", pub)
141 }
142 return task, d, sha, pub
143}
144
145// Publication stays the primary's word: a worker's pushed is refused; the
146// candidate counts as pushed only on the primary's published report for
147// the current attempt and candidate, and only then are checks and review
148// created. A failed push is Needs you retry, and Retry publishes the same
149// commit again without running Engineering.
150func TestContractPublication(t *testing.T) {
151 r := newMacRig(t)
152 w := r.newMacEngineer("Mac A", wire.EngineeringReady)
153 task, d, sha, pub := r.workerCommit(w, "iOS fix")
154 // The worker claiming it pushed is refused.
155 w.send(wire.FrameStep, wire.StepReport{TaskID: task, StepID: d.StepID, Attempt: d.Attempt, State: wire.StepDone, CandidateSHA: sha, Pushed: true, Termination: true, Findings: []wire.Finding{}})
156 k, _ := r.client.Task(r.ctx, task)
157 if k.Steps[0].Pushed || k.Steps[0].Publication != wire.PublicationPending || k.CandidateSHA != "" || len(k.Steps) != 1 {
158 t.Fatalf("before publication %+v", k.Steps[0])
159 }
160 // A published report from the worker is refused.
161 w.send(wire.FramePublished, wire.PublishedReport{Publication: pub.Publication, TaskID: task, StepID: d.StepID, Attempt: d.Attempt, CandidateSHA: sha, Pushed: true})
162 if k, _ = r.client.Task(r.ctx, task); k.Steps[0].Pushed {
163 t.Fatal("a worker published")
164 }
165 // The primary's push fails: Needs you retry.
166 if !r.primary.leasePublication(d.StepID, d.Attempt, pub.Publication) {
167 t.Fatal("publication refused")
168 }
169 r.primary.send(wire.FramePublished, wire.PublishedReport{Publication: pub.Publication, TaskID: task, StepID: d.StepID, Attempt: d.Attempt, CandidateSHA: sha, Pushed: false, Reason: "push_failed"})
170 k, _ = r.client.Task(r.ctx, task)
171 if k.NeedsYou != wire.NeedsRetry || k.Steps[0].Publication != wire.PublicationFailed {
172 t.Fatalf("after a failed push %+v", k)
173 }
174 acc, err := r.client.RetryTask(r.ctx, task)
175 if err != nil || acc.Attempt != d.Attempt {
176 t.Fatalf("retry %+v %v", acc, err)
177 }
178 again := r.fromPrimary(wire.FramePublish).(*wire.PublishCommand)
179 if again.CandidateSHA != sha || again.Transfer != pub.Transfer || again.Publication == pub.Publication || again.Attempt != d.Attempt {
180 t.Fatalf("republish %+v", again)
181 }
182 w.none(wire.FrameDispatch, 50*time.Millisecond)
183 // The old publication is no longer the pending one.
184 if r.primary.leasePublication(d.StepID, d.Attempt, pub.Publication) {
185 t.Fatal("a superseded publication was confirmed")
186 }
187 r.primary.send(wire.FramePublished, wire.PublishedReport{Publication: again.Publication, TaskID: task, StepID: d.StepID, Attempt: d.Attempt, CandidateSHA: sha, Pushed: true})
188 if _, c := r.macChecked(task, w); c.CandidateSHA != sha {
189 t.Fatalf("checks after publication %+v", c)
190 }
191 if k, _ = r.client.Task(r.ctx, task); !k.Steps[0].Pushed || k.Steps[0].Publication != wire.PublicationPublished || k.CandidateSHA != sha || k.EngineeringRunner != w.fr.Welcome.RunnerID {
192 t.Fatalf("published %+v", k)
193 }
194}
195
196// Cancel after the pack arrived and before the push: the primary's
197// confirmation right before it pushes is refused, so nothing is pushed.
198func TestContractCancelBeforePublication(t *testing.T) {
199 r := newMacRig(t)
200 w := r.newMacEngineer("Mac A", wire.EngineeringReady)
201 task, d, sha, pub := r.workerCommit(w, "iOS cancel")
202 if _, err := r.client.CancelTask(r.ctx, task); err != nil {
203 t.Fatal(err)
204 }
205 if r.primary.leasePublication(d.StepID, d.Attempt, pub.Publication) {
206 t.Fatal("a cancelled task's publication was confirmed")
207 }
208 r.primary.send(wire.FramePublished, wire.PublishedReport{Publication: pub.Publication, TaskID: task, StepID: d.StepID, Attempt: d.Attempt, CandidateSHA: sha, Pushed: false, Reason: "cancelled"})
209 if k, _ := r.client.Task(r.ctx, task); k.Stage != wire.StageCancelled || k.Steps[0].Pushed {
210 t.Fatalf("cancelled %+v", k)
211 }
212}
213