2015-10-22 22:39:12 +02:00
|
|
|
/**
|
|
|
|
* Test that adding invalid or duplicate shards will fail.
|
2021-07-22 13:35:13 +02:00
|
|
|
*
|
2015-10-22 22:39:12 +02:00
|
|
|
*/
|
|
|
|
(function() {
|
|
|
|
|
2016-03-09 18:17:50 +01:00
|
|
|
"use strict";
|
2015-10-22 22:39:12 +02:00
|
|
|
|
2021-09-20 19:34:30 +02:00
|
|
|
const st = new ShardingTest({shards: 1});
|
2015-10-22 22:39:12 +02:00
|
|
|
|
2021-09-20 19:34:30 +02:00
|
|
|
const configDB = st.s.getDB('config');
|
|
|
|
const shardDoc = configDB.shards.findOne();
|
2015-10-22 22:39:12 +02:00
|
|
|
|
2021-09-20 19:34:30 +02:00
|
|
|
/**
|
|
|
|
* @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();
|
2017-10-02 23:05:43 +02:00
|
|
|
|
2021-09-20 19:34:30 +02:00
|
|
|
const connectionString = oldBinaryReplTest.getURL();
|
2015-10-22 22:39:12 +02:00
|
|
|
|
2021-09-20 19:34:30 +02:00
|
|
|
assert.commandFailed(st.admin.runCommand({addshard: connectionString}));
|
|
|
|
oldBinaryReplTest.stopSet();
|
|
|
|
};
|
2020-08-04 20:26:49 +02:00
|
|
|
|
2021-09-20 19:34:30 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @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.
|
|
|
|
*/
|
2015-10-22 22:39:12 +02:00
|
|
|
|
2021-09-20 19:34:30 +02:00
|
|
|
/**
|
|
|
|
* @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());
|
2015-10-22 22:39:12 +02:00
|
|
|
|
2021-09-20 19:34:30 +02:00
|
|
|
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}));
|
|
|
|
});
|
2015-10-22 22:39:12 +02:00
|
|
|
|
2021-09-20 19:34:30 +02:00
|
|
|
replTest.stopSet();
|
|
|
|
};
|
2015-10-22 22:39:12 +02:00
|
|
|
|
2021-09-20 19:34:30 +02:00
|
|
|
// ---- TEST CASES ----
|
|
|
|
|
|
|
|
// Can't add a mongod with a lower binary version than our featureCompatibilityVersion.
|
|
|
|
executeOldBinaryTest("last-lts");
|
|
|
|
executeOldBinaryTest("last-continuous");
|
2015-10-22 22:39:12 +02:00
|
|
|
|
2021-09-20 19:34:30 +02:00
|
|
|
executeStandardTests((replicaSetConnectionURL) => {
|
|
|
|
const truncatedRSConnStr =
|
|
|
|
replicaSetConnectionURL.substring(0, replicaSetConnectionURL.indexOf(','));
|
2015-10-22 22:39:12 +02:00
|
|
|
|
2021-09-20 19:34:30 +02:00
|
|
|
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);
|
2015-10-22 22:39:12 +02:00
|
|
|
|
2016-03-09 18:17:50 +01:00
|
|
|
st.stop();
|
2015-10-22 22:39:12 +02:00
|
|
|
})();
|