summaryrefslogtreecommitdiff
path: root/jstests/noPassthroughWithMongod/rpc_protocols.js
blob: 7e33c3986d374d19382c2acfba6f4a266bec6d10 (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
// Regression test for SERVER-21673.

// A user can configure the shell to send commands via OP_QUERY or OP_COMMAND. This can be done at
// startup using the "--rpcProtocols" command line option, or at runtime using the
// "setClientRPCProtocols" method on the Mongo object.

var RPC_PROTOCOLS = {
    OP_QUERY: "opQueryOnly",
    OP_COMMAND: "opCommandOnly"
};

(function() {
    "use strict";

    db.rpcProtocols.drop();

    var oldProfilingLevel = db.getProfilingLevel();

    assert.commandWorked(db.setProfilingLevel(2));

    function runInShell(rpcProtocol, func) {
        assert(0 == _runMongoProgram("mongo",
                                     "--rpcProtocols=" + rpcProtocol,
                                     "--readMode=commands",  // ensure we use the find command.
                                     "--eval",
                                     "(" + func.toString() + ")();",
                                     db.getMongo().host));
    }

    // Test that --rpcProtocols=opQueryOnly forces OP_QUERY commands.
    runInShell(
        RPC_PROTOCOLS.OP_QUERY,
        function() {
            assert(db.getMongo().getClientRPCProtocols() === "opQueryOnly");
            db.getSiblingDB("test").rpcProtocols.find().comment("opQueryCommandLine").itcount();
        });
    var profileDoc = db.system.profile.findOne({"query.comment": "opQueryCommandLine"});
    assert(profileDoc !== null);
    assert.eq(profileDoc.protocol, "op_query");

    // Test that --rpcProtocols=opCommandOnly forces OP_COMMAND commands.
    runInShell(
        RPC_PROTOCOLS.OP_COMMAND,
        function() {
            assert(db.getMongo().getClientRPCProtocols() === "opCommandOnly");
            db.getSiblingDB("test").rpcProtocols.find().comment("opCommandCommandLine").itcount();
        });
    profileDoc = db.system.profile.findOne({"query.comment": "opCommandCommandLine"});
    assert(profileDoc !== null);
    assert.eq(profileDoc.protocol, "op_command");

    // Test that .setClientRPCProtocols("opQueryOnly") forces OP_QUERY commands. We start the shell
    // in OP_COMMAND only mode, then switch it to OP_QUERY mode at runtime.
    runInShell(RPC_PROTOCOLS.OP_COMMAND,
               function() {
                   assert(db.getMongo().getClientRPCProtocols() === "opCommandOnly");
                   db.getMongo().setClientRPCProtocols("opQueryOnly");
                   assert(db.getMongo().getClientRPCProtocols() === "opQueryOnly");
                   db.getSiblingDB("test").rpcProtocols.find().comment("opQueryRuntime").itcount();
               });
    profileDoc = db.system.profile.findOne({"query.comment": "opQueryRuntime"});
    assert(profileDoc !== null);
    assert.eq(profileDoc.protocol, "op_query");

    // Test that .setClientRPCProtocols("opCommandOnly") forces OP_COMMAND commands. We start the
    // shell in OP_QUERY only mode, then switch it to OP_COMMAND mode at runtime.
    runInShell(
        RPC_PROTOCOLS.OP_QUERY,
        function() {
            assert(db.getMongo().getClientRPCProtocols() === "opQueryOnly");
            db.getMongo().setClientRPCProtocols("opCommandOnly");
            assert(db.getMongo().getClientRPCProtocols() === "opCommandOnly");
            db.getSiblingDB("test").rpcProtocols.find().comment("opCommandRuntime").itcount();
        });
    profileDoc = db.system.profile.findOne({"query.comment": "opCommandRuntime"});
    assert(profileDoc !== null);
    assert.eq(profileDoc.protocol, "op_command");

    // Reset profiling level.
    assert.commandWorked(db.setProfilingLevel(oldProfilingLevel));
})();