gunk-dev/the-valley

report_test.go

sourcec736503d91ecmain's published tip history
17.4 KiB
1package change
2
3import (
4 "bytes"
5 "encoding/json"
6 "reflect"
7 "strings"
8 "testing"
9)
10
11const (
12 id2 = "kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk"
13 record1 = HeadPrefix + "3333333333333333333333333333333333333333"
14 record2 = HeadPrefix + "4444444444444444444444444444444444444444"
15 tip = HeadPrefix + "5555555555555555555555555555555555555555"
16 request = "refs/the-valley/integration-requests/main/topic"
17)
18
19// report is a well-formed report of a conflict in a stack of two layers,
20// as text, so each case below can change one thing about it.
21func report() string {
22 return `{
23 "schema": "valley-restack-report/v1",
24 "change_id": "` + id1 + `",
25 "target": "main",
26 "target_tip": "` + tip + `",
27 "operation": "20261017T203500Z-1",
28 "result": "conflict",
29 "layers": [
30 {"change_id": "` + id2 + `", "record": "` + record2 + `", "version": 1, "head": "` + head2 + `", "branch": "lower", "branch_tip": "` + head2 + `", "published": null},
31 {"change_id": "` + id1 + `", "record": "` + record1 + `", "version": 2, "head": "` + head1 + `", "branch": "topic", "branch_tip": "` + head1 + `", "published": null}
32 ],
33 "failing_layer": "` + id1 + `",
34 "conflict_paths": ["src/main.go", "docs/a b.md"],
35 "blocking_requests": [],
36 "refusal": null
37}
38`
39}
40
41// published is the same stack, both layers moved and versioned.
42func published() string {
43 r := strings.Replace(report(), `"result": "conflict"`, `"result": "complete"`, 1)
44 r = strings.Replace(r, `"failing_layer": "`+id1+`"`, `"failing_layer": null`, 1)
45 r = strings.Replace(r, `"conflict_paths": ["src/main.go", "docs/a b.md"]`, `"conflict_paths": []`, 1)
46 r = strings.Replace(r, `"branch_tip": "`+head2+`", "published": null`,
47 `"branch_tip": "`+head2+`", "published": {"head": "`+head1+`", "version": 2, "record": "`+record1+`"}`, 1)
48 return strings.Replace(r, `"branch_tip": "`+head1+`", "published": null`,
49 `"branch_tip": "`+head1+`", "published": {"head": "`+head2+`", "version": 3, "record": "`+record2+`"}`, 1)
50}
51
52func TestAWellFormedReportReadsWhole(t *testing.T) {
53 r, err := ParseRestackReport([]byte(report()))
54 if err != nil {
55 t.Fatal(err)
56 }
57 failing, tip1, tip2 := id1, head1, head2
58 want := &RestackReport{
59 Schema: RestackSchema, ChangeID: id1, Target: "main", TargetTip: tip,
60 Operation: "20261017T203500Z-1", Result: RestackConflict,
61 Layers: []RestackLayer{
62 {ChangeID: id2, Record: record2, Version: 1, Head: head2, Branch: "lower", BranchTip: &tip2},
63 {ChangeID: id1, Record: record1, Version: 2, Head: head1, Branch: "topic", BranchTip: &tip1},
64 },
65 Failing: &failing, Conflicts: []string{"src/main.go", "docs/a b.md"}, Requests: []RestackRequest{},
66 }
67 if !reflect.DeepEqual(r, want) {
68 t.Errorf("ParseRestackReport = %+v, want %+v", r, want)
69 }
70 for _, text := range []string{report(), published()} {
71 r, err := ParseRestackReport([]byte(text))
72 if err != nil {
73 t.Fatal(err)
74 }
75 encoded, err := EncodeRestackReport(r)
76 if err != nil {
77 t.Fatal(err)
78 }
79 again, err := ParseRestackReport(encoded)
80 if err != nil || !reflect.DeepEqual(again, r) {
81 t.Errorf("an encoded report read back as %+v, %v", again, err)
82 }
83 }
84 // A report written with no lists at all is written with empty ones.
85 r.Conflicts, r.Requests = nil, nil
86 encoded, err := EncodeRestackReport(r)
87 if err != nil {
88 t.Fatal(err)
89 }
90 if _, err := ParseRestackReport(encoded); err != nil {
91 t.Errorf("a report with nil lists encoded as one that does not read: %v", err)
92 }
93}
94
95// Each case changes one thing about a well-formed report, and each change
96// is one the reading has to refuse.
97func TestTheReportReadingIsStrict(t *testing.T) {
98 r := report()
99 swap := func(from, to string) string {
100 if !strings.Contains(r, from) {
101 t.Fatalf("the report holds no %q to change", from)
102 }
103 return strings.Replace(r, from, to, 1)
104 }
105 for _, c := range []struct {
106 what, text, says string
107 }{
108 // Structure and types, read by the same reader as a record.
109 {"bytes that are not UTF-8", swap("src/main.go", "src/\xff.go"), "not valid UTF-8"},
110 {"a duplicate key", swap(`"result": "conflict",`, `"result": "conflict", "result": "complete",`), `"result" twice`},
111 {"a duplicate key in a layer", swap(`"version": 1,`, `"version": 1, "version": 1,`), `"version" twice`},
112 {"an unknown field", swap(`"refusal": null`, `"refusal": null, "note": "x"`), `"note"`},
113 {"an unknown layer field", swap(`"version": 1,`, `"version": 1, "owner": "x",`), `"owner"`},
114 {"an unknown published field", published2(t, `"version": 2,`, `"version": 2, "at": 1,`), `"at"`},
115 {"a missing field", swap(`"operation": "20261017T203500Z-1",`, ``), `no field "operation"`},
116 {"a missing layer field", swap(`"branch": "lower", `, ``), `no field "branch"`},
117 {"trailing data", r + "{}", "more after the value"},
118 {"a version as a string", swap(`"version": 1`, `"version": "1"`), "where an integer is expected"},
119 {"a refusal as a number", swap(`"refusal": null`, `"refusal": 1`), "a string or null"},
120 {"a failing layer as true", swap(`"failing_layer": "`+id1+`"`, `"failing_layer": true`), "a string or null"},
121 {"paths as an object", swap(`"conflict_paths": ["src/main.go", "docs/a b.md"]`, `"conflict_paths": {}`), "where an array is expected"},
122 {"a path as a number", swap(`"src/main.go"`, `7`), "where a string is expected"},
123 {"requests as null", swap(`"blocking_requests": []`, `"blocking_requests": null`), "where an array is expected"},
124 {"a request as a string", swap(`"blocking_requests": []`, `"blocking_requests": ["`+request+`"]`), "blocking_requests[0] is a string, where an object is expected"},
125 {"a request with no tip", swap(`"blocking_requests": []`, `"blocking_requests": [{"ref": "`+request+`"}]`), `no field "tip"`},
126 {"an unknown request field", swap(`"blocking_requests": []`, `"blocking_requests": [{"ref": "`+request+`", "tip": "`+head1+`", "at": 1}]`), `"at"`},
127 {"a request's tip as null", swap(`"blocking_requests": []`, `"blocking_requests": [{"ref": "`+request+`", "tip": null}]`), "where a string is expected"},
128 {"a branch tip as a number", swap(`"branch_tip": "`+head2+`"`, `"branch_tip": 1`), "a string or null"},
129 {"a missing branch tip", swap(`"branch_tip": "`+head2+`", `, ``), `no field "branch_tip"`},
130 {"layers as null", withLayers(t, r, `null`), "where an array is expected"},
131 {"published as a string", swap(`"published": null`, `"published": "x"`), "where an object is expected"},
132
133 // Formats.
134 {"another schema", swap(`restack-report/v1`, `restack-report/v2`), "its schema"},
135 {"a change ID outside k to z", swap(`"change_id": "`+id1+`",
136 "target"`, `"change_id": "a`+id1[1:]+`",
137 "target"`), "not a change ID"},
138 {"a target that is no branch", swap(`"target": "main"`, `"target": "ma in"`), "its target"},
139 {"a tip that is no head", swap(tip, strings.ToUpper(tip)), "its target_tip"},
140 {"an empty operation", swap(`"20261017T203500Z-1"`, `""`), "its operation"},
141 {"an operation with a space", swap(`"20261017T203500Z-1"`, `"one two"`), "its operation"},
142 {"an operation past its bound", swap(`"20261017T203500Z-1"`, `"`+strings.Repeat("o", MaxOperation+1)+`"`), "its operation"},
143 {"another result", swap(`"result": "conflict"`, `"result": "merged"`), "its result"},
144 {"a layer's record that is no head", swap(record2, "git-sha1:xyz"), "layers[0].record"},
145 {"a layer's version of zero", swap(`"version": 1`, `"version": 0`), "outside 1 to 1000"},
146 {"a layer's branch git refuses", swap(`"branch": "lower"`, `"branch": "lo..wer"`), "layers[0].branch"},
147 {"a layer's head that is no head", swap(`"head": "`+head2+`"`, `"head": "`+head2[:20]+`"`), "layers[0].head"},
148 {"a layer's branch tip that is no head", swap(`"branch_tip": "`+head2+`"`, `"branch_tip": "`+head2[:20]+`"`), "layers[0].branch_tip"},
149 {"an empty path", swap(`"src/main.go"`, `""`), "conflict_paths[0]"},
150 {"a path with a newline", swap(`"src/main.go"`, `"src/\nmain.go"`), "conflict_paths[0]"},
151 {"a path past its bound", swap(`"src/main.go"`, `"`+strings.Repeat("p", MaxConflictPath+1)+`"`), "conflict_paths[0]"},
152 {"too many paths", swap(`"src/main.go", `, strings.Repeat(`"p", `, MaxConflictPaths)), "more than the 100"},
153 {"a request that is no request", swap(`"blocking_requests": []`, `"blocking_requests": [{"ref": "refs/heads/main", "tip": "`+head1+`"}]`), "blocking_requests[0].ref"},
154 {"a request on another target", swap(`"blocking_requests": []`, `"blocking_requests": [{"ref": "refs/the-valley/integration-requests/next/x", "tip": "`+head1+`"}]`), "blocking_requests[0].ref"},
155 {"a request past its bound", swap(`"blocking_requests": []`, `"blocking_requests": [{"ref": "refs/the-valley/integration-requests/main/`+strings.Repeat("r", MaxRequestRef)+`", "tip": "`+head1+`"}]`), "blocking_requests[0].ref"},
156 {"a request's tip that is no head", swap(`"blocking_requests": []`, `"blocking_requests": [{"ref": "`+request+`", "tip": "`+head1[:20]+`"}]`), "blocking_requests[0].tip"},
157 {"too many requests", swap(`"blocking_requests": []`, `"blocking_requests": [`+strings.Repeat(`{"ref": "`+request+`", "tip": "`+head1+`"}, `, MaxBlockingRequest)+`{"ref": "`+request+`", "tip": "`+head1+`"}]`), "more than the 64"},
158 {"an empty refusal", swap(`"refusal": null`, `"refusal": ""`), "its refusal"},
159 {"a refusal past its bound", swap(`"refusal": null`, `"refusal": "`+strings.Repeat("r", MaxRefusal+1)+`"`), "its refusal"},
160 {"a refusal with a control character", swap(`"refusal": null`, `"refusal": "a\u0007b"`), "its refusal"},
161 {"too many layers", swap(`"layers": [`, `"layers": [`+strings.Repeat(`{"change_id": "`+id2+`", "record": "`+record2+`", "version": 1, "head": "`+head2+`", "branch": "b", "branch_tip": null, "published": null}, `, MaxRestackLayers)), "more than the 64"},
162
163 // What the fields say about one another.
164 {"no layer", withLayers(t, r, `[]`), "holds no layer"},
165 {"no layer for its own change", swap(`"change_id": "`+id1+`", "record"`, `"change_id": "`+strings.Repeat("l", 32)+`", "record"`), "no layer names the change"},
166 {"a layer twice", swap(`"change_id": "`+id2+`", "record"`, `"change_id": "`+id1+`", "record"`), "which an earlier layer names"},
167 {"a branch tip with no branch", swap(`"branch": "lower"`, `"branch": ""`), "names no branch, so it observed no branch_tip"},
168 {"a layer with no branch that published", published2(t, `"branch": "lower", "branch_tip": "`+head2+`"`, `"branch": "", "branch_tip": null`), "a layer with no branch publishes nothing"},
169 {"a request twice", swap(`"blocking_requests": []`, `"blocking_requests": [{"ref": "`+request+`", "tip": "`+head1+`"}, {"ref": "`+request+`", "tip": "`+head2+`"}]`), "which an earlier entry names"},
170 {"a failing layer that is no layer", swap(`"failing_layer": "`+id1+`"`, `"failing_layer": "`+strings.Repeat("l", 32)+`"`), "none of its layers"},
171 {"a conflict with no failing layer", swap(`"failing_layer": "`+id1+`"`, `"failing_layer": null`), "names its failing_layer"},
172 {"a conflict that published", swap(`"published": null`, `"published": {"head": "`+head1+`", "version": null, "record": null}`), "publishes nothing"},
173 {"a block that published", strings.Replace(swap(`"published": null`, `"published": {"head": "`+head1+`", "version": null, "record": null}`), `"result": "conflict"`, `"result": "blocked"`, 1), "publishes nothing"},
174 {"a complete attempt with a layer unpublished", strings.Replace(published(), `"published": {"head": "`+head1+`", "version": 2, "record": "`+record1+`"}`, `"published": null`, 1), "a version for every layer"},
175 {"a complete attempt with a layer unversioned", strings.Replace(published(), `"version": 2, "record": "`+record1+`"`, `"version": null, "record": null`, 1), "a version for every layer"},
176 {"a version without its record", published2(t, `"record": "`+record1+`"`, `"record": null`), "come together"},
177 {"a record without its version", published2(t, `"version": 2,`, `"version": null,`), "come together"},
178 {"a published head that is no head", published2(t, `"published": {"head": "`+head1+`"`, `"published": {"head": "x"`), "published.head"},
179 {"a published record that is no head", published2(t, `"record": "`+record1+`"`, `"record": "x"`), "published.record"},
180 } {
181 _, err := ParseRestackReport([]byte(c.text))
182 if err == nil || !strings.Contains(err.Error(), c.says) {
183 t.Errorf("%s: error = %v, want one saying %q", c.what, err, c.says)
184 }
185 }
186
187 // A partial attempt may leave a layer's version unadded, and a block
188 // may name the requests that held it and the refusal it met.
189 partial := strings.Replace(published(), `"result": "complete"`, `"result": "partial"`, 1)
190 partial = strings.Replace(partial, `"version": 2, "record": "`+record1+`"`, `"version": null, "record": null`, 1)
191 blocked := strings.Replace(swap(`"result": "conflict"`, `"result": "blocked"`), `"blocking_requests": []`,
192 `"blocking_requests": [{"ref": "refs/the-valley/integration-requests/main/c-`+id1+`", "tip": "`+head1+`"}, {"ref": "refs/the-valley/integration-requests/main/topic/one", "tip": "`+head2+`"}]`, 1)
193 blocked = strings.Replace(blocked, `"refusal": null`, `"refusal": "the branch lower is not at the head its record names"`, 1)
194 for what, text := range map[string]string{"a partial attempt": partial, "a block": blocked} {
195 if _, err := ParseRestackReport([]byte(text)); err != nil {
196 t.Errorf("%s: %v", what, err)
197 }
198 }
199}
200
201// A report says what it saw of each ref, absence included, apart from
202// what the record says. A branch origin did not hold is a null tip, and a
203// version that names no branch has no branch to observe. Both read, and
204// both are written back as they read.
205func TestAReportKeepsWhatItSawOfEachRef(t *testing.T) {
206 text := strings.Replace(report(), `"branch": "lower", "branch_tip": "`+head2+`"`, `"branch": "", "branch_tip": null`, 1)
207 text = strings.Replace(text, `"branch_tip": "`+head1+`"`, `"branch_tip": null`, 1)
208 text = strings.Replace(text, `"blocking_requests": []`, `"blocking_requests": [{"ref": "`+request+`", "tip": "`+head2+`"}]`, 1)
209 r, err := ParseRestackReport([]byte(text))
210 if err != nil {
211 t.Fatal(err)
212 }
213 if l := r.Layers[0]; l.Branch != "" || l.BranchTip != nil || l.Head != head2 {
214 t.Errorf("a layer with no branch read as %+v", l)
215 }
216 if l := r.Layers[1]; l.Branch != "topic" || l.BranchTip != nil || l.Head != head1 {
217 t.Errorf("a layer whose branch was missing read as %+v", l)
218 }
219 if want := []RestackRequest{{Ref: request, Tip: head2}}; !reflect.DeepEqual(r.Requests, want) {
220 t.Errorf("its blocking requests read as %+v, want %+v", r.Requests, want)
221 }
222 encoded, err := EncodeRestackReport(r)
223 if err != nil {
224 t.Fatal(err)
225 }
226 if again, err := ParseRestackReport(encoded); err != nil || !reflect.DeepEqual(again, r) {
227 t.Errorf("the report was written back as %s, which reads as %+v, %v", encoded, again, err)
228 }
229 if !bytes.Contains(encoded, []byte(`"branch": "",`)) || bytes.Count(encoded, []byte(`"branch_tip": null`)) != 2 {
230 t.Errorf("the report was not written with an empty branch and two null tips:\n%s", encoded)
231 }
232}
233
234// withLayers is the report r with its layers replaced by the JSON given.
235func withLayers(t *testing.T, r, layers string) string {
236 t.Helper()
237 start := strings.Index(r, `"layers": `)
238 end := strings.Index(r, `,
239 "failing_layer"`)
240 if start < 0 || end < start {
241 t.Fatal("the report holds no layers to replace")
242 }
243 return r[:start] + `"layers": ` + layers + r[end:]
244}
245
246// published2 is the published report with one change made to it.
247func published2(t *testing.T, from, to string) string {
248 t.Helper()
249 p := published()
250 if !strings.Contains(p, from) {
251 t.Fatalf("the published report holds no %q to change", from)
252 }
253 return strings.Replace(p, from, to, 1)
254}
255
256func TestARestackRefNamesATargetAndAChange(t *testing.T) {
257 if target, id, ok := ParseRestackRef(RestackRef("main", id1)); !ok || target != "main" || id != id1 {
258 t.Errorf("ParseRestackRef(RestackRef(main, %s)) = %s, %s, %v", id1, target, id, ok)
259 }
260 for _, ref := range []string{
261 RestackPrefix + id1,
262 RestackPrefix + "main/" + id1[:31],
263 RestackPrefix + "ma..in/" + id1,
264 Ref("main", id1),
265 "refs/the-valley/restacked/main/" + id1,
266 } {
267 if _, _, ok := ParseRestackRef(ref); ok {
268 t.Errorf("ParseRestackRef(%s) read it as a restack report's ref", ref)
269 }
270 }
271}
272
273// The report parser reads bytes a restacker pushes. Whatever they are, it
274// returns a report or an error and never panics. What it accepts is a
275// report EncodeRestackReport writes back unchanged, and is never something
276// encoding/json reads differently.
277func FuzzParseRestackReport(f *testing.F) {
278 f.Add([]byte(report()))
279 f.Add([]byte(published()))
280 f.Add([]byte(`{}`))
281 f.Add([]byte(strings.Replace(report(), `"refusal": null`, `"refusal": null, "refusal": "x"`, 1)))
282 f.Add([]byte(strings.Replace(report(), `"published": null`, `"published": {"head": "x"}`, 1)))
283 f.Add([]byte(report() + "[]"))
284 f.Add([]byte(strings.Replace(report(), `"branch": "lower", "branch_tip": "`+head2+`"`, `"branch": "", "branch_tip": null`, 1)))
285 f.Add([]byte(strings.Replace(report(), `"blocking_requests": []`, `"blocking_requests": [{"ref": "`+request+`", "tip": "`+head1+`"}]`, 1)))
286 f.Fuzz(func(t *testing.T, data []byte) {
287 r, err := ParseRestackReport(data)
288 if err != nil {
289 return
290 }
291 out, err := EncodeRestackReport(r)
292 if err != nil {
293 t.Fatalf("a report ParseRestackReport accepted cannot be encoded: %v", err)
294 }
295 again, err := ParseRestackReport(out)
296 if err != nil || !reflect.DeepEqual(again, r) {
297 t.Fatalf("an encoded report read back as %+v, %v; want %+v", again, err, r)
298 }
299 var lenient RestackReport
300 dec := json.NewDecoder(bytes.NewReader(data))
301 dec.DisallowUnknownFields()
302 if err := dec.Decode(&lenient); err != nil || !reflect.DeepEqual(&lenient, r) {
303 t.Fatalf("encoding/json read the report as %+v, %v; want %+v", lenient, err, r)
304 }
305 })
306}