| 1 | package thintest |
| 2 | |
| 3 | import ( |
| 4 | "crypto/sha256" |
| 5 | "encoding/base64" |
| 6 | "encoding/hex" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | "time" |
| 10 | |
| 11 | "amadan.net/oficina/equipo/internal/equipo/chat" |
| 12 | "amadan.net/oficina/equipo/internal/wire" |
| 13 | ) |
| 14 | |
| 15 | // peer is a scripted runner with the frames it received but has not |
| 16 | // looked at yet. |
| 17 | type peer struct { |
| 18 | t *testing.T |
| 19 | r *toolRig |
| 20 | fr *FakeRunner |
| 21 | held []heldFrame |
| 22 | seq int |
| 23 | } |
| 24 | |
| 25 | func (p *peer) id(prefix string) string { |
| 26 | p.seq++ |
| 27 | return prefix + "-" + itoa(p.seq) |
| 28 | } |
| 29 | |
| 30 | // send sends a frame and, for a durable one, waits for its ack. |
| 31 | func (p *peer) send(kind string, body any) string { |
| 32 | p.t.Helper() |
| 33 | id := p.id("p-" + kind) |
| 34 | if err := p.fr.Send(p.r.ctx, id, kind, body); err != nil { |
| 35 | p.t.Fatal(err) |
| 36 | } |
| 37 | if !(wire.Frame{Type: kind}).Durable() && kind != wire.FrameLease { |
| 38 | return id |
| 39 | } |
| 40 | for { |
| 41 | f, body, err := p.fr.Next(p.r.ctx) |
| 42 | if err != nil { |
| 43 | p.t.Fatalf("waiting for the ack of %s: %v", id, err) |
| 44 | } |
| 45 | if a, ok := body.(*wire.Ack); ok && a.ID == id { |
| 46 | return id |
| 47 | } |
| 48 | if rf, ok := body.(*wire.Refused); ok && rf.ID == id { |
| 49 | p.held = append(p.held, heldFrame{f, body, nil}) |
| 50 | return id |
| 51 | } |
| 52 | if f.Type != wire.FrameAck { |
| 53 | p.held = append(p.held, heldFrame{f, body, p.fr.Received[len(p.fr.Received)-1]}) |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | // lease asks for the start authorization of an attempt: true when acked, |
| 59 | // false when refused. |
| 60 | func (p *peer) lease(step string, attempt int64) bool { |
| 61 | p.t.Helper() |
| 62 | id := p.send(wire.FrameLease, wire.LeaseCheck{Generation: p.fr.Welcome.Generation, StepID: step, Attempt: attempt}) |
| 63 | for i, h := range p.held { |
| 64 | if rf, ok := h.body.(*wire.Refused); ok && rf.ID == id { |
| 65 | p.held = append(p.held[:i:i], p.held[i+1:]...) |
| 66 | return false |
| 67 | } |
| 68 | } |
| 69 | return true |
| 70 | } |
| 71 | |
| 72 | // leasePublication is the primary's confirmation before it pushes. |
| 73 | func (p *peer) leasePublication(step string, attempt int64, publication string) bool { |
| 74 | p.t.Helper() |
| 75 | id := p.send(wire.FrameLease, wire.LeaseCheck{Generation: p.fr.Welcome.Generation, StepID: step, Attempt: attempt, Publication: publication}) |
| 76 | for i, h := range p.held { |
| 77 | if rf, ok := h.body.(*wire.Refused); ok && rf.ID == id { |
| 78 | p.held = append(p.held[:i:i], p.held[i+1:]...) |
| 79 | return false |
| 80 | } |
| 81 | } |
| 82 | return true |
| 83 | } |
| 84 | |
| 85 | // expect returns the next frame of kind (held first), acking it. |
| 86 | func (p *peer) expect(kind string) (wire.Frame, any) { |
| 87 | p.t.Helper() |
| 88 | for i, h := range p.held { |
| 89 | if h.f.Type == kind { |
| 90 | p.held = append(p.held[:i:i], p.held[i+1:]...) |
| 91 | p.ack(h.f) |
| 92 | return h.f, h.body |
| 93 | } |
| 94 | } |
| 95 | for { |
| 96 | f, body, err := p.fr.Next(p.r.ctx) |
| 97 | if err != nil { |
| 98 | p.t.Fatalf("waiting for %s: %v", kind, err) |
| 99 | } |
| 100 | if f.Type == kind { |
| 101 | p.ack(f) |
| 102 | return f, body |
| 103 | } |
| 104 | if f.Type != wire.FrameAck { |
| 105 | p.held = append(p.held, heldFrame{f, body, p.fr.Received[len(p.fr.Received)-1]}) |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | // none checks, after d, that nothing of kind was sent to this runner and |
| 111 | // none waits in its mailbox. (A read deadline would close the socket.) |
| 112 | func (p *peer) none(kind string, d time.Duration) { |
| 113 | p.t.Helper() |
| 114 | time.Sleep(d) |
| 115 | for _, h := range p.held { |
| 116 | if h.f.Type == kind { |
| 117 | p.t.Fatalf("unexpected %s: %s", kind, h.raw) |
| 118 | } |
| 119 | } |
| 120 | var n int |
| 121 | if err := p.r.inst.DB.Writer().QueryRow("SELECT count(*) FROM runner_commands WHERE runner_id = ? AND kind = ? AND acked_at IS NULL", p.fr.Welcome.RunnerID, kind).Scan(&n); err != nil { |
| 122 | p.t.Fatal(err) |
| 123 | } |
| 124 | if n != 0 { |
| 125 | p.t.Fatalf("%d %s waiting for this runner", n, kind) |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | // heartbeat says hello again (under the store's clock), then waits for a |
| 130 | // lease round trip so it has been applied. |
| 131 | func (p *peer) heartbeat(h wire.Hello) { |
| 132 | p.t.Helper() |
| 133 | h.Generation = p.fr.Welcome.Generation |
| 134 | if err := p.fr.Send(p.r.ctx, p.id("hb"), wire.FrameHello, h); err != nil { |
| 135 | p.t.Fatal(err) |
| 136 | } |
| 137 | p.lease("", 0) |
| 138 | } |
| 139 | |
| 140 | func (p *peer) ack(f wire.Frame) { |
| 141 | if f.Durable() { |
| 142 | if err := p.fr.Send(p.r.ctx, "a-"+f.ID, wire.FrameAck, wire.Ack{ID: f.ID}); err != nil { |
| 143 | p.t.Fatal(err) |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | // primaryHello is the tool rig's hello at protocol 2 from a Linux primary. |
| 149 | func primaryHello(generation int64) wire.Hello { |
| 150 | h := toolHello(generation) |
| 151 | h.Protocol, h.Platform, h.Label, h.Sandbox = wire.Protocol2, &wire.Platform{OS: "linux", Arch: "amd64"}, "docs-t1", wire.SandboxBubblewrap |
| 152 | h.Capabilities = &wire.Capabilities{Simulators: []wire.Simulator{}, Tags: []string{wire.TagLinux, wire.TagClaude, wire.TagGo}} |
| 153 | return h |
| 154 | } |
| 155 | |
| 156 | // runnersRig is the tool rig with its primary at protocol 2. |
| 157 | type runnersRig struct { |
| 158 | *toolRig |
| 159 | primary *peer |
| 160 | // creds are each worker connection's credential and label. |
| 161 | creds map[*FakeRunner][2]string |
| 162 | } |
| 163 | |
| 164 | func newRunnersRig(t *testing.T) *runnersRig { return newRunnersRigAt(t, wire.Protocol2) } |
| 165 | |
| 166 | // newRunnersRigAt is the runners rig with its primary at protocol |
| 167 | // (wire.ProtocolMergeFacts where Equipo should merge on its own). |
| 168 | func newRunnersRigAt(t *testing.T, protocol int) *runnersRig { |
| 169 | r := newToolRig(t) |
| 170 | gen := r.runner.Welcome.Generation |
| 171 | r.runner.Close() |
| 172 | h := primaryHello(gen) |
| 173 | h.Protocol = protocol |
| 174 | p, err := DialRunnerHello(r.ctx, r.inst.URL, r.rig.runner, h) |
| 175 | if err != nil { |
| 176 | t.Fatal(err) |
| 177 | } |
| 178 | t.Cleanup(p.Close) |
| 179 | if p.Welcome.Role != wire.RolePrimary || p.Welcome.Protocol != protocol { |
| 180 | t.Fatalf("primary welcome %+v", p.Welcome) |
| 181 | } |
| 182 | r.runner = p |
| 183 | rr := &runnersRig{toolRig: r, primary: &peer{t: t, r: r, fr: p}, creds: map[*FakeRunner][2]string{}} |
| 184 | return rr |
| 185 | } |
| 186 | |
| 187 | // worker enrols and connects a Mac worker. |
| 188 | func (r *runnersRig) newWorker(label string) (*peer, string) { |
| 189 | r.t.Helper() |
| 190 | token := r.inst.EnrolRunner(r.t).Token |
| 191 | return r.dialWorker(token, label, 0), token |
| 192 | } |
| 193 | |
| 194 | func (r *runnersRig) dialWorker(token, label string, gen int64) *peer { |
| 195 | r.t.Helper() |
| 196 | h := WorkerHello(gen) |
| 197 | h.Label = label |
| 198 | w, err := DialRunnerHello(r.ctx, r.inst.URL, token, h) |
| 199 | if err != nil { |
| 200 | r.t.Fatal(err) |
| 201 | } |
| 202 | r.t.Cleanup(w.Close) |
| 203 | r.creds[w] = [2]string{token, label} |
| 204 | return &peer{t: r.t, r: r.toolRig, fr: w} |
| 205 | } |
| 206 | |
| 207 | // candidate records a task and runs its Engineering step on the primary to |
| 208 | // a pushed candidate that touched Apple files, so its checks need a Mac. |
| 209 | // It returns the task and the checks step's id once it exists. |
| 210 | func (r *runnersRig) candidate(title string, apple bool) (task, sha string) { |
| 211 | r.t.Helper() |
| 212 | task = taskID(r.t, r.call("c-"+title, wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": title, "brief": "b"})) |
| 213 | r.call("s-"+title, wire.ToolStartTask, map[string]string{"task": task, "brief": "go"}) |
| 214 | _, body := r.expect(wire.FrameDispatch) |
| 215 | d := body.(*wire.DispatchCommand) |
| 216 | if d.RunnerID == "" || len(d.Needs) != 0 { |
| 217 | r.t.Fatalf("engineering dispatch %+v", d) |
| 218 | } |
| 219 | sha = strings.Repeat(string(rune('a'+len(title)%6)), 40) |
| 220 | r.step("e1-"+task, wire.StepReport{TaskID: task, StepID: d.StepID, Attempt: 1, State: wire.StepRunning, BaseSHA: strings.Repeat("1", 40), Findings: []wire.Finding{}}) |
| 221 | r.step("e2-"+task, wire.StepReport{TaskID: task, StepID: d.StepID, Attempt: 1, State: wire.StepRunning, CandidateSHA: sha, Findings: []wire.Finding{}}) |
| 222 | r.step("e3-"+task, wire.StepReport{TaskID: task, StepID: d.StepID, Attempt: 1, State: wire.StepDone, CandidateSHA: sha, Pushed: true, Termination: true, TerminationKind: wire.TerminationNamespace, Apple: apple, Findings: []wire.Finding{}}) |
| 223 | return task, sha |
| 224 | } |
| 225 | |
| 226 | func (r *runnersRig) checksStep(task string) wire.Step { |
| 227 | r.t.Helper() |
| 228 | k, err := r.client.Task(r.ctx, task) |
| 229 | if err != nil { |
| 230 | r.t.Fatal(err) |
| 231 | } |
| 232 | for _, s := range k.Steps { |
| 233 | if s.Specialist == wire.SpecialistChecks { |
| 234 | return s |
| 235 | } |
| 236 | } |
| 237 | r.t.Fatalf("no checks step: %+v", k.Steps) |
| 238 | return wire.Step{} |
| 239 | } |
| 240 | |
| 241 | // relay uploads a one-chunk pack from the primary for the inbound |
| 242 | // transfer it was asked for, and returns the chunk the worker receives. |
| 243 | func (r *runnersRig) relay(w *peer, data []byte) *wire.Blob { |
| 244 | r.t.Helper() |
| 245 | _, body := r.primary.expect(wire.FrameSendPack) |
| 246 | sp := body.(*wire.SendPackCommand) |
| 247 | sum := sha256.Sum256(data) |
| 248 | r.primary.send(wire.FrameBlob, wire.Blob{Transfer: sp.Transfer, Index: 0, Total: 1, SHA256: hex.EncodeToString(sum[:]), Data: base64.StdEncoding.EncodeToString(data)}) |
| 249 | _, got := w.expect(wire.FrameBlob) |
| 250 | return got.(*wire.Blob) |
| 251 | } |
| 252 | |
| 253 | func passed(task, step string, attempt int64, sha string) wire.StepReport { |
| 254 | zero := 0 |
| 255 | return wire.StepReport{TaskID: task, StepID: step, Attempt: attempt, State: wire.StepDone, CandidateSHA: sha, Checks: wire.ChecksPassed, Gate: "xcodebuild test", ExitCode: &zero, Output: "** TEST SUCCEEDED **", Termination: true, TerminationKind: wire.TerminationProcessGroup, Findings: []wire.Finding{}} |
| 256 | } |
| 257 | |
| 258 | // M1 at the contract: an iOS change's checks are placed on the Mac |
| 259 | // worker, the candidate is relayed as a pack from the primary, the worker |
| 260 | // starts the attempt with its start authorization and passes it, and |
| 261 | // review is placed back on the primary. Linux checks stay on the primary. |
| 262 | func TestContractChecksPlacedOnTheWorker(t *testing.T) { |
| 263 | r := newRunnersRig(t) |
| 264 | w, _ := r.newWorker("Tito Studio") |
| 265 | task, sha := r.candidate("iOS swipe", true) |
| 266 | _, body := w.expect(wire.FrameDispatch) |
| 267 | d := body.(*wire.DispatchCommand) |
| 268 | shape(t, "frames/dispatch-needs.json", w.fr.Received[len(w.fr.Received)-1]) |
| 269 | if d.Specialist != wire.SpecialistChecks || d.CandidateSHA != sha || d.RunnerID == "" || strings.Join(d.Needs, ",") != "macos,xcode" { |
| 270 | t.Fatalf("worker dispatch %+v", d) |
| 271 | } |
| 272 | blob := r.relay(w, []byte("PACK one commit")) |
| 273 | shape(t, "frames/blob.json", w.fr.Received[len(w.fr.Received)-1]) |
| 274 | if blob.Transfer.Direction != wire.TransferInbound || blob.Transfer.StepID != d.StepID || blob.Transfer.Attempt != 1 || blob.Transfer.CandidateSHA != sha { |
| 275 | t.Fatalf("relayed header %+v", blob.Transfer) |
| 276 | } |
| 277 | s := r.checksStep(task) |
| 278 | if s.RunnerLabel != "Tito Studio" || s.Placement != "" || strings.Join(s.Needs, ",") != "macos,xcode" { |
| 279 | t.Fatalf("checks step %+v", s) |
| 280 | } |
| 281 | if !w.lease(d.StepID, 1) { |
| 282 | t.Fatal("start refused") |
| 283 | } |
| 284 | if !w.lease(d.StepID, 1) { |
| 285 | t.Fatal("a repeated start authorization (a resent lease) was refused") |
| 286 | } |
| 287 | w.send(wire.FrameStep, wire.StepReport{TaskID: task, StepID: d.StepID, Attempt: 1, State: wire.StepRunning, CandidateSHA: sha, Findings: []wire.Finding{}}) |
| 288 | // P2 did not deny every attempt, so the worker ran checks outside the |
| 289 | // sandbox and says so; step and task carry it (Paul's ruling 2). |
| 290 | pass := passed(task, d.StepID, 1, sha) |
| 291 | pass.ChecksSandbox = wire.SandboxNone |
| 292 | w.send(wire.FrameStep, pass) |
| 293 | _, body = r.expect(wire.FrameDispatch) |
| 294 | if rv := body.(*wire.DispatchCommand); rv.Specialist != wire.SpecialistReview || len(rv.Needs) != 0 { |
| 295 | t.Fatalf("review dispatch %+v", rv) |
| 296 | } |
| 297 | k, _ := r.client.Task(r.ctx, task) |
| 298 | if k.Checks != wire.ChecksPassed || k.Steps[1].TerminationKind != wire.TerminationProcessGroup || k.Steps[1].StartedAt == nil { |
| 299 | t.Fatalf("task %+v", k) |
| 300 | } |
| 301 | if k.ChecksSandbox != wire.SandboxNone || k.Steps[1].ChecksSandbox != wire.SandboxNone { |
| 302 | t.Fatalf("checks outside the sandbox not labelled: task %q step %q", k.ChecksSandbox, k.Steps[1].ChecksSandbox) |
| 303 | } |
| 304 | runners, err := r.client.Runners(r.ctx) |
| 305 | if err != nil || len(runners.Items) != 2 || runners.Items[0].Role != wire.RolePrimary || runners.Items[1].Label != "Tito Studio" || !runners.Items[1].Online { |
| 306 | t.Fatalf("runners %+v %v", runners, err) |
| 307 | } |
| 308 | if c := runners.Items[1].Capabilities; c == nil || len(c.Probes) != 7 || c.Probes[1].ID != "P2" || c.Probes[1].Verdict != wire.ProbeFailed { |
| 309 | t.Fatalf("the worker's probes are not on the runners view: %+v", c) |
| 310 | } |
| 311 | // Approved, checks passed outside the sandbox: Equipo does not merge |
| 312 | // on its own; it asks (no merge dispatched; the next dispatch below is |
| 313 | // the next task's Engineering). |
| 314 | r.primary.send(wire.FrameStep, wire.StepReport{TaskID: task, StepID: body.(*wire.DispatchCommand).StepID, Attempt: 1, State: wire.StepDone, CandidateSHA: sha, Verdict: wire.VerdictApprove, ReviewLabel: wire.ReviewOtherFamily, Family: "codex", Termination: true, TerminationKind: wire.TerminationNamespace, Findings: []wire.Finding{}}) |
| 315 | deadline := time.Now().Add(5 * time.Second) |
| 316 | for { |
| 317 | k, _ = r.client.Task(r.ctx, task) |
| 318 | if k.Stage == wire.StageReadyToMerge || time.Now().After(deadline) { |
| 319 | break |
| 320 | } |
| 321 | time.Sleep(50 * time.Millisecond) |
| 322 | } |
| 323 | if k.Stage != wire.StageReadyToMerge || k.NeedsYou != wire.NeedsMerge { |
| 324 | t.Fatalf("checks outside the sandbox merged on their own: %+v", k) |
| 325 | } |
| 326 | |
| 327 | // A change that needs no Mac stays on the primary, worker or not. |
| 328 | plain, _ := r.candidate("Go fix", false) |
| 329 | _, body = r.expect(wire.FrameDispatch) |
| 330 | c := body.(*wire.DispatchCommand) |
| 331 | if c.Specialist != wire.SpecialistChecks || c.TaskID != plain || len(c.Needs) != 0 { |
| 332 | t.Fatalf("plain checks %+v", c) |
| 333 | } |
| 334 | // The primary's checks carry no worker label, whatever it reports. |
| 335 | own := passed(plain, c.StepID, 1, c.CandidateSHA) |
| 336 | own.ChecksSandbox = wire.SandboxNone |
| 337 | r.primary.send(wire.FrameStep, own) |
| 338 | r.expect(wire.FrameDispatch) |
| 339 | if k, _ := r.client.Task(r.ctx, plain); k.Checks != wire.ChecksPassed || k.ChecksSandbox != "" { |
| 340 | t.Fatalf("primary's checks labelled: %+v", k) |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | // M3 at the contract: with no worker that fits, checks no longer go to |
| 345 | // the Linux primary (2026-11-28): they wait for a Mac runner, nothing |
| 346 | // dispatched (TestContractChecksWaitForAMacRunner follows them to a Mac). |
| 347 | func TestContractChecksWithoutAWorker(t *testing.T) { |
| 348 | r := newRunnersRig(t) |
| 349 | task, _ := r.candidate("iOS swipe", true) |
| 350 | r.primary.none(wire.FrameDispatch, 200*time.Millisecond) |
| 351 | if s := r.checksStep(task); s.Placement != wire.PlacementMac || s.RunnerID != "" { |
| 352 | t.Fatalf("checks step %+v", s) |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | // M2 at the contract: the fitting worker is offline, so checks wait |
| 357 | // (placement waiting_runner, no dispatch anywhere); it reconnects and the |
| 358 | // step is placed on it. A second task waits 30 minutes (a fake clock) and |
| 359 | // resolves as not run, runner_offline, never a pass; review proceeds. |
| 360 | func TestContractChecksWaitForTheWorker(t *testing.T) { |
| 361 | r := newRunnersRig(t) |
| 362 | w, token := r.newWorker("Tito Studio") |
| 363 | gen := w.fr.Welcome.Generation |
| 364 | w.fr.Close() |
| 365 | time.Sleep(100 * time.Millisecond) |
| 366 | task, _ := r.candidate("iOS swipe", true) |
| 367 | if s := r.checksStep(task); s.Placement != wire.PlacementWaiting || s.RunnerID != "" { |
| 368 | t.Fatalf("checks did not wait: %+v", s) |
| 369 | } |
| 370 | r.primary.none(wire.FrameSendPack, 200*time.Millisecond) |
| 371 | w = r.dialWorker(token, "Tito Studio", gen) |
| 372 | _, body := w.expect(wire.FrameDispatch) |
| 373 | if d := body.(*wire.DispatchCommand); d.TaskID != task || d.Attempt != 1 { |
| 374 | t.Fatalf("dispatch after reconnect %+v", d) |
| 375 | } |
| 376 | if s := r.checksStep(task); s.Placement != "" || s.RunnerLabel != "Tito Studio" { |
| 377 | t.Fatalf("not placed after reconnect: %+v", s) |
| 378 | } |
| 379 | |
| 380 | w.fr.Close() |
| 381 | time.Sleep(100 * time.Millisecond) |
| 382 | other, _ := r.candidate("iOS other", true) |
| 383 | if s := r.checksStep(other); s.Placement != wire.PlacementWaiting { |
| 384 | t.Fatalf("second checks did not wait: %+v", s) |
| 385 | } |
| 386 | clock := time.Now().UTC() |
| 387 | r.inst.Store.Chat().SetClock(func() time.Time { return clock }) |
| 388 | if err := r.inst.Store.Chat().Place(r.ctx); err != nil { |
| 389 | t.Fatal(err) |
| 390 | } |
| 391 | if s := r.checksStep(other); s.State != wire.StepQueued { |
| 392 | t.Fatalf("resolved before 30 minutes: %+v", s) |
| 393 | } |
| 394 | clock = clock.Add(chat.OfflineLimit) |
| 395 | if err := r.inst.Store.Chat().Place(r.ctx); err != nil { |
| 396 | t.Fatal(err) |
| 397 | } |
| 398 | k, _ := r.client.Task(r.ctx, other) |
| 399 | s := r.checksStep(other) |
| 400 | if s.State != wire.StepDone || s.Checks != wire.ChecksCannotRun || s.Reason != "runner_offline" || k.Checks == wire.ChecksPassed || k.ChecksReason != "runner_offline" { |
| 401 | t.Fatalf("offline checks %+v task %+v", s, k) |
| 402 | } |
| 403 | _, body = r.expect(wire.FrameDispatch) |
| 404 | if rv := body.(*wire.DispatchCommand); rv.TaskID != other || rv.Specialist != wire.SpecialistReview { |
| 405 | t.Fatalf("review did not proceed: %+v", rv) |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | // M6 placement at the contract: a worker that lacks a need refuses the |
| 410 | // dispatch (placement_refused) and the step is placed on another worker. |
| 411 | // The first worker's later reports for that attempt are refused. |
| 412 | func TestContractPlacementRefused(t *testing.T) { |
| 413 | r := newRunnersRig(t) |
| 414 | a, _ := r.newWorker("Mac A") |
| 415 | task, sha := r.candidate("iOS swipe", true) |
| 416 | _, body := a.expect(wire.FrameDispatch) |
| 417 | d := body.(*wire.DispatchCommand) |
| 418 | b, _ := r.newWorker("Mac B") |
| 419 | a.send(wire.FrameStep, wire.StepReport{TaskID: task, StepID: d.StepID, Attempt: 1, State: wire.StepFailed, Reason: "placement_refused", Termination: true, Findings: []wire.Finding{}}) |
| 420 | _, body = b.expect(wire.FrameDispatch) |
| 421 | d2 := body.(*wire.DispatchCommand) |
| 422 | if d2.StepID != d.StepID || d2.Attempt != 2 { |
| 423 | t.Fatalf("re-placed dispatch %+v", d2) |
| 424 | } |
| 425 | if a.lease(d.StepID, 1) || a.lease(d.StepID, 2) { |
| 426 | t.Fatal("the refusing worker may still start the step") |
| 427 | } |
| 428 | // A delayed result from the first attempt is refused. |
| 429 | a.send(wire.FrameStep, passed(task, d.StepID, 1, sha)) |
| 430 | if s := r.checksStep(task); s.Attempt != 2 || s.State != wire.StepQueued || s.RunnerLabel != "Mac B" { |
| 431 | t.Fatalf("after a stale report %+v", s) |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | // Withdrawal (M6): an unacked dispatch on a worker that went offline is |
| 436 | // withdrawn when another fitting worker is online; it never started, so |
| 437 | // the next attempt is placed at once and the first can never start. An |
| 438 | // attempt that did start is cancelled instead, and the next attempt |
| 439 | // exists only once its termination is reported. |
| 440 | func TestContractWithdrawal(t *testing.T) { |
| 441 | r := newRunnersRig(t) |
| 442 | a, tokenA := r.newWorker("Mac A") |
| 443 | genA := a.fr.Welcome.Generation |
| 444 | task, _ := r.candidate("iOS swipe", true) |
| 445 | if _, _, err := a.fr.Expect(r.ctx, wire.FrameDispatch); err != nil { // received, never acked |
| 446 | t.Fatal(err) |
| 447 | } |
| 448 | step := r.checksStep(task).ID |
| 449 | a.fr.Close() |
| 450 | b, _ := r.newWorker("Mac B") |
| 451 | // A short absence withdraws nothing. |
| 452 | if err := r.inst.Store.Chat().Place(r.ctx); err != nil { |
| 453 | t.Fatal(err) |
| 454 | } |
| 455 | b.none(wire.FrameDispatch, 100*time.Millisecond) |
| 456 | clock := time.Now().UTC().Add(chat.WithdrawAfter) |
| 457 | r.inst.Store.Chat().SetClock(func() time.Time { return clock }) |
| 458 | b.heartbeat(withLabel(WorkerHello(0), "Mac B")) |
| 459 | if err := r.inst.Store.Chat().Place(r.ctx); err != nil { |
| 460 | t.Fatal(err) |
| 461 | } |
| 462 | _, body := b.expect(wire.FrameDispatch) |
| 463 | if d := body.(*wire.DispatchCommand); d.StepID != step || d.Attempt != 2 { |
| 464 | t.Fatalf("withdrawn and re-placed %+v", d) |
| 465 | } |
| 466 | r.inst.Store.Chat().SetClock(func() time.Time { return time.Now().UTC() }) |
| 467 | a = r.dialWorker(tokenA, "Mac A", genA) |
| 468 | if a.lease(step, 1) { |
| 469 | t.Fatal("a withdrawn attempt was authorised to start") |
| 470 | } |
| 471 | a.none(wire.FrameDispatch, 200*time.Millisecond) |
| 472 | |
| 473 | // Attempt 2 starts on B, then B goes away; A is online and fits. |
| 474 | if !b.lease(step, 2) { |
| 475 | t.Fatal("attempt 2 refused") |
| 476 | } |
| 477 | b.fr.Close() |
| 478 | // (B's last heartbeat was stamped by the first fake clock.) |
| 479 | clock = time.Now().UTC().Add(2*chat.WithdrawAfter + time.Second) |
| 480 | r.inst.Store.Chat().SetClock(func() time.Time { return clock }) |
| 481 | a.heartbeat(withLabel(WorkerHello(0), "Mac A")) |
| 482 | if err := r.inst.Store.Chat().Place(r.ctx); err != nil { |
| 483 | t.Fatal(err) |
| 484 | } |
| 485 | r.inst.Store.Chat().SetClock(func() time.Time { return time.Now().UTC() }) |
| 486 | a.none(wire.FrameDispatch, 300*time.Millisecond) |
| 487 | if s := r.checksStep(task); s.Attempt != 2 || s.State == wire.StepDone { |
| 488 | t.Fatalf("a started attempt was replaced before its termination: %+v", s) |
| 489 | } |
| 490 | // B comes back, is told to cancel, and proves termination: only then |
| 491 | // is attempt 3 placed (on A). |
| 492 | bb := r.reconnectPeer(b) |
| 493 | _, body = bb.expect(wire.FrameCancelStep) |
| 494 | if c := body.(*wire.CancelStepCommand); c.StepID != step || c.Attempt != 2 { |
| 495 | t.Fatalf("cancel %+v", c) |
| 496 | } |
| 497 | bb.send(wire.FrameStep, wire.StepReport{TaskID: task, StepID: step, Attempt: 2, State: wire.StepCancelled, Reason: "cancelled", Termination: true, TerminationKind: wire.TerminationProcessGroup, Findings: []wire.Finding{}}) |
| 498 | _, body = a.expect(wire.FrameDispatch) |
| 499 | if d := body.(*wire.DispatchCommand); d.StepID != step || d.Attempt != 3 { |
| 500 | t.Fatalf("attempt 3 %+v", d) |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | // reconnectPeer dials a runner again with the same credential and |
| 505 | // generation. |
| 506 | func (r *runnersRig) reconnectPeer(p *peer) *peer { |
| 507 | r.t.Helper() |
| 508 | c, ok := r.creds[p.fr] |
| 509 | if !ok { |
| 510 | r.t.Fatal("unknown peer") |
| 511 | } |
| 512 | return r.dialWorker(c[0], c[1], p.fr.Welcome.Generation) |
| 513 | } |
| 514 | |
| 515 | func withLabel(h wire.Hello, label string) wire.Hello { |
| 516 | h.Label = label |
| 517 | return h |
| 518 | } |
| 519 | |
| 520 | // macChecks runs one task's checks on the worker to a passed report |
| 521 | // labelled sandbox, and returns the task. |
| 522 | func (r *runnersRig) macChecks(w *peer, title, sandbox string) wire.Task { |
| 523 | r.t.Helper() |
| 524 | task, sha := r.candidate(title, true) |
| 525 | _, body := w.expect(wire.FrameDispatch) |
| 526 | d := body.(*wire.DispatchCommand) |
| 527 | r.relay(w, []byte("PACK "+title)) |
| 528 | if !w.lease(d.StepID, 1) { |
| 529 | r.t.Fatal("start refused") |
| 530 | } |
| 531 | pass := passed(task, d.StepID, 1, sha) |
| 532 | pass.ChecksSandbox = sandbox |
| 533 | w.send(wire.FrameStep, pass) |
| 534 | r.expect(wire.FrameDispatch) // review on the primary |
| 535 | k, err := r.client.Task(r.ctx, task) |
| 536 | if err != nil { |
| 537 | r.t.Fatal(err) |
| 538 | } |
| 539 | return k |
| 540 | } |
| 541 | |
| 542 | // The taint is the instance's to keep: once a worker reports checks run |
| 543 | // outside the sandbox, no later check is placed on it in the sandbox, |
| 544 | // whatever it says (they wait for its sandbox); its welcome says it is tainted (so a runner whose |
| 545 | // file was deleted takes it back); a hello saying tainted is enough too. |
| 546 | func TestContractWorkerTaintIsSticky(t *testing.T) { |
| 547 | r := newRunnersRig(t) |
| 548 | w, token := r.newWorker("Tito Studio") |
| 549 | if k := r.macChecks(w, "first", wire.SandboxSeatbelt); k.ChecksSandbox != wire.SandboxSeatbelt { |
| 550 | t.Fatalf("a clean runner's sandboxed checks: %q", k.ChecksSandbox) |
| 551 | } |
| 552 | if w.fr.Welcome.Tainted { |
| 553 | t.Fatal("a clean runner welcomed as tainted") |
| 554 | } |
| 555 | if k := r.macChecks(w, "second", wire.SandboxNone); k.ChecksSandbox != wire.SandboxNone { |
| 556 | t.Fatalf("outside the sandbox: %q", k.ChecksSandbox) |
| 557 | } |
| 558 | // Tainted, so its sandbox is not trusted again, whatever its hello |
| 559 | // says: the next checks wait for it instead of running. |
| 560 | third, _ := r.candidate("third", true) |
| 561 | r.waitsForSandbox(w, third, 1) |
| 562 | again := r.dialWorker(token, "Tito Studio", w.fr.Welcome.Generation) |
| 563 | if !again.fr.Welcome.Tainted { |
| 564 | t.Fatal("the welcome does not say tainted") |
| 565 | } |
| 566 | // A second Mac that says it is tainted is, from then on. |
| 567 | other, _ := r.newWorker("Mac mini") |
| 568 | h := WorkerHello(other.fr.Welcome.Generation) |
| 569 | h.Label, h.Capabilities.Tainted = "Mac mini", true |
| 570 | other.send(wire.FrameHello, h) |
| 571 | deadline := time.Now().Add(5 * time.Second) |
| 572 | for { |
| 573 | var n int |
| 574 | if err := r.inst.DB.Writer().QueryRow("SELECT count(*) FROM runners WHERE id = ? AND tainted_at IS NOT NULL", other.fr.Welcome.RunnerID).Scan(&n); err != nil { |
| 575 | t.Fatal(err) |
| 576 | } |
| 577 | if n == 1 { |
| 578 | break |
| 579 | } |
| 580 | if time.Now().After(deadline) { |
| 581 | t.Fatal("a tainted hello not recorded") |
| 582 | } |
| 583 | time.Sleep(50 * time.Millisecond) |
| 584 | } |
| 585 | } |
| 586 | |