Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(16)

Side by Side Diff: base/test/launcher/test_launcher_nacl_nonsfi.cc

Issue 1154313003: Non-SFI mode: Implement test launcher for nacl_helper_nonsfi_unittests (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/test/launcher/test_launcher.h" 5 #include <inttypes.h>
6 #include <stdio.h>
6 7
7 #include "base/at_exit.h" 8 #include <string>
8 #include "base/base_paths.h" 9
9 #include "base/bind.h"
10 #include "base/command_line.h" 10 #include "base/command_line.h"
11 #include "base/files/file_path.h" 11 #include "base/files/file_path.h"
12 #include "base/files/file_util.h" 12 #include "base/files/file_util.h"
13 #include "base/files/scoped_temp_dir.h"
14 #include "base/format_macros.h"
15 #include "base/message_loop/message_loop.h" 13 #include "base/message_loop/message_loop.h"
16 #include "base/path_service.h" 14 #include "base/path_service.h"
17 #include "base/process/launch.h" 15 #include "base/process/launch.h"
16 #include "base/strings/string_piece.h"
17 #include "base/strings/string_tokenizer.h"
18 #include "base/strings/string_util.h" 18 #include "base/strings/string_util.h"
19 #include "base/sys_info.h"
20 #include "base/test/launcher/test_launcher.h"
19 #include "base/test/launcher/unit_test_launcher.h" 21 #include "base/test/launcher/unit_test_launcher.h"
20 #include "base/test/test_switches.h" 22 #include "base/test/test_switches.h"
21 #include "base/test/test_timeouts.h" 23 #include "base/test/test_timeouts.h"
22 24
25 namespace base {
26
23 namespace { 27 namespace {
24 28
25 const char kHelpFlag[] = "help"; 29 const char kHelpFlag[] = "help";
26 30
27 void PrintUsage() { 31 void PrintUsage() {
28 fprintf(stdout, 32 fprintf(stdout,
29 "Runs tests using the gtest framework, each batch of tests being\n" 33 "Runs tests using the gtest framework, each batch of tests being\n"
30 "run in their own process. Supported command-line flags:\n" 34 "run in their own process. Supported command-line flags:\n"
31 "\n" 35 "\n"
32 " Common flags:\n" 36 " Common flags:\n"
(...skipping 15 matching lines...) Expand all
48 " auto means to print it when the test failed.\n" 52 " auto means to print it when the test failed.\n"
49 "\n" 53 "\n"
50 " --test-launcher-total-shards=N\n" 54 " --test-launcher-total-shards=N\n"
51 " Sets the total number of shards to N.\n" 55 " Sets the total number of shards to N.\n"
52 "\n" 56 "\n"
53 " --test-launcher-shard-index=N\n" 57 " --test-launcher-shard-index=N\n"
54 " Sets the shard index to run to N (from 0 to TOTAL - 1).\n"); 58 " Sets the shard index to run to N (from 0 to TOTAL - 1).\n");
55 fflush(stdout); 59 fflush(stdout);
56 } 60 }
57 61
58 class IOSUnitTestPlatformDelegate : public base::UnitTestPlatformDelegate { 62 class NonSfiUnitTestPlatformDelegate : public base::UnitTestPlatformDelegate {
59 public: 63 public:
60 IOSUnitTestPlatformDelegate() { 64 NonSfiUnitTestPlatformDelegate() {
61 } 65 }
62 66
63 bool Init() WARN_UNUSED_RESULT { 67 bool Init(const std::string& test_binary) {
64 if (!PathService::Get(base::DIR_EXE, &dir_exe_)) { 68 base::FilePath dir_exe;
65 LOG(ERROR) << "Failed to get directory of current executable."; 69 if (!PathService::Get(base::DIR_EXE, &dir_exe)) {
70 LOG(ERROR) << "Failed to get directory of the current executable.";
66 return false; 71 return false;
67 } 72 }
68 73
69 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); 74 test_path_ = dir_exe.AppendASCII(test_binary);
70 std::vector<std::string> args(command_line->GetArgs());
71 if (args.size() < 1) {
72 LOG(ERROR) << "Arguments expected.";
73 return false;
74 }
75 test_name_ = args[0];
76
77 base::CommandLine cmd_line(dir_exe_.AppendASCII(test_name_ + ".app"));
78 cmd_line.AppendSwitch(switches::kTestLauncherPrintWritablePath);
79 cmd_line.PrependWrapper(dir_exe_.AppendASCII("iossim").value());
80
81 std::string raw_output;
82 if (!base::GetAppOutput(cmd_line, &raw_output)) {
83 LOG(ERROR) << "GetAppOutput failed.";
84 return false;
85 }
86 writable_path_ = base::FilePath(raw_output);
87
88 return true; 75 return true;
89 } 76 }
90 77
91 bool GetTests(std::vector<base::SplitTestName>* output) override { 78 private:
92 base::ScopedTempDir temp_dir;
93 if (!temp_dir.CreateUniqueTempDirUnderPath(writable_path_))
94 return false;
95 base::FilePath test_list_path(
96 temp_dir.path().AppendASCII("test_list.json"));
97
98 base::CommandLine cmd_line(dir_exe_.AppendASCII(test_name_ + ".app"));
99 cmd_line.AppendSwitchPath(switches::kTestLauncherListTests, test_list_path);
100 cmd_line.PrependWrapper(dir_exe_.AppendASCII("iossim").value());
101
102 base::LaunchOptions launch_options;
103 launch_options.wait = true;
104
105 if (!base::LaunchProcess(cmd_line, launch_options).IsValid())
106 return false;
107
108 return base::ReadTestNamesFromFile(test_list_path, output);
109 }
110
111 bool CreateTemporaryFile(base::FilePath* path) override { 79 bool CreateTemporaryFile(base::FilePath* path) override {
112 if (!CreateTemporaryDirInDir(writable_path_, std::string(), path)) 80 if (!base::CreateNewTempDirectory(base::FilePath::StringType(), path))
113 return false; 81 return false;
114 *path = path->AppendASCII("test_results.xml"); 82 *path = path->AppendASCII("test_results.xml");
115 return true; 83 return true;
116 } 84 }
117 85
86 bool GetTests(std::vector<base::SplitTestName>* output) override {
87 base::FilePath output_file;
88 if (!base::CreateTemporaryFile(&output_file)) {
89 LOG(ERROR) << "Failed to create a temp file.";
90 return false;
91 }
92
93 base::CommandLine cmd_line(test_path_);
94 cmd_line.AppendSwitchPath(switches::kTestLauncherListTests, output_file);
95
96 base::Process subprocess =
97 base::LaunchProcess(cmd_line, base::LaunchOptions());
98 if (!subprocess.WaitForExit(nullptr)) {
Paweł Hajdan Jr. 2015/06/02 15:41:38 Why not use LaunchOptions.wait = true instead? It
hidehiko 2015/06/02 18:34:01 Oh, I misunderstood your intention. I referred tes
99 LOG(ERROR) << "Failed to run test with --"
Paweł Hajdan Jr. 2015/06/02 15:41:38 nit: No need for this log statement, please remove
hidehiko 2015/06/02 18:34:01 Done.
100 << switches::kTestLauncherListTests;
101 return false;
102 }
103
104 return base::ReadTestNamesFromFile(output_file, output);
105 }
106
107 std::string GetWrapperForChildGTestProcess() override {
108 return std::string();
109 }
110
118 base::CommandLine GetCommandLineForChildGTestProcess( 111 base::CommandLine GetCommandLineForChildGTestProcess(
119 const std::vector<std::string>& test_names, 112 const std::vector<std::string>& test_names,
120 const base::FilePath& output_file) override { 113 const base::FilePath& output_file) override {
121 base::CommandLine cmd_line(dir_exe_.AppendASCII(test_name_ + ".app")); 114 base::CommandLine cmd_line(test_path_);
122 cmd_line.AppendSwitchPath(switches::kTestLauncherOutput, output_file); 115 cmd_line.AppendSwitchPath(
123 cmd_line.AppendSwitchASCII(base::kGTestFilterFlag, 116 switches::kTestLauncherOutput, output_file);
124 JoinString(test_names, ":")); 117 cmd_line.AppendSwitchASCII(
118 base::kGTestFilterFlag, JoinString(test_names, ":"));
125 return cmd_line; 119 return cmd_line;
126 } 120 }
127 121
128 std::string GetWrapperForChildGTestProcess() override {
129 return dir_exe_.AppendASCII("iossim").value();
130 }
131
132 void RelaunchTests(base::TestLauncher* test_launcher, 122 void RelaunchTests(base::TestLauncher* test_launcher,
133 const std::vector<std::string>& test_names, 123 const std::vector<std::string>& test_names,
134 int launch_flags) override { 124 int launch_flags) override {
135 // Relaunch all tests in one big batch, since overhead of smaller batches
136 // is too big for serialized runs inside ios simulator.
137 RunUnitTestsBatch(test_launcher, this, test_names, launch_flags); 125 RunUnitTestsBatch(test_launcher, this, test_names, launch_flags);
138 } 126 }
139 127
140 private: 128 base::FilePath test_path_;
141 // Directory containing test launcher's executable.
142 base::FilePath dir_exe_;
143
144 // Name of the test executable to run.
145 std::string test_name_;
146
147 // Path that launched test binary can write to.
148 base::FilePath writable_path_;
149
150 DISALLOW_COPY_AND_ASSIGN(IOSUnitTestPlatformDelegate);
151 }; 129 };
152 130
153 } // namespace 131 } // namespace
154 132
155 int main(int argc, char** argv) { 133 int TestLauncherNonSfiMain(const std::string& test_binary) {
156 base::AtExitManager at_exit;
157
158 base::CommandLine::Init(argc, argv);
159
160 if (base::CommandLine::ForCurrentProcess()->HasSwitch(kHelpFlag)) { 134 if (base::CommandLine::ForCurrentProcess()->HasSwitch(kHelpFlag)) {
161 PrintUsage(); 135 PrintUsage();
162 return 0; 136 return 0;
163 } 137 }
164 138
165 base::TimeTicks start_time(base::TimeTicks::Now()); 139 base::TimeTicks start_time(base::TimeTicks::Now());
166 140
167 TestTimeouts::Initialize(); 141 TestTimeouts::Initialize();
168 142
169 base::MessageLoopForIO message_loop; 143 base::MessageLoopForIO message_loop;
170 144
171 IOSUnitTestPlatformDelegate platform_delegate; 145 NonSfiUnitTestPlatformDelegate platform_delegate;
172 if (!platform_delegate.Init()) { 146 if (!platform_delegate.Init(test_binary)) {
173 fprintf(stderr, "Failed to intialize test launcher platform delegate.\n"); 147 fprintf(stderr, "Failed to initialize test launcher.\n");
174 fflush(stderr); 148 fflush(stderr);
175 return 1; 149 return 1;
176 } 150 }
177 base::UnitTestLauncherDelegate delegate(&platform_delegate, 0, false); 151
178 // Force one job since we can't run multiple simulators in parallel. 152 base::UnitTestLauncherDelegate delegate(&platform_delegate, 10, true);
179 base::TestLauncher launcher(&delegate, 1); 153 base::TestLauncher launcher(&delegate, base::SysInfo::NumberOfProcessors());
180 bool success = launcher.Run(); 154 bool success = launcher.Run();
181 155
182 fprintf(stdout, "Tests took %" PRId64 " seconds.\n", 156 fprintf(stdout, "Tests took %" PRId64 " seconds.\n",
183 (base::TimeTicks::Now() - start_time).InSeconds()); 157 (base::TimeTicks::Now() - start_time).InSeconds());
184 fflush(stdout); 158 fflush(stdout);
159 return success ? 0 : 1;
160 }
185 161
186 return (success ? 0 : 1); 162 } // namespace base
187 }
OLDNEW
« no previous file with comments | « base/test/launcher/test_launcher_nacl_nonsfi.h ('k') | base/test/launcher/unit_test_launcher_nacl_nonsfi.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698