forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
2211 lines (2052 loc) · 67.2 KB
/
client.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
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2015, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package vtgateconntest provides the test methods to make sure a
// vtgateconn/vtgateservice pair over RPC works correctly.
package vtgateconntest
import (
"errors"
"fmt"
"io"
"reflect"
"strings"
"testing"
"time"
"github.com/youtube/vitess/go/sqltypes"
"github.com/youtube/vitess/go/tb"
"github.com/youtube/vitess/go/vt/callerid"
"github.com/youtube/vitess/go/vt/vterrors"
"github.com/youtube/vitess/go/vt/vtgate/vtgateconn"
"github.com/youtube/vitess/go/vt/vtgate/vtgateservice"
"golang.org/x/net/context"
querypb "github.com/youtube/vitess/go/vt/proto/query"
topodatapb "github.com/youtube/vitess/go/vt/proto/topodata"
vtgatepb "github.com/youtube/vitess/go/vt/proto/vtgate"
vtrpcpb "github.com/youtube/vitess/go/vt/proto/vtrpc"
)
// fakeVTGateService has the server side of this fake
type fakeVTGateService struct {
t *testing.T
panics bool
hasError bool
// If True, calls to Begin will always succeed. This is necessary so that
// we can test subsequent calls in the transaction (e.g., Commit, Rollback).
forceBeginSuccess bool
errorWait chan struct{}
}
const expectedErrMatch string = "test vtgate error"
const expectedCode vtrpcpb.ErrorCode = vtrpcpb.ErrorCode_BAD_INPUT
// the error string that is returned for partial execute errors (i.e., the Execute* rpc call
// succeeds, but returns an error as part of the response).
const executePartialErrString string = "execute partial error"
var errTestVtGateError = vterrors.FromError(expectedCode, errors.New(expectedErrMatch))
func newContext() context.Context {
ctx := context.Background()
ctx = callerid.NewContext(ctx, testCallerID, nil)
return ctx
}
func (f *fakeVTGateService) checkCallerID(ctx context.Context, name string) {
ef := callerid.EffectiveCallerIDFromContext(ctx)
if ef == nil {
f.t.Errorf("no effective caller id for %v", name)
} else {
if !reflect.DeepEqual(ef, testCallerID) {
f.t.Errorf("invalid effective caller id for %v: got %v expected %v", name, ef, testCallerID)
}
}
}
// queryExecute contains all the fields we use to test Execute
type queryExecute struct {
SQL string
BindVariables map[string]interface{}
Keyspace string
TabletType topodatapb.TabletType
Session *vtgatepb.Session
NotInTransaction bool
}
// Execute is part of the VTGateService interface
func (f *fakeVTGateService) Execute(ctx context.Context, sql string, bindVariables map[string]interface{}, keyspace string, tabletType topodatapb.TabletType, session *vtgatepb.Session, notInTransaction bool) (*sqltypes.Result, error) {
if f.hasError {
return nil, errTestVtGateError
}
if f.panics {
panic(fmt.Errorf("test forced panic"))
}
f.checkCallerID(ctx, "Execute")
execCase, ok := execMap[sql]
if !ok {
return nil, fmt.Errorf("no match for: %s", sql)
}
query := &queryExecute{
SQL: sql,
BindVariables: bindVariables,
Keyspace: keyspace,
TabletType: tabletType,
Session: session,
NotInTransaction: notInTransaction,
}
if !reflect.DeepEqual(query, execCase.execQuery) {
f.t.Errorf("Execute: %+v, want %+v", query, execCase.execQuery)
return nil, nil
}
if execCase.outSession != nil {
*session = *execCase.outSession
}
return execCase.result, nil
}
// queryExecuteShards contains all the fields we use to test ExecuteShards
type queryExecuteShards struct {
SQL string
BindVariables map[string]interface{}
Keyspace string
Shards []string
TabletType topodatapb.TabletType
Session *vtgatepb.Session
NotInTransaction bool
}
// ExecuteShards is part of the VTGateService interface
func (f *fakeVTGateService) ExecuteShards(ctx context.Context, sql string, bindVariables map[string]interface{}, keyspace string, shards []string, tabletType topodatapb.TabletType, session *vtgatepb.Session, notInTransaction bool) (*sqltypes.Result, error) {
if f.hasError {
return nil, errTestVtGateError
}
if f.panics {
panic(fmt.Errorf("test forced panic"))
}
f.checkCallerID(ctx, "ExecuteShards")
execCase, ok := execMap[sql]
if !ok {
return nil, fmt.Errorf("no match for: %s", sql)
}
query := &queryExecuteShards{
SQL: sql,
BindVariables: bindVariables,
TabletType: tabletType,
Keyspace: keyspace,
Shards: shards,
Session: session,
NotInTransaction: notInTransaction,
}
if !reflect.DeepEqual(query, execCase.shardQuery) {
f.t.Errorf("ExecuteShards: %+v, want %+v", query, execCase.shardQuery)
return nil, nil
}
if execCase.outSession != nil {
*session = *execCase.outSession
}
return execCase.result, nil
}
// queryExecuteKeyspaceIds contains all the fields we use to test
// ExecuteKeyspaceIds
type queryExecuteKeyspaceIds struct {
SQL string
BindVariables map[string]interface{}
Keyspace string
KeyspaceIds [][]byte
TabletType topodatapb.TabletType
Session *vtgatepb.Session
NotInTransaction bool
}
// ExecuteKeyspaceIds is part of the VTGateService interface
func (f *fakeVTGateService) ExecuteKeyspaceIds(ctx context.Context, sql string, bindVariables map[string]interface{}, keyspace string, keyspaceIds [][]byte, tabletType topodatapb.TabletType, session *vtgatepb.Session, notInTransaction bool) (*sqltypes.Result, error) {
if f.hasError {
return nil, errTestVtGateError
}
if f.panics {
panic(fmt.Errorf("test forced panic"))
}
f.checkCallerID(ctx, "ExecuteKeyspaceIds")
execCase, ok := execMap[sql]
if !ok {
return nil, fmt.Errorf("no match for: %s", sql)
}
query := &queryExecuteKeyspaceIds{
SQL: sql,
BindVariables: bindVariables,
TabletType: tabletType,
Keyspace: keyspace,
KeyspaceIds: keyspaceIds,
Session: session,
NotInTransaction: notInTransaction,
}
if !reflect.DeepEqual(query, execCase.keyspaceIDQuery) {
f.t.Errorf("ExecuteKeyspaceIds: %+v, want %+v", query, execCase.keyspaceIDQuery)
return nil, nil
}
if execCase.outSession != nil {
*session = *execCase.outSession
}
return execCase.result, nil
}
// queryExecuteKeyRanges contains all the fields we use to test ExecuteKeyRanges
type queryExecuteKeyRanges struct {
SQL string
BindVariables map[string]interface{}
Keyspace string
KeyRanges []*topodatapb.KeyRange
TabletType topodatapb.TabletType
Session *vtgatepb.Session
NotInTransaction bool
}
// ExecuteKeyRanges is part of the VTGateService interface
func (f *fakeVTGateService) ExecuteKeyRanges(ctx context.Context, sql string, bindVariables map[string]interface{}, keyspace string, keyRanges []*topodatapb.KeyRange, tabletType topodatapb.TabletType, session *vtgatepb.Session, notInTransaction bool) (*sqltypes.Result, error) {
if f.hasError {
return nil, errTestVtGateError
}
if f.panics {
panic(fmt.Errorf("test forced panic"))
}
f.checkCallerID(ctx, "ExecuteKeyRanges")
execCase, ok := execMap[sql]
if !ok {
return nil, fmt.Errorf("no match for: %s", sql)
}
query := &queryExecuteKeyRanges{
SQL: sql,
BindVariables: bindVariables,
TabletType: tabletType,
Keyspace: keyspace,
KeyRanges: keyRanges,
Session: session,
NotInTransaction: notInTransaction,
}
if !reflect.DeepEqual(query, execCase.keyRangeQuery) {
f.t.Errorf("ExecuteKeyRanges: %+v, want %+v", query, execCase.keyRangeQuery)
return nil, nil
}
if execCase.outSession != nil {
*session = *execCase.outSession
}
return execCase.result, nil
}
// queryExecuteEntityIds contains all the fields we use to test ExecuteEntityIds
type queryExecuteEntityIds struct {
SQL string
BindVariables map[string]interface{}
Keyspace string
EntityColumnName string
EntityKeyspaceIDs []*vtgatepb.ExecuteEntityIdsRequest_EntityId
TabletType topodatapb.TabletType
Session *vtgatepb.Session
NotInTransaction bool
}
// ExecuteEntityIds is part of the VTGateService interface
func (f *fakeVTGateService) ExecuteEntityIds(ctx context.Context, sql string, bindVariables map[string]interface{}, keyspace string, entityColumnName string, entityKeyspaceIDs []*vtgatepb.ExecuteEntityIdsRequest_EntityId, tabletType topodatapb.TabletType, session *vtgatepb.Session, notInTransaction bool) (*sqltypes.Result, error) {
if f.hasError {
return nil, errTestVtGateError
}
if f.panics {
panic(fmt.Errorf("test forced panic"))
}
f.checkCallerID(ctx, "ExecuteEntityIds")
execCase, ok := execMap[sql]
if !ok {
return nil, fmt.Errorf("no match for: %s", sql)
}
query := &queryExecuteEntityIds{
SQL: sql,
BindVariables: bindVariables,
TabletType: tabletType,
Keyspace: keyspace,
EntityColumnName: entityColumnName,
EntityKeyspaceIDs: entityKeyspaceIDs,
Session: session,
NotInTransaction: notInTransaction,
}
if len(query.EntityKeyspaceIDs) == 1 && len(query.EntityKeyspaceIDs[0].Value) == 0 {
query.EntityKeyspaceIDs[0].Value = nil
}
if !reflect.DeepEqual(query, execCase.entityIdsQuery) {
f.t.Errorf("ExecuteEntityIds: %+v, want %+v", query, execCase.entityIdsQuery)
return nil, nil
}
if execCase.outSession != nil {
*session = *execCase.outSession
}
return execCase.result, nil
}
// queryExecuteBatchShards contains all the fields we use to test
// ExecuteBatchShards
type queryExecuteBatchShards struct {
Queries []*vtgatepb.BoundShardQuery
TabletType topodatapb.TabletType
AsTransaction bool
Session *vtgatepb.Session
}
// ExecuteBatchShards is part of the VTGateService interface
func (f *fakeVTGateService) ExecuteBatchShards(ctx context.Context, queries []*vtgatepb.BoundShardQuery, tabletType topodatapb.TabletType, asTransaction bool, session *vtgatepb.Session) ([]sqltypes.Result, error) {
if f.hasError {
return nil, errTestVtGateError
}
if f.panics {
panic(fmt.Errorf("test forced panic"))
}
f.checkCallerID(ctx, "ExecuteBatchShards")
execCase, ok := execMap[queries[0].Query.Sql]
if !ok {
return nil, fmt.Errorf("no match for: %s", queries[0].Query.Sql)
}
query := &queryExecuteBatchShards{
Queries: queries,
TabletType: tabletType,
AsTransaction: asTransaction,
Session: session,
}
if !reflect.DeepEqual(query, execCase.batchQueryShard) {
f.t.Errorf("ExecuteBatchShards: %+v, want %+v", query, execCase.batchQueryShard)
return nil, nil
}
if execCase.outSession != nil {
*session = *execCase.outSession
}
if execCase.result != nil {
return []sqltypes.Result{*execCase.result}, nil
}
return nil, nil
}
// queryExecuteBatchKeyspaceIds contains all the fields we use to test
// ExecuteBatchKeyspaceIds
type queryExecuteBatchKeyspaceIds struct {
Queries []*vtgatepb.BoundKeyspaceIdQuery
TabletType topodatapb.TabletType
AsTransaction bool
Session *vtgatepb.Session
}
// ExecuteBatchKeyspaceIds is part of the VTGateService interface
func (f *fakeVTGateService) ExecuteBatchKeyspaceIds(ctx context.Context, queries []*vtgatepb.BoundKeyspaceIdQuery, tabletType topodatapb.TabletType, asTransaction bool, session *vtgatepb.Session) ([]sqltypes.Result, error) {
if f.hasError {
return nil, errTestVtGateError
}
if f.panics {
panic(fmt.Errorf("test forced panic"))
}
f.checkCallerID(ctx, "ExecuteBatchKeyspaceIds")
execCase, ok := execMap[queries[0].Query.Sql]
if !ok {
return nil, fmt.Errorf("no match for: %s", queries[0].Query.Sql)
}
query := &queryExecuteBatchKeyspaceIds{
Queries: queries,
TabletType: tabletType,
AsTransaction: asTransaction,
Session: session,
}
if !reflect.DeepEqual(query, execCase.keyspaceIDBatchQuery) {
f.t.Errorf("ExecuteBatchKeyspaceIds: %+v, want %+v", query, execCase.keyspaceIDBatchQuery)
return nil, nil
}
if execCase.outSession != nil {
*session = *execCase.outSession
}
if execCase.result != nil {
return []sqltypes.Result{*execCase.result}, nil
}
return nil, nil
}
// StreamExecute is part of the VTGateService interface
func (f *fakeVTGateService) StreamExecute(ctx context.Context, sql string, bindVariables map[string]interface{}, keyspace string, tabletType topodatapb.TabletType, sendReply func(*sqltypes.Result) error) error {
if f.panics {
panic(fmt.Errorf("test forced panic"))
}
execCase, ok := execMap[sql]
if !ok {
return fmt.Errorf("no match for: %s", sql)
}
f.checkCallerID(ctx, "StreamExecute")
query := &queryExecute{
SQL: sql,
BindVariables: bindVariables,
Keyspace: keyspace,
TabletType: tabletType,
}
if !reflect.DeepEqual(query, execCase.execQuery) {
f.t.Errorf("StreamExecute: %+v, want %+v", query, execCase.execQuery)
return nil
}
if execCase.result != nil {
result := &sqltypes.Result{
Fields: execCase.result.Fields,
}
if err := sendReply(result); err != nil {
return err
}
if f.hasError {
// wait until the client has the response, since all streaming implementation may not
// send previous messages if an error has been triggered.
<-f.errorWait
f.errorWait = make(chan struct{}) // for next test
return errTestVtGateError
}
for _, row := range execCase.result.Rows {
result := &sqltypes.Result{
Rows: [][]sqltypes.Value{row},
}
if err := sendReply(result); err != nil {
return err
}
}
}
return nil
}
// StreamExecuteShards is part of the VTGateService interface
func (f *fakeVTGateService) StreamExecuteShards(ctx context.Context, sql string, bindVariables map[string]interface{}, keyspace string, shards []string, tabletType topodatapb.TabletType, sendReply func(*sqltypes.Result) error) error {
if f.panics {
panic(fmt.Errorf("test forced panic"))
}
f.checkCallerID(ctx, "StreamExecuteShards")
execCase, ok := execMap[sql]
if !ok {
return fmt.Errorf("no match for: %s", sql)
}
query := &queryExecuteShards{
SQL: sql,
BindVariables: bindVariables,
Keyspace: keyspace,
Shards: shards,
TabletType: tabletType,
}
if !reflect.DeepEqual(query, execCase.shardQuery) {
f.t.Errorf("Execute: %+v, want %+v", query, execCase.shardQuery)
return nil
}
if execCase.result != nil {
result := &sqltypes.Result{
Fields: execCase.result.Fields,
}
if err := sendReply(result); err != nil {
return err
}
if f.hasError {
// wait until the client has the response, since all streaming implementation may not
// send previous messages if an error has been triggered.
<-f.errorWait
f.errorWait = make(chan struct{}) // for next test
return errTestVtGateError
}
for _, row := range execCase.result.Rows {
result := &sqltypes.Result{
Rows: [][]sqltypes.Value{row},
}
if err := sendReply(result); err != nil {
return err
}
}
}
return nil
}
// StreamExecuteKeyspaceIds is part of the VTGateService interface
func (f *fakeVTGateService) StreamExecuteKeyspaceIds(ctx context.Context, sql string, bindVariables map[string]interface{}, keyspace string, keyspaceIds [][]byte, tabletType topodatapb.TabletType, sendReply func(*sqltypes.Result) error) error {
if f.panics {
panic(fmt.Errorf("test forced panic"))
}
f.checkCallerID(ctx, "StreamExecuteKeyspaceIds")
execCase, ok := execMap[sql]
if !ok {
return fmt.Errorf("no match for: %s", sql)
}
query := &queryExecuteKeyspaceIds{
SQL: sql,
BindVariables: bindVariables,
Keyspace: keyspace,
KeyspaceIds: keyspaceIds,
TabletType: tabletType,
}
if !reflect.DeepEqual(query, execCase.keyspaceIDQuery) {
f.t.Errorf("StreamExecuteKeyspaceIds: %+v, want %+v", query, execCase.keyspaceIDQuery)
return nil
}
if execCase.result != nil {
result := &sqltypes.Result{
Fields: execCase.result.Fields,
}
if err := sendReply(result); err != nil {
return err
}
if f.hasError {
// wait until the client has the response, since all streaming implementation may not
// send previous messages if an error has been triggered.
<-f.errorWait
f.errorWait = make(chan struct{}) // for next test
return errTestVtGateError
}
for _, row := range execCase.result.Rows {
result := &sqltypes.Result{
Rows: [][]sqltypes.Value{row},
}
if err := sendReply(result); err != nil {
return err
}
}
}
return nil
}
// StreamExecuteKeyRanges is part of the VTGateService interface
func (f *fakeVTGateService) StreamExecuteKeyRanges(ctx context.Context, sql string, bindVariables map[string]interface{}, keyspace string, keyRanges []*topodatapb.KeyRange, tabletType topodatapb.TabletType, sendReply func(*sqltypes.Result) error) error {
if f.panics {
panic(fmt.Errorf("test forced panic"))
}
f.checkCallerID(ctx, "StreamExecuteKeyRanges")
execCase, ok := execMap[sql]
if !ok {
return fmt.Errorf("no match for: %s", sql)
}
query := &queryExecuteKeyRanges{
SQL: sql,
BindVariables: bindVariables,
Keyspace: keyspace,
KeyRanges: keyRanges,
TabletType: tabletType,
}
if !reflect.DeepEqual(query, execCase.keyRangeQuery) {
f.t.Errorf("StreamExecuteKeyRanges: %+v, want %+v", query, execCase.keyRangeQuery)
return nil
}
if execCase.result != nil {
result := &sqltypes.Result{
Fields: execCase.result.Fields,
}
if err := sendReply(result); err != nil {
return err
}
if f.hasError {
// wait until the client has the response, since all streaming implementation may not
// send previous messages if an error has been triggered.
<-f.errorWait
f.errorWait = make(chan struct{}) // for next test
return errTestVtGateError
}
for _, row := range execCase.result.Rows {
result := &sqltypes.Result{
Rows: [][]sqltypes.Value{row},
}
if err := sendReply(result); err != nil {
return err
}
}
}
return nil
}
// Begin is part of the VTGateService interface
func (f *fakeVTGateService) Begin(ctx context.Context) (*vtgatepb.Session, error) {
f.checkCallerID(ctx, "Begin")
switch {
case f.forceBeginSuccess:
case f.hasError:
return nil, errTestVtGateError
case f.panics:
panic(fmt.Errorf("test forced panic"))
default:
}
return session1, nil
}
// Commit is part of the VTGateService interface
func (f *fakeVTGateService) Commit(ctx context.Context, inSession *vtgatepb.Session) error {
f.checkCallerID(ctx, "Commit")
if f.hasError {
return errTestVtGateError
}
if f.panics {
panic(fmt.Errorf("test forced panic"))
}
if !reflect.DeepEqual(inSession, session2) {
return errors.New("commit: session mismatch")
}
return nil
}
// Rollback is part of the VTGateService interface
func (f *fakeVTGateService) Rollback(ctx context.Context, inSession *vtgatepb.Session) error {
if f.hasError {
return errTestVtGateError
}
if f.panics {
panic(fmt.Errorf("test forced panic"))
}
f.checkCallerID(ctx, "Rollback")
if !reflect.DeepEqual(inSession, session2) {
return errors.New("rollback: session mismatch")
}
return nil
}
// querySplitQuery contains all the fields we use to test SplitQuery
type querySplitQuery struct {
Keyspace string
SQL string
BindVariables map[string]interface{}
SplitColumn string
SplitCount int64
}
// SplitQuery is part of the VTGateService interface
func (f *fakeVTGateService) SplitQuery(ctx context.Context, keyspace string, sql string, bindVariables map[string]interface{}, splitColumn string, splitCount int64) ([]*vtgatepb.SplitQueryResponse_Part, error) {
if f.hasError {
return nil, errTestVtGateError
}
if f.panics {
panic(fmt.Errorf("test forced panic"))
}
f.checkCallerID(ctx, "SplitQuery")
query := &querySplitQuery{
Keyspace: keyspace,
SQL: sql,
BindVariables: bindVariables,
SplitColumn: splitColumn,
SplitCount: splitCount,
}
if !reflect.DeepEqual(query, splitQueryRequest) {
f.t.Errorf("SplitQuery has wrong input: got %#v wanted %#v", query, splitQueryRequest)
}
return splitQueryResult, nil
}
// querySplitQueryV2 contains all the fields we use to test SplitQuery
// TODO(erez): Rename to querySplitQuery after the migration to SplitQuery is done.
type querySplitQueryV2 struct {
Keyspace string
SQL string
BindVariables map[string]interface{}
SplitColumns []string
SplitCount int64
NumRowsPerQueryPart int64
Algorithm querypb.SplitQueryRequest_Algorithm
}
// SplitQueryV2 is part of the VTGateService interface
// TODO(erez): Rename to SplitQuery after the migration to SplitQuery is done.
func (f *fakeVTGateService) SplitQueryV2(
ctx context.Context,
keyspace string,
sql string,
bindVariables map[string]interface{},
splitColumns []string,
splitCount int64,
numRowsPerQueryPart int64,
algorithm querypb.SplitQueryRequest_Algorithm) ([]*vtgatepb.SplitQueryResponse_Part, error) {
if f.hasError {
return nil, errTestVtGateError
}
if f.panics {
panic(fmt.Errorf("test forced panic"))
}
f.checkCallerID(ctx, "SplitQuery")
query := &querySplitQueryV2{
Keyspace: keyspace,
SQL: sql,
BindVariables: bindVariables,
SplitColumns: splitColumns,
SplitCount: splitCount,
NumRowsPerQueryPart: numRowsPerQueryPart,
Algorithm: algorithm,
}
if !reflect.DeepEqual(query, splitQueryV2Request) {
f.t.Errorf("SplitQuery has wrong input: got %#v wanted %#v", query, splitQueryRequest)
}
return splitQueryResult, nil
}
// GetSrvKeyspace is part of the VTGateService interface
func (f *fakeVTGateService) GetSrvKeyspace(ctx context.Context, keyspace string) (*topodatapb.SrvKeyspace, error) {
if f.hasError {
return nil, errTestVtGateError
}
if f.panics {
panic(fmt.Errorf("test forced panic"))
}
if keyspace != getSrvKeyspaceKeyspace {
f.t.Errorf("GetSrvKeyspace has wrong input: got %v wanted %v", keyspace, getSrvKeyspaceKeyspace)
}
return getSrvKeyspaceResult, nil
}
// CreateFakeServer returns the fake server for the tests
func CreateFakeServer(t *testing.T) vtgateservice.VTGateService {
return &fakeVTGateService{
t: t,
panics: false,
errorWait: make(chan struct{}),
}
}
// RegisterTestDialProtocol registers a vtgateconn implementation under the "test" protocol
func RegisterTestDialProtocol(impl vtgateconn.Impl) {
vtgateconn.RegisterDialer("test", func(ctx context.Context, address string, timeout time.Duration) (vtgateconn.Impl, error) {
return impl, nil
})
}
// HandlePanic is part of the VTGateService interface
func (f *fakeVTGateService) HandlePanic(err *error) {
if x := recover(); x != nil {
// gRPC 0.13 chokes when you return a streaming error that contains newlines.
*err = fmt.Errorf("uncaught panic: %v, %s", x,
strings.Replace(string(tb.Stack(4)), "\n", ";", -1))
}
}
// TestSuite runs all the tests
func TestSuite(t *testing.T, impl vtgateconn.Impl, fakeServer vtgateservice.VTGateService) {
vtgateconn.RegisterDialer("test", func(ctx context.Context, address string, timeout time.Duration) (vtgateconn.Impl, error) {
return impl, nil
})
conn, err := vtgateconn.DialProtocol(context.Background(), "test", "", 0, "connection_ks")
if err != nil {
t.Fatalf("Got err: %v from vtgateconn.DialProtocol", err)
}
fs := fakeServer.(*fakeVTGateService)
testExecute(t, conn)
testExecuteShards(t, conn)
testExecuteKeyspaceIds(t, conn)
testExecuteKeyRanges(t, conn)
testExecuteEntityIds(t, conn)
testExecuteBatchShards(t, conn)
testExecuteBatchKeyspaceIds(t, conn)
testStreamExecute(t, conn)
testStreamExecuteShards(t, conn)
testStreamExecuteKeyRanges(t, conn)
testStreamExecuteKeyspaceIds(t, conn)
testTxPass(t, conn)
testTxFail(t, conn)
testSplitQuery(t, conn)
testSplitQueryV2(t, conn)
testGetSrvKeyspace(t, conn)
// force a panic at every call, then test that works
fs.panics = true
testBeginPanic(t, conn)
testCommitPanic(t, conn, fs)
testRollbackPanic(t, conn, fs)
testExecutePanic(t, conn)
testExecuteShardsPanic(t, conn)
testExecuteKeyspaceIdsPanic(t, conn)
testExecuteKeyRangesPanic(t, conn)
testExecuteEntityIdsPanic(t, conn)
testExecuteBatchShardsPanic(t, conn)
testExecuteBatchKeyspaceIdsPanic(t, conn)
testStreamExecutePanic(t, conn)
testStreamExecuteShardsPanic(t, conn)
testStreamExecuteKeyRangesPanic(t, conn)
testStreamExecuteKeyspaceIdsPanic(t, conn)
testSplitQueryPanic(t, conn)
testSplitQueryV2Panic(t, conn)
testGetSrvKeyspacePanic(t, conn)
fs.panics = false
}
// TestErrorSuite runs all the tests that expect errors
func TestErrorSuite(t *testing.T, fakeServer vtgateservice.VTGateService) {
conn, err := vtgateconn.DialProtocol(context.Background(), "test", "", 0, "connection_ks")
if err != nil {
t.Fatalf("Got err: %v from vtgateconn.DialProtocol", err)
}
fs := fakeServer.(*fakeVTGateService)
// return an error for every call, make sure they're handled properly
fs.hasError = true
testBeginError(t, conn)
testCommitError(t, conn, fs)
testRollbackError(t, conn, fs)
testExecuteError(t, conn, fs)
testExecuteShardsError(t, conn, fs)
testExecuteKeyspaceIdsError(t, conn, fs)
testExecuteKeyRangesError(t, conn, fs)
testExecuteEntityIdsError(t, conn, fs)
testExecuteBatchShardsError(t, conn, fs)
testExecuteBatchKeyspaceIdsError(t, conn, fs)
testStreamExecuteError(t, conn, fs)
testStreamExecuteShardsError(t, conn, fs)
testStreamExecuteKeyRangesError(t, conn, fs)
testStreamExecuteKeyspaceIdsError(t, conn, fs)
testSplitQueryError(t, conn)
testSplitQueryV2Error(t, conn)
testGetSrvKeyspaceError(t, conn)
fs.hasError = false
}
func expectPanic(t *testing.T, err error) {
expected1 := "test forced panic"
expected2 := "uncaught panic"
if err == nil || !strings.Contains(err.Error(), expected1) || !strings.Contains(err.Error(), expected2) {
t.Fatalf("Expected a panic error with '%v' or '%v' but got: %v", expected1, expected2, err)
}
}
// Verifies the returned error has the properties that we expect.
func verifyError(t *testing.T, err error, method string) {
if err == nil {
t.Errorf("%s was expecting an error, didn't get one", method)
return
}
// verify error code
code := vterrors.RecoverVtErrorCode(err)
if code != expectedCode {
t.Errorf("Unexpected error code from %s: got %v, wanted %v", method, code, expectedCode)
}
// verify error type
if _, ok := err.(*vterrors.VitessError); !ok {
t.Errorf("Unexpected error type from %s: got %v, wanted *vterrors.VitessError", method, reflect.TypeOf(err))
}
verifyErrorString(t, err, method)
}
func verifyErrorString(t *testing.T, err error, method string) {
if err == nil {
t.Errorf("%s was expecting an error, didn't get one", method)
return
}
if !strings.Contains(err.Error(), expectedErrMatch) {
t.Errorf("Unexpected error from %s: got %v, wanted err containing: %v", method, err, errTestVtGateError.Error())
}
}
func testExecute(t *testing.T, conn *vtgateconn.VTGateConn) {
ctx := newContext()
execCase := execMap["request1"]
qr, err := conn.Execute(ctx, execCase.execQuery.SQL, execCase.execQuery.BindVariables, execCase.execQuery.TabletType)
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(qr, execCase.result) {
t.Errorf("Unexpected result from Execute: got\n%#v want\n%#v", qr, execCase.result)
}
_, err = conn.Execute(ctx, "none", nil, topodatapb.TabletType_RDONLY)
want := "no match for: none"
if err == nil || !strings.Contains(err.Error(), want) {
t.Errorf("none request: %v, want %v", err, want)
}
}
func testExecuteError(t *testing.T, conn *vtgateconn.VTGateConn, fake *fakeVTGateService) {
ctx := newContext()
execCase := execMap["errorRequst"]
_, err := conn.Execute(ctx, execCase.execQuery.SQL, execCase.execQuery.BindVariables, execCase.execQuery.TabletType)
verifyError(t, err, "Execute")
}
func testExecutePanic(t *testing.T, conn *vtgateconn.VTGateConn) {
ctx := newContext()
execCase := execMap["request1"]
_, err := conn.Execute(ctx, execCase.execQuery.SQL, execCase.execQuery.BindVariables, execCase.execQuery.TabletType)
expectPanic(t, err)
}
func testExecuteShards(t *testing.T, conn *vtgateconn.VTGateConn) {
ctx := newContext()
execCase := execMap["request1"]
qr, err := conn.ExecuteShards(ctx, execCase.shardQuery.SQL, execCase.shardQuery.Keyspace, execCase.shardQuery.Shards, execCase.shardQuery.BindVariables, execCase.shardQuery.TabletType)
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(qr, execCase.result) {
t.Errorf("Unexpected result from Execute: got %+v want %+v", qr, execCase.result)
}
_, err = conn.ExecuteShards(ctx, "none", "", []string{}, nil, topodatapb.TabletType_RDONLY)
want := "no match for: none"
if err == nil || !strings.Contains(err.Error(), want) {
t.Errorf("none request: %v, want %v", err, want)
}
}
func testExecuteShardsError(t *testing.T, conn *vtgateconn.VTGateConn, fake *fakeVTGateService) {
ctx := newContext()
execCase := execMap["errorRequst"]
_, err := conn.ExecuteShards(ctx, execCase.shardQuery.SQL, execCase.shardQuery.Keyspace, execCase.shardQuery.Shards, execCase.shardQuery.BindVariables, execCase.shardQuery.TabletType)
verifyError(t, err, "ExecuteShards")
}
func testExecuteShardsPanic(t *testing.T, conn *vtgateconn.VTGateConn) {
ctx := newContext()
execCase := execMap["request1"]
_, err := conn.ExecuteShards(ctx, execCase.execQuery.SQL, "ks", []string{"1", "2"}, execCase.execQuery.BindVariables, execCase.execQuery.TabletType)
expectPanic(t, err)
}
func testExecuteKeyspaceIds(t *testing.T, conn *vtgateconn.VTGateConn) {
ctx := newContext()
execCase := execMap["request1"]
qr, err := conn.ExecuteKeyspaceIds(ctx, execCase.keyspaceIDQuery.SQL, execCase.keyspaceIDQuery.Keyspace, execCase.keyspaceIDQuery.KeyspaceIds, execCase.keyspaceIDQuery.BindVariables, execCase.keyspaceIDQuery.TabletType)
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(qr, execCase.result) {
t.Errorf("Unexpected result from Execute: got %+v want %+v", qr, execCase.result)
}
_, err = conn.ExecuteKeyspaceIds(ctx, "none", "", [][]byte{}, nil, topodatapb.TabletType_REPLICA)
want := "no match for: none"
if err == nil || !strings.Contains(err.Error(), want) {
t.Errorf("none request: %v, want %v", err, want)
}
}
func testExecuteKeyspaceIdsError(t *testing.T, conn *vtgateconn.VTGateConn, fake *fakeVTGateService) {
ctx := newContext()
execCase := execMap["errorRequst"]
_, err := conn.ExecuteKeyspaceIds(ctx, execCase.keyspaceIDQuery.SQL, execCase.keyspaceIDQuery.Keyspace, execCase.keyspaceIDQuery.KeyspaceIds, execCase.keyspaceIDQuery.BindVariables, execCase.keyspaceIDQuery.TabletType)
verifyError(t, err, "ExecuteKeyspaceIds")
}
func testExecuteKeyspaceIdsPanic(t *testing.T, conn *vtgateconn.VTGateConn) {
ctx := newContext()
execCase := execMap["request1"]
_, err := conn.ExecuteKeyspaceIds(ctx, execCase.keyspaceIDQuery.SQL, execCase.keyspaceIDQuery.Keyspace, execCase.keyspaceIDQuery.KeyspaceIds, execCase.keyspaceIDQuery.BindVariables, execCase.keyspaceIDQuery.TabletType)
expectPanic(t, err)
}
func testExecuteKeyRanges(t *testing.T, conn *vtgateconn.VTGateConn) {
ctx := newContext()
execCase := execMap["request1"]
qr, err := conn.ExecuteKeyRanges(ctx, execCase.keyRangeQuery.SQL, execCase.keyRangeQuery.Keyspace, execCase.keyRangeQuery.KeyRanges, execCase.keyRangeQuery.BindVariables, execCase.keyRangeQuery.TabletType)
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(qr, execCase.result) {
t.Errorf("Unexpected result from Execute: got %+v want %+v", qr, execCase.result)
}
_, err = conn.ExecuteKeyRanges(ctx, "none", "", []*topodatapb.KeyRange{}, nil, topodatapb.TabletType_REPLICA)
want := "no match for: none"
if err == nil || !strings.Contains(err.Error(), want) {
t.Errorf("none request: %v, want %v", err, want)
}
}
func testExecuteKeyRangesError(t *testing.T, conn *vtgateconn.VTGateConn, fake *fakeVTGateService) {
ctx := newContext()
execCase := execMap["errorRequst"]
_, err := conn.ExecuteKeyRanges(ctx, execCase.keyRangeQuery.SQL, execCase.keyRangeQuery.Keyspace, execCase.keyRangeQuery.KeyRanges, execCase.keyRangeQuery.BindVariables, execCase.keyRangeQuery.TabletType)
verifyError(t, err, "ExecuteKeyRanges")
}
func testExecuteKeyRangesPanic(t *testing.T, conn *vtgateconn.VTGateConn) {
ctx := newContext()
execCase := execMap["request1"]
_, err := conn.ExecuteKeyRanges(ctx, execCase.keyRangeQuery.SQL, execCase.keyRangeQuery.Keyspace, execCase.keyRangeQuery.KeyRanges, execCase.keyRangeQuery.BindVariables, execCase.keyRangeQuery.TabletType)
expectPanic(t, err)
}
func testExecuteEntityIds(t *testing.T, conn *vtgateconn.VTGateConn) {
ctx := newContext()
execCase := execMap["request1"]
qr, err := conn.ExecuteEntityIds(ctx, execCase.entityIdsQuery.SQL, execCase.entityIdsQuery.Keyspace, execCase.entityIdsQuery.EntityColumnName, execCase.entityIdsQuery.EntityKeyspaceIDs, execCase.entityIdsQuery.BindVariables, execCase.entityIdsQuery.TabletType)
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(qr, execCase.result) {
t.Errorf("Unexpected result from Execute: got %+v want %+v", qr, execCase.result)
}
_, err = conn.ExecuteEntityIds(ctx, "none", "", "", []*vtgatepb.ExecuteEntityIdsRequest_EntityId{}, nil, topodatapb.TabletType_REPLICA)
want := "no match for: none"
if err == nil || !strings.Contains(err.Error(), want) {
t.Errorf("none request: %v, want %v", err, want)
}
}
func testExecuteEntityIdsError(t *testing.T, conn *vtgateconn.VTGateConn, fake *fakeVTGateService) {
ctx := newContext()
execCase := execMap["errorRequst"]
_, err := conn.ExecuteEntityIds(ctx, execCase.entityIdsQuery.SQL, execCase.entityIdsQuery.Keyspace, execCase.entityIdsQuery.EntityColumnName, execCase.entityIdsQuery.EntityKeyspaceIDs, execCase.entityIdsQuery.BindVariables, execCase.entityIdsQuery.TabletType)
verifyError(t, err, "ExecuteEntityIds")
}
func testExecuteEntityIdsPanic(t *testing.T, conn *vtgateconn.VTGateConn) {
ctx := newContext()
execCase := execMap["request1"]
_, err := conn.ExecuteEntityIds(ctx, execCase.entityIdsQuery.SQL, execCase.entityIdsQuery.Keyspace, execCase.entityIdsQuery.EntityColumnName, execCase.entityIdsQuery.EntityKeyspaceIDs, execCase.entityIdsQuery.BindVariables, execCase.entityIdsQuery.TabletType)
expectPanic(t, err)
}
func testExecuteBatchShards(t *testing.T, conn *vtgateconn.VTGateConn) {