| 1 | package team |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "database/sql" |
| 6 | "errors" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | func TestAtomicAuthorityDenialPersists(t *testing.T) { |
| 11 | for _, op := range []string{"renew", "admit"} { |
| 12 | for _, revoked := range []bool{false, true} { |
| 13 | t.Run(op+map[bool]string{false: "/stale", true: "/revoked"}[revoked], func(t *testing.T) { |
| 14 | e := newEnv(t) |
| 15 | _, scope := e.createJob(softwareSpec(SoftwareSteps(false))) |
| 16 | h := e.claim(scope, "reproduce", "runner", "ws") |
| 17 | id, err := e.store.Intend(e.ctx, scope, h, intent("turn", Usage{InputTokens: 1})) |
| 18 | must(t, err) |
| 19 | want := ErrGrantStale |
| 20 | if revoked { |
| 21 | e.auth.Revoke(grant) |
| 22 | want = ErrGrantRevoked |
| 23 | } else { |
| 24 | e.auth.Amend(grant) |
| 25 | } |
| 26 | err = e.store.Atomic(e.ctx, func(ctx context.Context, tx *sql.Tx) error { |
| 27 | if _, err := tx.ExecContext(ctx, "UPDATE jobs SET outcome='must rollback' WHERE id=?", scope.Job); err != nil { |
| 28 | return err |
| 29 | } |
| 30 | if op == "renew" { |
| 31 | _, err := e.store.Renew(ctx, scope, h) |
| 32 | return err |
| 33 | } |
| 34 | _, err := e.store.Admit(ctx, scope, id) |
| 35 | return err |
| 36 | }) |
| 37 | if !errors.Is(err, want) { |
| 38 | t.Fatal("wrong denial", err) |
| 39 | } |
| 40 | var stale int |
| 41 | var state, outcome string |
| 42 | must(t, e.store.db.QueryRow("SELECT grant_stale,state,outcome FROM jobs WHERE id=?", scope.Job).Scan(&stale, &state, &outcome)) |
| 43 | if outcome == "must rollback" { |
| 44 | t.Fatal("denial committed unrelated writes") |
| 45 | } |
| 46 | if revoked && state != "cancelled" || !revoked && stale != 1 { |
| 47 | t.Fatal("denial rolled back authority effect", state, stale) |
| 48 | } |
| 49 | var attempt string |
| 50 | must(t, e.store.db.QueryRow("SELECT state FROM attempts WHERE id=?", h.Attempt).Scan(&attempt)) |
| 51 | if attempt != "stopping" { |
| 52 | t.Fatal("denial did not stop attempt", attempt) |
| 53 | } |
| 54 | }) |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |