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 "context"
5 "strings"
6 "testing"
7 "time"
8
9 "amadan.net/oficina/equipo/internal/equipo"
10 "amadan.net/oficina/equipo/internal/equipo/chat"
11 "amadan.net/oficina/equipo/internal/wire"
12)
13
14// revoke revokes a runner as the owner.
15func (r *runnersRig) revoke(id string) {
16 r.t.Helper()
17 var revision int64
18 if err := r.inst.DB.Writer().QueryRow("SELECT revision FROM runners WHERE id = ?", id).Scan(&revision); err != nil {
19 r.t.Fatal(err)
20 }
21 if err := r.inst.Store.RevokeRunner(r.ctx, equipo.Actor{Address: Owner}, id, revision); err != nil {
22 r.t.Fatal(err)
23 }
24}
25
26// Removing a primary keeps what it may have started its own until the
27// proof arrives: its running step is cancelled there, the replacement's
28// empty sync settles nothing, and Retry stays refused. A dispatch it never
29// started (protocol 2: no start authorization) moves to the replacement
30// as the next attempt, and the old attempt can never start.
31func TestContractRevokedPrimaryKeepsItsAttempts(t *testing.T) {
32 r := newRunnersRig(t)
33 ctx := r.ctx
34 old := r.runner.Welcome.RunnerID
35 oldGen := r.runner.Welcome.Generation
36 running := taskID(t, r.call("v1", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "Running when revoked", "brief": "b"}))
37 r.call("v2", wire.ToolStartTask, map[string]string{"task": running, "brief": "go"})
38 _, body := r.expect(wire.FrameDispatch)
39 d := body.(*wire.DispatchCommand)
40 if !r.primary.lease(d.StepID, 1) {
41 t.Fatal("start refused")
42 }
43 r.step("v-run", wire.StepReport{TaskID: running, StepID: d.StepID, Attempt: 1, State: wire.StepRunning, BaseSHA: strings.Repeat("1", 40), Findings: []wire.Finding{}})
44 unacked := taskID(t, r.call("v3", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "Dispatched, never acked", "brief": "b"}))
45 r.call("v4", wire.ToolStartTask, map[string]string{"task": unacked, "brief": "go"})
46 r.runner.Close()
47 r.revoke(old)
48
49 token := r.inst.EnrolRunner(t).Token
50 p, err := DialRunnerHello(ctx, r.inst.URL, token, primaryHello(0))
51 if err != nil {
52 t.Fatal(err)
53 }
54 defer p.Close()
55 if p.Welcome.Role != wire.RolePrimary {
56 t.Fatalf("replacement welcome %+v", p.Welcome)
57 }
58 np := &peer{t: t, r: r.toolRig, fr: p}
59 np.send(wire.FrameSync, wire.Sync{Sessions: []wire.SessionState{}, Runs: []wire.Run{}, Steps: []wire.StepRun{}, Asks: []wire.AskRun{}})
60 k, _ := r.client.Task(ctx, running)
61 if s := k.Steps[0]; wire.StepTerminal(s.State) || s.Terminated || s.RunnerID != old {
62 t.Fatalf("the replacement settled the removed primary's running attempt: %+v", s)
63 }
64 if _, err := r.client.RetryTask(ctx, running); Status(err) != 409 {
65 t.Fatalf("retry of an unproven attempt: %v", err)
66 }
67 _, body = np.expect(wire.FrameDispatch)
68 if d2 := body.(*wire.DispatchCommand); d2.TaskID != unacked || d2.Attempt != 2 {
69 t.Fatalf("the never-started attempt %+v", d2)
70 }
71 // The removed primary may still report its proof; its old attempt of
72 // the moved step can never start.
73 back, err := DialRunnerHello(ctx, r.inst.URL, r.rig.runner, primaryHello(oldGen))
74 if err != nil {
75 t.Fatal(err)
76 }
77 defer back.Close()
78 bp := &peer{t: t, r: r.toolRig, fr: back}
79 k, _ = r.client.Task(ctx, unacked)
80 if bp.lease(k.Steps[0].ID, 1) {
81 t.Fatal("a withdrawn attempt may start on the removed primary")
82 }
83}
84
85// Never-dispatched waiting checks end on Cancel, locally: nothing is
86// sent to an empty runner id, and the task is cancelled at once.
87func TestContractCancelWaitingSteps(t *testing.T) {
88 r := newRunnersRig(t)
89 w, _ := r.newWorker("Tito Studio")
90 w.fr.Close()
91 time.Sleep(100 * time.Millisecond)
92 task, _ := r.candidate("iOS waiting", true)
93 if s := r.checksStep(task); s.Placement != wire.PlacementWaiting {
94 t.Fatalf("not waiting %+v", s)
95 }
96 if _, err := r.client.CancelTask(r.ctx, task); err != nil {
97 t.Fatal(err)
98 }
99 k, _ := r.client.Task(r.ctx, task)
100 s := r.checksStep(task)
101 if k.Stage != wire.StageCancelled || s.State != wire.StepCancelled || !s.Terminated {
102 t.Fatalf("cancel of a waiting step: task %s step %+v", k.Stage, s)
103 }
104 var n int
105 if err := r.inst.DB.Writer().QueryRow("SELECT count(*) FROM runner_commands WHERE coalesce(runner_id, '') = '' AND kind IN (?, ?)", wire.FrameCancelStep, wire.FrameDispatch).Scan(&n); err != nil || n != 0 {
106 t.Fatalf("%d commands to nobody: %v", n, err)
107 }
108}
109
110// Engineering's pin survives the step's own save: a new task records the
111// runner its Engineering ran on.
112func TestContractEngineeringPinKept(t *testing.T) {
113 r := newRunnersRig(t)
114 task, _ := r.candidate("Pinned", false)
115 k, err := r.client.Task(r.ctx, task)
116 if err != nil || k.EngineeringRunner != r.runner.Welcome.RunnerID {
117 t.Fatalf("pin %q, want %q: %v", k.EngineeringRunner, r.runner.Welcome.RunnerID, err)
118 }
119}
120
121// startedOnWorker places checks on the only worker, starts the attempt,
122// takes the worker away for the offline limit (a fake clock), and has the
123// sweep withdraw the started attempt; the worker comes back to its cancel.
124func (r *runnersRig) startedOnWorker(title string) (task, step string, w *peer, sha string) {
125 r.t.Helper()
126 w, token := r.newWorker("Tito Studio")
127 task, sha = r.candidate(title, true)
128 _, body := w.expect(wire.FrameDispatch)
129 d := body.(*wire.DispatchCommand)
130 if !w.lease(d.StepID, 1) {
131 r.t.Fatal("start refused")
132 }
133 gen := w.fr.Welcome.Generation
134 w.fr.Close()
135 later := time.Now().UTC().Add(chat.OfflineLimit + time.Minute)
136 r.inst.Store.Chat().SetClock(func() time.Time { return later })
137 if err := r.inst.Store.Chat().Place(r.ctx); err != nil {
138 r.t.Fatal(err)
139 }
140 r.inst.Store.Chat().SetClock(func() time.Time { return time.Now().UTC() })
141 w = r.dialWorker(token, "Tito Studio", gen)
142 _, body = w.expect(wire.FrameCancelStep)
143 if c := body.(*wire.CancelStepCommand); c.StepID != d.StepID || c.Attempt != 1 {
144 r.t.Fatalf("cancel %+v", c)
145 }
146 return task, d.StepID, w, sha
147}
148
149// Withdrawal comes first (M6): a withdrawn attempt's delayed success does
150// not count; a failure without proof waits for the proof, and the proof
151// then places the next attempt; Retry works on that next attempt.
152func TestContractWithdrawnAttemptReports(t *testing.T) {
153 r := newRunnersRig(t)
154 task, step, w, sha := r.startedOnWorker("Withdrawn, then passed")
155 w.send(wire.FrameStep, passed(task, step, 1, sha))
156 s := r.checksStep(task)
157 if s.Checks == wire.ChecksPassed || s.Attempt != 2 {
158 t.Fatalf("a withdrawn attempt's delayed success counted: %+v", s)
159 }
160 _, body := w.expect(wire.FrameDispatch)
161 if d := body.(*wire.DispatchCommand); d.StepID != step || d.Attempt != 2 {
162 t.Fatalf("next attempt %+v", d)
163 }
164 // Attempt 2 fails normally (with its proof): Retry gives attempt 3,
165 // which may start.
166 if !w.lease(step, 2) {
167 t.Fatal("attempt 2 refused")
168 }
169 w.send(wire.FrameStep, wire.StepReport{TaskID: task, StepID: step, Attempt: 2, State: wire.StepFailed, Reason: "provider_failed", Termination: true, Findings: []wire.Finding{}})
170 if acc, err := r.client.RetryTask(r.ctx, task); err != nil || acc.Attempt != 3 {
171 t.Fatalf("retry after withdrawal %+v %v", acc, err)
172 }
173 w.expect(wire.FrameDispatch)
174 if !w.lease(step, 3) {
175 t.Fatal("attempt 3 refused after Retry")
176 }
177}
178
179func TestContractWithdrawnProofAfterFailure(t *testing.T) {
180 r := newRunnersRig(t)
181 task, step, w, _ := r.startedOnWorker("Withdrawn, failed, proven")
182 w.send(wire.FrameStep, wire.StepReport{TaskID: task, StepID: step, Attempt: 1, State: wire.StepCancelled, Reason: "cancelled", Findings: []wire.Finding{}})
183 if s := r.checksStep(task); s.Attempt != 1 || s.Terminated {
184 t.Fatalf("before the proof %+v", s)
185 }
186 w.send(wire.FrameStep, wire.StepReport{TaskID: task, StepID: step, Attempt: 1, State: wire.StepCancelled, Reason: "cancelled", Termination: true, TerminationKind: wire.TerminationProcessGroup, Findings: []wire.Finding{}})
187 _, body := w.expect(wire.FrameDispatch)
188 if d := body.(*wire.DispatchCommand); d.StepID != step || d.Attempt != 2 {
189 t.Fatalf("the proof did not place the next attempt: %+v", d)
190 }
191}
192
193// A pack too large to relay is declared by its chunk count, and both ends
194// read it the same way: the checks end as not run (checks_unavailable).
195func TestContractTransferDeclaredTooLarge(t *testing.T) {
196 r := newRunnersRig(t)
197 w, _ := r.newWorker("Mac A")
198 task, _ := r.candidate("iOS huge", true)
199 _, body := r.primary.expect(wire.FrameSendPack)
200 h := body.(*wire.SendPackCommand).Transfer
201 w.expect(wire.FrameDispatch)
202 c := chunk(h, []byte("PACK"))
203 c.Total = wire.MaxTransferChunks + 1
204 r.primary.send(wire.FrameBlob, c)
205 s := r.checksStep(task)
206 if s.State != wire.StepDone || s.Checks != wire.ChecksCannotRun || s.Reason != "checks_unavailable" {
207 t.Fatalf("declared oversize %+v", s)
208 }
209 if n := r.relayed(w); n != 0 {
210 t.Fatal("an oversized chunk was relayed")
211 }
212}
213
214// Asking a specialist whose runner is offline is answered at once: the
215// runner is offline (runner_offline), nothing queues.
216func TestContractAskOfflineRunner(t *testing.T) {
217 r := newToolRigAt(t, wire.ProtocolInjectionDefences)
218 id, _, _ := r.askableTask("o", strings.Repeat("1", 40), strings.Repeat("2", 40))
219 r.runner.Close()
220 deadline := time.Now().Add(5 * time.Second)
221 for {
222 runners, err := r.client.Runners(context.Background())
223 if err == nil && len(runners.Items) == 1 && !runners.Items[0].Online {
224 break
225 }
226 if time.Now().After(deadline) {
227 t.Fatalf("runner still online: %+v %v", runners, err)
228 }
229 time.Sleep(50 * time.Millisecond)
230 }
231 if _, err := r.client.Ask(r.ctx, id, "What did you change?", "", "off-1"); Status(err) != 409 || !strings.Contains(err.Error(), "runner_offline") {
232 t.Fatalf("ask of an offline runner: %v", err)
233 }
234 if k, _ := r.client.Task(r.ctx, id); len(k.Questions) != 0 {
235 t.Fatalf("a question queued for an offline runner: %+v", k.Questions)
236 }
237}
238