summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/set_user_write_block_mode.js
diff options
context:
space:
mode:
authorGabriel Marks <gabriel.marks@mongodb.com>2022-03-03 15:15:50 +0000
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2022-03-03 16:33:46 +0000
commit41125e11f9ddb9426e2f8faca2449da0405d3ed8 (patch)
tree2f6889086c2fcafec727bcd048acb8e3cef52b48 /jstests/noPassthrough/set_user_write_block_mode.js
parent3e50ef2b069bb09ab9e718730b55f4febe5e4b8e (diff)
downloadmongo-41125e11f9ddb9426e2f8faca2449da0405d3ed8.tar.gz
SERVER-63521 Add UserWriteBlockModeOpObserver to enforce write blocking
Diffstat (limited to 'jstests/noPassthrough/set_user_write_block_mode.js')
-rw-r--r--jstests/noPassthrough/set_user_write_block_mode.js51
1 files changed, 51 insertions, 0 deletions
diff --git a/jstests/noPassthrough/set_user_write_block_mode.js b/jstests/noPassthrough/set_user_write_block_mode.js
new file mode 100644
index 00000000000..cf206309016
--- /dev/null
+++ b/jstests/noPassthrough/set_user_write_block_mode.js
@@ -0,0 +1,51 @@
+// Test setUserWriteBlockMode command.
+//
+// @tags: [
+// requires_fcv_53,
+// requires_non_retryable_commands,
+// requires_replication,
+// ]
+
+(function() {
+'use strict';
+
+const runTest = (frontend, backend) => {
+ const db = frontend.getDB(jsTestName());
+ const coll = db.test;
+ const admin = backend.getDB("admin");
+
+ assert.commandWorked(coll.insert({a: 2}));
+
+ // With setUserWriteBlockMode enabled, ensure that inserts, updates, and removes fail and don't
+ // modify data
+ assert.commandWorked(admin.runCommand({setUserWriteBlockMode: 1, global: true}));
+ assert.commandFailedWithCode(coll.insert({a: 1}), ErrorCodes.OperationFailed);
+ assert.commandFailedWithCode(coll.update({a: 2}, {a: 2, b: 2}), ErrorCodes.OperationFailed);
+ assert.commandFailedWithCode(coll.remove({a: 2}), ErrorCodes.OperationFailed);
+ assert.eq(1, coll.find({a: 2}).count());
+ assert.eq(0, coll.find({a: 1}).count());
+ assert.eq(0, coll.find({b: 2}).count());
+
+ // Disable userWriteBlockMode and ensure that the above operations now succeed and modify data
+ assert.commandWorked(admin.runCommand({setUserWriteBlockMode: 1, global: false}));
+ assert.commandWorked(coll.insert({a: 1}));
+ assert.commandWorked(coll.update({a: 2}, {a: 2, b: 2}));
+ assert.eq(1, coll.find({a: 2, b: 2}).count());
+ assert.commandWorked(coll.remove({a: 2}));
+ assert.eq(0, coll.find({a: 2}).count());
+ assert.eq(1, coll.find({a: 1}).count());
+};
+
+// Test on standalone
+const conn = MongoRunner.runMongod();
+runTest(conn, conn);
+MongoRunner.stopMongod(conn);
+
+// Test on replset primary
+const rst = new ReplSetTest({nodes: 3});
+rst.startSet();
+rst.initiate();
+const primary = rst.getPrimary();
+runTest(primary, primary);
+rst.stopSet();
+})();