@@ -21,6 +21,7 @@ import (
21
21
"fmt"
22
22
"sync/atomic"
23
23
"testing"
24
+ "time"
24
25
25
26
"github.com/google/go-cmp/cmp"
26
27
"github.com/stretchr/testify/assert"
@@ -30,8 +31,10 @@ import (
30
31
"vitess.io/vitess/go/vt/external/golib/sqlutils"
31
32
topodatapb "vitess.io/vitess/go/vt/proto/topodata"
32
33
"vitess.io/vitess/go/vt/proto/vttime"
34
+ "vitess.io/vitess/go/vt/topo"
33
35
"vitess.io/vitess/go/vt/topo/memorytopo"
34
36
"vitess.io/vitess/go/vt/topo/topoproto"
37
+ "vitess.io/vitess/go/vt/vtctl/grpcvtctldserver/testutil"
35
38
"vitess.io/vitess/go/vt/vtorc/db"
36
39
"vitess.io/vitess/go/vt/vtorc/inst"
37
40
)
@@ -342,3 +345,259 @@ func TestGetLockAction(t *testing.T) {
342
345
})
343
346
}
344
347
}
348
+
349
+ func TestSetReadOnly (t * testing.T ) {
350
+ tests := []struct {
351
+ name string
352
+ tablet * topodatapb.Tablet
353
+ tmc * testutil.TabletManagerClient
354
+ remoteOpTimeout time.Duration
355
+ errShouldContain string
356
+ }{
357
+ {
358
+ name : "Success" ,
359
+ tablet : tab100 ,
360
+ tmc : & testutil.TabletManagerClient {
361
+ SetReadOnlyResults : map [string ]error {
362
+ "zone-1-0000000100" : nil ,
363
+ },
364
+ },
365
+ }, {
366
+ name : "Failure" ,
367
+ tablet : tab100 ,
368
+ tmc : & testutil.TabletManagerClient {
369
+ SetReadOnlyResults : map [string ]error {
370
+ "zone-1-0000000100" : fmt .Errorf ("testing error" ),
371
+ },
372
+ },
373
+ errShouldContain : "testing error" ,
374
+ }, {
375
+ name : "Timeout" ,
376
+ tablet : tab100 ,
377
+ remoteOpTimeout : 100 * time .Millisecond ,
378
+ tmc : & testutil.TabletManagerClient {
379
+ SetReadOnlyResults : map [string ]error {
380
+ "zone-1-0000000100" : nil ,
381
+ },
382
+ SetReadOnlyDelays : map [string ]time.Duration {
383
+ "zone-1-0000000100" : 200 * time .Millisecond ,
384
+ },
385
+ },
386
+ errShouldContain : "context deadline exceeded" ,
387
+ },
388
+ }
389
+ for _ , tt := range tests {
390
+ t .Run (tt .name , func (t * testing.T ) {
391
+ oldTmc := tmc
392
+ oldRemoteOpTimeout := topo .RemoteOperationTimeout
393
+ defer func () {
394
+ tmc = oldTmc
395
+ topo .RemoteOperationTimeout = oldRemoteOpTimeout
396
+ }()
397
+
398
+ tmc = tt .tmc
399
+ if tt .remoteOpTimeout != 0 {
400
+ topo .RemoteOperationTimeout = tt .remoteOpTimeout
401
+ }
402
+
403
+ err := setReadOnly (context .Background (), tt .tablet )
404
+ if tt .errShouldContain == "" {
405
+ require .NoError (t , err )
406
+ return
407
+ }
408
+ require .ErrorContains (t , err , tt .errShouldContain )
409
+ })
410
+ }
411
+ }
412
+
413
+ func TestTabletUndoDemotePrimary (t * testing.T ) {
414
+ tests := []struct {
415
+ name string
416
+ tablet * topodatapb.Tablet
417
+ tmc * testutil.TabletManagerClient
418
+ remoteOpTimeout time.Duration
419
+ errShouldContain string
420
+ }{
421
+ {
422
+ name : "Success" ,
423
+ tablet : tab100 ,
424
+ tmc : & testutil.TabletManagerClient {
425
+ UndoDemotePrimaryResults : map [string ]error {
426
+ "zone-1-0000000100" : nil ,
427
+ },
428
+ },
429
+ }, {
430
+ name : "Failure" ,
431
+ tablet : tab100 ,
432
+ tmc : & testutil.TabletManagerClient {
433
+ UndoDemotePrimaryResults : map [string ]error {
434
+ "zone-1-0000000100" : fmt .Errorf ("testing error" ),
435
+ },
436
+ },
437
+ errShouldContain : "testing error" ,
438
+ }, {
439
+ name : "Timeout" ,
440
+ tablet : tab100 ,
441
+ remoteOpTimeout : 100 * time .Millisecond ,
442
+ tmc : & testutil.TabletManagerClient {
443
+ UndoDemotePrimaryResults : map [string ]error {
444
+ "zone-1-0000000100" : nil ,
445
+ },
446
+ UndoDemotePrimaryDelays : map [string ]time.Duration {
447
+ "zone-1-0000000100" : 200 * time .Millisecond ,
448
+ },
449
+ },
450
+ errShouldContain : "context deadline exceeded" ,
451
+ },
452
+ }
453
+ for _ , tt := range tests {
454
+ t .Run (tt .name , func (t * testing.T ) {
455
+ oldTmc := tmc
456
+ oldRemoteOpTimeout := topo .RemoteOperationTimeout
457
+ defer func () {
458
+ tmc = oldTmc
459
+ topo .RemoteOperationTimeout = oldRemoteOpTimeout
460
+ }()
461
+
462
+ tmc = tt .tmc
463
+ if tt .remoteOpTimeout != 0 {
464
+ topo .RemoteOperationTimeout = tt .remoteOpTimeout
465
+ }
466
+
467
+ err := tabletUndoDemotePrimary (context .Background (), tt .tablet , false )
468
+ if tt .errShouldContain == "" {
469
+ require .NoError (t , err )
470
+ return
471
+ }
472
+ require .ErrorContains (t , err , tt .errShouldContain )
473
+ })
474
+ }
475
+ }
476
+
477
+ func TestChangeTabletType (t * testing.T ) {
478
+ tests := []struct {
479
+ name string
480
+ tablet * topodatapb.Tablet
481
+ tmc * testutil.TabletManagerClient
482
+ remoteOpTimeout time.Duration
483
+ errShouldContain string
484
+ }{
485
+ {
486
+ name : "Success" ,
487
+ tablet : tab100 ,
488
+ tmc : & testutil.TabletManagerClient {
489
+ ChangeTabletTypeResult : map [string ]error {
490
+ "zone-1-0000000100" : nil ,
491
+ },
492
+ },
493
+ }, {
494
+ name : "Failure" ,
495
+ tablet : tab100 ,
496
+ tmc : & testutil.TabletManagerClient {
497
+ ChangeTabletTypeResult : map [string ]error {
498
+ "zone-1-0000000100" : fmt .Errorf ("testing error" ),
499
+ },
500
+ },
501
+ errShouldContain : "testing error" ,
502
+ }, {
503
+ name : "Timeout" ,
504
+ tablet : tab100 ,
505
+ remoteOpTimeout : 100 * time .Millisecond ,
506
+ tmc : & testutil.TabletManagerClient {
507
+ ChangeTabletTypeResult : map [string ]error {
508
+ "zone-1-0000000100" : nil ,
509
+ },
510
+ ChangeTabletTypeDelays : map [string ]time.Duration {
511
+ "zone-1-0000000100" : 200 * time .Millisecond ,
512
+ },
513
+ },
514
+ errShouldContain : "context deadline exceeded" ,
515
+ },
516
+ }
517
+ for _ , tt := range tests {
518
+ t .Run (tt .name , func (t * testing.T ) {
519
+ oldTmc := tmc
520
+ oldRemoteOpTimeout := topo .RemoteOperationTimeout
521
+ defer func () {
522
+ tmc = oldTmc
523
+ topo .RemoteOperationTimeout = oldRemoteOpTimeout
524
+ }()
525
+
526
+ tmc = tt .tmc
527
+ if tt .remoteOpTimeout != 0 {
528
+ topo .RemoteOperationTimeout = tt .remoteOpTimeout
529
+ }
530
+
531
+ err := changeTabletType (context .Background (), tt .tablet , topodatapb .TabletType_REPLICA , false )
532
+ if tt .errShouldContain == "" {
533
+ require .NoError (t , err )
534
+ return
535
+ }
536
+ require .ErrorContains (t , err , tt .errShouldContain )
537
+ })
538
+ }
539
+ }
540
+
541
+ func TestSetReplicationSource (t * testing.T ) {
542
+ tests := []struct {
543
+ name string
544
+ tablet * topodatapb.Tablet
545
+ tmc * testutil.TabletManagerClient
546
+ remoteOpTimeout time.Duration
547
+ errShouldContain string
548
+ }{
549
+ {
550
+ name : "Success" ,
551
+ tablet : tab100 ,
552
+ tmc : & testutil.TabletManagerClient {
553
+ SetReplicationSourceResults : map [string ]error {
554
+ "zone-1-0000000100" : nil ,
555
+ },
556
+ },
557
+ }, {
558
+ name : "Failure" ,
559
+ tablet : tab100 ,
560
+ tmc : & testutil.TabletManagerClient {
561
+ SetReplicationSourceResults : map [string ]error {
562
+ "zone-1-0000000100" : fmt .Errorf ("testing error" ),
563
+ },
564
+ },
565
+ errShouldContain : "testing error" ,
566
+ }, {
567
+ name : "Timeout" ,
568
+ tablet : tab100 ,
569
+ remoteOpTimeout : 100 * time .Millisecond ,
570
+ tmc : & testutil.TabletManagerClient {
571
+ SetReplicationSourceResults : map [string ]error {
572
+ "zone-1-0000000100" : nil ,
573
+ },
574
+ SetReplicationSourceDelays : map [string ]time.Duration {
575
+ "zone-1-0000000100" : 200 * time .Millisecond ,
576
+ },
577
+ },
578
+ errShouldContain : "context deadline exceeded" ,
579
+ },
580
+ }
581
+ for _ , tt := range tests {
582
+ t .Run (tt .name , func (t * testing.T ) {
583
+ oldTmc := tmc
584
+ oldRemoteOpTimeout := topo .RemoteOperationTimeout
585
+ defer func () {
586
+ tmc = oldTmc
587
+ topo .RemoteOperationTimeout = oldRemoteOpTimeout
588
+ }()
589
+
590
+ tmc = tt .tmc
591
+ if tt .remoteOpTimeout != 0 {
592
+ topo .RemoteOperationTimeout = tt .remoteOpTimeout
593
+ }
594
+
595
+ err := setReplicationSource (context .Background (), tt .tablet , tab101 , false )
596
+ if tt .errShouldContain == "" {
597
+ require .NoError (t , err )
598
+ return
599
+ }
600
+ require .ErrorContains (t , err , tt .errShouldContain )
601
+ })
602
+ }
603
+ }
0 commit comments