-
Notifications
You must be signed in to change notification settings - Fork 12
/
lineage_test.go
276 lines (236 loc) · 6.81 KB
/
lineage_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package thema
import (
"bytes"
"fmt"
"testing"
"cuelang.org/go/cue"
"cuelang.org/go/cue/build"
"cuelang.org/go/cue/cuecontext"
"cuelang.org/go/cue/errors"
"cuelang.org/go/cue/load"
"github.com/grafana/thema/internal/txtartest/vanilla"
)
func TestBindLineage(t *testing.T) {
test := vanilla.TxTarTest{
Root: "./testdata/lineage",
Name: "bind",
ThemaFS: CueJointFS,
ToDo: map[string]string{
"lineage/defaultchange": "Thema compat analyzer fails to classify changes to default values as breaking",
"lineage/optional": "Optional fields do not satisfy struct.MinFields(), causing #Lineage constraints to fail",
},
}
ctx := cuecontext.New()
rt := NewRuntime(ctx)
test.Run(t, func(tc *vanilla.Test) {
lin, err := bindTxtarLineage(tc, rt, "lineagePath")
if testing.Short() && tc.HasTag("slow") {
t.Skip("case is tagged #slow, skipping for -short")
}
if err != nil {
tc.Fatalf("error binding lineage: %+v", err)
}
schemaselem := cue.Str("schemas")
sspath := cue.MakePath(schemaselem)
slen, err := lin.Underlying().LookupPath(sspath).Len().Int64()
if err != nil {
tc.Fatal("error getting schemas len", err)
}
fmt.Fprintf(tc, "Schema count: %v\n", slen)
fmt.Fprintf(tc, "Schema versions: %s\n", lin.allVersions())
var llen int64
if slen > 1 {
lenseselem := cue.Str("lenses")
slpath := cue.MakePath(lenseselem)
llen, err = lin.Underlying().LookupPath(slpath).Len().Int64()
if err != nil {
tc.Fatal("error getting lenses len", err)
}
}
fmt.Fprintf(tc, "Lenses count: %v\n", llen)
})
}
func TestInvalidLineages(t *testing.T) {
test := vanilla.TxTarTest{
Root: "./testdata/invalidlineage",
Name: "bindfail",
ThemaFS: CueJointFS,
ToDo: map[string]string{
"invalidlineage/joindef": "no invariant checker written to disallow definitions from joinSchema",
"invalidlineage/onlydef": "Lineage schema non-emptiness constraints are temporarily suspended while migrating grafana to flattened lineage structure",
},
}
ctx := cuecontext.New()
rt := NewRuntime(ctx)
test.Run(t, func(tc *vanilla.Test) {
_, err := bindTxtarLineage(tc, rt, "lineagePath")
if testing.Short() && tc.HasTag("slow") {
tc.Skip("case is tagged #slow, skipping for -short")
}
if err == nil {
tc.Fatal("expected error from known-invalid lineage")
}
// TODO more verbose error output, should include CUE line-level analysis
tc.WriteErrors(errors.Promote(err, ""))
})
}
func TestIsAppendOnly(t *testing.T) {
test := vanilla.TxTarTest{
Root: "./testdata/isappendonly/valid",
Name: "isappendonly",
ThemaFS: CueJointFS,
ToDo: map[string]string{
"isappendonly/valid/withconstraints": "Subsume doesn't support constraints using built-in validators",
"isappendonly/valid/disjunction": "Subsume requires the Final() option to consider two complex disjunctions as equal but this creates false negatives",
"isappendonly/valid/maps": "Subsume requires the Final() option to consider two maps as equal but this creates false negatives",
},
}
ctx := cuecontext.New()
rt := NewRuntime(ctx)
test.Run(t, func(tc *vanilla.Test) {
if testing.Short() && tc.HasTag("slow") {
t.Skip("case is tagged #slow, skipping for -short")
}
lin1, err := bindTxtarLineage(tc, rt, "firstLin")
if err != nil {
tc.Fatalf("error binding first lineage: %+v", err)
}
lin2, err := bindTxtarLineage(tc, rt, "secondLin")
if err != nil {
tc.Fatalf("error binding second lineage: %+v", err)
}
err = IsAppendOnly(lin1, lin2)
if err != nil {
tc.Fatalf("IsAppendOnly returned an error: %+v", err)
}
})
}
func TestIsAppendOnlyFail(t *testing.T) {
test := vanilla.TxTarTest{
Root: "./testdata/isappendonly/invalid",
Name: "isappendonly-fail",
ThemaFS: CueJointFS,
}
ctx := cuecontext.New()
rt := NewRuntime(ctx)
test.Run(t, func(tc *vanilla.Test) {
if testing.Short() && tc.HasTag("slow") {
t.Skip("case is tagged #slow, skipping for -short")
}
lin1, err := bindTxtarLineage(tc, rt, "firstLin")
if err != nil {
tc.Fatalf("error binding first lineage: %+v", err)
}
lin2, err := bindTxtarLineage(tc, rt, "secondLin")
if err != nil {
tc.Fatalf("error binding second lineage: %+v", err)
}
err = IsAppendOnly(lin1, lin2)
if err == nil {
tc.Fatalf("expected error from known invalid updates")
}
// TODO more verbose error output, should include CUE line-level analysis
tc.WriteErrors(errors.Promote(err, "IsAppendOnly fail"))
})
}
func bindTxtarLineage(t *vanilla.Test, rt *Runtime, path string) (Lineage, error) {
if rt == nil {
rt = NewRuntime(cuecontext.New())
}
ctx := rt.Context()
t.Helper()
inst := t.Instance()
val := ctx.BuildInstance(inst)
if p, ok := t.Value(path); ok {
pp := cue.ParsePath(p)
if len(pp.Selectors()) == 0 {
t.Fatalf("%q is not a valid value for the #%s key", p, path)
}
val = val.LookupPath(pp)
if !val.Exists() {
t.Fatalf("path %q specified in #%s does not exist in input cue instance", p, path)
}
}
return BindLineage(val, rt)
}
var benchBindstr = `
name: "trivial-two"
schemas: [{
version: [0, 0]
schema: {
firstfield: string
}
},
{
version: [0, 1]
schema: {
firstfield: string
secondfield?: int32
}
}]
lenses: [{
from: [0, 1]
to: [0, 0]
input: _
result: {
firstfield: input.firstfield
}
}]
`
func BenchmarkUnifyLineage(b *testing.B) {
bi := getCaseWithImport()
val := cuecontext.New().BuildInstance(getCaseWithImport())
if val.Err() != nil {
b.Fatal(val.Err())
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
cuecontext.New().BuildInstance(bi)
}
}
func getCaseWithImport() *build.Instance {
themaInst := load.Instances(nil, &load.Config{
Package: "thema",
})[0]
buf := new(bytes.Buffer)
buf.WriteString(`import "github.com/grafana/thema"
thema.#Lineage
`)
buf.WriteString(benchBindstr)
bi := load.Instances([]string{"-"}, &load.Config{
Context: themaInst.Context(),
Stdin: buf,
})
return bi[0]
}
// BenchmarkBindLineage benchmarks binding a lineage in Go, with and without
// explicitly unifying the input lineage with thema.#Lineage in the CUE source.
//
// Keeping these separate lets us see the difference between the performance cost
// of just the pure, native CUE logic, vs. the cost of the Go code that wraps it.
func BenchmarkBindLineage(b *testing.B) {
b.Run("PreUnified", func(b *testing.B) {
ctx := cuecontext.New()
rt := NewRuntime(ctx)
linv := ctx.BuildInstance(getCaseWithImport())
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := BindLineage(linv, rt)
if err != nil {
b.Fatal(err)
}
}
})
b.Run("NotUnified", func(b *testing.B) {
ctx := cuecontext.New()
rt := NewRuntime(ctx)
linv := ctx.CompileString(benchBindstr)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := BindLineage(linv, rt)
if err != nil {
b.Fatal(err)
}
}
})
}