gunk-dev/the-valley

history_test.go

sourceb675d1fe0233main's published tip history
16.3 KiB
1package review
2
3import (
4 "bytes"
5 "errors"
6 "fmt"
7 "os"
8 "path/filepath"
9 "reflect"
10 "strings"
11 "testing"
12
13 "the-valley/change"
14)
15
16var ref = Ref("main", changeID)
17
18// as is a principal that may write reviews under any name.
19func as(principal string) Pusher {
20 return Pusher{Principal: principal, MayReview: func(string) bool { return true }}
21}
22
23// appendAs builds rec on the head of w's history, checks it as the hook
24// will, as pusher, and moves the ref, and returns the new head.
25func (w *world) appendAs(rec *Record, pusher Pusher) string {
26 w.t.Helper()
27 old := w.refAt(ref)
28 commit, err := Build(w.repo(), old, rec, soon())
29 if err != nil {
30 w.t.Fatal(err)
31 }
32 if err := CheckAppend(w.repo(), Update{Ref: ref, Old: old, New: commit}, pusher, w.against()); err != nil {
33 w.t.Fatalf("CheckAppend: %v", err)
34 }
35 w.setRef(ref, commit)
36 return commit
37}
38
39// A history keeps every review in order, each commit on the one before it
40// and the record it binds, and a withdrawal on the one before it alone. It
41// keeps the records it binds once the change's own record ref is deleted
42// and the repository pruned.
43func TestAHistoryKeepsEveryReviewInOrder(t *testing.T) {
44 w := newWorld(t)
45 s := w.story()
46 r1 := w.verdict(s, "alice", RequestChanges)
47 r2 := w.verdict(s, "bob", Approve)
48 r3 := withdrawal(r1, "alice")
49 c1 := w.appendAs(r1, as("alice"))
50 c2 := w.appendAs(r2, as("bob"))
51 c3 := w.appendAs(r3, as("alice"))
52
53 h, err := ReadHistory(w.repo(), ref, c3, soon())
54 if err != nil {
55 t.Fatal(err)
56 }
57 got := []Entry{}
58 for _, e := range h.Entries {
59 got = append(got, Entry{Commit: e.Commit, Parents: e.Parents})
60 }
61 want := []Entry{{c1, []string{s.rec2}, nil}, {c2, []string{c1, s.rec2}, nil}, {c3, []string{c2}, nil}}
62 if !reflect.DeepEqual(got, want) || h.Head() != c3 {
63 t.Fatalf("the history is %+v, want %+v", got, want)
64 }
65 for i, r := range []*Record{r1, r2, r3} {
66 got, _ := Encode(h.Entries[i].Review)
67 want, _ := Encode(r)
68 if !bytes.Equal(got, want) {
69 t.Fatalf("review %d reads back as %s, want %s", i, got, want)
70 }
71 }
72 if e, ok := h.Withdrawal(r1.ReviewID); !ok || e.Commit != c3 {
73 t.Fatalf("the withdrawal of the first review: %+v, %v", e, ok)
74 }
75 if _, ok := h.Withdrawal(r2.ReviewID); ok {
76 t.Fatal("the second review reads as withdrawn")
77 }
78 if e, ok := h.Find(r2.ReviewID); !ok || e.Commit != c2 {
79 t.Fatalf("the second review: %+v, %v", e, ok)
80 }
81
82 // The change's record ref goes, and with it every other way to reach
83 // its records. The history keeps them, and the heads they name.
84 dangling := w.blob("nothing reaches this\n")
85 w.git(nil, "update-ref", "-d", change.Ref("main", changeID))
86 w.git(nil, "gc", "--quiet", "--prune=now")
87 if _, err := w.run(nil, "cat-file", "-e", dangling); err == nil {
88 t.Fatal("gc pruned nothing, so the test shows nothing")
89 }
90 for _, kept := range []string{s.rec2, s.rec1, s.head2, s.head1} {
91 if _, err := w.run(nil, "cat-file", "-e", kept); err != nil {
92 t.Fatalf("%s was pruned with the record ref: %v", kept, err)
93 }
94 }
95 if _, err := ReadHistory(w.repo(), ref, c3, soon()); err != nil {
96 t.Fatalf("the history after the record ref went: %v", err)
97 }
98 // No record now, so no new judgment; a withdrawal needs none.
99 r4 := w.verdict(s, "carol", Approve)
100 r4.Binding.Record = change.HeadPrefix + s.rec2
101 c4, err := Build(w.repo(), c3, r4, soon())
102 if err != nil {
103 t.Fatal(err)
104 }
105 err = CheckAppend(w.repo(), Update{Ref: ref, Old: c3, New: c4}, as("carol"), w.against())
106 if err == nil || !strings.Contains(err.Error(), "holds no record of the change") {
107 t.Fatalf("a judgment of a change with no record: %v", err)
108 }
109 w.appendAs(withdrawal(r2, "bob"), as("bob"))
110}
111
112// Each case is an update the hook must refuse, and CheckAppend refuses it,
113// saying why.
114func TestCheckAppendRefusesWhatIsNoAppend(t *testing.T) {
115 w := newWorld(t)
116 s := w.story()
117 r1 := w.verdict(s, "alice", RequestChanges)
118 r2 := w.verdict(s, "bob", Approve)
119 c1 := w.appendAs(r1, as("alice"))
120 c2 := w.appendAs(r2, as("bob"))
121 withdrawn := w.appendAs(withdrawal(r1, "alice"), as("alice"))
122 w.setRef(ref, c2)
123
124 encode := func(r *Record) []byte {
125 data, err := Encode(r)
126 if err != nil {
127 t.Fatal(err)
128 }
129 return data
130 }
131 // commit writes a review commit by hand, past every check Build makes.
132 commit := func(r *Record, parents ...string) string { return w.oneFile(File, encode(r), parents...) }
133 edited := func(edit func(*Record)) *Record {
134 r := w.verdict(s, "carol", Approve)
135 edit(r)
136 return r
137 }
138 unknown := withdrawal(r2, "bob")
139 *unknown.Withdraws = id1
140 renamed := withdrawal(r2, "bob")
141 renamed.Review = "operator"
142 extra := w.git([]byte("100644 blob "+w.blob(string(encode(r2)))+"\t"+File+"\n100644 blob "+w.blob("x")+"\textra\n"), "mktree")
143 big := w.oneFile(File, []byte(strings.Repeat(" ", MaxRecord+1)), c2, s.rec2)
144
145 cases := []struct {
146 name string
147 update Update
148 pusher Pusher
149 against func(*Against)
150 says string
151 }{
152 {name: "a delete", update: Update{ref, c2, strings.Repeat("0", 40)}, says: "no push deletes one"},
153 {name: "a ref that is no history's", update: Update{RefPrefix + "main/" + id1, c2, c2}, says: "a review history's ref is"},
154 {name: "a key that names no principal", update: Update{ref, c2, commit(edited(func(r *Record) {}), c2, s.rec2)}, pusher: Pusher{}, says: "named principal"},
155 {name: "a review under a name the pusher may not write", update: Update{ref, c2, commit(edited(func(r *Record) {}), c2, s.rec2)}, pusher: Pusher{Principal: "carol", MayReview: func(name string) bool { return name == "operator" }}, says: "carol may not write reviews under that name"},
156 {name: "a review by a pusher who may write none", update: Update{ref, c2, commit(edited(func(r *Record) {}), c2, s.rec2)}, pusher: Pusher{Principal: "carol"}, says: "carol may not write reviews under that name"},
157 {name: "a review by someone else", update: Update{ref, c2, commit(edited(func(r *Record) {}), c2, s.rec2)}, pusher: as("dave"), says: "names carol as the reviewer, and dave is pushing it"},
158 {name: "an ID the history holds", update: Update{ref, c2, commit(edited(func(r *Record) { r.ReviewID = r1.ReviewID }), c2, s.rec2)}, says: "is the ID of the review in " + c1},
159 {name: "a review of another change", update: Update{ref, c2, commit(edited(func(r *Record) { r.ChangeID = otherID }), c2, s.rec2)}, says: "a review of the change " + otherID},
160 {name: "a first review on a history", update: Update{ref, c2, commit(edited(func(r *Record) {}), s.rec2)}, says: "as the first review commit does"},
161 {name: "a review on an older head", update: Update{ref, c2, commit(edited(func(r *Record) {}), c1, s.rec2)}, says: "its first parent is " + c1},
162 {name: "a second history", update: Update{ref, "", commit(edited(func(r *Record) {}), c1, s.rec2)}, says: "it starts the history"},
163 {name: "a judgment that does not keep its record", update: Update{ref, c2, commit(edited(func(r *Record) {}), c2, s.head2)}, says: "as its last parent"},
164 {name: "a judgment with three parents", update: Update{ref, c2, commit(edited(func(r *Record) {}), c2, s.rec1, s.rec2)}, says: "has 3 parents"},
165 {name: "a tree with another file", update: Update{ref, c2, w.commitTree(extra, c2, s.rec2)}, says: "holds something other than review.json alone"},
166 {name: "a review of more than 64 KiB", update: Update{ref, c2, big}, says: "more than the 65536 it may be"},
167 {name: "a blob", update: Update{ref, c2, w.blob("x")}, says: "is a blob, where a commit is expected"},
168 {name: "a history that does not read", update: Update{ref, s.head2, commit(edited(func(r *Record) {}), s.head2, s.rec2)}, says: "the history it appends to does not read"},
169 {name: "a binding that does not verify", update: Update{ref, c2, commit(edited(func(r *Record) { r.Binding.Delta = digest }), c2, s.rec2)}, says: "its binding does not verify"},
170 {name: "a change with no record", update: Update{ref, c2, commit(edited(func(r *Record) {}), c2, s.rec2)}, against: func(a *Against) { a.RecordTip = "" }, says: "holds no record of the change"},
171 {name: "a withdrawal of someone else's review", update: Update{ref, c2, commit(withdrawal(r1, "bob"), c2)}, pusher: as("bob"), says: "only its reviewer or an operator"},
172 {name: "a withdrawal of a review the history lacks", update: Update{ref, c2, commit(unknown, c2)}, pusher: as("bob"), says: "holds no review with that ID"},
173 {name: "a withdrawal under another name", update: Update{ref, c2, commit(renamed, c2)}, pusher: as("bob"), says: "under the review name operator"},
174 {name: "a withdrawal twice", update: Update{ref, withdrawn, commit(withdrawal(r1, "alice"), withdrawn)}, pusher: as("alice"), says: "withdrew already"},
175 {name: "a withdrawal with the record as a parent", update: Update{ref, c2, commit(withdrawal(r2, "bob"), c2, s.rec2)}, pusher: as("bob"), says: "where a withdrawal has one"},
176 {name: "a withdrawal that starts a history", update: Update{ref, "", commit(withdrawal(r2, "bob"), s.rec2)}, pusher: as("bob"), says: "holds no review with that ID"},
177 }
178 for _, c := range cases {
179 t.Run(c.name, func(t *testing.T) {
180 pusher := c.pusher
181 if pusher.Principal == "" && !strings.Contains(c.says, "named principal") {
182 pusher = as("carol")
183 }
184 a := w.against()
185 if c.against != nil {
186 c.against(&a)
187 }
188 err := CheckAppend(w.repo(), c.update, pusher, a)
189 if err == nil {
190 t.Fatal("it was admitted")
191 }
192 if !strings.Contains(err.Error(), c.says) {
193 t.Fatalf("the refusal does not say %q: %v", c.says, err)
194 }
195 })
196 }
197 // A withdrawal is final: none withdraws it.
198 w.setRef(ref, withdrawn)
199 h, err := ReadHistory(w.repo(), ref, withdrawn, soon())
200 if err != nil {
201 t.Fatal(err)
202 }
203 final := withdrawal(h.Entries[2].Review, "alice")
204 err = CheckAppend(w.repo(), Update{ref, withdrawn, commit(final, withdrawn)}, as("alice"), w.against())
205 if err == nil || !strings.Contains(err.Error(), "a withdrawal is final") {
206 t.Fatalf("a withdrawal of a withdrawal: %v", err)
207 }
208 // An operator may withdraw anyone's review, under any name, and needs
209 // no grant of the name to do it; to judge a version, they need one.
210 w.appendAs(withdrawal(r2, "operator"), Pusher{Principal: "operator", Operator: true})
211 judged := w.verdict(s, "operator", Approve)
212 err = CheckAppend(w.repo(), Update{ref, w.refAt(ref), commit(judged, w.refAt(ref), s.rec2)}, Pusher{Principal: "operator", Operator: true}, w.against())
213 if err == nil || !strings.Contains(err.Error(), "operator may not write reviews under that name") {
214 t.Fatalf("an operator's judgment under a name they may not write: %v", err)
215 }
216}
217
218// A judgment needs the grant of its review name, and a withdrawal does
219// not. A reviewer whose grant is revoked can still take back their own
220// judgment, and an operator can take back anyone's. Nobody else can take
221// one back, with the grant or without it.
222func TestAWithdrawalNeedsNoGrantOfItsName(t *testing.T) {
223 w := newWorld(t)
224 s := w.story()
225 r1 := w.verdict(s, "alice", RequestChanges)
226 r2 := w.verdict(s, "bob", RequestChanges)
227 w.appendAs(r1, as("alice"))
228 head := w.appendAs(r2, as("bob"))
229 revoked := func(principal string) Pusher {
230 return Pusher{Principal: principal, MayReview: func(string) bool { return false }}
231 }
232 check := func(rec *Record, p Pusher) error {
233 t.Helper()
234 c, err := Build(w.repo(), head, rec, soon())
235 if err != nil {
236 t.Fatal(err)
237 }
238 return CheckAppend(w.repo(), Update{ref, head, c}, p, w.against())
239 }
240
241 if err := check(w.verdict(s, "alice", Approve), revoked("alice")); err == nil || !strings.Contains(err.Error(), "alice may not write reviews under that name") {
242 t.Fatalf("a judgment under a revoked grant: %v", err)
243 }
244 for _, p := range []Pusher{as("carol"), revoked("carol"), {Principal: "carol"}} {
245 if err := check(withdrawal(r1, "carol"), p); err == nil || !strings.Contains(err.Error(), "only its reviewer or an operator") {
246 t.Fatalf("a withdrawal of alice's review by carol: %v", err)
247 }
248 }
249
250 w.appendAs(withdrawal(r1, "alice"), revoked("alice"))
251 w.appendAs(withdrawal(r2, "operator"), Pusher{Principal: "operator", Operator: true})
252 h, err := ReadHistory(w.repo(), ref, w.refAt(ref), soon())
253 if err != nil {
254 t.Fatal(err)
255 }
256 for _, r := range []*Record{r1, r2} {
257 if _, ok := h.Withdrawal(r.ReviewID); !ok {
258 t.Fatalf("%s's review is not withdrawn", r.Reviewer)
259 }
260 }
261}
262
263// Two clients append to one history at once. The second's publish fails
264// on its stale lease; it reads the new head and appends again, with the
265// same review. Building that review once more finds it recorded.
266func TestAppendsAreCompareAndSwap(t *testing.T) {
267 origin := newWorld(t)
268 s := origin.story()
269 a, b := origin.clone(), origin.clone()
270 ra := a.verdict(s, "alice", RequestChanges)
271 rb := b.verdict(s, "bob", Approve)
272
273 ca, err := Build(a.repo(), "", ra, soon())
274 if err != nil {
275 t.Fatal(err)
276 }
277 cb, err := Build(b.repo(), "", rb, soon())
278 if err != nil {
279 t.Fatal(err)
280 }
281 if err := Publish(a.repo(), "origin", ref, "", ca, soon()); err != nil {
282 t.Fatal(err)
283 }
284 if err := Publish(b.repo(), "origin", ref, "", cb, soon()); !errors.Is(err, ErrStale) {
285 t.Fatalf("a publish on a stale lease: %v", err)
286 }
287 b.git(nil, "fetch", "--quiet", "origin", "+"+ref+":"+ref)
288 head := b.refAt(ref)
289 if head != ca {
290 t.Fatalf("the fetched head is %s, where %s published %s", head, "alice", ca)
291 }
292 cb, err = Build(b.repo(), head, rb, soon())
293 if err != nil {
294 t.Fatal(err)
295 }
296 if err := CheckAppend(b.repo(), Update{ref, head, cb}, as("bob"), b.against()); err != nil {
297 t.Fatal(err)
298 }
299 if err := Publish(b.repo(), "origin", ref, head, cb, soon()); err != nil {
300 t.Fatal(err)
301 }
302 if again, err := Build(b.repo(), cb, rb, soon()); !errors.Is(err, ErrRecorded) || again != cb {
303 t.Fatalf("building a recorded review again: %s, %v", again, err)
304 }
305 changed := *rb
306 changed.Summary = "Another judgment under the same ID."
307 if _, err := Build(b.repo(), cb, &changed, soon()); err == nil || errors.Is(err, ErrRecorded) {
308 t.Fatalf("another review under a recorded ID: %v", err)
309 }
310 if err := Publish(a.repo(), "origin", ref, ca, ca, soon()); !errors.Is(err, ErrStale) {
311 t.Fatalf("a publish on a lease of an older head: %v", err)
312 }
313 // A withdrawal, which has no notes, is found recorded too.
314 wb := withdrawal(rb, "bob")
315 cw, err := Build(b.repo(), cb, wb, soon())
316 if err != nil {
317 t.Fatal(err)
318 }
319 if err := Publish(b.repo(), "origin", ref, cb, cw, soon()); err != nil {
320 t.Fatal(err)
321 }
322 if again, err := Build(b.repo(), cw, wb, soon()); !errors.Is(err, ErrRecorded) || again != cw {
323 t.Fatalf("building a recorded withdrawal again: %s, %v", again, err)
324 }
325 h, err := ReadHistory(origin.repo(), ref, origin.refAt(ref), soon())
326 if err != nil {
327 t.Fatal(err)
328 }
329 if len(h.Entries) != 3 || h.Entries[0].Review.ReviewID != ra.ReviewID || h.Entries[1].Review.ReviewID != rb.ReviewID {
330 t.Fatalf("origin's history holds %d reviews", len(h.Entries))
331 }
332}
333
334// A history holds at most MaxHistory reviews: reading a longer one fails,
335// and so does appending to a full one.
336func TestAHistoryIsBounded(t *testing.T) {
337 w := newWorld(t)
338 s := w.story()
339 r := w.verdict(s, "alice", Approve)
340 var stream strings.Builder
341 for k := 1; k <= MaxHistory+1; k++ {
342 id, err := NewID()
343 if err != nil {
344 t.Fatal(err)
345 }
346 r.ReviewID = id
347 data, err := Encode(r)
348 if err != nil {
349 t.Fatal(err)
350 }
351 fmt.Fprintf(&stream, "commit refs/heads/scratch\nmark :%d\ncommitter t <t@localhost> 0 +0000\ndata 1\nr\n", k)
352 if k == 1 {
353 fmt.Fprintf(&stream, "from %s\n", s.rec2)
354 } else {
355 fmt.Fprintf(&stream, "from :%d\nmerge %s\n", k-1, s.rec2)
356 }
357 fmt.Fprintf(&stream, "deleteall\nM 100644 inline %s\ndata %d\n%s\n", File, len(data), data)
358 }
359 marks := filepath.Join(t.TempDir(), "marks")
360 w.git([]byte(stream.String()), "fast-import", "--quiet", "--export-marks="+marks)
361 exported, err := os.ReadFile(marks)
362 if err != nil {
363 t.Fatal(err)
364 }
365 commits := map[string]string{}
366 for _, line := range strings.Split(strings.TrimSpace(string(exported)), "\n") {
367 mark, oid, _ := strings.Cut(line, " ")
368 commits[mark] = oid
369 }
370 full, past := commits[fmt.Sprintf(":%d", MaxHistory)], commits[fmt.Sprintf(":%d", MaxHistory+1)]
371 h, err := ReadHistory(w.repo(), ref, full, soon())
372 if err != nil || len(h.Entries) != MaxHistory {
373 t.Fatalf("a history of %d reviews: %v", MaxHistory, err)
374 }
375 if _, err := ReadHistory(w.repo(), ref, past, soon()); err == nil || !strings.Contains(err.Error(), "more than the 512") {
376 t.Fatalf("a history of %d reviews: %v", MaxHistory+1, err)
377 }
378 err = CheckAppend(w.repo(), Update{ref, full, past}, as("alice"), w.against())
379 if err == nil || !strings.Contains(err.Error(), "the most a history may") {
380 t.Fatalf("an append to a full history: %v", err)
381 }
382}