summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/apply_ops_mode.js
blob: f9bfb5a00178436f8a8d6fcdd6308ad68ee8dba3 (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
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
 * Tests that applyOps correctly respects the 'oplogApplicationMode' and 'alwaysUpsert' flags.
 * 'alwaysUpsert' defaults to true and 'oplogApplicationMode' defaults to 'ApplyOps'. We test
 * that these default values do not lead to command failure.
 */

(function() {
    'use strict';

    var standalone = MongoRunner.runMongod();
    var db = standalone.getDB("test");

    var coll = db.getCollection("apply_ops_mode1");
    coll.drop();
    assert.writeOK(coll.insert({_id: 1}));

    // ------------ Testing normal updates ---------------

    var id = ObjectId();
    var updateOp = {op: 'u', ns: coll.getFullName(), o: {_id: id, x: 1}, o2: {_id: id}};
    assert.commandFailed(db.adminCommand({applyOps: [updateOp], alwaysUpsert: false}));
    assert.eq(coll.count({x: 1}), 0);

    // Test that 'InitialSync' does not override 'alwaysUpsert: false'.
    assert.commandFailed(db.adminCommand(
        {applyOps: [updateOp], alwaysUpsert: false, oplogApplicationMode: "InitialSync"}));
    assert.eq(coll.count({x: 1}), 0);

    // Test parsing failure.
    assert.commandFailedWithCode(
        db.adminCommand({applyOps: [updateOp], oplogApplicationMode: "BadMode"}),
        ErrorCodes.FailedToParse);
    assert.commandFailedWithCode(db.adminCommand({applyOps: [updateOp], oplogApplicationMode: 5}),
                                 ErrorCodes.TypeMismatch);

    // Test default succeeds.
    assert.commandWorked(db.adminCommand({applyOps: [updateOp]}));
    assert.eq(coll.count({x: 1}), 1);

    // Use new collection to make logs cleaner.
    coll = db.getCollection("apply_ops_mode2");
    coll.drop();
    updateOp.ns = coll.getFullName();
    assert.writeOK(coll.insert({_id: 1}));

    // Test default succeeds in 'InitialSync' mode.
    assert.commandWorked(
        db.adminCommand({applyOps: [updateOp], oplogApplicationMode: "InitialSync"}));
    assert.eq(coll.count({x: 1}), 1);

    // ------------ Testing fCV updates ---------------

    var adminDB = db.getSiblingDB("admin");
    const systemVersionColl = adminDB.getCollection("system.version");

    updateOp = {
        op: 'u',
        ns: systemVersionColl.getFullName(),
        o: {_id: "featureCompatibilityVersion", version: "3.4"},
        o2: {_id: "featureCompatibilityVersion"}
    };
    assert.commandFailed(
        db.adminCommand({applyOps: [updateOp], oplogApplicationMode: "InitialSync"}));

    assert.commandWorked(db.adminCommand({applyOps: [updateOp], oplogApplicationMode: "ApplyOps"}));

    // Test default succeeds.
    updateOp.o.targetVersion = "3.6";
    assert.commandWorked(db.adminCommand({
        applyOps: [updateOp],
    }));

    // ------------ Testing commands on the fCV collection ---------------

    var collModOp = {
        op: 'c',
        ns: systemVersionColl.getDB() + ".$cmd",
        o: {collMod: systemVersionColl.getName(), validationLevel: "off"},
    };
    assert.commandFailed(
        db.adminCommand({applyOps: [collModOp], oplogApplicationMode: "InitialSync"}));

    assert.commandWorked(
        db.adminCommand({applyOps: [collModOp], oplogApplicationMode: "ApplyOps"}));

    // Test default succeeds.
    collModOp.o.usePowerOf2Sizes = true;
    assert.commandWorked(db.adminCommand({
        applyOps: [collModOp],
    }));

})();