| 1 | package thintest |
| 2 | |
| 3 | import ( |
| 4 | "io" |
| 5 | "log/slog" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | |
| 9 | "amadan.net/oficina/equipo/internal/equipo/chat" |
| 10 | "amadan.net/oficina/equipo/internal/wire" |
| 11 | "amadan.net/rastrillo/rastrillo/db" |
| 12 | ) |
| 13 | |
| 14 | // slice4 strips the runners migration from a stopped instance's database, |
| 15 | // leaving the schema and rows a slice 4 instance would have left. |
| 16 | func slice4(t *testing.T, path string) { |
| 17 | t.Helper() |
| 18 | d, err := db.Open(path, slog.New(slog.NewTextHandler(io.Discard, nil))) |
| 19 | if err != nil { |
| 20 | t.Fatal(err) |
| 21 | } |
| 22 | defer d.Close() |
| 23 | for _, q := range []string{ |
| 24 | "DROP INDEX runner_command_runner", "DROP INDEX chat_step_runner", "DROP TABLE runner_transfers", |
| 25 | "ALTER TABLE runners DROP COLUMN role", "ALTER TABLE runners DROP COLUMN removing_at", "ALTER TABLE runner_commands DROP COLUMN runner_id", "ALTER TABLE runner_sessions DROP COLUMN protocol", |
| 26 | "ALTER TABLE chat_tasks DROP COLUMN engineering_runner", |
| 27 | "ALTER TABLE chat_steps DROP COLUMN runner_id", "ALTER TABLE chat_steps DROP COLUMN needs", "ALTER TABLE chat_steps DROP COLUMN placement", |
| 28 | "ALTER TABLE chat_steps DROP COLUMN waiting_since", "ALTER TABLE chat_steps DROP COLUMN refused_by", "ALTER TABLE chat_steps DROP COLUMN withdrawn", |
| 29 | "ALTER TABLE chat_steps DROP COLUMN termination_kind", "ALTER TABLE chat_steps DROP COLUMN publication", "ALTER TABLE chat_steps DROP COLUMN publication_id", "ALTER TABLE chat_steps DROP COLUMN apple", |
| 30 | "DELETE FROM rastrillo_migrations WHERE id LIKE 'equipo/0054_%'", |
| 31 | } { |
| 32 | if _, err := d.Writer().Exec(q); err != nil { |
| 33 | t.Fatalf("%s: %v", q, err) |
| 34 | } |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | // Migration 0054 over a slice 4 database with live work (a running step, |
| 39 | // an attempt awaiting its termination proof, a dispatch the runner never |
| 40 | // acked, a running turn) binds it all to the primary, and the runner that |
| 41 | // held it carries on as before: the dispatch is delivered, the reports |
| 42 | // are accepted, Retry still waits for termination. |
| 43 | func TestContractMigrationOverSlice4Work(t *testing.T) { |
| 44 | r := newToolRig(t) |
| 45 | ctx := r.ctx |
| 46 | running := taskID(t, r.call("m1", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "Running when the upgrade lands", "brief": "b"})) |
| 47 | r.call("m2", wire.ToolStartTask, map[string]string{"task": running, "brief": "go"}) |
| 48 | _, body := r.expect(wire.FrameDispatch) |
| 49 | d1 := body.(*wire.DispatchCommand) |
| 50 | r.step("r-m1", wire.StepReport{TaskID: running, StepID: d1.StepID, Attempt: 1, State: wire.StepRunning, BaseSHA: strings.Repeat("1", 40)}) |
| 51 | pending := taskID(t, r.call("m3", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "Awaiting its proof", "brief": "b"})) |
| 52 | r.call("m4", wire.ToolStartTask, map[string]string{"task": pending, "brief": "go"}) |
| 53 | _, body = r.expect(wire.FrameDispatch) |
| 54 | d2 := body.(*wire.DispatchCommand) |
| 55 | r.step("r-m2", wire.StepReport{TaskID: pending, StepID: d2.StepID, Attempt: 1, State: wire.StepFailed, Reason: "provider_failed", BaseSHA: strings.Repeat("1", 40)}) |
| 56 | unacked := taskID(t, r.call("m5", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "Never acked", "brief": "b"})) |
| 57 | r.call("m6", wire.ToolStartTask, map[string]string{"task": unacked, "brief": "go"}) |
| 58 | |
| 59 | r.runner.Close() |
| 60 | r.inst.Stop() |
| 61 | slice4(t, r.inst.Path) |
| 62 | r.inst.Restart(t) |
| 63 | n, err := chat.UnboundSteps(r.inst.DB.G) |
| 64 | if err != nil || n != 0 { |
| 65 | t.Fatalf("unbound steps: %d %v", n, err) |
| 66 | } |
| 67 | |
| 68 | // The same runner reconnects: the unacked dispatch is still its own. |
| 69 | r.runner = r.dial(r.runner.Welcome.Generation) |
| 70 | f, body, err := r.runner.Expect(ctx, wire.FrameDispatch) |
| 71 | for err == nil && body.(*wire.DispatchCommand).TaskID != unacked { |
| 72 | f, body, err = r.runner.Expect(ctx, wire.FrameDispatch) |
| 73 | } |
| 74 | if err != nil { |
| 75 | t.Fatalf("unacked dispatch lost in the upgrade: %v", err) |
| 76 | } |
| 77 | shape(t, "frames/dispatch.json", r.runner.Received[len(r.runner.Received)-1]) |
| 78 | if err := r.runner.Send(ctx, "a-"+f.ID, wire.FrameAck, wire.Ack{ID: f.ID}); err != nil { |
| 79 | t.Fatal(err) |
| 80 | } |
| 81 | // Its sync holds the running step: nothing is settled. |
| 82 | if err := r.runner.Send(ctx, "r-sync-m", wire.FrameSync, wire.Sync{Sessions: []wire.SessionState{}, Runs: []wire.Run{{TurnID: r.turn, Attempt: 1, State: "running"}}, Steps: []wire.StepRun{{TaskID: running, StepID: d1.StepID, Attempt: 1, State: "running"}, {TaskID: pending, StepID: d2.StepID, Attempt: 1, State: "pending"}, {TaskID: unacked, StepID: body.(*wire.DispatchCommand).StepID, Attempt: 1, State: "running"}}}); err != nil { |
| 83 | t.Fatal(err) |
| 84 | } |
| 85 | // Retry still waits for the termination proof. |
| 86 | if _, err := r.client.RetryTask(ctx, pending); Status(err) != 409 { |
| 87 | t.Fatalf("retry before termination after the upgrade: %v", err) |
| 88 | } |
| 89 | r.step("r-m3", wire.StepReport{TaskID: pending, StepID: d2.StepID, Attempt: 1, State: wire.StepFailed, Reason: "provider_failed", Termination: true}) |
| 90 | if acc, err := r.client.RetryTask(ctx, pending); err != nil || acc.Attempt != 2 { |
| 91 | t.Fatalf("retry after the proof: %+v %v", acc, err) |
| 92 | } |
| 93 | // The running step's report is accepted from the runner it was bound to. |
| 94 | r.step("r-m4", wire.StepReport{TaskID: running, StepID: d1.StepID, Attempt: 1, State: wire.StepFailed, Reason: "provider_failed", Termination: true}) |
| 95 | if k, err := r.client.Task(ctx, running); err != nil || k.Steps[0].State != wire.StepFailed || !k.Steps[0].Terminated { |
| 96 | t.Fatalf("the running step's report after the upgrade: %+v %v", k, err) |
| 97 | } |
| 98 | // The turn that was running is still the primary's. |
| 99 | if k, _ := r.client.Turn(ctx, r.turn); k.State != wire.TurnRunning { |
| 100 | t.Fatalf("turn after the upgrade: %+v", k) |
| 101 | } |
| 102 | } |
| 103 | |