-
Notifications
You must be signed in to change notification settings - Fork 20
/
integration_test.go
321 lines (269 loc) · 6.61 KB
/
integration_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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
Copyright 2018 Iguazio Systems Ltd.
Licensed under the Apache License, Version 2.0 (the "License") with
an addition restriction as set forth herein. You may not use this
file except in compliance with the License. You may obtain a copy of
the License at http://www.apache.org/licenses/LICENSE-2.0.
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied. See the License for the specific language governing
permissions and limitations under the License.
In addition, you may not use the software for any purposes that are
illegal under applicable law, and the grant of the foregoing license
under the Apache 2.0 license is conditioned upon your compliance with
such restriction.
*/
package frames_test
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"math/rand"
"net"
"os"
"os/exec"
"testing"
"time"
"github.com/ghodss/yaml"
"github.com/v3io/frames"
"github.com/v3io/frames/pb"
)
const (
configFile = "config.yaml"
frameSize = 293
)
var (
random *rand.Rand
testsConfig map[string]*testConfig
)
type frameFn func(t testing.TB, size int) frames.Frame
type testConfig struct {
frameFn frameFn
create func(*pb.CreateRequest) // if not defined, create won't be called
write func(*frames.WriteRequest)
read func(*pb.ReadRequest)
del func(*pb.DeleteRequest) // can't use "delete", it's a keyword
exec func(*pb.ExecRequest) // if not defined, exec won't be called
}
func setupRoot(t testing.TB) string {
root, err := ioutil.TempDir("", "frames-e2e")
if err != nil {
t.Fatal(err)
}
csvFile := "weather.csv"
src := fmt.Sprintf("./testdata/%s", csvFile)
dest := fmt.Sprintf("%s/%s", root, csvFile)
in, err := os.Open(src)
if err != nil {
t.Fatal(err)
}
out, err := os.Create(dest)
if err != nil {
t.Fatal(err)
}
if _, err := io.Copy(out, in); err != nil {
t.Fatal(err)
}
return root
}
func sessionInfo(t testing.TB) *frames.Session {
data := os.Getenv("V3IO_SESSION")
if data == "" {
return nil
}
var s struct {
Address string
frames.Session
}
if err := json.Unmarshal([]byte(data), &s); err != nil {
t.Fatal(err)
}
if s.Address != "" {
s.Session.Url = s.Address
}
return &s.Session
}
func genConfig(root string, session *frames.Session) *frames.Config {
backends := []*frames.BackendConfig{
{
Type: "csv",
RootDir: root,
},
}
if session != nil {
backends = append(backends, &frames.BackendConfig{
Type: "kv",
})
backends = append(backends, &frames.BackendConfig{
Type: "stream",
})
backends = append(backends, &frames.BackendConfig{
Type: "tsdb",
Workers: 16,
})
}
return &frames.Config{
Log: frames.LogConfig{
Level: "debug",
},
Backends: backends,
}
}
func encodeConfig(t testing.TB, config *frames.Config, path string) {
data, err := yaml.Marshal(config)
if err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(path, data, 0600); err != nil {
t.Fatal(err)
}
}
func freePort(t testing.TB) int {
l, err := net.Listen("tcp", ":0")
if err != nil {
t.Fatal(err)
}
l.Close()
return l.Addr().(*net.TCPAddr).Port
}
func waitForServer(t testing.TB, port int) {
address := fmt.Sprintf("localhost:%d", port)
timeout := 30 * time.Second
for start := time.Now(); time.Now().Sub(start) < timeout; {
conn, err := net.Dial("tcp", address)
if err == nil {
conn.Close()
return
}
time.Sleep(100 * time.Millisecond)
}
t.Fatalf("server not up after %v", timeout)
}
func runServer(t testing.TB, root string, grpcPort int, httpPort int) *exec.Cmd {
exePath := fmt.Sprintf("%s/framesd", root)
cmd := exec.Command(
"go", "build",
"-o", exePath,
"cmd/framesd/framesd.go",
)
if err := cmd.Run(); err != nil {
t.Fatal(err)
}
logFile, err := os.Create(fmt.Sprintf("%s/framesd.log", root))
if err != nil {
t.Fatal(err)
}
cmd = exec.Command(
exePath,
"-grpcAddr", fmt.Sprintf(":%d", grpcPort),
"-httpAddr", fmt.Sprintf(":%d", httpPort),
"-config", fmt.Sprintf("%s/%s", root, configFile),
)
cmd.Stderr = logFile
cmd.Stdout = logFile
if err := cmd.Start(); err != nil {
t.Fatal(err)
}
waitForServer(t, httpPort)
return cmd
}
func floatCol(t testing.TB, name string, size int) frames.Column {
floats := make([]float64, size)
for i := range floats {
floats[i] = random.Float64()
}
col, err := frames.NewSliceColumn(name, floats)
if err != nil {
t.Fatal(err)
}
return col
}
func stringCol(t testing.TB, name string, size int) frames.Column {
strings := make([]string, size)
for i := range strings {
strings[i] = fmt.Sprintf("val-%d", i)
}
col, err := frames.NewSliceColumn(name, strings)
if err != nil {
t.Fatal(err)
}
return col
}
func csvFrame(t testing.TB, size int) frames.Frame {
var (
columns []frames.Column
col frames.Column
err error
)
bools := make([]bool, size)
for i := range bools {
if random.Float64() < 0.5 {
bools[i] = true
}
}
col, err = frames.NewSliceColumn("bools", bools)
if err != nil {
t.Fatal(err)
}
columns = append(columns, col)
col = floatCol(t, "floats", size)
columns = append(columns, col)
ints := make([]int64, size)
for i := range ints {
ints[i] = random.Int63()
}
col, err = frames.NewSliceColumn("ints", ints)
if err != nil {
t.Fatal(err)
}
columns = append(columns, col)
col = stringCol(t, "strings", size)
columns = append(columns, col)
times := make([]time.Time, size)
for i := range times {
times[i] = time.Now().Add(time.Duration(i) * time.Second)
}
col, err = frames.NewSliceColumn("times", times)
if err != nil {
t.Fatal(err)
}
columns = append(columns, col)
frame, err := frames.NewFrame(columns, nil, nil)
if err != nil {
t.Fatal(err)
}
return frame
}
type testInfo struct {
config *frames.Config
grpcAddr string
httpAddr string
process *os.Process
root string
session *frames.Session
}
func (t *testInfo) killProcess() {
_ = t.process.Kill()
}
func setupTest(t testing.TB) *testInfo {
info := &testInfo{}
info.root = setupRoot(t)
t.Logf("root: %s", info.root)
info.session = sessionInfo(t)
t.Logf("session: %+v", info.session)
info.config = genConfig(info.root, info.session)
t.Logf("config: %+v", info.config)
configPath := fmt.Sprintf("%s/%s", info.root, configFile)
encodeConfig(t, info.config, configPath)
grpcPort, httpPort := freePort(t), freePort(t)
cmd := runServer(t, info.root, grpcPort, httpPort)
info.process = cmd.Process
info.grpcAddr = fmt.Sprintf("localhost:%d", grpcPort)
info.httpAddr = fmt.Sprintf("http://localhost:%d", httpPort)
return info
}
func init() {
random = rand.New(rand.NewSource(time.Now().Unix()))
}