gunk-dev/the-valley

report_test.go

source2886163c0514main's published tip history
10.9 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 request grant.
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 request grant", 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 request grant's alone.
98 r.expect("a delete", "restacker", recordRef, ready, zeroID, "only a principal holding the request grant may delete")
99
100 // A holder that owns the change, or holds the request 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 request grant", "both", ready, encode(t, moved(v1, change.Draft)), "", ready)
108 r.expect("another's record, deleted with the request 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 := changeID
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, Branch: "topic", Head: change.HeadPrefix + h,
121 }},
122 Failing: &failing, Conflicts: []string{"a.go"},
123 }
124}
125
126func encodeReport(t *testing.T, rep change.RestackReport) string {
127 t.Helper()
128 out, err := change.EncodeRestackReport(&rep)
129 if err != nil {
130 t.Fatal(err)
131 }
132 return string(out)
133}
134
135// writeReport writes a report commit holding text as report.json.
136func (r *records) writeReport(text string, parents ...string) string {
137 tree := r.git(fmt.Sprintf("100644 blob %s\treport.json\n", r.blob(text)), "mktree")
138 return r.commit(tree, parents, "a restack report")
139}
140
141func TestWritingARestackReport(t *testing.T) {
142 r := restackers(t)
143 h := r.source("one")
144 good := encodeReport(t, report(t, h))
145 expect := func(what, who, old, new, says string) {
146 t.Helper()
147 r.expect(what, who, reportRef, old, new, says)
148 }
149
150 // About a change the repository records.
151 expect("a report about a change no record names", "restacker", zeroID, r.writeReport(good),
152 "holds no record of the change "+changeID)
153 r.git("", "update-ref", recordRef, r.write(encode(t, first("alice", h)), h))
154
155 first := r.writeReport(good)
156 expect("the first report", "restacker", zeroID, first, "")
157 r.git("", "update-ref", reportRef, first)
158 second := r.writeReport(good, first)
159 expect("a report extending it", "restacker", first, second, "")
160
161 // Who.
162 for _, who := range []string{"alice", "operator", ""} {
163 expect("a report by "+who, who, first, second, "refs/the-valley/restack/ takes writes only from a principal holding the restack grant")
164 }
165
166 // History.
167 expect("a delete", "restacker", first, zeroID, "no push deletes one")
168 expect("a first report with a parent", "restacker", zeroID, second, "has no parent, and this one has 1")
169 expect("a report that starts over", "restacker", first, r.writeReport(good), "as its one parent")
170 expect("a report on another parent", "restacker", first, r.writeReport(good, h), "as its one parent")
171 expect("a report with two parents", "restacker", first, r.writeReport(good, first, h), "as its one parent")
172
173 // The ref.
174 other := change.RestackRef("release", changeID)
175 r.expect("another target", "restacker", other, zeroID, r.writeReport(good), "refs/the-valley/restack/main/<change ID>")
176 r.expect("no change ID", "restacker", change.RestackPrefix+"main/topic", zeroID, r.writeReport(good), "32 letters from k to z")
177 r.expect("a ref that only begins with the namespace", "restacker", "refs/the-valley/restacked/main/"+changeID, zeroID,
178 r.writeReport(good), "written only by the machinery")
179
180 // The report.
181 rep := report(t, h)
182 rep.ChangeID, rep.Layers[0].ChangeID, *rep.Failing = strings.Repeat("k", 32), strings.Repeat("k", 32), strings.Repeat("k", 32)
183 expect("a report about another change", "restacker", first, r.writeReport(encodeReport(t, rep), first),
184 "about the change kkkk")
185 rep = report(t, h)
186 rep.Target = "release"
187 expect("a report naming another target", "restacker", first, r.writeReport(encodeReport(t, rep), first),
188 "names the target release")
189 expect("an unknown field", "restacker", first,
190 r.writeReport(strings.Replace(good, `"result"`, `"extra": 1, "result"`, 1), first), `"extra"`)
191 expect("a report that is no JSON", "restacker", first, r.writeReport("conflict", first), "does not read")
192
193 // The commit's shape.
194 blob := r.blob(good)
195 shaped := func(what, entries, says string) {
196 t.Helper()
197 expect(what, "restacker", first, r.commit(r.git(entries, "mktree"), []string{first}, "a restack report"), says)
198 }
199 shaped("a second file", fmt.Sprintf("100644 blob %s\treport.json\n100644 blob %s\tz\n", blob, blob),
200 "a restack report's tree holds report.json alone")
201 shaped("another name", fmt.Sprintf("100644 blob %s\trecord.json\n", blob), `"record.json"`)
202 shaped("an executable", fmt.Sprintf("100755 blob %s\treport.json\n", blob), "mode 100755")
203
204 // Bounds, each refused on the object's type and size alone.
205 expect("an oversized report.json", "restacker", first, r.writeReport(good+strings.Repeat(" ", change.MaxReportBlob), first),
206 "more than the 65536")
207 big := r.commit(r.git(fmt.Sprintf("100644 blob %s\treport.json\n", blob), "mktree"), []string{first},
208 strings.Repeat("x", change.MaxReportCommit))
209 expect("an oversized commit", "restacker", first, big, "more than the 4096")
210 objects, err := openObjectsWithin(budget{subject: "a restack report", depth: 1, entriesInTree: 1, entries: 1,
211 treeSize: change.MaxReportTree, fileSize: change.MaxReportBlob, bytes: 1 << 20})
212 if err != nil {
213 t.Fatal(err)
214 }
215 _, err = readFileCommit(objects, big, reportLayout)
216 objects.close()
217 if err == nil || strings.Join(objects.requests, "; ") != "info "+big {
218 t.Errorf("an oversized commit: requests = %v, error = %v; want its info alone", objects.requests, err)
219 }
220}
221
222// A push may update at most maxReportUpdates reports, and one that names
223// more is refused before any is read.
224func TestAPushIsBoundInTheReportsItWrites(t *testing.T) {
225 var lines []string
226 for i := 0; i <= maxReportUpdates; i++ {
227 id := strings.Repeat("k", 30) + string(rune('k'+i/16)) + string(rune('k'+i%16))
228 lines = append(lines, zeroID+" "+strings.Repeat("1", 40)+" "+change.RestackRef("main", id))
229 }
230 code, says := pushTo(t, unreadable{t}, "restacker", lines)
231 if code == 0 || !strings.Contains(says, "it updates 65 restack reports, and a push may update at most 64") {
232 t.Errorf("a push of %d reports: exit %d, %q", len(lines), code, says)
233 }
234}