summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYuhong Zhang <yuhong.zhang@mongodb.com>2022-04-13 18:21:10 +0000
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2022-04-15 14:37:23 +0000
commita59e32ba3883e46077a750d1f65f383e35ab3e12 (patch)
tree0b2c6340f6316bc476dcc92b11204927fb6c5584
parent248c9c2f35e0a0b1fcf1e56c35ed2fb06682cd14 (diff)
downloadmongo-a59e32ba3883e46077a750d1f65f383e35ab3e12.tar.gz
SERVER-64444 Add unittests for repairing and validating index specs
-rw-r--r--src/mongo/db/catalog/index_key_validate_test.cpp35
-rw-r--r--src/mongo/dbtests/validate_tests.cpp26
2 files changed, 61 insertions, 0 deletions
diff --git a/src/mongo/db/catalog/index_key_validate_test.cpp b/src/mongo/db/catalog/index_key_validate_test.cpp
index 2c25d1b8791..adfd2e25a5a 100644
--- a/src/mongo/db/catalog/index_key_validate_test.cpp
+++ b/src/mongo/db/catalog/index_key_validate_test.cpp
@@ -318,5 +318,40 @@ TEST(IndexKeyValidateTest, Background) {
nullptr, fromjson("{key: {a: 1}, name: 'index', background: []}")));
}
+TEST(IndexKeyValidateTest, RemoveUnkownFieldsFromIndexSpecs) {
+ ASSERT(fromjson("{key: {a: 1}, name: 'index'}")
+ .binaryEqual(index_key_validate::removeUnknownFields(
+ NamespaceString("coll"),
+ fromjson("{key: {a: 1}, name: 'index', safe: true, force: true}"))));
+}
+
+TEST(IndexKeyValidateTest, RepairIndexSpecs) {
+ ASSERT(fromjson("{key: {a: 1}, name: 'index'}")
+ .binaryEqual(index_key_validate::repairIndexSpec(
+ NamespaceString("coll"),
+ fromjson("{key: {a: 1}, name: 'index', safe: true, force: true}"))));
+
+ ASSERT(fromjson("{key: {a: 1}, name: 'index', sparse: true}")
+ .binaryEqual(index_key_validate::repairIndexSpec(
+ NamespaceString("coll"),
+ fromjson("{key: {a: 1}, name: 'index', sparse: 'true'}"))));
+
+ ASSERT(fromjson("{key: {a: 1}, name: 'index', background: true}")
+ .binaryEqual(index_key_validate::repairIndexSpec(
+ NamespaceString("coll"),
+ fromjson("{key: {a: 1}, name: 'index', background: '1'}"))));
+
+ ASSERT(fromjson("{key: {a: 1}, name: 'index', sparse: true, background: true}")
+ .binaryEqual(index_key_validate::repairIndexSpec(
+ NamespaceString("coll"),
+ fromjson("{key: {a: 1}, name: 'index', sparse: 'true', background: '1'}"))));
+
+ ASSERT(fromjson("{key: {a: 1}, name: 'index', sparse: true, background: true}")
+ .binaryEqual(index_key_validate::repairIndexSpec(
+ NamespaceString("coll"),
+ fromjson("{key: {a: 1}, name: 'index', sparse: 'true', background: '1', safe: "
+ "true, force: true}"))));
+}
+
} // namespace
} // namespace mongo
diff --git a/src/mongo/dbtests/validate_tests.cpp b/src/mongo/dbtests/validate_tests.cpp
index 031057cb7cf..ceedaccb3ee 100644
--- a/src/mongo/dbtests/validate_tests.cpp
+++ b/src/mongo/dbtests/validate_tests.cpp
@@ -909,6 +909,31 @@ public:
}
};
+class ValidateIndexMetadata : public ValidateBase {
+public:
+ ValidateIndexMetadata() : ValidateBase(/*full=*/false, /*background=*/false) {}
+
+ void run() {
+ SharedBufferFragmentBuilder pooledBuilder(
+ KeyString::HeapBuilder::kHeapAllocatorDefaultBytes);
+
+ // Create an index with bad index specs.
+ lockDb(MODE_X);
+
+ const std::string indexName = "bad_specs_index";
+ auto status =
+ dbtests::createIndexFromSpec(&_opCtx,
+ coll()->ns().ns(),
+ BSON("name" << indexName << "key" << BSON("a" << 1) << "v"
+ << static_cast<int>(kIndexVersion) << "sparse"
+ << "false"));
+
+ ASSERT_OK(status);
+ releaseDb();
+ ensureValidateFailed();
+ }
+};
+
template <bool full, bool background>
class ValidateWildCardIndex : public ValidateBase {
public:
@@ -4486,6 +4511,7 @@ public:
// Tests for index validation.
add<ValidateIndexEntry<false, false>>();
add<ValidateIndexEntry<false, true>>();
+ add<ValidateIndexMetadata>();
// Tests that the 'missingIndexEntries' and 'extraIndexEntries' field are populated
// correctly.