@@ -8,6 +8,7 @@ import { bson, ServiceProvider, Cursor as ServiceProviderCursor } from '@mongosh
8
8
import { EventEmitter } from 'events' ;
9
9
import ShellInternalState from './shell-internal-state' ;
10
10
import { UpdateResult } from './result' ;
11
+ import { CliServiceProvider } from '../../service-provider-server' ;
11
12
12
13
describe ( 'Shard' , ( ) => {
13
14
describe ( 'help' , ( ) => {
@@ -51,7 +52,7 @@ describe('Shard', () => {
51
52
} ) ;
52
53
} ) ;
53
54
} ) ;
54
- describe ( 'commands ' , ( ) => {
55
+ describe ( 'unit ' , ( ) => {
55
56
let mongo : Mongo ;
56
57
let serviceProvider : StubbedInstance < ServiceProvider > ;
57
58
let shard : Shard ;
@@ -1082,4 +1083,131 @@ describe('Shard', () => {
1082
1083
} ) ;
1083
1084
} ) ;
1084
1085
} ) ;
1086
+
1087
+ xdescribe ( 'integration' , ( ) => {
1088
+ let serviceProvider : CliServiceProvider ;
1089
+ let internalState ;
1090
+ let mongo ;
1091
+ let sh ;
1092
+ const dbName = 'test' ;
1093
+ const ns = `${ dbName } .coll` ;
1094
+ const mongosPort = 27017 ;
1095
+ const host = 'localhost' ;
1096
+ const shardId = 'rs-shard0' ;
1097
+ const shardPorts = [ '47017' , '47020' ] ;
1098
+ const connectionString = `mongodb://${ host } :${ mongosPort } ` ; // startTestServer();
1099
+
1100
+ before ( async ( ) => {
1101
+ serviceProvider = await CliServiceProvider . connect ( connectionString ) ;
1102
+ internalState = new ShellInternalState ( serviceProvider ) ;
1103
+ mongo = internalState . currentDb . getMongo ( ) ;
1104
+ sh = new Shard ( mongo ) ;
1105
+
1106
+ // check replset uninitialized
1107
+ let members = await sh . _mongo . getDB ( 'config' ) . getCollection ( 'shards' ) . find ( ) . sort ( { _id : 1 } ) . toArray ( ) ;
1108
+ if ( members . length === 0 ) {
1109
+ // add new shards
1110
+ expect ( ( await sh . addShard ( `${ shardId } -0/${ host } :${ shardPorts [ 0 ] } ` ) ) . shardAdded ) . to . equal ( `${ shardId } -0` ) ;
1111
+ expect ( ( await sh . addShard ( `${ shardId } -1/${ host } :${ shardPorts [ 1 ] } ` ) ) . shardAdded ) . to . equal ( `${ shardId } -1` ) ;
1112
+ members = await sh . _mongo . getDB ( 'config' ) . getCollection ( 'shards' ) . find ( ) . sort ( { _id : 1 } ) . toArray ( ) ;
1113
+ } else {
1114
+ console . log ( 'WARN: shards already added' ) ;
1115
+ }
1116
+ expect ( members . length ) . to . equal ( 2 ) ;
1117
+ await sh . _mongo . getDB ( dbName ) . dropDatabase ( ) ;
1118
+ } ) ;
1119
+
1120
+ after ( ( ) => {
1121
+ return serviceProvider . close ( true ) ;
1122
+ } ) ;
1123
+
1124
+ describe ( 'sharding info' , ( ) => {
1125
+ it ( 'returns the status' , async ( ) => {
1126
+ const result = await sh . status ( ) ;
1127
+ expect ( result . type ) . to . equal ( 'StatsResult' ) ;
1128
+ expect ( Object . keys ( result . value ) ) . to . include . members ( [
1129
+ 'shardingVersion' , 'shards' , 'active mongoses' , 'autosplit' , 'balancer' , 'databases'
1130
+ ] ) ;
1131
+ } ) ;
1132
+ } ) ;
1133
+ describe ( 'turn on sharding' , ( ) => {
1134
+ it ( 'enableSharding for a db' , async ( ) => {
1135
+ expect ( ( await sh . status ( ) ) . value . databases . length ) . to . equal ( 1 ) ;
1136
+ expect ( ( await sh . enableSharding ( dbName ) ) . ok ) . to . equal ( 1 ) ;
1137
+ expect ( ( await sh . status ( ) ) . value . databases . length ) . to . equal ( 2 ) ;
1138
+ } ) ;
1139
+ it ( 'enableSharding for a collection' , async ( ) => {
1140
+ expect ( Object . keys ( ( await sh . status ( ) ) . value . databases [ 0 ] . collections ) . length ) . to . equal ( 0 ) ;
1141
+ expect ( ( await sh . shardCollection ( ns , { key : 1 } ) ) . collectionsharded ) . to . equal ( ns ) ;
1142
+ expect ( ( await sh . status ( ) ) . value . databases [ 0 ] . collections [ ns ] . shardKey ) . to . deep . equal ( { key : 1 } ) ;
1143
+ } ) ;
1144
+ } ) ;
1145
+ describe ( 'autosplit' , ( ) => {
1146
+ it ( 'disables correctly' , async ( ) => {
1147
+ expect ( ( await sh . disableAutoSplit ( ) ) . acknowledged ) . to . equal ( 1 ) ;
1148
+ expect ( ( await sh . status ( ) ) . value . autosplit [ 'Currently enabled' ] ) . to . equal ( 'no' ) ;
1149
+ } ) ;
1150
+ it ( 'enables correctly' , async ( ) => {
1151
+ expect ( ( await sh . enableAutoSplit ( ) ) . acknowledged ) . to . equal ( 1 ) ;
1152
+ expect ( ( await sh . status ( ) ) . value . autosplit [ 'Currently enabled' ] ) . to . equal ( 'yes' ) ;
1153
+ } ) ;
1154
+ } ) ;
1155
+ describe ( 'tags' , ( ) => {
1156
+ it ( 'creates a zone' , async ( ) => {
1157
+ expect ( ( await sh . addShardTag ( `${ shardId } -1` , 'zone1' ) ) . ok ) . to . equal ( 1 ) ;
1158
+ expect ( ( await sh . status ( ) ) . value . shards [ 1 ] . tags ) . to . deep . equal ( [ 'zone1' ] ) ;
1159
+ expect ( ( await sh . addShardToZone ( `${ shardId } -0` , 'zone0' ) ) . ok ) . to . equal ( 1 ) ;
1160
+ expect ( ( await sh . status ( ) ) . value . shards [ 0 ] . tags ) . to . deep . equal ( [ 'zone0' ] ) ;
1161
+ } ) ;
1162
+ it ( 'sets a zone key range' , async ( ) => {
1163
+ expect ( ( await sh . updateZoneKeyRange ( ns , { key : 0 } , { key : 20 } , 'zone1' ) ) . ok ) . to . equal ( 1 ) ;
1164
+ expect ( ( await sh . status ( ) ) . value . databases [ 0 ] . collections [ ns ] . tags [ 0 ] ) . to . deep . equal ( {
1165
+ tag : 'zone1' , min : { key : 0 } , max : { key : 20 }
1166
+ } ) ;
1167
+ expect ( ( await sh . addTagRange ( ns , { key : 21 } , { key : 40 } , 'zone0' ) ) . ok ) . to . equal ( 1 ) ;
1168
+ expect ( ( await sh . status ( ) ) . value . databases [ 0 ] . collections [ ns ] . tags [ 1 ] ) . to . deep . equal ( {
1169
+ tag : 'zone0' , min : { key : 21 } , max : { key : 40 }
1170
+ } ) ;
1171
+ } ) ;
1172
+ it ( 'removes a key range' , async ( ) => {
1173
+ expect ( ( await sh . status ( ) ) . value . databases [ 0 ] . collections [ ns ] . tags . length ) . to . equal ( 2 ) ;
1174
+ expect ( ( await sh . removeRangeFromZone ( ns , { key : 0 } , { key : 20 } ) ) . ok ) . to . equal ( 1 ) ;
1175
+ expect ( ( await sh . status ( ) ) . value . databases [ 0 ] . collections [ ns ] . tags . length ) . to . equal ( 1 ) ;
1176
+ expect ( ( await sh . removeTagRange ( ns , { key : 21 } , { key : 40 } ) ) . ok ) . to . equal ( 1 ) ;
1177
+ expect ( ( await sh . status ( ) ) . value . databases [ 0 ] . collections [ ns ] . tags . length ) . to . equal ( 0 ) ;
1178
+ } ) ;
1179
+ it ( 'removes zones' , async ( ) => {
1180
+ expect ( ( await sh . removeShardFromZone ( `${ shardId } -1` , 'zone1' ) ) . ok ) . to . equal ( 1 ) ;
1181
+ expect ( ( await sh . status ( ) ) . value . shards [ 1 ] . tags ) . to . deep . equal ( [ ] ) ;
1182
+ expect ( ( await sh . removeShardTag ( `${ shardId } -0` , 'zone0' ) ) . ok ) . to . equal ( 1 ) ;
1183
+ expect ( ( await sh . status ( ) ) . value . shards [ 0 ] . tags ) . to . deep . equal ( [ ] ) ;
1184
+ } ) ;
1185
+ } ) ;
1186
+ describe ( 'balancer' , ( ) => {
1187
+ it ( 'reports balancer state' , async ( ) => {
1188
+ expect ( Object . keys ( await sh . isBalancerRunning ( ) ) ) . to . include . members ( [
1189
+ 'mode' , 'inBalancerRound' , 'numBalancerRounds'
1190
+ ] ) ;
1191
+ } ) ;
1192
+ it ( 'stops balancer' , async ( ) => {
1193
+ expect ( ( await sh . stopBalancer ( ) ) . ok ) . to . equal ( 1 ) ;
1194
+ expect ( ( await sh . isBalancerRunning ( ) ) . mode ) . to . equal ( 'off' ) ;
1195
+ } ) ;
1196
+ it ( 'starts balancer' , async ( ) => {
1197
+ expect ( ( await sh . startBalancer ( ) ) . ok ) . to . equal ( 1 ) ;
1198
+ expect ( ( await sh . isBalancerRunning ( ) ) . mode ) . to . equal ( 'full' ) ;
1199
+ } ) ;
1200
+ it ( 'reports state for collection' , async ( ) => {
1201
+ expect ( Object . keys ( await sh . balancerCollectionStatus ( ns ) ) ) . to . include ( 'balancerCompliant' ) ;
1202
+ } ) ;
1203
+ it ( 'disables balancing' , async ( ) => {
1204
+ expect ( ( await sh . disableBalancing ( ns ) ) . acknowledged ) . to . equal ( 1 ) ;
1205
+ expect ( ( await sh . _mongo . getDB ( 'config' ) . getCollection ( 'collections' ) . findOne ( { _id : ns } ) ) . noBalance ) . to . equal ( true ) ;
1206
+ } ) ;
1207
+ it ( 'enables balancing' , async ( ) => {
1208
+ expect ( ( await sh . enableBalancing ( ns ) ) . acknowledged ) . to . equal ( 1 ) ;
1209
+ expect ( ( await sh . _mongo . getDB ( 'config' ) . getCollection ( 'collections' ) . findOne ( { _id : ns } ) ) . noBalance ) . to . equal ( false ) ;
1210
+ } ) ;
1211
+ } ) ;
1212
+ } ) ;
1085
1213
} ) ;
0 commit comments