diff options
author | Antonio Fuschetto <antonio.fuschetto@mongodb.com> | 2022-02-02 13:57:50 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2022-02-02 15:27:01 +0000 |
commit | b96cf6c3cd26e240b84e40756b148b2e3cfde063 (patch) | |
tree | 655e663f0e54261a27cdde93d635a9f10ff3af16 /src/mongo/db/s/sharding_ddl_util_test.cpp | |
parent | d6a545506f7842ec6bc369ed7aedcdb8c2d40cd6 (diff) | |
download | mongo-b96cf6c3cd26e240b84e40756b148b2e3cfde063.tar.gz |
SERVER-62906 Add a check in the createCollection/shardCollection path verifying the collection name length
Diffstat (limited to 'src/mongo/db/s/sharding_ddl_util_test.cpp')
-rw-r--r-- | src/mongo/db/s/sharding_ddl_util_test.cpp | 55 |
1 files changed, 39 insertions, 16 deletions
diff --git a/src/mongo/db/s/sharding_ddl_util_test.cpp b/src/mongo/db/s/sharding_ddl_util_test.cpp index eacd1372f5f..044e11ccda9 100644 --- a/src/mongo/db/s/sharding_ddl_util_test.cpp +++ b/src/mongo/db/s/sharding_ddl_util_test.cpp @@ -198,14 +198,16 @@ TEST_F(ShardingDDLUtilTest, ShardedRenameMetadata) { } } -// Test all combinations of sharded rename acceptable preconditions: -// (1) Target collection doesn't exist and doesn't have no associated tags -// (2) Target collection exists and doesn't have associated tags -TEST_F(ShardingDDLUtilTest, ShardedRenamePreconditionsAreMet) { +// Test all combinations of rename acceptable preconditions: +// (1) Namespace of target collection is not too long +// (2) Target collection doesn't exist and doesn't have no associated tags +// (3) Target collection exists and doesn't have associated tags +TEST_F(ShardingDDLUtilTest, RenamePreconditionsAreMet) { auto opCtx = operationContext(); // No exception is thrown if the TO collection does not exist and has no associated tags - sharding_ddl_util::checkShardedRenamePreconditions(opCtx, kToNss, false /* dropTarget */); + sharding_ddl_util::checkRenamePreconditions( + opCtx, false /* sourceIsSharded */, kToNss, false /* dropTarget */); // Initialize a chunk ChunkVersion chunkVersion(1, 1, OID::gen(), Timestamp(2, 1)); @@ -221,10 +223,31 @@ TEST_F(ShardingDDLUtilTest, ShardedRenamePreconditionsAreMet) { // Initialize the sharded TO collection setupCollection(kToNss, KeyPattern(BSON("x" << 1)), {chunk}); - sharding_ddl_util::checkShardedRenamePreconditions(opCtx, kToNss, true /* dropTarget */); + sharding_ddl_util::checkRenamePreconditions( + opCtx, false /* sourceIsSharded */, kToNss, true /* dropTarget */); } -TEST_F(ShardingDDLUtilTest, ShardedRenamePreconditionsTargetCollectionExists) { +TEST_F(ShardingDDLUtilTest, RenamePreconditionsTargetNamespaceIsTooLong) { + auto opCtx{operationContext()}; + + const std::string dbName{"test"}; + + // Check that no exception is thrown if the namespace of the target collection is long enough + const NamespaceString longEnoughNss{ + dbName + "." + + std::string(NamespaceString::MaxNsShardedCollectionLen - dbName.length() - 1, 'x')}; + sharding_ddl_util::checkRenamePreconditions( + opCtx, true /* sourceIsSharded */, longEnoughNss, false /* dropTarget */); + + // Check that an exception is thrown if the namespace of the target collection is too long + const NamespaceString tooLongNss{longEnoughNss.ns() + 'x'}; + ASSERT_THROWS_CODE(sharding_ddl_util::checkRenamePreconditions( + opCtx, true /* sourceIsSharded */, tooLongNss, false /* dropTarget */), + AssertionException, + ErrorCodes::InvalidNamespace); +} + +TEST_F(ShardingDDLUtilTest, RenamePreconditionsTargetCollectionExists) { auto opCtx = operationContext(); // Initialize a chunk @@ -242,13 +265,13 @@ TEST_F(ShardingDDLUtilTest, ShardedRenamePreconditionsTargetCollectionExists) { setupCollection(kToNss, KeyPattern(BSON("x" << 1)), {chunk}); // Check that an exception is thrown if the target collection exists and dropTarget is not set - ASSERT_THROWS_CODE( - sharding_ddl_util::checkShardedRenamePreconditions(opCtx, kToNss, false /* dropTarget */), - AssertionException, - ErrorCodes::NamespaceExists); + ASSERT_THROWS_CODE(sharding_ddl_util::checkRenamePreconditions( + opCtx, false /* sourceIsSharded */, kToNss, false /* dropTarget */), + AssertionException, + ErrorCodes::NamespaceExists); } -TEST_F(ShardingDDLUtilTest, ShardedRenamePreconditionTargetCollectionHasTags) { +TEST_F(ShardingDDLUtilTest, RenamePreconditionTargetCollectionHasTags) { auto opCtx = operationContext(); // Associate a tag to the target collection @@ -260,10 +283,10 @@ TEST_F(ShardingDDLUtilTest, ShardedRenamePreconditionTargetCollectionHasTags) { ASSERT_OK(insertToConfigCollection(operationContext(), TagsType::ConfigNS, tagDoc.toBSON())); // Check that an exception is thrown if some tag is associated to the target collection - ASSERT_THROWS_CODE( - sharding_ddl_util::checkShardedRenamePreconditions(opCtx, kToNss, false /* dropTarget */), - AssertionException, - ErrorCodes::CommandFailed); + ASSERT_THROWS_CODE(sharding_ddl_util::checkRenamePreconditions( + opCtx, false /* sourceIsSharded */, kToNss, false /* dropTarget */), + AssertionException, + ErrorCodes::CommandFailed); } } // namespace |