summaryrefslogtreecommitdiff
path: root/jstests/core
diff options
context:
space:
mode:
Diffstat (limited to 'jstests/core')
-rw-r--r--jstests/core/apply_ops1.js11
-rw-r--r--jstests/core/apply_ops2.js3
-rw-r--r--jstests/core/apply_ops_atomicity.js27
-rw-r--r--jstests/core/bypass_doc_validation.js4
4 files changed, 38 insertions, 7 deletions
diff --git a/jstests/core/apply_ops1.js b/jstests/core/apply_ops1.js
index 96840f125e9..34a38185c01 100644
--- a/jstests/core/apply_ops1.js
+++ b/jstests/core/apply_ops1.js
@@ -39,20 +39,23 @@
assert.commandWorked(db.adminCommand({applyOps: [{op: 'n', ns: ''}]}),
'applyOps should work on no op operation with empty "ns" field value');
+ // Missing dbname in 'ns' field.
+ assert.commandFailed(db.adminCommand({applyOps: [{op: 'd', ns: t.getName(), o: {_id: 1}}]}));
+
// Missing 'o' field value in an operation of type 'c' (command).
- assert.commandFailed(db.adminCommand({applyOps: [{op: 'c', ns: 'foo'}]}),
+ assert.commandFailed(db.adminCommand({applyOps: [{op: 'c', ns: t.getFullName()}]}),
'applyOps should fail on command operation without "o" field');
// Non-object 'o' field value in an operation of type 'c' (command).
- assert.commandFailed(db.adminCommand({applyOps: [{op: 'c', ns: 'foo', o: 'bar'}]}),
+ assert.commandFailed(db.adminCommand({applyOps: [{op: 'c', ns: t.getFullName(), o: 'bar'}]}),
'applyOps should fail on command operation with non-object "o" field');
// Empty object 'o' field value in an operation of type 'c' (command).
- assert.commandFailed(db.adminCommand({applyOps: [{op: 'c', ns: 'foo', o: {}}]}),
+ assert.commandFailed(db.adminCommand({applyOps: [{op: 'c', ns: t.getFullName(), o: {}}]}),
'applyOps should fail on command operation with empty object "o" field');
// Unknown key in 'o' field value in an operation of type 'c' (command).
- assert.commandFailed(db.adminCommand({applyOps: [{op: 'c', ns: 'foo', o: {a: 1}}]}),
+ assert.commandFailed(db.adminCommand({applyOps: [{op: 'c', ns: t.getFullName(), o: {a: 1}}]}),
'applyOps should fail on command operation on unknown key in "o" field');
// Empty 'ns' field value in operation type other than 'n'.
diff --git a/jstests/core/apply_ops2.js b/jstests/core/apply_ops2.js
index bf804214846..4e99cee37ff 100644
--- a/jstests/core/apply_ops2.js
+++ b/jstests/core/apply_ops2.js
@@ -33,7 +33,8 @@ res = db.runCommand({
alwaysUpsert: false
});
-assert.eq(true, res.results[0], "upsert = false, existing doc update failed");
+// Because the CRUD apply-ops are atomic, all results are false, even the first one.
+assert.eq(false, res.results[0], "upsert = false, existing doc update failed");
assert.eq(false, res.results[1], "upsert = false, nonexisting doc upserted");
assert.eq(2, t.find().count(), "2 docs expected after upsert failure");
diff --git a/jstests/core/apply_ops_atomicity.js b/jstests/core/apply_ops_atomicity.js
new file mode 100644
index 00000000000..2d093555200
--- /dev/null
+++ b/jstests/core/apply_ops_atomicity.js
@@ -0,0 +1,27 @@
+// SERVER-23326: Make applyOps atomic for CRUD operations
+(function() {
+ 'use strict';
+
+ var t = db.applyOps;
+ t.drop();
+ assert.writeOK(t.insert({_id: 1}));
+
+ // Operations including commands should not be atomic, so the insert will succeed.
+ assert.commandFailed(db.adminCommand({
+ applyOps: [
+ {op: 'i', ns: t.getFullName(), o: {_id: ObjectId(), x: 1}},
+ {op: 'c', ns: "invalid", o: {create: "t"}},
+ ]
+ }));
+ assert.eq(t.count({x: 1}), 1);
+
+ // Operations only including CRUD commands should be atomic, so the next insert will fail.
+ var tooLong = Array(2000).join("hello");
+ assert.commandFailed(db.adminCommand({
+ applyOps: [
+ {op: 'i', ns: t.getFullName(), o: {_id: ObjectId(), x: 1}},
+ {op: 'i', ns: t.getFullName(), o: {_id: tooLong, x: 1}},
+ ]
+ }));
+ assert.eq(t.count({x: 1}), 1);
+})();
diff --git a/jstests/core/bypass_doc_validation.js b/jstests/core/bypass_doc_validation.js
index 79a2eb7d4a2..d9bca81ab6d 100644
--- a/jstests/core/bypass_doc_validation.js
+++ b/jstests/core/bypass_doc_validation.js
@@ -19,9 +19,9 @@
// Test applyOps with a simple insert if not on mongos.
if (!db.runCommand({isdbgrid: 1}).isdbgrid) {
var op = [{ts: Timestamp(0, 0), h: 1, v: 2, op: 'i', ns: coll.getFullName(), o: {_id: 9}}];
- // SERVER-21345: applyOps is returning UnknownError instead of DocumentValidationFailure
assert.commandFailedWithCode(
- myDb.runCommand({applyOps: op, bypassDocumentValidation: false}), 8);
+ myDb.runCommand({applyOps: op, bypassDocumentValidation: false}),
+ ErrorCodes.DocumentValidationFailure);
assert.eq(0, coll.count({_id: 9}));
assert.commandWorked(myDb.runCommand({applyOps: op, bypassDocumentValidation: true}));
assert.eq(1, coll.count({_id: 9}));