gunk-dev/the-valley

report_test.go

sourcef14c339919dfmain's published tip history
13.1 KiB
1package main
2
3import (
4 "bytes"
5 "context"
6 "encoding/base64"
7 "encoding/json"
8 "fmt"
9 "os"
10 "path/filepath"
11 "reflect"
12 "strings"
13 "testing"
14 "time"
15
16 "the-valley/change"
17 "the-valley/rebase"
18)
19
20// id is the nth of a run of change IDs, 32 letters from k to z.
21func id(n int) string {
22 var b strings.Builder
23 for i := 0; i < 32; i++ {
24 b.WriteByte(byte('k' + (n>>(i%8*2))%16))
25 }
26 return b.String()
27}
28
29// oversized is an observation of a conflicted family that holds more than
30// a report may in every way: 70 recorded changes, 150 conflict paths with
31// unusual names among them, 70 blocking requests, one of whose refs is
32// too long and one of whose names is no branch name, and a refusal longer
33// than a report holds, with control characters in it.
34func oversized() Observation {
35 o := Observation{Action: restack, Result: change.RestackConflict, Conflicts: []string{}, Requests: []RequestPlan{}}
36 for i := 0; i < 70; i++ {
37 o.Layers = append(o.Layers, ObservedLayer{
38 Name: id(i)[:6], ID: id(i), Branch: fmt.Sprintf("stack/%02d", i), BranchTip: strings.Repeat("1", 40),
39 Record: strings.Repeat("2", 40), Version: 1, Head: strings.Repeat("1", 40), State: "ready",
40 })
41 }
42 o.Failing = id(40)
43 o.Conflicts = append(o.Conflicts, "line\nbreak.txt", "bell\x07.txt", "latin1-\xe9.txt", strings.Repeat("long/", 220)+"x")
44 for i := len(o.Conflicts); i < 150; i++ {
45 o.Conflicts = append(o.Conflicts, fmt.Sprintf("src/file-%03d.go", i))
46 }
47 o.Requests = append(o.Requests,
48 RequestPlan{Name: strings.Repeat("r", 300), Head: strings.Repeat("3", 40)},
49 RequestPlan{Name: "no..branch", Head: strings.Repeat("3", 40)})
50 for i := 0; i < 68; i++ {
51 o.Requests = append(o.Requests, RequestPlan{Name: fmt.Sprintf("request-%02d", i), Head: strings.Repeat("3", 40)})
52 }
53 o.Refusal = strings.Repeat("the layer conflicts\nwith main\t", 150)
54 return o
55}
56
57// A report of an observation that holds more than the format allows is a
58// fallback that the format takes: the change's own layer and the failing
59// one, the conflict paths and requests that fit, up to the format's
60// count, and a refusal cut to fit, free of control characters, ending
61// with what it leaves out and the digest of the whole observation.
62func TestAReportFallsBackWithinEveryBound(t *testing.T) {
63 o := oversized()
64 self := id(3)
65 r, omitted, err := bound(fullReport("op-1", strings.Repeat("4", 40), self, o, nil), o.Digest())
66 if err != nil {
67 t.Fatal(err)
68 }
69 data, err := change.EncodeRestackReport(r)
70 if err != nil {
71 t.Fatal(err)
72 }
73 if _, err := change.ParseRestackReport(data); err != nil || len(data) > change.MaxReportBlob {
74 t.Fatalf("the fallback is %d bytes and reads as %v", len(data), err)
75 }
76 if len(r.Layers) != 2 || r.Layers[0].ChangeID != self || r.Layers[1].ChangeID != o.Failing {
77 t.Errorf("the fallback keeps the layers %+v", r.Layers)
78 }
79 if len(r.Conflicts) != change.MaxConflictPaths || r.Conflicts[0] != "src/file-004.go" {
80 t.Errorf("the fallback keeps %d conflict paths, first %q", len(r.Conflicts), r.Conflicts[0])
81 }
82 if len(r.Requests) != 64 || r.Requests[0].Ref != rebase.RequestPrefix+"request-00" {
83 t.Errorf("the fallback keeps %d requests, first %+v", len(r.Requests), r.Requests[0])
84 }
85 if r.Refusal == nil || len(*r.Refusal) > change.MaxRefusal || !strings.Contains(*r.Refusal, "digest "+o.Digest()) ||
86 !strings.HasPrefix(*r.Refusal, "the layer conflicts with main the layer") {
87 t.Errorf("the fallback's refusal is %q", *r.Refusal)
88 }
89 for _, want := range []string{"68 of the family's 70 recorded changes", "50 of its 150 conflict paths", "6 of its 70 blocking requests", "the end of its refusal"} {
90 if !strings.Contains(strings.Join(omitted, "; "), want) {
91 t.Errorf("the fallback does not say it leaves out %s: %v", want, omitted)
92 }
93 }
94}
95
96// A report whose every field fits on its own, and that holds more than 64
97// KiB in all, sheds its conflict paths, then its requests, then the
98// layers it does not need, until it fits.
99func TestAReportTooLargeInAllSheds(t *testing.T) {
100 o := Observation{Action: restack, Result: change.RestackConflict, Requests: []RequestPlan{}}
101 for i := 0; i < 64; i++ {
102 o.Layers = append(o.Layers, ObservedLayer{
103 Name: id(i)[:6], ID: id(i), Branch: fmt.Sprintf("%02d/", i) + strings.Repeat("b", 900), BranchTip: strings.Repeat("1", 40),
104 Record: strings.Repeat("2", 40), Version: 1, Head: strings.Repeat("1", 40), State: "ready",
105 })
106 }
107 o.Failing = id(0)
108 for i := 0; i < 100; i++ {
109 o.Conflicts = append(o.Conflicts, fmt.Sprintf("%03d/", i)+strings.Repeat("c", 1000))
110 }
111 full := fullReport("op-1", strings.Repeat("4", 40), id(9), o, nil)
112 if err := full.Check(); err != nil {
113 t.Fatalf("each field should fit on its own: %v", err)
114 }
115 r, omitted, err := bound(full, o.Digest())
116 if err != nil {
117 t.Fatal(err)
118 }
119 data, err := change.EncodeRestackReport(r)
120 if err != nil || len(data) > change.MaxReportBlob {
121 t.Fatalf("the fallback is %d bytes: %v", len(data), err)
122 }
123 if len(r.Conflicts) != 0 || len(r.Layers) != 2 || !strings.Contains(strings.Join(omitted, "; "), "the 100 conflict paths left") {
124 t.Errorf("the fallback keeps %d paths and %d layers, leaving out %v", len(r.Conflicts), len(r.Layers), omitted)
125 }
126}
127
128// Two reports are the same when their observations and result are,
129// whatever operation wrote each. Two fallbacks that show the same are told
130// apart by the digest of what they leave out.
131func TestReportsAreTheSameByWhatTheySay(t *testing.T) {
132 o := oversized()
133 tip := strings.Repeat("4", 40)
134 a, _, err := bound(fullReport("op-1", tip, id(3), o, nil), o.Digest())
135 if err != nil {
136 t.Fatal(err)
137 }
138 b, _, _ := bound(fullReport("op-2", tip, id(3), o, nil), o.Digest())
139 if !sameReport(a, b) {
140 t.Error("the same observation written by two operations is not the same report")
141 }
142 o.Conflicts[149] = "src/another.go"
143 c, _, _ := bound(fullReport("op-2", tip, id(3), o, nil), o.Digest())
144 if sameReport(a, c) {
145 t.Error("two fallbacks of different observations are the same report")
146 }
147 if strings.Join(a.Conflicts, " ") != strings.Join(c.Conflicts, " ") {
148 t.Fatal("the test's fallbacks should show the same conflict paths")
149 }
150}
151
152// A conflict over more paths than a report holds keeps them all in the
153// journal, and its report, which the format takes, names what fits and
154// says what it leaves out.
155func TestAConflictKeepsEveryPathInTheJournal(t *testing.T) {
156 f := newFixture(t, "")
157 f.git("checkout", "--quiet", "-b", "a", "main")
158 names := []string{"line\nbreak.txt", "bell\x07.txt"}
159 for i := 0; i < 120; i++ {
160 names = append(names, fmt.Sprintf("many/file-%03d.txt", i))
161 }
162 write := func(content string) {
163 for _, n := range names {
164 path := filepath.Join(f.work, n)
165 if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
166 t.Fatal(err)
167 }
168 if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
169 t.Fatal(err)
170 }
171 }
172 f.git("add", "-A")
173 }
174 write("a\n")
175 f.git("commit", "--quiet", "-m", "layer a")
176 f.git("push", "--quiet", "origin", "a")
177 head := f.git("rev-parse", "HEAD")
178 f.writeRecord(idA, &change.Record{
179 Schema: change.Schema, ChangeID: idA, Target: rebase.Target, Owner: "alice", State: change.Ready, Seq: 1, Active: 1,
180 Versions: []change.Version{{N: 1, Head: change.HeadPrefix + head, Title: "A", Branch: "a", Uploader: "alice"}},
181 }, "", head)
182 f.git("checkout", "--quiet", "main")
183 write("main\n")
184 f.git("commit", "--quiet", "-m", "main has its own")
185 f.git("push", "--quiet", "origin", "main")
186
187 r := newRestacker(t, f)
188 j := r.journal()
189 a := of(t, j, idA)
190 if a.Observed.Result != change.RestackConflict || len(a.Observed.Conflicts) != len(names) || a.Branches != nil {
191 t.Fatalf("the journal keeps %d conflict paths, of %d, as %s", len(a.Observed.Conflicts), len(names), a.Observed.Result)
192 }
193 rep := a.Reports[0]
194 if len(rep.Pending.Report.Conflicts) != change.MaxConflictPaths || *rep.Pending.Report.Failing != idA ||
195 !strings.Contains(strings.Join(rep.Omitted, " "), "22 of its 122 conflict paths") {
196 t.Errorf("the report names %d paths and leaves out %v", len(rep.Pending.Report.Conflicts), rep.Omitted)
197 }
198 if !strings.Contains(*rep.Pending.Report.Refusal, a.Observed.Digest()) {
199 t.Errorf("the report's refusal names no digest: %q", *rep.Pending.Report.Refusal)
200 }
201}
202
203// A conflict on a file whose name is not UTF-8 keeps the name in the
204// journal byte for byte, in base64, and the journal reads back as kept,
205// so recovery decides it. The report, which holds only UTF-8, leaves the
206// name out and says so.
207func TestAConflictOnAFileNameNotInUTF8KeepsItByteForByte(t *testing.T) {
208 f := newFixture(t, "")
209 latin1 := "caf\xe9.txt"
210 names := []string{latin1, "plain.txt"}
211 write := func(content string) {
212 for _, n := range names {
213 if err := os.WriteFile(filepath.Join(f.work, n), []byte(content), 0o644); err != nil {
214 t.Fatal(err)
215 }
216 }
217 f.git("add", "-A")
218 }
219 f.git("checkout", "--quiet", "-b", "a", "main")
220 write("a\n")
221 f.git("commit", "--quiet", "-m", "layer a")
222 f.git("push", "--quiet", "origin", "a")
223 head := f.git("rev-parse", "HEAD")
224 f.writeRecord(idA, &change.Record{
225 Schema: change.Schema, ChangeID: idA, Target: rebase.Target, Owner: "alice", State: change.Ready, Seq: 1, Active: 1,
226 Versions: []change.Version{{N: 1, Head: change.HeadPrefix + head, Title: "A", Branch: "a", Uploader: "alice"}},
227 }, "", head)
228 f.git("checkout", "--quiet", "main")
229 write("main\n")
230 f.git("commit", "--quiet", "-m", "main has its own")
231 f.git("push", "--quiet", "origin", "main")
232
233 r := newRestacker(t, f)
234 j := r.journal()
235 a := of(t, j, idA)
236 if a.Observed.Result != change.RestackConflict || !reflect.DeepEqual([]string(a.Observed.Conflicts), names) {
237 t.Fatalf("the journal keeps the conflict paths %q, as %s", a.Observed.Conflicts, a.Observed.Result)
238 }
239 kept, err := loadJournal(r.c.state, r.c.binding())
240 if err != nil {
241 t.Fatalf("the restacker refuses its own journal: %v", err)
242 }
243 if !reflect.DeepEqual(kept, j) || of(t, kept, idA).Observed.Conflicts[0] != latin1 {
244 t.Errorf("the journal reads back with the conflict paths %q", of(t, kept, idA).Observed.Conflicts)
245 }
246 data, err := os.ReadFile(filepath.Join(r.c.state, journalFile))
247 if err != nil {
248 t.Fatal(err)
249 }
250 if !bytes.Contains(data, []byte(base64.StdEncoding.EncodeToString([]byte(latin1)))) {
251 t.Error("the journal does not keep the name in base64")
252 }
253 rep := a.Reports[0]
254 if !reflect.DeepEqual(rep.Pending.Report.Conflicts, []string{"plain.txt"}) ||
255 !strings.Contains(strings.Join(rep.Omitted, " "), "1 of its 2 conflict paths") {
256 t.Errorf("the report names %q and leaves out %v", rep.Pending.Report.Conflicts, rep.Omitted)
257 }
258 r.recovery()
259}
260
261// A report that says what the change's current report on origin says is
262// not built again, under a new operation: a held family whose report has
263// been pushed has nothing left to write.
264func TestAnUnchangedReportIsNotBuiltAgain(t *testing.T) {
265 f := newFixture(t, "")
266 f.readyAs(idE, "alice", "e", "main", "E's title")
267 f.plainBranch("e-plain", "e")
268 f.moveMain()
269 r := newRestacker(t, f)
270 j := r.journal()
271 if w := of(t, j, idE).Reports[0].Pending.Push(context.Background(), r.g); w.Push != rebase.Pushed {
272 t.Fatalf("e's report: %+v", w)
273 }
274
275 p, s := r.plan()
276 out, err := build(context.Background(), r.g, s, p, r.c.binding(), newOperation(time.Now().Add(time.Hour)))
277 if err != nil {
278 t.Fatal(err)
279 }
280 if len(out.journal.Families) != 0 {
281 t.Errorf("an unchanged report was built again: %+v", out.journal.Families)
282 }
283 if w := p.Families[0].Writes; w == nil || len(w.Reports) != 1 || !w.Reports[0].Unchanged {
284 t.Errorf("the plan's writes are %+v", w)
285 }
286}
287
288// One snapshot gives the same reports however often it is planned. A
289// family that several requests hold is planned again and again from the
290// same snapshot, and each time its observation, and so its report, holds
291// those requests and the reasons they give in one order.
292func TestOneSnapshotGivesTheSameReports(t *testing.T) {
293 f := newFixture(t, "")
294 head := f.readyAs(idA, "alice", "a", "main", "A's title")
295 f.moveMain()
296 for _, name := range []string{"q-delta", "q-alpha", "q-echo", "q-charlie", "q-bravo", "q-foxtrot"} {
297 f.onOrigin("update-ref", rebase.RequestPrefix+name, head)
298 }
299 r := newRestacker(t, f)
300 ctx := context.Background()
301 s, err := rebase.ReadSnapshot(ctx, r.g, r.c.verify)
302 if err != nil {
303 t.Fatal(err)
304 }
305 var first []byte
306 for i := 0; i < 20; i++ {
307 p, err := plan(ctx, r.g, r.c, s, 64)
308 if err != nil {
309 t.Fatal(err)
310 }
311 out, err := build(ctx, r.g, s, p, r.c.binding(), "20261021T000000Z-00000000")
312 if err != nil {
313 t.Fatal(err)
314 }
315 a := of(t, out.journal, idA)
316 if len(a.Reports) != 1 || a.Observed.Result != change.RestackBlocked {
317 t.Fatalf("the held family's writes are %+v", a)
318 }
319 report, err := change.EncodeRestackReport(a.Reports[0].Pending.Report)
320 if err != nil {
321 t.Fatal(err)
322 }
323 observed, err := json.Marshal(a.Observed)
324 if err != nil {
325 t.Fatal(err)
326 }
327 now := append(report, observed...)
328 if first == nil {
329 first = now
330 if !strings.Contains(*a.Reports[0].Pending.Report.Refusal, "request q-alpha might still land") {
331 t.Fatalf("the report does not say what holds the family: %q", *a.Reports[0].Pending.Report.Refusal)
332 }
333 continue
334 }
335 if !bytes.Equal(now, first) {
336 t.Fatalf("plan %d of the same snapshot gives another report or observation:\n%s\nwhere the first gave:\n%s", i, now, first)
337 }
338 }
339}