summaryrefslogtreecommitdiff
path: root/jstests/multiVersion/genericSetFCVUsage/add_invalid_shard.js
blob: df9a2b7a4fd6f78d8768877237bf83c6c1ce5c0c (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
/**
 * Test that adding invalid or duplicate shards will fail.
 *
 */
(function() {

"use strict";

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

const configDB = st.s.getDB('config');
const shardDoc = configDB.shards.findOne();

/**
 * @summary Starts a new replica set with the specified binVersion and replicaName.
 * Tests that we can't add that replicaSet as a shard to our existing cluster. Once the assertion
 * completes successfully, it will stop the replica set.
 * @param {string} binVersion The binVersion to configure the replica set with.
 * @param {string} replicaName  The name given to the replica set.
 */
const executeOldBinaryTest = (binVersion) => {
    jsTest.log(`Executing binary test for binVersion ${binVersion}`);
    const oldBinaryReplTest = new ReplSetTest({nodes: 2, nodeOptions: {binVersion, shardsvr: ""}});
    oldBinaryReplTest.startSet();
    oldBinaryReplTest.initiate();

    const connectionString = oldBinaryReplTest.getURL();

    assert.commandFailed(st.admin.runCommand({addshard: connectionString}));
    oldBinaryReplTest.stopSet();
};

/**
 *
 * @callback TestCaseGenerator
 * @param {String} connectionString - The host URL of the replica set.
 * @returns {Array<{addshard: string, name: string, setup: any}>} - An array of test cases to
 *     execute.
 */

/**
 * @summary Receives a function that it can call to generate addshard command objects.
 * It will then go through each command and assert that it will fail. After going through each
 * command, it will stop the replica set.
 * @param {TestCaseGenerator} createStandardTestCases
 */
const executeStandardTests = (createStandardTestCases) => {
    jsTest.log("Starting to execute the standard test cases");
    const replTest = new ReplSetTest({nodes: 2, nodeOptions: {shardsvr: ""}});
    replTest.startSet({oplogSize: 10});
    replTest.initiate();

    const addShardCommands = createStandardTestCases(replTest.getURL());

    addShardCommands.forEach(({addshard, name, setup}) => {
        jsTest.log(`About to run addshard command with value ${addshard} and name ${name}`);
        if (setup) {
            setup();
        }
        assert.commandFailed(st.admin.runCommand({addshard: addshard, name: name}));
    });

    replTest.stopSet();
};

// ---- TEST CASES ----

// Can't add a mongod with a lower binary version than our featureCompatibilityVersion.
executeOldBinaryTest("last-lts");
executeOldBinaryTest("last-continuous");

executeStandardTests((replicaSetConnectionURL) => {
    const truncatedRSConnStr =
        replicaSetConnectionURL.substring(0, replicaSetConnectionURL.indexOf(','));

    return [
        {
            // Can't add replSet as shard if the name doesn't match the replSet config.
            addshard: "prefix_" + replicaSetConnectionURL,
        },
        {
            // Cannot add the same replSet shard host twice when using a unique shard name.
            addshard: replicaSetConnectionURL,
            name: 'dupRS',
            setup: () => (assert.commandWorked(
                st.admin.runCommand({addshard: replicaSetConnectionURL, name: 'dummyRS'})))
        },
        {
            // Cannot add a replica set connection string containing a member that isn't actually
            // part of the replica set.
            addshard: truncatedRSConnStr + 'fakehost',
            name: 'dummyRS'
        },
        {addshard: shardDoc.host, name: 'dupShard'},
        {addshard: st._configDB},  // Can't add config servers as shard.
    ];
});

// Can't add mongos as shard.
assert.commandFailedWithCode(st.admin.runCommand({addshard: st.s.host}),
                             ErrorCodes.IllegalOperation);

st.stop();
})();