summaryrefslogtreecommitdiff
path: root/jstests/sharding/listDatabases.js
blob: f5b046c26b714c36c806b8fd87f09af39df2099b (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
(function() {
    'use strict';
    var test = new ShardingTest({shards: 1, mongos: 1, other: {chunkSize: 1}});

    var mongos = test.s0;
    var mongod = test.shard0;

    var res;
    var dbArray;

    // grab the config db instance by name
    var getDBSection = function(dbsArray, dbToFind) {
        for (var pos in dbsArray) {
            if (dbsArray[pos].name && dbsArray[pos].name === dbToFind)
                return dbsArray[pos];
        }
        return null;
    };

    // Function to verify information for a database entry in listDatabases.
    var dbEntryCheck = function(dbEntry, onConfig) {
        assert.neq(null, dbEntry);
        assert.neq(null, dbEntry.sizeOnDisk);
        assert.eq(false, dbEntry.empty);

        // Check against shards
        var shards = dbEntry.shards;
        assert(shards);
        assert((shards["config"] && onConfig) || (!shards["config"] && !onConfig));
    };

    // Non-config-server db checks.
    {
        assert.writeOK(mongos.getDB("blah").foo.insert({_id: 1}));
        assert.writeOK(mongos.getDB("foo").foo.insert({_id: 1}));
        assert.writeOK(mongos.getDB("raw").foo.insert({_id: 1}));

        res = mongos.adminCommand("listDatabases");
        dbArray = res.databases;

        dbEntryCheck(getDBSection(dbArray, "blah"), false);
        dbEntryCheck(getDBSection(dbArray, "foo"), false);
        dbEntryCheck(getDBSection(dbArray, "raw"), false);
    }

    // Local db is never returned.
    {
        res = mongos.adminCommand("listDatabases");
        dbArray = res.databases;

        assert(!getDBSection(dbArray, 'local'));
    }

    // Admin and config are always reported on the config shard.
    {
        assert.writeOK(mongos.getDB("admin").test.insert({_id: 1}));
        assert.writeOK(mongos.getDB("config").test.insert({_id: 1}));

        res = mongos.adminCommand("listDatabases");
        dbArray = res.databases;

        dbEntryCheck(getDBSection(dbArray, "config"), true);
        dbEntryCheck(getDBSection(dbArray, "admin"), true);
    }

    // Config db can be present on config shard and on other shards.
    {
        mongod.getDB("config").foo.insert({_id: 1});

        res = mongos.adminCommand("listDatabases");
        dbArray = res.databases;

        var entry = getDBSection(dbArray, "config");
        dbEntryCheck(entry, true);
        assert(entry["shards"]);
        assert.eq(Object.keys(entry["shards"]).length, 2);
    }

    // Admin db is only reported on the config shard, never on other shards.
    {
        mongod.getDB("admin").foo.insert({_id: 1});

        res = mongos.adminCommand("listDatabases");
        dbArray = res.databases;

        var entry = getDBSection(dbArray, "admin");
        dbEntryCheck(entry, true);
        assert(entry["shards"]);
        assert.eq(Object.keys(entry["shards"]).length, 1);
    }

    test.stop();
})();