oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package equipotest
2
3import (
4 "context"
5 "errors"
6 "testing"
7 "time"
8
9 "amadan.net/oficina/equipo/internal/equipo"
10)
11
12func TestBugWithoutPairWaits(t *testing.T) {
13 for _, setup := range []bool{false, true} {
14 t.Run(map[bool]string{false: "family", true: "setup"}[setup], func(t *testing.T) {
15 f := bugFixture(t)
16 if setup {
17 executionMust(t, setDirectAvailable(f.s, f.principal, f.reports, false))
18 } else {
19 mustSQL(t, f.d, "DELETE FROM connections WHERE id=?", f.ids["codex"])
20 }
21 task, err := f.s.CreateBug(connectionContext, ownerActor, f.project.ID, "waiting bug", "wait")
22 executionMust(t, err)
23 want := "task.wait.family"
24 if setup {
25 want = "task.wait.setup"
26 }
27 if task.WaitCode != want || task.JobID != 0 || task.State != "waiting" {
28 t.Fatal("missing waiting task", task)
29 }
30 view, err := f.s.TaskView(connectionContext, ownerActor, task.ID)
31 executionMust(t, err)
32 if view.StateKey != want {
33 t.Fatal("waiting reason absent from page data", view.StateKey)
34 }
35 if scalar(t, f.d, "SELECT count(*) FROM runner_assignments") != 0 {
36 t.Fatal("waiting task dispatched")
37 }
38 })
39 }
40}
41
42func TestSyncSkipsStalePreparedTask(t *testing.T) {
43 for _, change := range []string{"specialist", "budget", "project", "requester"} {
44 t.Run(change, func(t *testing.T) {
45 f := runnerFixtureNew(t, false)
46 task, err := f.s.CreateBug(connectionContext, ownerActor, f.project.ID, "stale bug", "stale")
47 executionMust(t, err)
48 task, err = f.s.PrepareBug(connectionContext, f.principal, task.ID, task.Revision, bugPrepared(f.connectionFixture))
49 executionMust(t, err)
50 switch change {
51 case "specialist":
52 mustSQL(t, f.d, "UPDATE specialists SET revision=revision+1 WHERE key='engineering'")
53 case "budget":
54 mustSQL(t, f.d, "UPDATE task_outboxes SET budget_revision=budget_revision-1 WHERE task_id=?", task.ID)
55 case "project":
56 mustSQL(t, f.d, "UPDATE task_outboxes SET project_revision=project_revision-1 WHERE task_id=?", task.ID)
57 case "requester":
58 mustSQL(t, f.d, "UPDATE tasks SET requester='removed@example.com' WHERE id=?", task.ID)
59 }
60 executionMust(t, f.x.Sync(connectionContext))
61 var wait string
62 executionMust(t, f.d.Writer().QueryRow("SELECT wait_code FROM tasks WHERE id=?", task.ID).Scan(&wait))
63 if wait != "task.wait.setup" {
64 t.Fatal("stale task has no setup wait", wait)
65 }
66 })
67 }
68}
69
70func TestPollReplacesStaleUnclaimedAssignment(t *testing.T) {
71 f := runnerFixtureNew(t, false)
72 mustSQL(t, f.d, "UPDATE specialists SET revision=revision+1 WHERE key='engineering'")
73 ctx, cancel := context.WithTimeout(connectionContext, 250*time.Millisecond)
74 defer cancel()
75 assignments, err := f.x.Poll(ctx, f.principal, "")
76 if err != nil && !errors.Is(err, context.DeadlineExceeded) {
77 t.Fatal("one stale task failed poll", err)
78 }
79 if len(assignments) != 0 {
80 t.Fatal("stale assignment dispatched")
81 }
82 var wait string
83 executionMust(t, f.d.Writer().QueryRow("SELECT wait_code FROM tasks WHERE id=?", f.a.TaskID).Scan(&wait))
84 if wait != "task.wait.setup" {
85 t.Fatal("stale assignment has no wait", wait)
86 }
87 executionMust(t, f.x.Sync(connectionContext))
88 retryCtx, retryCancel := context.WithTimeout(connectionContext, 250*time.Millisecond)
89 defer retryCancel()
90 aa, err := f.x.Poll(retryCtx, f.principal, "")
91 if err != nil || len(aa) != 1 || aa[0].ID == f.a.ID || aa[0].Revision <= f.a.Revision || aa[0].Selection.SpecialistRevision <= f.a.Selection.SpecialistRevision {
92 t.Fatal("unclaimed assignment was not replaced with a current snapshot", aa, err)
93 }
94 if scalar(t, f.d, "SELECT count(*) FROM runner_assignments WHERE id=? AND state='fenced' AND claim_request=''", f.a.ID) != 1 {
95 t.Fatal("stale assignment retained authority")
96 }
97}
98
99func TestPreparationTaintDoesNotInvalidate(t *testing.T) {
100 f := bugFixture(t)
101 task := bugTask(t, f)
102 mustSQL(t, f.d, `UPDATE task_outboxes SET engineering=json_set(engineering,'$.Isolation.runner_tainted',json('false'),'$.Isolation.acknowledgement_ids',json('[]')) WHERE task_id=?`, task.ID)
103 _, err := f.s.PrepareBug(connectionContext, f.principal, task.ID, task.Revision, bugPrepared(f))
104 executionMust(t, err)
105}
106
107func TestExpiredPreparationDoesNotBlockClaim(t *testing.T) {
108 for _, state := range []string{"claimed", "fenced"} {
109 t.Run(state, func(t *testing.T) {
110 f := runnerFixtureNew(t, false)
111 mustSQL(t, f.d, `INSERT INTO runner_assignments(protocol_version,id,task_id,step_key,workspace_key,team_id,runner_id,person,selection,isolation,kind,state,revision,job_id,attempt_id,fence,claim_request,lease_deadline,brief) SELECT 2,'old-prepare',task_id,'prepare','old',team_id,runner_id,person,selection,isolation,'prepare',?,1,0,0,1,'old',?,'{}' FROM runner_assignments WHERE id=?`, state, f.clock.Now().Add(-time.Minute), f.a.ID)
112 _, err := f.x.Claim(connectionContext, f.principal, equipo.ClaimRequest{Version: 2, AssignmentID: f.a.ID, Revision: f.a.Revision, RequestID: "claim"})
113 executionMust(t, err)
114 })
115 }
116}
117
118func TestPreparationCapacityIsPerSpecialist(t *testing.T) {
119 f := runnerFixtureNew(t, false)
120 mustSQL(t, f.d, `UPDATE steps SET depends_on='[]' WHERE key='review'`)
121 mustSQL(t, f.d, `UPDATE runner_assignments SET step_key='review',selection=(SELECT review FROM task_outboxes WHERE task_id=runner_assignments.task_id),brief=json_set(brief,'$.step.StepKey','review','$.step.Action','review','$.step.SpecialistKey','review') WHERE id=?`, f.a.ID)
122 mustSQL(t, f.d, `INSERT INTO runner_assignments(protocol_version,id,task_id,step_key,workspace_key,team_id,runner_id,person,selection,isolation,kind,state,revision,job_id,attempt_id,fence,claim_request,lease_deadline,brief) SELECT 2,'other-prepare',task_id,'prepare','other',team_id,runner_id,person,selection,isolation,'prepare','claimed',1,0,0,1,'other',?,'{}' FROM runner_assignments WHERE id=?`, f.clock.Now().Add(time.Minute), f.a.ID)
123 _, err := f.x.Claim(connectionContext, f.principal, equipo.ClaimRequest{Version: 2, AssignmentID: f.a.ID, Revision: f.a.Revision, RequestID: "review"})
124 executionMust(t, err)
125}
126
127func TestWaitingBugResumesAfterSetup(t *testing.T) {
128 f := bugFixture(t)
129 f.reports[1].Available = false
130 executionMust(t, f.s.ReportConnections(connectionContext, f.principal, f.reports))
131 task, err := f.s.CreateBug(connectionContext, ownerActor, f.project.ID, "waiting", "waiting")
132 executionMust(t, err)
133 x := attachBugExecution(t, f)
134 executionMust(t, x.Sync(connectionContext))
135 if scalar(t, f.d, "SELECT count(*) FROM runner_assignments") != 0 {
136 t.Fatal("waiting task dispatched")
137 }
138 f.reports[1].Available = true
139 executionMust(t, f.s.ReportConnections(connectionContext, f.principal, f.reports))
140 executionMust(t, x.Sync(connectionContext))
141 ctx, cancel := context.WithTimeout(connectionContext, time.Second)
142 defer cancel()
143 aa, err := x.Poll(ctx, f.principal, "")
144 executionMust(t, err)
145 if len(aa) != 1 || aa[0].TaskID != task.ID || aa[0].Kind != "prepare" {
146 t.Fatal("waiting task did not resume", aa)
147 }
148}
149