manager.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. # Copyright (C) 2010 Google Inc. All rights reserved.
  2. # Copyright (C) 2010 Gabor Rapcsanyi (rgabor@inf.u-szeged.hu), University of Szeged
  3. #
  4. # Redistribution and use in source and binary forms, with or without
  5. # modification, are permitted provided that the following conditions are
  6. # met:
  7. #
  8. # * Redistributions of source code must retain the above copyright
  9. # notice, this list of conditions and the following disclaimer.
  10. # * Redistributions in binary form must reproduce the above
  11. # copyright notice, this list of conditions and the following disclaimer
  12. # in the documentation and/or other materials provided with the
  13. # distribution.
  14. # * Neither the name of Google Inc. nor the names of its
  15. # contributors may be used to endorse or promote products derived from
  16. # this software without specific prior written permission.
  17. #
  18. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. """
  30. The Manager runs a series of tests (TestType interface) against a set
  31. of test files. If a test file fails a TestType, it returns a list of TestFailure
  32. objects to the Manager. The Manager then aggregates the TestFailures to
  33. create a final report.
  34. """
  35. import json
  36. import logging
  37. import random
  38. import sys
  39. import time
  40. from webkitpy.layout_tests.controllers.layout_test_finder import LayoutTestFinder
  41. from webkitpy.layout_tests.controllers.layout_test_runner import LayoutTestRunner
  42. from webkitpy.layout_tests.controllers.test_result_writer import TestResultWriter
  43. from webkitpy.layout_tests.layout_package import json_layout_results_generator
  44. from webkitpy.layout_tests.layout_package import json_results_generator
  45. from webkitpy.layout_tests.models import test_expectations
  46. from webkitpy.layout_tests.models import test_failures
  47. from webkitpy.layout_tests.models import test_run_results
  48. from webkitpy.layout_tests.models.test_input import TestInput
  49. _log = logging.getLogger(__name__)
  50. # Builder base URL where we have the archived test results.
  51. BUILDER_BASE_URL = "http://build.chromium.org/buildbot/layout_test_results/"
  52. TestExpectations = test_expectations.TestExpectations
  53. class Manager(object):
  54. """A class for managing running a series of tests on a series of layout
  55. test files."""
  56. def __init__(self, port, options, printer):
  57. """Initialize test runner data structures.
  58. Args:
  59. port: an object implementing port-specific
  60. options: a dictionary of command line options
  61. printer: a Printer object to record updates to.
  62. """
  63. self._port = port
  64. self._filesystem = port.host.filesystem
  65. self._options = options
  66. self._printer = printer
  67. self._expectations = None
  68. self.HTTP_SUBDIR = 'http' + port.TEST_PATH_SEPARATOR
  69. self.PERF_SUBDIR = 'perf'
  70. self.WEBSOCKET_SUBDIR = 'websocket' + port.TEST_PATH_SEPARATOR
  71. self.LAYOUT_TESTS_DIRECTORY = 'LayoutTests'
  72. # disable wss server. need to install pyOpenSSL on buildbots.
  73. # self._websocket_secure_server = websocket_server.PyWebSocket(
  74. # options.results_directory, use_tls=True, port=9323)
  75. self._results_directory = self._port.results_directory()
  76. self._finder = LayoutTestFinder(self._port, self._options)
  77. self._runner = LayoutTestRunner(self._options, self._port, self._printer, self._results_directory, self._test_is_slow)
  78. def _collect_tests(self, args):
  79. return self._finder.find_tests(self._options, args)
  80. def _is_http_test(self, test):
  81. return self.HTTP_SUBDIR in test or self._is_websocket_test(test)
  82. def _is_websocket_test(self, test):
  83. return self.WEBSOCKET_SUBDIR in test
  84. def _http_tests(self, test_names):
  85. return set(test for test in test_names if self._is_http_test(test))
  86. def _is_perf_test(self, test):
  87. return self.PERF_SUBDIR == test or (self.PERF_SUBDIR + self._port.TEST_PATH_SEPARATOR) in test
  88. def _prepare_lists(self, paths, test_names):
  89. tests_to_skip = self._finder.skip_tests(paths, test_names, self._expectations, self._http_tests(test_names))
  90. tests_to_run = [test for test in test_names if test not in tests_to_skip]
  91. # Create a sorted list of test files so the subset chunk,
  92. # if used, contains alphabetically consecutive tests.
  93. if self._options.order == 'natural':
  94. tests_to_run.sort(key=self._port.test_key)
  95. elif self._options.order == 'random':
  96. random.shuffle(tests_to_run)
  97. tests_to_run, tests_in_other_chunks = self._finder.split_into_chunks(tests_to_run)
  98. self._expectations.add_skipped_tests(tests_in_other_chunks)
  99. tests_to_skip.update(tests_in_other_chunks)
  100. return tests_to_run, tests_to_skip
  101. def _test_input_for_file(self, test_file):
  102. return TestInput(test_file,
  103. self._options.slow_time_out_ms if self._test_is_slow(test_file) else self._options.time_out_ms,
  104. self._test_requires_lock(test_file))
  105. def _test_requires_lock(self, test_file):
  106. """Return True if the test needs to be locked when
  107. running multiple copies of NRWTs. Perf tests are locked
  108. because heavy load caused by running other tests in parallel
  109. might cause some of them to timeout."""
  110. return self._is_http_test(test_file) or self._is_perf_test(test_file)
  111. def _test_is_slow(self, test_file):
  112. return self._expectations.has_modifier(test_file, test_expectations.SLOW)
  113. def needs_servers(self, test_names):
  114. return any(self._test_requires_lock(test_name) for test_name in test_names) and self._options.http
  115. def _set_up_run(self, test_names):
  116. self._printer.write_update("Checking build ...")
  117. if not self._port.check_build(self.needs_servers(test_names)):
  118. _log.error("Build check failed")
  119. return False
  120. # This must be started before we check the system dependencies,
  121. # since the helper may do things to make the setup correct.
  122. if self._options.pixel_tests:
  123. self._printer.write_update("Starting pixel test helper ...")
  124. self._port.start_helper()
  125. # Check that the system dependencies (themes, fonts, ...) are correct.
  126. if not self._options.nocheck_sys_deps:
  127. self._printer.write_update("Checking system dependencies ...")
  128. if not self._port.check_sys_deps(self.needs_servers(test_names)):
  129. self._port.stop_helper()
  130. return False
  131. if self._options.clobber_old_results:
  132. self._clobber_old_results()
  133. # Create the output directory if it doesn't already exist.
  134. self._port.host.filesystem.maybe_make_directory(self._results_directory)
  135. self._port.setup_test_run()
  136. return True
  137. def run(self, args):
  138. """Run the tests and return a RunDetails object with the results."""
  139. self._printer.write_update("Collecting tests ...")
  140. try:
  141. paths, test_names = self._collect_tests(args)
  142. except IOError:
  143. # This is raised if --test-list doesn't exist
  144. return test_run_results.RunDetails(exit_code=-1)
  145. self._printer.write_update("Parsing expectations ...")
  146. self._expectations = test_expectations.TestExpectations(self._port, test_names)
  147. tests_to_run, tests_to_skip = self._prepare_lists(paths, test_names)
  148. self._printer.print_found(len(test_names), len(tests_to_run), self._options.repeat_each, self._options.iterations)
  149. # Check to make sure we're not skipping every test.
  150. if not tests_to_run:
  151. _log.critical('No tests to run.')
  152. return test_run_results.RunDetails(exit_code=-1)
  153. if not self._set_up_run(tests_to_run):
  154. return test_run_results.RunDetails(exit_code=-1)
  155. start_time = time.time()
  156. enabled_pixel_tests_in_retry = False
  157. try:
  158. initial_results = self._run_tests(tests_to_run, tests_to_skip, self._options.repeat_each, self._options.iterations,
  159. int(self._options.child_processes), retrying=False)
  160. tests_to_retry = self._tests_to_retry(initial_results, include_crashes=self._port.should_retry_crashes())
  161. if self._options.retry_failures and tests_to_retry and not initial_results.interrupted:
  162. enabled_pixel_tests_in_retry = self._force_pixel_tests_if_needed()
  163. _log.info('')
  164. _log.info("Retrying %d unexpected failure(s) ..." % len(tests_to_retry))
  165. _log.info('')
  166. # If we are in multi test runner mode, change that using option --chunk-size = 1
  167. if self._options.chunk_size > 1:
  168. self._options.chunk_size = 1
  169. retry_results = self._run_tests(tests_to_retry, tests_to_skip=set(), repeat_each=1, iterations=1,
  170. num_workers=1, retrying=True)
  171. if enabled_pixel_tests_in_retry:
  172. self._options.pixel_tests = False
  173. else:
  174. retry_results = None
  175. finally:
  176. self._clean_up_run()
  177. end_time = time.time()
  178. # Some crash logs can take a long time to be written out so look
  179. # for new logs after the test run finishes.
  180. _log.debug("looking for new crash logs")
  181. self._look_for_new_crash_logs(initial_results, start_time)
  182. if retry_results:
  183. self._look_for_new_crash_logs(retry_results, start_time)
  184. _log.debug("summarizing results")
  185. summarized_results = test_run_results.summarize_results(self._port, self._expectations, initial_results, retry_results, enabled_pixel_tests_in_retry)
  186. self._printer.print_results(end_time - start_time, initial_results, summarized_results)
  187. if not self._options.dry_run:
  188. self._port.print_leaks_summary()
  189. self._upload_json_files(summarized_results, initial_results)
  190. results_path = self._filesystem.join(self._results_directory, "results.html")
  191. self._copy_results_html_file(results_path)
  192. if self._options.show_results and (initial_results.unexpected_results_by_name or
  193. (self._options.full_results_html and initial_results.total_failures)):
  194. self._port.show_results_html_file(results_path)
  195. return test_run_results.RunDetails(self._port.exit_code_from_summarized_results(summarized_results),
  196. summarized_results, initial_results, retry_results, enabled_pixel_tests_in_retry)
  197. def _run_tests(self, tests_to_run, tests_to_skip, repeat_each, iterations, num_workers, retrying):
  198. needs_http = any(self._is_http_test(test) for test in tests_to_run)
  199. needs_websockets = any(self._is_websocket_test(test) for test in tests_to_run)
  200. test_inputs = []
  201. for _ in xrange(iterations):
  202. for test in tests_to_run:
  203. for _ in xrange(repeat_each):
  204. test_inputs.append(self._test_input_for_file(test))
  205. return self._runner.run_tests(self._expectations, test_inputs, tests_to_skip, num_workers, needs_http, needs_websockets, retrying)
  206. def _clean_up_run(self):
  207. _log.debug("Flushing stdout")
  208. sys.stdout.flush()
  209. _log.debug("Flushing stderr")
  210. sys.stderr.flush()
  211. _log.debug("Stopping helper")
  212. self._port.stop_helper()
  213. _log.debug("Cleaning up port")
  214. self._port.clean_up_test_run()
  215. def _force_pixel_tests_if_needed(self):
  216. if self._options.pixel_tests:
  217. return False
  218. _log.debug("Restarting helper")
  219. self._port.stop_helper()
  220. self._options.pixel_tests = True
  221. self._port.start_helper()
  222. return True
  223. def _look_for_new_crash_logs(self, run_results, start_time):
  224. """Since crash logs can take a long time to be written out if the system is
  225. under stress do a second pass at the end of the test run.
  226. run_results: the results of the test run
  227. start_time: time the tests started at. We're looking for crash
  228. logs after that time.
  229. """
  230. crashed_processes = []
  231. for test, result in run_results.unexpected_results_by_name.iteritems():
  232. if (result.type != test_expectations.CRASH):
  233. continue
  234. for failure in result.failures:
  235. if not isinstance(failure, test_failures.FailureCrash):
  236. continue
  237. crashed_processes.append([test, failure.process_name, failure.pid])
  238. sample_files = self._port.look_for_new_samples(crashed_processes, start_time)
  239. if sample_files:
  240. for test, sample_file in sample_files.iteritems():
  241. writer = TestResultWriter(self._port._filesystem, self._port, self._port.results_directory(), test)
  242. writer.copy_sample_file(sample_file)
  243. crash_logs = self._port.look_for_new_crash_logs(crashed_processes, start_time)
  244. if crash_logs:
  245. for test, crash_log in crash_logs.iteritems():
  246. writer = TestResultWriter(self._port._filesystem, self._port, self._port.results_directory(), test)
  247. writer.write_crash_log(crash_log)
  248. def _clobber_old_results(self):
  249. # Just clobber the actual test results directories since the other
  250. # files in the results directory are explicitly used for cross-run
  251. # tracking.
  252. self._printer.write_update("Clobbering old results in %s" %
  253. self._results_directory)
  254. layout_tests_dir = self._port.layout_tests_dir()
  255. possible_dirs = self._port.test_dirs()
  256. for dirname in possible_dirs:
  257. if self._filesystem.isdir(self._filesystem.join(layout_tests_dir, dirname)):
  258. self._filesystem.rmtree(self._filesystem.join(self._results_directory, dirname))
  259. def _tests_to_retry(self, run_results, include_crashes):
  260. return [result.test_name for result in run_results.unexpected_results_by_name.values() if
  261. ((result.type != test_expectations.PASS) and
  262. (result.type != test_expectations.MISSING) and
  263. (result.type != test_expectations.CRASH or include_crashes))]
  264. def _upload_json_files(self, summarized_results, initial_results):
  265. """Writes the results of the test run as JSON files into the results
  266. dir and upload the files to the appengine server.
  267. Args:
  268. summarized_results: dict of results
  269. initial_results: full summary object
  270. """
  271. _log.debug("Writing JSON files in %s." % self._results_directory)
  272. # FIXME: Upload stats.json to the server and delete times_ms.
  273. times_trie = json_results_generator.test_timings_trie(self._port, initial_results.results_by_name.values())
  274. times_json_path = self._filesystem.join(self._results_directory, "times_ms.json")
  275. json_results_generator.write_json(self._filesystem, times_trie, times_json_path)
  276. stats_trie = self._stats_trie(initial_results)
  277. stats_path = self._filesystem.join(self._results_directory, "stats.json")
  278. self._filesystem.write_text_file(stats_path, json.dumps(stats_trie))
  279. full_results_path = self._filesystem.join(self._results_directory, "full_results.json")
  280. # We write full_results.json out as jsonp because we need to load it from a file url and Chromium doesn't allow that.
  281. json_results_generator.write_json(self._filesystem, summarized_results, full_results_path, callback="ADD_RESULTS")
  282. generator = json_layout_results_generator.JSONLayoutResultsGenerator(
  283. self._port, self._options.builder_name, self._options.build_name,
  284. self._options.build_number, self._results_directory,
  285. BUILDER_BASE_URL,
  286. self._expectations, initial_results,
  287. self._options.test_results_server,
  288. "layout-tests",
  289. self._options.master_name)
  290. _log.debug("Finished writing JSON files.")
  291. json_files = ["incremental_results.json", "full_results.json", "times_ms.json"]
  292. generator.upload_json_files(json_files)
  293. incremental_results_path = self._filesystem.join(self._results_directory, "incremental_results.json")
  294. # Remove these files from the results directory so they don't take up too much space on the buildbot.
  295. # The tools use the version we uploaded to the results server anyway.
  296. self._filesystem.remove(times_json_path)
  297. self._filesystem.remove(incremental_results_path)
  298. def _copy_results_html_file(self, destination_path):
  299. base_dir = self._port.path_from_webkit_base('LayoutTests', 'fast', 'harness')
  300. results_file = self._filesystem.join(base_dir, 'results.html')
  301. # Note that the results.html template file won't exist when we're using a MockFileSystem during unit tests,
  302. # so make sure it exists before we try to copy it.
  303. if self._filesystem.exists(results_file):
  304. self._filesystem.copyfile(results_file, destination_path)
  305. def _stats_trie(self, initial_results):
  306. def _worker_number(worker_name):
  307. return int(worker_name.split('/')[1]) if worker_name else -1
  308. stats = {}
  309. for result in initial_results.results_by_name.values():
  310. if result.type != test_expectations.SKIP:
  311. stats[result.test_name] = {'results': (_worker_number(result.worker_name), result.test_number, result.pid, int(result.test_run_time * 1000), int(result.total_run_time * 1000))}
  312. stats_trie = {}
  313. for name, value in stats.iteritems():
  314. json_results_generator.add_path_to_trie(name, value, stats_trie)
  315. return stats_trie