summaryrefslogtreecommitdiff
path: root/jstests/sharding
diff options
context:
space:
mode:
Diffstat (limited to 'jstests/sharding')
-rw-r--r--jstests/sharding/hidden_index.js80
-rw-r--r--jstests/sharding/timeseries_coll_mod.js9
2 files changed, 84 insertions, 5 deletions
diff --git a/jstests/sharding/hidden_index.js b/jstests/sharding/hidden_index.js
new file mode 100644
index 00000000000..9b4cce7d516
--- /dev/null
+++ b/jstests/sharding/hidden_index.js
@@ -0,0 +1,80 @@
+/**
+ * Test to validate that a shard key index cannot be hidden if it cannot be dropped.
+ * @tags: [
+ * requires_fcv_62,
+ * ]
+ */
+
+(function() {
+'use strict';
+load("jstests/libs/index_catalog_helpers.js"); // For IndexCatalogHelpers.findByName.
+
+// Test to validate the correct behaviour of hiding an index in a sharded cluster with a shard key.
+function validateHiddenIndexBehaviour() {
+ let index_type = 1;
+ let index_name = "a_" + index_type;
+ assert.commandWorked(coll.createIndex({"a": index_type}));
+
+ let idxSpec = IndexCatalogHelpers.findByName(coll.getIndexes(), index_name);
+ assert.eq(idxSpec.hidden, undefined);
+
+ assert.commandWorked(coll.hideIndex(index_name));
+ idxSpec = IndexCatalogHelpers.findByName(coll.getIndexes(), index_name);
+ assert(idxSpec.hidden);
+
+ assert.commandWorked(coll.unhideIndex(index_name));
+ idxSpec = IndexCatalogHelpers.findByName(coll.getIndexes(), index_name);
+ assert.eq(idxSpec.hidden, undefined);
+
+ assert.commandWorked(coll.dropIndex(index_name));
+ assert.commandWorked(coll.createIndex({"a": index_type}, {hidden: true}));
+
+ idxSpec = IndexCatalogHelpers.findByName(coll.getIndexes(), index_name);
+ assert(idxSpec.hidden);
+ assert.commandWorked(coll.dropIndexes());
+}
+
+// Check that command will fail when we try to hide the only shard key index of the collection
+function validateOneShardKeyHiddenIndexBehaviour() {
+ assert.commandFailedWithCode(coll.hideIndex("skey_1"), ErrorCodes.InvalidOptions);
+ assert.commandFailedWithCode(
+ testDb.runCommand({"collMod": coll.getName(), "index": {"name": "skey_1", "hidden": true}}),
+ ErrorCodes.InvalidOptions);
+}
+
+// Check that command will fail when we try to hide or drop an index that is the last shard key
+// index of the collection
+function validateDifferentHiddenIndexesBehaviour() {
+ // Create index on skey
+ assert.commandWorked(coll.createIndex({skey: 1, anotherkey: 1}));
+
+ assert.commandWorked(testDb.runCommand(
+ {"collMod": coll.getName(), "index": {"name": "skey_1", "hidden": true}}));
+
+ assert.commandFailedWithCode(
+ testDb.runCommand(
+ {"collMod": coll.getName(), "index": {"name": "skey_1_anotherkey_1", "hidden": true}}),
+ ErrorCodes.InvalidOptions);
+
+ assert.commandFailed(coll.dropIndex("skey_1_anotherkey_1"));
+}
+
+// Configure initial sharded cluster
+const st = new ShardingTest({shards: 2});
+const mongos = st.s;
+const testDb = mongos.getDB("test");
+const coll = testDb.getCollection("foo");
+
+// Enable sharding at collection 'foo' and create a new shard key
+assert.commandWorked(st.s.adminCommand({enableSharding: testDb.getName()}));
+
+// Crate a new shard key
+assert.commandWorked(
+ st.s.adminCommand({shardcollection: testDb.getName() + '.' + coll.getName(), key: {skey: 1}}));
+
+validateHiddenIndexBehaviour();
+validateOneShardKeyHiddenIndexBehaviour();
+validateDifferentHiddenIndexesBehaviour();
+
+st.stop();
+})();
diff --git a/jstests/sharding/timeseries_coll_mod.js b/jstests/sharding/timeseries_coll_mod.js
index 08e882ef64a..67be3a51bfc 100644
--- a/jstests/sharding/timeseries_coll_mod.js
+++ b/jstests/sharding/timeseries_coll_mod.js
@@ -60,11 +60,10 @@ function runBasicTest() {
key: {[metaField]: 1},
}));
- // Normal collMod commands works for the sharded time-series collection.
- assert.commandWorked(
- db.runCommand({collMod: collName, index: {name: indexName, hidden: true}}));
- assert.commandWorked(
- db.runCommand({collMod: collName, index: {name: indexName, hidden: false}}));
+ // Check that collMod commands works for the sharded time-series collection.
+ assert.commandWorked(db[collName].createIndex({'a': 1}));
+ assert.commandWorked(db.runCommand({collMod: collName, index: {name: 'a_1', hidden: true}}));
+ assert.commandWorked(db.runCommand({collMod: collName, index: {name: 'a_1', hidden: false}}));
// Granularity update works for sharded time-series collection, when we're using DDL
// coordinator logic.