summaryrefslogtreecommitdiff
path: root/jstests/replsets/nested_apply_ops_create_indexes.js
blob: 1349d706c3735488a785cdacab323310ebf472d5 (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
/**
 * Test createIndexes while recursively locked in a nested applyOps.
 */
(function() {
    "use strict";

    let ensureIndexExists = function(testDB, collName, indexName, expectedNumIndexes) {
        let cmd = {listIndexes: collName};
        let res = testDB.runCommand(cmd);
        assert.commandWorked(res, "could not run " + tojson(cmd));
        let indexes = testDB[collName].getIndexes();

        assert.eq(indexes.length, expectedNumIndexes);

        let foundIndex = indexes.some(index => index.name === indexName);
        assert(foundIndex,
               "did not find the index '" + indexName + "' amongst the collection indexes: " +
                   tojson(indexes));
    };

    let rst = new ReplSetTest({nodes: 3});
    rst.startSet();
    rst.initiate();

    let collName = "col";
    let dbName = "nested_apply_ops_create_indexes";

    let primaryTestDB = rst.getPrimary().getDB(dbName);
    let cmd = {"create": collName};
    let res = primaryTestDB.runCommand(cmd);
    assert.commandWorked(res, "could not run " + tojson(cmd));
    rst.awaitReplication();

    let uuid = primaryTestDB.getCollectionInfos()[0].info.uuid;
    let cmdFormatIndexNameA = "a_1";
    cmd = {
        applyOps: [{
            op: "c",
            ns: dbName + ".$cmd",
            ui: uuid,
            o: {
                applyOps: [{
                    op: "c",
                    ns: dbName + "." + collName,
                    ui: uuid,
                    o: {
                        createIndexes: collName,
                        v: 2,
                        key: {a: 1},
                        name: cmdFormatIndexNameA
                    }
                }]
            }
        }]
    };
    res = primaryTestDB.runCommand(cmd);
    assert.commandWorked(res, "could not run " + tojson(cmd));
    rst.awaitReplication();
    ensureIndexExists(primaryTestDB, collName, cmdFormatIndexNameA, 2);

    rst.stopSet();
})();