-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
320 lines (276 loc) · 8.9 KB
/
main.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
package main
import (
"bytes"
"context"
"os"
"strconv"
"strings"
"time"
v2 "github.com/bblfsh/sdk/v3/protocol"
"github.com/bblfsh/sdk/v3/uast/nodes"
"github.com/bblfsh/sdk/v3/uast/nodes/nodesproto"
"github.com/jessevdk/go-flags"
"github.com/src-d/regression-core"
gitbase "github.com/src-d/regression-gitbase"
mockup "github.com/src-d/regression-gitbase/bblfsh-mockups"
capture_output "github.com/src-d/regression-gitbase/capture-output"
"gopkg.in/src-d/go-errors.v1"
"gopkg.in/src-d/go-log.v1"
)
const (
limit = 10
queryTimeoutTotal = 15 * time.Second
captureOutputDelay = 500 * time.Millisecond
)
var (
testErrParseErrorOnly = []*v2.ParseError{{"only one parse error"}}
testErrParseErrorSeveral = []*v2.ParseError{
{"first parse error"},
{"second parse error"},
{"third parse error"}}
testErrParseErrorNilSlice []*v2.ParseError = nil
uastQuery = `select file_path, uast(blob_content) name
from refs natural
join commit_files natural
join blobs
where LANGUAGE(file_path) = 'Go'
limit ` + strconv.Itoa(limit)
)
func main() {
var testsFailed []string
for _, t := range []struct {
name string
f func() error
}{
{"TestResponseErrorWarnings", TestResponseErrorWarnings},
{"TestBrokenUASTInResponseWarning", TestBrokenUASTInResponseWarning},
{"TestParseErrorWarnings", TestParseErrorWarnings},
{"TestQueryExecBeforeTimeout", TestQueryExecBeforeTimeout},
// TODO(lwsanty): can possibly fail because of https://github.com/src-d/go-mysql-server/pull/801
// TODO this test needs refactor after https://github.com/src-d/gitbase/issues/950
{"TestQueryExecAfterTimeout", TestQueryExecAfterTimeout},
} {
t := t
log.Infof("=====> %v", t.name)
if err := t.f(); err != nil {
log.Infof("error occurred: %v", err)
testsFailed = append(testsFailed, t.name)
}
log.Infof("=====> done")
}
if len(testsFailed) > 0 {
log.Infof("%v tests have failed:\n%+v", len(testsFailed), testsFailed)
os.Exit(1)
}
log.Infof("ALL TESTS PASSED")
}
// TestQueryExecBeforeTimeout
// 1) prepare bblfsh mockup that performs sleep responseLag time during the ParseRequest handling
// and returns specific errText error on parse request
// 2) run gitbase with GITBASE_CONNECTION_TIMEOUT = responseLag + 1 second
// 3) execute query uastQuery
// 4) check that no error was returned during the query execution
// 5) check gitbase stderr: there should be warnings with errText
// 6) the amount of warnings should be equal to limit
func TestQueryExecBeforeTimeout() error {
const (
errText = "parse error"
responseLagSeconds = 1
responseLag = responseLagSeconds * time.Second
connectionTimeoutSeconds = responseLagSeconds + 1
)
return testWarnings(context.Background(), mockup.OptsV2{
ParseResponse: nil,
ParseResponseLag: responseLag,
ParseResponseErr: errors.NewKind(errText).New(),
}, map[string]string{
"GITBASE_CONNECTION_TIMEOUT": strconv.Itoa(connectionTimeoutSeconds),
}, errTextUnableToParse(errTextRPC(errText)), limit)
}
// TestQueryExecAfterTimeout
// 1) prepare bblfsh mockup that performs sleep responseLag time during the ParseRequest
// and returns specific errText error on parse request
// 2) run gitbase with GITBASE_CONNECTION_TIMEOUT = responseLag - 1 second
// 3) execute query uastQuery
// 4) check that warning(or error?) related to timeout appeared?
func TestQueryExecAfterTimeout() error {
const (
errText = "parse error"
errTimeoutOnRowRead = "row read wait bigger than connection timeout"
responseLagSeconds = 2
responseLag = responseLagSeconds * time.Second
connectionTimeoutSeconds = responseLagSeconds - 1
)
var errAssert = errors.NewKind("expected error containing %q, got: %v")
err := testWarnings(context.Background(), mockup.OptsV2{
ParseResponse: nil,
ParseResponseLag: responseLag,
ParseResponseErr: errors.NewKind(errText).New(),
}, map[string]string{
"GITBASE_CONNECTION_TIMEOUT": strconv.Itoa(connectionTimeoutSeconds),
}, "timeout", limit)
if err == nil || !strings.Contains(err.Error(), errTimeoutOnRowRead) {
return errAssert.New(errTimeoutOnRowRead, err)
}
return nil
}
// TestResponseErrorWarnings
// 1) prepare bblfsh mockup that returns specific errText error on parse request
// 2) run gitbase
// 3) execute query uastQuery
// 4) check that no error was returned during the query execution
// 5) check gitbase stderr: there should be warnings with errText
// 6) the amount of warnings should be equal to limit
func TestResponseErrorWarnings() error {
const errText = "parse error"
return testWarnings(context.Background(), mockup.OptsV2{
ParseResponse: nil,
ParseResponseErr: errors.NewKind(errText).New(),
}, nil, errTextUnableToParse(errTextRPC(errText)), limit)
}
// TestBrokenUASTInResponseWarning
// 1) prepare bblfsh mockup that returns broken UAST bytes sequence
// 2) run gitbase
// 3) execute query uastQuery
// 4) check that no error was returned during the query execution
// 5) check gitbase stderr: there should be warnings with "short read"
// 6) the amount of warnings should be equal to limit
func TestBrokenUASTInResponseWarning() error {
return testWarnings(context.Background(), mockup.OptsV2{
ParseResponse: &v2.ParseResponse{Language: "go", Uast: []byte{1}},
}, nil, errTextUnableToParse("short read"), limit)
}
// TestParseErrorWarnings
// contains three subtests with 3 bblfsh mockups: without ParseErrors, with one ParseError and with three ParseErrors
// for each of the mockup:
// 1) run gitbase
// 2) execute query uastQuery
// 3) check that no error was returned during the query execution
// 4) check that related warnings appeared
func TestParseErrorWarnings() error {
bufData, err := getUASTBytes("a")
if err != nil {
return err
}
testWarningsParseErrors := func(parseErrs []*v2.ParseError) error {
var (
text string
repeats = limit
)
if len(parseErrs) == 0 {
text = "level=warning"
repeats = 1
} else {
var tmp []string
for _, pe := range parseErrs {
tmp = append(tmp, pe.Text)
}
text = errTextUnableToParse(errSyntax(strings.Join(tmp, `\n`)))
}
return testWarnings(context.Background(), mockup.OptsV2{
ParseResponse: &v2.ParseResponse{Language: "go", Uast: bufData, Errors: parseErrs},
}, nil, text, repeats)
}
for _, e := range [][]*v2.ParseError{
testErrParseErrorNilSlice,
testErrParseErrorOnly,
testErrParseErrorSeveral,
} {
if err := testWarningsParseErrors(e); err != nil {
return err
}
}
return nil
}
type queryResult struct {
out string
err error
}
func testWarnings(ctx context.Context, o mockup.OptsV2, gitbaseEnvs map[string]string, expWarning string, repeats int) error {
closer, err := mockup.PrepareGRPCServer(mockup.Options{OptsV2: o})
defer closer()
if err != nil {
return err
}
test, err := prepareGitBaseEnvironment()
if err != nil {
return err
}
resc := make(chan queryResult)
go func() {
var tmpErr error
tmpOut := capture_output.Capture(func() {
tmpErr = test.RunQueryCtx(ctx, gitbaseEnvs, gitbase.Query{
Statements: []string{uastQuery},
})
}, captureOutputDelay)
resc <- queryResult{out: tmpOut, err: tmpErr}
}()
var outPut string
select {
case res := <-resc:
if res.err != nil {
return res.err
}
outPut = res.out
case <-time.After(queryTimeoutTotal):
return errors.NewKind("query is being executed longer than expected").New()
}
log.Infof("out: %v", outPut)
actRepeats := strings.Count(outPut, expWarning)
if repeats != actRepeats {
return errors.NewKind("repeats of %q: exp: %v act: %v").New(expWarning, repeats, actRepeats)
}
return nil
}
func prepareGitBaseEnvironment() (*gitbase.Test, error) {
config := regression.NewConfig()
parser := flags.NewParser(&config, flags.Default)
args, err := parser.Parse()
if err != nil {
if err, ok := err.(*flags.Error); ok {
if err.Type == flags.ErrHelp {
os.Exit(0)
}
}
return nil, errors.NewKind("could not parse arguments").Wrap(err)
}
if config.ShowRepos {
repos, err := regression.NewRepositories(config)
if err != nil {
return nil, errors.NewKind("could not get repositories").Wrap(err)
}
repos.ShowRepos()
os.Exit(0)
}
if len(args) == 0 {
return nil, errors.NewKind("at least one version required").New()
}
config.Versions = args
test, err := gitbase.NewTest(config)
if err != nil {
return nil, err
}
log.Infof("Preparing run")
if err := test.Prepare(); err != nil {
return nil, errors.NewKind("could not prepare environment").Wrap(err)
}
return test, nil
}
func getUASTBytes(text string) ([]byte, error) {
node := nodes.String(text)
buf := bytes.NewBuffer(nil)
if err := nodesproto.WriteTo(buf, node); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func errTextUnableToParse(errText string) string {
return "unable to parse the given blob using bblfsh: " + errText
}
func errTextRPC(errText string) string {
return "rpc error: code = Unknown desc = " + errText
}
func errSyntax(errText string) string {
return "syntax error: " + errText
}