oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package plan3test
2
3import (
4 "context"
5 "errors"
6 "testing"
7
8 "amadan.net/oficina/equipo/internal/equipo"
9 "amadan.net/oficina/equipo/internal/equipo/team"
10 "amadan.net/oficina/equipo/internal/runnerproto"
11)
12
13func TestCoordinatorDefinitiveRefusalReleasesQueue(t *testing.T) {
14 for _, op := range []string{"claim", "recover", "containment", "delivery"} {
15 for _, phase := range []string{"queued", "claimed", "completed-unreported"} {
16 t.Run(op+"/"+phase, func(t *testing.T) {
17 f, x, p, b := coordinatorFixture(t)
18 x.SetRunnerValidators(equipo.RunnerValidators{Containment: func(context.Context, equipo.Assignment, equipo.RunnerRequest) error { return nil }})
19 turn := queueCoordinator(t, f, "first", "first")
20 c := equipo.CoordinatorClaim{Turn: turn}
21 if phase != "queued" {
22 var err error
23 c, err = x.ClaimCoordinator(ctx, p, turn.ID, turn.Revision)
24 must(t, err)
25 }
26 if phase == "completed-unreported" {
27 must(t, x.Core.Record(ctx, c.Scope, c.Admission.Dispatch.Action, team.Receipt{ID: "completed", Outcome: team.OutcomeSuccess}))
28 }
29 if phase != "queued" {
30 proof := runnerproto.ContainmentProof{Version: 2, Scope: c.Scope, Handle: c.Handle}
31 must(t, x.ContainCoordinator(ctx, p, c, proof))
32 }
33 _, err := f.Store.ResetCoordinator(ctx, owner, equipo.Mutation{RequestID: "reset", Revision: b.Revision})
34 must(t, err)
35 var refusal error
36 switch op {
37 case "claim":
38 _, refusal = x.ClaimCoordinator(ctx, p, turn.ID, turn.Revision)
39 case "recover":
40 _, refusal = x.RecoverCoordinator(ctx, p, turn.ID, turn.Revision)
41 case "containment":
42 refusal = x.ContainCoordinator(ctx, p, c, runnerproto.ContainmentProof{})
43 case "delivery":
44 refusal = x.CoordinatorEvent(ctx, p, c, runnerproto.Event{Kind: "coordinator.delivery"})
45 }
46 var definitive *runnerproto.RefusalError
47 if !errors.As(refusal, &definitive) || definitive.Reason != "coordinator.fenced" {
48 t.Fatal("missing definitive code", refusal)
49 }
50 var state string
51 must(t, f.DB.Writer().QueryRow("SELECT state FROM coordinator_turns WHERE id=?", turn.ID).Scan(&state))
52 if state != "fenced" {
53 t.Fatal("definitive refusal without durable fence", state)
54 }
55 next := queueCoordinator(t, f, "later", "later")
56 _, err = x.ClaimCoordinator(ctx, p, next.ID, next.Revision)
57 must(t, err)
58 })
59 }
60 }
61}
62func TestCoordinatorRefusalDoesNotFenceTransientOrPending(t *testing.T) {
63 f, x, p, _ := coordinatorFixture(t)
64 turn := queueCoordinator(t, f, "first", "first")
65 bad := p
66 bad.CredentialID = "stale"
67 _, err := x.RecoverCoordinator(ctx, bad, turn.ID, turn.Revision)
68 var coded *runnerproto.RefusalError
69 if err == nil || errors.As(err, &coded) {
70 t.Fatal("authentication error made definitive", err)
71 }
72 c, err := x.ClaimCoordinator(ctx, p, turn.ID, turn.Revision)
73 must(t, err)
74 f.exec(t, "UPDATE coordinator_turns SET state='uncertain' WHERE id=?", turn.ID)
75 _, err = x.ClaimCoordinator(ctx, p, turn.ID, turn.Revision)
76 if errors.As(err, &coded) {
77 t.Fatal("pending containment made definitive", err)
78 }
79 var state string
80 must(t, f.DB.Writer().QueryRow("SELECT containment FROM attempts WHERE id=?", c.Handle.Attempt).Scan(&state))
81 if state != "pending" {
82 t.Fatal("refusal invented containment", state)
83 }
84}
85
86func TestCoordinatorUncertainSameGenerationWaitsForPerson(t *testing.T) {
87 acceptanceUncertainRequiresPerson(t)
88}
89
90func TestCoordinatorDefinitiveFenceAtomic(t *testing.T) {
91 f, x, p, b := coordinatorFixture(t)
92 first := queueCoordinator(t, f, "first", "first")
93 _, err := f.Store.ResetCoordinator(ctx, owner, equipo.Mutation{RequestID: "reset", Revision: b.Revision})
94 must(t, err)
95 f.exec(t, `CREATE TRIGGER reject_fence BEFORE UPDATE ON coordinator_turns WHEN NEW.state='fenced' BEGIN SELECT RAISE(ABORT,'fence write failed'); END`)
96 _, err = x.ClaimCoordinator(ctx, p, first.ID, first.Revision)
97 var refusal *runnerproto.RefusalError
98 if err == nil || errors.As(err, &refusal) {
99 t.Fatal("definitive response escaped failed commit", err)
100 }
101 f.exec(t, "DROP TRIGGER reject_fence")
102 _, err = x.ClaimCoordinator(ctx, p, first.ID, first.Revision)
103 if !errors.As(err, &refusal) {
104 t.Fatal("restored fence not definitive", err)
105 }
106}
107
108func TestCoordinatorFenceUnknownContainmentDenied(t *testing.T) {
109 f, x, p, _ := coordinatorFixture(t)
110 first := queueCoordinator(t, f, "first", "first")
111 _, err := x.ClaimCoordinator(ctx, p, first.ID, first.Revision)
112 must(t, err)
113 f.exec(t, "UPDATE coordinator_turns SET state='uncertain' WHERE id=?", first.ID)
114 f.exec(t, "ALTER TABLE attempts RENAME TO unavailable_attempts")
115 _, err = x.ClaimCoordinator(ctx, p, first.ID, first.Revision)
116 var refusal *runnerproto.RefusalError
117 if err == nil || errors.As(err, &refusal) {
118 t.Fatal("unknown containment became definitive", err)
119 }
120}
121