oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package forge
2
3import (
4 "context"
5 "errors"
6 "fmt"
7 "os"
8 "path/filepath"
9 "testing"
10 "time"
11
12 "amadan.net/oficina/equipo/internal/equipo/team"
13 "amadan.net/oficina/equipo/internal/runnerproto"
14)
15
16func assertNoEffect(t *testing.T, f *fixture, err error) {
17 t.Helper()
18 if !errors.Is(err, team.ErrNotDispatched) || errors.Is(err, team.ErrUncertain) || f.pushes != 0 {
19 t.Fatalf("want definitive no dispatch, got %v; pushes=%d", err, f.pushes)
20 }
21 r, err := f.p.Reconcile(ctx, f.auth.Ticket.Dispatch)
22 if !errors.Is(err, team.ErrNotDispatched) || r.Receipt != nil {
23 t.Fatalf("reconcile did not confirm no dispatch: %+v %v", r, err)
24 }
25}
26
27func TestPublishNoEffect(t *testing.T) {
28 for _, name := range []string{"authorize", "validate", "revalidate", "credential", "mkdir", "export", "init", "rev-parse", "expired_export", "journal", "query", "final_authority", "final_validate", "final_objects", "description", "cancel_description", "push_marker", "context"} {
29 t.Run(name, func(t *testing.T) {
30 f := newFixture(t)
31 cleanup := func() {}
32 switch name {
33 case "authorize":
34 f.p.cfg.Authorize = func(context.Context, team.Dispatch) (Authorization, error) { return Authorization{}, team.ErrFence }
35 case "validate":
36 f.auth.Ticket.Incarnation++
37 case "revalidate":
38 f.p.cfg.Revalidate = func(context.Context, Authorization) error { return team.ErrFence }
39 case "credential":
40 f.p.cfg.Credential = func(context.Context, string) (Credential, error) { return Credential{}, team.ErrUncertain }
41 case "mkdir":
42 original := f.p.cfg.Credential
43 f.p.cfg.Credential = func(c context.Context, s string) (Credential, error) {
44 must(t, os.Rename(f.p.cfg.Root, f.p.cfg.Root+"-saved"))
45 must(t, os.WriteFile(f.p.cfg.Root, nil, 0600))
46 return original(c, s)
47 }
48 cleanup = func() { must(t, os.Remove(f.p.cfg.Root)); must(t, os.Rename(f.p.cfg.Root+"-saved", f.p.cfg.Root)) }
49 case "export":
50 f.p.cfg.Export = func(context.Context, team.GitCandidate, string) error { return team.ErrUncertain }
51 case "init", "rev-parse":
52 wrapper := filepath.Join(f.root, "git-fail")
53 must(t, os.WriteFile(wrapper, []byte(fmt.Sprintf("#!/bin/sh\nfor arg do\n if [ \"$arg\" = %s ]; then exit 79; fi\ndone\nexec /usr/bin/git \"$@\"\n", name)), 0700))
54 f.p.cfg.GitPath = wrapper
55 case "expired_export":
56 original := f.p.cfg.Export
57 f.p.cfg.Export = func(c context.Context, g team.GitCandidate, s string) error {
58 e := original(c, g, s)
59 f.p.cfg.Clock = func() time.Time { return f.auth.Ticket.ExpiresAt }
60 return e
61 }
62 case "journal":
63 original := f.p.cfg.Export
64 f.p.cfg.Export = func(c context.Context, g team.GitCandidate, s string) error {
65 e := original(c, g, s)
66 must(t, os.Mkdir(f.p.path(f.auth.Ticket.Dispatch), 0700))
67 return e
68 }
69 cleanup = func() { must(t, os.Remove(f.p.path(f.auth.Ticket.Dispatch))) }
70 case "query":
71 must(t, os.Rename(f.remote, f.remote+"-hidden"))
72 case "final_authority":
73 calls := 0
74 f.p.cfg.Revalidate = func(context.Context, Authorization) error {
75 calls++
76 if calls > 1 {
77 return team.ErrFence
78 }
79 return nil
80 }
81 case "final_validate":
82 original := f.p.localRemote
83 f.p.localRemote = func(s string) string {
84 f.p.cfg.Clock = func() time.Time { return f.auth.Ticket.ExpiresAt }
85 return original(s)
86 }
87 case "final_objects":
88 original := f.p.localRemote
89 f.p.localRemote = func(s string) string {
90 dirs, e := filepath.Glob(filepath.Join(f.p.cfg.Root, "objects-*", "git", "objects"))
91 must(t, e)
92 for _, dir := range dirs {
93 must(t, os.RemoveAll(dir))
94 }
95 return original(s)
96 }
97 case "description":
98 f.desc.fail = true
99 case "cancel_description":
100 f.desc.before = func() { f.cancelled = true }
101 case "push_marker":
102 f.desc.before = func() {
103 path := f.p.path(f.auth.Ticket.Dispatch)
104 must(t, os.Rename(path, path+"-saved"))
105 must(t, os.Mkdir(path, 0700))
106 }
107 cleanup = func() {
108 path := f.p.path(f.auth.Ticket.Dispatch)
109 must(t, os.Remove(path))
110 must(t, os.Rename(path+"-saved", path))
111 }
112 }
113 callCtx := ctx
114 if name == "context" {
115 var cancel context.CancelFunc
116 callCtx, cancel = context.WithCancel(ctx)
117 cancel()
118 }
119 _, err := f.p.Execute(callCtx, f.auth.Ticket.Dispatch)
120 cleanup()
121 if name == "description" || name == "cancel_description" || name == "push_marker" {
122 if !errors.Is(err, team.ErrUncertain) {
123 t.Fatal("post-description failure claimed no effect", err)
124 }
125 if _, e := f.p.Reconcile(ctx, f.auth.Ticket.Dispatch); !errors.Is(e, team.ErrUncertain) {
126 t.Fatal("description uncertainty lost", e)
127 }
128 return
129 }
130 if name == "expired_export" {
131 if _, e := os.Stat(f.p.path(f.auth.Ticket.Dispatch)); !os.IsNotExist(e) {
132 t.Fatal("expired export wrote a journal")
133 }
134 }
135 assertNoEffect(t, f, err)
136 if name == "final_authority" || name == "final_validate" || name == "final_objects" {
137 if f.desc.calls != 0 {
138 t.Fatal("description changed before final authority and object checks")
139 }
140 }
141 })
142 }
143}
144
145func TestPublishMissingJournal(t *testing.T) {
146 f := newFixture(t)
147 r, err := f.p.Reconcile(ctx, f.auth.Ticket.Dispatch)
148 if !errors.Is(err, team.ErrNotDispatched) || r.Receipt != nil {
149 t.Fatalf("missing journal is no dispatch: %+v %v", r, err)
150 }
151}
152
153func TestPublishIndependentActionLocks(t *testing.T) {
154 a := newFixture(t)
155 _, err := a.p.Execute(ctx, a.auth.Ticket.Dispatch)
156 must(t, err)
157 entered, release, done := make(chan struct{}), make(chan struct{}), make(chan error, 1)
158 credential := a.p.cfg.Credential
159 a.p.cfg.Credential = func(c context.Context, s string) (Credential, error) {
160 close(entered)
161 <-release
162 return credential(c, s)
163 }
164 go func() { _, e := a.p.Reconcile(ctx, a.auth.Ticket.Dispatch); done <- e }()
165 <-entered
166 b := newFixture(t)
167 b.p.cfg.Root = a.p.cfg.Root
168 b.auth.Ticket.Dispatch.Action++
169 b.auth.Ticket.Scope.Job++
170 _, err = b.p.Execute(ctx, b.auth.Ticket.Dispatch)
171 close(release)
172 must(t, <-done)
173 must(t, err)
174 if b.pushes != 1 {
175 t.Fatal("independent action did not push")
176 }
177}
178
179func TestPublishSameActionLockStaysUncertain(t *testing.T) {
180 f := newFixture(t)
181 entered, release, done := make(chan struct{}), make(chan struct{}), make(chan error, 1)
182 original := f.p.cfg.Export
183 f.p.cfg.Export = func(c context.Context, g team.GitCandidate, s string) error {
184 close(entered)
185 <-release
186 return original(c, g, s)
187 }
188 go func() { _, err := f.p.Execute(ctx, f.auth.Ticket.Dispatch); done <- err }()
189 <-entered
190 _, err := f.p.Reconcile(ctx, f.auth.Ticket.Dispatch)
191 close(release)
192 must(t, <-done)
193 if !errors.Is(err, team.ErrUncertain) {
194 t.Fatal("live action was reported absent", err)
195 }
196}
197
198func TestPublishDefaultChangeAndManualMerge(t *testing.T) {
199 for _, name := range []string{"default_change", "manual_merge"} {
200 t.Run(name, func(t *testing.T) {
201 f := newFixture(t)
202 gitTest(t, f.root, "--git-dir="+f.remote, "fetch", f.source, f.auth.Candidate.Commit)
203 gitTest(t, f.root, "--git-dir="+f.remote, "update-ref", "refs/heads/main", f.auth.Candidate.Commit)
204 if name == "default_change" {
205 gitTest(t, f.root, "--git-dir="+f.remote, "symbolic-ref", "HEAD", "refs/heads/new-default")
206 }
207 f.auth.Publication.Ref = "main"
208 f.auth.Ticket.Dispatch.Payload, f.auth.Ticket.Dispatch.Hash, _ = Canonical(marshalPublication(t, f.auth.Publication))
209 _, err := f.p.Execute(ctx, f.auth.Ticket.Dispatch)
210 assertNoEffect(t, f, err)
211 if f.desc.calls != 0 {
212 t.Fatal("external branch state granted description authority")
213 }
214 })
215 }
216}
217
218func TestPublishPushMarkerDurable(t *testing.T) {
219 f := newFixture(t)
220 f.p.beforePush = func() {
221 f.pushes++
222 r, err := f.p.load(f.auth.Ticket.Dispatch)
223 must(t, err)
224 if r.Phase != "push_started" {
225 t.Fatal("push began without durable marker", r.Phase)
226 }
227 }
228 _, err := f.p.Execute(ctx, f.auth.Ticket.Dispatch)
229 must(t, err)
230}
231
232func TestPublishJournalRecoveryClasses(t *testing.T) {
233 for _, name := range []string{"legacy", "unknown_phase", "corrupt", "prepared_with_remote", "already_identical"} {
234 t.Run(name, func(t *testing.T) {
235 f := newFixture(t)
236 d := f.auth.Ticket.Dispatch
237 if name == "already_identical" {
238 gitTest(t, f.root, "--git-dir="+f.remote, "fetch", f.source, f.auth.Candidate.Commit)
239 gitTest(t, f.root, "--git-dir="+f.remote, "update-ref", "refs/heads/heads/main", f.auth.Candidate.Commit)
240 _, err := f.p.Execute(ctx, d)
241 must(t, err)
242 r, err := f.p.Reconcile(ctx, d)
243 must(t, err)
244 if r.Receipt == nil || f.pushes != 0 || f.desc.calls != 1 {
245 t.Fatal("identical head did not reconcile", r)
246 }
247 return
248 }
249 _, err := f.p.Execute(ctx, d)
250 must(t, err)
251 r, err := f.p.load(d)
252 must(t, err)
253 switch name {
254 case "legacy":
255 r.Phase = ""
256 case "unknown_phase":
257 r.Phase = "unrecognized"
258 case "prepared_with_remote":
259 r.Phase = "prepared"
260 }
261 must(t, runnerproto.JournalCredentials(f.p.path(d), r))
262 if name == "corrupt" {
263 must(t, os.WriteFile(f.p.path(d), []byte("{"), 0600))
264 }
265 result, err := f.p.Reconcile(ctx, d)
266 switch name {
267 case "legacy":
268 must(t, err)
269 if result.Receipt == nil {
270 t.Fatal("legacy lost receipt")
271 }
272 case "prepared_with_remote":
273 if !errors.Is(err, team.ErrNotDispatched) {
274 t.Fatal("prepared journal inferred dispatch from remote", err)
275 }
276 default:
277 if !errors.Is(err, team.ErrUncertain) {
278 t.Fatal("unreadable history became no effect", err)
279 }
280 }
281 })
282 }
283}
284
285func TestPublishReconcileTombstone(t *testing.T) {
286 f := newFixture(t)
287 _, err := f.p.Reconcile(ctx, f.auth.Ticket.Dispatch)
288 if !errors.Is(err, team.ErrNotDispatched) {
289 t.Fatal(err)
290 }
291 _, err = f.p.Execute(ctx, f.auth.Ticket.Dispatch)
292 if !errors.Is(err, team.ErrNotDispatched) || f.pushes != 0 {
293 t.Fatal("reconciled absence was dispatched", err, f.pushes)
294 }
295}
296func TestPublishDescriptionFailureUncertain(t *testing.T) {
297 f := newFixture(t)
298 f.desc.fail = true
299 _, err := f.p.Execute(ctx, f.auth.Ticket.Dispatch)
300 if !errors.Is(err, team.ErrUncertain) {
301 t.Fatal("description Set may have succeeded", err)
302 }
303}
304