summaryrefslogtreecommitdiff
path: root/jstests
diff options
context:
space:
mode:
authorYuhong Zhang <yuhong.zhang@mongodb.com>2022-02-09 03:22:20 +0000
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2022-02-11 20:03:25 +0000
commit76c21bda5674f7c38f2bf52be9abda6ff1cd77e3 (patch)
treec0a67e6e8ba300fd6306eca1edecf8af0e2bed65 /jstests
parent7f30babba00b8bea2df2f1d62d3b29f3bf1f14fa (diff)
downloadmongo-76c21bda5674f7c38f2bf52be9abda6ff1cd77e3.tar.gz
SERVER-63443 Make `disallowNewDuplicateKeys` persisted in catalog
Diffstat (limited to 'jstests')
-rw-r--r--jstests/noPassthrough/collmod_disallow_duplicates_restart.js63
-rw-r--r--jstests/noPassthrough/collmod_disallow_duplicates_step_up.js63
2 files changed, 126 insertions, 0 deletions
diff --git a/jstests/noPassthrough/collmod_disallow_duplicates_restart.js b/jstests/noPassthrough/collmod_disallow_duplicates_restart.js
new file mode 100644
index 00000000000..ea03c03613d
--- /dev/null
+++ b/jstests/noPassthrough/collmod_disallow_duplicates_restart.js
@@ -0,0 +1,63 @@
+/**
+ * Tests that the collMod command disallows writes that introduce new duplicate keys and the option
+ * is persisted over restarts.
+ *
+ * @tags: [
+ * # TODO(SERVER-61181): Fix validation errors under ephemeralForTest.
+ * incompatible_with_eft,
+ * requires_persistence,
+ * # Replication requires journaling support so this tag also implies exclusion from
+ * # --nojournal test configurations.
+ * requires_replication,
+ * ]
+ */
+
+(function() {
+'use strict';
+
+const rst = new ReplSetTest({nodes: 1});
+rst.startSet();
+rst.initiate();
+
+let primary = rst.getPrimary();
+const collModIndexUniqueEnabled =
+ assert.commandWorked(primary.adminCommand({getParameter: 1, featureFlagCollModIndexUnique: 1}))
+ .featureFlagCollModIndexUnique.value;
+
+if (!collModIndexUniqueEnabled) {
+ jsTestLog('Skipping test because the collMod unique index feature flag is disabled');
+ rst.stopSet();
+ return;
+}
+
+const collName = 'collmod_disallow_duplicates_step_up';
+let db_primary = primary.getDB('test');
+let coll_primary = db_primary.getCollection(collName);
+
+// Sets 'disallowNewDuplicateKeys' and checks that the index rejects duplicates.
+coll_primary.drop();
+assert.commandWorked(coll_primary.createIndex({a: 1}));
+assert.commandWorked(coll_primary.insert({_id: 0, a: 1}));
+assert.commandWorked(db_primary.runCommand(
+ {collMod: collName, index: {keyPattern: {a: 1}, disallowNewDuplicateKeys: true}}));
+assert.commandFailedWithCode(coll_primary.insert({_id: 1, a: 1}), ErrorCodes.DuplicateKey);
+
+// Restarts the primary and checks the index spec is persisted.
+rst.restart(primary);
+rst.waitForPrimary();
+primary = rst.getPrimary();
+db_primary = primary.getDB('test');
+coll_primary = db_primary.getCollection(collName);
+assert.commandFailedWithCode(coll_primary.insert({_id: 1, a: 1}), ErrorCodes.DuplicateKey);
+
+// Converts the index to unique.
+assert.commandWorked(
+ db_primary.runCommand({collMod: collName, index: {keyPattern: {a: 1}, unique: true}}));
+
+const uniqueIndexes = coll_primary.getIndexes().filter(function(doc) {
+ return doc.unique && friendlyEqual(doc.key, {a: 1});
+});
+assert.eq(1, uniqueIndexes.length);
+
+rst.stopSet();
+})(); \ No newline at end of file
diff --git a/jstests/noPassthrough/collmod_disallow_duplicates_step_up.js b/jstests/noPassthrough/collmod_disallow_duplicates_step_up.js
new file mode 100644
index 00000000000..9d6908e3590
--- /dev/null
+++ b/jstests/noPassthrough/collmod_disallow_duplicates_step_up.js
@@ -0,0 +1,63 @@
+/**
+ * Tests that the collMod command disallows writes that introduce new duplicate keys and the option
+ * is persisted on all nodes in the replica set.
+ *
+ * @tags: [
+ * # TODO(SERVER-61181): Fix validation errors under ephemeralForTest.
+ * incompatible_with_eft,
+ * # TODO(SERVER-61182): Fix WiredTigerKVEngine::alterIdentMetadata() under inMemory.
+ * requires_persistence,
+ * # Replication requires journaling support so this tag also implies exclusion from
+ * # --nojournal test configurations.
+ * requires_replication,
+ * ]
+ */
+
+(function() {
+'use strict';
+
+const rst = new ReplSetTest({nodes: 3});
+rst.startSet();
+rst.initiate();
+
+const primary = rst.getPrimary();
+const [secondary, _] = rst.getSecondaries();
+const collModIndexUniqueEnabled =
+ assert.commandWorked(primary.adminCommand({getParameter: 1, featureFlagCollModIndexUnique: 1}))
+ .featureFlagCollModIndexUnique.value;
+
+if (!collModIndexUniqueEnabled) {
+ jsTestLog('Skipping test because the collMod unique index feature flag is disabled');
+ rst.stopSet();
+ return;
+}
+
+const collName = 'collmod_disallow_duplicates_step_up';
+const db_primary = primary.getDB('test');
+const coll_primary = db_primary.getCollection(collName);
+const db_secondary = secondary.getDB('test');
+const coll_secondary = db_secondary.getCollection(collName);
+
+// Sets 'disallowNewDuplicateKeys' on the old primary and checks that the index rejects duplicates.
+coll_primary.drop();
+assert.commandWorked(coll_primary.createIndex({a: 1}));
+assert.commandWorked(coll_primary.insert({_id: 0, a: 1}));
+assert.commandWorked(db_primary.runCommand(
+ {collMod: collName, index: {keyPattern: {a: 1}, disallowNewDuplicateKeys: true}}));
+assert.commandFailedWithCode(coll_primary.insert({_id: 1, a: 1}), ErrorCodes.DuplicateKey);
+
+// Steps up a new primary and checks the index spec is replicated.
+rst.stepUp(secondary);
+assert.commandFailedWithCode(coll_secondary.insert({_id: 1, a: 1}), ErrorCodes.DuplicateKey);
+
+// Converts the index to unique on the new primary.
+assert.commandWorked(
+ db_secondary.runCommand({collMod: collName, index: {keyPattern: {a: 1}, unique: true}}));
+
+const uniqueIndexes = coll_secondary.getIndexes().filter(function(doc) {
+ return doc.unique && friendlyEqual(doc.key, {a: 1});
+});
+assert.eq(1, uniqueIndexes.length);
+
+rst.stopSet();
+})(); \ No newline at end of file