summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/server_status_metric_validator_and_json_schema.js
blob: a1ed8bf91da2c4b6933b7026003f276d34c40919 (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/**
 * Tests for serverStatus metrics.commands.<name>.validator stats.
 * @tags: [requires_sharding]
 *
 */
(function() {
"use strict";

function runCommandAndCheckValidatorCount({cmdToRun, cmdName, countDict, error, notFirst}) {
    let metricsBeforeCommandInvoked = {failed: 0, jsonSchema: 0, total: 0};
    if (notFirst) {
        metricsBeforeCommandInvoked = db.serverStatus().metrics.commands[cmdName].validator;
    }
    if (error) {
        cmdToRun();
    } else {
        assert.commandWorked(cmdToRun());
    }
    const metricsAfterCommandInvoked = db.serverStatus().metrics.commands[cmdName].validator;
    assert.eq(metricsAfterCommandInvoked.total - metricsBeforeCommandInvoked.total,
              countDict.total,
              metricsAfterCommandInvoked);
    assert.eq(metricsAfterCommandInvoked.failed - metricsBeforeCommandInvoked.failed,
              countDict.failed,
              metricsAfterCommandInvoked);
    assert.eq(metricsAfterCommandInvoked.jsonSchema - metricsBeforeCommandInvoked.jsonSchema,
              countDict.jsonSchema,
              metricsAfterCommandInvoked);
}

function runTests(db, collName, collCount) {
    //   Test that validator count is 0 if no validator specified.
    runCommandAndCheckValidatorCount({
        cmdToRun: () => db.createCollection(collName),
        cmdName: "create",
        countDict: {failed: NumberLong(0), jsonSchema: NumberLong(0), total: NumberLong(0)},
        error: false,
        notFirst: false
    });
    collCount++;
    assert.commandWorked(db[collName].createIndex({a: 1}));
    runCommandAndCheckValidatorCount({
        cmdToRun: () => db.runCommand(
            {collMod: collName, index: {keyPattern: {a: 1}, expireAfterSeconds: 3600}}),
        cmdName: "collMod",
        countDict: {failed: NumberLong(0), jsonSchema: NumberLong(0), total: NumberLong(0)},
        error: false,
        notFirst: false
    });

    // Test that validator total and failed count increments when a $jsonSchema validation error is
    // raised.
    let schema = {
        '$jsonSchema': {
            'bsonType': "object",
            'properties': {
                'a': "",
                'b': {
                    'bsonType': "number",
                },
                'c': {
                    'bsonType': "number",
                },
            }
        }
    };
    runCommandAndCheckValidatorCount({
        cmdToRun: () => db.runCommand({"create": collName + collCount, validator: schema}),
        cmdName: "create",
        countDict: {failed: NumberLong(1), jsonSchema: NumberLong(1), total: NumberLong(1)},
        error: true,
        notFirst: true
    });
    collCount++;

    runCommandAndCheckValidatorCount({
        cmdToRun: () => db.runCommand({collMod: collName, validator: schema}),
        cmdName: "collMod",
        countDict: {failed: NumberLong(1), jsonSchema: NumberLong(1), total: NumberLong(1)},
        error: true,
        notFirst: true
    });

    // Test that validator total count increments, but not failed count for valid $jsonSchema.
    schema = {
        '$jsonSchema': {
            'bsonType': "object",
            'properties': {
                'a': {
                    'bsonType': "number",
                },
                'b': {
                    'bsonType': "number",
                },
                'c': {
                    'bsonType': "number",
                },
            }
        }
    };

    runCommandAndCheckValidatorCount({
        cmdToRun: () => db.createCollection(collName + collCount, {validator: schema}),
        cmdName: "create",
        countDict: {failed: NumberLong(0), jsonSchema: NumberLong(1), total: NumberLong(1)},
        error: false,
        notFirst: true
    });
    collCount++;
    runCommandAndCheckValidatorCount({
        cmdToRun: () => db.runCommand({collMod: collName, validator: schema}),
        cmdName: "collMod",
        countDict: {failed: NumberLong(0), jsonSchema: NumberLong(1), total: NumberLong(1)},
        error: false,
        notFirst: true
    });

    // Test that only the validator 'total' count gets incremented with match expression validator.
    // Neither the 'find' nor 'jsonSchema' fields should be incremented.
    schema = {$expr: {$eq: ["$a", {$multiply: ["$v", {$sum: [1, "$c"]}]}]}};
    runCommandAndCheckValidatorCount({
        cmdToRun: () => db.createCollection(collName + collCount, {validator: schema}),
        cmdName: "create",
        countDict: {failed: NumberLong(0), jsonSchema: NumberLong(0), total: NumberLong(1)},
        error: false,
        notFirst: true
    });
    collCount++;

    runCommandAndCheckValidatorCount({
        cmdToRun: () => db.runCommand({collMod: collName, validator: schema}),
        cmdName: "collMod",
        countDict: {failed: NumberLong(0), jsonSchema: NumberLong(0), total: NumberLong(1)},
        error: false,
        notFirst: true
    });

    // Test that validator count does not increment with empty validator object.
    runCommandAndCheckValidatorCount({
        cmdToRun: () => db.createCollection(collName + collCount, {validator: {}}),
        cmdName: "create",
        countDict: {failed: NumberLong(0), jsonSchema: NumberLong(0), total: NumberLong(0)},
        error: false,
        notFirst: true
    });
    collCount++;
    runCommandAndCheckValidatorCount({
        cmdToRun: () => db.runCommand({collMod: collName, validator: {}}),
        cmdName: "collMod",
        countDict: {failed: NumberLong(0), jsonSchema: NumberLong(0), total: NumberLong(0)},
        error: false,
        notFirst: true
    });
}

// Standalone
const conn = MongoRunner.runMongod({});
assert.neq(conn, null, "mongod failed to start");
let db = conn.getDB(jsTestName());
let collCount = 0;
const collName = jsTestName();
runTests(db, collName, collCount);

MongoRunner.stopMongod(conn);

//  Sharded cluster
const st = new ShardingTest({shards: 2});
assert.commandWorked(st.s.adminCommand({enableSharding: jsTestName()}));
st.ensurePrimaryShard(jsTestName(), st.shard0.shardName);
db = st.rs0.getPrimary().getDB(jsTestName());
collCount = 0;
runTests(db, collName, collCount);

st.stop();
}());