summaryrefslogtreecommitdiff
path: root/jstests/replsets/reindex.js
blob: 0eb499e8ca97ffd71d13de852a94ff8be0fccca3 (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
/**
 * Test that the reindex command only runs on a node in standalone mode. First it will make sure
 * that the command can't be run on a primary or a secondary. Then it will make sure that the
 * reindex command can be successfully run on a standalone node.
 *
 * @tags: [
 * ]
 */

(function() {
"use strict";

jsTestLog("Testing that the reindex command cannot be run on a primary or secondary");

const replTest = new ReplSetTest({name: 'reindexTest', nodes: 2});
replTest.startSet();
replTest.initiate();

const primary = replTest.getPrimary();
const secondary = replTest.getSecondary();

const dbName = "test";
const collName = "reindex";
const primaryDB = primary.getDB(dbName);
const primaryColl = primaryDB.getCollection(collName);
const secondaryDB = secondary.getDB(dbName);
const secondaryColl = secondaryDB.getCollection(collName);

assert.commandWorked(primaryColl.insert({a: 1000}));
assert.commandWorked(primaryColl.createIndex({a: 1}));

replTest.awaitReplication();
replTest.awaitReplication();

assert.eq(2,
          primaryColl.getIndexes().length,
          "Primary didn't have expected number of indexes before reindex");
assert.eq(2,
          secondaryColl.getIndexes().length,
          "Secondary didn't have expected number of indexes before reindex");

assert.commandFailedWithCode(primaryColl.reIndex(), ErrorCodes.IllegalOperation);
assert.commandFailedWithCode(secondaryColl.reIndex(), ErrorCodes.IllegalOperation);

assert.eq(2,
          primaryColl.getIndexes().length,
          "Primary didn't have expected number of indexes after failed reindex");
assert.eq(2,
          secondaryColl.getIndexes().length,
          "Secondary didn't have expected number of indexes after failed reindex");

replTest.stopSet();

jsTestLog("Testing that the reindex command can successfully be run on a standalone node");

const standalone = MongoRunner.runMongod({});
assert.neq(null, standalone, "mongod failed to start.");

const testDB = standalone.getDB(dbName);
const testColl = testDB.getCollection(collName);

assert.commandWorked(testColl.insert({a: 1000}));
assert.commandWorked(testColl.createIndex({a: 1}));
assert.eq(2, testColl.getIndexes().length, "Standalone didn't have proper indexes before reindex");

assert.commandWorked(testColl.reIndex());

const nonExistentDb = standalone.getDB('does_not_exist');
assert.commandFailedWithCode(nonExistentDb.getCollection('test').reIndex(),
                             ErrorCodes.NamespaceNotFound);

assert.eq(2, testColl.getIndexes().length, "Standalone didn't have proper indexes after reindex");

MongoRunner.stopMongod(standalone);
})();