Repository: envoyproxy/envoy
File: test/test_runner.cc (C++)

Code before:
  Logger::Context logging_state(options.logLevel(), options.logFormat(), lock, false,
                                options.enableFineGrainLogging());

  // Allocate fake log access manager.
  testing::NiceMock<AccessLog::MockAccessLogManager> access_log_manager;
  std::unique_ptr<Logger::FileSinkDelegate> file_logger;

  // Redirect all logs to fake file when --log-path arg is specified in command line.
  // However do not redirect to file from death test children as the parent typically
  // looks for specific output in stderr
  if (!TestEnvironment::getOptions().logPath().empty() && !is_death_test_child) {
    file_logger = *Logger::FileSinkDelegate::create(
        TestEnvironment::getOptions().logPath(), access_log_manager, Logger::Registry::getSink());
  }

  // Reset all ENVOY_BUG counters.
  Envoy::Assert::resetEnvoyBugCountersForTest();

#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  // Fuzz tests may run Envoy tests in fuzzing mode to generate corpora. In this case, we do not
  // want to fail building the fuzz test because of a failed test run, which can happen when testing
  // functionality in fuzzing test mode. Dependencies (like RE2) change behavior when in fuzzing
  // mode, so we do not want to rely on a behavior test when generating a corpus.
  (void)RUN_ALL_TESTS();
  return 0;
#else
  return RUN_ALL_TESTS();
#endif
}

} // namespace Envoy


Review comment (suggestion):
Could we make this a `InjectableSingleton` and we needn't to store it at the prod sink. We can inject it if necessary here and clear at the end of the main function. The lifetime of the record could also be managed by an unique pointer and we needn't to care the leak.

Code after:

  Server::Options& options = TestEnvironment::getOptions();
  Logger::Context logging_state(options.logLevel(), options.logFormat(), lock, false,
                                options.enableFineGrainLogging());

  // Allocate fake log access manager.
  testing::NiceMock<AccessLog::MockAccessLogManager> access_log_manager;
  std::unique_ptr<Logger::FileSinkDelegate> file_logger;

  // Redirect all logs to fake file when --log-path arg is specified in command line.
  // However do not redirect to file from death test children as the parent typically
  // looks for specific output in stderr
  if (!TestEnvironment::getOptions().logPath().empty() && !is_death_test_child) {
    file_logger = *Logger::FileSinkDelegate::create(
        TestEnvironment::getOptions().logPath(), access_log_manager, Logger::Registry::getSink());
  }

  // Reset all ENVOY_BUG counters.
  Envoy::Assert::resetEnvoyBugCountersForTest();

  // Initialize log recording sink.
  LogRecordingSink* recorder;
  if (std::getenv("ENVOY_NO_LOG_SINK") == nullptr) {
    recorder = absl::IgnoreLeak(new LogRecordingSink(Logger::Registry::getSink()));
    Logger::Registry::getSink()->recorder_test_only_ = recorder;
  }

#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  // Fuzz tests may run Envoy tests in fuzzing mode to generate corpora. In this case, we do not
  // want to fail building the fuzz test because of a failed test run, which can happen when testing
  // functionality in fuzzing test mode. Dependencies (like RE2) change behavior when in fuzzing
  // mode, so we do not want to rely on a behavior test when generating a corpus.
  (void)RUN_ALL_TESTS();
  return 0;
#else
  return RUN_ALL_TESTS();
#endif
}

} // namespace Envoy