| 1 | package runner |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "strconv" |
| 7 | "testing" |
| 8 | |
| 9 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 10 | "amadan.net/oficina/equipo/internal/runnerproto" |
| 11 | ) |
| 12 | |
| 13 | type recoveryRefusalAPI struct { |
| 14 | *coordinatorAPIStub |
| 15 | operation string |
| 16 | status int |
| 17 | refusedID string |
| 18 | refusals, claims int |
| 19 | offers []runnerproto.CoordinatorTurn |
| 20 | } |
| 21 | |
| 22 | func (a *recoveryRefusalAPI) CoordinatorCall(ctx context.Context, op string, q runnerproto.CoordinatorRequest, out any) error { |
| 23 | id := q.TurnID |
| 24 | if q.Claim != nil { |
| 25 | id = q.Claim.Turn.ID |
| 26 | } |
| 27 | if op == a.operation && id == a.refusedID { |
| 28 | a.refusals++ |
| 29 | return &RefusalError{Reason: "coordinator.fenced", Status: a.status} |
| 30 | } |
| 31 | if op == "poll" { |
| 32 | *out.(*[]runnerproto.CoordinatorTurn) = a.offers |
| 33 | return nil |
| 34 | } |
| 35 | if op == "claim" { |
| 36 | a.claims++ |
| 37 | } |
| 38 | return a.coordinatorAPIStub.CoordinatorCall(ctx, op, q, out) |
| 39 | } |
| 40 | func TestCoordinatorRecoveryCallRefusalKeepsServing(t *testing.T) { |
| 41 | for _, op := range []string{"recover", "containment"} { |
| 42 | for _, status := range []int{401, 403, 409, 422} { |
| 43 | t.Run(op+"/"+strconv.Itoa(status), func(t *testing.T) { |
| 44 | ctx := context.Background() |
| 45 | s, base, launcher, c := coordinatorServiceFixture(t) |
| 46 | api := &recoveryRefusalAPI{coordinatorAPIStub: base, operation: op, status: status, refusedID: c.Turn.ID} |
| 47 | s.API = api |
| 48 | if op == "recover" { |
| 49 | mustJournal(t, s.Journal.saveState(ctx, "coordinator-claim", c.Turn.ID, runnerproto.CoordinatorRequest{TurnID: c.Turn.ID, Revision: c.Turn.Revision})) |
| 50 | } else { |
| 51 | mustJournal(t, s.Journal.saveState(ctx, "coordinator", c.Turn.ID, coordinatorState{Claim: c})) |
| 52 | } |
| 53 | for restart := 0; restart < 2; restart++ { |
| 54 | path := s.Journal.path |
| 55 | mustJournal(t, s.Journal.Close()) |
| 56 | var err error |
| 57 | s.Journal, err = OpenJournal(ctx, path) |
| 58 | mustJournal(t, err) |
| 59 | defer s.Journal.Close() |
| 60 | launcher.j = s.Journal |
| 61 | runSpecialistAfterCoordinator(t, s) |
| 62 | if api.refusals != 1 { |
| 63 | t.Fatal("repeated recovery refusal", api.refusals) |
| 64 | } |
| 65 | var states []coordinatorState |
| 66 | mustJournal(t, s.Journal.readStates(ctx, "coordinator", &states)) |
| 67 | if len(states) != 1 || !states[0].Refused || !states[0].Finished { |
| 68 | t.Fatal("recovery refusal not durably retired", states) |
| 69 | } |
| 70 | if states[0].RefusalOperation != op || states[0].RefusalStatus != status { |
| 71 | t.Fatal("refusal operation/status missing", states[0]) |
| 72 | } |
| 73 | if op == "containment" && (states[0].Containment == nil || states[0].Containment.CheckedAt.IsZero()) { |
| 74 | t.Fatal("refused report lost local containment proof") |
| 75 | } |
| 76 | if err := s.RunTurn(ctx, c); !errors.Is(err, provider.ErrUncertain) { |
| 77 | t.Fatal("refused prompt can restart", err) |
| 78 | } |
| 79 | } |
| 80 | if launcher.starts != 0 { |
| 81 | t.Fatal("recovery resent prompt", launcher.starts) |
| 82 | } |
| 83 | // A stale refused offer cannot prevent a later coordinator from running. |
| 84 | next := c |
| 85 | next.Turn.ID = "next-turn" |
| 86 | next.Scope.Job++ |
| 87 | next.Handle.Attempt++ |
| 88 | next.Admission.Scope, next.Admission.Handle = next.Scope, next.Handle |
| 89 | base.c = next |
| 90 | api.offers = []runnerproto.CoordinatorTurn{c.Turn, next.Turn} |
| 91 | mustJournal(t, s.Poll(ctx)) |
| 92 | if api.claims != 1 || launcher.starts != 1 || !base.delivery { |
| 93 | t.Fatal("refused offer blocked another coordinator", api.claims, launcher.starts, base.delivery) |
| 94 | } |
| 95 | }) |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | func TestCoordinatorContainmentCallRefusalOnCleanup(t *testing.T) { |
| 101 | s, base, launcher, c := coordinatorServiceFixture(t) |
| 102 | api := &recoveryRefusalAPI{coordinatorAPIStub: base, operation: "containment", status: 403, refusedID: c.Turn.ID} |
| 103 | s.API = api |
| 104 | mustJournal(t, s.RunTurn(context.Background(), c)) |
| 105 | mustJournal(t, s.Recover(context.Background())) |
| 106 | if api.refusals != 1 || launcher.starts != 1 { |
| 107 | t.Fatal("cleanup repeated refused call or prompt", api.refusals, launcher.starts) |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | func TestCoordinatorRefusalInterruptedRetirement(t *testing.T) { |
| 112 | ctx := context.Background() |
| 113 | s, base, _, c := coordinatorServiceFixture(t) |
| 114 | api := &recoveryRefusalAPI{coordinatorAPIStub: base, operation: "containment", status: 403, refusedID: c.Turn.ID} |
| 115 | s.API = api |
| 116 | mustJournal(t, s.Journal.saveState(ctx, "coordinator", c.Turn.ID, coordinatorState{Claim: c})) |
| 117 | _, err := s.Journal.db.ExecContext(ctx, `CREATE TRIGGER interrupt_retirement BEFORE INSERT ON journal_supervisor WHEN NEW.kind='coordinator' AND json_extract(NEW.body,'$.Finished')=1 BEGIN SELECT RAISE(ABORT,'interrupted retirement'); END`) |
| 118 | mustJournal(t, err) |
| 119 | if err := s.Recover(ctx); err == nil { |
| 120 | t.Fatal("fixture did not interrupt retirement") |
| 121 | } |
| 122 | path := s.Journal.path |
| 123 | mustJournal(t, s.Journal.Close()) |
| 124 | s.Journal, err = OpenJournal(ctx, path) |
| 125 | mustJournal(t, err) |
| 126 | defer s.Journal.Close() |
| 127 | _, err = s.Journal.db.ExecContext(ctx, "DROP TRIGGER interrupt_retirement") |
| 128 | mustJournal(t, err) |
| 129 | mustJournal(t, s.Recover(ctx)) |
| 130 | var states []coordinatorState |
| 131 | mustJournal(t, s.Journal.readStates(ctx, "coordinator", &states)) |
| 132 | if api.refusals != 1 || len(states) != 1 || !states[0].Finished || !states[0].Refused { |
| 133 | t.Fatal("interrupted retirement repeated refused call", api.refusals, states) |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | func TestCoordinatorRefusalRequiresLocalContainment(t *testing.T) { |
| 138 | s, base, _, c := coordinatorServiceFixture(t) |
| 139 | api := &recoveryRefusalAPI{coordinatorAPIStub: base, operation: "containment", status: 403, refusedID: c.Turn.ID} |
| 140 | s.API = api |
| 141 | s.Contain = func(context.Context, runnerproto.CoordinatorClaim) (runnerproto.ContainmentProof, error) { |
| 142 | return runnerproto.ContainmentProof{}, provider.ErrContainment |
| 143 | } |
| 144 | mustJournal(t, s.Journal.saveState(context.Background(), "coordinator", c.Turn.ID, coordinatorState{Claim: c})) |
| 145 | if err := s.Recover(context.Background()); !errors.Is(err, provider.ErrContainment) { |
| 146 | t.Fatal("local containment failure ignored", err) |
| 147 | } |
| 148 | var states []coordinatorState |
| 149 | mustJournal(t, s.Journal.readStates(context.Background(), "coordinator", &states)) |
| 150 | if api.refusals != 0 || states[0].Finished || states[0].Refused { |
| 151 | t.Fatal("uncontained turn retired") |
| 152 | } |
| 153 | } |
| 154 | |