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 "crypto/sha256"
5 "encoding/hex"
6 "errors"
7 "strings"
8 "testing"
9 "time"
10
11 "amadan.net/oficina/equipo/internal/equipo"
12 "amadan.net/oficina/equipo/internal/equipo/team"
13 "amadan.net/oficina/equipo/internal/runnerproto"
14)
15
16func TestPlanApprovalPrecedenceDispatch(t *testing.T) {
17 f := newFixture(t)
18 p := proposed(t, f)
19 f.planConnections(t, p.ProjectID)
20 must(t, f.Store.CheckPlanDispatch(ctx, owner, p.TaskID))
21 must(t, f.Store.SetProjectApproval(ctx, owner, p.ProjectID, equipo.ApprovalRequired, equipo.Mutation{RequestID: "require"}))
22 if err := f.Store.CheckPlanDispatch(ctx, owner, p.TaskID); !errors.Is(err, equipo.ErrConflict) {
23 t.Fatal("switch to required did not stop dispatch", err)
24 }
25 m := equipo.Mutation{RequestID: "approve", Revision: 1}
26 approved, err := f.Store.ApprovePlan(ctx, owner, p.TaskID, m)
27 must(t, err)
28 if approved.ApprovalRevision != 1 {
29 t.Fatal("approval is not bound to plan")
30 }
31 must(t, f.Store.CheckPlanDispatch(ctx, owner, p.TaskID))
32 p.Steps[1].Instructions = "second revision"
33 p, err = f.Store.EditPlan(ctx, owner, p, equipo.Mutation{RequestID: "edit-approved", Revision: 1})
34 must(t, err)
35 if err = f.Store.CheckPlanDispatch(ctx, owner, p.TaskID); !errors.Is(err, equipo.ErrConflict) {
36 t.Fatal("edited plan retained approval", err)
37 }
38 replay, err := f.Store.ApprovePlan(ctx, owner, p.TaskID, m)
39 must(t, err)
40 if replay.ApprovalRevision != 1 {
41 t.Fatal("approval retry lost saved result")
42 }
43 if err = f.Store.CheckPlanDispatch(ctx, owner, p.TaskID); !errors.Is(err, equipo.ErrConflict) {
44 t.Fatal("old approval replay authorized new revision", err)
45 }
46 _, err = f.Store.ApprovePlan(ctx, owner, p.TaskID, equipo.Mutation{RequestID: "approve-two", Revision: p.Revision})
47 must(t, err)
48 must(t, f.Store.CheckPlanDispatch(ctx, owner, p.TaskID))
49 f.exec(t, "UPDATE specialists SET model_selection='fixed',provider='unavailable' WHERE key='engineering'")
50 if err = f.Store.CheckPlanDispatch(ctx, owner, p.TaskID); err == nil {
51 t.Fatal("dispatch ignored current model constraints")
52 }
53 if _, err = f.Store.ApprovePlan(ctx, owner, p.TaskID, equipo.Mutation{RequestID: "approve-model", Revision: p.Revision}); err == nil {
54 t.Fatal("approval ignored current model constraints")
55 }
56 f.exec(t, "UPDATE specialists SET model_selection='best_fit' WHERE key='engineering'")
57 f.exec(t, "DELETE FROM budget_records")
58 if err = f.Store.CheckPlanDispatch(ctx, owner, p.TaskID); err == nil {
59 t.Fatal("dispatch ignored missing budget")
60 }
61 if _, err = f.Store.ApprovePlan(ctx, owner, p.TaskID, equipo.Mutation{RequestID: "approve-budget", Revision: p.Revision}); err == nil {
62 t.Fatal("approval ignored missing budget")
63 }
64}
65func TestPlanApprovalOverrideAuthority(t *testing.T) {
66 f := newFixture(t)
67 p := proposed(t, f)
68 f.exec(t, "UPDATE members SET role='member' WHERE address=?", peer.Address)
69 if err := f.Store.SetProjectApproval(ctx, peer, p.ProjectID, equipo.ApprovalRequired, equipo.Mutation{RequestID: "not-owner"}); !errors.Is(err, equipo.ErrForbidden) {
70 t.Fatal("non-owner changed project approval", err)
71 }
72 mode := equipo.ApprovalRequired
73 must(t, f.Store.SetApprovalOverride(ctx, peer, p.ProjectID, &mode, equipo.Mutation{RequestID: "own-override"}))
74 must(t, f.Store.SetApprovalOverride(ctx, peer, p.ProjectID, nil, equipo.Mutation{RequestID: "own-inherit", Revision: 1}))
75 f.exec(t, "UPDATE members SET active=0 WHERE address=?", peer.Address)
76 if err := f.Store.SetApprovalOverride(ctx, peer, p.ProjectID, nil, equipo.Mutation{RequestID: "own-inherit", Revision: 1}); !errors.Is(err, equipo.ErrForbidden) {
77 t.Fatal("removed member replay allowed", err)
78 }
79}
80
81func TestPlanApprovalPrecedenceClaim(t *testing.T) {
82 f := newFixture(t)
83 p := proposed(t, f)
84 f.planConnections(t, p.ProjectID)
85 var path string
86 must(t, f.DB.Writer().QueryRow("SELECT file FROM pragma_database_list WHERE name='main'").Scan(&path))
87 x, err := f.Store.AttachExecution(ctx, path, team.Options{Clock: team.RealClock{}, Authority: equipo.NewAuthority(f.Store, nil)})
88 must(t, err)
89 t.Cleanup(func() { must(t, x.Core.Close()) })
90 _, err = x.Core.Resume(ctx)
91 must(t, err)
92 f.exec(t, "INSERT INTO runner_credentials(id,runner_id,instance_id,audience,person,machine_key_hash,token_hash,expires_at) VALUES('fixture-credential','fixture-runner',?,?,?,'','fixture-token',?)", f.TeamID, runnerproto.RunnerAudience, owner.Address, time.Now().Add(time.Hour).UTC())
93 must(t, x.Sync(ctx))
94 var id string
95 var revision int64
96 must(t, f.DB.Writer().QueryRow("SELECT id,revision FROM runner_assignments WHERE task_id=? AND state='pending'", p.TaskID).Scan(&id, &revision))
97 must(t, f.Store.SetProjectApproval(ctx, owner, p.ProjectID, equipo.ApprovalRequired, equipo.Mutation{RequestID: "require-before-claim"}))
98 _, err = x.Claim(ctx, equipo.RunnerPrincipal{RunnerID: "fixture-runner", Person: owner.Address, CredentialID: "fixture-credential"}, equipo.ClaimRequest{Version: 2, AssignmentID: id, RequestID: "claim", Revision: revision})
99 if !errors.Is(err, equipo.ErrConflict) {
100 t.Fatal("claim ignored current approval mode", err)
101 }
102 if f.count(t, "SELECT count(*) FROM runner_cleanups") != 0 || f.count(t, "SELECT count(*) FROM attempts") != 0 || f.count(t, "SELECT count(*) FROM runner_assignments WHERE id=? AND state='pending'", id) != 1 {
103 t.Fatal("denied claim partially committed")
104 }
105}
106
107func TestPlanApprovalPrecedenceStart(t *testing.T) {
108 f := newFixture(t)
109 p := proposed(t, f)
110 f.planConnections(t, p.ProjectID)
111 var path string
112 must(t, f.DB.Writer().QueryRow("SELECT file FROM pragma_database_list WHERE name='main'").Scan(&path))
113 x, err := f.Store.AttachExecution(ctx, path, team.Options{Clock: team.RealClock{}, Authority: equipo.NewAuthority(f.Store, nil)})
114 must(t, err)
115 t.Cleanup(func() { must(t, x.Core.Close()) })
116 _, err = x.Core.Resume(ctx)
117 must(t, err)
118 f.exec(t, "INSERT INTO runner_credentials(id,runner_id,instance_id,audience,person,machine_key_hash,token_hash,expires_at) VALUES('fixture-credential','fixture-runner',?,?,?,'','fixture-token',?)", f.TeamID, runnerproto.RunnerAudience, owner.Address, time.Now().Add(time.Hour).UTC())
119 principal := equipo.RunnerPrincipal{RunnerID: "fixture-runner", Person: owner.Address, CredentialID: "fixture-credential"}
120 must(t, x.Sync(ctx))
121 var project equipo.Project
122 must(t, f.DB.G.First(&project, p.ProjectID).Error)
123 definition := []byte("fixture check")
124 hash := sha256.Sum256(definition)
125 prepared := equipo.PreparedBug{Repository: project.Repository, DefaultBranch: project.DefaultBranch, BaseCommit: strings.Repeat("a", 40), CheckDefinition: definition, CheckDefinitionHash: hex.EncodeToString(hash[:]), BaselineTestSetHash: strings.Repeat("b", 64)}
126 must(t, f.Store.SetProjectApproval(ctx, owner, p.ProjectID, equipo.ApprovalRequired, equipo.Mutation{RequestID: "require-prepare"}))
127 if _, err = f.Store.PrepareBug(ctx, principal, p.TaskID, 1, prepared); !errors.Is(err, equipo.ErrConflict) {
128 t.Fatal("prepare ignored current approval", err)
129 }
130 must(t, f.Store.SetProjectApproval(ctx, owner, p.ProjectID, equipo.ApprovalImmediate, equipo.Mutation{RequestID: "immediate-prepare", Revision: 1}))
131 task, err := f.Store.PrepareBug(ctx, principal, p.TaskID, 1, prepared)
132 must(t, err)
133 must(t, f.Store.SetProjectApproval(ctx, owner, p.ProjectID, equipo.ApprovalRequired, equipo.Mutation{RequestID: "require-start", Revision: 2}))
134 if _, err = f.Store.StartBug(ctx, principal, p.TaskID, task.Revision); !errors.Is(err, equipo.ErrConflict) {
135 t.Fatal("start ignored current approval", err)
136 }
137 if f.count(t, "SELECT count(*) FROM jobs") != 0 {
138 t.Fatal("denied start left core job")
139 }
140 task, err = f.Store.ApprovePlan(ctx, owner, p.TaskID, equipo.Mutation{RequestID: "approve-start", Revision: 1})
141 must(t, err)
142 task, err = f.Store.StartBug(ctx, principal, p.TaskID, task.Revision)
143 must(t, err)
144 if task.JobID == 0 {
145 t.Fatal("approved plan not dispatched")
146 }
147}
148