summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/system_indexes.js
blob: 610dbd5c2067cf767b50d5b5305b5d92be672799 (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
/** Ensure that authorization system collections' indexes are correctly generated.
 *
 * This test requires users to persist across a restart.
 * @tags: [requires_persistence]
 */

(function() {
    let conn = MongoRunner.runMongod({smallfiles: ""});
    let config = conn.getDB("config");
    let db = conn.getDB("admin");

    // TEST: User and role collections start off with no indexes
    assert.eq(0, db.system.users.getIndexes().length);
    assert.eq(0, db.system.roles.getIndexes().length);

    // TEST: User and role creation generates indexes
    db.createUser({user: "user", pwd: "pwd", roles: []});
    assert.eq(2, db.system.users.getIndexes().length);

    db.createRole({role: "role", privileges: [], roles: []});
    assert.eq(2, db.system.roles.getIndexes().length);

    // TEST: Destroying admin.system.users index and restarting will recreate it
    assert.commandWorked(db.system.users.dropIndexes());
    MongoRunner.stopMongod(conn);
    conn = MongoRunner.runMongod({restart: conn, cleanData: false});
    db = conn.getDB("admin");
    assert.eq(2, db.system.users.getIndexes().length);
    assert.eq(2, db.system.roles.getIndexes().length);

    // TEST: Destroying admin.system.roles index and restarting will recreate it
    assert.commandWorked(db.system.roles.dropIndexes());
    MongoRunner.stopMongod(conn);
    conn = MongoRunner.runMongod({restart: conn, cleanData: false});
    db = conn.getDB("admin");
    assert.eq(2, db.system.users.getIndexes().length);
    assert.eq(2, db.system.roles.getIndexes().length);

    // TEST: Destroying both authorization indexes and restarting will recreate them
    assert.commandWorked(db.system.users.dropIndexes());
    assert.commandWorked(db.system.roles.dropIndexes());
    MongoRunner.stopMongod(conn);
    conn = MongoRunner.runMongod({restart: conn, cleanData: false});
    db = conn.getDB("admin");
    assert.eq(2, db.system.users.getIndexes().length);
    assert.eq(2, db.system.roles.getIndexes().length);

    // Retain the current FCV so that it can be restored after the admin db is dropped.
    const fcv =
        assert.commandWorked(db.adminCommand({getParameter: 1, featureCompatibilityVersion: 1}))
            .featureCompatibilityVersion.version;

    // TEST: Destroying the admin.system.users index and restarting will recreate it, even if
    // admin.system.roles does not exist
    db.dropDatabase();
    // Restore the featureCompatibilityVersion document since as of SERVER-29452, mongod fails to
    // startup without such a document.
    assert.commandWorked(db.runCommand({setFeatureCompatibilityVersion: fcv}));
    db.createUser({user: "user", pwd: "pwd", roles: []});
    assert.commandWorked(db.system.users.dropIndexes());
    MongoRunner.stopMongod(conn);
    conn = MongoRunner.runMongod({restart: conn, cleanData: false});
    db = conn.getDB("admin");
    assert.eq(2, db.system.users.getIndexes().length);

    // TEST: Destroying the admin.system.roles index and restarting will recreate it, even if
    // admin.system.users does not exist
    db.dropDatabase();
    // Restore the featureCompatibilityVersion document since as of SERVER-29452, mongod fails to
    // startup without such a document.
    assert.commandWorked(db.runCommand({setFeatureCompatibilityVersion: fcv}));
    db.createRole({role: "role", privileges: [], roles: []});
    assert.commandWorked(db.system.roles.dropIndexes());
    MongoRunner.stopMongod(conn);
    conn = MongoRunner.runMongod({restart: conn, cleanData: false});
    db = conn.getDB("admin");
    assert.eq(2, db.system.roles.getIndexes().length);

})();