diff options
author | Billy Donahue <billy.donahue@mongodb.com> | 2020-04-14 18:03:21 -0400 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2020-05-01 02:49:37 +0000 |
commit | 5d32cf67738d377070c5ab0e44a267d4b3b3b7bb (patch) | |
tree | 1476452753fbfc24f9564ce9108cb9ef0098a496 /src/mongo | |
parent | 6a5e183833f49e3d5ec7ed54da4bf5471b63ec8d (diff) | |
download | mongo-5d32cf67738d377070c5ab0e44a267d4b3b3b7bb.tar.gz |
SERVER-47418 deploy unittest::MinimumLoggedSeverityGuard to all tests
(cherry picked from commit e4794531419bff51da233bc4453db6f99d1809ff)
Diffstat (limited to 'src/mongo')
21 files changed, 156 insertions, 165 deletions
diff --git a/src/mongo/client/scanning_replica_set_monitor_test_concurrent.cpp b/src/mongo/client/scanning_replica_set_monitor_test_concurrent.cpp index 77ae952034d..5b2cb6ae73b 100644 --- a/src/mongo/client/scanning_replica_set_monitor_test_concurrent.cpp +++ b/src/mongo/client/scanning_replica_set_monitor_test_concurrent.cpp @@ -62,8 +62,6 @@ protected: ThreadPoolExecutorTest::setUp(); launchExecutorThread(); _startTime = getNet()->now(); - - { setMinimumLoggedSeverity(logv2::LogComponent::kNetwork, logv2::LogSeverity::Debug(2)); } setGlobalServiceContext(std::move(serviceContext)); } @@ -154,6 +152,9 @@ protected: } private: + unittest::MinimumLoggedSeverityGuard _networkSeverityGuard{logv2::LogComponent::kNetwork, + logv2::LogSeverity::Debug(2)}; + std::map<HostAndPort, int> _numChecks; Date_t _startTime; diff --git a/src/mongo/db/repl/apply_ops_test.cpp b/src/mongo/db/repl/apply_ops_test.cpp index 64b675c22e4..3d5026f54d4 100644 --- a/src/mongo/db/repl/apply_ops_test.cpp +++ b/src/mongo/db/repl/apply_ops_test.cpp @@ -82,6 +82,9 @@ private: void tearDown() override; protected: + // Reset default log level when each test is over in case it was changed. + unittest::MinimumLoggedSeverityGuard _verbosityGuard{logv2::LogComponent::kReplication}; + OpObserverMock* _opObserver = nullptr; std::unique_ptr<StorageInterface> _storage; }; @@ -116,9 +119,6 @@ void ApplyOpsTest::tearDown() { _storage = {}; _opObserver = nullptr; - // Reset default log level in case it was changed. - setMinimumLoggedSeverity(logv2::LogComponent::kReplication, logv2::LogSeverity::Debug(0)); - ServiceContextMongoDTest::tearDown(); } @@ -291,7 +291,8 @@ TEST_F(ApplyOpsTest, ApplyOpsPropagatesOplogApplicationMode) { auto opCtx = cc().makeOperationContext(); // Increase log component verbosity to check for op application messages. - setMinimumLoggedSeverity(logv2::LogComponent::kReplication, logv2::LogSeverity::Debug(3)); + auto verbosityGuard = unittest::MinimumLoggedSeverityGuard{logv2::LogComponent::kReplication, + logv2::LogSeverity::Debug(3)}; // Test that the 'applyOps' function passes the oplog application mode through correctly to the // underlying op application functions. diff --git a/src/mongo/db/repl/cloner_test_fixture.cpp b/src/mongo/db/repl/cloner_test_fixture.cpp index de432238e4c..4ed5bb7d31c 100644 --- a/src/mongo/db/repl/cloner_test_fixture.cpp +++ b/src/mongo/db/repl/cloner_test_fixture.cpp @@ -57,8 +57,6 @@ BSONObj ClonerTestFixture::createCursorResponse(const std::string& nss, const BS void ClonerTestFixture::setUp() { unittest::Test::setUp(); - logger::globalLogDomain()->setMinimumLoggedSeverity( - logger::LogComponent::kReplicationInitialSync, logger::LogSeverity::Debug(1)); Client::initThread("ClonerTest"); ThreadPool::Options options; options.minThreads = 1U; @@ -89,8 +87,6 @@ void ClonerTestFixture::setUp() { void ClonerTestFixture::tearDown() { _dbWorkThreadPool.reset(); Client::releaseCurrent(); - logger::globalLogDomain()->setMinimumLoggedSeverity( - logger::LogComponent::kReplicationInitialSync, logger::LogSeverity::Debug(0)); unittest::Test::tearDown(); } diff --git a/src/mongo/db/repl/cloner_test_fixture.h b/src/mongo/db/repl/cloner_test_fixture.h index 6dddb4162da..8cbf4812fca 100644 --- a/src/mongo/db/repl/cloner_test_fixture.h +++ b/src/mongo/db/repl/cloner_test_fixture.h @@ -34,6 +34,9 @@ #include "mongo/db/repl/storage_interface_mock.h" #include "mongo/db/service_context_test_fixture.h" #include "mongo/dbtests/mock/mock_dbclient_connection.h" +#include "mongo/logv2/log_component.h" +#include "mongo/logv2/log_severity.h" +#include "mongo/unittest/log_test.h" #include "mongo/unittest/unittest.h" #include "mongo/util/clock_source_mock.h" #include "mongo/util/concurrency/thread_pool.h" @@ -69,6 +72,9 @@ protected: private: static constexpr int kInitialRollbackId = 1; + + unittest::MinimumLoggedSeverityGuard _verboseGuard{logv2::LogComponent::kReplicationInitialSync, + logv2::LogSeverity::Debug(1)}; }; } // namespace repl diff --git a/src/mongo/db/repl/replication_coordinator_impl_elect_v1_test.cpp b/src/mongo/db/repl/replication_coordinator_impl_elect_v1_test.cpp index 2a74373adc4..c3f64ef471b 100644 --- a/src/mongo/db/repl/replication_coordinator_impl_elect_v1_test.cpp +++ b/src/mongo/db/repl/replication_coordinator_impl_elect_v1_test.cpp @@ -581,7 +581,8 @@ TEST_F(ReplCoordTest, NodeWillNotStandForElectionDuringHeartbeatReconfig) { ASSERT_EQUALS(ErrorCodes::ConfigurationInProgress, getReplCoord()->processReplSetReconfig(&opCtx, args, &result)); - setMinimumLoggedSeverity(logv2::LogSeverity::Debug(2)); + auto severityGuard = unittest::MinimumLoggedSeverityGuard{logv2::LogComponent::kDefault, + logv2::LogSeverity::Debug(2)}; startCapturingLogMessages(); // receive sufficient heartbeats to allow the node to see a majority. @@ -1191,7 +1192,8 @@ TEST_F(TakeoverTest, PrefersPriorityToCatchupTakeoverIfNodeHasHighestPriority) { << "node3:12345")) << "protocolVersion" << 1); - setMinimumLoggedSeverity(logv2::LogSeverity::Debug(2)); + auto severityGuard = unittest::MinimumLoggedSeverityGuard{logv2::LogComponent::kDefault, + logv2::LogSeverity::Debug(2)}; startCapturingLogMessages(); assertStartSuccess(configObj, HostAndPort("node1", 12345)); diff --git a/src/mongo/db/repl/replication_coordinator_impl_heartbeat_v1_test.cpp b/src/mongo/db/repl/replication_coordinator_impl_heartbeat_v1_test.cpp index ea55725ddc8..f74f211a5f0 100644 --- a/src/mongo/db/repl/replication_coordinator_impl_heartbeat_v1_test.cpp +++ b/src/mongo/db/repl/replication_coordinator_impl_heartbeat_v1_test.cpp @@ -87,7 +87,8 @@ ReplSetHeartbeatResponse ReplCoordHBV1Test::receiveHeartbeatFrom(const ReplSetCo TEST_F(ReplCoordHBV1Test, NodeJoinsExistingReplSetWhenReceivingAConfigContainingTheNodeViaHeartbeat) { - setMinimumLoggedSeverity(logv2::LogSeverity::Debug(3)); + auto severityGuard = unittest::MinimumLoggedSeverityGuard{logv2::LogComponent::kDefault, + logv2::LogSeverity::Debug(3)}; ReplSetConfig rsConfig = assertMakeRSConfig(BSON("_id" << "mySet" << "version" << 3 << "members" @@ -157,7 +158,6 @@ TEST_F(ReplCoordHBV1Test, class ReplCoordHBV1ReconfigTest : public ReplCoordHBV1Test { public: void setUp() { - setMinimumLoggedSeverity(logv2::LogSeverity::Debug(3)); BSONObj configBson = BSON("_id" << "mySet" << "version" << initConfigVersion << "term" << initConfigTerm @@ -190,6 +190,9 @@ public: return assertMakeRSConfig(makeConfigObj(version, term)); } + unittest::MinimumLoggedSeverityGuard severityGuard{logv2::LogComponent::kDefault, + logv2::LogSeverity::Debug(3)}; + int initConfigVersion = 2; int initConfigTerm = 2; long long UninitializedTerm = OpTime::kUninitializedTerm; @@ -498,7 +501,8 @@ TEST_F(ReplCoordHBV1Test, AwaitIsMasterReturnsResponseOnReconfigViaHeartbeat) { ASSERT_EQUALS("node3", hosts[2].host()); }); - setMinimumLoggedSeverity(logv2::LogSeverity::Debug(3)); + auto severityGuard = unittest::MinimumLoggedSeverityGuard{logv2::LogComponent::kDefault, + logv2::LogSeverity::Debug(3)}; ReplSetConfig rsConfig = assertMakeRSConfig(BSON("_id" << "mySet" @@ -549,7 +553,8 @@ TEST_F(ReplCoordHBV1Test, AwaitIsMasterReturnsResponseOnReconfigViaHeartbeat) { TEST_F(ReplCoordHBV1Test, ArbiterJoinsExistingReplSetWhenReceivingAConfigContainingTheArbiterViaHeartbeat) { - setMinimumLoggedSeverity(logv2::LogSeverity::Debug(3)); + auto severityGuard = unittest::MinimumLoggedSeverityGuard{logv2::LogComponent::kDefault, + logv2::LogSeverity::Debug(3)}; ReplSetConfig rsConfig = assertMakeRSConfig(BSON("_id" << "mySet" @@ -622,7 +627,8 @@ TEST_F(ReplCoordHBV1Test, NodeDoesNotJoinExistingReplSetWhenReceivingAConfigNotContainingTheNodeViaHeartbeat) { // Tests that a node in RS_STARTUP will not transition to RS_REMOVED if it receives a // configuration that does not contain it. - setMinimumLoggedSeverity(logv2::LogSeverity::Debug(3)); + auto severityGuard = unittest::MinimumLoggedSeverityGuard{logv2::LogComponent::kDefault, + logv2::LogSeverity::Debug(3)}; ReplSetConfig rsConfig = assertMakeRSConfig(BSON("_id" << "mySet" << "version" << 3 << "members" @@ -704,7 +710,8 @@ TEST_F(ReplCoordHBV1Test, TEST_F(ReplCoordHBV1Test, NodeChangesToRecoveringStateWhenAllNodesRespondToHeartbeatsWithUnauthorized) { // Tests that a node that only has auth error heartbeats is recovering - setMinimumLoggedSeverity(logv2::LogSeverity::Debug(3)); + auto severityGuard = unittest::MinimumLoggedSeverityGuard{logv2::LogComponent::kDefault, + logv2::LogSeverity::Debug(3)}; assertStartSuccess(BSON("_id" << "mySet" << "version" << 1 << "members" diff --git a/src/mongo/db/repl/replication_coordinator_impl_reconfig_test.cpp b/src/mongo/db/repl/replication_coordinator_impl_reconfig_test.cpp index d4a6fbb276c..0e7fee0157f 100644 --- a/src/mongo/db/repl/replication_coordinator_impl_reconfig_test.cpp +++ b/src/mongo/db/repl/replication_coordinator_impl_reconfig_test.cpp @@ -717,7 +717,8 @@ TEST_F(ReplCoordTest, NodeDoesNotAcceptHeartbeatReconfigWhileInTheMidstOfReconfi hbResp.addToBSON(&respObj2); net->scheduleResponse(noi, net->now(), makeResponseStatus(respObj2.obj())); - setMinimumLoggedSeverity(logv2::LogSeverity::Debug(1)); + auto severityGuard = unittest::MinimumLoggedSeverityGuard{logv2::LogComponent::kDefault, + logv2::LogSeverity::Debug(1)}; startCapturingLogMessages(); // execute hb reconfig, which should fail with a log message; confirmed at end of test net->runReadyNetworkOperations(); @@ -730,7 +731,6 @@ TEST_F(ReplCoordTest, NodeDoesNotAcceptHeartbeatReconfigWhileInTheMidstOfReconfi "the midst of a configuration process")); shutdown(opCtx.get()); reconfigThread.join(); - setMinimumLoggedSeverity(logv2::LogSeverity::Log()); } TEST_F(ReplCoordTest, NodeAcceptsConfigFromAReconfigWithForceTrueWhileNotPrimary) { @@ -774,9 +774,10 @@ TEST_F(ReplCoordTest, NodeAcceptsConfigFromAReconfigWithForceTrueWhileNotPrimary class ReplCoordReconfigTest : public ReplCoordTest { public: - void setUp() { - setMinimumLoggedSeverity(logv2::LogSeverity::Debug(3)); - } + int counter = 0; + std::vector<HostAndPort> initialSyncNodes; + unittest::MinimumLoggedSeverityGuard severityGuard{logv2::LogComponent::kDefault, + logv2::LogSeverity::Debug(3)}; BSONObj member(int id, std::string host) { return BSON("_id" << id << "host" << host); diff --git a/src/mongo/db/repl/replication_coordinator_impl_test.cpp b/src/mongo/db/repl/replication_coordinator_impl_test.cpp index 57fcb8e5d7f..ab6b07c7cdd 100644 --- a/src/mongo/db/repl/replication_coordinator_impl_test.cpp +++ b/src/mongo/db/repl/replication_coordinator_impl_test.cpp @@ -6619,8 +6619,8 @@ TEST_F(ReplCoordTest, TEST_F(ReplCoordTest, CancelAndRescheduleElectionTimeoutLogging) { // Log all the election messages. - setMinimumLoggedSeverity(logv2::LogComponent::kReplicationElection, - logv2::LogSeverity::Debug(5)); + auto replElectionAllSeverityGuard = unittest::MinimumLoggedSeverityGuard{ + logv2::LogComponent::kReplicationElection, logv2::LogSeverity::Debug(5)}; startCapturingLogMessages(); // heartbeatTimeoutSecs is made large so we can advance the clock without worrying about // additional heartbeats. @@ -6679,8 +6679,8 @@ TEST_F(ReplCoordTest, CancelAndRescheduleElectionTimeoutLogging) { ASSERT_EQ(1, countTextFormatLogLinesContaining("Rescheduling election timeout callback")); ASSERT_EQ(1, countTextFormatLogLinesContaining("Canceling election timeout callback")); - setMinimumLoggedSeverity(logv2::LogComponent::kReplicationElection, - logv2::LogSeverity::Debug(4)); + auto replElectionReducedSeverityGuard = unittest::MinimumLoggedSeverityGuard{ + logv2::LogComponent::kReplicationElection, logv2::LogSeverity::Debug(4)}; net->enterNetwork(); until = electionTimeoutWhen + Milliseconds(500); net->runUntil(until); diff --git a/src/mongo/db/repl/replication_coordinator_test_fixture.cpp b/src/mongo/db/repl/replication_coordinator_test_fixture.cpp index d13f8d80eb0..7d882e454cc 100644 --- a/src/mongo/db/repl/replication_coordinator_test_fixture.cpp +++ b/src/mongo/db/repl/replication_coordinator_test_fixture.cpp @@ -87,7 +87,6 @@ ReplCoordTest::ReplCoordTest() { } ReplCoordTest::~ReplCoordTest() { - clearMinimumLoggedSeverity(logv2::LogComponent::kReplicationElection); globalFailPointRegistry().find("blockHeartbeatReconfigFinish")->setMode(FailPoint::off); if (_callShutdown) { diff --git a/src/mongo/db/repl/rollback_test_fixture.cpp b/src/mongo/db/repl/rollback_test_fixture.cpp index 4583e8232b6..862ba41e289 100644 --- a/src/mongo/db/repl/rollback_test_fixture.cpp +++ b/src/mongo/db/repl/rollback_test_fixture.cpp @@ -110,10 +110,6 @@ void RollbackTest::setUp() { _replicationProcess->getConsistencyMarkers()->setMinValid(_opCtx.get(), OpTime{}); _replicationProcess->initializeRollbackID(_opCtx.get()).transitional_ignore(); - // Increase rollback log component verbosity for unit tests. - setMinimumLoggedSeverity(logv2::LogComponent::kReplicationRollback, - logv2::LogSeverity::Debug(2)); - auto observerRegistry = checked_cast<OpObserverRegistry*>(serviceContext->getOpObserver()); observerRegistry->addObserver(std::make_unique<RollbackTestOpObserver>()); diff --git a/src/mongo/db/repl/rollback_test_fixture.h b/src/mongo/db/repl/rollback_test_fixture.h index 79e27adc557..2ee64652aef 100644 --- a/src/mongo/db/repl/rollback_test_fixture.h +++ b/src/mongo/db/repl/rollback_test_fixture.h @@ -40,6 +40,10 @@ #include "mongo/db/repl/storage_interface_impl.h" #include "mongo/db/service_context.h" #include "mongo/db/service_context_d_test_fixture.h" +#include "mongo/logv2/log_component.h" +#include "mongo/logv2/log_severity.h" +#include "mongo/unittest/log_test.h" +#include "mongo/unittest/unittest.h" namespace mongo { namespace repl { @@ -133,6 +137,10 @@ protected: DropPendingCollectionReaper* _dropPendingCollectionReaper = nullptr; ReadWriteConcernDefaultsLookupMock _lookupMock; + + // Increase rollback log component verbosity for unit tests. + unittest::MinimumLoggedSeverityGuard severityGuard{logv2::LogComponent::kReplicationRollback, + logv2::LogSeverity::Debug(2)}; }; class RollbackTest::StorageInterfaceRollback : public StorageInterfaceImpl { diff --git a/src/mongo/db/repl/topology_coordinator_v1_test.cpp b/src/mongo/db/repl/topology_coordinator_v1_test.cpp index eb0f8d9f232..d59db60f743 100644 --- a/src/mongo/db/repl/topology_coordinator_v1_test.cpp +++ b/src/mongo/db/repl/topology_coordinator_v1_test.cpp @@ -7175,16 +7175,9 @@ TEST_F(HeartbeatResponseTestTwoRetriesV1, HeartbeatThreeNonconsecutiveFailures) class HeartbeatResponseHighVerbosityTestV1 : public HeartbeatResponseTestV1 { public: - virtual void setUp() { - HeartbeatResponseTestV1::setUp(); - // set verbosity as high as the highest verbosity log message we'd like to check for - setMinimumLoggedSeverity(logv2::LogSeverity::Debug(3)); - } - - virtual void tearDown() { - HeartbeatResponseTestV1::tearDown(); - setMinimumLoggedSeverity(logv2::LogSeverity::Log()); - } + // set verbosity as high as the highest verbosity log message we'd like to check for + unittest::MinimumLoggedSeverityGuard severityGuard{logv2::LogComponent::kDefault, + logv2::LogSeverity::Debug(3)}; }; // TODO(dannenberg) figure out why this test is useful diff --git a/src/mongo/db/s/transaction_coordinator_test.cpp b/src/mongo/db/s/transaction_coordinator_test.cpp index 33422d1f831..6133f6ec5ee 100644 --- a/src/mongo/db/s/transaction_coordinator_test.cpp +++ b/src/mongo/db/s/transaction_coordinator_test.cpp @@ -2112,13 +2112,15 @@ TEST_F(TransactionCoordinatorMetricsTest, CoordinatorsAWSIsShutDownWhileCoordina } TEST_F(TransactionCoordinatorMetricsTest, LogsTransactionAtLogLevelOne) { - setMinimumLoggedSeverity(logv2::LogComponent::kTransaction, logv2::LogSeverity::Debug(1)); + auto severityGuard = unittest::MinimumLoggedSeverityGuard{logv2::LogComponent::kTransaction, + logv2::LogSeverity::Debug(1)}; runSimpleTwoPhaseCommitWithCommitDecisionAndCaptureLogLines(); ASSERT_EQUALS(1, countTextFormatLogLinesContaining("two-phase commit")); } TEST_F(TransactionCoordinatorMetricsTest, DoesNotLogTransactionAtLogLevelZero) { - setMinimumLoggedSeverity(logv2::LogComponent::kTransaction, logv2::LogSeverity::Log()); + auto severityGuard = unittest::MinimumLoggedSeverityGuard{logv2::LogComponent::kTransaction, + logv2::LogSeverity::Log()}; runSimpleTwoPhaseCommitWithCommitDecisionAndCaptureLogLines(); ASSERT_EQUALS(0, countTextFormatLogLinesContaining("two-phase commit")); } @@ -2126,7 +2128,8 @@ TEST_F(TransactionCoordinatorMetricsTest, DoesNotLogTransactionAtLogLevelZero) { TEST_F(TransactionCoordinatorMetricsTest, DoesNotLogTransactionsUnderSlowMSThreshold) { // Set the log level to 0 so that the slow logging is only done if the transaction exceeds the // slowMS setting. - setMinimumLoggedSeverity(logv2::LogComponent::kTransaction, logv2::LogSeverity::Log()); + auto severityGuard = unittest::MinimumLoggedSeverityGuard{logv2::LogComponent::kTransaction, + logv2::LogSeverity::Log()}; serverGlobalParams.slowMS = 100; startCapturingLogMessages(); @@ -2157,7 +2160,8 @@ TEST_F( DoesNotLogTransactionsUnderSlowMSThresholdEvenIfCoordinatorHasExistedForLongerThanSlowThreshold) { // Set the log level to 0 so that the slow logging is only done if the transaction exceeds the // slowMS setting. - setMinimumLoggedSeverity(logv2::LogComponent::kTransaction, logv2::LogSeverity::Log()); + auto severityGuard = unittest::MinimumLoggedSeverityGuard{logv2::LogComponent::kTransaction, + logv2::LogSeverity::Log()}; serverGlobalParams.slowMS = 100; startCapturingLogMessages(); @@ -2186,7 +2190,8 @@ TEST_F( TEST_F(TransactionCoordinatorMetricsTest, LogsTransactionsOverSlowMSThreshold) { // Set the log level to 0 so that the slow logging is only done if the transaction exceeds the // slowMS setting. - setMinimumLoggedSeverity(logv2::LogComponent::kTransaction, logv2::LogSeverity::Log()); + auto severityGuard = unittest::MinimumLoggedSeverityGuard{logv2::LogComponent::kTransaction, + logv2::LogSeverity::Log()}; serverGlobalParams.slowMS = 100; startCapturingLogMessages(); diff --git a/src/mongo/db/server_options_test.cpp b/src/mongo/db/server_options_test.cpp index 415a9b25a93..81d1b975f35 100644 --- a/src/mongo/db/server_options_test.cpp +++ b/src/mongo/db/server_options_test.cpp @@ -67,6 +67,10 @@ namespace { using mongo::ErrorCodes; using mongo::Status; + +using mongo::unittest::getMinimumLogSeverity; +using mongo::unittest::hasMinimumLogSeverity; + namespace moe = mongo::optionenvironment; MONGO_INITIALIZER(ServerLogRedirection)(mongo::InitializerContext*) { @@ -97,14 +101,17 @@ private: std::string _config; }; -TEST(Verbosity, Default) { +class Verbosity : public mongo::unittest::Test { + /** Reset the log level before we test */ + mongo::unittest::MinimumLoggedSeverityGuard _severityGuard{mongo::logv2::LogComponent::kDefault, + mongo::logv2::LogSeverity::Info()}; +}; + +TEST_F(Verbosity, Default) { OptionsParserTester parser; moe::Environment environment; moe::OptionSection options; - // Reset the log level before we test - mongo::setMinimumLoggedSeverity(::mongo::logv2::LogSeverity::Info()); - ASSERT_OK(::mongo::addGeneralServerOptions(&options)); std::vector<std::string> argv; @@ -119,17 +126,14 @@ TEST(Verbosity, Default) { ASSERT_OK(::mongo::storeServerOptions(environment)); // Make sure the log level didn't change since we didn't specify any verbose options - ASSERT_EQUALS(mongo::getMinimumLogSeverity(), ::mongo::logv2::LogSeverity::Info()); + ASSERT_EQUALS(getMinimumLogSeverity(), ::mongo::logv2::LogSeverity::Info()); } -TEST(Verbosity, CommandLineImplicit) { +TEST_F(Verbosity, CommandLineImplicit) { OptionsParserTester parser; moe::Environment environment; moe::OptionSection options; - // Reset the log level before we test - mongo::setMinimumLoggedSeverity(::mongo::logv2::LogSeverity::Info()); - ASSERT_OK(::mongo::addGeneralServerOptions(&options)); std::vector<std::string> argv; @@ -145,17 +149,14 @@ TEST(Verbosity, CommandLineImplicit) { ASSERT_OK(::mongo::storeServerOptions(environment)); int verbosity = 1; - ASSERT_EQUALS(mongo::getMinimumLogSeverity(), ::mongo::logv2::LogSeverity::Debug(verbosity)); + ASSERT_EQUALS(getMinimumLogSeverity(), ::mongo::logv2::LogSeverity::Debug(verbosity)); } -TEST(Verbosity, CommandLineString) { +TEST_F(Verbosity, CommandLineString) { OptionsParserTester parser; moe::Environment environment; moe::OptionSection options; - // Reset the log level before we test - mongo::setMinimumLoggedSeverity(::mongo::logv2::LogSeverity::Info()); - ASSERT_OK(::mongo::addGeneralServerOptions(&options)); std::vector<std::string> argv; @@ -172,17 +173,14 @@ TEST(Verbosity, CommandLineString) { ASSERT_OK(::mongo::storeServerOptions(environment)); int verbosity = 4; - ASSERT_EQUALS(mongo::getMinimumLogSeverity(), ::mongo::logv2::LogSeverity::Debug(verbosity)); + ASSERT_EQUALS(getMinimumLogSeverity(), ::mongo::logv2::LogSeverity::Debug(verbosity)); } -TEST(Verbosity, CommandLineStringDisguisedLongForm) { +TEST_F(Verbosity, CommandLineStringDisguisedLongForm) { OptionsParserTester parser; moe::Environment environment; moe::OptionSection options; - // Reset the log level before we test - mongo::setMinimumLoggedSeverity(::mongo::logv2::LogSeverity::Info()); - ASSERT_OK(::mongo::addGeneralServerOptions(&options)); std::vector<std::string> argv; @@ -199,17 +197,14 @@ TEST(Verbosity, CommandLineStringDisguisedLongForm) { ASSERT_OK(::mongo::storeServerOptions(environment)); int verbosity = 4; - ASSERT_EQUALS(mongo::getMinimumLogSeverity(), ::mongo::logv2::LogSeverity::Debug(verbosity)); + ASSERT_EQUALS(getMinimumLogSeverity(), ::mongo::logv2::LogSeverity::Debug(verbosity)); } -TEST(Verbosity, CommandLineEmptyString) { +TEST_F(Verbosity, CommandLineEmptyString) { OptionsParserTester parser; moe::Environment environment; moe::OptionSection options; - // Reset the log level before we test - mongo::setMinimumLoggedSeverity(::mongo::logv2::LogSeverity::Info()); - ASSERT_OK(::mongo::addGeneralServerOptions(&options)); std::vector<std::string> argv; @@ -221,14 +216,11 @@ TEST(Verbosity, CommandLineEmptyString) { ASSERT_NOT_OK(parser.run(options, argv, env_map, &environment)); } -TEST(Verbosity, CommandLineBadString) { +TEST_F(Verbosity, CommandLineBadString) { OptionsParserTester parser; moe::Environment environment; moe::OptionSection options; - // Reset the log level before we test - mongo::setMinimumLoggedSeverity(::mongo::logv2::LogSeverity::Info()); - ASSERT_OK(::mongo::addGeneralServerOptions(&options)); std::vector<std::string> argv; @@ -242,14 +234,11 @@ TEST(Verbosity, CommandLineBadString) { ASSERT_NOT_OK(::mongo::validateServerOptions(environment)); } -TEST(Verbosity, CommandLineBadStringOnlyDash) { +TEST_F(Verbosity, CommandLineBadStringOnlyDash) { OptionsParserTester parser; moe::Environment environment; moe::OptionSection options; - // Reset the log level before we test - mongo::setMinimumLoggedSeverity(::mongo::logv2::LogSeverity::Info()); - ASSERT_OK(::mongo::addGeneralServerOptions(&options)); std::vector<std::string> argv; @@ -260,14 +249,11 @@ TEST(Verbosity, CommandLineBadStringOnlyDash) { ASSERT_NOT_OK(parser.run(options, argv, env_map, &environment)); } -TEST(Verbosity, CommandLineBadStringOnlyTwoDashes) { +TEST_F(Verbosity, CommandLineBadStringOnlyTwoDashes) { OptionsParserTester parser; moe::Environment environment; moe::OptionSection options; - // Reset the log level before we test - mongo::setMinimumLoggedSeverity(::mongo::logv2::LogSeverity::Info()); - ASSERT_OK(::mongo::addGeneralServerOptions(&options)); std::vector<std::string> argv; @@ -278,14 +264,11 @@ TEST(Verbosity, CommandLineBadStringOnlyTwoDashes) { ASSERT_OK(parser.run(options, argv, env_map, &environment)); } -TEST(Verbosity, INIConfigString) { +TEST_F(Verbosity, INIConfigString) { OptionsParserTester parser; moe::Environment environment; moe::OptionSection options; - // Reset the log level before we test - mongo::setMinimumLoggedSeverity(::mongo::logv2::LogSeverity::Info()); - ASSERT_OK(::mongo::addGeneralServerOptions(&options)); std::vector<std::string> argv; @@ -304,17 +287,14 @@ TEST(Verbosity, INIConfigString) { ASSERT_OK(::mongo::storeServerOptions(environment)); int verbosity = 4; - ASSERT_EQUALS(mongo::getMinimumLogSeverity(), ::mongo::logv2::LogSeverity::Debug(verbosity)); + ASSERT_EQUALS(getMinimumLogSeverity(), ::mongo::logv2::LogSeverity::Debug(verbosity)); } -TEST(Verbosity, INIConfigBadString) { +TEST_F(Verbosity, INIConfigBadString) { OptionsParserTester parser; moe::Environment environment; moe::OptionSection options; - // Reset the log level before we test - mongo::setMinimumLoggedSeverity(::mongo::logv2::LogSeverity::Info()); - ASSERT_OK(::mongo::addGeneralServerOptions(&options)); std::vector<std::string> argv; @@ -330,14 +310,11 @@ TEST(Verbosity, INIConfigBadString) { ASSERT_NOT_OK(::mongo::validateServerOptions(environment)); } -TEST(Verbosity, INIConfigEmptyString) { +TEST_F(Verbosity, INIConfigEmptyString) { OptionsParserTester parser; moe::Environment environment; moe::OptionSection options; - // Reset the log level before we test - mongo::setMinimumLoggedSeverity(::mongo::logv2::LogSeverity::Info()); - ASSERT_OK(::mongo::addGeneralServerOptions(&options)); std::vector<std::string> argv; @@ -356,17 +333,14 @@ TEST(Verbosity, INIConfigEmptyString) { ASSERT_OK(::mongo::storeServerOptions(environment)); int verbosity = 0; - ASSERT_EQUALS(mongo::getMinimumLogSeverity(), ::mongo::logv2::LogSeverity::Debug(verbosity)); + ASSERT_EQUALS(getMinimumLogSeverity(), ::mongo::logv2::LogSeverity::Debug(verbosity)); } -TEST(Verbosity, JSONConfigString) { +TEST_F(Verbosity, JSONConfigString) { OptionsParserTester parser; moe::Environment environment; moe::OptionSection options; - // Reset the log level before we test - mongo::setMinimumLoggedSeverity(::mongo::logv2::LogSeverity::Info()); - ASSERT_OK(::mongo::addGeneralServerOptions(&options)); std::vector<std::string> argv; @@ -385,17 +359,14 @@ TEST(Verbosity, JSONConfigString) { ASSERT_OK(::mongo::storeServerOptions(environment)); int verbosity = 4; - ASSERT_EQUALS(mongo::getMinimumLogSeverity(), ::mongo::logv2::LogSeverity::Debug(verbosity)); + ASSERT_EQUALS(getMinimumLogSeverity(), ::mongo::logv2::LogSeverity::Debug(verbosity)); } -TEST(Verbosity, MultipleSourcesMultipleOptions) { +TEST_F(Verbosity, MultipleSourcesMultipleOptions) { OptionsParserTester parser; moe::Environment environment; moe::OptionSection options; - // Reset the log level before we test - mongo::setMinimumLoggedSeverity(::mongo::logv2::LogSeverity::Info()); - ASSERT_OK(::mongo::addGeneralServerOptions(&options)); std::vector<std::string> argv; @@ -416,19 +387,17 @@ TEST(Verbosity, MultipleSourcesMultipleOptions) { ASSERT_OK(::mongo::storeServerOptions(environment)); int verbosity = 3; - ASSERT_EQUALS(mongo::getMinimumLogSeverity(), ::mongo::logv2::LogSeverity::Debug(verbosity)); + ASSERT_EQUALS(getMinimumLogSeverity(), ::mongo::logv2::LogSeverity::Debug(verbosity)); } -TEST(Verbosity, YAMLConfigStringLogComponent) { +TEST_F(Verbosity, YAMLConfigStringLogComponent) { OptionsParserTester parser; moe::Environment environment; moe::OptionSection options; - // Reset the log level before we test - mongo::setMinimumLoggedSeverity(::mongo::logv2::LogSeverity::Info()); // Log level for Storage will be cleared by config file value. - mongo::setMinimumLoggedSeverity(::mongo::logv2::LogComponent::kStorage, - ::mongo::logv2::LogSeverity::Debug(1)); + auto storageSeverityGuard = mongo::unittest::MinimumLoggedSeverityGuard{ + mongo::logv2::LogComponent::kStorage, mongo::logv2::LogSeverity::Debug(1)}; ASSERT_OK(::mongo::addGeneralServerOptions(&options)); @@ -460,28 +429,28 @@ TEST(Verbosity, YAMLConfigStringLogComponent) { int verbosity = 4; // Default - ASSERT_EQUALS(mongo::getMinimumLogSeverity(), ::mongo::logv2::LogSeverity::Debug(verbosity)); - ASSERT_EQUALS(mongo::getMinimumLogSeverity(::mongo::logv2::LogComponent::kDefault), + ASSERT_EQUALS(getMinimumLogSeverity(), ::mongo::logv2::LogSeverity::Debug(verbosity)); + ASSERT_EQUALS(getMinimumLogSeverity(::mongo::logv2::LogComponent::kDefault), ::mongo::logv2::LogSeverity::Debug(verbosity)); // AccessControl - ASSERT_TRUE(mongo::hasMinimumLogSeverity(::mongo::logv2::LogComponent::kAccessControl)); - ASSERT_EQUALS(mongo::getMinimumLogSeverity(::mongo::logv2::LogComponent::kAccessControl), + ASSERT_TRUE(hasMinimumLogSeverity(::mongo::logv2::LogComponent::kAccessControl)); + ASSERT_EQUALS(getMinimumLogSeverity(::mongo::logv2::LogComponent::kAccessControl), ::mongo::logv2::LogSeverity::Log()); // Query - not mentioned in configuration. should match default. - ASSERT_FALSE(mongo::hasMinimumLogSeverity(::mongo::logv2::LogComponent::kStorage)); - ASSERT_EQUALS(mongo::getMinimumLogSeverity(::mongo::logv2::LogComponent::kStorage), + ASSERT_FALSE(hasMinimumLogSeverity(::mongo::logv2::LogComponent::kStorage)); + ASSERT_EQUALS(getMinimumLogSeverity(::mongo::logv2::LogComponent::kStorage), ::mongo::logv2::LogSeverity::Debug(verbosity)); // Storage - cleared by -1 value in configuration. should match default. - ASSERT_FALSE(mongo::hasMinimumLogSeverity(::mongo::logv2::LogComponent::kStorage)); - ASSERT_EQUALS(mongo::getMinimumLogSeverity(::mongo::logv2::LogComponent::kStorage), + ASSERT_FALSE(hasMinimumLogSeverity(::mongo::logv2::LogComponent::kStorage)); + ASSERT_EQUALS(getMinimumLogSeverity(::mongo::logv2::LogComponent::kStorage), ::mongo::logv2::LogSeverity::Debug(verbosity)); // Journaling - explicitly set to 2 in configuration. - ASSERT_TRUE(mongo::hasMinimumLogSeverity(::mongo::logv2::LogComponent::kJournal)); - ASSERT_EQUALS(mongo::getMinimumLogSeverity(::mongo::logv2::LogComponent::kJournal), + ASSERT_TRUE(hasMinimumLogSeverity(::mongo::logv2::LogComponent::kJournal)); + ASSERT_EQUALS(getMinimumLogSeverity(::mongo::logv2::LogComponent::kJournal), ::mongo::logv2::LogSeverity::Debug(2)); } diff --git a/src/mongo/db/storage/wiredtiger/wiredtiger_kv_engine_test.cpp b/src/mongo/db/storage/wiredtiger/wiredtiger_kv_engine_test.cpp index d96f7fa57cc..28e77c94487 100644 --- a/src/mongo/db/storage/wiredtiger/wiredtiger_kv_engine_test.cpp +++ b/src/mongo/db/storage/wiredtiger/wiredtiger_kv_engine_test.cpp @@ -257,10 +257,8 @@ TEST_F(WiredTigerKVEngineTest, TestOplogTruncation) { wiredTigerGlobalOptions.checkpointDelaySecs = 1; // To diagnose any intermittent failures, maximize logging from WiredTigerKVEngine and friends. - const auto kStorage = logv2::LogComponent::kStorage; - auto originalVerbosity = getMinimumLogSeverity(kStorage); - setMinimumLoggedSeverity(kStorage, logv2::LogSeverity::Debug(3)); - ON_BLOCK_EXIT([&]() { setMinimumLoggedSeverity(kStorage, originalVerbosity); }); + auto severityGuard = unittest::MinimumLoggedSeverityGuard{logv2::LogComponent::kStorage, + logv2::LogSeverity::Debug(3)}; // Simulate the callback that queries config.transactions for the oldest active transaction. boost::optional<Timestamp> oldestActiveTxnTimestamp; diff --git a/src/mongo/db/transaction_participant_test.cpp b/src/mongo/db/transaction_participant_test.cpp index 990b1763a70..d85af86e9fe 100644 --- a/src/mongo/db/transaction_participant_test.cpp +++ b/src/mongo/db/transaction_participant_test.cpp @@ -3810,7 +3810,8 @@ TEST_F(TransactionsMetricsTest, LogTransactionInfoVerbosityInfo) { }); // Set verbosity level of transaction components to info. - setMinimumLoggedSeverity(logv2::LogComponent::kTransaction, logv2::LogSeverity::Info()); + auto severityGuard = unittest::MinimumLoggedSeverityGuard{logv2::LogComponent::kTransaction, + logv2::LogSeverity::Info()}; txnParticipant.unstashTransactionResources(opCtx(), "commitTransaction"); @@ -3828,7 +3829,8 @@ TEST_F(TransactionsMetricsTest, LogTransactionInfoVerbosityDebug) { auto txnParticipant = TransactionParticipant::get(opCtx()); // Set verbosity level of transaction components to debug. - setMinimumLoggedSeverity(logv2::LogComponent::kTransaction, logv2::LogSeverity::Debug(1)); + auto severityGuard = unittest::MinimumLoggedSeverityGuard{logv2::LogComponent::kTransaction, + logv2::LogSeverity::Debug(1)}; txnParticipant.unstashTransactionResources(opCtx(), "commitTransaction"); @@ -3849,9 +3851,6 @@ TEST_F(TransactionsMetricsTest, LogTransactionInfoVerbosityDebug) { txnParticipant.commitUnpreparedTransaction(opCtx()); stopCapturingLogMessages(); - // Reset verbosity level of transaction components. - setMinimumLoggedSeverity(logv2::LogComponent::kTransaction, logv2::LogSeverity::Info()); - // Test that the transaction is still logged. ASSERT_EQUALS(1, countTextFormatLogLinesContaining("transaction")); } diff --git a/src/mongo/rpc/op_msg_test.cpp b/src/mongo/rpc/op_msg_test.cpp index c9dd83b866b..362d5fb5ef9 100644 --- a/src/mongo/rpc/op_msg_test.cpp +++ b/src/mongo/rpc/op_msg_test.cpp @@ -159,17 +159,8 @@ public: // Fixture class to raise log verbosity so that invalid messages are printed by the parser. class OpMsgParser : public unittest::Test { -public: - void setUp() override { - _original = getMinimumLogSeverity(logv2::LogComponent::kNetwork); - setMinimumLoggedSeverity(logv2::LogComponent::kNetwork, logv2::LogSeverity::Debug(1)); - } - void tearDown() override { - setMinimumLoggedSeverity(logv2::LogComponent::kNetwork, _original); - } - -private: - logv2::LogSeverity _original = logv2::LogSeverity::Debug(0); + unittest::MinimumLoggedSeverityGuard _severityGuard{logv2::LogComponent::kNetwork, + logv2::LogSeverity::Debug(1)}; }; // Section bytes diff --git a/src/mongo/s/transaction_router_test.cpp b/src/mongo/s/transaction_router_test.cpp index 7155c0a961f..458ed429720 100644 --- a/src/mongo/s/transaction_router_test.cpp +++ b/src/mongo/s/transaction_router_test.cpp @@ -3171,15 +3171,16 @@ TEST_F(TransactionRouterMetricsTest, OnlyLogSlowTransactionsOnce) { } TEST_F(TransactionRouterMetricsTest, NoTransactionsLoggedAtDefaultTransactionLogLevel) { - // Set verbosity level of transaction components to the default, i.e. debug level 0. - setMinimumLoggedSeverity(logv2::LogComponent::kTransaction, logv2::LogSeverity::Log()); + auto severityGuard = unittest::MinimumLoggedSeverityGuard{logv2::LogComponent::kTransaction, + logv2::LogSeverity::Log()}; beginTxnWithDefaultTxnNumber(); runSingleShardCommit(); assertDidNotPrintSlowLogLine(); } TEST_F(TransactionRouterMetricsTest, AllTransactionsLoggedAtTransactionLogLevelOne) { - setMinimumLoggedSeverity(logv2::LogComponent::kTransaction, logv2::LogSeverity::Debug(1)); + auto severityGuard = unittest::MinimumLoggedSeverityGuard{logv2::LogComponent::kTransaction, + logv2::LogSeverity::Debug(1)}; beginTxnWithDefaultTxnNumber(); runSingleShardCommit(); assertPrintedExactlyOneSlowLogLine(); diff --git a/src/mongo/unittest/log_test.h b/src/mongo/unittest/log_test.h index 66a2b22ca64..9ab394f9fd2 100644 --- a/src/mongo/unittest/log_test.h +++ b/src/mongo/unittest/log_test.h @@ -36,35 +36,48 @@ #include "mongo/logv2/log_manager.h" #include "mongo/logv2/log_severity.h" -namespace mongo { +namespace mongo::unittest { -inline logv2::LogSeverity getMinimumLogSeverity() { - return logv2::LogManager::global().getGlobalSettings().getMinimumLogSeverity( - mongo::logv2::LogComponent::kDefault); +namespace log_test_detail { + +using logv2::LogComponent; +using logv2::LogSeverity; + +inline auto& _settings() { + return logv2::LogManager::global().getGlobalSettings(); +} + +inline bool hasMinimumLogSeverity(LogComponent component) { + return _settings().hasMinimumLogSeverity(component); } -inline logv2::LogSeverity getMinimumLogSeverity(logv2::LogComponent component) { - return logv2::LogManager::global().getGlobalSettings().getMinimumLogSeverity(component); +inline LogSeverity getMinimumLogSeverity(LogComponent component) { + return _settings().getMinimumLogSeverity(component); } -inline void setMinimumLoggedSeverity(logv2::LogSeverity severity) { - return logv2::LogManager::global().getGlobalSettings().setMinimumLoggedSeverity( - mongo::logv2::LogComponent::kDefault, severity); +inline LogSeverity getMinimumLogSeverity() { + return getMinimumLogSeverity(LogComponent::kDefault); } -inline void setMinimumLoggedSeverity(logv2::LogComponent component, logv2::LogSeverity severity) { - return logv2::LogManager::global().getGlobalSettings().setMinimumLoggedSeverity(component, - severity); +inline void setMinimumLoggedSeverity(LogComponent component, LogSeverity severity) { + return _settings().setMinimumLoggedSeverity(component, severity); } -inline void clearMinimumLoggedSeverity(logv2::LogComponent component) { - return logv2::LogManager::global().getGlobalSettings().clearMinimumLoggedSeverity(component); +inline void setMinimumLoggedSeverity(LogSeverity severity) { + return setMinimumLoggedSeverity(LogComponent::kDefault, severity); } -inline bool hasMinimumLogSeverity(logv2::LogComponent component) { - return logv2::LogManager::global().getGlobalSettings().hasMinimumLogSeverity(component); +inline void clearMinimumLoggedSeverity(LogComponent component) { + return _settings().clearMinimumLoggedSeverity(component); } +} // namespace log_test_detail + +using log_test_detail::clearMinimumLoggedSeverity; +using log_test_detail::getMinimumLogSeverity; +using log_test_detail::hasMinimumLogSeverity; +using log_test_detail::setMinimumLoggedSeverity; + /** * Configure a LogComponent`s MinimumLoggedSeverity, saving the old state and restoring it * when this guard object dies. There can be no severity mapping for a LogComponent, so @@ -72,9 +85,14 @@ inline bool hasMinimumLogSeverity(logv2::LogComponent component) { */ class MinimumLoggedSeverityGuard { public: + /** Just save and restore: do not change the severity at ctor time. */ + explicit MinimumLoggedSeverityGuard(logv2::LogComponent component) + : _component{component}, _savedSeverity{_get()} {} + + /** Change the `component` to have `severity`. */ MinimumLoggedSeverityGuard(logv2::LogComponent component, boost::optional<logv2::LogSeverity> severity) - : _component{component}, _savedSeverity{_get()} { + : MinimumLoggedSeverityGuard{component} { _put(severity); } @@ -101,4 +119,4 @@ private: boost::optional<logv2::LogSeverity> _savedSeverity; }; -} // namespace mongo +} // namespace mongo::unittest diff --git a/src/mongo/unittest/unittest_main.cpp b/src/mongo/unittest/unittest_main.cpp index 3ddb44f977d..6b0330f5a45 100644 --- a/src/mongo/unittest/unittest_main.cpp +++ b/src/mongo/unittest/unittest_main.cpp @@ -95,7 +95,7 @@ int main(int argc, char** argv, char** envp) { std::cerr << options.helpString(); return EXIT_FAILURE; } - mongo::setMinimumLoggedSeverity(::mongo::logv2::LogSeverity::Debug(verbose.length())); + mongo::unittest::setMinimumLoggedSeverity(mongo::logv2::LogSeverity::Debug(verbose.size())); if (list) { auto suiteNames = ::mongo::unittest::getAllSuiteNames(); diff --git a/src/mongo/util/log_with_sampling_test.cpp b/src/mongo/util/log_with_sampling_test.cpp index 29121afe664..430893e8893 100644 --- a/src/mongo/util/log_with_sampling_test.cpp +++ b/src/mongo/util/log_with_sampling_test.cpp @@ -59,7 +59,7 @@ auto scenario(bool debugLogEnabled, bool slowOp, bool forceSample) { const auto client = serviceContext->makeClient("log_with_sampling_test"); const auto opCtx = client->makeOperationContext(); - auto loggedSeverityGuard = MinimumLoggedSeverityGuard( + auto loggedSeverityGuard = unittest::MinimumLoggedSeverityGuard( component, debugLogEnabled ? logv2::LogSeverity::Debug(1) : logv2::LogSeverity::Info()); auto sampleRateGuard = makeGuard( |