summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenety Goh <benety@mongodb.com>2018-09-24 15:16:22 -0400
committerBenety Goh <benety@mongodb.com>2018-09-25 16:42:12 -0400
commit1e4f12dc5e4c12e7656270907cf9d49715fa732f (patch)
treea9021c147310a97b12bb6fd64d6d777fcbda20bc
parent22296574a08f69f32c9b916cf2e2b1df8b6160e3 (diff)
downloadmongo-1e4f12dc5e4c12e7656270907cf9d49715fa732f.tar.gz
SERVER-36944 add characterization test for applyOps index creation
(cherry picked from commit 72ff3cb4ee0082e4310505fb10e32eaa133cb0a6) (cherry picked from commit 0e65a5855d5af2f08d8fcc330adfd7a3bb31a7b4)
-rw-r--r--jstests/core/apply_ops_invalid_index_spec.js58
1 files changed, 58 insertions, 0 deletions
diff --git a/jstests/core/apply_ops_invalid_index_spec.js b/jstests/core/apply_ops_invalid_index_spec.js
new file mode 100644
index 00000000000..47ff5e2e045
--- /dev/null
+++ b/jstests/core/apply_ops_invalid_index_spec.js
@@ -0,0 +1,58 @@
+/**
+ * Tests how applyOps handles index specs with unknown fields.
+ *
+ * We subject index specs with version 2 or later to stricter validation than version 1 index specs.
+ * When given an index spec with an unrecognized field, applyOps will reject v:2 indexes with an
+ * InvalidIndexSpecificationOption error while v:1 indexes are accepted as-is.
+ *
+ * @tags: [
+ * requires_non_retryable_commands,
+ * requires_fastcount,
+ *
+ * # applyOps uses the oplog which requires replication support.
+ * requires_replication,
+ * ]
+ */
+
+(function() {
+ 'use strict';
+
+ const t = db.apply_ops_invalid_index_spec;
+ t.drop();
+
+ const collNs = t.getFullName();
+ const cmdNs = db.getName() + '.$cmd';
+ const systemIndexesNs = db.getCollection('system.indexes').getFullName();
+
+ assert.commandWorked(db.createCollection(t.getName()));
+ assert.writeOK(t.save({_id: 100, a: 100}));
+
+ // Tests that db.collection.createIndex() fails when given an index spec containing an unknown
+ // field.
+ assert.commandFailedWithCode(t.createIndex({a: 1}, {v: 2, name: 'a_1_base_v2', unknown: 1}),
+ ErrorCodes.InvalidIndexSpecificationOption);
+ assert.commandFailedWithCode(t.createIndex({a: 1}, {v: 1, name: 'a_1_base_v1', unknown: 1}),
+ ErrorCodes.InvalidIndexSpecificationOption);
+
+ // Inserting a v:2 index directly into system.indexes with an unknown field in the index
+ // spec should return an error.
+ assert.commandFailedWithCode(db.adminCommand({
+ applyOps: [{
+ op: 'i',
+ ns: systemIndexesNs,
+ o: {v: 2, key: {a: 1}, name: 'a_1_system_v2', ns: collNs, unknown: 1},
+ }],
+ }),
+ ErrorCodes.UnknownError);
+
+ // Inserting a v:1 index directly into system.indexes with an unknown field in the index spec
+ // should ignore the unrecognized field and create the index.
+ assert.commandFailedWithCode(db.adminCommand({
+ applyOps: [{
+ op: 'i',
+ ns: systemIndexesNs,
+ o: {v: 1, key: {a: 1}, name: 'a_1_system_v1', ns: collNs, unknown: 1},
+ }],
+ }),
+ ErrorCodes.UnknownError);
+})();