summaryrefslogtreecommitdiff
path: root/jstests/core/txns/no_writes_to_system_collections_in_txn.js
blob: 4b13908773e58369a1d0f5b4e88bf0513bd5f14c (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
// Tests that it is illegal to write to system collections within a transaction.
// @tags: [uses_transactions, uses_snapshot_read_concern]
(function() {
    "use strict";

    const session = db.getMongo().startSession({causalConsistency: false});

    // Use a custom database, to avoid conflict with other tests that use the system.js collection.
    const testDB = session.getDatabase("no_writes_system_collections_in_txn");
    assert.commandWorked(testDB.dropDatabase());
    const systemColl = testDB.getCollection("system.js");
    const systemDotViews = testDB.getCollection("system.views");

    // Ensure that a collection exists with at least one document.
    assert.commandWorked(systemColl.insert({name: 0}, {writeConcern: {w: "majority"}}));

    session.startTransaction({readConcern: {level: "snapshot"}});
    let error = assert.throws(() => systemColl.findAndModify({query: {}, update: {}}));
    assert.commandFailedWithCode(error, 50781);
    assert.commandFailedWithCode(session.abortTransaction_forTesting(),
                                 ErrorCodes.NoSuchTransaction);

    session.startTransaction({readConcern: {level: "snapshot"}});
    error = assert.throws(() => systemColl.findAndModify({query: {}, remove: true}));
    assert.commandFailedWithCode(error, 50781);
    assert.commandFailedWithCode(session.abortTransaction_forTesting(),
                                 ErrorCodes.NoSuchTransaction);

    session.startTransaction({readConcern: {level: "snapshot"}});
    assert.commandFailedWithCode(systemColl.insert({name: "new"}), 50791);
    assert.commandFailedWithCode(session.abortTransaction_forTesting(),
                                 ErrorCodes.NoSuchTransaction);

    session.startTransaction({readConcern: {level: "snapshot"}});
    assert.commandFailedWithCode(
        systemDotViews.insert({_id: "new.view", viewOn: "bar", pipeline: []}), 50791);
    assert.commandFailedWithCode(session.abortTransaction_forTesting(),
                                 ErrorCodes.NoSuchTransaction);

    session.startTransaction({readConcern: {level: "snapshot"}});
    assert.commandFailedWithCode(systemColl.update({name: 0}, {$set: {name: "jungsoo"}}), 50791);
    assert.commandFailedWithCode(session.abortTransaction_forTesting(),
                                 ErrorCodes.NoSuchTransaction);

    session.startTransaction({readConcern: {level: "snapshot"}});
    assert.commandFailedWithCode(
        systemColl.update({name: "nonexistent"}, {$set: {name: "jungsoo"}}, {upsert: true}), 50791);
    assert.commandFailedWithCode(session.abortTransaction_forTesting(),
                                 ErrorCodes.NoSuchTransaction);

    session.startTransaction({readConcern: {level: "snapshot"}});
    assert.commandFailedWithCode(systemColl.remove({name: 0}), 50791);
    assert.commandFailedWithCode(session.abortTransaction_forTesting(),
                                 ErrorCodes.NoSuchTransaction);

    assert.commandWorked(systemColl.remove({_id: {$exists: true}}));
    assert.eq(systemColl.find().itcount(), 0);
}());