3 # Copyright (C) 2011, 2012, 2017 Igalia S.L.
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Library General Public
7 # License as published by the Free Software Foundation; either
8 # version 2 of the License, or (at your option) any later version.
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Library General Public License for more details.
15 # You should have received a copy of the GNU Library General Public License
16 # along with this library; see the file COPYING.LIB. If not, write to
17 # the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 # Boston, MA 02110-1301, USA.
25 from signal import SIGKILL, SIGSEGV
26 from glib_test_runner import GLibTestRunner
28 top_level_directory = os.path.normpath(os.path.join(os.path.dirname(__file__), "..", ".."))
29 sys.path.insert(0, os.path.join(top_level_directory, "Tools", "glib"))
31 from webkitpy.common.host import Host
32 from webkitpy.common.test_expectations import TestExpectations
33 from webkitpy.common.timeout_context import Timeout
36 class TestRunner(object):
39 def __init__(self, port, options, tests=[]):
40 self._options = options
42 self._build_type = "Debug" if self._options.debug else "Release"
43 common.set_build_types((self._build_type,))
44 self._port = Host().port_factory.get(port)
45 self._driver = self._create_driver()
47 self._programs_path = common.binary_build_path()
48 expectations_file = os.path.join(common.top_level_path(), "Tools", "TestWebKitAPI", "glib", "TestExpectations.json")
49 self._expectations = TestExpectations(self._port.name(), expectations_file, self._build_type)
50 self._tests = self._get_tests(tests)
51 self._disabled_tests = []
53 def _test_programs_base_dir(self):
54 return os.path.join(self._programs_path, "TestWebKitAPI")
56 def _get_tests_from_dir(self, test_dir):
57 if not os.path.isdir(test_dir):
61 for test_file in os.listdir(test_dir):
62 if not test_file.lower().startswith("test"):
64 test_path = os.path.join(test_dir, test_file)
65 if os.path.isfile(test_path) and os.access(test_path, os.X_OK):
66 tests.append(test_path)
69 def _get_tests(self, initial_tests):
71 for test in initial_tests:
72 if os.path.isdir(test):
73 tests.extend(self._get_tests_from_dir(test))
80 for test_dir in self.TEST_DIRS:
81 absolute_test_dir = os.path.join(self._test_programs_base_dir(), test_dir)
82 tests.extend(self._get_tests_from_dir(absolute_test_dir))
85 def _create_driver(self, port_options=[]):
86 self._port._display_server = self._options.display_server
87 driver = self._port.create_driver(worker_number=0, no_timeout=True)._make_driver(pixel_tests=False)
88 if not driver.check_driver(self._port):
89 raise RuntimeError("Failed to check driver %s" % driver.__class__.__name__)
92 def _setup_testing_environment(self):
93 self._test_env = self._driver._setup_environ_for_test()
94 self._test_env["TEST_WEBKIT_API_WEBKIT2_RESOURCES_PATH"] = common.top_level_path("Tools", "TestWebKitAPI", "Tests", "WebKit")
95 self._test_env["TEST_WEBKIT_API_WEBKIT2_INJECTED_BUNDLE_PATH"] = common.library_build_path()
96 self._test_env["WEBKIT_EXEC_PATH"] = self._programs_path
100 def _tear_down_testing_environment(self):
104 def _test_cases_to_skip(self, test_program):
105 if self._options.skipped_action != 'skip':
108 return self._expectations.skipped_subtests(os.path.basename(test_program))
110 def _should_run_test_program(self, test_program):
111 for disabled_test in self._disabled_tests:
112 if test_program.endswith(disabled_test):
115 if self._options.skipped_action != 'skip':
118 return os.path.basename(test_program) not in self._expectations.skipped_tests()
120 def _kill_process(self, pid):
122 os.kill(pid, SIGKILL)
124 # Process already died.
127 def _waitpid(self, pid):
130 dummy, status = os.waitpid(pid, 0)
131 if os.WIFSIGNALED(status):
132 return -os.WTERMSIG(status)
133 if os.WIFEXITED(status):
134 return os.WEXITSTATUS(status)
136 # Should never happen
137 raise RuntimeError("Unknown child exit status!")
138 except (OSError, IOError) as e:
139 if e.errno == errno.EINTR:
141 if e.errno == errno.ECHILD:
142 # This happens if SIGCLD is set to be ignored or waiting
143 # for child processes has otherwise been disabled for our
144 # process. This child is dead, we can't get the status.
148 def _run_test_glib(self, test_program):
149 timeout = self._options.timeout
150 test = os.path.join(os.path.basename(os.path.dirname(test_program)), os.path.basename(test_program))
151 if self._expectations.is_slow(os.path.basename(test_program)):
153 return GLibTestRunner(test_program, timeout).run(skipped=self._test_cases_to_skip(test_program), env=self._test_env)
155 def _get_tests_from_google_test_suite(self, test_program):
157 output = subprocess.check_output([test_program, '--gtest_list_tests'], env=self._test_env)
158 except subprocess.CalledProcessError:
159 sys.stderr.write("ERROR: could not list available tests for binary %s.\n" % (test_program))
163 skipped_test_cases = self._test_cases_to_skip(test_program)
167 for line in output.split('\n'):
168 if not line.startswith(' '):
172 test_name = prefix + line.strip()
173 if not test_name in skipped_test_cases:
174 tests.append(test_name)
177 def _run_google_test(self, test_program, subtest):
178 command = [test_program, '--gtest_filter=%s' % (subtest)]
179 timeout = self._options.timeout
180 if self._expectations.is_slow(os.path.basename(test_program), subtest):
183 pid, fd = os.forkpty()
185 os.execvpe(command[0], command, self._test_env)
188 with Timeout(timeout):
190 common.parse_output_lines(fd, sys.stdout.write)
191 status = self._waitpid(pid)
193 self._kill_process(pid)
194 sys.stdout.write("**TIMEOUT** %s\n" % subtest)
196 return {subtest: "TIMEOUT"}
198 if status == -SIGSEGV:
199 sys.stdout.write("**CRASH** %s\n" % subtest)
201 return {subtest: "CRASH"}
204 return {subtest: "FAIL"}
206 return {subtest: "PASS"}
208 def _run_google_test_suite(self, test_program):
210 for subtest in self._get_tests_from_google_test_suite(test_program):
211 result.update(self._run_google_test(test_program, subtest))
214 def is_glib_test(self, test_program):
215 raise NotImplementedError
217 def is_google_test(self, test_program):
218 raise NotImplementedError
220 def _run_test(self, test_program):
221 if self.is_glib_test(test_program):
222 return self._run_test_glib(test_program)
224 if self.is_google_test(test_program):
225 return self._run_google_test_suite(test_program)
231 sys.stderr.write("ERROR: tests not found in %s.\n" % (self._test_programs_base_dir()))
235 if not self._setup_testing_environment():
238 # Remove skipped tests now instead of when we find them, because
239 # some tests might be skipped while setting up the test environment.
240 self._tests = [test for test in self._tests if self._should_run_test_program(test)]
247 for test in self._tests:
248 results = self._run_test(test)
249 for test_case, result in results.iteritems():
250 if result in self._expectations.get_expectation(os.path.basename(test), test_case):
254 failed_tests.setdefault(test, []).append(test_case)
255 elif result == "TIMEOUT":
256 timed_out_tests.setdefault(test, []).append(test_case)
257 elif result == "CRASH":
258 crashed_tests.setdefault(test, []).append(test_case)
259 elif result == "PASS":
260 passed_tests.setdefault(test, []).append(test_case)
262 self._tear_down_testing_environment()
264 def report(tests, title, base_dir):
267 sys.stdout.write("\nUnexpected %s (%d)\n" % (title, sum(len(value) for value in tests.itervalues())))
269 sys.stdout.write(" %s\n" % (test.replace(base_dir, '', 1)))
270 for test_case in tests[test]:
271 sys.stdout.write(" %s\n" % (test_case))
274 report(failed_tests, "failures", self._test_programs_base_dir())
275 report(crashed_tests, "crashes", self._test_programs_base_dir())
276 report(timed_out_tests, "timeouts", self._test_programs_base_dir())
277 report(passed_tests, "passes", self._test_programs_base_dir())
279 return len(failed_tests) + len(timed_out_tests)
282 def add_options(option_parser):
283 option_parser.add_option('-r', '--release',
284 action='store_true', dest='release',
285 help='Run in Release')
286 option_parser.add_option('-d', '--debug',
287 action='store_true', dest='debug',
289 option_parser.add_option('--skipped', action='store', dest='skipped_action',
290 choices=['skip', 'ignore', 'only'], default='skip',
291 metavar='skip|ignore|only',
292 help='Specifies how to treat the skipped tests')
293 option_parser.add_option('-t', '--timeout',
294 action='store', type='int', dest='timeout', default=5,
295 help='Time in seconds until a test times out')