package thintest import ( "strings" "testing" "time" "amadan.net/oficina/equipo/internal/wire" ) // worker enrols a second runner of the owner (a worker: the first is the // primary) and connects it at protocol 2. func (r *toolRig) worker() *FakeRunner { r.t.Helper() token := r.inst.EnrolRunner(r.t).Token w, err := DialRunnerHello(r.ctx, r.inst.URL, token, WorkerHello(0)) if err != nil { r.t.Fatal(err) } r.t.Cleanup(w.Close) return w } // A person's first runner is the primary and later ones are workers; the // welcome says so at protocol 2 and says nothing new at protocol 1; a // protocol 1 runner that is a worker is refused. func TestContractRunnerRoles(t *testing.T) { r := newToolRig(t) shape(t, "frames/welcome.json", r.runner.Received[0]) if r.runner.Welcome.Role != "" || r.runner.Welcome.Protocol != 0 { t.Fatalf("protocol 1 welcome carries protocol 2 fields: %+v", r.runner.Welcome) } token := r.inst.EnrolRunner(t).Token w, err := DialRunnerHello(r.ctx, r.inst.URL, token, WorkerHello(0)) if err != nil { t.Fatal(err) } defer w.Close() if w.Welcome.Role != wire.RoleWorker || w.Welcome.Protocol != wire.Protocol2 || w.Welcome.Release == "" { t.Fatalf("worker welcome %+v", w.Welcome) } shape(t, "frames/welcome-worker.json", w.Received[0]) w.Close() // The same worker at protocol 1 is refused: it could not say what it is. if _, err := DialRunner(r.ctx, r.inst.URL, token, 0); CloseStatus(err) != wire.CloseRevoked { t.Fatalf("protocol 1 worker connected: %v", err) } // The primary at protocol 2 is told it is the primary. hello := WorkerHello(r.runner.Welcome.Generation) hello.Platform, hello.Label, hello.Sandbox = &wire.Platform{OS: "linux", Arch: "amd64"}, "docs-t1", wire.SandboxBubblewrap p, err := DialRunnerHello(r.ctx, r.inst.URL, r.rig.runner, hello) if err != nil { t.Fatal(err) } defer p.Close() if p.Welcome.Role != wire.RolePrimary || p.Welcome.Protocol != wire.Protocol2 { t.Fatalf("primary welcome %+v", p.Welcome) } } // Mailbox scoping (M6): a worker sees none of the primary's commands; its // ack of one is a no-op; its report of the primary's step is refused // without effect; its sync settles none of the primary's steps, including // an attempt still awaiting its termination proof, which still refuses // Retry. func TestContractWorkerScoping(t *testing.T) { r := newToolRig(t) ctx := r.ctx running := taskID(t, r.call("w1", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "Running on the primary", "brief": "b"})) r.call("w2", wire.ToolStartTask, map[string]string{"task": running, "brief": "go"}) _, body := r.expect(wire.FrameDispatch) d1 := body.(*wire.DispatchCommand) r.step("r-w1", wire.StepReport{TaskID: running, StepID: d1.StepID, Attempt: 1, State: wire.StepRunning, BaseSHA: strings.Repeat("1", 40)}) pending := taskID(t, r.call("w3", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "Awaiting its proof", "brief": "b"})) r.call("w4", wire.ToolStartTask, map[string]string{"task": pending, "brief": "go"}) _, body = r.expect(wire.FrameDispatch) d2 := body.(*wire.DispatchCommand) r.step("r-w2", wire.StepReport{TaskID: pending, StepID: d2.StepID, Attempt: 1, State: wire.StepFailed, Reason: "provider_failed", BaseSHA: strings.Repeat("1", 40)}) r.wait("failed, unproven", func(c *Conversation) bool { s := c.Steps[d2.StepID] return s.State == wire.StepFailed && !s.Terminated }) // A third task's dispatch the primary has not acked yet. unacked := taskID(t, r.call("w5", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "Not acked", "brief": "b"})) r.call("w6", wire.ToolStartTask, map[string]string{"task": unacked, "brief": "go"}) var held wire.Frame for _, h := range r.held { if h.f.Type == wire.FrameDispatch { held = h.f } } if held.ID == "" { f, _, err := r.runner.Expect(ctx, wire.FrameDispatch) if err != nil { t.Fatal(err) } held = f } w := r.worker() // The worker's ack of the primary's command does nothing. if err := w.Send(ctx, "wa-1", wire.FrameAck, wire.Ack{ID: held.ID}); err != nil { t.Fatal(err) } // The worker's report of the primary's step is refused. if err := w.Send(ctx, "w-rep", wire.FrameStep, wire.StepReport{TaskID: running, StepID: d1.StepID, Attempt: 1, State: wire.StepFailed, Reason: "provider_failed", Termination: true}); err != nil { t.Fatal(err) } if f, body, err := w.Expect(ctx, wire.FrameAck); err != nil || body.(*wire.Ack).ID != "w-rep" { t.Fatalf("refused report not acked: %v %v", f, err) } // The worker syncs holding nothing. if err := w.Send(ctx, "w-sync", wire.FrameSync, wire.Sync{Sessions: []wire.SessionState{}, Runs: []wire.Run{}, Steps: []wire.StepRun{}, Asks: []wire.AskRun{}}); err != nil { t.Fatal(err) } // A durable frame after the sync: once acked, both were applied. if err := w.Send(ctx, "w-after", wire.FrameStep, wire.StepReport{TaskID: running, StepID: d1.StepID, Attempt: 1, State: wire.StepRunning}); err != nil { t.Fatal(err) } if _, body, err := w.Expect(ctx, wire.FrameAck); err != nil || body.(*wire.Ack).ID != "w-after" { t.Fatalf("no ack after the sync: %v", err) } time.Sleep(100 * time.Millisecond) for id, want := range map[string]string{running: wire.StepRunning, pending: wire.StepFailed, unacked: wire.StepQueued} { k, err := r.client.Task(ctx, id) if err != nil { t.Fatal(err) } if s := k.Steps[0]; s.State != want || s.Terminated { t.Fatalf("worker changed the primary's step %s: %+v", id, s) } } // Still awaiting its proof: Retry is refused. if _, err := r.client.RetryTask(ctx, pending); Status(err) != 409 { t.Fatalf("retry before termination: %v", err) } // The worker was never sent the primary's commands, and the primary's // dispatch is still unacked: the primary gets it again on reconnect. for _, raw := range w.Received { f, _, _ := wire.ParseFrame(raw) if f.Type != wire.FrameAck && f.Type != wire.FrameWelcome { t.Fatalf("worker received %s", raw) } } r.runner.Close() again := r.dial(r.runner.Welcome.Generation) f, _, err := again.Expect(ctx, wire.FrameDispatch) for err == nil && f.ID != held.ID { f, _, err = again.Expect(ctx, wire.FrameDispatch) } if err != nil { t.Fatalf("the unacked dispatch was lost: %v", err) } }