| 1 | package equipotest |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "testing" |
| 7 | "time" |
| 8 | |
| 9 | "amadan.net/rastrillo/rastrillo/db" |
| 10 | "gorm.io/gorm" |
| 11 | |
| 12 | "amadan.net/oficina/equipo/internal/equipo" |
| 13 | "amadan.net/oficina/equipo/internal/equipo/team" |
| 14 | ) |
| 15 | |
| 16 | func TestRecoveryReleaseOwnerAndRevision(t *testing.T) { |
| 17 | d, path := executionDB(t) |
| 18 | ctx := context.Background() |
| 19 | s := equipo.NewStore(d) |
| 20 | executionMust(t, s.Bootstrap(ctx, []string{ownerAddress})) |
| 21 | executionMust(t, s.SetMember(ctx, equipo.Actor{Address: ownerAddress}, "member@example.com", equipo.RoleMember, true, 0)) |
| 22 | x, err := s.AttachExecution(ctx, path, executionOptions()) |
| 23 | executionMust(t, err) |
| 24 | defer x.Core.Close() |
| 25 | executionMust(t, x.Core.Pause(ctx, "restore review")) |
| 26 | snap, err := x.Core.RecoverySnapshot(ctx) |
| 27 | executionMust(t, err) |
| 28 | if err = s.ReleaseExecution(ctx, equipo.Actor{Address: "member@example.com"}, snap.Revision, "reviewed", snap.Hash); !errors.Is(err, equipo.ErrForbidden) { |
| 29 | t.Fatalf("member release: %v", err) |
| 30 | } |
| 31 | if err = s.ReleaseExecution(ctx, equipo.Actor{Address: ownerAddress}, snap.Revision, "reviewed", snap.Hash); err == nil { |
| 32 | t.Fatal("missing reconciliation accepted") |
| 33 | } |
| 34 | _, err = x.Core.Resume(ctx) |
| 35 | executionMust(t, err) |
| 36 | snap, err = x.Core.RecoverySnapshot(ctx) |
| 37 | executionMust(t, err) |
| 38 | for _, bad := range []struct { |
| 39 | rev int64 |
| 40 | hash, reason string |
| 41 | }{{snap.Revision - 1, snap.Hash, "reviewed"}, {snap.Revision, "wrong", "reviewed"}, {snap.Revision, snap.Hash, ""}} { |
| 42 | if err = s.ReleaseExecution(ctx, equipo.Actor{Address: ownerAddress}, bad.rev, bad.reason, bad.hash); err == nil { |
| 43 | t.Fatal("invalid release accepted") |
| 44 | } |
| 45 | } |
| 46 | _, err = x.Core.Status(ctx) |
| 47 | executionMust(t, err) |
| 48 | same, err := x.Core.RecoverySnapshot(ctx) |
| 49 | executionMust(t, err) |
| 50 | if snap.Revision != same.Revision || snap.Hash != same.Hash { |
| 51 | t.Fatal("read changed recovery snapshot") |
| 52 | } |
| 53 | executionMust(t, s.ReleaseExecution(ctx, equipo.Actor{Address: ownerAddress}, snap.Revision, "reviewed", snap.Hash)) |
| 54 | st, err := x.Core.Status(ctx) |
| 55 | executionMust(t, err) |
| 56 | if st.Quarantine || !st.AdmissionPaused { |
| 57 | t.Fatalf("release reopened admission: %+v", st) |
| 58 | } |
| 59 | _, err = x.Core.Resume(ctx) |
| 60 | executionMust(t, err) |
| 61 | if scalar(t, d, "SELECT count(*) FROM audit_events WHERE kind='execution.release'") != 1 || scalar(t, d, "SELECT count(*) FROM recovery_events WHERE kind='release'") != 1 { |
| 62 | t.Fatal("release audit missing") |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | func TestProjectGrantChargeAgreement(t *testing.T) { |
| 67 | _, _, p, clock, auth, req := authorityFixture(t) |
| 68 | req.Project = team.ProjectID(p.ID) + 1 |
| 69 | requireRevoked(t, auth, req) |
| 70 | req.Project = team.ProjectID(p.ID) |
| 71 | core, err := team.Open(context.Background(), t.TempDir()+"/core.db", team.Options{Clock: clock, Authority: auth}) |
| 72 | executionMust(t, err) |
| 73 | defer core.Close() |
| 74 | _, err = core.Resume(context.Background()) |
| 75 | executionMust(t, err) |
| 76 | spec := team.JobSpec{Account: req.Account, Project: req.Project + 1, Requester: "member@example.com", GrantRef: req.Grant, Target: req.Target, Policy: team.DefaultPolicy(5000000, team.Rates{Version: 1}), Evidence: team.EvidenceRequirement{Predicate: team.PredicateSoftwareFix, ExpectedSymptom: team.FixtureSymptom, CheckDefinitionHash: team.CheckDefinitionHash}, Steps: team.SoftwareSteps(false)} |
| 77 | if _, err = core.CreateJob(context.Background(), spec); !errors.Is(err, team.ErrGrantRevoked) { |
| 78 | t.Fatalf("wrong project created: %v", err) |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | func recoveryRuntime(t *testing.T) (*equipo.Store, *db.DB, *equipo.Execution, equipo.Project, *team.FakeClock, team.AdmissionRequest) { |
| 83 | t.Helper() |
| 84 | s, d, p, clock, auth, req := authorityFixture(t) |
| 85 | var seq int |
| 86 | var name, path string |
| 87 | executionMust(t, d.Writer().QueryRow(`PRAGMA database_list`).Scan(&seq, &name, &path)) |
| 88 | x, err := s.AttachExecution(context.Background(), path, team.Options{Clock: clock, Authority: auth, Adapters: map[team.Operation]team.Adapter{team.OpProviderCall: team.NewFakeProvider("fake", "model", map[string]string{})}}) |
| 89 | executionMust(t, err) |
| 90 | t.Cleanup(func() { x.Core.Close() }) |
| 91 | _, err = x.Core.Resume(context.Background()) |
| 92 | executionMust(t, err) |
| 93 | return s, d, x, p, clock, req |
| 94 | } |
| 95 | |
| 96 | func recoveryJob(t *testing.T, x *equipo.Execution, req team.AdmissionRequest, key string) (team.Scope, team.Handle, team.ActionID) { |
| 97 | t.Helper() |
| 98 | ctx := context.Background() |
| 99 | job, err := x.Core.CreateJob(ctx, team.JobSpec{Account: req.Account, Project: req.Project, Requester: "member@example.com", GrantRef: req.Grant, Target: req.Target, Policy: team.DefaultPolicy(5000000, team.Rates{Version: 1}), Evidence: team.EvidenceRequirement{Predicate: team.PredicateSoftwareFix, ExpectedSymptom: team.FixtureSymptom, CheckDefinitionHash: team.CheckDefinitionHash}, Steps: team.SoftwareSteps(false)}) |
| 100 | executionMust(t, err) |
| 101 | scope := team.Scope{Account: job.Account, Job: job.ID} |
| 102 | h, err := x.Core.Claim(ctx, scope, "reproduce", key, key) |
| 103 | executionMust(t, err) |
| 104 | id, err := x.Core.Intend(ctx, scope, h, team.IntentSpec{Key: key, Operation: team.OpProviderCall, Payload: []byte(`{"task":"implement","prompt":"test"}`), Reserve: team.Usage{InputTokens: 10}}) |
| 105 | executionMust(t, err) |
| 106 | return scope, h, id |
| 107 | } |
| 108 | |
| 109 | func TestProjectGrantChargeAgreementRenewAndAdmit(t *testing.T) { |
| 110 | for _, operation := range []string{"renew", "admit"} { |
| 111 | t.Run(operation, func(t *testing.T) { |
| 112 | _, _, x, _, _, req := recoveryRuntime(t) |
| 113 | scope, h, id := recoveryJob(t, x, req, operation) |
| 114 | ctx := context.Background() |
| 115 | executionMust(t, x.Write(ctx, func(tx *gorm.DB) error { |
| 116 | return tx.Exec(`UPDATE jobs SET project_id=project_id+1 WHERE id=?`, scope.Job).Error |
| 117 | })) |
| 118 | var err error |
| 119 | if operation == "renew" { |
| 120 | _, err = x.Core.Renew(ctx, scope, h) |
| 121 | } else { |
| 122 | _, err = x.Core.Admit(ctx, scope, id) |
| 123 | } |
| 124 | if !errors.Is(err, team.ErrGrantRevoked) { |
| 125 | t.Fatalf("wrong project %s: %v", operation, err) |
| 126 | } |
| 127 | }) |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | func TestRuntimeAuthorityEditFencesOnlyChangedGrants(t *testing.T) { |
| 132 | s, d, x, p, _, req := recoveryRuntime(t) |
| 133 | ctx := context.Background() |
| 134 | owner := equipo.Actor{Address: ownerAddress} |
| 135 | other := p |
| 136 | other.ID = 0 |
| 137 | other.Revision = 0 |
| 138 | other.Repository = "amadan.net/oficina/second" |
| 139 | other = saveProject(t, s, other, []equipo.DecisionOwner{{Decision: "work", Address: "member@example.com"}, {Decision: "merge", Address: ownerAddress}}, "second") |
| 140 | rb := req |
| 141 | rb.Project = team.ProjectID(other.ID) |
| 142 | rb.Grant = equipo.GrantRef(other.ID, "member@example.com") |
| 143 | rb.Target.Repository = other.Repository |
| 144 | a, ha, ia := recoveryJob(t, x, req, "a") |
| 145 | b, hb, ib := recoveryJob(t, x, rb, "b") |
| 146 | // Labels and non-work routing do not alter execution authority. |
| 147 | p.Name = "Renamed" |
| 148 | p = saveProject(t, s, p, []equipo.DecisionOwner{{Decision: "work", Address: "member@example.com"}, {Decision: "merge", Address: "member@example.com"}}, "label") |
| 149 | var account equipo.Account |
| 150 | executionMust(t, d.G.First(&account, p.AccountID).Error) |
| 151 | account.Label = "Renamed account" |
| 152 | _, err := s.SaveAccount(ctx, owner, account) |
| 153 | executionMust(t, err) |
| 154 | _, err = x.Core.Renew(ctx, a, ha) |
| 155 | executionMust(t, err) |
| 156 | _, err = x.Core.Renew(ctx, b, hb) |
| 157 | executionMust(t, err) |
| 158 | if scalar(t, d, "SELECT count(*) FROM jobs WHERE grant_stale<>0") != 0 { |
| 159 | t.Fatal("metadata edit staled grants") |
| 160 | } |
| 161 | // The target changes only A, leaving B's admission and renewal live. |
| 162 | p.DefaultBranch = "trunk" |
| 163 | p = saveProject(t, s, p, []equipo.DecisionOwner{{Decision: "work", Address: "member@example.com"}, {Decision: "merge", Address: ownerAddress}}, "target") |
| 164 | _, err = x.Core.Resume(ctx) |
| 165 | executionMust(t, err) |
| 166 | if _, err = x.Core.Renew(ctx, a, ha); err == nil { |
| 167 | t.Fatal("target edit did not fence A") |
| 168 | } |
| 169 | if _, err = x.Core.Admit(ctx, a, ia); err == nil { |
| 170 | t.Fatal("target edit did not revoke A intent") |
| 171 | } |
| 172 | _, err = x.Core.Renew(ctx, b, hb) |
| 173 | executionMust(t, err) |
| 174 | _, err = x.Core.Admit(ctx, b, ib) |
| 175 | executionMust(t, err) |
| 176 | job, err := x.Core.GetJob(ctx, a) |
| 177 | executionMust(t, err) |
| 178 | if job.State != team.JobCancelled { |
| 179 | t.Fatal("target-changed job not cancelled") |
| 180 | } |
| 181 | // A roster epoch change stales all remaining grants, even unrelated people. |
| 182 | executionMust(t, s.SetMember(ctx, owner, "new@example.com", equipo.RoleMember, true, 0)) |
| 183 | if scalar(t, d, "SELECT count(*) FROM attempts WHERE state='active'") != 0 { |
| 184 | t.Fatal("roster commit left active attempts") |
| 185 | } |
| 186 | if _, err = x.Core.Renew(ctx, b, hb); err == nil { |
| 187 | t.Fatal("roster epoch did not fence B") |
| 188 | } |
| 189 | attempts, err := x.Core.Attempts(ctx, b) |
| 190 | executionMust(t, err) |
| 191 | if attempts[0].State != team.AttemptStopping { |
| 192 | t.Fatal("roster change did not request stop") |
| 193 | } |
| 194 | executionMust(t, x.Core.Contain(ctx, b, hb.Attempt, "supervisor")) |
| 195 | _, err = x.Core.Resume(ctx) |
| 196 | executionMust(t, err) |
| 197 | _, err = x.Core.Claim(ctx, b, "reproduce", "new", "new") |
| 198 | executionMust(t, err) |
| 199 | } |
| 200 | |
| 201 | func TestRuntimeAuthorityBudgetSyncAndRemoval(t *testing.T) { |
| 202 | s, d, x, p, _, req := recoveryRuntime(t) |
| 203 | ctx := context.Background() |
| 204 | owner := equipo.Actor{Address: ownerAddress} |
| 205 | l := team.PeriodLimits{Calls: 100, InputTokens: 10000, OutputTokens: 10000, Seconds: 10000} |
| 206 | executionMust(t, s.SaveBudgets(ctx, owner, equipo.BudgetConfig{Team: l, Projects: map[team.ProjectID]team.PeriodLimits{req.Project: l}})) |
| 207 | scope, h, _ := recoveryJob(t, x, req, "budget") |
| 208 | l.Calls = 90 |
| 209 | executionMust(t, s.SaveBudgets(ctx, owner, equipo.BudgetConfig{Revision: 1, Team: l, Projects: map[team.ProjectID]team.PeriodLimits{req.Project: l}})) |
| 210 | _, err := x.Core.Renew(ctx, scope, h) |
| 211 | executionMust(t, err) |
| 212 | if scalar(t, d, "SELECT count(*) FROM period_budgets WHERE revision=2") != 1 || scalar(t, d, "SELECT count(*) FROM jobs WHERE grant_stale<>0") != 0 { |
| 213 | t.Fatal("budget edit did not synchronize independently") |
| 214 | } |
| 215 | // An owner need not be a decision owner to have a grant; removal cancels it. |
| 216 | executionMust(t, s.SetMember(ctx, owner, "second@example.com", equipo.RoleOwner, true, 0)) |
| 217 | r := req |
| 218 | r.Grant = equipo.GrantRef(p.ID, "second@example.com") |
| 219 | removed, rh, _ := recoveryJob(t, x, r, "removed") |
| 220 | executionMust(t, s.SetMember(ctx, owner, "second@example.com", equipo.RoleOwner, false, 1)) |
| 221 | job, err := x.Core.GetJob(ctx, removed) |
| 222 | executionMust(t, err) |
| 223 | if job.State != team.JobCancelled { |
| 224 | t.Fatal("member removal did not cancel work") |
| 225 | } |
| 226 | attempts, err := x.Core.Attempts(ctx, removed) |
| 227 | executionMust(t, err) |
| 228 | if attempts[0].State != team.AttemptStopping { |
| 229 | t.Fatal("member removal omitted stop") |
| 230 | } |
| 231 | if _, err = x.Core.Renew(ctx, removed, rh); err == nil { |
| 232 | t.Fatal("removed member renewed") |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | func TestRecoveryReleaseUncontainedAndAtomicAudit(t *testing.T) { |
| 237 | s, d, x, _, _, req := recoveryRuntime(t) |
| 238 | ctx := context.Background() |
| 239 | owner := equipo.Actor{Address: ownerAddress} |
| 240 | scope, h, _ := recoveryJob(t, x, req, "release") |
| 241 | executionMust(t, x.Core.Pause(ctx, "owner review")) |
| 242 | _, err := x.Core.Resume(ctx) |
| 243 | executionMust(t, err) |
| 244 | snap, err := x.Core.RecoverySnapshot(ctx) |
| 245 | executionMust(t, err) |
| 246 | if err = s.ReleaseExecution(ctx, owner, snap.Revision, "reviewed", snap.Hash); err == nil { |
| 247 | t.Fatal("uncontained release accepted") |
| 248 | } |
| 249 | executionMust(t, x.Core.StopAttempt(ctx, scope, h, "cleanup")) |
| 250 | executionMust(t, x.Core.Contain(ctx, scope, h.Attempt, "supervisor")) |
| 251 | _, err = x.Core.Resume(ctx) |
| 252 | executionMust(t, err) |
| 253 | snap, err = x.Core.RecoverySnapshot(ctx) |
| 254 | executionMust(t, err) |
| 255 | _, err = d.Writer().Exec(`CREATE TRIGGER reject_release_audit BEFORE INSERT ON audit_events WHEN NEW.kind='execution.release' BEGIN SELECT RAISE(ABORT,'audit unavailable'); END`) |
| 256 | executionMust(t, err) |
| 257 | if err = s.ReleaseExecution(ctx, owner, snap.Revision, "reviewed", snap.Hash); err == nil { |
| 258 | t.Fatal("release survived failed audit") |
| 259 | } |
| 260 | st, err := x.Core.Status(ctx) |
| 261 | executionMust(t, err) |
| 262 | if !st.Quarantine || scalar(t, d, "SELECT count(*) FROM recovery_events WHERE kind='release'") != 0 { |
| 263 | t.Fatal("release partially committed") |
| 264 | } |
| 265 | _, err = d.Writer().Exec(`DROP TRIGGER reject_release_audit`) |
| 266 | executionMust(t, err) |
| 267 | executionMust(t, s.ReleaseExecution(ctx, owner, snap.Revision, "reviewed", snap.Hash)) |
| 268 | } |
| 269 | |
| 270 | func TestRecoveryReleaseMissingHistoryCharge(t *testing.T) { |
| 271 | s, d, x, _, _, req := recoveryRuntime(t) |
| 272 | ctx := context.Background() |
| 273 | owner := equipo.Actor{Address: ownerAddress} |
| 274 | l := team.PeriodLimits{Calls: 100, InputTokens: 10000, OutputTokens: 10000, Seconds: 10000} |
| 275 | executionMust(t, s.SaveBudgets(ctx, owner, equipo.BudgetConfig{Team: l, Projects: map[team.ProjectID]team.PeriodLimits{req.Project: l}})) |
| 276 | executionMust(t, x.Core.Pause(ctx, "missing history")) |
| 277 | executionMust(t, x.Write(ctx, func(tx *gorm.DB) error { return tx.Exec(`UPDATE service SET restore_uncertain=1`).Error })) |
| 278 | _, err := x.Core.Resume(ctx) |
| 279 | executionMust(t, err) |
| 280 | snap, err := x.Core.RecoverySnapshot(ctx) |
| 281 | executionMust(t, err) |
| 282 | if err = s.ReleaseExecution(ctx, owner, snap.Revision, "attest missing work", snap.Hash); err == nil { |
| 283 | t.Fatal("missing history released without charge") |
| 284 | } |
| 285 | charge := team.RecoveryCharge{Key: "restore-1", Account: req.Account, Project: req.Project, Reserved: team.PeriodLimits{Calls: 10, Seconds: 100}} |
| 286 | if err = s.RecordRecoveryCharge(ctx, equipo.Actor{Address: "member@example.com"}, charge, "attest"); !errors.Is(err, equipo.ErrForbidden) { |
| 287 | t.Fatalf("member recovery charge: %v", err) |
| 288 | } |
| 289 | executionMust(t, s.RecordRecoveryCharge(ctx, owner, charge, "attest missing work and conservative reserve")) |
| 290 | executionMust(t, s.RecordRecoveryCharge(ctx, owner, charge, "retry")) |
| 291 | if scalar(t, d, "SELECT count(*) FROM recovery_charges") != 1 { |
| 292 | t.Fatal("recovery charge duplicated") |
| 293 | } |
| 294 | altered := charge |
| 295 | altered.Reserved.Calls++ |
| 296 | if err = s.RecordRecoveryCharge(ctx, owner, altered, "changed"); err == nil { |
| 297 | t.Fatal("recovery key content changed") |
| 298 | } |
| 299 | if err = s.ReleaseExecution(ctx, owner, snap.Revision, "reviewed", snap.Hash); err == nil { |
| 300 | t.Fatal("charge did not invalidate reconciliation") |
| 301 | } |
| 302 | _, err = x.Core.Resume(ctx) |
| 303 | executionMust(t, err) |
| 304 | snap, err = x.Core.RecoverySnapshot(ctx) |
| 305 | executionMust(t, err) |
| 306 | executionMust(t, s.ReleaseExecution(ctx, owner, snap.Revision, "attest missing history has been conservatively charged", snap.Hash)) |
| 307 | usage, err := x.Core.PeriodUsage(ctx, req.Account, req.Project, time.Now()) |
| 308 | executionMust(t, err) |
| 309 | if usage.Spent != charge.Reserved || scalar(t, d, "SELECT count(*) FROM actions WHERE receipt<>''") != 0 { |
| 310 | t.Fatalf("charge lost or fabricated receipt: %+v", usage) |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | func TestRuntimeAuthorityAttachSavedBudgets(t *testing.T) { |
| 315 | s, d, p, clock, auth, req := authorityFixture(t) |
| 316 | ctx := context.Background() |
| 317 | owner := equipo.Actor{Address: ownerAddress} |
| 318 | l := team.PeriodLimits{Calls: 100, InputTokens: 10000, OutputTokens: 10000, Seconds: 10000} |
| 319 | config := equipo.BudgetConfig{Team: l, Projects: map[team.ProjectID]team.PeriodLimits{team.ProjectID(p.ID): l}} |
| 320 | executionMust(t, s.SaveBudgets(ctx, owner, config)) |
| 321 | config.Revision = 1 |
| 322 | executionMust(t, s.SaveBudgets(ctx, owner, config)) |
| 323 | var seq int |
| 324 | var name, path string |
| 325 | executionMust(t, d.Writer().QueryRow(`PRAGMA database_list`).Scan(&seq, &name, &path)) |
| 326 | x, err := s.AttachExecution(ctx, path, team.Options{Clock: clock, Authority: auth}) |
| 327 | executionMust(t, err) |
| 328 | defer x.Core.Close() |
| 329 | if scalar(t, d, "SELECT count(*) FROM period_budgets WHERE revision=2") != 1 { |
| 330 | t.Fatal("attachment lost existing budgets") |
| 331 | } |
| 332 | config.Revision = 2 |
| 333 | executionMust(t, s.SaveBudgets(ctx, owner, config)) |
| 334 | usage, err := x.Core.PeriodUsage(ctx, req.Account, req.Project, clock.Now()) |
| 335 | executionMust(t, err) |
| 336 | if usage != (team.PeriodUsage{}) { |
| 337 | t.Fatalf("fresh budget usage: %+v", usage) |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | func TestRuntimeAuthorityAdmissionRacesEdit(t *testing.T) { |
| 342 | s, d, x, p, _, req := recoveryRuntime(t) |
| 343 | ctx := context.Background() |
| 344 | changed := false |
| 345 | executionMust(t, d.G.Callback().Query().After("gorm:query").Register("race_grant_creation", func(tx *gorm.DB) { |
| 346 | if tx.Statement.Table != "projects" || changed { |
| 347 | return |
| 348 | } |
| 349 | changed = true |
| 350 | p.DefaultBranch = "trunk" |
| 351 | p = saveProject(t, s, p, []equipo.DecisionOwner{{Decision: "work", Address: "member@example.com"}, {Decision: "merge", Address: ownerAddress}}, "race") |
| 352 | })) |
| 353 | defer d.G.Callback().Query().Remove("race_grant_creation") |
| 354 | _, err := x.Core.CreateJob(ctx, team.JobSpec{Account: req.Account, Project: req.Project, Requester: "member@example.com", GrantRef: req.Grant, Target: req.Target, Policy: team.DefaultPolicy(5000000, team.Rates{Version: 1}), Evidence: team.EvidenceRequirement{Predicate: team.PredicateSoftwareFix, ExpectedSymptom: team.FixtureSymptom, CheckDefinitionHash: team.CheckDefinitionHash}, Steps: team.SoftwareSteps(false)}) |
| 355 | if !changed || !errors.Is(err, team.ErrGrantStale) { |
| 356 | t.Fatalf("stale creation raced edit: changed=%v err=%v", changed, err) |
| 357 | } |
| 358 | if scalar(t, d, "SELECT count(*) FROM jobs") != 0 { |
| 359 | t.Fatal("stale creation committed") |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | func TestRuntimeAuthorityEditAuditRollback(t *testing.T) { |
| 364 | s, d, x, p, _, req := recoveryRuntime(t) |
| 365 | ctx := context.Background() |
| 366 | scope, h, id := recoveryJob(t, x, req, "rollback") |
| 367 | _, err := d.Writer().Exec(`CREATE TRIGGER reject_project_audit BEFORE INSERT ON audit_events WHEN NEW.kind='project.save' BEGIN SELECT RAISE(ABORT,'audit unavailable'); END`) |
| 368 | executionMust(t, err) |
| 369 | p.DefaultBranch = "trunk" |
| 370 | if _, err = s.SaveProject(ctx, equipo.Actor{Address: ownerAddress}, p, []equipo.DecisionOwner{{Decision: "work", Address: "member@example.com"}, {Decision: "merge", Address: ownerAddress}}, "rollback"); err == nil { |
| 371 | t.Fatal("project edit survived audit failure") |
| 372 | } |
| 373 | _, err = x.Core.Renew(ctx, scope, h) |
| 374 | executionMust(t, err) |
| 375 | _, err = x.Core.Admit(ctx, scope, id) |
| 376 | executionMust(t, err) |
| 377 | if scalar(t, d, "SELECT count(*) FROM jobs WHERE grant_stale<>0") != 0 { |
| 378 | t.Fatal("failed project edit committed fence") |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | func TestRuntimeAuthorityLookupFailureDenies(t *testing.T) { |
| 383 | _, d, x, _, _, req := recoveryRuntime(t) |
| 384 | ctx := context.Background() |
| 385 | scope, h, id := recoveryJob(t, x, req, "lookup") |
| 386 | executionMust(t, d.G.Callback().Query().Before("gorm:query").Register("failed_roster_lookup", func(tx *gorm.DB) { |
| 387 | if tx.Statement.Table == "members" { |
| 388 | tx.AddError(errors.New("roster lookup unavailable")) |
| 389 | } |
| 390 | })) |
| 391 | defer d.G.Callback().Query().Remove("failed_roster_lookup") |
| 392 | if _, err := x.Core.Renew(ctx, scope, h); !errors.Is(err, team.ErrAuthorityUnavailable) { |
| 393 | t.Fatalf("lookup failure renewed: %v", err) |
| 394 | } |
| 395 | if _, err := x.Core.Admit(ctx, scope, id); !errors.Is(err, team.ErrAuthorityUnavailable) { |
| 396 | t.Fatalf("lookup failure admitted: %v", err) |
| 397 | } |
| 398 | job, err := x.Core.GetJob(ctx, scope) |
| 399 | executionMust(t, err) |
| 400 | if job.State == team.JobCancelled { |
| 401 | t.Fatal("lookup error became revocation") |
| 402 | } |
| 403 | } |
| 404 | |