forked from vitessio/vitess
/
test_agent_rpc.go
1277 lines (1089 loc) · 45.3 KB
/
test_agent_rpc.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 2014, 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 agentrpctest
import (
"fmt"
"io"
"reflect"
"strings"
"sync"
"testing"
"time"
"golang.org/x/net/context"
"github.com/youtube/vitess/go/sqltypes"
"github.com/youtube/vitess/go/vt/hook"
"github.com/youtube/vitess/go/vt/logutil"
"github.com/youtube/vitess/go/vt/mysqlctl/tmutils"
"github.com/youtube/vitess/go/vt/tabletmanager"
"github.com/youtube/vitess/go/vt/tabletmanager/tmclient"
querypb "github.com/youtube/vitess/go/vt/proto/query"
replicationdatapb "github.com/youtube/vitess/go/vt/proto/replicationdata"
tabletmanagerdatapb "github.com/youtube/vitess/go/vt/proto/tabletmanagerdata"
topodatapb "github.com/youtube/vitess/go/vt/proto/topodata"
)
// fakeRPCAgent implements tabletmanager.RPCAgent and fills in all
// possible values in all APIs
type fakeRPCAgent struct {
t *testing.T
panics bool
// slow if true will let Ping() sleep and effectively not respond to an RPC.
slow bool
// mu guards accesses of "slow".
mu sync.Mutex
}
func (fra *fakeRPCAgent) setSlow(slow bool) {
fra.mu.Lock()
fra.slow = slow
fra.mu.Unlock()
}
// NewFakeRPCAgent returns a fake tabletmanager.RPCAgent that's just a mirror.
func NewFakeRPCAgent(t *testing.T) tabletmanager.RPCAgent {
return &fakeRPCAgent{
t: t,
}
}
// The way this test is organized is a repetition of:
// - static test data for a call
// - implementation of the tabletmanager.RPCAgent method for fakeRPCAgent
// - static test method for the call (client side)
// for each possible method of the interface.
// This makes the implementations all in the same spot.
func compare(t *testing.T, name string, got, want interface{}) {
if !reflect.DeepEqual(got, want) {
t.Errorf("Unexpected %v: got %v expected %v", name, got, want)
}
}
func compareBool(t *testing.T, name string, got bool) {
if !got {
t.Errorf("Unexpected %v: got false expected true", name)
}
}
func compareError(t *testing.T, name string, err error, got, want interface{}) {
if err != nil {
t.Errorf("%v failed: %v", name, err)
} else {
compare(t, name+" result", got, want)
}
}
var testLogString = "test log"
func logStuff(logger logutil.Logger, count int) {
for i := 0; i < count; i++ {
logger.Infof(testLogString)
}
}
func compareLoggedStuff(t *testing.T, name string, stream logutil.EventStream, count int) error {
for i := 0; i < count; i++ {
le, err := stream.Recv()
if err != nil {
t.Errorf("No logged value for %v/%v", name, i)
return err
}
if le.Value != testLogString {
t.Errorf("Unexpected log response for %v: got %v expected %v", name, le.Value, testLogString)
}
}
_, err := stream.Recv()
if err == nil {
t.Fatalf("log channel wasn't closed for %v", name)
}
if err == io.EOF {
return nil
}
return err
}
func expectRPCWrapPanic(t *testing.T, err error) {
expected := "RPCWrap caught panic"
if err == nil || !strings.Contains(err.Error(), expected) {
t.Fatalf("Expected a panic error with '%v' but got: %v", expected, err)
}
}
func expectRPCWrapLockPanic(t *testing.T, err error) {
expected := "RPCWrapLock caught panic"
if err == nil || !strings.Contains(err.Error(), expected) {
t.Fatalf("Expected a panic error with '%v' but got: %v", expected, err)
}
}
func expectRPCWrapLockActionPanic(t *testing.T, err error) {
expected := "RPCWrapLockAction caught panic"
if err == nil || !strings.Contains(err.Error(), expected) {
t.Fatalf("Expected a panic error with '%v' but got: %v", expected, err)
}
}
//
// Various read-only methods
//
func (fra *fakeRPCAgent) Ping(ctx context.Context, args string) string {
if fra.panics {
panic(fmt.Errorf("test-triggered panic"))
}
fra.mu.Lock()
slow := fra.slow
fra.mu.Unlock()
if slow {
time.Sleep(time.Minute)
}
return args
}
func agentRPCTestPing(ctx context.Context, t *testing.T, client tmclient.TabletManagerClient, tablet *topodatapb.Tablet) {
err := client.Ping(ctx, tablet)
if err != nil {
t.Errorf("Ping failed: %v", err)
}
}
func agentRPCTestPingPanic(ctx context.Context, t *testing.T, client tmclient.TabletManagerClient, tablet *topodatapb.Tablet) {
err := client.Ping(ctx, tablet)
expectRPCWrapPanic(t, err)
}
// agentRPCTestDialExpiredContext verifies that
// the context returns the right DeadlineExceeded Err() for
// RPCs failed due to an expired context before .Dial().
func agentRPCTestDialExpiredContext(ctx context.Context, t *testing.T, client tmclient.TabletManagerClient, tablet *topodatapb.Tablet) {
// Using a timeout of 0 here such that .Dial() will fail immediately.
expiredCtx, cancel := context.WithTimeout(ctx, 0)
defer cancel()
err := client.Ping(expiredCtx, tablet)
if err == nil {
t.Fatal("agentRPCTestDialExpiredContext: RPC with expired context did not fail")
}
// The context was already expired when we created it. Here we only verify that it returns the expected error.
select {
case <-expiredCtx.Done():
if err := expiredCtx.Err(); err != context.DeadlineExceeded {
t.Errorf("agentRPCTestDialExpiredContext: got %v want context.DeadlineExceeded", err)
}
default:
t.Errorf("agentRPCTestDialExpiredContext: context.Done() not closed")
}
}
// agentRPCTestRPCTimeout verifies that
// the context returns the right DeadlineExceeded Err() for
// RPCs failed due to an expired context during execution.
func agentRPCTestRPCTimeout(ctx context.Context, t *testing.T, client tmclient.TabletManagerClient, tablet *topodatapb.Tablet, fakeAgent *fakeRPCAgent) {
// We must use a timeout > 0 such that the context deadline hasn't expired
// yet in grpctmclient.Client.dial().
// NOTE: This might still race e.g. when test execution takes too long the
// context will be expired in dial() already. In such cases coverage
// will be reduced but the test will not flake.
shortCtx, cancel := context.WithTimeout(ctx, 10*time.Millisecond)
defer cancel()
fakeAgent.setSlow(true)
defer func() { fakeAgent.setSlow(false) }()
err := client.Ping(shortCtx, tablet)
if err == nil {
t.Fatal("agentRPCTestRPCTimeout: RPC with expired context did not fail")
}
select {
case <-shortCtx.Done():
if err := shortCtx.Err(); err != context.DeadlineExceeded {
t.Errorf("agentRPCTestRPCTimeout: got %v want context.DeadlineExceeded", err)
}
default:
t.Errorf("agentRPCTestRPCTimeout: context.Done() not closed")
}
}
var testGetSchemaTables = []string{"table1", "table2"}
var testGetSchemaExcludeTables = []string{"etable1", "etable2", "etable3"}
var testGetSchemaReply = &tabletmanagerdatapb.SchemaDefinition{
DatabaseSchema: "CREATE DATABASE {{.DatabaseName}}",
TableDefinitions: []*tabletmanagerdatapb.TableDefinition{
{
Name: "table_name",
Schema: "create table_name",
Columns: []string{"col1", "col2"},
PrimaryKeyColumns: []string{"col1"},
Type: tmutils.TableView,
DataLength: 12,
RowCount: 6,
},
{
Name: "table_name2",
Schema: "create table_name2",
Columns: []string{"col1"},
PrimaryKeyColumns: []string{"col1"},
Type: tmutils.TableBaseTable,
DataLength: 12,
RowCount: 6,
},
},
Version: "xxx",
}
func (fra *fakeRPCAgent) GetSchema(ctx context.Context, tables, excludeTables []string, includeViews bool) (*tabletmanagerdatapb.SchemaDefinition, error) {
if fra.panics {
panic(fmt.Errorf("test-triggered panic"))
}
compare(fra.t, "GetSchema tables", tables, testGetSchemaTables)
compare(fra.t, "GetSchema excludeTables", excludeTables, testGetSchemaExcludeTables)
compareBool(fra.t, "GetSchema includeViews", includeViews)
return testGetSchemaReply, nil
}
func agentRPCTestGetSchema(ctx context.Context, t *testing.T, client tmclient.TabletManagerClient, tablet *topodatapb.Tablet) {
result, err := client.GetSchema(ctx, tablet, testGetSchemaTables, testGetSchemaExcludeTables, true)
compareError(t, "GetSchema", err, result, testGetSchemaReply)
}
func agentRPCTestGetSchemaPanic(ctx context.Context, t *testing.T, client tmclient.TabletManagerClient, tablet *topodatapb.Tablet) {
_, err := client.GetSchema(ctx, tablet, testGetSchemaTables, testGetSchemaExcludeTables, true)
expectRPCWrapPanic(t, err)
}
var testGetPermissionsReply = &tabletmanagerdatapb.Permissions{
UserPermissions: []*tabletmanagerdatapb.UserPermission{
{
Host: "host1",
User: "user1",
PasswordChecksum: 666,
Privileges: map[string]string{
"create": "yes",
"delete": "no",
},
},
},
DbPermissions: []*tabletmanagerdatapb.DbPermission{
{
Host: "host2",
Db: "db1",
User: "user2",
Privileges: map[string]string{
"create": "no",
"delete": "yes",
},
},
},
}
func (fra *fakeRPCAgent) GetPermissions(ctx context.Context) (*tabletmanagerdatapb.Permissions, error) {
if fra.panics {
panic(fmt.Errorf("test-triggered panic"))
}
return testGetPermissionsReply, nil
}
func agentRPCTestGetPermissions(ctx context.Context, t *testing.T, client tmclient.TabletManagerClient, tablet *topodatapb.Tablet) {
result, err := client.GetPermissions(ctx, tablet)
compareError(t, "GetPermissions", err, result, testGetPermissionsReply)
}
func agentRPCTestGetPermissionsPanic(ctx context.Context, t *testing.T, client tmclient.TabletManagerClient, tablet *topodatapb.Tablet) {
_, err := client.GetPermissions(ctx, tablet)
expectRPCWrapPanic(t, err)
}
//
// Various read-write methods
//
var testSetReadOnlyExpectedValue bool
func (fra *fakeRPCAgent) SetReadOnly(ctx context.Context, rdonly bool) error {
if fra.panics {
panic(fmt.Errorf("test-triggered panic"))
}
if rdonly != testSetReadOnlyExpectedValue {
fra.t.Errorf("Wrong SetReadOnly value: got %v expected %v", rdonly, testSetReadOnlyExpectedValue)
}
return nil
}
func agentRPCTestSetReadOnly(ctx context.Context, t *testing.T, client tmclient.TabletManagerClient, tablet *topodatapb.Tablet) {
testSetReadOnlyExpectedValue = true
err := client.SetReadOnly(ctx, tablet)
if err != nil {
t.Errorf("SetReadOnly failed: %v", err)
}
testSetReadOnlyExpectedValue = false
err = client.SetReadWrite(ctx, tablet)
if err != nil {
t.Errorf("SetReadWrite failed: %v", err)
}
}
func agentRPCTestSetReadOnlyPanic(ctx context.Context, t *testing.T, client tmclient.TabletManagerClient, tablet *topodatapb.Tablet) {
err := client.SetReadOnly(ctx, tablet)
expectRPCWrapLockActionPanic(t, err)
}
var testChangeTypeValue = topodatapb.TabletType_REPLICA
func (fra *fakeRPCAgent) ChangeType(ctx context.Context, tabletType topodatapb.TabletType) error {
if fra.panics {
panic(fmt.Errorf("test-triggered panic"))
}
compare(fra.t, "ChangeType tabletType", tabletType, testChangeTypeValue)
return nil
}
func agentRPCTestChangeType(ctx context.Context, t *testing.T, client tmclient.TabletManagerClient, tablet *topodatapb.Tablet) {
err := client.ChangeType(ctx, tablet, testChangeTypeValue)
if err != nil {
t.Errorf("ChangeType failed: %v", err)
}
}
func agentRPCTestChangeTypePanic(ctx context.Context, t *testing.T, client tmclient.TabletManagerClient, tablet *topodatapb.Tablet) {
err := client.ChangeType(ctx, tablet, testChangeTypeValue)
expectRPCWrapLockActionPanic(t, err)
}
var testSleepDuration = time.Minute
func (fra *fakeRPCAgent) Sleep(ctx context.Context, duration time.Duration) {
if fra.panics {
panic(fmt.Errorf("test-triggered panic"))
}
compare(fra.t, "Sleep duration", duration, testSleepDuration)
}
func agentRPCTestSleep(ctx context.Context, t *testing.T, client tmclient.TabletManagerClient, tablet *topodatapb.Tablet) {
err := client.Sleep(ctx, tablet, testSleepDuration)
if err != nil {
t.Errorf("Sleep failed: %v", err)
}
}
func agentRPCTestSleepPanic(ctx context.Context, t *testing.T, client tmclient.TabletManagerClient, tablet *topodatapb.Tablet) {
err := client.Sleep(ctx, tablet, testSleepDuration)
expectRPCWrapLockActionPanic(t, err)
}
var testExecuteHookHook = &hook.Hook{
Name: "captain hook",
Parameters: []string{"param1", "param2"},
ExtraEnv: map[string]string{
"boat": "blue",
"sea": "red",
},
}
var testExecuteHookHookResult = &hook.HookResult{
ExitStatus: hook.HOOK_STAT_FAILED,
Stdout: "out",
Stderr: "err",
}
func (fra *fakeRPCAgent) ExecuteHook(ctx context.Context, hk *hook.Hook) *hook.HookResult {
if fra.panics {
panic(fmt.Errorf("test-triggered panic"))
}
compare(fra.t, "ExecuteHook hook", hk, testExecuteHookHook)
return testExecuteHookHookResult
}
func agentRPCTestExecuteHook(ctx context.Context, t *testing.T, client tmclient.TabletManagerClient, tablet *topodatapb.Tablet) {
hr, err := client.ExecuteHook(ctx, tablet, testExecuteHookHook)
compareError(t, "ExecuteHook", err, hr, testExecuteHookHookResult)
}
func agentRPCTestExecuteHookPanic(ctx context.Context, t *testing.T, client tmclient.TabletManagerClient, tablet *topodatapb.Tablet) {
_, err := client.ExecuteHook(ctx, tablet, testExecuteHookHook)
expectRPCWrapLockActionPanic(t, err)
}
var testRefreshStateCalled = false
func (fra *fakeRPCAgent) RefreshState(ctx context.Context) {
if fra.panics {
panic(fmt.Errorf("test-triggered panic"))
}
if testRefreshStateCalled {
fra.t.Errorf("RefreshState called multiple times?")
}
testRefreshStateCalled = true
}
func agentRPCTestRefreshState(ctx context.Context, t *testing.T, client tmclient.TabletManagerClient, tablet *topodatapb.Tablet) {
err := client.RefreshState(ctx, tablet)
if err != nil {
t.Errorf("RefreshState failed: %v", err)
}
if !testRefreshStateCalled {
t.Errorf("RefreshState didn't call the server side")
}
}
func agentRPCTestRefreshStatePanic(ctx context.Context, t *testing.T, client tmclient.TabletManagerClient, tablet *topodatapb.Tablet) {
err := client.RefreshState(ctx, tablet)
expectRPCWrapLockActionPanic(t, err)
}
func (fra *fakeRPCAgent) RunHealthCheck(ctx context.Context) {
if fra.panics {
panic(fmt.Errorf("test-triggered panic"))
}
}
var testIgnoreHealthErrorValue = ".*"
func (fra *fakeRPCAgent) IgnoreHealthError(ctx context.Context, pattern string) error {
if fra.panics {
panic(fmt.Errorf("test-triggered panic"))
}
compare(fra.t, "IgnoreHealthError pattern", pattern, testIgnoreHealthErrorValue)
return nil
}
func agentRPCTestRunHealthCheck(ctx context.Context, t *testing.T, client tmclient.TabletManagerClient, tablet *topodatapb.Tablet) {
err := client.RunHealthCheck(ctx, tablet)
if err != nil {
t.Errorf("RunHealthCheck failed: %v", err)
}
}
func agentRPCTestRunHealthCheckPanic(ctx context.Context, t *testing.T, client tmclient.TabletManagerClient, tablet *topodatapb.Tablet) {
err := client.RunHealthCheck(ctx, tablet)
expectRPCWrapPanic(t, err)
}
func agentRPCTestIgnoreHealthError(ctx context.Context, t *testing.T, client tmclient.TabletManagerClient, tablet *topodatapb.Tablet) {
err := client.IgnoreHealthError(ctx, tablet, testIgnoreHealthErrorValue)
if err != nil {
t.Errorf("IgnoreHealthError failed: %v", err)
}
}
func agentRPCTestIgnoreHealthErrorPanic(ctx context.Context, t *testing.T, client tmclient.TabletManagerClient, tablet *topodatapb.Tablet) {
err := client.IgnoreHealthError(ctx, tablet, testIgnoreHealthErrorValue)
expectRPCWrapPanic(t, err)
}
var testReloadSchemaCalled = false
func (fra *fakeRPCAgent) ReloadSchema(ctx context.Context, waitPosition string) error {
if fra.panics {
panic(fmt.Errorf("test-triggered panic"))
}
if testReloadSchemaCalled {
fra.t.Errorf("ReloadSchema called multiple times?")
}
testReloadSchemaCalled = true
return nil
}
func agentRPCTestReloadSchema(ctx context.Context, t *testing.T, client tmclient.TabletManagerClient, tablet *topodatapb.Tablet) {
err := client.ReloadSchema(ctx, tablet, "")
if err != nil {
t.Errorf("ReloadSchema failed: %v", err)
}
if !testReloadSchemaCalled {
t.Errorf("ReloadSchema didn't call the server side")
}
}
func agentRPCTestReloadSchemaPanic(ctx context.Context, t *testing.T, client tmclient.TabletManagerClient, tablet *topodatapb.Tablet) {
err := client.ReloadSchema(ctx, tablet, "")
expectRPCWrapPanic(t, err)
}
var testPreflightSchema = []string{"change table add table cloth"}
var testSchemaChangeResult = []*tabletmanagerdatapb.SchemaChangeResult{
{
BeforeSchema: testGetSchemaReply,
AfterSchema: testGetSchemaReply,
},
}
func (fra *fakeRPCAgent) PreflightSchema(ctx context.Context, changes []string) ([]*tabletmanagerdatapb.SchemaChangeResult, error) {
if fra.panics {
panic(fmt.Errorf("test-triggered panic"))
}
compare(fra.t, "PreflightSchema result", changes, testPreflightSchema)
return testSchemaChangeResult, nil
}
func agentRPCTestPreflightSchema(ctx context.Context, t *testing.T, client tmclient.TabletManagerClient, tablet *topodatapb.Tablet) {
scr, err := client.PreflightSchema(ctx, tablet, testPreflightSchema)
compareError(t, "PreflightSchema", err, scr, testSchemaChangeResult)
}
func agentRPCTestPreflightSchemaPanic(ctx context.Context, t *testing.T, client tmclient.TabletManagerClient, tablet *topodatapb.Tablet) {
_, err := client.PreflightSchema(ctx, tablet, testPreflightSchema)
expectRPCWrapLockActionPanic(t, err)
}
var testSchemaChange = &tmutils.SchemaChange{
SQL: "alter table add fruit basket",
Force: true,
AllowReplication: true,
BeforeSchema: testGetSchemaReply,
AfterSchema: testGetSchemaReply,
}
func (fra *fakeRPCAgent) ApplySchema(ctx context.Context, change *tmutils.SchemaChange) (*tabletmanagerdatapb.SchemaChangeResult, error) {
if fra.panics {
panic(fmt.Errorf("test-triggered panic"))
}
compare(fra.t, "ApplySchema change", change, testSchemaChange)
return testSchemaChangeResult[0], nil
}
func agentRPCTestApplySchema(ctx context.Context, t *testing.T, client tmclient.TabletManagerClient, tablet *topodatapb.Tablet) {
scr, err := client.ApplySchema(ctx, tablet, testSchemaChange)
compareError(t, "ApplySchema", err, scr, testSchemaChangeResult[0])
}
func agentRPCTestApplySchemaPanic(ctx context.Context, t *testing.T, client tmclient.TabletManagerClient, tablet *topodatapb.Tablet) {
_, err := client.ApplySchema(ctx, tablet, testSchemaChange)
expectRPCWrapLockActionPanic(t, err)
}
var testExecuteFetchQuery = "fetch this"
var testExecuteFetchMaxRows = 100
var testExecuteFetchResult = &querypb.QueryResult{
Fields: []*querypb.Field{
{
Name: "column1",
Type: sqltypes.Blob,
},
{
Name: "column2",
Type: sqltypes.Datetime,
},
},
RowsAffected: 10,
InsertId: 32,
Rows: []*querypb.Row{
{
Lengths: []int64{
3,
-1,
},
Values: []byte{
'A', 'B', 'C',
},
},
},
}
func (fra *fakeRPCAgent) ExecuteFetchAsDba(ctx context.Context, query string, dbName string, maxrows int, disableBinlogs bool, reloadSchema bool) (*querypb.QueryResult, error) {
if fra.panics {
panic(fmt.Errorf("test-triggered panic"))
}
compare(fra.t, "ExecuteFetchAsDba query", query, testExecuteFetchQuery)
compare(fra.t, "ExecuteFetchAsDba maxrows", maxrows, testExecuteFetchMaxRows)
compareBool(fra.t, "ExecuteFetchAsDba disableBinlogs", disableBinlogs)
compareBool(fra.t, "ExecuteFetchAsDba reloadSchema", reloadSchema)
return testExecuteFetchResult, nil
}
func (fra *fakeRPCAgent) ExecuteFetchAsApp(ctx context.Context, query string, maxrows int) (*querypb.QueryResult, error) {
if fra.panics {
panic(fmt.Errorf("test-triggered panic"))
}
compare(fra.t, "ExecuteFetchAsApp query", query, testExecuteFetchQuery)
compare(fra.t, "ExecuteFetchAsApp maxrows", maxrows, testExecuteFetchMaxRows)
return testExecuteFetchResult, nil
}
func agentRPCTestExecuteFetch(ctx context.Context, t *testing.T, client tmclient.TabletManagerClient, tablet *topodatapb.Tablet) {
qr, err := client.ExecuteFetchAsDba(ctx, tablet, testExecuteFetchQuery, testExecuteFetchMaxRows, true, true)
compareError(t, "ExecuteFetchAsDba", err, qr, testExecuteFetchResult)
qr, err = client.ExecuteFetchAsApp(ctx, tablet, testExecuteFetchQuery, testExecuteFetchMaxRows)
compareError(t, "ExecuteFetchAsApp", err, qr, testExecuteFetchResult)
}
func agentRPCTestExecuteFetchPanic(ctx context.Context, t *testing.T, client tmclient.TabletManagerClient, tablet *topodatapb.Tablet) {
_, err := client.ExecuteFetchAsDba(ctx, tablet, testExecuteFetchQuery, testExecuteFetchMaxRows, true, false)
expectRPCWrapPanic(t, err)
_, err = client.ExecuteFetchAsApp(ctx, tablet, testExecuteFetchQuery, testExecuteFetchMaxRows)
expectRPCWrapPanic(t, err)
}
//
// Replication related methods
//
var testReplicationStatus = &replicationdatapb.Status{
Position: "MariaDB/1-345-789",
SlaveIoRunning: true,
SlaveSqlRunning: true,
SecondsBehindMaster: 654,
MasterHost: "master.host",
MasterPort: 3366,
MasterConnectRetry: 12,
}
func (fra *fakeRPCAgent) SlaveStatus(ctx context.Context) (*replicationdatapb.Status, error) {
if fra.panics {
panic(fmt.Errorf("test-triggered panic"))
}
return testReplicationStatus, nil
}
func agentRPCTestSlaveStatus(ctx context.Context, t *testing.T, client tmclient.TabletManagerClient, tablet *topodatapb.Tablet) {
rs, err := client.SlaveStatus(ctx, tablet)
compareError(t, "SlaveStatus", err, rs, testReplicationStatus)
}
func agentRPCTestSlaveStatusPanic(ctx context.Context, t *testing.T, client tmclient.TabletManagerClient, tablet *topodatapb.Tablet) {
_, err := client.SlaveStatus(ctx, tablet)
expectRPCWrapPanic(t, err)
}
var testReplicationPosition = "MariaDB/5-456-890"
func (fra *fakeRPCAgent) MasterPosition(ctx context.Context) (string, error) {
if fra.panics {
panic(fmt.Errorf("test-triggered panic"))
}
return testReplicationPosition, nil
}
func agentRPCTestMasterPosition(ctx context.Context, t *testing.T, client tmclient.TabletManagerClient, tablet *topodatapb.Tablet) {
rs, err := client.MasterPosition(ctx, tablet)
compareError(t, "MasterPosition", err, rs, testReplicationPosition)
}
func agentRPCTestMasterPositionPanic(ctx context.Context, t *testing.T, client tmclient.TabletManagerClient, tablet *topodatapb.Tablet) {
_, err := client.MasterPosition(ctx, tablet)
expectRPCWrapPanic(t, err)
}
var testStopSlaveCalled = false
func (fra *fakeRPCAgent) StopSlave(ctx context.Context) error {
if fra.panics {
panic(fmt.Errorf("test-triggered panic"))
}
testStopSlaveCalled = true
return nil
}
func agentRPCTestStopSlave(ctx context.Context, t *testing.T, client tmclient.TabletManagerClient, tablet *topodatapb.Tablet) {
err := client.StopSlave(ctx, tablet)
compareError(t, "StopSlave", err, true, testStopSlaveCalled)
}
func agentRPCTestStopSlavePanic(ctx context.Context, t *testing.T, client tmclient.TabletManagerClient, tablet *topodatapb.Tablet) {
err := client.StopSlave(ctx, tablet)
expectRPCWrapLockPanic(t, err)
}
var testStopSlaveMinimumWaitTime = time.Hour
func (fra *fakeRPCAgent) StopSlaveMinimum(ctx context.Context, position string, waitTime time.Duration) (string, error) {
if fra.panics {
panic(fmt.Errorf("test-triggered panic"))
}
compare(fra.t, "StopSlaveMinimum position", position, testReplicationPosition)
compare(fra.t, "StopSlaveMinimum waitTime", waitTime, testStopSlaveMinimumWaitTime)
return testReplicationPositionReturned, nil
}
func agentRPCTestStopSlaveMinimum(ctx context.Context, t *testing.T, client tmclient.TabletManagerClient, tablet *topodatapb.Tablet) {
pos, err := client.StopSlaveMinimum(ctx, tablet, testReplicationPosition, testStopSlaveMinimumWaitTime)
compareError(t, "StopSlaveMinimum", err, pos, testReplicationPositionReturned)
}
func agentRPCTestStopSlaveMinimumPanic(ctx context.Context, t *testing.T, client tmclient.TabletManagerClient, tablet *topodatapb.Tablet) {
_, err := client.StopSlaveMinimum(ctx, tablet, testReplicationPosition, testStopSlaveMinimumWaitTime)
expectRPCWrapLockPanic(t, err)
}
var testStartSlaveCalled = false
func (fra *fakeRPCAgent) StartSlave(ctx context.Context) error {
if fra.panics {
panic(fmt.Errorf("test-triggered panic"))
}
testStartSlaveCalled = true
return nil
}
func agentRPCTestStartSlave(ctx context.Context, t *testing.T, client tmclient.TabletManagerClient, tablet *topodatapb.Tablet) {
err := client.StartSlave(ctx, tablet)
compareError(t, "StartSlave", err, true, testStartSlaveCalled)
}
func agentRPCTestStartSlavePanic(ctx context.Context, t *testing.T, client tmclient.TabletManagerClient, tablet *topodatapb.Tablet) {
err := client.StartSlave(ctx, tablet)
expectRPCWrapLockPanic(t, err)
}
var testTabletExternallyReparentedCalled = false
func (fra *fakeRPCAgent) TabletExternallyReparented(ctx context.Context, externalID string) error {
if fra.panics {
panic(fmt.Errorf("test-triggered panic"))
}
testTabletExternallyReparentedCalled = true
return nil
}
func agentRPCTestTabletExternallyReparented(ctx context.Context, t *testing.T, client tmclient.TabletManagerClient, tablet *topodatapb.Tablet) {
err := client.TabletExternallyReparented(ctx, tablet, "")
compareError(t, "TabletExternallyReparented", err, true, testTabletExternallyReparentedCalled)
}
func agentRPCTestTabletExternallyReparentedPanic(ctx context.Context, t *testing.T, client tmclient.TabletManagerClient, tablet *topodatapb.Tablet) {
err := client.TabletExternallyReparented(ctx, tablet, "")
expectRPCWrapLockPanic(t, err)
}
var testGetSlavesResult = []string{"slave1", "slave2"}
func (fra *fakeRPCAgent) GetSlaves(ctx context.Context) ([]string, error) {
if fra.panics {
panic(fmt.Errorf("test-triggered panic"))
}
return testGetSlavesResult, nil
}
func agentRPCTestGetSlaves(ctx context.Context, t *testing.T, client tmclient.TabletManagerClient, tablet *topodatapb.Tablet) {
s, err := client.GetSlaves(ctx, tablet)
compareError(t, "GetSlaves", err, s, testGetSlavesResult)
}
func agentRPCTestGetSlavesPanic(ctx context.Context, t *testing.T, client tmclient.TabletManagerClient, tablet *topodatapb.Tablet) {
_, err := client.GetSlaves(ctx, tablet)
expectRPCWrapPanic(t, err)
}
var testBlpPosition = &tabletmanagerdatapb.BlpPosition{
Uid: 73,
Position: "testReplicationPosition",
}
var testWaitBlpPositionWaitTime = time.Hour
var testWaitBlpPositionCalled = false
func (fra *fakeRPCAgent) WaitBlpPosition(ctx context.Context, blpPosition *tabletmanagerdatapb.BlpPosition, waitTime time.Duration) error {
if fra.panics {
panic(fmt.Errorf("test-triggered panic"))
}
compare(fra.t, "WaitBlpPosition blpPosition", blpPosition, testBlpPosition)
compare(fra.t, "WaitBlpPosition waitTime", waitTime, testWaitBlpPositionWaitTime)
testWaitBlpPositionCalled = true
return nil
}
func agentRPCTestWaitBlpPosition(ctx context.Context, t *testing.T, client tmclient.TabletManagerClient, tablet *topodatapb.Tablet) {
err := client.WaitBlpPosition(ctx, tablet, testBlpPosition, testWaitBlpPositionWaitTime)
compareError(t, "WaitBlpPosition", err, true, testWaitBlpPositionCalled)
}
func agentRPCTestWaitBlpPositionPanic(ctx context.Context, t *testing.T, client tmclient.TabletManagerClient, tablet *topodatapb.Tablet) {
err := client.WaitBlpPosition(ctx, tablet, testBlpPosition, testWaitBlpPositionWaitTime)
expectRPCWrapLockPanic(t, err)
}
var testBlpPositionList = []*tabletmanagerdatapb.BlpPosition{
{
Uid: 12,
Position: "testBlpPosition",
},
}
func (fra *fakeRPCAgent) StopBlp(ctx context.Context) ([]*tabletmanagerdatapb.BlpPosition, error) {
if fra.panics {
panic(fmt.Errorf("test-triggered panic"))
}
return testBlpPositionList, nil
}
func agentRPCTestStopBlp(ctx context.Context, t *testing.T, client tmclient.TabletManagerClient, tablet *topodatapb.Tablet) {
bpl, err := client.StopBlp(ctx, tablet)
compareError(t, "StopBlp", err, bpl, testBlpPositionList)
}
func agentRPCTestStopBlpPanic(ctx context.Context, t *testing.T, client tmclient.TabletManagerClient, tablet *topodatapb.Tablet) {
_, err := client.StopBlp(ctx, tablet)
expectRPCWrapLockPanic(t, err)
}
var testStartBlpCalled = false
func (fra *fakeRPCAgent) StartBlp(ctx context.Context) error {
if fra.panics {
panic(fmt.Errorf("test-triggered panic"))
}
testStartBlpCalled = true
return nil
}
func agentRPCTestStartBlp(ctx context.Context, t *testing.T, client tmclient.TabletManagerClient, tablet *topodatapb.Tablet) {
err := client.StartBlp(ctx, tablet)
compareError(t, "StartBlp", err, true, testStartBlpCalled)
}
func agentRPCTestStartBlpPanic(ctx context.Context, t *testing.T, client tmclient.TabletManagerClient, tablet *topodatapb.Tablet) {
err := client.StartBlp(ctx, tablet)
expectRPCWrapLockPanic(t, err)
}
var testRunBlpUntilWaitTime = 3 * time.Minute
func (fra *fakeRPCAgent) RunBlpUntil(ctx context.Context, bpl []*tabletmanagerdatapb.BlpPosition, waitTime time.Duration) (string, error) {
if fra.panics {
panic(fmt.Errorf("test-triggered panic"))
}
compare(fra.t, "RunBlpUntil bpl", bpl, testBlpPositionList)
compare(fra.t, "RunBlpUntil waitTime", waitTime, testRunBlpUntilWaitTime)
return testReplicationPosition, nil
}
func agentRPCTestRunBlpUntil(ctx context.Context, t *testing.T, client tmclient.TabletManagerClient, tablet *topodatapb.Tablet) {
rp, err := client.RunBlpUntil(ctx, tablet, testBlpPositionList, testRunBlpUntilWaitTime)
compareError(t, "RunBlpUntil", err, rp, testReplicationPosition)
}
func agentRPCTestRunBlpUntilPanic(ctx context.Context, t *testing.T, client tmclient.TabletManagerClient, tablet *topodatapb.Tablet) {
_, err := client.RunBlpUntil(ctx, tablet, testBlpPositionList, testRunBlpUntilWaitTime)
expectRPCWrapLockPanic(t, err)
}
//
// Reparenting related functions
//
var testResetReplicationCalled = false
func (fra *fakeRPCAgent) ResetReplication(ctx context.Context) error {
if fra.panics {
panic(fmt.Errorf("test-triggered panic"))
}
testResetReplicationCalled = true
return nil
}
func agentRPCTestResetReplication(ctx context.Context, t *testing.T, client tmclient.TabletManagerClient, tablet *topodatapb.Tablet) {
err := client.ResetReplication(ctx, tablet)
compareError(t, "ResetReplication", err, true, testResetReplicationCalled)
}
func agentRPCTestResetReplicationPanic(ctx context.Context, t *testing.T, client tmclient.TabletManagerClient, tablet *topodatapb.Tablet) {
err := client.ResetReplication(ctx, tablet)
expectRPCWrapLockActionPanic(t, err)
}
func (fra *fakeRPCAgent) InitMaster(ctx context.Context) (string, error) {
if fra.panics {
panic(fmt.Errorf("test-triggered panic"))
}
return testReplicationPosition, nil
}
func agentRPCTestInitMaster(ctx context.Context, t *testing.T, client tmclient.TabletManagerClient, tablet *topodatapb.Tablet) {
rp, err := client.InitMaster(ctx, tablet)
compareError(t, "InitMaster", err, rp, testReplicationPosition)
}
func agentRPCTestInitMasterPanic(ctx context.Context, t *testing.T, client tmclient.TabletManagerClient, tablet *topodatapb.Tablet) {
_, err := client.InitMaster(ctx, tablet)
expectRPCWrapLockActionPanic(t, err)
}
var testPopulateReparentJournalCalled = false
var testTimeCreatedNS int64 = 4569900
var testActionName = "TestActionName"
var testMasterAlias = &topodatapb.TabletAlias{
Cell: "ce",
Uid: 372,
}
func (fra *fakeRPCAgent) PopulateReparentJournal(ctx context.Context, timeCreatedNS int64, actionName string, masterAlias *topodatapb.TabletAlias, position string) error {
if fra.panics {
panic(fmt.Errorf("test-triggered panic"))
}
compare(fra.t, "PopulateReparentJournal timeCreatedNS", timeCreatedNS, testTimeCreatedNS)
compare(fra.t, "PopulateReparentJournal actionName", actionName, testActionName)
compare(fra.t, "PopulateReparentJournal masterAlias", masterAlias, testMasterAlias)
compare(fra.t, "PopulateReparentJournal pos", position, testReplicationPosition)
testPopulateReparentJournalCalled = true
return nil
}
func agentRPCTestPopulateReparentJournal(ctx context.Context, t *testing.T, client tmclient.TabletManagerClient, tablet *topodatapb.Tablet) {
err := client.PopulateReparentJournal(ctx, tablet, testTimeCreatedNS, testActionName, testMasterAlias, testReplicationPosition)
compareError(t, "PopulateReparentJournal", err, true, testPopulateReparentJournalCalled)
}
func agentRPCTestPopulateReparentJournalPanic(ctx context.Context, t *testing.T, client tmclient.TabletManagerClient, tablet *topodatapb.Tablet) {
err := client.PopulateReparentJournal(ctx, tablet, testTimeCreatedNS, testActionName, testMasterAlias, testReplicationPosition)
expectRPCWrapPanic(t, err)
}
var testInitSlaveCalled = false
func (fra *fakeRPCAgent) InitSlave(ctx context.Context, parent *topodatapb.TabletAlias, position string, timeCreatedNS int64) error {
if fra.panics {
panic(fmt.Errorf("test-triggered panic"))
}
compare(fra.t, "InitSlave parent", parent, testMasterAlias)
compare(fra.t, "InitSlave pos", position, testReplicationPosition)
compare(fra.t, "InitSlave timeCreatedNS", timeCreatedNS, testTimeCreatedNS)
testInitSlaveCalled = true
return nil
}
func agentRPCTestInitSlave(ctx context.Context, t *testing.T, client tmclient.TabletManagerClient, tablet *topodatapb.Tablet) {
err := client.InitSlave(ctx, tablet, testMasterAlias, testReplicationPosition, testTimeCreatedNS)
compareError(t, "InitSlave", err, true, testInitSlaveCalled)
}
func agentRPCTestInitSlavePanic(ctx context.Context, t *testing.T, client tmclient.TabletManagerClient, tablet *topodatapb.Tablet) {
err := client.InitSlave(ctx, tablet, testMasterAlias, testReplicationPosition, testTimeCreatedNS)
expectRPCWrapLockActionPanic(t, err)
}
func (fra *fakeRPCAgent) DemoteMaster(ctx context.Context) (string, error) {
if fra.panics {
panic(fmt.Errorf("test-triggered panic"))
}
return testReplicationPosition, nil
}
func agentRPCTestDemoteMaster(ctx context.Context, t *testing.T, client tmclient.TabletManagerClient, tablet *topodatapb.Tablet) {
rp, err := client.DemoteMaster(ctx, tablet)
compareError(t, "DemoteMaster", err, rp, testReplicationPosition)
}
func agentRPCTestDemoteMasterPanic(ctx context.Context, t *testing.T, client tmclient.TabletManagerClient, tablet *topodatapb.Tablet) {
_, err := client.DemoteMaster(ctx, tablet)
expectRPCWrapLockActionPanic(t, err)
}
var testReplicationPositionReturned = "MariaDB/5-567-3456"
func (fra *fakeRPCAgent) PromoteSlaveWhenCaughtUp(ctx context.Context, position string) (string, error) {
if fra.panics {
panic(fmt.Errorf("test-triggered panic"))
}
compare(fra.t, "PromoteSlaveWhenCaughtUp pos", position, testReplicationPosition)
return testReplicationPositionReturned, nil
}
func agentRPCTestPromoteSlaveWhenCaughtUp(ctx context.Context, t *testing.T, client tmclient.TabletManagerClient, tablet *topodatapb.Tablet) {
rp, err := client.PromoteSlaveWhenCaughtUp(ctx, tablet, testReplicationPosition)
compareError(t, "PromoteSlaveWhenCaughtUp", err, rp, testReplicationPositionReturned)
}
func agentRPCTestPromoteSlaveWhenCaughtUpPanic(ctx context.Context, t *testing.T, client tmclient.TabletManagerClient, tablet *topodatapb.Tablet) {
_, err := client.PromoteSlaveWhenCaughtUp(ctx, tablet, testReplicationPosition)
expectRPCWrapLockActionPanic(t, err)
}
var testSlaveWasPromotedCalled = false
func (fra *fakeRPCAgent) SlaveWasPromoted(ctx context.Context) error {
if fra.panics {
panic(fmt.Errorf("test-triggered panic"))
}
testSlaveWasPromotedCalled = true
return nil
}