1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/test/launcher/test_launcher.h"
11 #include "base/at_exit.h"
12 #include "base/bind.h"
13 #include "base/command_line.h"
14 #include "base/environment.h"
15 #include "base/files/file_path.h"
16 #include "base/files/file_util.h"
17 #include "base/files/scoped_file.h"
18 #include "base/format_macros.h"
19 #include "base/hash.h"
20 #include "base/lazy_instance.h"
21 #include "base/logging.h"
22 #include "base/memory/scoped_ptr.h"
23 #include "base/message_loop/message_loop.h"
24 #include "base/process/kill.h"
25 #include "base/process/launch.h"
26 #include "base/strings/string_number_conversions.h"
27 #include "base/strings/string_split.h"
28 #include "base/strings/string_util.h"
29 #include "base/strings/stringize_macros.h"
30 #include "base/strings/stringprintf.h"
31 #include "base/strings/utf_string_conversions.h"
32 #include "base/test/launcher/test_results_tracker.h"
33 #include "base/test/sequenced_worker_pool_owner.h"
34 #include "base/test/test_switches.h"
35 #include "base/test/test_timeouts.h"
36 #include "base/threading/thread_checker.h"
37 #include "base/time/time.h"
38 #include "testing/gtest/include/gtest/gtest.h"
40 #if defined(OS_MACOSX)
41 #include "base/mac/scoped_nsautorelease_pool.h"
45 #include "base/win/windows_version.h"
50 // See https://groups.google.com/a/chromium.org/d/msg/chromium-dev/nkdTP7sstSc/uT3FaE_sgkAJ .
53 // The environment variable name for the total number of test shards.
54 const char kTestTotalShards[] = "GTEST_TOTAL_SHARDS";
55 // The environment variable name for the test shard index.
56 const char kTestShardIndex[] = "GTEST_SHARD_INDEX";
60 // Global tag for test runs where the results are incomplete or unreliable
61 // for any reason, e.g. early exit because of too many broken tests.
62 const char kUnreliableResultsTag[] = "UNRELIABLE_RESULTS";
64 // Maximum time of no output after which we print list of processes still
65 // running. This deliberately doesn't use TestTimeouts (which is otherwise
66 // a recommended solution), because they can be increased. This would defeat
67 // the purpose of this timeout, which is 1) to avoid buildbot "no output for
68 // X seconds" timeout killing the process 2) help communicate status of
69 // the test launcher to people looking at the output (no output for a long
70 // time is mysterious and gives no info about what is happening) 3) help
71 // debugging in case the process hangs anyway.
72 const int kOutputTimeoutSeconds = 15;
74 // Limit of output snippet lines when printing to stdout.
75 // Avoids flooding the logs with amount of output that gums up
76 // the infrastructure.
77 const size_t kOutputSnippetLinesLimit = 5000;
79 // Set of live launch test processes with corresponding lock (it is allowed
80 // for callers to launch processes on different threads).
81 LazyInstance<std::map<ProcessHandle, CommandLine> > g_live_processes
82 = LAZY_INSTANCE_INITIALIZER;
83 LazyInstance<Lock> g_live_processes_lock = LAZY_INSTANCE_INITIALIZER;
86 // Self-pipe that makes it possible to do complex shutdown handling
87 // outside of the signal handler.
88 int g_shutdown_pipe[2] = { -1, -1 };
90 void ShutdownPipeSignalHandler(int signal) {
91 HANDLE_EINTR(write(g_shutdown_pipe[1], "q", 1));
94 void KillSpawnedTestProcesses() {
95 // Keep the lock until exiting the process to prevent further processes
96 // from being spawned.
97 AutoLock lock(g_live_processes_lock.Get());
100 "Sending SIGTERM to %" PRIuS " child processes... ",
101 g_live_processes.Get().size());
104 for (std::map<ProcessHandle, CommandLine>::iterator i =
105 g_live_processes.Get().begin();
106 i != g_live_processes.Get().end();
108 // Send the signal to entire process group.
109 kill((-1) * (i->first), SIGTERM);
113 "done.\nGiving processes a chance to terminate cleanly... ");
116 PlatformThread::Sleep(TimeDelta::FromMilliseconds(500));
118 fprintf(stdout, "done.\n");
122 "Sending SIGKILL to %" PRIuS " child processes... ",
123 g_live_processes.Get().size());
126 for (std::map<ProcessHandle, CommandLine>::iterator i =
127 g_live_processes.Get().begin();
128 i != g_live_processes.Get().end();
130 // Send the signal to entire process group.
131 kill((-1) * (i->first), SIGKILL);
134 fprintf(stdout, "done.\n");
138 // I/O watcher for the reading end of the self-pipe above.
139 // Terminates any launched child processes and exits the process.
140 class SignalFDWatcher : public MessageLoopForIO::Watcher {
145 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE {
146 fprintf(stdout, "\nCaught signal. Killing spawned test processes...\n");
149 KillSpawnedTestProcesses();
151 // The signal would normally kill the process, so exit now.
155 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE {
160 DISALLOW_COPY_AND_ASSIGN(SignalFDWatcher);
162 #endif // defined(OS_POSIX)
164 // Parses the environment variable var as an Int32. If it is unset, returns
165 // true. If it is set, unsets it then converts it to Int32 before
166 // returning it in |result|. Returns true on success.
167 bool TakeInt32FromEnvironment(const char* const var, int32* result) {
168 scoped_ptr<Environment> env(Environment::Create());
171 if (!env->GetVar(var, &str_val))
174 if (!env->UnSetVar(var)) {
175 LOG(ERROR) << "Invalid environment: we could not unset " << var << ".\n";
179 if (!StringToInt(str_val, result)) {
180 LOG(ERROR) << "Invalid environment: " << var << " is not an integer.\n";
187 // Unsets the environment variable |name| and returns true on success.
188 // Also returns true if the variable just doesn't exist.
189 bool UnsetEnvironmentVariableIfExists(const std::string& name) {
190 scoped_ptr<Environment> env(Environment::Create());
193 if (!env->GetVar(name.c_str(), &str_val))
196 return env->UnSetVar(name.c_str());
199 // Returns true if bot mode has been requested, i.e. defaults optimized
200 // for continuous integration bots. This way developers don't have to remember
201 // special command-line flags.
202 bool BotModeEnabled() {
203 scoped_ptr<Environment> env(Environment::Create());
204 return CommandLine::ForCurrentProcess()->HasSwitch(
205 switches::kTestLauncherBotMode) ||
206 env->HasVar("CHROMIUM_TEST_LAUNCHER_BOT_MODE");
209 // Returns command line command line after gtest-specific processing
210 // and applying |wrapper|.
211 CommandLine PrepareCommandLineForGTest(const CommandLine& command_line,
212 const std::string& wrapper) {
213 CommandLine new_command_line(command_line.GetProgram());
214 CommandLine::SwitchMap switches = command_line.GetSwitches();
216 // Strip out gtest_repeat flag - this is handled by the launcher process.
217 switches.erase(kGTestRepeatFlag);
219 // Don't try to write the final XML report in child processes.
220 switches.erase(kGTestOutputFlag);
222 for (CommandLine::SwitchMap::const_iterator iter = switches.begin();
223 iter != switches.end(); ++iter) {
224 new_command_line.AppendSwitchNative((*iter).first, (*iter).second);
227 // Prepend wrapper after last CommandLine quasi-copy operation. CommandLine
228 // does not really support removing switches well, and trying to do that
229 // on a CommandLine with a wrapper is known to break.
230 // TODO(phajdan.jr): Give it a try to support CommandLine removing switches.
232 new_command_line.PrependWrapper(ASCIIToWide(wrapper));
233 #elif defined(OS_POSIX)
234 new_command_line.PrependWrapper(wrapper);
237 return new_command_line;
240 // Launches a child process using |command_line|. If the child process is still
241 // running after |timeout|, it is terminated and |*was_timeout| is set to true.
242 // Returns exit code of the process.
243 int LaunchChildTestProcessWithOptions(const CommandLine& command_line,
244 const LaunchOptions& options,
246 base::TimeDelta timeout,
248 #if defined(OS_POSIX)
249 // Make sure an option we rely on is present - see LaunchChildGTestProcess.
250 DCHECK(options.new_process_group);
253 LaunchOptions new_options(options);
256 DCHECK(!new_options.job_handle);
258 win::ScopedHandle job_handle;
259 if (flags & TestLauncher::USE_JOB_OBJECTS) {
260 job_handle.Set(CreateJobObject(NULL, NULL));
261 if (!job_handle.IsValid()) {
262 LOG(ERROR) << "Could not create JobObject.";
266 DWORD job_flags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
268 // Allow break-away from job since sandbox and few other places rely on it
269 // on Windows versions prior to Windows 8 (which supports nested jobs).
270 if (win::GetVersion() < win::VERSION_WIN8 &&
271 flags & TestLauncher::ALLOW_BREAKAWAY_FROM_JOB) {
272 job_flags |= JOB_OBJECT_LIMIT_BREAKAWAY_OK;
275 if (!SetJobObjectLimitFlags(job_handle.Get(), job_flags)) {
276 LOG(ERROR) << "Could not SetJobObjectLimitFlags.";
280 new_options.job_handle = job_handle.Get();
282 #endif // defined(OS_WIN)
284 #if defined(OS_LINUX)
285 // To prevent accidental privilege sharing to an untrusted child, processes
286 // are started with PR_SET_NO_NEW_PRIVS. Do not set that here, since this
287 // new child will be privileged and trusted.
288 new_options.allow_new_privs = true;
291 base::ProcessHandle process_handle;
294 // Note how we grab the lock before the process possibly gets created.
295 // This ensures that when the lock is held, ALL the processes are registered
297 AutoLock lock(g_live_processes_lock.Get());
299 if (!base::LaunchProcess(command_line, new_options, &process_handle))
302 g_live_processes.Get().insert(std::make_pair(process_handle, command_line));
306 if (!base::WaitForExitCodeWithTimeout(process_handle,
310 exit_code = -1; // Set a non-zero exit code to signal a failure.
312 // Ensure that the process terminates.
313 base::KillProcess(process_handle, -1, true);
317 // Note how we grab the log before issuing a possibly broad process kill.
318 // Other code parts that grab the log kill processes, so avoid trying
319 // to do that twice and trigger all kinds of log messages.
320 AutoLock lock(g_live_processes_lock.Get());
322 #if defined(OS_POSIX)
323 if (exit_code != 0) {
324 // On POSIX, in case the test does not exit cleanly, either due to a crash
325 // or due to it timing out, we need to clean up any child processes that
326 // it might have created. On Windows, child processes are automatically
327 // cleaned up using JobObjects.
328 base::KillProcessGroup(process_handle);
332 g_live_processes.Get().erase(process_handle);
335 base::CloseProcessHandle(process_handle);
341 const TestLauncher::LaunchChildGTestProcessCallback& callback,
343 const TimeDelta& elapsed_time,
345 const std::string& output) {
346 callback.Run(exit_code, elapsed_time, was_timeout, output);
349 void DoLaunchChildTestProcess(
350 const CommandLine& command_line,
351 base::TimeDelta timeout,
354 scoped_refptr<MessageLoopProxy> message_loop_proxy,
355 const TestLauncher::LaunchChildGTestProcessCallback& callback) {
356 TimeTicks start_time = TimeTicks::Now();
358 // Redirect child process output to a file.
359 base::FilePath output_file;
360 CHECK(base::CreateTemporaryFile(&output_file));
362 LaunchOptions options;
364 win::ScopedHandle handle;
366 if (redirect_stdio) {
367 // Make the file handle inheritable by the child.
368 SECURITY_ATTRIBUTES sa_attr;
369 sa_attr.nLength = sizeof(SECURITY_ATTRIBUTES);
370 sa_attr.lpSecurityDescriptor = NULL;
371 sa_attr.bInheritHandle = TRUE;
373 handle.Set(CreateFile(output_file.value().c_str(),
375 FILE_SHARE_READ | FILE_SHARE_DELETE,
378 FILE_ATTRIBUTE_TEMPORARY,
380 CHECK(handle.IsValid());
381 options.inherit_handles = true;
382 options.stdin_handle = INVALID_HANDLE_VALUE;
383 options.stdout_handle = handle.Get();
384 options.stderr_handle = handle.Get();
386 #elif defined(OS_POSIX)
387 options.new_process_group = true;
389 base::FileHandleMappingVector fds_mapping;
390 base::ScopedFD output_file_fd;
392 if (redirect_stdio) {
393 output_file_fd.reset(open(output_file.value().c_str(), O_RDWR));
394 CHECK(output_file_fd.is_valid());
396 fds_mapping.push_back(std::make_pair(output_file_fd.get(), STDOUT_FILENO));
397 fds_mapping.push_back(std::make_pair(output_file_fd.get(), STDERR_FILENO));
398 options.fds_to_remap = &fds_mapping;
402 bool was_timeout = false;
403 int exit_code = LaunchChildTestProcessWithOptions(
404 command_line, options, flags, timeout, &was_timeout);
406 if (redirect_stdio) {
408 FlushFileBuffers(handle.Get());
410 #elif defined(OS_POSIX)
411 output_file_fd.reset();
415 std::string output_file_contents;
416 CHECK(base::ReadFileToString(output_file, &output_file_contents));
418 if (!base::DeleteFile(output_file, false)) {
419 // This needs to be non-fatal at least for Windows.
420 LOG(WARNING) << "Failed to delete " << output_file.AsUTF8Unsafe();
423 // Run target callback on the thread it was originating from, not on
424 // a worker pool thread.
425 message_loop_proxy->PostTask(
430 TimeTicks::Now() - start_time,
432 output_file_contents));
437 const char kGTestFilterFlag[] = "gtest_filter";
438 const char kGTestHelpFlag[] = "gtest_help";
439 const char kGTestListTestsFlag[] = "gtest_list_tests";
440 const char kGTestRepeatFlag[] = "gtest_repeat";
441 const char kGTestRunDisabledTestsFlag[] = "gtest_also_run_disabled_tests";
442 const char kGTestOutputFlag[] = "gtest_output";
444 TestLauncherDelegate::~TestLauncherDelegate() {
447 TestLauncher::TestLauncher(TestLauncherDelegate* launcher_delegate,
448 size_t parallel_jobs)
449 : launcher_delegate_(launcher_delegate),
453 test_started_count_(0),
454 test_finished_count_(0),
455 test_success_count_(0),
456 test_broken_count_(0),
460 watchdog_timer_(FROM_HERE,
461 TimeDelta::FromSeconds(kOutputTimeoutSeconds),
463 &TestLauncher::OnOutputTimeout),
464 parallel_jobs_(parallel_jobs) {
467 TestLauncher::~TestLauncher() {
468 if (worker_pool_owner_)
469 worker_pool_owner_->pool()->Shutdown();
472 bool TestLauncher::Run() {
476 // Value of |cycles_| changes after each iteration. Keep track of the
478 int requested_cycles = cycles_;
480 #if defined(OS_POSIX)
481 CHECK_EQ(0, pipe(g_shutdown_pipe));
483 struct sigaction action;
484 memset(&action, 0, sizeof(action));
485 sigemptyset(&action.sa_mask);
486 action.sa_handler = &ShutdownPipeSignalHandler;
488 CHECK_EQ(0, sigaction(SIGINT, &action, NULL));
489 CHECK_EQ(0, sigaction(SIGQUIT, &action, NULL));
490 CHECK_EQ(0, sigaction(SIGTERM, &action, NULL));
492 MessageLoopForIO::FileDescriptorWatcher controller;
493 SignalFDWatcher watcher;
495 CHECK(MessageLoopForIO::current()->WatchFileDescriptor(
498 MessageLoopForIO::WATCH_READ,
501 #endif // defined(OS_POSIX)
503 // Start the watchdog timer.
504 watchdog_timer_.Reset();
506 MessageLoop::current()->PostTask(
508 Bind(&TestLauncher::RunTestIteration, Unretained(this)));
510 MessageLoop::current()->Run();
512 if (requested_cycles != 1)
513 results_tracker_.PrintSummaryOfAllIterations();
515 MaybeSaveSummaryAsJSON();
520 void TestLauncher::LaunchChildGTestProcess(
521 const CommandLine& command_line,
522 const std::string& wrapper,
523 base::TimeDelta timeout,
525 const LaunchChildGTestProcessCallback& callback) {
526 DCHECK(thread_checker_.CalledOnValidThread());
528 // Record the exact command line used to launch the child.
529 CommandLine new_command_line(
530 PrepareCommandLineForGTest(command_line, wrapper));
532 // When running in parallel mode we need to redirect stdio to avoid mixed-up
533 // output. We also always redirect on the bots to get the test output into
535 bool redirect_stdio = (parallel_jobs_ > 1) || BotModeEnabled();
537 worker_pool_owner_->pool()->PostWorkerTask(
539 Bind(&DoLaunchChildTestProcess,
544 MessageLoopProxy::current(),
545 Bind(&TestLauncher::OnLaunchTestProcessFinished,
550 void TestLauncher::OnTestFinished(const TestResult& result) {
551 ++test_finished_count_;
553 bool print_snippet = false;
554 std::string print_test_stdio("auto");
555 if (CommandLine::ForCurrentProcess()->HasSwitch(
556 switches::kTestLauncherPrintTestStdio)) {
557 print_test_stdio = CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
558 switches::kTestLauncherPrintTestStdio);
560 if (print_test_stdio == "auto") {
561 print_snippet = (result.status != TestResult::TEST_SUCCESS);
562 } else if (print_test_stdio == "always") {
563 print_snippet = true;
564 } else if (print_test_stdio == "never") {
565 print_snippet = false;
567 LOG(WARNING) << "Invalid value of " << switches::kTestLauncherPrintTestStdio
568 << ": " << print_test_stdio;
571 std::vector<std::string> snippet_lines;
572 SplitString(result.output_snippet, '\n', &snippet_lines);
573 if (snippet_lines.size() > kOutputSnippetLinesLimit) {
574 size_t truncated_size = snippet_lines.size() - kOutputSnippetLinesLimit;
576 snippet_lines.begin(),
577 snippet_lines.begin() + truncated_size);
578 snippet_lines.insert(snippet_lines.begin(), "<truncated>");
580 fprintf(stdout, "%s", JoinString(snippet_lines, "\n").c_str());
584 if (result.status == TestResult::TEST_SUCCESS) {
585 ++test_success_count_;
587 tests_to_retry_.insert(result.full_name);
590 results_tracker_.AddTestResult(result);
592 // TODO(phajdan.jr): Align counter (padding).
593 std::string status_line(
594 StringPrintf("[%" PRIuS "/%" PRIuS "] %s ",
595 test_finished_count_,
597 result.full_name.c_str()));
598 if (result.completed()) {
599 status_line.append(StringPrintf("(%" PRId64 " ms)",
600 result.elapsed_time.InMilliseconds()));
601 } else if (result.status == TestResult::TEST_TIMEOUT) {
602 status_line.append("(TIMED OUT)");
603 } else if (result.status == TestResult::TEST_CRASH) {
604 status_line.append("(CRASHED)");
605 } else if (result.status == TestResult::TEST_SKIPPED) {
606 status_line.append("(SKIPPED)");
607 } else if (result.status == TestResult::TEST_UNKNOWN) {
608 status_line.append("(UNKNOWN)");
610 // Fail very loudly so it's not ignored.
611 CHECK(false) << "Unhandled test result status: " << result.status;
613 fprintf(stdout, "%s\n", status_line.c_str());
616 // We just printed a status line, reset the watchdog timer.
617 watchdog_timer_.Reset();
619 // Do not waste time on timeouts. We include tests with unknown results here
620 // because sometimes (e.g. hang in between unit tests) that's how a timeout
622 if (result.status == TestResult::TEST_TIMEOUT ||
623 result.status == TestResult::TEST_UNKNOWN) {
624 test_broken_count_++;
626 size_t broken_threshold =
627 std::max(static_cast<size_t>(20), test_started_count_ / 10);
628 if (test_broken_count_ >= broken_threshold) {
629 fprintf(stdout, "Too many badly broken tests (%" PRIuS "), exiting now.\n",
633 #if defined(OS_POSIX)
634 KillSpawnedTestProcesses();
635 #endif // defined(OS_POSIX)
637 results_tracker_.AddGlobalTag("BROKEN_TEST_EARLY_EXIT");
638 results_tracker_.AddGlobalTag(kUnreliableResultsTag);
639 MaybeSaveSummaryAsJSON();
644 if (test_finished_count_ != test_started_count_)
647 if (tests_to_retry_.empty() || retry_count_ >= retry_limit_) {
648 OnTestIterationFinished();
652 if (tests_to_retry_.size() >= broken_threshold) {
654 "Too many failing tests (%" PRIuS "), skipping retries.\n",
655 tests_to_retry_.size());
658 results_tracker_.AddGlobalTag("BROKEN_TEST_SKIPPED_RETRIES");
659 results_tracker_.AddGlobalTag(kUnreliableResultsTag);
661 OnTestIterationFinished();
667 std::vector<std::string> test_names(tests_to_retry_.begin(),
668 tests_to_retry_.end());
670 tests_to_retry_.clear();
672 size_t retry_started_count = launcher_delegate_->RetryTests(this, test_names);
673 if (retry_started_count == 0) {
674 // Signal failure, but continue to run all requested test iterations.
675 // With the summary of all iterations at the end this is a good default.
678 OnTestIterationFinished();
682 fprintf(stdout, "Retrying %" PRIuS " test%s (retry #%" PRIuS ")\n",
684 retry_started_count > 1 ? "s" : "",
688 test_started_count_ += retry_started_count;
692 std::string TestLauncher::FormatFullTestName(const std::string& test_case_name,
693 const std::string& test_name) {
694 return test_case_name + "." + test_name;
697 bool TestLauncher::Init() {
698 const CommandLine* command_line = CommandLine::ForCurrentProcess();
700 // Initialize sharding. Command line takes precedence over legacy environment
702 if (command_line->HasSwitch(switches::kTestLauncherTotalShards) &&
703 command_line->HasSwitch(switches::kTestLauncherShardIndex)) {
705 command_line->GetSwitchValueASCII(
706 switches::kTestLauncherTotalShards),
708 LOG(ERROR) << "Invalid value for " << switches::kTestLauncherTotalShards;
712 command_line->GetSwitchValueASCII(
713 switches::kTestLauncherShardIndex),
715 LOG(ERROR) << "Invalid value for " << switches::kTestLauncherShardIndex;
719 "Using sharding settings from command line. This is shard %d/%d\n",
720 shard_index_, total_shards_);
723 if (!TakeInt32FromEnvironment(kTestTotalShards, &total_shards_))
725 if (!TakeInt32FromEnvironment(kTestShardIndex, &shard_index_))
728 "Using sharding settings from environment. This is shard %d/%d\n",
729 shard_index_, total_shards_);
732 if (shard_index_ < 0 ||
734 shard_index_ >= total_shards_) {
735 LOG(ERROR) << "Invalid sharding settings: we require 0 <= "
736 << kTestShardIndex << " < " << kTestTotalShards
737 << ", but you have " << kTestShardIndex << "=" << shard_index_
738 << ", " << kTestTotalShards << "=" << total_shards_ << ".\n";
742 // Make sure we don't pass any sharding-related environment to the child
743 // processes. This test launcher implements the sharding completely.
744 CHECK(UnsetEnvironmentVariableIfExists("GTEST_TOTAL_SHARDS"));
745 CHECK(UnsetEnvironmentVariableIfExists("GTEST_SHARD_INDEX"));
747 if (command_line->HasSwitch(kGTestRepeatFlag) &&
748 !StringToInt(command_line->GetSwitchValueASCII(kGTestRepeatFlag),
750 LOG(ERROR) << "Invalid value for " << kGTestRepeatFlag;
754 if (command_line->HasSwitch(switches::kTestLauncherRetryLimit)) {
755 int retry_limit = -1;
756 if (!StringToInt(command_line->GetSwitchValueASCII(
757 switches::kTestLauncherRetryLimit), &retry_limit) ||
759 LOG(ERROR) << "Invalid value for " << switches::kTestLauncherRetryLimit;
763 retry_limit_ = retry_limit;
764 } else if (!command_line->HasSwitch(kGTestFilterFlag) || BotModeEnabled()) {
765 // Retry failures 3 times by default if we are running all of the tests or
770 if (command_line->HasSwitch(switches::kTestLauncherJobs)) {
772 if (!StringToInt(command_line->GetSwitchValueASCII(
773 switches::kTestLauncherJobs), &jobs) ||
775 LOG(ERROR) << "Invalid value for " << switches::kTestLauncherJobs;
779 parallel_jobs_ = jobs;
780 } else if (command_line->HasSwitch(kGTestFilterFlag) && !BotModeEnabled()) {
781 // Do not run jobs in parallel by default if we are running a subset of
782 // the tests and if bot mode is off.
786 fprintf(stdout, "Using %" PRIuS " parallel jobs.\n", parallel_jobs_);
788 worker_pool_owner_.reset(
789 new SequencedWorkerPoolOwner(parallel_jobs_, "test_launcher"));
791 if (command_line->HasSwitch(switches::kTestLauncherFilterFile) &&
792 command_line->HasSwitch(kGTestFilterFlag)) {
793 LOG(ERROR) << "Only one of --test-launcher-filter-file and --gtest_filter "
794 << "at a time is allowed.";
798 if (command_line->HasSwitch(switches::kTestLauncherFilterFile)) {
800 if (!ReadFileToString(
801 command_line->GetSwitchValuePath(switches::kTestLauncherFilterFile),
803 LOG(ERROR) << "Failed to read the filter file.";
807 std::vector<std::string> filter_lines;
808 SplitString(filter, '\n', &filter_lines);
809 for (size_t i = 0; i < filter_lines.size(); i++) {
810 if (filter_lines[i].empty())
813 if (filter_lines[i][0] == '-')
814 negative_test_filter_.push_back(filter_lines[i].substr(1));
816 positive_test_filter_.push_back(filter_lines[i]);
819 // Split --gtest_filter at '-', if there is one, to separate into
820 // positive filter and negative filter portions.
821 std::string filter = command_line->GetSwitchValueASCII(kGTestFilterFlag);
822 size_t dash_pos = filter.find('-');
823 if (dash_pos == std::string::npos) {
824 SplitString(filter, ':', &positive_test_filter_);
826 // Everything up to the dash.
827 SplitString(filter.substr(0, dash_pos), ':', &positive_test_filter_);
829 // Everything after the dash.
830 SplitString(filter.substr(dash_pos + 1), ':', &negative_test_filter_);
834 if (!results_tracker_.Init(*command_line)) {
835 LOG(ERROR) << "Failed to initialize test results tracker.";
840 results_tracker_.AddGlobalTag("MODE_RELEASE");
842 results_tracker_.AddGlobalTag("MODE_DEBUG");
845 // Operating systems (sorted alphabetically).
846 // Note that they can deliberately overlap, e.g. OS_LINUX is a subset
848 #if defined(OS_ANDROID)
849 results_tracker_.AddGlobalTag("OS_ANDROID");
853 results_tracker_.AddGlobalTag("OS_BSD");
856 #if defined(OS_FREEBSD)
857 results_tracker_.AddGlobalTag("OS_FREEBSD");
861 results_tracker_.AddGlobalTag("OS_IOS");
864 #if defined(OS_LINUX)
865 results_tracker_.AddGlobalTag("OS_LINUX");
868 #if defined(OS_MACOSX)
869 results_tracker_.AddGlobalTag("OS_MACOSX");
873 results_tracker_.AddGlobalTag("OS_NACL");
876 #if defined(OS_OPENBSD)
877 results_tracker_.AddGlobalTag("OS_OPENBSD");
880 #if defined(OS_POSIX)
881 results_tracker_.AddGlobalTag("OS_POSIX");
884 #if defined(OS_SOLARIS)
885 results_tracker_.AddGlobalTag("OS_SOLARIS");
889 results_tracker_.AddGlobalTag("OS_WIN");
893 #if defined(ARCH_CPU_32_BITS)
894 results_tracker_.AddGlobalTag("CPU_32_BITS");
897 #if defined(ARCH_CPU_64_BITS)
898 results_tracker_.AddGlobalTag("CPU_64_BITS");
904 void TestLauncher::RunTests() {
905 testing::UnitTest* const unit_test = testing::UnitTest::GetInstance();
907 std::vector<std::string> test_names;
909 for (int i = 0; i < unit_test->total_test_case_count(); ++i) {
910 const testing::TestCase* test_case = unit_test->GetTestCase(i);
911 for (int j = 0; j < test_case->total_test_count(); ++j) {
912 const testing::TestInfo* test_info = test_case->GetTestInfo(j);
913 std::string test_name = FormatFullTestName(
914 test_info->test_case_name(), test_info->name());
916 results_tracker_.AddTest(test_name);
918 const CommandLine* command_line = CommandLine::ForCurrentProcess();
919 if (test_name.find("DISABLED") != std::string::npos) {
920 results_tracker_.AddDisabledTest(test_name);
922 // Skip disabled tests unless explicitly requested.
923 if (!command_line->HasSwitch(kGTestRunDisabledTestsFlag))
927 if (!launcher_delegate_->ShouldRunTest(test_case, test_info))
930 // Skip the test that doesn't match the filter (if given).
931 if (!positive_test_filter_.empty()) {
933 for (size_t k = 0; k < positive_test_filter_.size(); ++k) {
934 if (MatchPattern(test_name, positive_test_filter_[k])) {
943 bool excluded = false;
944 for (size_t k = 0; k < negative_test_filter_.size(); ++k) {
945 if (MatchPattern(test_name, negative_test_filter_[k])) {
953 if (base::Hash(test_name) % total_shards_ !=
954 static_cast<uint32>(shard_index_)) {
958 test_names.push_back(test_name);
962 test_started_count_ = launcher_delegate_->RunTests(this, test_names);
964 if (test_started_count_ == 0) {
965 fprintf(stdout, "0 tests run\n");
968 // No tests have actually been started, so kick off the next iteration.
969 MessageLoop::current()->PostTask(
971 Bind(&TestLauncher::RunTestIteration, Unretained(this)));
975 void TestLauncher::RunTestIteration() {
977 MessageLoop::current()->Quit();
981 // Special value "-1" means "repeat indefinitely".
982 cycles_ = (cycles_ == -1) ? cycles_ : cycles_ - 1;
984 test_started_count_ = 0;
985 test_finished_count_ = 0;
986 test_success_count_ = 0;
987 test_broken_count_ = 0;
989 tests_to_retry_.clear();
990 results_tracker_.OnTestIterationStarting();
992 MessageLoop::current()->PostTask(
993 FROM_HERE, Bind(&TestLauncher::RunTests, Unretained(this)));
996 void TestLauncher::MaybeSaveSummaryAsJSON() {
997 const CommandLine* command_line = CommandLine::ForCurrentProcess();
998 if (command_line->HasSwitch(switches::kTestLauncherSummaryOutput)) {
999 FilePath summary_path(command_line->GetSwitchValuePath(
1000 switches::kTestLauncherSummaryOutput));
1001 if (!results_tracker_.SaveSummaryAsJSON(summary_path)) {
1002 LOG(ERROR) << "Failed to save test launcher output summary.";
1007 void TestLauncher::OnLaunchTestProcessFinished(
1008 const LaunchChildGTestProcessCallback& callback,
1010 const TimeDelta& elapsed_time,
1012 const std::string& output) {
1013 DCHECK(thread_checker_.CalledOnValidThread());
1015 callback.Run(exit_code, elapsed_time, was_timeout, output);
1018 void TestLauncher::OnTestIterationFinished() {
1019 TestResultsTracker::TestStatusMap tests_by_status(
1020 results_tracker_.GetTestStatusMapForCurrentIteration());
1021 if (!tests_by_status[TestResult::TEST_UNKNOWN].empty())
1022 results_tracker_.AddGlobalTag(kUnreliableResultsTag);
1024 // When we retry tests, success is determined by having nothing more
1025 // to retry (everything eventually passed), as opposed to having
1026 // no failures at all.
1027 if (tests_to_retry_.empty()) {
1028 fprintf(stdout, "SUCCESS: all tests passed.\n");
1031 // Signal failure, but continue to run all requested test iterations.
1032 // With the summary of all iterations at the end this is a good default.
1033 run_result_ = false;
1036 results_tracker_.PrintSummaryOfCurrentIteration();
1038 // Kick off the next iteration.
1039 MessageLoop::current()->PostTask(
1041 Bind(&TestLauncher::RunTestIteration, Unretained(this)));
1044 void TestLauncher::OnOutputTimeout() {
1045 DCHECK(thread_checker_.CalledOnValidThread());
1047 AutoLock lock(g_live_processes_lock.Get());
1049 fprintf(stdout, "Still waiting for the following processes to finish:\n");
1051 for (std::map<ProcessHandle, CommandLine>::iterator i =
1052 g_live_processes.Get().begin();
1053 i != g_live_processes.Get().end();
1056 fwprintf(stdout, L"\t%s\n", i->second.GetCommandLineString().c_str());
1058 fprintf(stdout, "\t%s\n", i->second.GetCommandLineString().c_str());
1064 // Arm the timer again - otherwise it would fire only once.
1065 watchdog_timer_.Reset();
1068 std::string GetTestOutputSnippet(const TestResult& result,
1069 const std::string& full_output) {
1070 size_t run_pos = full_output.find(std::string("[ RUN ] ") +
1072 if (run_pos == std::string::npos)
1073 return std::string();
1075 size_t end_pos = full_output.find(std::string("[ FAILED ] ") +
1078 // Only clip the snippet to the "OK" message if the test really
1079 // succeeded. It still might have e.g. crashed after printing it.
1080 if (end_pos == std::string::npos &&
1081 result.status == TestResult::TEST_SUCCESS) {
1082 end_pos = full_output.find(std::string("[ OK ] ") +
1086 if (end_pos != std::string::npos) {
1087 size_t newline_pos = full_output.find("\n", end_pos);
1088 if (newline_pos != std::string::npos)
1089 end_pos = newline_pos + 1;
1092 std::string snippet(full_output.substr(run_pos));
1093 if (end_pos != std::string::npos)
1094 snippet = full_output.substr(run_pos, end_pos - run_pos);