forked from brimdata/super
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zed_test.go
240 lines (227 loc) · 5.17 KB
/
zed_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
package zed_test
import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"testing"
"github.com/brimdata/zed"
"github.com/brimdata/zed/zio/anyio"
"github.com/brimdata/zed/zio/parquetio"
"github.com/brimdata/zed/ztest"
"github.com/stretchr/testify/require"
)
func TestZed(t *testing.T) {
t.Parallel()
dirs, err := findZTests()
require.NoError(t, err)
for d := range dirs {
d := d
t.Run(d, func(t *testing.T) {
t.Parallel()
ztest.Run(t, d)
})
}
t.Run("ParquetBoomerang", func(t *testing.T) {
runParquetBoomerangs(t, dirs)
})
t.Run("ZSONBoomerang", func(t *testing.T) {
runZSONBoomerangs(t, dirs)
})
}
func findZTests() (map[string]struct{}, error) {
dirs := map[string]struct{}{}
pattern := fmt.Sprintf(`.*ztests\%c.*\.yaml$`, filepath.Separator)
re := regexp.MustCompile(pattern)
err := filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
if !info.IsDir() && strings.HasSuffix(path, ".yaml") && re.MatchString(path) {
dirs[filepath.Dir(path)] = struct{}{}
}
return err
})
return dirs, err
}
func runZSONBoomerangs(t *testing.T, dirs map[string]struct{}) {
if testing.Short() {
return
}
const script = `
exec 2>&1
zq -f zson - > baseline.zson &&
zq -i zson -f zson baseline.zson > boomerang.zson &&
diff baseline.zson boomerang.zson
`
bundles, err := findInputs(t, dirs, script, isValidForZSON)
if err != nil {
t.Fatal(err)
}
shellPath := ztest.ShellPath()
for _, b := range bundles {
b := b
t.Run(b.TestName, func(t *testing.T) {
t.Parallel()
err := b.RunScript(shellPath, t.TempDir())
if err != nil {
err = &BoomerangError{
*b.Test.Inputs[0].Data,
b.FileName,
err,
}
}
require.NoError(t, err)
})
}
}
type BoomerangError struct {
ZSON string
FileName string
Err error
}
func (b *BoomerangError) Error() string {
return fmt.Sprintf("%s\n=== with this input ===\n\n%s\n\n=== from file ===\n\n%s\n\n", b.Err, b.ZSON, b.FileName)
}
func boomerang(script, input string) *ztest.ZTest {
var empty string
return &ztest.ZTest{
Script: script,
Inputs: []ztest.File{
{
Name: "stdin",
Data: &input,
},
},
Outputs: []ztest.File{
{
Name: "stdout",
Data: &empty,
},
{
Name: "stderr",
Data: &empty,
},
},
}
}
func expectFailure(b ztest.Bundle) bool {
if b.Test.ErrorRE != "" {
return true
}
for _, f := range b.Test.Outputs {
if f.Name == "stderr" {
return true
}
}
return false
}
func isValidForZSON(input string) bool {
zrc, err := anyio.NewReader(strings.NewReader(input), zed.NewContext())
if err != nil {
return false
}
defer zrc.Close()
for {
rec, err := zrc.Read()
if err != nil {
return false
}
if rec == nil {
return true
}
}
}
func runParquetBoomerangs(t *testing.T, dirs map[string]struct{}) {
if testing.Short() {
return
}
const script = `
exec 2>&1
zq -f parquet -o baseline.parquet fuse - &&
zq -i parquet -f parquet -o boomerang.parquet baseline.parquet &&
diff baseline.parquet boomerang.parquet
`
bundles, err := findInputs(t, dirs, script, isValidForParquet)
if err != nil {
t.Fatal(err)
}
shellPath := ztest.ShellPath()
for _, b := range bundles {
b := b
t.Run(b.TestName, func(t *testing.T) {
t.Parallel()
err := b.RunScript(shellPath, t.TempDir())
if err != nil {
if s := err.Error(); strings.Contains(s, parquetio.ErrEmptyRecordType.Error()) ||
strings.Contains(s, parquetio.ErrNullType.Error()) ||
strings.Contains(s, parquetio.ErrUnionType.Error()) ||
strings.Contains(s, "column has no name") {
t.Skip("skipping because the Parquet writer cannot handle an input type")
}
err = &BoomerangError{
*b.Test.Inputs[0].Data,
b.FileName,
err,
}
}
require.NoError(t, err)
})
}
}
func isValidForParquet(input string) bool {
zrc, err := anyio.NewReader(strings.NewReader(input), zed.NewContext())
if err != nil {
return false
}
defer zrc.Close()
var found bool
for {
rec, err := zrc.Read()
if err != nil {
return false
}
if rec == nil {
return found
}
if !zed.IsRecordType(rec.Type) {
// zio/parquetio requires records at top level.
return false
}
found = true
}
}
func findInputs(t *testing.T, dirs map[string]struct{}, script string, isValidInput func(string) bool) ([]ztest.Bundle, error) {
var out []ztest.Bundle
for path := range dirs {
bundles, err := ztest.Load(path)
if err != nil {
t.Log(err)
continue
}
// Transform the bundles into boomerang tests by taking each
// source and creating a new ztest.Bundle.
for _, bundle := range bundles {
if bundle.Error != nil || expectFailure(bundle) {
continue
}
// Normalize the diffrent kinds of test inputs into
// a single pattern.
if input := bundle.Test.Input; isValidInput(input) {
out = append(out, ztest.Bundle{
TestName: bundle.TestName,
FileName: bundle.FileName,
Test: boomerang(script, input),
})
}
for _, input := range bundle.Test.Inputs {
if input.Data != nil && isValidInput(*input.Data) {
out = append(out, ztest.Bundle{
TestName: bundle.TestName,
FileName: bundle.FileName,
Test: boomerang(script, *input.Data),
})
}
}
}
}
return out, nil
}