forked from gregrahn/sqllogictest
-
Notifications
You must be signed in to change notification settings - Fork 4
/
runner.go
608 lines (516 loc) · 16 KB
/
runner.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
// Copyright 2019-2020 Dolthub, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// 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.
package logictest
import (
"bufio"
"context"
"crypto/md5"
"errors"
"fmt"
"os"
"path/filepath"
"runtime/debug"
"strconv"
"strings"
"sync"
"time"
"github.com/dolthub/sqllogictest/go/logictest/parser"
)
const defaultTimeout = time.Minute * 20
var currTestFile string
var currRecord *parser.Record
var _, TruncateQueriesInLog = os.LookupEnv("SQLLOGICTEST_TRUNCATE_QUERIES")
var startTime time.Time
var testTimeoutError = errors.New("test in file timed out")
// GetCurrentFileName returns path to the test file that is currently executing.
func GetCurrentFileName() string {
return testFilePath(currTestFile)
}
// RunTestFiles runs the test files found under any of the paths given. Can specify individual test files, or directories that
// contain test files somewhere underneath. All files named *.test encountered under a directory will be attempted to be
// parsed as a test file, and will panic for malformed test files or paths that don't exist.
func RunTestFiles(harness Harness, paths ...string) {
testFiles := collectTestFiles(paths)
for _, file := range testFiles {
runTestFile(harness, file)
}
}
// Returns all the test files residing at the paths given.
func collectTestFiles(paths []string) []string {
var testFiles []string
for _, arg := range paths {
abs, err := filepath.Abs(arg)
if err != nil {
panic(err)
}
stat, err := os.Stat(abs)
if err != nil {
panic(err)
}
if stat.IsDir() {
filepath.Walk(arg, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
if strings.HasSuffix(path, ".test") {
testFiles = append(testFiles, path)
}
return nil
})
} else {
testFiles = append(testFiles, abs)
}
}
return testFiles
}
// Generates the test files given by executing the query and replacing expected results with the ones obtained by the
// test run. Files written will have the .generated suffix.
func GenerateTestFiles(harness Harness, paths ...string) {
testFiles := collectTestFiles(paths)
for _, file := range testFiles {
generateTestFile(harness, file, false)
}
}
// GenerateTestFilesWithFailedTestsExcluded generates the specified test files by executing statements and queries,
// filtering out any failed tests, and replacing expected results with the ones from the test run. Files written will
// have the .generated suffix.
func GenerateTestFilesWithFailedTestsExcluded(harness Harness, paths ...string) {
testFiles := collectTestFiles(paths)
for _, file := range testFiles {
generateTestFile(harness, file, true)
}
}
// generateTestFile generates a test file by executing the statements in the specified file, including the query
// results in the generated file, and optionally filtering out any statements that don't execute correctly.
func generateTestFile(harness Harness, f string, filterOutFailedTests bool) {
currTestFile = f
err := harness.Init()
if err != nil {
panic(err)
}
file, err := os.Open(f)
if err != nil {
panic(err)
}
testRecords, err := parser.ParseTestFile(f)
if err != nil {
panic(err)
}
generatedFile, err := os.Create(f + ".generated")
if err != nil {
panic(err)
}
scanner := &parser.LineScanner{
bufio.NewScanner(file), 0,
}
wr := bufio.NewWriter(generatedFile)
defer func() {
err = wr.Flush()
if err != nil {
panic(err)
}
err = generatedFile.Close()
if err != nil {
panic(err)
}
}()
curTimeout := defaultTimeout
if t := harness.GetTimeout(); t != 0 {
curTimeout = time.Second * time.Duration(t)
}
for _, record := range testRecords {
// currRecord is used by logMessagePrefix, so needs to be set as we iterate
currRecord = record
ctx, cancel := context.WithTimeout(context.Background(), curTimeout)
lockCtx := context.WithValue(ctx, "lock", &loggingLock{})
schema, records, _, err := executeRecord(lockCtx, cancel, harness, record)
// If there was an error and we're filtering out failed tests, skip copying
// this record over to the generated test file and continue to the next record.
if err != nil && filterOutFailedTests {
skipUntilEndOfRecord(scanner, wr)
continue
}
// If there was an error or we skipped this test, then just copy output until the next record.
if err != nil || !record.ShouldExecuteForEngine(harness.EngineStr()) {
copyUntilEndOfRecord(scanner, wr) // advance until the next record
continue
} else if record.Type() == parser.Halt {
copyRestOfFile(scanner, wr)
return
}
// Copy until we get to the line before the query we executed (e.g. "query IIRT no-sort")
for scanner.Scan() && scanner.LineNum < record.LineNum()-1 {
line := scanner.Text()
writeLine(wr, line)
}
if record.Type() == parser.Statement {
// Copy statements directly
writeLine(wr, scanner.Text())
copyUntilEndOfRecord(scanner, wr)
} else if record.Type() == parser.Query {
// Fill in the actual query result schema
var label string
if record.Label() != "" {
label = " " + record.Label()
}
writeLine(wr, fmt.Sprintf("query %s %s%s", schema, record.SortString(), label))
copyUntilSeparator(scanner, wr) // copy the original query and separator
writeResults(record, records, wr) // write the query result
skipUntilEndOfRecord(scanner, wr) // advance until the next record
}
}
copyRestOfFile(scanner, wr)
}
func writeLine(wr *bufio.Writer, s string) {
_, err := wr.WriteString(s + "\n")
if err != nil {
panic(err)
}
}
func copyRestOfFile(scanner *parser.LineScanner, wr *bufio.Writer) {
for scanner.Scan() {
writeLine(wr, scanner.Text())
}
}
func writeResults(record *parser.Record, results []string, wr *bufio.Writer) {
results = record.SortResults(results)
if len(results) > record.HashThreshold() {
hash, err := hashResults(results)
if err != nil {
panic(err)
}
writeLine(wr, fmt.Sprintf("%d values hashing to %s", len(results), hash))
} else {
for _, result := range results {
writeLine(wr, fmt.Sprintf("%s", result))
}
}
}
func copyUntilSeparator(scanner *parser.LineScanner, wr *bufio.Writer) {
for scanner.Scan() {
line := scanner.Text()
writeLine(wr, line)
if strings.TrimSpace(line) == parser.Separator {
break
}
}
}
func copyUntilEndOfRecord(scanner *parser.LineScanner, wr *bufio.Writer) {
for scanner.Scan() {
line := scanner.Text()
writeLine(wr, line)
if strings.TrimSpace(line) == "" {
break
}
}
}
func skipUntilEndOfRecord(scanner *parser.LineScanner, wr *bufio.Writer) {
for scanner.Scan() {
line := scanner.Text()
if strings.TrimSpace(line) == "" {
writeLine(wr, "")
break
}
}
}
type loggingLock struct {
mux sync.Mutex
logged bool
}
func runTestFile(harness Harness, file string) {
currTestFile = file
err := harness.Init()
if err != nil {
panic(err)
}
testRecords, err := parser.ParseTestFile(file)
if err != nil {
panic(err)
}
curTimeout := defaultTimeout
if t := harness.GetTimeout(); t != 0 {
curTimeout = time.Second * time.Duration(t)
}
dnr := false
for _, record := range testRecords {
currRecord = record
startTime = time.Now()
ctx, cancel := context.WithTimeout(context.Background(), curTimeout)
lockCtx := context.WithValue(ctx, "lock", &loggingLock{})
if dnr {
logResult(lockCtx, DidNotRun, "")
cancel()
continue
}
_, _, cont, err := executeRecord(lockCtx, cancel, harness, record)
if err == testTimeoutError {
dnr = true
}
if !cont {
break
}
}
}
type R struct {
schema string
results []string
cont bool
err error
}
// Executes a single record and returns whether execution of records should continue
func executeRecord(ctx context.Context, cancel context.CancelFunc, harness Harness, record *parser.Record) (schema string, results []string, cont bool, err error) {
defer cancel()
rc := make(chan *R, 1)
go func() {
schema, results, cont, err := execute(ctx, harness, record)
rc <- &R{
schema: schema,
results: results,
cont: cont,
err: err,
}
}()
select {
case res := <-rc:
return res.schema, res.results, res.cont, res.err
case <-ctx.Done():
logResult(ctx, Timeout, "")
return "", []string{}, true, testTimeoutError
}
}
func execute(ctx context.Context, harness Harness, record *parser.Record) (schema string, results []string, cont bool, err error) {
defer func() {
if r := recover(); r != nil {
toLog := r
if str, ok := r.(string); ok {
// attempt to keep entries on one line
toLog = strings.ReplaceAll(str, "\n", " ")
} else if err, ok := r.(error); ok {
// attempt to keep entries on one line
toLog = strings.ReplaceAll(err.Error(), "\n", " ")
}
logResult(ctx, NotOk, "Caught panic: %v", toLog)
fmt.Println("stacktrace from panic: \n" + string(debug.Stack()))
cont = true
}
}()
if !record.ShouldExecuteForEngine(harness.EngineStr()) {
// Log a skip for queries and statements only, not other control records
if record.Type() == parser.Query || record.Type() == parser.Statement {
logResult(ctx, Skipped, "")
}
return "", nil, true, nil
}
switch record.Type() {
case parser.Statement:
err := harness.ExecuteStatementContext(ctx, record.Query())
if record.ExpectError() {
if err == nil {
logResult(ctx, NotOk, "Expected error but didn't get one")
return "", nil, true, nil
}
} else if err != nil {
logResult(ctx, NotOk, "Unexpected error %v", err)
return "", nil, true, err
}
logResult(ctx, Ok, "")
return "", nil, true, nil
case parser.Query:
schemaStr, results, err := harness.ExecuteQueryContext(ctx, record.Query())
if err != nil {
logResult(ctx, NotOk, "Unexpected error %v", err)
return "", nil, true, err
}
// Only log one error per record, so if schema comparison fails don't bother with result comparison
if verifySchema(ctx, record, schemaStr) {
verifyResults(ctx, record, schemaStr, results)
}
return schemaStr, results, true, nil
case parser.Halt:
return "", nil, false, nil
default:
panic(fmt.Sprintf("Uncrecognized record type %v", record.Type()))
}
}
func verifyResults(ctx context.Context, record *parser.Record, schema string, results []string) {
if len(results) != record.NumResults() {
logResult(ctx, NotOk, fmt.Sprintf("Incorrect number of results. Expected %v, got %v", record.NumResults(), len(results)))
return
}
results = normalizeResults(results, record.Schema())
results = record.SortResults(results)
if record.IsHashResult() {
verifyHash(ctx, record, results)
} else {
verifyRows(ctx, record, results)
}
}
// Normalizes the results according to the schema given.
// Test files have type rules that conform to MySQL's actual behavior, which is pretty odd in some cases. For example,
// the type of the expression `- - - 8` is decimal (float) as of MySQL 8.0. Rather than expect all databases to
// duplicate these semantics, we allow integer types to be freely converted to floats. This means we need to format
// integer results as float results, with three trailing zeros, where necessary.
func normalizeResults(results []string, schema string) []string {
newResults := make([]string, len(results))
for i := range results {
typ := schema[i%len(schema)]
if typ == 'R' && !strings.Contains(results[i], ".") {
_, err := strconv.Atoi(results[i])
if err == nil {
newResults[i] = results[i] + ".000"
continue
}
}
newResults[i] = results[i]
}
return newResults
}
// Verifies that the rows given exactly match the expected rows of the record, in the order given. Rows must have been
// previously sorted according to the semantics of the record.
func verifyRows(ctx context.Context, record *parser.Record, results []string) {
for i := range record.Result() {
if record.Result()[i] != results[i] {
logResult(ctx, NotOk, "Incorrect result at position %d. Expected %v, got %v", i, record.Result()[i], results[i])
return
}
}
logResult(ctx, Ok, "")
}
// Verifies that the hash of the rows given exactly match the expected hash of the record given. Rows must have been
// previously sorted according to the semantics of the record.
func verifyHash(ctx context.Context, record *parser.Record, results []string) {
results = record.SortResults(results)
computedHash, err := hashResults(results)
if err != nil {
logResult(ctx, NotOk, "Error hashing results: %v", err)
return
}
if record.HashResult() != computedHash {
logResult(ctx, NotOk, "Hash of results differ. Expected %v, got %v", record.HashResult(), computedHash)
} else {
logResult(ctx, Ok, "")
}
}
// Computes the md5 hash of the results given, using the same algorithm as the original sqllogictest C code.
func hashResults(results []string) (string, error) {
h := md5.New()
for _, r := range results {
if _, err := h.Write(append([]byte(r), byte('\n'))); err != nil {
return "", err
}
}
return fmt.Sprintf("%x", h.Sum(nil)), nil
}
// Returns whether the schema given matches the record's expected schema, and logging an error if not.
func verifySchema(ctx context.Context, record *parser.Record, schemaStr string) bool {
if schemaStr == record.Schema() {
return true
}
if len(schemaStr) != len(record.Schema()) {
logResult(ctx, NotOk, "Schemas differ. Expected %s, got %s", record.Schema(), schemaStr)
return false
}
// MySQL has odd rules for when a result is a float v. an integer. Rather than try to replicate MySQL's type logic
// exactly, we allow integer results in place of floats. See normalizeResults for details.
for i, c := range record.Schema() {
if !compatibleSchemaTypes(c, rune(schemaStr[i])) {
logResult(ctx, NotOk, "Schemas differ. Expected %s, got %s", record.Schema(), schemaStr)
return false
}
}
return true
}
func compatibleSchemaTypes(expected, actual rune) bool {
if expected != actual {
if expected == 'R' && actual == 'I' {
return true
} else {
return false
}
}
return true
}
func logResult(ctx context.Context, rt ResultType, message string, args ...interface{}) {
lock := ctx.Value("lock").(*loggingLock)
if lock == nil {
panic("Unable to acquire lock from context")
}
lock.mux.Lock()
defer lock.mux.Unlock()
if lock.logged {
return
}
switch rt {
case Ok:
logSuccess()
case NotOk:
logFailure(message, args...)
case Skipped:
logSkip()
case Timeout:
logTimeout()
case DidNotRun:
logDidNotRun()
}
lock.logged = true
}
func logFailure(message string, args ...interface{}) {
newMsg := logMessagePrefix() + " not ok: " + message
failureMessage := fmt.Sprintf(newMsg, args...)
failureMessage = strings.ReplaceAll(failureMessage, "\n", " ")
fmt.Println(failureMessage)
}
func logSkip() {
fmt.Println(logMessagePrefix(), "skipped")
}
func logSuccess() {
fmt.Println(logMessagePrefix(), "ok")
}
func logTimeout() {
fmt.Println(logMessagePrefix(), "timeout")
}
func logDidNotRun() {
fmt.Println(logMessagePrefix(), "did not run")
}
func logMessagePrefix() string {
return fmt.Sprintf("%s %d %s:%d: %s",
time.Now().Format(time.RFC3339Nano),
time.Now().Sub(startTime).Milliseconds(),
testFilePath(currTestFile),
currRecord.LineNum(),
truncateQuery(currRecord.Query()))
}
func testFilePath(f string) string {
var pathElements []string
filename := f
for len(pathElements) < 4 && len(filename) > 0 {
dir, file := filepath.Split(filename)
// Stop recursing at the leading "test/" directory (root directory for the sqllogictest files)
if file == "test" {
break
}
pathElements = append([]string{file}, pathElements...)
filename = filepath.Clean(dir)
}
return strings.ReplaceAll(filepath.Join(pathElements...), "\\", "/")
}
func truncateQuery(query string) string {
if TruncateQueriesInLog && len(query) > 50 {
return query[:47] + "..."
}
return query
}