@@ -12,6 +12,8 @@ import {
12
12
ALL_SERVER_VERSIONS ,
13
13
ALL_TOPOLOGIES
14
14
} from './enums' ;
15
+ import { CliServiceProvider } from '../../service-provider-server' ;
16
+ import util from 'util' ;
15
17
16
18
describe ( 'ReplicaSet' , ( ) => {
17
19
describe ( 'help' , ( ) => {
@@ -49,14 +51,14 @@ describe('ReplicaSet', () => {
49
51
} ) ;
50
52
} ) ;
51
53
52
- describe ( 'commands ' , ( ) => {
54
+ describe ( 'unit ' , ( ) => {
53
55
let mongo : Mongo ;
54
56
let serviceProvider : StubbedInstance < ServiceProvider > ;
55
57
let rs : ReplicaSet ;
56
58
let bus : StubbedInstance < EventEmitter > ;
57
59
let internalState : ShellInternalState ;
58
60
59
- const findResolvesWith = ( expectedResult ) => {
61
+ const findResolvesWith = ( expectedResult ) : void => {
60
62
const findCursor = stubInterface < ServiceProviderCursor > ( ) ;
61
63
findCursor . next . resolves ( expectedResult ) ;
62
64
serviceProvider . find . returns ( findCursor ) ;
@@ -133,7 +135,7 @@ describe('ReplicaSet', () => {
133
135
expect ( serviceProvider . runCommandWithCheck ) . to . have . been . calledWith (
134
136
ADMIN_DB ,
135
137
{
136
- replSetReconfig : 1
138
+ replSetGetConfig : 1
137
139
}
138
140
) ;
139
141
} ) ;
@@ -585,4 +587,151 @@ describe('ReplicaSet', () => {
585
587
} ) ;
586
588
} ) ;
587
589
} ) ;
590
+
591
+ xdescribe ( 'integration' , ( ) => {
592
+ const port0 = 27017 ;
593
+ const host = '127.0.0.1' ;
594
+ const replId = 'rs0' ;
595
+ const connectionString = `mongodb://${ host } :${ port0 } ` ; // startTestServer();
596
+ const cfg = {
597
+ _id : replId ,
598
+ members : [
599
+ { _id : 0 , host : `${ host } :${ port0 } ` , priority : 1 } ,
600
+ { _id : 1 , host : `${ host } :${ port0 + 1 } ` , priority : 0 } ,
601
+ { _id : 2 , host : `${ host } :${ port0 + 2 } ` , priority : 0 }
602
+ ]
603
+ } ;
604
+ let serviceProvider : CliServiceProvider ;
605
+ let internalState ;
606
+ let mongo ;
607
+ let rs ;
608
+
609
+ const delay = util . promisify ( setTimeout ) ;
610
+ const ensureMaster = async ( timeout ) : Promise < void > => {
611
+ while ( ! ( await rs . isMaster ( ) ) . ismaster ) {
612
+ if ( timeout > 8000 ) {
613
+ return expect . fail ( `Waited for ${ host } :${ port0 } to become master, never happened` ) ;
614
+ }
615
+ await delay ( timeout ) ;
616
+ timeout *= 2 ; // try again but wait double
617
+ }
618
+ expect ( ( await rs . conf ( ) ) . members . length ) . to . equal ( 3 ) ;
619
+ } ;
620
+
621
+ before ( async ( ) => {
622
+ serviceProvider = await CliServiceProvider . connect ( connectionString ) ;
623
+ internalState = new ShellInternalState ( serviceProvider ) ;
624
+ mongo = internalState . currentDb . getMongo ( ) ;
625
+ rs = new ReplicaSet ( mongo ) ;
626
+
627
+ // check replset uninitialized
628
+ try {
629
+ await rs . status ( ) ;
630
+ } catch ( error ) {
631
+ // eslint-disable-next-line no-console
632
+ console . log ( 'WARNING: Initializing new replset' ) ;
633
+ expect ( error . message ) . to . include ( 'no replset config' ) ;
634
+ const result = await rs . initiate ( cfg ) ;
635
+ expect ( result . ok ) . to . equal ( 1 ) ;
636
+ return expect ( result . $clusterTime ) . to . not . be . undefined ;
637
+ }
638
+ // eslint-disable-next-line no-console
639
+ console . log ( 'WARNING: Using existing replset' ) ;
640
+ } ) ;
641
+
642
+ beforeEach ( async ( ) => {
643
+ await ensureMaster ( 1000 ) ;
644
+ } ) ;
645
+
646
+ after ( ( ) => {
647
+ return serviceProvider . close ( true ) ;
648
+ } ) ;
649
+
650
+ describe ( 'replica set info' , ( ) => {
651
+ it ( 'returns the status' , async ( ) => {
652
+ const result = await rs . status ( ) ;
653
+ expect ( result . set ) . to . equal ( replId ) ;
654
+ } ) ;
655
+ it ( 'returns the config' , async ( ) => {
656
+ const result = await rs . conf ( ) ;
657
+ expect ( result . _id ) . to . equal ( replId ) ;
658
+ } ) ;
659
+ it ( 'is connected to master' , async ( ) => {
660
+ const result = await rs . isMaster ( ) ;
661
+ expect ( result . ismaster ) . to . be . true ;
662
+ } ) ;
663
+ it ( 'returns StatsResult for print secondary replication info' , async ( ) => {
664
+ const result = await rs . printSecondaryReplicationInfo ( ) ;
665
+ expect ( result . type ) . to . equal ( 'StatsResult' ) ;
666
+ } ) ;
667
+ it ( 'returns StatsResult for print replication info' , async ( ) => {
668
+ const result = await rs . printSecondaryReplicationInfo ( ) ;
669
+ expect ( result . type ) . to . equal ( 'StatsResult' ) ;
670
+ } ) ;
671
+ it ( 'returns data for db.getReplicationInfo' , async ( ) => {
672
+ const result = await rs . _mongo . getDB ( 'any' ) . getReplicationInfo ( ) ;
673
+ expect ( Object . keys ( result ) ) . to . include ( 'logSizeMB' ) ;
674
+ } ) ;
675
+ } ) ;
676
+ describe ( 'reconfig' , ( ) => {
677
+ it ( 'reconfig with one less secondary' , async ( ) => {
678
+ const newcfg = {
679
+ _id : replId ,
680
+ members : [ cfg . members [ 0 ] , cfg . members [ 1 ] ]
681
+ } ;
682
+ const version = ( await rs . conf ( ) ) . version ;
683
+ const result = await rs . reconfig ( newcfg ) ;
684
+ expect ( result . ok ) . to . equal ( 1 ) ;
685
+ const status = await rs . conf ( ) ;
686
+ expect ( status . members . length ) . to . equal ( 2 ) ;
687
+ expect ( status . version ) . to . equal ( version + 1 ) ;
688
+ } ) ;
689
+ afterEach ( async ( ) => {
690
+ await rs . reconfig ( cfg ) ;
691
+ const status = await rs . conf ( ) ;
692
+ expect ( status . members . length ) . to . equal ( 3 ) ;
693
+ } ) ;
694
+ } ) ;
695
+
696
+ describe ( 'add member' , ( ) => {
697
+ it ( 'adds a regular member to the config' , async ( ) => {
698
+ const version = ( await rs . conf ( ) ) . version ;
699
+ const result = await rs . add ( `${ host } :${ port0 + 3 } ` ) ;
700
+ expect ( result . ok ) . to . equal ( 1 ) ;
701
+ const conf = await rs . conf ( ) ;
702
+ expect ( conf . members . length ) . to . equal ( 4 ) ;
703
+ expect ( conf . version ) . to . equal ( version + 1 ) ;
704
+ } ) ;
705
+ it ( 'adds a arbiter member to the config' , async ( ) => {
706
+ const version = ( await rs . conf ( ) ) . version ;
707
+ const result = await rs . addArb ( `${ host } :${ port0 + 3 } ` ) ;
708
+ expect ( result . ok ) . to . equal ( 1 ) ;
709
+ const conf = await rs . conf ( ) ;
710
+ expect ( conf . members . length ) . to . equal ( 4 ) ;
711
+ expect ( conf . members [ 3 ] . arbiterOnly ) . to . equal ( true ) ;
712
+ expect ( conf . version ) . to . equal ( version + 1 ) ;
713
+ } ) ;
714
+ afterEach ( async ( ) => {
715
+ await rs . reconfig ( cfg ) ;
716
+ const status = await rs . conf ( ) ;
717
+ expect ( status . members . length ) . to . equal ( 3 ) ;
718
+ } ) ;
719
+ } ) ;
720
+
721
+ describe ( 'remove member' , ( ) => {
722
+ it ( 'removes a member of the config' , async ( ) => {
723
+ const version = ( await rs . conf ( ) ) . version ;
724
+ const result = await rs . remove ( `${ host } :${ port0 + 2 } ` ) ;
725
+ expect ( result . ok ) . to . equal ( 1 ) ;
726
+ const conf = await rs . conf ( ) ;
727
+ expect ( conf . members . length ) . to . equal ( 2 ) ;
728
+ expect ( conf . version ) . to . equal ( version + 1 ) ;
729
+ } ) ;
730
+ afterEach ( async ( ) => {
731
+ await rs . reconfig ( cfg ) ;
732
+ const status = await rs . conf ( ) ;
733
+ expect ( status . members . length ) . to . equal ( 3 ) ;
734
+ } ) ;
735
+ } ) ;
736
+ } ) ;
588
737
} ) ;
0 commit comments