oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package team
2
3import (
4 "context"
5 "os"
6 "path/filepath"
7 "strings"
8 "testing"
9 "time"
10)
11
12func TestScopeIsAccountAndJob(t *testing.T) {
13 e := newEnv(t)
14 _, scope := e.createJob(softwareSpec(SoftwareSteps(false)))
15 foreign := Scope{Account: acctB, Job: scope.Job}
16 _, err := e.store.GetJob(e.ctx, foreign)
17 wantCode(t, err, CodeScope)
18 _, err = e.store.Claim(e.ctx, foreign, "reproduce", "r", "ws")
19 wantCode(t, err, CodeScope)
20 _, err = e.store.Cancel(e.ctx, foreign, "x", "y")
21 wantCode(t, err, CodeScope)
22 _, err = e.store.Events(e.ctx, foreign, 0)
23 wantCode(t, err, CodeScope)
24 _, err = e.store.Ledger(e.ctx, foreign)
25 wantCode(t, err, CodeScope)
26 // A missing job in the right account answers the same code.
27 _, err = e.store.GetJob(e.ctx, Scope{Account: acctA, Job: 999})
28 wantCode(t, err, CodeScope)
29 _, err = e.store.GetJob(e.ctx, Scope{})
30 wantCode(t, err, CodeScope)
31 // A handle from one job cannot act on another job of the same account.
32 h := e.claim(scope, "reproduce", "runner-a", "ws-1")
33 _, other := e.createJob(softwareSpec(SoftwareSteps(false)))
34 _, err = e.store.Register(e.ctx, other, h, ArtifactSpec{Role: RoleReproduction, MediaType: "application/json", Payload: FixtureReproduction,
35 Provenance: Provenance{Producer: "r", Provider: "p", Context: "c"}})
36 wantCode(t, err, CodeScope)
37}
38
39func TestUnknownKindsFailClosed(t *testing.T) {
40 e := newEnv(t)
41 spec := softwareSpec(SoftwareSteps(false))
42 bad := spec
43 bad.Steps = []StepSpec{{Key: "x", Capability: "deploy", Version: 1}}
44 _, err := e.store.CreateJob(e.ctx, bad)
45 wantCode(t, err, CodeUnknownKind)
46 bad = spec
47 bad.Steps = []StepSpec{{Key: "a", Capability: CapReproduce, Version: 1, DependsOn: []string{"b"}}, {Key: "b", Capability: CapImplement, Version: 1, DependsOn: []string{"a"}}}
48 _, err = e.store.CreateJob(e.ctx, bad)
49 wantCode(t, err, CodeDependency)
50 bad = spec
51 bad.Target.Ref = "main"
52 _, err = e.store.CreateJob(e.ctx, bad)
53 wantCode(t, err, CodeInvalid)
54 bad = spec
55 bad.Target = TargetBinding{Kind: "cluster", Subject: "x"}
56 _, err = e.store.CreateJob(e.ctx, bad)
57 wantCode(t, err, CodeUnknownKind)
58 bad = spec
59 bad.Evidence.Predicate = "narrative"
60 _, err = e.store.CreateJob(e.ctx, bad)
61 wantCode(t, err, CodeUnknownKind)
62 bad = spec
63 bad.Policy.Version = PolicyVersion + 1
64 _, err = e.store.CreateJob(e.ctx, bad)
65 wantCode(t, err, CodeUnknownKind)
66 bad = spec
67 bad.Policy.Job.CostMicros = 0
68 _, err = e.store.CreateJob(e.ctx, bad)
69 wantCode(t, err, CodeInvalid)
70 // Policies with fields this binary does not know are refused.
71 _, err = ParsePolicy([]byte(`{"version":1,"allow_merge":true}`))
72 wantCode(t, err, CodeUnknownKind)
73 if _, err := ParsePolicy(mustJSON(testPolicy())); err != nil {
74 t.Fatal(err)
75 }
76 // A grant that does not cover the operation refuses creation.
77 e.auth.Issue("narrow", Grant{Account: acctA, Operations: map[Operation]bool{OpProviderCall: true}})
78 bad = spec
79 bad.GrantRef = "narrow"
80 _, err = e.store.CreateJob(e.ctx, bad)
81 wantCode(t, err, CodeGrantRevoked)
82 // A stored state outside the closed set fails every read.
83 _, scope := e.createJob(spec)
84 if _, err := e.store.db.Exec(`UPDATE jobs SET state='paused' WHERE id=?`, int64(scope.Job)); err != nil {
85 t.Fatal(err)
86 }
87 _, err = e.store.GetJob(e.ctx, scope)
88 wantCode(t, err, CodeUnknownKind)
89 _, err = e.store.Claim(e.ctx, scope, "reproduce", "r", "ws")
90 wantCode(t, err, CodeUnknownKind)
91}
92
93func TestStartupPausedUntilResume(t *testing.T) {
94 e := newEnv(t)
95 _, scope := e.createJob(softwareSpec(SoftwareSteps(false)))
96 must(t, e.store.Close())
97 e.store = nil
98 s, err := Open(e.ctx, e.path, e.options())
99 must(t, err)
100 e.store = s
101 st, err := s.Status(e.ctx)
102 must(t, err)
103 if !st.AdmissionPaused || st.Quarantine || st.Incarnation != 2 {
104 t.Fatalf("status %+v", st)
105 }
106 _, err = s.Claim(e.ctx, scope, "reproduce", "r", "ws")
107 wantCode(t, err, CodeAdmissionPaused)
108 _, err = s.CreateJob(e.ctx, softwareSpec(SoftwareSteps(false)))
109 wantCode(t, err, CodeAdmissionPaused)
110 // Reads and cancellation work while paused.
111 if _, err := s.GetJob(e.ctx, scope); err != nil {
112 t.Fatal(err)
113 }
114 rr, err := s.Resume(e.ctx)
115 must(t, err)
116 if !rr.Resumed || len(rr.Revalidated) != 1 {
117 t.Fatalf("resume %+v", rr)
118 }
119 if _, err := s.Claim(e.ctx, scope, "reproduce", "r", "ws"); err != nil {
120 t.Fatal(err)
121 }
122 raw, err := os.ReadFile(e.path + ".incarnation")
123 must(t, err)
124 if !strings.HasPrefix(string(raw), "incarnation=2\nclean=false\nrevision=") {
125 t.Fatalf("marker %q", raw)
126 }
127 must(t, s.Close())
128 e.store = nil
129 raw, _ = os.ReadFile(e.path + ".incarnation")
130 if !strings.HasPrefix(string(raw), "incarnation=2\nclean=true\nrevision=") {
131 t.Fatalf("marker after close %q", raw)
132 }
133 // A missing marker on a used database is quarantine.
134 must(t, os.Remove(e.path+".incarnation"))
135 s, err = Open(e.ctx, e.path, e.options())
136 must(t, err)
137 e.store = s
138 st, _ = s.Status(e.ctx)
139 if !st.Quarantine {
140 t.Fatalf("status %+v", st)
141 }
142}
143
144func TestOpenRefusesNewerSchemaAndMissingDeps(t *testing.T) {
145 e := newEnv(t)
146 if _, err := e.store.db.Exec(`UPDATE service SET schema_version=? WHERE id=1`, SchemaVersion+1); err != nil {
147 t.Fatal(err)
148 }
149 must(t, e.store.Close())
150 e.store = nil
151 _, err := Open(e.ctx, e.path, e.options())
152 wantCode(t, err, CodeStaleStore)
153 _, err = Open(e.ctx, filepath.Join(e.dir, "other.db"), Options{Clock: e.clock})
154 wantCode(t, err, CodeInvalid)
155}
156
157func TestNestedJobsShareTheParentBudget(t *testing.T) {
158 e := newEnv(t)
159 spec := softwareSpec(SoftwareSteps(false))
160 spec.Policy.Job.Calls = 2
161 spec.Policy.Account.Calls = 10
162 parent, pscope := e.createJob(spec)
163 child := spec
164 child.Parent = parent.ID
165 child.Policy.Job.Calls = 10
166 _, cscope := e.createJob(child)
167 grandchild := child
168 grandchild.Parent = cscope.Job
169 _, err := e.store.CreateJob(e.ctx, grandchild)
170 wantCode(t, err, CodeQuota)
171 // Two calls charged to the child exhaust the parent's cap of two.
172 h := e.claim(cscope, "reproduce", "runner-a", "ws-c")
173 for i, key := range []string{"c/1", "c/2"} {
174 if _, err := e.store.Intend(e.ctx, cscope, h, IntentSpec{Key: key, Operation: OpProviderCall, Payload: []byte(`{"task":"x","prompt":"` + key + `"}`), Reserve: Usage{InputTokens: 10, OutputTokens: 10}}); err != nil {
175 t.Fatalf("intent %d: %v", i, err)
176 }
177 }
178 _, err = e.store.Intend(e.ctx, cscope, h, IntentSpec{Key: "c/3", Operation: OpProviderCall, Payload: []byte(`{"task":"x","prompt":"c/3"}`), Reserve: Usage{InputTokens: 10, OutputTokens: 10}})
179 wantCode(t, err, CodeBudget)
180 hp := e.claim(pscope, "reproduce", "runner-b", "ws-p")
181 _, err = e.store.Intend(e.ctx, pscope, hp, IntentSpec{Key: "p/1", Operation: OpProviderCall, Payload: []byte(`{"task":"x","prompt":"p/1"}`), Reserve: Usage{InputTokens: 10, OutputTokens: 10}})
182 wantCode(t, err, CodeBudget)
183 led, err := e.store.Ledger(e.ctx, pscope)
184 must(t, err)
185 if led.Reserved.Calls != 2 {
186 t.Fatalf("parent ledger %+v", led)
187 }
188}
189
190func TestJobDeadlineAndAttemptCap(t *testing.T) {
191 e := newEnv(t)
192 spec := softwareSpec(SoftwareSteps(false))
193 spec.Policy.JobSeconds = 60
194 _, scope := e.createJob(spec)
195 e.clock.Advance(61 * time.Second)
196 _, err := e.store.Claim(e.ctx, scope, "reproduce", "r", "ws")
197 wantCode(t, err, CodeQuota)
198}
199
200var _ = context.Background
201