summaryrefslogtreecommitdiff
path: root/jstests/hooks/validate_collections.js
blob: 89c7f73e798e6f8964ca7a59e9d3c447c561528a (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
// Wrapper around the validate command that can be used to validate index key counts.
'use strict';

function validateCollections(db, obj) {
    function dumpCollection(coll, limit) {
        print('Printing indexes in: ' + coll.getFullName());
        printjson(coll.getIndexes());

        print('Printing the first ' + limit + ' documents in: ' + coll.getFullName());
        var res = coll.find().limit(limit);
        while (res.hasNext()) {
            printjson(res.next());
        }
    }

    function getFeatureCompatibilityVersion(adminDB) {
        var res = adminDB.system.version.findOne({_id: "featureCompatibilityVersion"});
        if (res === null) {
            return "3.2";
        }
        return res.version;
    }

    function setFeatureCompatibilityVersion(adminDB, version) {
        assert.commandWorked(adminDB.runCommand({setFeatureCompatibilityVersion: version}));
        assert.eq(version, getFeatureCompatibilityVersion(adminDB));
    }

    assert.eq(typeof db, 'object', 'Invalid `db` object, is the shell connected to a mongod?');
    assert.eq(typeof obj, 'object', 'The `obj` argument must be an object');
    assert(obj.hasOwnProperty('full'), 'Please specify whether to use full validation');

    var full = obj.full;

    var success = true;

    var adminDB = db.getSiblingDB("admin");

    // Set the featureCompatibilityVersion to its required value for performing validation. Save the
    // original value.
    var originalFeatureCompatibilityVersion;
    if (jsTest.options().forceValidationWithFeatureCompatibilityVersion) {
        originalFeatureCompatibilityVersion = getFeatureCompatibilityVersion(adminDB);
        setFeatureCompatibilityVersion(
            adminDB, jsTest.options().forceValidationWithFeatureCompatibilityVersion);
    }

    // Don't run validate on view namespaces.
    let listCollectionsRes = db.runCommand({listCollections: 1, filter: {"type": "collection"}});
    if (jsTest.options().skipValidationOnInvalidViewDefinitions && listCollectionsRes.ok === 0) {
        assert.commandFailedWithCode(listCollectionsRes, ErrorCodes.InvalidViewDefinition);
        print('Skipping validate hook because of invalid views in system.views');
        return true;
    }
    assert.commandWorked(listCollectionsRes);

    let collInfo = new DBCommandCursor(db.getMongo(), listCollectionsRes).toArray();

    for (var collDocument of collInfo) {
        var coll = db.getCollection(collDocument["name"]);
        var res = coll.validate(full);

        if (!res.ok || !res.valid) {
            print('Collection validation failed with response: ' + tojson(res));
            dumpCollection(coll, 100);
            success = false;
        }
    }

    // Restore the original value for featureCompatibilityVersion.
    if (jsTest.options().forceValidationWithFeatureCompatibilityVersion) {
        setFeatureCompatibilityVersion(adminDB, originalFeatureCompatibilityVersion);
    }

    return success;
}