-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
server.go
841 lines (709 loc) · 24.6 KB
/
server.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
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
/*
Copyright 2019 The Vitess Authors.
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 fakesqldb provides a MySQL server for tests.
package fakesqldb
import (
"errors"
"fmt"
"os"
"path"
"regexp"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
"vitess.io/vitess/go/mysql/replication"
"vitess.io/vitess/go/vt/sqlparser"
"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/mysql"
"vitess.io/vitess/go/sqltypes"
"vitess.io/vitess/go/vt/dbconfigs"
querypb "vitess.io/vitess/go/vt/proto/query"
)
const appendEntry = -1
// DB is a fake database and all its methods are thread safe. It
// creates a mysql.Listener and implements the mysql.Handler
// interface. We use a Unix socket to connect to the database, as
// this is the most common way for clients to connect to MySQL. This
// impacts the error codes we're getting back: when the server side is
// closed, the client queries will return CRServerGone(2006) when sending
// the data, as opposed to CRServerLost(2013) when reading the response.
type DB struct {
mysql.UnimplementedHandler
// Fields set at construction time.
// t is our testing.TB instance
t testing.TB
// listener is our mysql.Listener.
listener *mysql.Listener
// socketFile is the path to the unix socket file.
socketFile string
// acceptWG is set when we listen, and can be waited on to
// make sure we don't accept any more.
acceptWG sync.WaitGroup
// orderMatters is set when the query order matters.
orderMatters atomic.Bool
// Fields set at runtime.
// mu protects all the following fields.
mu sync.Mutex
// name is the name of this DB. Set to 'fakesqldb' by default.
// Use SetName() to change.
name string
// isConnFail trigger a panic in the connection handler.
isConnFail atomic.Bool
// connDelay causes a sleep in the connection handler
connDelay time.Duration
// shouldClose, if true, tells ComQuery() to close the connection when
// processing the next query. This will trigger a MySQL client error with
// errno 2013 ("server lost").
shouldClose atomic.Bool
// allowAll: if set to true, ComQuery returns an empty result
// for all queries. This flag is used for benchmarking.
allowAll atomic.Bool
// Handler: interface that allows a caller to override the query handling
// implementation. By default it points to the DB itself
Handler QueryHandler
// This next set of fields is used when ordering of the queries doesn't
// matter.
// data maps tolower(query) to a result.
data map[string]*ExpectedResult
// rejectedData maps tolower(query) to an error.
rejectedData map[string]error
// patternData is a map of regexp queries to results.
patternData map[string]exprResult
// queryCalled keeps track of how many times a query was called.
queryCalled map[string]int
// querylog keeps track of all called queries
querylog []string
// This next set of fields is used when ordering of the queries matters.
// expectedExecuteFetch is the array of expected queries.
expectedExecuteFetch []ExpectedExecuteFetch
// expectedExecuteFetchIndex is the current index of the query.
expectedExecuteFetchIndex int
// connections tracks all open connections.
// The key for the map is the value of mysql.Conn.ConnectionID.
connections map[uint32]*mysql.Conn
// queryPatternUserCallback stores optional callbacks when a query with a pattern is called
queryPatternUserCallback map[*regexp.Regexp]func(string)
// if fakesqldb is asked to serve queries or query patterns that it has not been explicitly told about it will
// error out by default. However if you set this flag then any unmatched query results in an empty result
neverFail atomic.Bool
// lastError stores the last error in returning a query result.
lastErrorMu sync.Mutex
lastError error
}
// QueryHandler is the interface used by the DB to simulate executed queries
type QueryHandler interface {
HandleQuery(*mysql.Conn, string, func(*sqltypes.Result) error) error
}
// ExpectedResult holds the data for a matched query.
type ExpectedResult struct {
*sqltypes.Result
// BeforeFunc() is synchronously called before the server returns the result.
BeforeFunc func()
}
type exprResult struct {
queryPattern string
expr *regexp.Regexp
result *sqltypes.Result
err string
}
// ExpectedExecuteFetch defines for an expected query the to be faked output.
// It is used for ordered expected output.
type ExpectedExecuteFetch struct {
Query string
QueryResult *sqltypes.Result
Error error
// AfterFunc is a callback which is executed while the query
// is executed i.e., before the fake responds to the client.
AfterFunc func()
}
// New creates a server, and starts listening.
func New(t testing.TB) *DB {
// Pick a path for our socket.
socketDir, err := os.MkdirTemp("", "fakesqldb")
if err != nil {
t.Fatalf("os.MkdirTemp failed: %v", err)
}
socketFile := path.Join(socketDir, "fakesqldb.sock")
// Create our DB.
db := &DB{
t: t,
socketFile: socketFile,
name: "fakesqldb",
data: make(map[string]*ExpectedResult),
rejectedData: make(map[string]error),
queryCalled: make(map[string]int),
connections: make(map[uint32]*mysql.Conn),
queryPatternUserCallback: make(map[*regexp.Regexp]func(string)),
patternData: make(map[string]exprResult),
lastErrorMu: sync.Mutex{},
}
db.Handler = db
authServer := mysql.NewAuthServerNone()
// Start listening.
db.listener, err = mysql.NewListener("unix", socketFile, authServer, db, 0, 0, false, false, 0)
if err != nil {
t.Fatalf("NewListener failed: %v", err)
}
db.acceptWG.Add(1)
go func() {
defer db.acceptWG.Done()
db.listener.Accept()
}()
db.AddQuery("use `fakesqldb`", &sqltypes.Result{})
// Return the db.
return db
}
// Name returns the name of the DB.
func (db *DB) Name() string {
db.mu.Lock()
defer db.mu.Unlock()
return db.name
}
// SetName sets the name of the DB. to differentiate them in tests if needed.
func (db *DB) SetName(name string) *DB {
db.mu.Lock()
defer db.mu.Unlock()
db.name = name
return db
}
// OrderMatters sets the orderMatters flag.
func (db *DB) OrderMatters() {
db.orderMatters.Store(true)
}
// Close closes the Listener and waits for it to stop accepting.
// It then closes all connections, and cleans up the temporary directory.
func (db *DB) Close() {
db.listener.Close()
db.acceptWG.Wait()
db.CloseAllConnections()
tmpDir := path.Dir(db.socketFile)
os.RemoveAll(tmpDir)
}
// CloseAllConnections can be used to provoke MySQL client errors for open
// connections.
// Make sure to call WaitForClose() as well.
func (db *DB) CloseAllConnections() {
db.mu.Lock()
defer db.mu.Unlock()
for _, c := range db.connections {
c.Close()
}
}
// LastError gives the last error the DB ran into
func (db *DB) LastError() error {
db.lastErrorMu.Lock()
defer db.lastErrorMu.Unlock()
return db.lastError
}
// WaitForClose should be used after CloseAllConnections() is closed and
// you want to provoke a MySQL client error with errno 2006.
//
// If you don't call this function and execute the next query right away, you
// will very likely see errno 2013 instead due to timing issues.
// That's because the following can happen:
//
// 1. vttablet MySQL client is able to send the query to this fake server.
// 2. The fake server sees the query and calls the ComQuery() callback.
// 3. The fake server tries to write the response back on the connection.
// => This will finally fail because the connection is already closed.
// In this example, the client would have been able to send off the query and
// therefore return errno 2013 ("server lost").
// Instead, if step 1 already fails, the client returns errno 2006 ("gone away").
// By waiting for the connections to close, you make sure of that.
func (db *DB) WaitForClose(timeout time.Duration) error {
start := time.Now()
for {
db.mu.Lock()
count := len(db.connections)
db.mu.Unlock()
if count == 0 {
return nil
}
if d := time.Since(start); d > timeout {
return fmt.Errorf("connections were not correctly closed after %v: %v are left", d, count)
}
time.Sleep(1 * time.Microsecond)
}
}
// ConnParams returns the ConnParams to connect to the DB.
func (db *DB) ConnParams() dbconfigs.Connector {
return dbconfigs.New(&mysql.ConnParams{
UnixSocket: db.socketFile,
Uname: "user1",
Pass: "password1",
DbName: "fakesqldb",
})
}
// ConnParamsWithUname returns ConnParams to connect to the DB with the Uname set to the provided value.
func (db *DB) ConnParamsWithUname(uname string) dbconfigs.Connector {
return dbconfigs.New(&mysql.ConnParams{
UnixSocket: db.socketFile,
Uname: uname,
Pass: "password1",
DbName: "fakesqldb",
})
}
//
// mysql.Handler interface
//
// NewConnection is part of the mysql.Handler interface.
func (db *DB) NewConnection(c *mysql.Conn) {
db.mu.Lock()
defer db.mu.Unlock()
if db.isConnFail.Load() {
panic(fmt.Errorf("simulating a connection failure"))
}
if db.connDelay != 0 {
time.Sleep(db.connDelay)
}
if conn, ok := db.connections[c.ConnectionID]; ok {
db.t.Fatalf("BUG: connection with id: %v is already active. existing conn: %v new conn: %v", c.ConnectionID, conn, c)
}
db.connections[c.ConnectionID] = c
}
// ConnectionClosed is part of the mysql.Handler interface.
func (db *DB) ConnectionClosed(c *mysql.Conn) {
db.mu.Lock()
defer db.mu.Unlock()
if _, ok := db.connections[c.ConnectionID]; !ok {
panic(fmt.Errorf("BUG: Cannot delete connection from list of open connections because it is not registered. ID: %v Conn: %v", c.ConnectionID, c))
}
delete(db.connections, c.ConnectionID)
}
// ComQuery is part of the mysql.Handler interface.
func (db *DB) ComQuery(c *mysql.Conn, query string, callback func(*sqltypes.Result) error) error {
return db.Handler.HandleQuery(c, query, callback)
}
// WarningCount is part of the mysql.Handler interface.
func (db *DB) WarningCount(c *mysql.Conn) uint16 {
return 0
}
// HandleQuery is the default implementation of the QueryHandler interface
func (db *DB) HandleQuery(c *mysql.Conn, query string, callback func(*sqltypes.Result) error) (err error) {
defer func() {
if err != nil {
db.lastErrorMu.Lock()
db.lastError = err
db.lastErrorMu.Unlock()
}
}()
if db.allowAll.Load() {
return callback(&sqltypes.Result{})
}
if db.orderMatters.Load() {
result, err := db.comQueryOrdered(query)
if err != nil {
return err
}
return callback(result)
}
key := strings.ToLower(query)
db.mu.Lock()
defer db.mu.Unlock()
db.queryCalled[key]++
db.querylog = append(db.querylog, key)
// Check if we should close the connection and provoke errno 2013.
if db.shouldClose.Load() {
c.Close()
// log error
if err := callback(&sqltypes.Result{}); err != nil {
log.Errorf("callback failed : %v", err)
}
return nil
}
// Using special handling for setting the charset and connection collation.
// The driver may send this at connection time, and we don't want it to
// interfere.
if key == "set names utf8" || strings.HasPrefix(key, "set collation_connection = ") {
// log error
if err := callback(&sqltypes.Result{}); err != nil {
log.Errorf("callback failed : %v", err)
}
return nil
}
// check if we should reject it.
if err, ok := db.rejectedData[key]; ok {
return err
}
// Check explicit queries from AddQuery().
result, ok := db.data[key]
if ok {
if f := result.BeforeFunc; f != nil {
f()
}
return callback(result.Result)
}
// Check query patterns from AddQueryPattern().
for _, pat := range db.patternData {
if pat.expr.MatchString(query) {
userCallback, ok := db.queryPatternUserCallback[pat.expr]
if ok {
userCallback(query)
}
if pat.err != "" {
return fmt.Errorf(pat.err)
}
return callback(pat.result)
}
}
if db.neverFail.Load() {
return callback(&sqltypes.Result{})
}
// Nothing matched.
err = fmt.Errorf("fakesqldb:: query: '%s' is not supported on %v",
sqlparser.TruncateForUI(query), db.name)
log.Errorf("Query not found: %s", sqlparser.TruncateForUI(query))
return err
}
func (db *DB) comQueryOrdered(query string) (*sqltypes.Result, error) {
var (
afterFn func()
entry ExpectedExecuteFetch
err error
expected string
result *sqltypes.Result
)
defer func() {
if afterFn != nil {
afterFn()
}
}()
db.mu.Lock()
defer db.mu.Unlock()
// when creating a connection to the database, we send an initial query to set the connection's
// collation, we want to skip the query check if we get such initial query.
// this is done to ease the test readability.
if strings.HasPrefix(query, "SET collation_connection =") || strings.EqualFold(query, "use `fakesqldb`") {
return &sqltypes.Result{}, nil
}
index := db.expectedExecuteFetchIndex
if index >= len(db.expectedExecuteFetch) {
if db.neverFail.Load() {
return &sqltypes.Result{}, nil
}
db.t.Errorf("%v: got unexpected out of bound fetch: %v >= %v (%s)", db.name, index, len(db.expectedExecuteFetch), query)
return nil, errors.New("unexpected out of bound fetch")
}
entry = db.expectedExecuteFetch[index]
afterFn = entry.AfterFunc
err = entry.Error
expected = entry.Query
result = entry.QueryResult
if strings.HasSuffix(expected, "*") {
if !strings.HasPrefix(query, expected[0:len(expected)-1]) {
if db.neverFail.Load() {
return &sqltypes.Result{}, nil
}
db.t.Errorf("%v: got unexpected query start (index=%v): %v != %v", db.name, index, query, expected)
return nil, errors.New("unexpected query")
}
} else {
if query != expected {
if db.neverFail.Load() {
return &sqltypes.Result{}, nil
}
db.t.Errorf("%v: got unexpected query (index=%v): %v != %v", db.name, index, query, expected)
return nil, errors.New("unexpected query")
}
}
db.expectedExecuteFetchIndex++
db.t.Logf("ExecuteFetch: %v: %v", db.name, query)
if err != nil {
return nil, err
}
return result, nil
}
// ComPrepare is part of the mysql.Handler interface.
func (db *DB) ComPrepare(c *mysql.Conn, query string, bindVars map[string]*querypb.BindVariable) ([]*querypb.Field, error) {
return nil, nil
}
// ComStmtExecute is part of the mysql.Handler interface.
func (db *DB) ComStmtExecute(c *mysql.Conn, prepare *mysql.PrepareData, callback func(*sqltypes.Result) error) error {
return nil
}
// ComRegisterReplica is part of the mysql.Handler interface.
func (db *DB) ComRegisterReplica(c *mysql.Conn, replicaHost string, replicaPort uint16, replicaUser string, replicaPassword string) error {
return nil
}
// ComBinlogDump is part of the mysql.Handler interface.
func (db *DB) ComBinlogDump(c *mysql.Conn, logFile string, binlogPos uint32) error {
return nil
}
// ComBinlogDumpGTID is part of the mysql.Handler interface.
func (db *DB) ComBinlogDumpGTID(c *mysql.Conn, logFile string, logPos uint64, gtidSet replication.GTIDSet) error {
return nil
}
//
// Methods to add expected queries and results.
//
// AddQuery adds a query and its expected result.
func (db *DB) AddQuery(query string, expectedResult *sqltypes.Result) *ExpectedResult {
if len(expectedResult.Rows) > 0 && len(expectedResult.Fields) == 0 {
panic(fmt.Errorf("please add Fields to this Result so it's valid: %v", query))
}
resultCopy := &sqltypes.Result{}
*resultCopy = *expectedResult
db.mu.Lock()
defer db.mu.Unlock()
key := strings.ToLower(query)
r := &ExpectedResult{resultCopy, nil}
db.data[key] = r
db.queryCalled[key] = 0
return r
}
// SetBeforeFunc sets the BeforeFunc field for the previously registered "query".
func (db *DB) SetBeforeFunc(query string, f func()) {
db.mu.Lock()
defer db.mu.Unlock()
key := strings.ToLower(query)
r, ok := db.data[key]
if !ok {
db.t.Fatalf("BUG: no query registered for: %v", query)
}
r.BeforeFunc = f
}
// AddQueryPattern adds an expected result for a set of queries.
// These patterns are checked if no exact matches from AddQuery() are found.
// This function forces the addition of begin/end anchors (^$) and turns on
// case-insensitive matching mode.
func (db *DB) AddQueryPattern(queryPattern string, expectedResult *sqltypes.Result) {
if len(expectedResult.Rows) > 0 && len(expectedResult.Fields) == 0 {
panic(fmt.Errorf("please add Fields to this Result so it's valid: %v", queryPattern))
}
expr := regexp.MustCompile("(?is)^" + queryPattern + "$")
result := *expectedResult
db.mu.Lock()
defer db.mu.Unlock()
db.patternData[queryPattern] = exprResult{queryPattern: queryPattern, expr: expr, result: &result}
}
// RejectQueryPattern allows a query pattern to be rejected with an error
func (db *DB) RejectQueryPattern(queryPattern, error string) {
expr := regexp.MustCompile("(?is)^" + queryPattern + "$")
db.mu.Lock()
defer db.mu.Unlock()
db.patternData[queryPattern] = exprResult{queryPattern: queryPattern, expr: expr, err: error}
}
// ClearQueryPattern removes all query patterns set up
func (db *DB) ClearQueryPattern() {
db.patternData = make(map[string]exprResult)
}
// AddQueryPatternWithCallback is similar to AddQueryPattern: in addition it calls the provided callback function
// The callback can be used to set user counters/variables for testing specific usecases
func (db *DB) AddQueryPatternWithCallback(queryPattern string, expectedResult *sqltypes.Result, callback func(string)) {
db.AddQueryPattern(queryPattern, expectedResult)
db.queryPatternUserCallback[db.patternData[queryPattern].expr] = callback
}
// DeleteQuery deletes query from the fake DB.
func (db *DB) DeleteQuery(query string) {
db.mu.Lock()
defer db.mu.Unlock()
key := strings.ToLower(query)
delete(db.data, key)
delete(db.queryCalled, key)
}
// AddRejectedQuery adds a query which will be rejected at execution time.
func (db *DB) AddRejectedQuery(query string, err error) {
db.mu.Lock()
defer db.mu.Unlock()
db.rejectedData[strings.ToLower(query)] = err
}
// DeleteRejectedQuery deletes query from the fake DB.
func (db *DB) DeleteRejectedQuery(query string) {
db.mu.Lock()
defer db.mu.Unlock()
delete(db.rejectedData, strings.ToLower(query))
}
// GetQueryCalledNum returns how many times db executes a certain query.
func (db *DB) GetQueryCalledNum(query string) int {
db.mu.Lock()
defer db.mu.Unlock()
num, ok := db.queryCalled[strings.ToLower(query)]
if !ok {
return 0
}
return num
}
// QueryLog returns the query log in a semicomma separated string
func (db *DB) QueryLog() string {
db.mu.Lock()
defer db.mu.Unlock()
return strings.Join(db.querylog, ";")
}
// ResetQueryLog resets the query log
func (db *DB) ResetQueryLog() {
db.mu.Lock()
defer db.mu.Unlock()
db.querylog = nil
}
// EnableConnFail makes connection to this fake DB fail.
func (db *DB) EnableConnFail() {
db.isConnFail.Store(true)
}
// DisableConnFail makes connection to this fake DB success.
func (db *DB) DisableConnFail() {
db.isConnFail.Store(false)
}
// SetConnDelay delays connections to this fake DB for the given duration
func (db *DB) SetConnDelay(d time.Duration) {
db.mu.Lock()
defer db.mu.Unlock()
db.connDelay = d
}
// EnableShouldClose closes the connection when processing the next query.
func (db *DB) EnableShouldClose() {
db.shouldClose.Store(true)
}
//
// The following methods are used for ordered expected queries.
//
// AddExpectedExecuteFetch adds an ExpectedExecuteFetch directly.
func (db *DB) AddExpectedExecuteFetch(entry ExpectedExecuteFetch) {
db.AddExpectedExecuteFetchAtIndex(appendEntry, entry)
}
// AddExpectedExecuteFetchAtIndex inserts a new entry at index.
// index values start at 0.
func (db *DB) AddExpectedExecuteFetchAtIndex(index int, entry ExpectedExecuteFetch) {
db.mu.Lock()
defer db.mu.Unlock()
if db.expectedExecuteFetch == nil || index < 0 || index >= len(db.expectedExecuteFetch) {
index = appendEntry
}
if index == appendEntry {
db.expectedExecuteFetch = append(db.expectedExecuteFetch, entry)
} else {
// Grow the slice by one element.
if cap(db.expectedExecuteFetch) == len(db.expectedExecuteFetch) {
db.expectedExecuteFetch = append(db.expectedExecuteFetch, make([]ExpectedExecuteFetch, 1)...)
} else {
db.expectedExecuteFetch = db.expectedExecuteFetch[0 : len(db.expectedExecuteFetch)+1]
}
// Use copy to move the upper part of the slice out of the way and open a hole.
copy(db.expectedExecuteFetch[index+1:], db.expectedExecuteFetch[index:])
// Store the new value.
db.expectedExecuteFetch[index] = entry
}
}
// AddExpectedQuery adds a single query with no result.
func (db *DB) AddExpectedQuery(query string, err error) {
db.AddExpectedExecuteFetch(ExpectedExecuteFetch{
Query: query,
QueryResult: &sqltypes.Result{},
Error: err,
})
}
// AddExpectedQueryAtIndex adds an expected ordered query at an index.
func (db *DB) AddExpectedQueryAtIndex(index int, query string, err error) {
db.AddExpectedExecuteFetchAtIndex(index, ExpectedExecuteFetch{
Query: query,
QueryResult: &sqltypes.Result{},
Error: err,
})
}
// GetEntry returns the expected entry at "index". If index is out of bounds,
// the return value will be nil.
func (db *DB) GetEntry(index int) *ExpectedExecuteFetch {
db.mu.Lock()
defer db.mu.Unlock()
if index < 0 || index >= len(db.expectedExecuteFetch) {
panic(fmt.Sprintf("index out of range. current length: %v", len(db.expectedExecuteFetch)))
}
return &db.expectedExecuteFetch[index]
}
// DeleteAllEntries removes all ordered entries.
func (db *DB) DeleteAllEntries() {
db.mu.Lock()
defer db.mu.Unlock()
db.expectedExecuteFetch = make([]ExpectedExecuteFetch, 0)
db.expectedExecuteFetchIndex = 0
}
// DeleteAllEntriesAfterIndex removes all queries after the index.
func (db *DB) DeleteAllEntriesAfterIndex(index int) {
db.mu.Lock()
defer db.mu.Unlock()
if index < 0 || index >= len(db.expectedExecuteFetch) {
panic(fmt.Sprintf("index out of range. current length: %v", len(db.expectedExecuteFetch)))
}
if index+1 < db.expectedExecuteFetchIndex {
// Don't delete entries which were already answered.
return
}
db.expectedExecuteFetch = db.expectedExecuteFetch[:index+1]
}
// VerifyAllExecutedOrFail checks that all expected queries where actually
// received and executed. If not, it will let the test fail.
func (db *DB) VerifyAllExecutedOrFail() {
db.mu.Lock()
defer db.mu.Unlock()
if db.expectedExecuteFetchIndex != len(db.expectedExecuteFetch) {
db.t.Errorf("%v: not all expected queries were executed. leftovers: %v", db.name, db.expectedExecuteFetch[db.expectedExecuteFetchIndex:])
}
}
func (db *DB) SetAllowAll(allowAll bool) {
db.allowAll.Store(allowAll)
}
func (db *DB) SetNeverFail(neverFail bool) {
db.neverFail.Store(neverFail)
}
func (db *DB) MockQueriesForTable(table string, result *sqltypes.Result) {
// pattern for selecting explicit list of columns where database is specified
selectQueryPattern := fmt.Sprintf("select .* from `%s`.`%s` where 1 != 1", db.name, table)
db.AddQueryPattern(selectQueryPattern, result)
// pattern for selecting explicit list of columns where database is not specified
selectQueryPattern = fmt.Sprintf("select .* from %s where 1 != 1", table)
db.AddQueryPattern(selectQueryPattern, result)
// mock query for returning columns from information_schema.columns based on specified result
var cols []string
for _, field := range result.Fields {
cols = append(cols, field.Name)
}
db.AddQueryPattern(fmt.Sprintf(mysql.GetColumnNamesQueryPatternForTable, table), sqltypes.MakeTestResult(
sqltypes.MakeTestFields(
"column_name",
"varchar",
),
cols...,
))
}
// GetRejectedQueryResult checks if we should reject the query.
func (db *DB) GetRejectedQueryResult(key string) error {
if err, ok := db.rejectedData[key]; ok {
return err
}
return nil
}
// GetQueryResult checks for explicit queries add through AddQuery().
func (db *DB) GetQueryResult(key string) *ExpectedResult {
result, ok := db.data[key]
if ok {
return result
}
return nil
}
// GetQueryPatternResult checks if a query matches any pattern previously added using AddQueryPattern().
func (db *DB) GetQueryPatternResult(key string) (func(string), ExpectedResult, bool, error) {
for _, pat := range db.patternData {
if pat.expr.MatchString(key) {
userCallback, ok := db.queryPatternUserCallback[pat.expr]
if ok {
if pat.err != "" {
return userCallback, ExpectedResult{pat.result, nil}, true, fmt.Errorf(pat.err)
}
return userCallback, ExpectedResult{pat.result, nil}, true, nil
}
return nil, ExpectedResult{nil, nil}, false, nil
}
}
return nil, ExpectedResult{nil, nil}, false, nil
}