gunk-dev/the-valley

report_test.go

sourcefce1c319f644main's published tip history
11.0 KiB
1package main
2
3import (
4 "fmt"
5 "strings"
6 "testing"
7
8 "the-valley/change"
9)
10
11var reportRef = change.RestackRef("main", changeID)
12
13// restackers is a repository where restacker holds the restack grant and
14// both holds it beside the approve grant, under its name request.
15func restackers(t *testing.T) *records {
16 r := newRecords(t)
17 r.p.grants["restacker"] = map[string]bool{grantRestack: true}
18 r.p.grants["both"] = map[string]bool{grantRestack: true, grantRequest: true}
19 return r
20}
21
22// restacked is rec with a new version at head h uploaded by who, on the
23// active version's branch and under its title, as the restack row has it.
24func restacked(rec change.Record, who, h string) change.Record {
25 was := rec.ActiveVersion()
26 rec = next(rec, who, h)
27 rec.Versions[len(rec.Versions)-1].Title = was.Title
28 rec.Versions[len(rec.Versions)-1].Branch = was.Branch
29 return rec
30}
31
32func TestRestackingAVersion(t *testing.T) {
33 r := restackers(t)
34 h1, h2, h3 := r.source("one"), r.source("two"), r.source("three")
35 v1 := first("alice", h1)
36 ready := r.write(encode(t, v1), h1)
37 update := func(what, who, old, text, says string, parents ...string) {
38 t.Helper()
39 r.expect(what, who, recordRef, old, r.write(text, parents...), says)
40 }
41 mutate := func(f func(*change.Record)) string {
42 rec := restacked(v1, "restacker", h2)
43 f(&rec)
44 return encode(t, rec)
45 }
46 good := encode(t, restacked(v1, "restacker", h2))
47
48 update("the restack row", "restacker", ready, good, "", ready, h2)
49
50 // Who. A principal without the grant is refused as before, and the
51 // grant opens nothing to a key that names no principal.
52 update("a principal without the grant", "mallory", ready, encode(t, restacked(v1, "mallory", h2)),
53 "only the change's owner, alice, or a principal holding the approve grant (also accepted as request)", ready, h2)
54 update("an untagged key", "", ready, good, "a key that names no principal", ready, h2)
55
56 // The state before and after.
57 for _, state := range []string{change.Draft, change.Withdrawn} {
58 paused := moved(v1, state)
59 old := r.write(encode(t, paused), ready)
60 update("a version on a change that is "+state, "restacker", old,
61 encode(t, restacked(paused, "restacker", h2)), "the change is "+state+", and the restack grant adds a version only to a change that is ready", old, h2)
62 }
63 for _, state := range []string{change.Draft, change.Withdrawn} {
64 update("a move to "+state, "restacker", ready, encode(t, moved(v1, state)),
65 "the restack grant changes no state", ready)
66 }
67 update("a new version that leaves the change a draft", "restacker", ready,
68 mutate(func(c *change.Record) { c.State = change.Draft }), "state is draft", ready, h2)
69
70 // What every new version is held to holds here too.
71 update("two new versions", "restacker", ready,
72 encode(t, restacked(restacked(v1, "restacker", h3), "restacker", h2)), "holds 3", ready, h2)
73 update("a changed earlier version", "restacker", ready,
74 mutate(func(c *change.Record) { c.Versions[0].Title = "rewritten" }), "changes version 1", ready, h2)
75 update("the owner changed", "restacker", ready, mutate(func(c *change.Record) { c.Owner = "restacker" }),
76 "changes the owner from alice to restacker", ready, h2)
77 update("the target changed", "restacker", ready, mutate(func(c *change.Record) { c.Target = "release" }),
78 "names the target release", ready, h2)
79 update("a forged uploader", "restacker", ready, encode(t, restacked(v1, "alice", h2)),
80 "names alice as its uploader, and restacker is pushing it", ready, h2)
81 update("seq incremented twice", "restacker", ready, mutate(func(c *change.Record) { c.Seq = 3 }),
82 "seq is 3", ready, h2)
83 update("active left on the old version", "restacker", ready, mutate(func(c *change.Record) { c.Active = 1 }),
84 "active is 1", ready, h2)
85 update("the active head again", "restacker", ready, encode(t, restacked(v1, "restacker", h1)),
86 "needs a new head", ready, h1)
87 update("a head other than the second parent", "restacker", ready, good, "second parent is "+h3, ready, h3)
88
89 // What the restack row holds a version to beyond those.
90 update("another branch", "restacker", ready, mutate(func(c *change.Record) { c.Versions[1].Branch = "elsewhere" }),
91 `names the branch "elsewhere"`, ready, h2)
92 update("no branch", "restacker", ready, mutate(func(c *change.Record) { c.Versions[1].Branch = "" }),
93 `names the branch ""`, ready, h2)
94 update("another title", "restacker", ready, mutate(func(c *change.Record) { c.Versions[1].Title = "retitled" }),
95 `is titled "retitled"`, ready, h2)
96
97 // Deleting is the approve grant's alone.
98 r.expect("a delete", "restacker", recordRef, ready, zeroID, "only a principal holding the approve grant (also accepted as request) may delete")
99
100 // A holder that owns the change, or holds the approve grant beside
101 // it, writes by those rows, unrestricted by this one.
102 own := first("restacker", h1)
103 mine := r.write(encode(t, own), h1)
104 r.expect("its own record, created", "restacker", recordRef, zeroID, mine, "")
105 update("its own record, unreadied", "restacker", mine, encode(t, moved(own, change.Draft)), "", mine)
106 update("its own record, retitled", "restacker", mine, encode(t, next(own, "restacker", h2)), "", mine, h2)
107 update("another's record, unreadied with the approve grant", "both", ready, encode(t, moved(v1, change.Draft)), "", ready)
108 r.expect("another's record, deleted with the approve grant", "both", recordRef, ready, zeroID, "")
109}
110
111// report is a well-formed report about the change, of a conflict
112// observed at the head h.
113func report(t *testing.T, h string) change.RestackReport {
114 t.Helper()
115 failing, tip := changeID, change.HeadPrefix+h
116 return change.RestackReport{
117 Schema: change.RestackSchema, ChangeID: changeID, Target: "main",
118 TargetTip: change.HeadPrefix + h, Operation: "op-1", Result: change.RestackConflict,
119 Layers: []change.RestackLayer{{
120 ChangeID: changeID, Record: change.HeadPrefix + h, Version: 1, Head: change.HeadPrefix + h,
121 Branch: "topic", BranchTip: &tip,
122 }},
123 Failing: &failing, Conflicts: []string{"a.go"},
124 }
125}
126
127func encodeReport(t *testing.T, rep change.RestackReport) string {
128 t.Helper()
129 out, err := change.EncodeRestackReport(&rep)
130 if err != nil {
131 t.Fatal(err)
132 }
133 return string(out)
134}
135
136// writeReport writes a report commit holding text as report.json.
137func (r *records) writeReport(text string, parents ...string) string {
138 tree := r.git(fmt.Sprintf("100644 blob %s\treport.json\n", r.blob(text)), "mktree")
139 return r.commit(tree, parents, "a restack report")
140}
141
142func TestWritingARestackReport(t *testing.T) {
143 r := restackers(t)
144 h := r.source("one")
145 good := encodeReport(t, report(t, h))
146 expect := func(what, who, old, new, says string) {
147 t.Helper()
148 r.expect(what, who, reportRef, old, new, says)
149 }
150
151 // About a change the repository records.
152 expect("a report about a change no record names", "restacker", zeroID, r.writeReport(good),
153 "holds no record of the change "+changeID)
154 r.git("", "update-ref", recordRef, r.write(encode(t, first("alice", h)), h))
155
156 first := r.writeReport(good)
157 expect("the first report", "restacker", zeroID, first, "")
158 r.git("", "update-ref", reportRef, first)
159 second := r.writeReport(good, first)
160 expect("a report extending it", "restacker", first, second, "")
161
162 // Who.
163 for _, who := range []string{"alice", "operator", ""} {
164 expect("a report by "+who, who, first, second, "refs/the-valley/restack/ takes writes only from a principal holding the restack grant")
165 }
166
167 // History.
168 expect("a delete", "restacker", first, zeroID, "no push deletes one")
169 expect("a first report with a parent", "restacker", zeroID, second, "has no parent, and this one has 1")
170 expect("a report that starts over", "restacker", first, r.writeReport(good), "as its one parent")
171 expect("a report on another parent", "restacker", first, r.writeReport(good, h), "as its one parent")
172 expect("a report with two parents", "restacker", first, r.writeReport(good, first, h), "as its one parent")
173
174 // The ref.
175 other := change.RestackRef("release", changeID)
176 r.expect("another target", "restacker", other, zeroID, r.writeReport(good), "refs/the-valley/restack/main/<change ID>")
177 r.expect("no change ID", "restacker", change.RestackPrefix+"main/topic", zeroID, r.writeReport(good), "32 letters from k to z")
178 r.expect("a ref that only begins with the namespace", "restacker", "refs/the-valley/restacked/main/"+changeID, zeroID,
179 r.writeReport(good), "written only by the machinery")
180
181 // The report.
182 rep := report(t, h)
183 rep.ChangeID, rep.Layers[0].ChangeID, *rep.Failing = strings.Repeat("k", 32), strings.Repeat("k", 32), strings.Repeat("k", 32)
184 expect("a report about another change", "restacker", first, r.writeReport(encodeReport(t, rep), first),
185 "about the change kkkk")
186 rep = report(t, h)
187 rep.Target = "release"
188 expect("a report naming another target", "restacker", first, r.writeReport(encodeReport(t, rep), first),
189 "names the target release")
190 expect("an unknown field", "restacker", first,
191 r.writeReport(strings.Replace(good, `"result"`, `"extra": 1, "result"`, 1), first), `"extra"`)
192 expect("a report that is no JSON", "restacker", first, r.writeReport("conflict", first), "does not read")
193
194 // The commit's shape.
195 blob := r.blob(good)
196 shaped := func(what, entries, says string) {
197 t.Helper()
198 expect(what, "restacker", first, r.commit(r.git(entries, "mktree"), []string{first}, "a restack report"), says)
199 }
200 shaped("a second file", fmt.Sprintf("100644 blob %s\treport.json\n100644 blob %s\tz\n", blob, blob),
201 "a restack report's tree holds report.json alone")
202 shaped("another name", fmt.Sprintf("100644 blob %s\trecord.json\n", blob), `"record.json"`)
203 shaped("an executable", fmt.Sprintf("100755 blob %s\treport.json\n", blob), "mode 100755")
204
205 // Bounds, each refused on the object's type and size alone.
206 expect("an oversized report.json", "restacker", first, r.writeReport(good+strings.Repeat(" ", change.MaxReportBlob), first),
207 "more than the 65536")
208 big := r.commit(r.git(fmt.Sprintf("100644 blob %s\treport.json\n", blob), "mktree"), []string{first},
209 strings.Repeat("x", change.MaxReportCommit))
210 expect("an oversized commit", "restacker", first, big, "more than the 4096")
211 objects, err := openObjectsWithin(budget{subject: "a restack report", depth: 1, entriesInTree: 1, entries: 1,
212 treeSize: change.MaxReportTree, fileSize: change.MaxReportBlob, bytes: 1 << 20})
213 if err != nil {
214 t.Fatal(err)
215 }
216 _, err = readFileCommit(objects, big, reportLayout)
217 objects.close()
218 if err == nil || strings.Join(objects.requests, "; ") != "info "+big {
219 t.Errorf("an oversized commit: requests = %v, error = %v; want its info alone", objects.requests, err)
220 }
221}
222
223// A push may update at most maxReportUpdates reports, and one that names
224// more is refused before any is read.
225func TestAPushIsBoundInTheReportsItWrites(t *testing.T) {
226 var lines []string
227 for i := 0; i <= maxReportUpdates; i++ {
228 id := strings.Repeat("k", 30) + string(rune('k'+i/16)) + string(rune('k'+i%16))
229 lines = append(lines, zeroID+" "+strings.Repeat("1", 40)+" "+change.RestackRef("main", id))
230 }
231 code, says := pushTo(t, unreadable{t}, "restacker", lines)
232 if code == 0 || !strings.Contains(says, "it updates 65 restack reports, and a push may update at most 64") {
233 t.Errorf("a push of %d reports: exit %d, %q", len(lines), code, says)
234 }
235}