summaryrefslogtreecommitdiff
path: root/jstests/hooks/run_validate_collections.js
blob: 49131c9ebe94b96a532772266635d1d66688ffa4 (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
// Runner for validateCollections that runs full validation on all collections when loaded into
// the mongo shell.
'use strict';

(function() {
    assert.eq(typeof db, 'object', 'Invalid `db` object, is the shell connected to a mongod?');
    load('jstests/hooks/validate_collections.js');  // For validateCollections.

    function getDirectConnections(conn) {
        // If conn does not point to a repl set, then this function returns [conn].
        const res = conn.adminCommand({isMaster: 1});
        const connections = [];

        if (res.hasOwnProperty('hosts')) {
            for (let hostString of res.hosts) {
                connections.push(new Mongo(hostString));
            }
        } else {
            connections.push(conn);
        }

        return connections;
    }

    function getConfigConnStr() {
        const shardMap = db.adminCommand({getShardMap: 1});
        if (!shardMap.hasOwnProperty('map')) {
            throw new Error('Expected getShardMap() to return an object a "map" field: ' +
                            tojson(shardMap));
        }

        const map = shardMap.map;

        if (!map.hasOwnProperty('config')) {
            throw new Error('Expected getShardMap().map to have a "config" field: ' + tojson(map));
        }

        return map.config;
    }

    function isMongos() {
        return db.isMaster().msg === 'isdbgrid';
    }

    function getServerList() {
        const serverList = [];

        if (isMongos()) {
            // We're connected to a sharded cluster through a mongos.

            // 1) Add all the config servers to the server list.
            const configConnStr = getConfigConnStr();
            const configServerReplSetConn = new Mongo(configConnStr);
            serverList.push(...getDirectConnections(configServerReplSetConn));

            // 2) Add shard members to the server list.
            const configDB = db.getSiblingDB('config');
            const cursor = configDB.shards.find();

            while (cursor.hasNext()) {
                const shard = cursor.next();
                const shardReplSetConn = new Mongo(shard.host);
                serverList.push(...getDirectConnections(shardReplSetConn));
            }
        } else {
            // We're connected to a mongod.
            serverList.push(...getDirectConnections(db.getMongo()));
        }

        return serverList;
    }

    const serverList = getServerList();
    for (let server of serverList) {
        print('Running validate() on ' + server.host);
        server.setSlaveOk();
        jsTest.authenticate(server);

        const dbNames = server.getDBNames();
        for (let dbName of dbNames) {
            if (!validateCollections(server.getDB(dbName), {full: true})) {
                throw new Error('Collection validation failed');
            }
        }
    }
})();