summaryrefslogtreecommitdiff
path: root/jstests/replsets/get_replication_info_helper.js
blob: c031fb58779997ddffc0f47403891e0619210fd7 (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
// Tests the output of db.getReplicationInfo() and tests db.printSlaveReplicationInfo().

(function () {
    "use strict";
    var name = "getReplicationInfo";
    var replSet = new ReplSetTest({name: name, nodes: 3, oplogSize: 50});
    var nodes = replSet.nodeList();
    replSet.startSet();
    replSet.initiate();

    var primary = replSet.getPrimary();
    for (var i = 0; i < 100; i++) {
        primary.getDB('test').foo.insert({a:i});
    }
    replSet.awaitReplication();

    var replInfo = primary.getDB('admin').getReplicationInfo();
    var replInfoString = tojson(replInfo);

    assert.eq(50, replInfo.logSizeMB, replInfoString);
    assert.lt(0, replInfo.usedMB, replInfoString);
    assert.lte(0, replInfo.timeDiff, replInfoString);
    assert.lte(0, replInfo.timeDiffHours, replInfoString);
    // Just make sure the following fields exist since it would be hard to predict their values
    assert(replInfo.tFirst, replInfoString);
    assert(replInfo.tLast, replInfoString);
    assert(replInfo.now, replInfoString);

    // calling this function with and without a primary, should provide sufficient code coverage
    // to catch any JS errors
    var mongo = startParallelShell("db.getSiblingDB('admin').printSlaveReplicationInfo();",
                                   primary.port);
    mongo();
    assert.soon(function() {
        return rawMongoProgramOutput().match("behind the primary");
    });

    // get to a primaryless state
    for (i in replSet.liveNodes.slaves) {
        var secondary = replSet.liveNodes.slaves[i];
        secondary.getDB('admin').runCommand({replSetFreeze: 120});
    }
    try {
        primary.getDB('admin').runCommand({replSetStepDown: 120, force: true});
    }
    catch (e) {}

    mongo = startParallelShell("db.getSiblingDB('admin').printSlaveReplicationInfo();",
                               primary.port);
    mongo();
    assert.soon(function() {
        return rawMongoProgramOutput().match("behind the freshest");
    });

})();