0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-11-24 00:17:37 +01:00
mongodb/jstests/sharding/drop_collection_if_uuid_not_matching.js
Matt Broadstone 771dabd098 SERVER-81339 Convert ReplSetTest and ShardingTest to modules (#26332)
GitOrigin-RevId: 744aa110a53786b23c62ff53f87a1418b5991e8d
2024-08-20 22:00:49 +00:00

63 lines
2.4 KiB
JavaScript

/**
* Tests that the _shardsvrDropCollectionIfUUIDNotMatchingWithWriteConcern command works as
* expected:
* - Noop in case the collection doesn't exist.
* - Drop collection if uuid different from the expected.
* - Keep the collection if the uuid is exactly the expected one.
*
* @tags: [
* does_not_support_stepdowns, # The command is not resilient to stepdowns
* ]
*/
import {ShardingTest} from "jstests/libs/shardingtest.js";
// This test requires running commands directly against the shard.
TestData.replicaSetEndpointIncompatible = true;
const dbName = "test";
const st = new ShardingTest({shards: 1});
const mongos = st.s;
const db = st.rs0.getPrimary().getDB(dbName);
assert.commandWorked(mongos.adminCommand({enableSharding: dbName}));
function appenWriteConcern(command, writeConcern) {
if (writeConcern) {
Object.assign(command, writeConcern);
return command;
} else {
return command;
}
}
function runTests(collName, commandName, writeConcern) {
const ns = dbName + "." + collName;
// Non-existing collection with a random expected UUID (command succeeds, noop)
assert.commandWorked(db.runCommand(appenWriteConcern(
{[`${commandName}`]: collName, expectedCollectionUUID: UUID()}, writeConcern)));
// Existing collection with a random expected UUID (command succeeds after successful drop)
assert.commandWorked(db.getCollection(collName).insert({_id: 0}));
assert.commandWorked(db.runCommand(appenWriteConcern(
{[`${commandName}`]: collName, expectedCollectionUUID: UUID()}, writeConcern)));
assert.eq(null, db.getCollection(collName).findOne({_id: 0}));
// Existing collection with the expected UUID (command succeeds but no drop)
assert.commandWorked(st.s.adminCommand({shardCollection: ns, key: {_id: 1}}));
// Read from the mongoS to ensure the shard has refreshed its filtering information.
assert.eq(0, st.s.getDB(dbName).getCollection(collName).countDocuments({}));
const collUUID = st.config.collections.findOne({_id: ns}).uuid;
assert.commandWorked(db.getCollection(collName).insert({_id: 0}));
assert.commandWorked(db.runCommand(appenWriteConcern(
{[`${commandName}`]: collName, expectedCollectionUUID: collUUID}, writeConcern)));
assert.neq(null, db.getCollection(collName).findOne({_id: 0}));
}
runTests("coll2",
"_shardsvrDropCollectionIfUUIDNotMatchingWithWriteConcern",
{writeConcern: {w: 'majority'}});
st.stop();