summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/can_load_ttl_index_capped_collection.js
blob: 0e0c47c6ed8730bbdd7ca95aeadbfdf07af9f022 (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
// Makes sure that TTL indexes on capped collection are allowed to be loaded if they already exist.
// This ensures that it is safe for old versions with such indexes already existing to upgrade to
// the latest version, even if such indexes are no longer allowed to be built on the latest version.
//
// @tags: [requires_persistence]
(function() {
"use strict";

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

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

// Set up a TTL index on a capped collection, using a failpoint to bypass the check that should
// prevent this.
let conn = MongoRunner.runMongod();
const fp = configureFailPoint(conn, 'ignoreTTLIndexCappedCollectionCheck');
let db = conn.getDB(dbName);
let coll = db.getCollection(collName);

assert.commandWorked(db.createCollection(collName, {capped: true, size: 102400}));
assert.commandWorked(coll.createIndex({foo: 1}));
assert.commandWorked(coll.createIndex({bar: 1}, {expireAfterSeconds: 10}));
fp.off();
MongoRunner.stopMongod(conn);

// Check that the server can startup successfully with a pre-existing TTL index on a capped
// collection.
conn = MongoRunner.runMongod({
    restart: true,
    cleanData: false,
    dbpath: conn.dbpath,
});
db = conn.getDB(dbName);
coll = db.getCollection(collName);

// Since the failpoint is disabled, ensure a new TTL index cannot be created.
assert.commandFailedWithCode(coll.createIndex({baz: 1}, {expireAfterSeconds: 10}),
                             ErrorCodes.CannotCreateIndex);

// Ensure listIndexes returns the TTL index on the capped collection even though a new TTL index
// cannot be created.
const res = assert.commandWorked(db.runCommand({listIndexes: collName}));
const indexes = res.cursor.firstBatch;
assert.eq(
    3, indexes.length, "Unexpected number of indexes in listIndexes output: " + tojson(indexes));
assert.eq(indexes[2].expireAfterSeconds, 10, "Index is not TTL as expected: " + tojson(indexes));

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