diff options
Diffstat (limited to 'src/mongo/s/catalog/type_chunk_test.cpp')
-rw-r--r-- | src/mongo/s/catalog/type_chunk_test.cpp | 130 |
1 files changed, 96 insertions, 34 deletions
diff --git a/src/mongo/s/catalog/type_chunk_test.cpp b/src/mongo/s/catalog/type_chunk_test.cpp index bcf1470240c..4589e4d4c38 100644 --- a/src/mongo/s/catalog/type_chunk_test.cpp +++ b/src/mongo/s/catalog/type_chunk_test.cpp @@ -35,14 +35,17 @@ #include "mongo/unittest/unittest.h" #include "mongo/util/time_support.h" +namespace mongo { namespace { -using namespace mongo; - using std::string; using unittest::assertGet; -TEST(ChunkType, MissingRequiredFields) { +const BSONObj kMin = BSON("a" << 10); +const BSONObj kMax = BSON("a" << 20); +const ShardId kShard("shard0000"); + +TEST(ChunkType, MissingConfigRequiredFields) { ChunkVersion chunkVersion(1, 2, OID::gen()); BSONObj objModNS = @@ -53,7 +56,7 @@ TEST(ChunkType, MissingRequiredFields) { << "lastmodEpoch" << chunkVersion.epoch() << ChunkType::shard("shard0001")); - StatusWith<ChunkType> chunkRes = ChunkType::fromBSON(objModNS); + StatusWith<ChunkType> chunkRes = ChunkType::fromConfigBSON(objModNS); ASSERT_FALSE(chunkRes.isOK()); BSONObj objModKeys = @@ -62,7 +65,7 @@ TEST(ChunkType, MissingRequiredFields) { << "lastmodEpoch" << chunkVersion.epoch() << ChunkType::shard("shard0001")); - chunkRes = ChunkType::fromBSON(objModKeys); + chunkRes = ChunkType::fromConfigBSON(objModKeys); ASSERT_FALSE(chunkRes.isOK()); BSONObj objModShard = @@ -73,10 +76,66 @@ TEST(ChunkType, MissingRequiredFields) { << Timestamp(chunkVersion.toLong()) << "lastmodEpoch" << chunkVersion.epoch()); - chunkRes = ChunkType::fromBSON(objModShard); + chunkRes = ChunkType::fromConfigBSON(objModShard); + ASSERT_FALSE(chunkRes.isOK()); + + BSONObj objModVersion = + BSON(ChunkType::name("test.mycol-a_MinKey") << ChunkType::ns("test.mycol") + << ChunkType::min(BSON("a" << 10 << "b" << 10)) + << ChunkType::max(BSON("a" << 20)) + << ChunkType::shard("shard0001")); + chunkRes = ChunkType::fromConfigBSON(objModVersion); ASSERT_FALSE(chunkRes.isOK()); } +TEST(ChunkType, MissingShardRequiredFields) { + const OID epoch = OID::gen(); + ChunkVersion chunkVersion(1, 2, epoch); + const auto lastmod = Timestamp(chunkVersion.toLong()); + + BSONObj objModMin = + BSON(ChunkType::max(kMax) << ChunkType::shard(kShard.toString()) << "lastmod" << lastmod); + StatusWith<ChunkType> chunkRes = ChunkType::fromShardBSON(objModMin, epoch); + ASSERT_EQUALS(chunkRes.getStatus(), ErrorCodes::NoSuchKey); + ASSERT_STRING_CONTAINS(chunkRes.getStatus().reason(), ChunkType::minShardID.name()); + + BSONObj objModMax = BSON( + ChunkType::minShardID(kMin) << ChunkType::shard(kShard.toString()) << "lastmod" << lastmod); + chunkRes = ChunkType::fromShardBSON(objModMax, epoch); + ASSERT_EQUALS(chunkRes.getStatus(), ErrorCodes::NoSuchKey); + ASSERT_STRING_CONTAINS(chunkRes.getStatus().reason(), ChunkType::max.name()); + + BSONObj objModShard = + BSON(ChunkType::minShardID(kMin) << ChunkType::max(kMax) << "lastmod" << lastmod); + chunkRes = ChunkType::fromShardBSON(objModShard, epoch); + ASSERT_EQUALS(chunkRes.getStatus(), ErrorCodes::NoSuchKey); + ASSERT_STRING_CONTAINS(chunkRes.getStatus().reason(), ChunkType::shard.name()); + + BSONObj objModLastmod = BSON( + ChunkType::minShardID(kMin) << ChunkType::max(kMax) << ChunkType::shard(kShard.toString())); + chunkRes = ChunkType::fromShardBSON(objModLastmod, epoch); + ASSERT_EQUALS(chunkRes.getStatus(), ErrorCodes::BadValue); +} + +TEST(ChunkType, ToFromShardBSON) { + const OID epoch = OID::gen(); + ChunkVersion chunkVersion(1, 2, epoch); + auto lastmod = Timestamp(chunkVersion.toLong()); + + BSONObj obj = BSON(ChunkType::minShardID(kMin) << ChunkType::max(kMax) + << ChunkType::shard(kShard.toString()) + << "lastmod" + << lastmod); + ChunkType shardChunk = assertGet(ChunkType::fromShardBSON(obj, epoch)); + + ASSERT_BSONOBJ_EQ(obj, shardChunk.toShardBSON()); + + ASSERT_BSONOBJ_EQ(kMin, shardChunk.getMin()); + ASSERT_BSONOBJ_EQ(kMax, shardChunk.getMax()); + ASSERT_EQUALS(kShard, shardChunk.getShard()); + ASSERT_EQUALS(chunkVersion, shardChunk.getVersion()); +} + TEST(ChunkType, MinAndMaxShardKeysDifferInNumberOfKeys) { ChunkVersion chunkVersion(1, 2, OID::gen()); BSONObj obj = @@ -88,7 +147,7 @@ TEST(ChunkType, MinAndMaxShardKeysDifferInNumberOfKeys) { << "lastmodEpoch" << chunkVersion.epoch() << ChunkType::shard("shard0001")); - StatusWith<ChunkType> chunkRes = ChunkType::fromBSON(obj); + StatusWith<ChunkType> chunkRes = ChunkType::fromConfigBSON(obj); ASSERT_OK(chunkRes.getStatus()); ASSERT_FALSE(chunkRes.getValue().validate().isOK()); } @@ -103,12 +162,12 @@ TEST(ChunkType, MinAndMaxShardKeysDifferInKeyNames) { << "lastmodEpoch" << chunkVersion.epoch() << ChunkType::shard("shard0001")); - StatusWith<ChunkType> chunkRes = ChunkType::fromBSON(obj); + StatusWith<ChunkType> chunkRes = ChunkType::fromConfigBSON(obj); ASSERT_OK(chunkRes.getStatus()); ASSERT_FALSE(chunkRes.getValue().validate().isOK()); } -TEST(ChunkType, NotAscending) { +TEST(ChunkType, MinToMaxNotAscending) { ChunkVersion chunkVersion(1, 2, OID::gen()); BSONObj obj = BSON(ChunkType::name("test.mycol-a_MinKey") << ChunkType::ns("test.mycol") << ChunkType::min(BSON("a" << 20)) @@ -118,24 +177,26 @@ TEST(ChunkType, NotAscending) { << "lastmodEpoch" << chunkVersion.epoch() << ChunkType::shard("shard0001")); - StatusWith<ChunkType> chunkRes = ChunkType::fromBSON(obj); + StatusWith<ChunkType> chunkRes = ChunkType::fromConfigBSON(obj); ASSERT_EQ(ErrorCodes::FailedToParse, chunkRes.getStatus()); } -TEST(ChunkType, CorrectContents) { +TEST(ChunkType, ToFromConfigBSON) { ChunkVersion chunkVersion(1, 2, OID::gen()); - BSONObj obj = BSON(ChunkType::name("test.mycol-a_MinKey") << ChunkType::ns("test.mycol") - << ChunkType::min(BSON("a" << 10)) - << ChunkType::max(BSON("a" << 20)) - << "lastmod" - << Timestamp(chunkVersion.toLong()) - << "lastmodEpoch" - << chunkVersion.epoch() - << ChunkType::shard("shard0001")); - StatusWith<ChunkType> chunkRes = ChunkType::fromBSON(obj); + BSONObj obj = BSON(ChunkType::name("test.mycol-a_10") << ChunkType::ns("test.mycol") + << ChunkType::min(BSON("a" << 10)) + << ChunkType::max(BSON("a" << 20)) + << ChunkType::shard("shard0001") + << "lastmod" + << Timestamp(chunkVersion.toLong()) + << "lastmodEpoch" + << chunkVersion.epoch()); + StatusWith<ChunkType> chunkRes = ChunkType::fromConfigBSON(obj); ASSERT_OK(chunkRes.getStatus()); ChunkType chunk = chunkRes.getValue(); + ASSERT_BSONOBJ_EQ(chunk.toConfigBSON(), obj); + ASSERT_EQUALS(chunk.getNS(), "test.mycol"); ASSERT_BSONOBJ_EQ(chunk.getMin(), BSON("a" << 10)); ASSERT_BSONOBJ_EQ(chunk.getMax(), BSON("a" << 20)); @@ -146,18 +207,18 @@ TEST(ChunkType, CorrectContents) { } TEST(ChunkType, Pre22Format) { - ChunkType chunk = assertGet(ChunkType::fromBSON(BSON("_id" - << "test.mycol-a_MinKey" - << "lastmod" - << Date_t::fromMillisSinceEpoch(1) - << "ns" - << "test.mycol" - << "min" - << BSON("a" << 10) - << "max" - << BSON("a" << 20) - << "shard" - << "shard0001"))); + ChunkType chunk = assertGet(ChunkType::fromConfigBSON(BSON("_id" + << "test.mycol-a_MinKey" + << "lastmod" + << Date_t::fromMillisSinceEpoch(1) + << "ns" + << "test.mycol" + << "min" + << BSON("a" << 10) + << "max" + << BSON("a" << 20) + << "shard" + << "shard0001"))); ASSERT_OK(chunk.validate()); ASSERT_EQUALS(chunk.getNS(), "test.mycol"); @@ -170,7 +231,7 @@ TEST(ChunkType, Pre22Format) { TEST(ChunkType, BadType) { BSONObj obj = BSON(ChunkType::name() << 0); - StatusWith<ChunkType> chunkRes = ChunkType::fromBSON(obj); + StatusWith<ChunkType> chunkRes = ChunkType::fromConfigBSON(obj); ASSERT_FALSE(chunkRes.isOK()); } @@ -190,4 +251,5 @@ TEST(ChunkRange, MinGreaterThanMaxShouldError) { ASSERT_EQ(ErrorCodes::FailedToParse, parseStatus.getStatus()); } -} // unnamed namespace +} // namespace +} // namespace mongo |