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
|
//
// Test the listShards command by adding stand-alone and replica-set shards to a cluster
//
(function() {
'use strict';
function checkShardName(shardName, shardsArray) {
var found = false;
shardsArray.forEach(function(shardObj) {
if (shardObj._id === shardName) {
found = true;
return;
}
});
return found;
}
var shardTest =
new ShardingTest({name: 'listShardsTest', shards: 1, mongos: 1, other: {useHostname: true}});
var mongos = shardTest.s0;
var res = mongos.adminCommand('listShards');
assert.commandWorked(res, 'listShards command failed');
var shardsArray = res.shards;
assert.eq(shardsArray.length, 1);
// add standalone mongod
var standaloneShard = MongoRunner.runMongod({useHostName: true, shardsvr: ""});
res = shardTest.admin.runCommand({addShard: standaloneShard.host, name: 'standalone'});
assert.commandWorked(res, 'addShard command failed');
res = mongos.adminCommand('listShards');
assert.commandWorked(res, 'listShards command failed');
shardsArray = res.shards;
assert.eq(shardsArray.length, 2);
assert(checkShardName('standalone', shardsArray),
'listShards command didn\'t return standalone shard: ' + tojson(shardsArray));
// add replica set named 'repl'
var rs1 = new ReplSetTest({name: 'repl', nodes: 1, useHostName: true, nodeOptions: {shardsvr: ""}});
rs1.startSet();
rs1.initiate();
res = shardTest.admin.runCommand({addShard: rs1.getURL()});
assert.commandWorked(res, 'addShard command failed');
res = mongos.adminCommand('listShards');
assert.commandWorked(res, 'listShards command failed');
shardsArray = res.shards;
assert.eq(shardsArray.length, 3);
assert(checkShardName('repl', shardsArray),
'listShards command didn\'t return replica set shard: ' + tojson(shardsArray));
// remove 'repl' shard
assert.soon(function() {
var res = shardTest.admin.runCommand({removeShard: 'repl'});
assert.commandWorked(res, 'removeShard command failed');
return res.state === 'completed';
}, 'failed to remove the replica set shard');
res = mongos.adminCommand('listShards');
assert.commandWorked(res, 'listShards command failed');
shardsArray = res.shards;
assert.eq(shardsArray.length, 2);
assert(!checkShardName('repl', shardsArray),
'listShards command returned removed replica set shard: ' + tojson(shardsArray));
rs1.stopSet();
shardTest.stop();
MongoRunner.stopMongod(standaloneShard);
})();
|