summaryrefslogtreecommitdiff
path: root/jstests/libs/sessions_collection.js
blob: ba6d9b684c4aec4005a78e06b41617c269f4abab (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
// Helpers for testing the logical sessions collection.

/**
 * Validates that the sessions collection exists if we expect it to,
 * and has a TTL index on the lastUse field, if we expect it to.
 */
function validateSessionsCollection(conn, collectionExists, indexExists, assertIfNotExists = true) {
    var config = conn.getDB("config");

    var info = config.getCollectionInfos({name: "system.sessions"});
    var size = collectionExists ? 1 : 0;

    if (assertIfNotExists) {
        assert.eq(info.length, size);
    } else {
        return (info.length != size);
    }

    var indexes = config.system.sessions.getIndexes();
    var found = false;
    for (var i = 0; i < indexes.length; i++) {
        var entry = indexes[i];
        if (entry["name"] == "lsidTTLIndex") {
            found = true;

            assert.eq(entry["ns"], "config.system.sessions");
            assert.eq(entry["key"], {"lastUse": 1});
            assert(entry.hasOwnProperty("expireAfterSeconds"));
        }
    }

    if (assertIfNotExists) {
        if (indexExists) {
            assert(collectionExists);
            assert(found, "expected sessions collection TTL index to exist");
        } else {
            assert(!found, "TTL index on sessions collection exists");
        }
    } else {
        return (indexExists != found);
    }
}