| 1 | package thintest |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | "time" |
| 7 | |
| 8 | "amadan.net/oficina/equipo/internal/wire" |
| 9 | ) |
| 10 | |
| 11 | // worker enrols a second runner of the owner (a worker: the first is the |
| 12 | // primary) and connects it at protocol 2. |
| 13 | func (r *toolRig) worker() *FakeRunner { |
| 14 | r.t.Helper() |
| 15 | token := r.inst.EnrolRunner(r.t).Token |
| 16 | w, err := DialRunnerHello(r.ctx, r.inst.URL, token, WorkerHello(0)) |
| 17 | if err != nil { |
| 18 | r.t.Fatal(err) |
| 19 | } |
| 20 | r.t.Cleanup(w.Close) |
| 21 | return w |
| 22 | } |
| 23 | |
| 24 | // A person's first runner is the primary and later ones are workers; the |
| 25 | // welcome says so at protocol 2 and says nothing new at protocol 1; a |
| 26 | // protocol 1 runner that is a worker is refused. |
| 27 | func TestContractRunnerRoles(t *testing.T) { |
| 28 | r := newToolRig(t) |
| 29 | shape(t, "frames/welcome.json", r.runner.Received[0]) |
| 30 | if r.runner.Welcome.Role != "" || r.runner.Welcome.Protocol != 0 { |
| 31 | t.Fatalf("protocol 1 welcome carries protocol 2 fields: %+v", r.runner.Welcome) |
| 32 | } |
| 33 | token := r.inst.EnrolRunner(t).Token |
| 34 | w, err := DialRunnerHello(r.ctx, r.inst.URL, token, WorkerHello(0)) |
| 35 | if err != nil { |
| 36 | t.Fatal(err) |
| 37 | } |
| 38 | defer w.Close() |
| 39 | if w.Welcome.Role != wire.RoleWorker || w.Welcome.Protocol != wire.Protocol2 || w.Welcome.Release == "" { |
| 40 | t.Fatalf("worker welcome %+v", w.Welcome) |
| 41 | } |
| 42 | shape(t, "frames/welcome-worker.json", w.Received[0]) |
| 43 | w.Close() |
| 44 | // The same worker at protocol 1 is refused: it could not say what it is. |
| 45 | if _, err := DialRunner(r.ctx, r.inst.URL, token, 0); CloseStatus(err) != wire.CloseRevoked { |
| 46 | t.Fatalf("protocol 1 worker connected: %v", err) |
| 47 | } |
| 48 | // The primary at protocol 2 is told it is the primary. |
| 49 | hello := WorkerHello(r.runner.Welcome.Generation) |
| 50 | hello.Platform, hello.Label, hello.Sandbox = &wire.Platform{OS: "linux", Arch: "amd64"}, "docs-t1", wire.SandboxBubblewrap |
| 51 | p, err := DialRunnerHello(r.ctx, r.inst.URL, r.rig.runner, hello) |
| 52 | if err != nil { |
| 53 | t.Fatal(err) |
| 54 | } |
| 55 | defer p.Close() |
| 56 | if p.Welcome.Role != wire.RolePrimary || p.Welcome.Protocol != wire.Protocol2 { |
| 57 | t.Fatalf("primary welcome %+v", p.Welcome) |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | // Mailbox scoping (M6): a worker sees none of the primary's commands; its |
| 62 | // ack of one is a no-op; its report of the primary's step is refused |
| 63 | // without effect; its sync settles none of the primary's steps, including |
| 64 | // an attempt still awaiting its termination proof, which still refuses |
| 65 | // Retry. |
| 66 | func TestContractWorkerScoping(t *testing.T) { |
| 67 | r := newToolRig(t) |
| 68 | ctx := r.ctx |
| 69 | running := taskID(t, r.call("w1", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "Running on the primary", "brief": "b"})) |
| 70 | r.call("w2", wire.ToolStartTask, map[string]string{"task": running, "brief": "go"}) |
| 71 | _, body := r.expect(wire.FrameDispatch) |
| 72 | d1 := body.(*wire.DispatchCommand) |
| 73 | r.step("r-w1", wire.StepReport{TaskID: running, StepID: d1.StepID, Attempt: 1, State: wire.StepRunning, BaseSHA: strings.Repeat("1", 40)}) |
| 74 | |
| 75 | pending := taskID(t, r.call("w3", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "Awaiting its proof", "brief": "b"})) |
| 76 | r.call("w4", wire.ToolStartTask, map[string]string{"task": pending, "brief": "go"}) |
| 77 | _, body = r.expect(wire.FrameDispatch) |
| 78 | d2 := body.(*wire.DispatchCommand) |
| 79 | r.step("r-w2", wire.StepReport{TaskID: pending, StepID: d2.StepID, Attempt: 1, State: wire.StepFailed, Reason: "provider_failed", BaseSHA: strings.Repeat("1", 40)}) |
| 80 | r.wait("failed, unproven", func(c *Conversation) bool { |
| 81 | s := c.Steps[d2.StepID] |
| 82 | return s.State == wire.StepFailed && !s.Terminated |
| 83 | }) |
| 84 | |
| 85 | // A third task's dispatch the primary has not acked yet. |
| 86 | unacked := taskID(t, r.call("w5", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "Not acked", "brief": "b"})) |
| 87 | r.call("w6", wire.ToolStartTask, map[string]string{"task": unacked, "brief": "go"}) |
| 88 | var held wire.Frame |
| 89 | for _, h := range r.held { |
| 90 | if h.f.Type == wire.FrameDispatch { |
| 91 | held = h.f |
| 92 | } |
| 93 | } |
| 94 | if held.ID == "" { |
| 95 | f, _, err := r.runner.Expect(ctx, wire.FrameDispatch) |
| 96 | if err != nil { |
| 97 | t.Fatal(err) |
| 98 | } |
| 99 | held = f |
| 100 | } |
| 101 | |
| 102 | w := r.worker() |
| 103 | // The worker's ack of the primary's command does nothing. |
| 104 | if err := w.Send(ctx, "wa-1", wire.FrameAck, wire.Ack{ID: held.ID}); err != nil { |
| 105 | t.Fatal(err) |
| 106 | } |
| 107 | // The worker's report of the primary's step is refused. |
| 108 | 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 { |
| 109 | t.Fatal(err) |
| 110 | } |
| 111 | if f, body, err := w.Expect(ctx, wire.FrameAck); err != nil || body.(*wire.Ack).ID != "w-rep" { |
| 112 | t.Fatalf("refused report not acked: %v %v", f, err) |
| 113 | } |
| 114 | // The worker syncs holding nothing. |
| 115 | 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 { |
| 116 | t.Fatal(err) |
| 117 | } |
| 118 | // A durable frame after the sync: once acked, both were applied. |
| 119 | if err := w.Send(ctx, "w-after", wire.FrameStep, wire.StepReport{TaskID: running, StepID: d1.StepID, Attempt: 1, State: wire.StepRunning}); err != nil { |
| 120 | t.Fatal(err) |
| 121 | } |
| 122 | if _, body, err := w.Expect(ctx, wire.FrameAck); err != nil || body.(*wire.Ack).ID != "w-after" { |
| 123 | t.Fatalf("no ack after the sync: %v", err) |
| 124 | } |
| 125 | time.Sleep(100 * time.Millisecond) |
| 126 | for id, want := range map[string]string{running: wire.StepRunning, pending: wire.StepFailed, unacked: wire.StepQueued} { |
| 127 | k, err := r.client.Task(ctx, id) |
| 128 | if err != nil { |
| 129 | t.Fatal(err) |
| 130 | } |
| 131 | if s := k.Steps[0]; s.State != want || s.Terminated { |
| 132 | t.Fatalf("worker changed the primary's step %s: %+v", id, s) |
| 133 | } |
| 134 | } |
| 135 | // Still awaiting its proof: Retry is refused. |
| 136 | if _, err := r.client.RetryTask(ctx, pending); Status(err) != 409 { |
| 137 | t.Fatalf("retry before termination: %v", err) |
| 138 | } |
| 139 | // The worker was never sent the primary's commands, and the primary's |
| 140 | // dispatch is still unacked: the primary gets it again on reconnect. |
| 141 | for _, raw := range w.Received { |
| 142 | f, _, _ := wire.ParseFrame(raw) |
| 143 | if f.Type != wire.FrameAck && f.Type != wire.FrameWelcome { |
| 144 | t.Fatalf("worker received %s", raw) |
| 145 | } |
| 146 | } |
| 147 | r.runner.Close() |
| 148 | again := r.dial(r.runner.Welcome.Generation) |
| 149 | f, _, err := again.Expect(ctx, wire.FrameDispatch) |
| 150 | for err == nil && f.ID != held.ID { |
| 151 | f, _, err = again.Expect(ctx, wire.FrameDispatch) |
| 152 | } |
| 153 | if err != nil { |
| 154 | t.Fatalf("the unacked dispatch was lost: %v", err) |
| 155 | } |
| 156 | } |
| 157 | |