summaryrefslogtreecommitdiff
path: root/jstests/sharding/enable_sharding_basic.js
blob: 531a2efe57f0ff7254b45ce16d3a59abcf925f0c (plain)
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
//
// Basic tests for enableSharding command.
//

(function() {
    'use strict';

    var st = new ShardingTest({mongos: 1, shards: 2});

    var mongos = st.s0;

    // enableSharing can run only on mongos.
    assert.commandFailedWithCode(st.d0.getDB('admin').runCommand({enableSharding: 'db'}),
                                 ErrorCodes.CommandNotFound);

    // enableSharing can run only against the admin database.
    assert.commandFailedWithCode(mongos.getDB('test').runCommand({enableSharding: 'db'}),
                                 ErrorCodes.Unauthorized);

    // Can't shard 'config' database.
    assert.commandFailed(mongos.adminCommand({enableSharding: 'config'}));

    // Can't shard 'local' database.
    assert.commandFailed(mongos.adminCommand({enableSharding: 'local'}));

    // Can't shard 'admin' database.
    assert.commandFailed(mongos.adminCommand({enableSharding: 'admin'}));

    // Can't shard db with the name that just differ on case.
    assert.commandWorked(mongos.adminCommand({enableSharding: 'db'}));
    assert.eq(mongos.getDB('config').databases.findOne({_id: 'db'}).partitioned, true);

    assert.commandFailedWithCode(mongos.adminCommand({enableSharding: 'DB'}),
                                 ErrorCodes.DatabaseDifferCase);

    // Can't shard invalid db name.
    assert.commandFailed(mongos.adminCommand({enableSharding: 'a.b'}));
    assert.commandFailed(mongos.adminCommand({enableSharding: ''}));

    // Can't shard already sharded database.
    assert.commandFailedWithCode(mongos.adminCommand({enableSharding: 'db'}),
                                 ErrorCodes.AlreadyInitialized);
    assert.eq(mongos.getDB('config').databases.findOne({_id: 'db'}).partitioned, true);

    // Verify config.databases metadata.
    assert.writeOK(mongos.getDB('unsharded').foo.insert({aKey: "aValue"}));
    assert.eq(mongos.getDB('config').databases.findOne({_id: 'unsharded'}).partitioned, false);
    assert.commandWorked(mongos.adminCommand({enableSharding: 'unsharded'}));
    assert.eq(mongos.getDB('config').databases.findOne({_id: 'unsharded'}).partitioned, true);

    st.stop();

})();