gunk-dev/the-valley

report_test.go

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