1*635a8641SAndroid Build Coastguard Worker // Copyright 2013 The Chromium Authors. All rights reserved. 2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be 3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file. 4*635a8641SAndroid Build Coastguard Worker 5*635a8641SAndroid Build Coastguard Worker #ifndef BASE_TEST_LAUNCHER_TEST_LAUNCHER_H_ 6*635a8641SAndroid Build Coastguard Worker #define BASE_TEST_LAUNCHER_TEST_LAUNCHER_H_ 7*635a8641SAndroid Build Coastguard Worker 8*635a8641SAndroid Build Coastguard Worker #include <stddef.h> 9*635a8641SAndroid Build Coastguard Worker #include <stdint.h> 10*635a8641SAndroid Build Coastguard Worker 11*635a8641SAndroid Build Coastguard Worker #include <memory> 12*635a8641SAndroid Build Coastguard Worker #include <set> 13*635a8641SAndroid Build Coastguard Worker #include <string> 14*635a8641SAndroid Build Coastguard Worker #include <vector> 15*635a8641SAndroid Build Coastguard Worker 16*635a8641SAndroid Build Coastguard Worker #include "base/compiler_specific.h" 17*635a8641SAndroid Build Coastguard Worker #include "base/macros.h" 18*635a8641SAndroid Build Coastguard Worker #include "base/process/launch.h" 19*635a8641SAndroid Build Coastguard Worker #include "base/test/gtest_util.h" 20*635a8641SAndroid Build Coastguard Worker #include "base/test/launcher/test_result.h" 21*635a8641SAndroid Build Coastguard Worker #include "base/test/launcher/test_results_tracker.h" 22*635a8641SAndroid Build Coastguard Worker #include "base/threading/thread_checker.h" 23*635a8641SAndroid Build Coastguard Worker #include "base/time/time.h" 24*635a8641SAndroid Build Coastguard Worker #include "base/timer/timer.h" 25*635a8641SAndroid Build Coastguard Worker #include "build/build_config.h" 26*635a8641SAndroid Build Coastguard Worker 27*635a8641SAndroid Build Coastguard Worker namespace base { 28*635a8641SAndroid Build Coastguard Worker 29*635a8641SAndroid Build Coastguard Worker class CommandLine; 30*635a8641SAndroid Build Coastguard Worker struct LaunchOptions; 31*635a8641SAndroid Build Coastguard Worker class TestLauncher; 32*635a8641SAndroid Build Coastguard Worker 33*635a8641SAndroid Build Coastguard Worker // Constants for GTest command-line flags. 34*635a8641SAndroid Build Coastguard Worker extern const char kGTestFilterFlag[]; 35*635a8641SAndroid Build Coastguard Worker extern const char kGTestFlagfileFlag[]; 36*635a8641SAndroid Build Coastguard Worker extern const char kGTestHelpFlag[]; 37*635a8641SAndroid Build Coastguard Worker extern const char kGTestListTestsFlag[]; 38*635a8641SAndroid Build Coastguard Worker extern const char kGTestRepeatFlag[]; 39*635a8641SAndroid Build Coastguard Worker extern const char kGTestRunDisabledTestsFlag[]; 40*635a8641SAndroid Build Coastguard Worker extern const char kGTestOutputFlag[]; 41*635a8641SAndroid Build Coastguard Worker extern const char kGTestShuffleFlag[]; 42*635a8641SAndroid Build Coastguard Worker extern const char kGTestRandomSeedFlag[]; 43*635a8641SAndroid Build Coastguard Worker 44*635a8641SAndroid Build Coastguard Worker // Interface for use with LaunchTests that abstracts away exact details 45*635a8641SAndroid Build Coastguard Worker // which tests and how are run. 46*635a8641SAndroid Build Coastguard Worker class TestLauncherDelegate { 47*635a8641SAndroid Build Coastguard Worker public: 48*635a8641SAndroid Build Coastguard Worker // Called to get names of tests available for running. The delegate 49*635a8641SAndroid Build Coastguard Worker // must put the result in |output| and return true on success. 50*635a8641SAndroid Build Coastguard Worker virtual bool GetTests(std::vector<TestIdentifier>* output) = 0; 51*635a8641SAndroid Build Coastguard Worker 52*635a8641SAndroid Build Coastguard Worker // Called before a test is considered for running. If it returns false, 53*635a8641SAndroid Build Coastguard Worker // the test is not run. If it returns true, the test will be run provided 54*635a8641SAndroid Build Coastguard Worker // it is part of the current shard. 55*635a8641SAndroid Build Coastguard Worker virtual bool ShouldRunTest(const std::string& test_case_name, 56*635a8641SAndroid Build Coastguard Worker const std::string& test_name) = 0; 57*635a8641SAndroid Build Coastguard Worker 58*635a8641SAndroid Build Coastguard Worker // Called to make the delegate run the specified tests. The delegate must 59*635a8641SAndroid Build Coastguard Worker // return the number of actual tests it's going to run (can be smaller, 60*635a8641SAndroid Build Coastguard Worker // equal to, or larger than size of |test_names|). It must also call 61*635a8641SAndroid Build Coastguard Worker // |test_launcher|'s OnTestFinished method once per every run test, 62*635a8641SAndroid Build Coastguard Worker // regardless of its success. 63*635a8641SAndroid Build Coastguard Worker virtual size_t RunTests(TestLauncher* test_launcher, 64*635a8641SAndroid Build Coastguard Worker const std::vector<std::string>& test_names) = 0; 65*635a8641SAndroid Build Coastguard Worker 66*635a8641SAndroid Build Coastguard Worker // Called to make the delegate retry the specified tests. The delegate must 67*635a8641SAndroid Build Coastguard Worker // return the number of actual tests it's going to retry (can be smaller, 68*635a8641SAndroid Build Coastguard Worker // equal to, or larger than size of |test_names|). It must also call 69*635a8641SAndroid Build Coastguard Worker // |test_launcher|'s OnTestFinished method once per every retried test, 70*635a8641SAndroid Build Coastguard Worker // regardless of its success. 71*635a8641SAndroid Build Coastguard Worker virtual size_t RetryTests(TestLauncher* test_launcher, 72*635a8641SAndroid Build Coastguard Worker const std::vector<std::string>& test_names) = 0; 73*635a8641SAndroid Build Coastguard Worker 74*635a8641SAndroid Build Coastguard Worker protected: 75*635a8641SAndroid Build Coastguard Worker virtual ~TestLauncherDelegate(); 76*635a8641SAndroid Build Coastguard Worker }; 77*635a8641SAndroid Build Coastguard Worker 78*635a8641SAndroid Build Coastguard Worker // An observer of child process lifetime events generated by 79*635a8641SAndroid Build Coastguard Worker // LaunchChildGTestProcess. 80*635a8641SAndroid Build Coastguard Worker class ProcessLifetimeObserver { 81*635a8641SAndroid Build Coastguard Worker public: 82*635a8641SAndroid Build Coastguard Worker virtual ~ProcessLifetimeObserver() = default; 83*635a8641SAndroid Build Coastguard Worker 84*635a8641SAndroid Build Coastguard Worker // Invoked once the child process is started. |handle| is a handle to the 85*635a8641SAndroid Build Coastguard Worker // child process and |id| is its pid. NOTE: this method is invoked on the 86*635a8641SAndroid Build Coastguard Worker // thread the process is launched on immediately after it is launched. The 87*635a8641SAndroid Build Coastguard Worker // caller owns the ProcessHandle. OnLaunched(ProcessHandle handle,ProcessId id)88*635a8641SAndroid Build Coastguard Worker virtual void OnLaunched(ProcessHandle handle, ProcessId id) {} 89*635a8641SAndroid Build Coastguard Worker 90*635a8641SAndroid Build Coastguard Worker // Invoked when a test process exceeds its runtime, immediately before it is 91*635a8641SAndroid Build Coastguard Worker // terminated. |command_line| is the command line used to launch the process. 92*635a8641SAndroid Build Coastguard Worker // NOTE: this method is invoked on the thread the process is launched on. OnTimedOut(const CommandLine & command_line)93*635a8641SAndroid Build Coastguard Worker virtual void OnTimedOut(const CommandLine& command_line) {} 94*635a8641SAndroid Build Coastguard Worker 95*635a8641SAndroid Build Coastguard Worker // Invoked after a child process finishes, reporting the process |exit_code|, 96*635a8641SAndroid Build Coastguard Worker // child process |elapsed_time|, whether or not the process was terminated as 97*635a8641SAndroid Build Coastguard Worker // a result of a timeout, and the output of the child (stdout and stderr 98*635a8641SAndroid Build Coastguard Worker // together). NOTE: this method is invoked on the same thread as 99*635a8641SAndroid Build Coastguard Worker // LaunchChildGTestProcess. OnCompleted(int exit_code,TimeDelta elapsed_time,bool was_timeout,const std::string & output)100*635a8641SAndroid Build Coastguard Worker virtual void OnCompleted(int exit_code, 101*635a8641SAndroid Build Coastguard Worker TimeDelta elapsed_time, 102*635a8641SAndroid Build Coastguard Worker bool was_timeout, 103*635a8641SAndroid Build Coastguard Worker const std::string& output) {} 104*635a8641SAndroid Build Coastguard Worker 105*635a8641SAndroid Build Coastguard Worker protected: 106*635a8641SAndroid Build Coastguard Worker ProcessLifetimeObserver() = default; 107*635a8641SAndroid Build Coastguard Worker 108*635a8641SAndroid Build Coastguard Worker private: 109*635a8641SAndroid Build Coastguard Worker DISALLOW_COPY_AND_ASSIGN(ProcessLifetimeObserver); 110*635a8641SAndroid Build Coastguard Worker }; 111*635a8641SAndroid Build Coastguard Worker 112*635a8641SAndroid Build Coastguard Worker // Launches tests using a TestLauncherDelegate. 113*635a8641SAndroid Build Coastguard Worker class TestLauncher { 114*635a8641SAndroid Build Coastguard Worker public: 115*635a8641SAndroid Build Coastguard Worker // Flags controlling behavior of LaunchChildGTestProcess. 116*635a8641SAndroid Build Coastguard Worker enum LaunchChildGTestProcessFlags { 117*635a8641SAndroid Build Coastguard Worker // Allows usage of job objects on Windows. Helps properly clean up child 118*635a8641SAndroid Build Coastguard Worker // processes. 119*635a8641SAndroid Build Coastguard Worker USE_JOB_OBJECTS = (1 << 0), 120*635a8641SAndroid Build Coastguard Worker 121*635a8641SAndroid Build Coastguard Worker // Allows breakaway from job on Windows. May result in some child processes 122*635a8641SAndroid Build Coastguard Worker // not being properly terminated after launcher dies if these processes 123*635a8641SAndroid Build Coastguard Worker // fail to cooperate. 124*635a8641SAndroid Build Coastguard Worker ALLOW_BREAKAWAY_FROM_JOB = (1 << 1), 125*635a8641SAndroid Build Coastguard Worker }; 126*635a8641SAndroid Build Coastguard Worker 127*635a8641SAndroid Build Coastguard Worker struct LaunchOptions { 128*635a8641SAndroid Build Coastguard Worker LaunchOptions(); 129*635a8641SAndroid Build Coastguard Worker LaunchOptions(const LaunchOptions& other); 130*635a8641SAndroid Build Coastguard Worker ~LaunchOptions(); 131*635a8641SAndroid Build Coastguard Worker 132*635a8641SAndroid Build Coastguard Worker int flags = 0; 133*635a8641SAndroid Build Coastguard Worker // These mirror values in base::LaunchOptions, see it for details. 134*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN) 135*635a8641SAndroid Build Coastguard Worker base::LaunchOptions::Inherit inherit_mode = 136*635a8641SAndroid Build Coastguard Worker base::LaunchOptions::Inherit::kSpecific; 137*635a8641SAndroid Build Coastguard Worker base::HandlesToInheritVector handles_to_inherit; 138*635a8641SAndroid Build Coastguard Worker #else 139*635a8641SAndroid Build Coastguard Worker FileHandleMappingVector fds_to_remap; 140*635a8641SAndroid Build Coastguard Worker #endif 141*635a8641SAndroid Build Coastguard Worker }; 142*635a8641SAndroid Build Coastguard Worker 143*635a8641SAndroid Build Coastguard Worker // Constructor. |parallel_jobs| is the limit of simultaneous parallel test 144*635a8641SAndroid Build Coastguard Worker // jobs. 145*635a8641SAndroid Build Coastguard Worker TestLauncher(TestLauncherDelegate* launcher_delegate, size_t parallel_jobs); 146*635a8641SAndroid Build Coastguard Worker ~TestLauncher(); 147*635a8641SAndroid Build Coastguard Worker 148*635a8641SAndroid Build Coastguard Worker // Runs the launcher. Must be called at most once. 149*635a8641SAndroid Build Coastguard Worker bool Run() WARN_UNUSED_RESULT; 150*635a8641SAndroid Build Coastguard Worker 151*635a8641SAndroid Build Coastguard Worker // Launches a child process (assumed to be gtest-based binary) using 152*635a8641SAndroid Build Coastguard Worker // |command_line|. If |wrapper| is not empty, it is prepended to the final 153*635a8641SAndroid Build Coastguard Worker // command line. |observer|, if not null, is used to convey process lifetime 154*635a8641SAndroid Build Coastguard Worker // events to the caller. |observer| is destroyed after its OnCompleted 155*635a8641SAndroid Build Coastguard Worker // method is invoked. 156*635a8641SAndroid Build Coastguard Worker void LaunchChildGTestProcess( 157*635a8641SAndroid Build Coastguard Worker const CommandLine& command_line, 158*635a8641SAndroid Build Coastguard Worker const std::string& wrapper, 159*635a8641SAndroid Build Coastguard Worker TimeDelta timeout, 160*635a8641SAndroid Build Coastguard Worker const LaunchOptions& options, 161*635a8641SAndroid Build Coastguard Worker std::unique_ptr<ProcessLifetimeObserver> observer); 162*635a8641SAndroid Build Coastguard Worker 163*635a8641SAndroid Build Coastguard Worker // Called when a test has finished running. 164*635a8641SAndroid Build Coastguard Worker void OnTestFinished(const TestResult& result); 165*635a8641SAndroid Build Coastguard Worker 166*635a8641SAndroid Build Coastguard Worker private: 167*635a8641SAndroid Build Coastguard Worker bool Init() WARN_UNUSED_RESULT; 168*635a8641SAndroid Build Coastguard Worker 169*635a8641SAndroid Build Coastguard Worker // Runs all tests in current iteration. 170*635a8641SAndroid Build Coastguard Worker void RunTests(); 171*635a8641SAndroid Build Coastguard Worker 172*635a8641SAndroid Build Coastguard Worker void CombinePositiveTestFilters(std::vector<std::string> filter_a, 173*635a8641SAndroid Build Coastguard Worker std::vector<std::string> filter_b); 174*635a8641SAndroid Build Coastguard Worker 175*635a8641SAndroid Build Coastguard Worker void RunTestIteration(); 176*635a8641SAndroid Build Coastguard Worker 177*635a8641SAndroid Build Coastguard Worker #if defined(OS_POSIX) 178*635a8641SAndroid Build Coastguard Worker void OnShutdownPipeReadable(); 179*635a8641SAndroid Build Coastguard Worker #endif 180*635a8641SAndroid Build Coastguard Worker 181*635a8641SAndroid Build Coastguard Worker // Saves test results summary as JSON if requested from command line. 182*635a8641SAndroid Build Coastguard Worker void MaybeSaveSummaryAsJSON(const std::vector<std::string>& additional_tags); 183*635a8641SAndroid Build Coastguard Worker 184*635a8641SAndroid Build Coastguard Worker // Called when a test iteration is finished. 185*635a8641SAndroid Build Coastguard Worker void OnTestIterationFinished(); 186*635a8641SAndroid Build Coastguard Worker 187*635a8641SAndroid Build Coastguard Worker // Called by the delay timer when no output was made for a while. 188*635a8641SAndroid Build Coastguard Worker void OnOutputTimeout(); 189*635a8641SAndroid Build Coastguard Worker 190*635a8641SAndroid Build Coastguard Worker // Make sure we don't accidentally call the wrong methods e.g. on the worker 191*635a8641SAndroid Build Coastguard Worker // pool thread. Should be the first member so that it's destroyed last: when 192*635a8641SAndroid Build Coastguard Worker // destroying other members, especially the worker pool, we may check the code 193*635a8641SAndroid Build Coastguard Worker // is running on the correct thread. 194*635a8641SAndroid Build Coastguard Worker ThreadChecker thread_checker_; 195*635a8641SAndroid Build Coastguard Worker 196*635a8641SAndroid Build Coastguard Worker TestLauncherDelegate* launcher_delegate_; 197*635a8641SAndroid Build Coastguard Worker 198*635a8641SAndroid Build Coastguard Worker // Support for outer sharding, just like gtest does. 199*635a8641SAndroid Build Coastguard Worker int32_t total_shards_; // Total number of outer shards, at least one. 200*635a8641SAndroid Build Coastguard Worker int32_t shard_index_; // Index of shard the launcher is to run. 201*635a8641SAndroid Build Coastguard Worker 202*635a8641SAndroid Build Coastguard Worker int cycles_; // Number of remaining test iterations, or -1 for infinite. 203*635a8641SAndroid Build Coastguard Worker 204*635a8641SAndroid Build Coastguard Worker // Test filters (empty means no filter). 205*635a8641SAndroid Build Coastguard Worker bool has_at_least_one_positive_filter_; 206*635a8641SAndroid Build Coastguard Worker std::vector<std::string> positive_test_filter_; 207*635a8641SAndroid Build Coastguard Worker std::vector<std::string> negative_test_filter_; 208*635a8641SAndroid Build Coastguard Worker 209*635a8641SAndroid Build Coastguard Worker // Tests to use (cached result of TestLauncherDelegate::GetTests). 210*635a8641SAndroid Build Coastguard Worker std::vector<TestIdentifier> tests_; 211*635a8641SAndroid Build Coastguard Worker 212*635a8641SAndroid Build Coastguard Worker // Number of tests found in this binary. 213*635a8641SAndroid Build Coastguard Worker size_t test_found_count_; 214*635a8641SAndroid Build Coastguard Worker 215*635a8641SAndroid Build Coastguard Worker // Number of tests started in this iteration. 216*635a8641SAndroid Build Coastguard Worker size_t test_started_count_; 217*635a8641SAndroid Build Coastguard Worker 218*635a8641SAndroid Build Coastguard Worker // Number of tests finished in this iteration. 219*635a8641SAndroid Build Coastguard Worker size_t test_finished_count_; 220*635a8641SAndroid Build Coastguard Worker 221*635a8641SAndroid Build Coastguard Worker // Number of tests successfully finished in this iteration. 222*635a8641SAndroid Build Coastguard Worker size_t test_success_count_; 223*635a8641SAndroid Build Coastguard Worker 224*635a8641SAndroid Build Coastguard Worker // Number of tests either timing out or having an unknown result, 225*635a8641SAndroid Build Coastguard Worker // likely indicating a more systemic problem if widespread. 226*635a8641SAndroid Build Coastguard Worker size_t test_broken_count_; 227*635a8641SAndroid Build Coastguard Worker 228*635a8641SAndroid Build Coastguard Worker // Number of retries in this iteration. 229*635a8641SAndroid Build Coastguard Worker size_t retry_count_; 230*635a8641SAndroid Build Coastguard Worker 231*635a8641SAndroid Build Coastguard Worker // Maximum number of retries per iteration. 232*635a8641SAndroid Build Coastguard Worker size_t retry_limit_; 233*635a8641SAndroid Build Coastguard Worker 234*635a8641SAndroid Build Coastguard Worker // If true will not early exit nor skip retries even if too many tests are 235*635a8641SAndroid Build Coastguard Worker // broken. 236*635a8641SAndroid Build Coastguard Worker bool force_run_broken_tests_; 237*635a8641SAndroid Build Coastguard Worker 238*635a8641SAndroid Build Coastguard Worker // Tests to retry in this iteration. 239*635a8641SAndroid Build Coastguard Worker std::set<std::string> tests_to_retry_; 240*635a8641SAndroid Build Coastguard Worker 241*635a8641SAndroid Build Coastguard Worker // Result to be returned from Run. 242*635a8641SAndroid Build Coastguard Worker bool run_result_; 243*635a8641SAndroid Build Coastguard Worker 244*635a8641SAndroid Build Coastguard Worker // Support for test shuffling, just like gtest does. 245*635a8641SAndroid Build Coastguard Worker bool shuffle_; 246*635a8641SAndroid Build Coastguard Worker uint32_t shuffle_seed_; 247*635a8641SAndroid Build Coastguard Worker 248*635a8641SAndroid Build Coastguard Worker TestResultsTracker results_tracker_; 249*635a8641SAndroid Build Coastguard Worker 250*635a8641SAndroid Build Coastguard Worker // Watchdog timer to make sure we do not go without output for too long. 251*635a8641SAndroid Build Coastguard Worker DelayTimer watchdog_timer_; 252*635a8641SAndroid Build Coastguard Worker 253*635a8641SAndroid Build Coastguard Worker // Number of jobs to run in parallel. 254*635a8641SAndroid Build Coastguard Worker size_t parallel_jobs_; 255*635a8641SAndroid Build Coastguard Worker 256*635a8641SAndroid Build Coastguard Worker DISALLOW_COPY_AND_ASSIGN(TestLauncher); 257*635a8641SAndroid Build Coastguard Worker }; 258*635a8641SAndroid Build Coastguard Worker 259*635a8641SAndroid Build Coastguard Worker // Return the number of parallel jobs to use, or 0U in case of error. 260*635a8641SAndroid Build Coastguard Worker size_t NumParallelJobs(); 261*635a8641SAndroid Build Coastguard Worker 262*635a8641SAndroid Build Coastguard Worker // Extract part from |full_output| that applies to |result|. 263*635a8641SAndroid Build Coastguard Worker std::string GetTestOutputSnippet(const TestResult& result, 264*635a8641SAndroid Build Coastguard Worker const std::string& full_output); 265*635a8641SAndroid Build Coastguard Worker 266*635a8641SAndroid Build Coastguard Worker } // namespace base 267*635a8641SAndroid Build Coastguard Worker 268*635a8641SAndroid Build Coastguard Worker #endif // BASE_TEST_LAUNCHER_TEST_LAUNCHER_H_ 269