summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/validate_detects_invalid_index_options.js
blob: 917aa7fd11cd7d3895bf77b8af65301d0d07e1ed (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
/**
 * Tests that the validate command detects invalid index options.
 *
 * @tags: [requires_fsync, requires_wiredtiger, requires_persistence]
 */
(function() {
"use strict";

load("jstests/libs/fail_point_util.js");

const conn = MongoRunner.runMongod();

const dbName = "test";
const collName = jsTestName();

const db = conn.getDB(dbName);
const coll = db.getCollection(collName);

// In earlier versions of the server, users were able to add invalid index options when creating an
// index. This fail point allows us to skip validating index options to simulate the old behaviour.
const fp = configureFailPoint(db, "skipIndexCreateFieldNameValidation");

// "safe" and "force" are invalid index options.
assert.commandWorked(coll.createIndex({x: 1}, {safe: true, sparse: true, force: false}));

fp.off();

// Foreground and background validations can detect invalid index options.
let validateRes = assert.commandWorked(db.runCommand({validate: collName}));
assert(!validateRes.valid);

// Ensure that $collStats info gets logged when validation fails.
checkLog.containsJson(conn, 7463200);

// Forces a checkpoint to make the background validation see the data.
assert.commandWorked(db.adminCommand({fsync: 1}));
validateRes = assert.commandWorked(db.runCommand({validate: collName, background: true}));
assert(!validateRes.valid);

// Validating only the metadata can detect invalid index options.
validateRes = assert.commandWorked(db.runCommand({validate: collName, metadata: true}));
assert(!validateRes.valid);

// Validation of metadata complete for collection. Problems detected.
checkLog.containsJson(conn, 5980501);

// Cannot use { metadata: true } with any other options.
assert.commandFailedWithCode(db.runCommand({validate: collName, metadata: true, background: true}),
                             ErrorCodes.InvalidOptions);
assert.commandFailedWithCode(db.runCommand({validate: collName, metadata: true, repair: true}),
                             ErrorCodes.InvalidOptions);
assert.commandFailedWithCode(db.runCommand({validate: collName, metadata: true, full: true}),
                             ErrorCodes.InvalidOptions);
assert.commandFailedWithCode(
    db.runCommand({validate: collName, metadata: true, enforceFastCount: true}),
    ErrorCodes.InvalidOptions);

// Drop the index with the invalid index options and validate only the metadata.
assert.commandWorked(coll.dropIndex({x: 1}));

validateRes = assert.commandWorked(db.runCommand({validate: collName, metadata: true}));
assert(validateRes.valid);

// Validation of metadata complete for collection. No problems detected.
checkLog.containsJson(conn, 5980500);

MongoRunner.stopMongod(conn);
}());