jstests/noPassthrough/change_stream_preimages_standalone_mode.js
/**
 * Tests the pre-image recording behavior when the server transitions to and from the stand-alone
 * mode for a collection with specified 'collectionOptions'.
 *
 * @param {function} assertPreImagesRecordingEnabledFunc - asserts that pre-images
 *     recording collection option is enabled for the specified collection.
 * @param {function} assertPreImagesRecordedFunc - asserts that pre-images are recorded while
 *     executing the 'writeOps' (update/replace/delete operations) on the collection.
 * @param {function} assertNoPreImagesRecordedFunc - asserts that no pre-images are recorded while
 *     executing the 'writeOps' (update/replace/delete operations) on the collection.
 */
Tests the pre-image recording behavior when the server transitions to and from the stand-alone
mode for a collection with specified 'collectionOptions'.

@param {function} assertPreImagesRecordingEnabledFunc - asserts that pre-images
recording collection option is enabled for the specified collection.
@param {function} assertPreImagesRecordedFunc - asserts that pre-images are recorded while
executing the 'writeOps' (update/replace/delete operations) on the collection.
@param {function} assertNoPreImagesRecordedFunc - asserts that no pre-images are recorded while
executing the 'writeOps' (update/replace/delete operations) on the collection.
Tests the pre - image recording behavior when the server transitions to and from the stand - alone mode for a collection with specified ' collectionOptions ' . @param { function } assertPreImagesRecordingEnabledFunc - asserts that pre - images recording collection option is enabled for the specified collection . @param { function } assertPreImagesRecordedFunc - asserts that pre - images are recorded while executing the ' writeOps ' ( update / replace / delete operations ) on the collection . @param { function } assertNoPreImagesRecordedFunc - asserts that no pre - images are recorded while executing the ' writeOps ' ( update / replace / delete operations ) on the collection .
function testStandaloneMode({
    collectionOptions = {},
    assertPreImagesRecordingEnabledFunc = (db, collName) => {},
    assertPreImagesRecordedFunc = (db, writeOps) => {},
    assertNoPreImagesRecordedFunc = (db, writeOps) => {}
}) {
    const rst = new ReplSetTest({nodes: 1});
    rst.startSet();
    rst.initiate();
    const collName = "coll";
    const dbName = jsTestName();
    let primary = rst.getPrimary();
    let testDB = primary.getDB(dbName);
    let testColl = assertDropAndRecreateCollection(testDB, collName, collectionOptions);
    assertPreImagesRecordingEnabledFunc(testDB, collName);
    const writeOpsForReplSetMode = () => {
        assert.commandWorked(testColl.insert({a: 1, b: 1}));
        assert.commandWorked(testColl.update({a: 1}, {a: 2, b: 2}));
        assert.commandWorked(
            testColl.update({a: 2}, {a: 3, b: 3}, {writeConcern: {w: 1, j: true}}));
        assert.commandWorked(testDB.adminCommand({fsync: 1}));
    };
    assertPreImagesRecordedFunc(testDB, writeOpsForReplSetMode);
    const replicaSetNodeId = rst.getNodeId(primary);
    const replicaSetNodeDBPath = primary.dbpath;
    rst.stop(replicaSetNodeId);
    const standaloneConn = MongoRunner.runMongod({
        dbpath: replicaSetNodeDBPath,
        noCleanData: true,
    });
    const standaloneDB = standaloneConn.getDB(dbName);
    const standaloneColl = standaloneDB.getCollection(collName);
    assertPreImagesRecordingEnabledFunc(standaloneDB, collName);
    const writeOpsForStandaloneMode = () => {
        assert.commandWorked(standaloneColl.insert({c: 1, d: 1}));
        assert.commandWorked(standaloneColl.update({c: 1}, {c: 2, d: 2}));
        assert.commandWorked(standaloneColl.update({c: 2}, {c: 3, d: 3}));
        assert.commandWorked(standaloneColl.insert({c: 1, d: 1}));
        assert.commandWorked(standaloneColl.remove({c: 1, d: 1}));
    };
    assertNoPreImagesRecordedFunc(standaloneDB, writeOpsForStandaloneMode);
    MongoRunner.stopMongod(standaloneConn);
    rst.start(replicaSetNodeId, {}, true );
    primary = rst.getPrimary();
    testDB = primary.getDB(dbName);
    testColl = testDB.getCollection(collName);
    assertPreImagesRecordingEnabledFunc(testDB, collName);
    const writeOpsForReplSetModeAfterStandalone = () => {
        assert.commandWorked(testColl.update({a: 3}, {a: 4, b: 4}));
        assert.commandWorked(testColl.update({a: 4}, {a: 5, b: 5}));
    };
    assertPreImagesRecordedFunc(testDB, writeOpsForReplSetModeAfterStandalone);
    rst.stopSet();
}
function testStandaloneMode ( { collectionOptions = { } , assertPreImagesRecordingEnabledFunc = ( db , collName ) => { } , assertPreImagesRecordedFunc = ( db , writeOps ) => { } , assertNoPreImagesRecordedFunc = ( db , writeOps ) => { } } ) { const rst = new ReplSetTest ( { nodes : 1 } ) ; rst . startSet ( ) ; rst . initiate ( ) ; const collName = "coll" ; const dbName = jsTestName ( ) ; let primary = rst . getPrimary ( ) ; let testDB = primary . getDB ( dbName ) ; let testColl = assertDropAndRecreateCollection ( testDB , collName , collectionOptions ) ; assertPreImagesRecordingEnabledFunc ( testDB , collName ) ; const writeOpsForReplSetMode = ( ) => { assert . commandWorked ( testColl . insert ( { a : 1 , b : 1 } ) ) ; assert . commandWorked ( testColl . update ( { a : 1 } , { a : 2 , b : 2 } ) ) ; assert . commandWorked ( testColl . update ( { a : 2 } , { a : 3 , b : 3 } , { writeConcern : { w : 1 , j : true } } ) ) ; assert . commandWorked ( testDB . adminCommand ( { fsync : 1 } ) ) ; } ; assertPreImagesRecordedFunc ( testDB , writeOpsForReplSetMode ) ; const replicaSetNodeId = rst . getNodeId ( primary ) ; const replicaSetNodeDBPath = primary . dbpath ; rst . stop ( replicaSetNodeId ) ; const standaloneConn = MongoRunner . runMongod ( { dbpath : replicaSetNodeDBPath , noCleanData : true , } ) ; const standaloneDB = standaloneConn . getDB ( dbName ) ; const standaloneColl = standaloneDB . getCollection ( collName ) ; assertPreImagesRecordingEnabledFunc ( standaloneDB , collName ) ; const writeOpsForStandaloneMode = ( ) => { assert . commandWorked ( standaloneColl . insert ( { c : 1 , d : 1 } ) ) ; assert . commandWorked ( standaloneColl . update ( { c : 1 } , { c : 2 , d : 2 } ) ) ; assert . commandWorked ( standaloneColl . update ( { c : 2 } , { c : 3 , d : 3 } ) ) ; assert . commandWorked ( standaloneColl . insert ( { c : 1 , d : 1 } ) ) ; assert . commandWorked ( standaloneColl . remove ( { c : 1 , d : 1 } ) ) ; } ; assertNoPreImagesRecordedFunc ( standaloneDB , writeOpsForStandaloneMode ) ; MongoRunner . stopMongod ( standaloneConn ) ; rst . start ( replicaSetNodeId , { } , true ) ; primary = rst . getPrimary ( ) ; testDB = primary . getDB ( dbName ) ; testColl = testDB . getCollection ( collName ) ; assertPreImagesRecordingEnabledFunc ( testDB , collName ) ; const writeOpsForReplSetModeAfterStandalone = ( ) => { assert . commandWorked ( testColl . update ( { a : 3 } , { a : 4 , b : 4 } ) ) ; assert . commandWorked ( testColl . update ( { a : 4 } , { a : 5 , b : 5 } ) ) ; } ; assertPreImagesRecordedFunc ( testDB , writeOpsForReplSetModeAfterStandalone ) ; rst . stopSet ( ) ; }
Tests the pre-image recording behavior when the server transitions to and from the stand-alone
 mode for a collection with specified 'collectionOptions'.
Tests the pre - image recording behavior when the server transitions to and from the stand - alone mode for a collection with specified ' collectionOptions ' .
// Create a collection with pre-images recording collection option enabled. // Verify that pre-images are recorded for the specified operations. // Ensure that the last write with j:true write concern has reached the disk, and now fsync // will checkpoint that data. // Restart the replica set member as a standalone node. // The collection must have pre-images recording option enabled even when running in standalone // mode. // Verify that no pre-images are recorded while running in standalone mode. // Shut down standalone server. // Restart the node as a replica set member. /*restart*/ // Check that everything is still working properly after being in standalone mode.