summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/cluster_commands_require_cluster_node.js
blob: 05a4e80685f27a5c67c3d217345dcc6aa681796d (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/**
 * Verify the "cluster" versions of commands can only run on a sharding enabled shardsvr mongod.
 *
 * @tags: [
 *   requires_replication,
 *   requires_sharding,
 * ]
 */
(function() {
"use strict";

const kDBName = "foo";
const kCollName = "bar";

// Note some commands may still fail, e.g. committing a non-existent transaction, so we validate the
// error was different than when a command is rejected for not having sharding enabled.
const clusterCommandsCases = [
    {cmd: {clusterAbortTransaction: 1}, expectedErr: ErrorCodes.InvalidOptions},
    {cmd: {clusterAggregate: kCollName, pipeline: [{$match: {}}], cursor: {}}},
    {cmd: {clusterCommitTransaction: 1}, expectedErr: ErrorCodes.InvalidOptions},
    {cmd: {clusterDelete: kCollName, deletes: [{q: {}, limit: 1}]}},
    {cmd: {clusterFind: kCollName}},
    {
        cmd: {clusterGetMore: NumberLong(1), collection: kCollName},
        expectedErr: ErrorCodes.CursorNotFound
    },
    {cmd: {clusterInsert: kCollName, documents: [{x: 1}]}},
    {cmd: {clusterUpdate: kCollName, updates: [{q: {doesNotExist: 1}, u: {x: 1}}]}},
];

function runTestCaseExpectFail(conn, testCase, code) {
    assert.commandFailedWithCode(conn.adminCommand(testCase.cmd), code, tojson(testCase.cmd));
}

function runTestCaseExpectSuccess(conn, testCase) {
    assert.commandWorked(conn.adminCommand(testCase.cmd), tojson(testCase.cmd));
}

//
// Standalone mongods have cluster commands, but they cannot be run.
//
{
    const standalone = MongoRunner.runMongod({});
    assert(standalone);

    for (let testCase of clusterCommandsCases) {
        runTestCaseExpectFail(standalone, testCase, ErrorCodes.ShardingStateNotInitialized);
    }

    MongoRunner.stopMongod(standalone);
}

//
// Standalone replica sets mongods have cluster commands, but they cannot be run.
//
{
    const rst = new ReplSetTest({nodes: 1});
    rst.startSet();
    rst.initiate();

    for (let testCase of clusterCommandsCases) {
        runTestCaseExpectFail(rst.getPrimary(), testCase, ErrorCodes.ShardingStateNotInitialized);
    }

    rst.stopSet();
}

//
// Cluster commands exist on shardsvrs but require sharding to be enabled.
//
{
    const shardsvrRst = new ReplSetTest({nodes: 1});
    shardsvrRst.startSet({shardsvr: ""});
    shardsvrRst.initiate();

    for (let testCase of clusterCommandsCases) {
        runTestCaseExpectFail(
            shardsvrRst.getPrimary(), testCase, ErrorCodes.ShardingStateNotInitialized);
    }

    shardsvrRst.stopSet();
}

{
    const st = new ShardingTest({mongos: 1, shards: 1, config: 1});

    //
    // Cluster commands do not exist on mongos.
    //

    for (let testCase of clusterCommandsCases) {
        runTestCaseExpectFail(st.s, testCase, ErrorCodes.CommandNotFound);
    }

    //
    // Cluster commands are not allowed on a config server node.
    //

    for (let testCase of clusterCommandsCases) {
        runTestCaseExpectFail(st.configRS.getPrimary(), testCase, ErrorCodes.NoShardingEnabled);
    }

    //
    // Cluster commands work on sharding enabled shardsvr.
    //

    for (let testCase of clusterCommandsCases) {
        if (testCase.expectedErr) {
            runTestCaseExpectFail(st.rs0.getPrimary(), testCase, testCase.expectedErr);
        } else {
            runTestCaseExpectSuccess(st.rs0.getPrimary(), testCase);
        }
    }

    st.stop();
}
}());