From baecc1ad1893c1502f316cc18d2439fb225d9dcd Mon Sep 17 00:00:00 2001 From: Ian Boros Date: Fri, 27 Jul 2018 16:08:02 -0400 Subject: SERVER-36199 Put allPaths index creation behind a query knob --- src/mongo/db/catalog/index_key_validate_test.cpp | 58 ++++++++++++++---------- 1 file changed, 34 insertions(+), 24 deletions(-) (limited to 'src/mongo/db/catalog/index_key_validate_test.cpp') diff --git a/src/mongo/db/catalog/index_key_validate_test.cpp b/src/mongo/db/catalog/index_key_validate_test.cpp index cc8978d0673..1d170fbbde7 100644 --- a/src/mongo/db/catalog/index_key_validate_test.cpp +++ b/src/mongo/db/catalog/index_key_validate_test.cpp @@ -36,6 +36,7 @@ #include "mongo/bson/bsonmisc.h" #include "mongo/bson/bsonobjbuilder.h" #include "mongo/db/index/index_descriptor.h" +#include "mongo/db/query/query_knobs.h" #include "mongo/unittest/unittest.h" namespace mongo { @@ -45,6 +46,31 @@ namespace { using IndexVersion = IndexDescriptor::IndexVersion; using index_key_validate::validateKeyPattern; +/** + * Helper class to ensure proper FCV & test commands enabled. + * TODO: Remove test command enabling/disabling in SERVER-36198 + */ +class TestCommandQueryKnobGuard { + +public: + TestCommandQueryKnobGuard() { + _prevEnabled = getTestCommandsEnabled(); + setTestCommandsEnabled(true); + + _prevKnobEnabled = internalQueryAllowAllPathsIndexes.load(); + internalQueryAllowAllPathsIndexes.store(true); + } + + ~TestCommandQueryKnobGuard() { + setTestCommandsEnabled(_prevEnabled); + internalQueryAllowAllPathsIndexes.store(_prevKnobEnabled); + } + +private: + bool _prevEnabled; + bool _prevKnobEnabled; +}; + TEST(IndexKeyValidateTest, KeyElementValueOfSmallPositiveIntSucceeds) { for (auto indexVersion : IndexDescriptor::getSupportedIndexVersions()) { ASSERT_OK(validateKeyPattern(BSON("x" << 1), indexVersion)); @@ -237,77 +263,61 @@ TEST(IndexKeyValidateTest, KeyElementNameTextSucceedsOnTextIndex) { } TEST(IndexKeyValidateTest, KeyElementNameAllPathsSucceedsOnSubPath) { - const bool temp = getTestCommandsEnabled(); - setTestCommandsEnabled(true); + TestCommandQueryKnobGuard guard; ASSERT_OK(validateKeyPattern(BSON("a.$**" << 1), IndexVersion::kV2)); - setTestCommandsEnabled(temp); } TEST(IndexKeyValidateTest, KeyElementNameAllPathsSucceeds) { - const bool temp = getTestCommandsEnabled(); - setTestCommandsEnabled(true); + TestCommandQueryKnobGuard guard; ASSERT_OK(validateKeyPattern(BSON("$**" << 1), IndexVersion::kV2)); - setTestCommandsEnabled(temp); } TEST(IndexKeyValidateTest, KeyElementNameAllPathsFailsOnRepeat) { - const bool temp = getTestCommandsEnabled(); - setTestCommandsEnabled(true); + TestCommandQueryKnobGuard guard; auto status = validateKeyPattern(BSON("$**.$**" << 1), IndexVersion::kV2); ASSERT_NOT_OK(status); ASSERT_EQ(status, ErrorCodes::CannotCreateIndex); - setTestCommandsEnabled(temp); } TEST(IndexKeyValidateTest, KeyElementNameAllPathsFailsOnSubPathRepeat) { - const bool temp = getTestCommandsEnabled(); - setTestCommandsEnabled(true); + TestCommandQueryKnobGuard guard; auto status = validateKeyPattern(BSON("a.$**.$**" << 1), IndexVersion::kV2); ASSERT_NOT_OK(status); ASSERT_EQ(status, ErrorCodes::CannotCreateIndex); - setTestCommandsEnabled(temp); } TEST(IndexKeyValidateTest, KeyElementNameAllPathsFailsOnCompound) { - const bool temp = getTestCommandsEnabled(); - setTestCommandsEnabled(true); + TestCommandQueryKnobGuard guard; auto status = validateKeyPattern(BSON("$**" << 1 << "a" << 1), IndexVersion::kV2); ASSERT_NOT_OK(status); ASSERT_EQ(status, ErrorCodes::CannotCreateIndex); - setTestCommandsEnabled(temp); } TEST(IndexKeyValidateTest, KeyElementNameAllPathsFailsOnIncorrectValue) { - const bool temp = getTestCommandsEnabled(); - setTestCommandsEnabled(true); + TestCommandQueryKnobGuard guard; auto status = validateKeyPattern(BSON("$**" << false), IndexVersion::kV2); ASSERT_NOT_OK(status); ASSERT_EQ(status, ErrorCodes::CannotCreateIndex); - setTestCommandsEnabled(temp); } TEST(IndexKeyValidateTest, KeyElementNameAllPathsFailsWhenValueIsPluginNameWithInvalidKeyName) { // TODO: Remove test command enabling/disabling in SERVER-36198 - const bool temp = getTestCommandsEnabled(); - setTestCommandsEnabled(true); + TestCommandQueryKnobGuard guard; auto status = validateKeyPattern(BSON("a" << "allPaths"), IndexVersion::kV2); ASSERT_NOT_OK(status); ASSERT_EQ(status, ErrorCodes::CannotCreateIndex); - setTestCommandsEnabled(temp); } TEST(IndexKeyValidateTest, KeyElementNameAllPathsFailsWhenValueIsPluginNameWithValidKeyName) { // TODO: Remove test command enabling/disabling in SERVER-36198 - const bool temp = getTestCommandsEnabled(); - setTestCommandsEnabled(true); + TestCommandQueryKnobGuard guard; auto status = validateKeyPattern(BSON("$**" << "allPaths"), IndexVersion::kV2); ASSERT_NOT_OK(status); ASSERT_EQ(status, ErrorCodes::CannotCreateIndex); - setTestCommandsEnabled(temp); } } // namespace -- cgit v1.2.1