base.py 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548
  1. # Copyright (C) 2010 Google Inc. All rights reserved.
  2. #
  3. # Redistribution and use in source and binary forms, with or without
  4. # modification, are permitted provided that the following conditions are
  5. # met:
  6. #
  7. # * Redistributions of source code must retain the above copyright
  8. # notice, this list of conditions and the following disclaimer.
  9. # * Redistributions in binary form must reproduce the above
  10. # copyright notice, this list of conditions and the following disclaimer
  11. # in the documentation and/or other materials provided with the
  12. # distribution.
  13. # * Neither the Google name nor the names of its
  14. # contributors may be used to endorse or promote products derived from
  15. # this software without specific prior written permission.
  16. #
  17. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  20. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  21. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  22. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  23. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  24. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  25. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. """Abstract base class of Port-specific entry points for the layout tests
  29. test infrastructure (the Port and Driver classes)."""
  30. import cgi
  31. import difflib
  32. import errno
  33. import itertools
  34. import logging
  35. import os
  36. import operator
  37. import optparse
  38. import re
  39. import sys
  40. try:
  41. from collections import OrderedDict
  42. except ImportError:
  43. # Needed for Python < 2.7
  44. from webkitpy.thirdparty.ordered_dict import OrderedDict
  45. from webkitpy.common import find_files
  46. from webkitpy.common import read_checksum_from_png
  47. from webkitpy.common.memoized import memoized
  48. from webkitpy.common.system import path
  49. from webkitpy.common.system.executive import ScriptError
  50. from webkitpy.common.system.systemhost import SystemHost
  51. from webkitpy.common.webkit_finder import WebKitFinder
  52. from webkitpy.layout_tests.models.test_configuration import TestConfiguration
  53. from webkitpy.port import config as port_config
  54. from webkitpy.port import driver
  55. from webkitpy.port import http_lock
  56. from webkitpy.port import image_diff
  57. from webkitpy.port import server_process
  58. from webkitpy.port.factory import PortFactory
  59. from webkitpy.layout_tests.servers import apache_http_server
  60. from webkitpy.layout_tests.servers import http_server
  61. from webkitpy.layout_tests.servers import websocket_server
  62. _log = logging.getLogger(__name__)
  63. class Port(object):
  64. """Abstract class for Port-specific hooks for the layout_test package."""
  65. # Subclasses override this. This should indicate the basic implementation
  66. # part of the port name, e.g., 'win', 'gtk'; there is probably (?) one unique value per class.
  67. # FIXME: We should probably rename this to something like 'implementation_name'.
  68. port_name = None
  69. # Test names resemble unix relative paths, and use '/' as a directory separator.
  70. TEST_PATH_SEPARATOR = '/'
  71. ALL_BUILD_TYPES = ('debug', 'release')
  72. @classmethod
  73. def determine_full_port_name(cls, host, options, port_name):
  74. """Return a fully-specified port name that can be used to construct objects."""
  75. # Subclasses will usually override this.
  76. options = options or {}
  77. assert port_name.startswith(cls.port_name)
  78. if getattr(options, 'webkit_test_runner', False) and not '-wk2' in port_name:
  79. return port_name + '-wk2'
  80. return port_name
  81. def __init__(self, host, port_name, options=None, **kwargs):
  82. # This value may be different from cls.port_name by having version modifiers
  83. # and other fields appended to it (for example, 'qt-arm' or 'mac-wk2').
  84. self._name = port_name
  85. # These are default values that should be overridden in a subclasses.
  86. self._version = ''
  87. self._architecture = 'x86'
  88. # FIXME: Ideally we'd have a package-wide way to get a
  89. # well-formed options object that had all of the necessary
  90. # options defined on it.
  91. self._options = options or optparse.Values()
  92. if self._name and '-wk2' in self._name:
  93. self._options.webkit_test_runner = True
  94. self.host = host
  95. self._executive = host.executive
  96. self._filesystem = host.filesystem
  97. self._webkit_finder = WebKitFinder(host.filesystem)
  98. self._config = port_config.Config(self._executive, self._filesystem, self.port_name)
  99. self._helper = None
  100. self._http_server = None
  101. self._websocket_server = None
  102. self._image_differ = None
  103. self._server_process_constructor = server_process.ServerProcess # overridable for testing
  104. self._http_lock = None # FIXME: Why does this live on the port object?
  105. # Python's Popen has a bug that causes any pipes opened to a
  106. # process that can't be executed to be leaked. Since this
  107. # code is specifically designed to tolerate exec failures
  108. # to gracefully handle cases where wdiff is not installed,
  109. # the bug results in a massive file descriptor leak. As a
  110. # workaround, if an exec failure is ever experienced for
  111. # wdiff, assume it's not available. This will leak one
  112. # file descriptor but that's better than leaking each time
  113. # wdiff would be run.
  114. #
  115. # http://mail.python.org/pipermail/python-list/
  116. # 2008-August/505753.html
  117. # http://bugs.python.org/issue3210
  118. self._wdiff_available = None
  119. # FIXME: prettypatch.py knows this path, why is it copied here?
  120. self._pretty_patch_path = self.path_from_webkit_base("Websites", "bugs.webkit.org", "PrettyPatch", "prettify.rb")
  121. self._pretty_patch_available = None
  122. if not hasattr(options, 'configuration') or not options.configuration:
  123. self.set_option_default('configuration', self.default_configuration())
  124. self._test_configuration = None
  125. self._reftest_list = {}
  126. self._results_directory = None
  127. self._root_was_set = hasattr(options, 'root') and options.root
  128. def additional_drt_flag(self):
  129. return []
  130. def supports_per_test_timeout(self):
  131. return False
  132. def default_pixel_tests(self):
  133. # FIXME: Disable until they are run by default on build.webkit.org.
  134. return False
  135. def default_timeout_ms(self):
  136. if self.get_option('webkit_test_runner'):
  137. # Add some more time to WebKitTestRunner because it needs to syncronise the state
  138. # with the web process and we want to detect if there is a problem with that in the driver.
  139. return 80 * 1000
  140. return 35 * 1000
  141. def driver_stop_timeout(self):
  142. """ Returns the amount of time in seconds to wait before killing the process in driver.stop()."""
  143. # We want to wait for at least 3 seconds, but if we are really slow, we want to be slow on cleanup as
  144. # well (for things like ASAN, Valgrind, etc.)
  145. return 3.0 * float(self.get_option('time_out_ms', '0')) / self.default_timeout_ms()
  146. def wdiff_available(self):
  147. if self._wdiff_available is None:
  148. self._wdiff_available = self.check_wdiff(logging=False)
  149. return self._wdiff_available
  150. def pretty_patch_available(self):
  151. if self._pretty_patch_available is None:
  152. self._pretty_patch_available = self.check_pretty_patch(logging=False)
  153. return self._pretty_patch_available
  154. def should_retry_crashes(self):
  155. return False
  156. def default_child_processes(self):
  157. """Return the number of DumpRenderTree instances to use for this port."""
  158. return self._executive.cpu_count()
  159. def default_max_locked_shards(self):
  160. """Return the number of "locked" shards to run in parallel (like the http tests)."""
  161. return 1
  162. def worker_startup_delay_secs(self):
  163. # FIXME: If we start workers up too quickly, DumpRenderTree appears
  164. # to thrash on something and time out its first few tests. Until
  165. # we can figure out what's going on, sleep a bit in between
  166. # workers. See https://bugs.webkit.org/show_bug.cgi?id=79147 .
  167. return 0.1
  168. def baseline_path(self):
  169. """Return the absolute path to the directory to store new baselines in for this port."""
  170. # FIXME: remove once all callers are calling either baseline_version_dir() or baseline_platform_dir()
  171. return self.baseline_version_dir()
  172. def baseline_platform_dir(self):
  173. """Return the absolute path to the default (version-independent) platform-specific results."""
  174. return self._filesystem.join(self.layout_tests_dir(), 'platform', self.port_name)
  175. def baseline_version_dir(self):
  176. """Return the absolute path to the platform-and-version-specific results."""
  177. baseline_search_paths = self.baseline_search_path()
  178. return baseline_search_paths[0]
  179. def baseline_search_path(self):
  180. return self.get_option('additional_platform_directory', []) + self._compare_baseline() + self.default_baseline_search_path()
  181. def default_baseline_search_path(self):
  182. """Return a list of absolute paths to directories to search under for
  183. baselines. The directories are searched in order."""
  184. search_paths = []
  185. if self.get_option('webkit_test_runner'):
  186. search_paths.append(self._wk2_port_name())
  187. search_paths.append(self.name())
  188. if self.name() != self.port_name:
  189. search_paths.append(self.port_name)
  190. return map(self._webkit_baseline_path, search_paths)
  191. @memoized
  192. def _compare_baseline(self):
  193. factory = PortFactory(self.host)
  194. target_port = self.get_option('compare_port')
  195. if target_port:
  196. return factory.get(target_port).default_baseline_search_path()
  197. return []
  198. def check_build(self, needs_http):
  199. """This routine is used to ensure that the build is up to date
  200. and all the needed binaries are present."""
  201. # If we're using a pre-built copy of WebKit (--root), we assume it also includes a build of DRT.
  202. if not self._root_was_set and self.get_option('build') and not self._build_driver():
  203. return False
  204. if not self._check_driver():
  205. return False
  206. if self.get_option('pixel_tests'):
  207. if not self.check_image_diff():
  208. return False
  209. if not self._check_port_build():
  210. return False
  211. return True
  212. def _check_driver(self):
  213. driver_path = self._path_to_driver()
  214. if not self._filesystem.exists(driver_path):
  215. _log.error("%s was not found at %s" % (self.driver_name(), driver_path))
  216. return False
  217. return True
  218. def _check_port_build(self):
  219. # Ports can override this method to do additional checks.
  220. return True
  221. def check_sys_deps(self, needs_http):
  222. """If the port needs to do some runtime checks to ensure that the
  223. tests can be run successfully, it should override this routine.
  224. This step can be skipped with --nocheck-sys-deps.
  225. Returns whether the system is properly configured."""
  226. if needs_http:
  227. return self.check_httpd()
  228. return True
  229. def check_image_diff(self, override_step=None, logging=True):
  230. """This routine is used to check whether image_diff binary exists."""
  231. image_diff_path = self._path_to_image_diff()
  232. if not self._filesystem.exists(image_diff_path):
  233. _log.error("ImageDiff was not found at %s" % image_diff_path)
  234. return False
  235. return True
  236. def check_pretty_patch(self, logging=True):
  237. """Checks whether we can use the PrettyPatch ruby script."""
  238. try:
  239. _ = self._executive.run_command(['ruby', '--version'])
  240. except OSError, e:
  241. if e.errno in [errno.ENOENT, errno.EACCES, errno.ECHILD]:
  242. if logging:
  243. _log.warning("Ruby is not installed; can't generate pretty patches.")
  244. _log.warning('')
  245. return False
  246. if not self._filesystem.exists(self._pretty_patch_path):
  247. if logging:
  248. _log.warning("Unable to find %s; can't generate pretty patches." % self._pretty_patch_path)
  249. _log.warning('')
  250. return False
  251. return True
  252. def check_wdiff(self, logging=True):
  253. if not self._path_to_wdiff():
  254. # Don't need to log here since this is the port choosing not to use wdiff.
  255. return False
  256. try:
  257. _ = self._executive.run_command([self._path_to_wdiff(), '--help'])
  258. except OSError:
  259. if logging:
  260. message = self._wdiff_missing_message()
  261. if message:
  262. for line in message.splitlines():
  263. _log.warning(' ' + line)
  264. _log.warning('')
  265. return False
  266. return True
  267. def _wdiff_missing_message(self):
  268. return 'wdiff is not installed; please install it to generate word-by-word diffs.'
  269. def check_httpd(self):
  270. if self._uses_apache():
  271. httpd_path = self._path_to_apache()
  272. else:
  273. httpd_path = self._path_to_lighttpd()
  274. try:
  275. server_name = self._filesystem.basename(httpd_path)
  276. env = self.setup_environ_for_server(server_name)
  277. if self._executive.run_command([httpd_path, "-v"], env=env, return_exit_code=True) != 0:
  278. _log.error("httpd seems broken. Cannot run http tests.")
  279. return False
  280. return True
  281. except OSError:
  282. _log.error("No httpd found. Cannot run http tests.")
  283. return False
  284. def do_text_results_differ(self, expected_text, actual_text):
  285. return expected_text != actual_text
  286. def do_audio_results_differ(self, expected_audio, actual_audio):
  287. return expected_audio != actual_audio
  288. def diff_image(self, expected_contents, actual_contents, tolerance=None):
  289. """Compare two images and return a tuple of an image diff, a percentage difference (0-100), and an error string.
  290. |tolerance| should be a percentage value (0.0 - 100.0).
  291. If it is omitted, the port default tolerance value is used.
  292. If an error occurs (like ImageDiff isn't found, or crashes, we log an error and return True (for a diff).
  293. """
  294. if not actual_contents and not expected_contents:
  295. return (None, 0, None)
  296. if not actual_contents or not expected_contents:
  297. return (True, 0, None)
  298. if not self._image_differ:
  299. self._image_differ = image_diff.ImageDiffer(self)
  300. self.set_option_default('tolerance', 0.1)
  301. if tolerance is None:
  302. tolerance = self.get_option('tolerance')
  303. return self._image_differ.diff_image(expected_contents, actual_contents, tolerance)
  304. def diff_text(self, expected_text, actual_text, expected_filename, actual_filename):
  305. """Returns a string containing the diff of the two text strings
  306. in 'unified diff' format."""
  307. # The filenames show up in the diff output, make sure they're
  308. # raw bytes and not unicode, so that they don't trigger join()
  309. # trying to decode the input.
  310. def to_raw_bytes(string_value):
  311. if isinstance(string_value, unicode):
  312. return string_value.encode('utf-8')
  313. return string_value
  314. expected_filename = to_raw_bytes(expected_filename)
  315. actual_filename = to_raw_bytes(actual_filename)
  316. diff = difflib.unified_diff(expected_text.splitlines(True),
  317. actual_text.splitlines(True),
  318. expected_filename,
  319. actual_filename)
  320. return ''.join(diff)
  321. def check_for_leaks(self, process_name, process_pid):
  322. # Subclasses should check for leaks in the running process
  323. # and print any necessary warnings if leaks are found.
  324. # FIXME: We should consider moving much of this logic into
  325. # Executive and make it platform-specific instead of port-specific.
  326. pass
  327. def print_leaks_summary(self):
  328. # Subclasses can override this to print a summary of leaks found
  329. # while running the layout tests.
  330. pass
  331. def driver_name(self):
  332. if self.get_option('driver_name'):
  333. return self.get_option('driver_name')
  334. if self.get_option('webkit_test_runner'):
  335. return 'WebKitTestRunner'
  336. return 'DumpRenderTree'
  337. def expected_baselines_by_extension(self, test_name):
  338. """Returns a dict mapping baseline suffix to relative path for each baseline in
  339. a test. For reftests, it returns ".==" or ".!=" instead of the suffix."""
  340. # FIXME: The name similarity between this and expected_baselines() below, is unfortunate.
  341. # We should probably rename them both.
  342. baseline_dict = {}
  343. reference_files = self.reference_files(test_name)
  344. if reference_files:
  345. # FIXME: How should this handle more than one type of reftest?
  346. baseline_dict['.' + reference_files[0][0]] = self.relative_test_filename(reference_files[0][1])
  347. for extension in self.baseline_extensions():
  348. path = self.expected_filename(test_name, extension, return_default=False)
  349. baseline_dict[extension] = self.relative_test_filename(path) if path else path
  350. return baseline_dict
  351. def baseline_extensions(self):
  352. """Returns a tuple of all of the non-reftest baseline extensions we use. The extensions include the leading '.'."""
  353. return ('.wav', '.webarchive', '.txt', '.png')
  354. def expected_baselines(self, test_name, suffix, all_baselines=False):
  355. """Given a test name, finds where the baseline results are located.
  356. Args:
  357. test_name: name of test file (usually a relative path under LayoutTests/)
  358. suffix: file suffix of the expected results, including dot; e.g.
  359. '.txt' or '.png'. This should not be None, but may be an empty
  360. string.
  361. all_baselines: If True, return an ordered list of all baseline paths
  362. for the given platform. If False, return only the first one.
  363. Returns
  364. a list of ( platform_dir, results_filename ), where
  365. platform_dir - abs path to the top of the results tree (or test
  366. tree)
  367. results_filename - relative path from top of tree to the results
  368. file
  369. (port.join() of the two gives you the full path to the file,
  370. unless None was returned.)
  371. Return values will be in the format appropriate for the current
  372. platform (e.g., "\\" for path separators on Windows). If the results
  373. file is not found, then None will be returned for the directory,
  374. but the expected relative pathname will still be returned.
  375. This routine is generic but lives here since it is used in
  376. conjunction with the other baseline and filename routines that are
  377. platform specific.
  378. """
  379. baseline_filename = self._filesystem.splitext(test_name)[0] + '-expected' + suffix
  380. baseline_search_path = self.baseline_search_path()
  381. baselines = []
  382. for platform_dir in baseline_search_path:
  383. if self._filesystem.exists(self._filesystem.join(platform_dir, baseline_filename)):
  384. baselines.append((platform_dir, baseline_filename))
  385. if not all_baselines and baselines:
  386. return baselines
  387. # If it wasn't found in a platform directory, return the expected
  388. # result in the test directory, even if no such file actually exists.
  389. platform_dir = self.layout_tests_dir()
  390. if self._filesystem.exists(self._filesystem.join(platform_dir, baseline_filename)):
  391. baselines.append((platform_dir, baseline_filename))
  392. if baselines:
  393. return baselines
  394. return [(None, baseline_filename)]
  395. def expected_filename(self, test_name, suffix, return_default=True):
  396. """Given a test name, returns an absolute path to its expected results.
  397. If no expected results are found in any of the searched directories,
  398. the directory in which the test itself is located will be returned.
  399. The return value is in the format appropriate for the platform
  400. (e.g., "\\" for path separators on windows).
  401. Args:
  402. test_name: name of test file (usually a relative path under LayoutTests/)
  403. suffix: file suffix of the expected results, including dot; e.g. '.txt'
  404. or '.png'. This should not be None, but may be an empty string.
  405. platform: the most-specific directory name to use to build the
  406. search list of directories; e.g. 'mountainlion-wk2'
  407. return_default: if True, returns the path to the generic expectation if nothing
  408. else is found; if False, returns None.
  409. This routine is generic but is implemented here to live alongside
  410. the other baseline and filename manipulation routines.
  411. """
  412. # FIXME: The [0] here is very mysterious, as is the destructured return.
  413. platform_dir, baseline_filename = self.expected_baselines(test_name, suffix)[0]
  414. if platform_dir:
  415. return self._filesystem.join(platform_dir, baseline_filename)
  416. actual_test_name = self.lookup_virtual_test_base(test_name)
  417. if actual_test_name:
  418. return self.expected_filename(actual_test_name, suffix)
  419. if return_default:
  420. return self._filesystem.join(self.layout_tests_dir(), baseline_filename)
  421. return None
  422. def expected_checksum(self, test_name):
  423. """Returns the checksum of the image we expect the test to produce, or None if it is a text-only test."""
  424. png_path = self.expected_filename(test_name, '.png')
  425. if self._filesystem.exists(png_path):
  426. with self._filesystem.open_binary_file_for_reading(png_path) as filehandle:
  427. return read_checksum_from_png.read_checksum(filehandle)
  428. return None
  429. def expected_image(self, test_name):
  430. """Returns the image we expect the test to produce."""
  431. baseline_path = self.expected_filename(test_name, '.png')
  432. if not self._filesystem.exists(baseline_path):
  433. return None
  434. return self._filesystem.read_binary_file(baseline_path)
  435. def expected_audio(self, test_name):
  436. baseline_path = self.expected_filename(test_name, '.wav')
  437. if not self._filesystem.exists(baseline_path):
  438. return None
  439. return self._filesystem.read_binary_file(baseline_path)
  440. def expected_text(self, test_name):
  441. """Returns the text output we expect the test to produce, or None
  442. if we don't expect there to be any text output.
  443. End-of-line characters are normalized to '\n'."""
  444. # FIXME: DRT output is actually utf-8, but since we don't decode the
  445. # output from DRT (instead treating it as a binary string), we read the
  446. # baselines as a binary string, too.
  447. baseline_path = self.expected_filename(test_name, '.txt')
  448. if not self._filesystem.exists(baseline_path):
  449. baseline_path = self.expected_filename(test_name, '.webarchive')
  450. if not self._filesystem.exists(baseline_path):
  451. return None
  452. text = self._filesystem.read_binary_file(baseline_path)
  453. return text.replace("\r\n", "\n")
  454. def _get_reftest_list(self, test_name):
  455. dirname = self._filesystem.join(self.layout_tests_dir(), self._filesystem.dirname(test_name))
  456. if dirname not in self._reftest_list:
  457. self._reftest_list[dirname] = Port._parse_reftest_list(self._filesystem, dirname)
  458. return self._reftest_list[dirname]
  459. @staticmethod
  460. def _parse_reftest_list(filesystem, test_dirpath):
  461. reftest_list_path = filesystem.join(test_dirpath, 'reftest.list')
  462. if not filesystem.isfile(reftest_list_path):
  463. return None
  464. reftest_list_file = filesystem.read_text_file(reftest_list_path)
  465. parsed_list = {}
  466. for line in reftest_list_file.split('\n'):
  467. line = re.sub('#.+$', '', line)
  468. split_line = line.split()
  469. if len(split_line) < 3:
  470. continue
  471. expectation_type, test_file, ref_file = split_line
  472. parsed_list.setdefault(filesystem.join(test_dirpath, test_file), []).append((expectation_type, filesystem.join(test_dirpath, ref_file)))
  473. return parsed_list
  474. def reference_files(self, test_name):
  475. """Return a list of expectation (== or !=) and filename pairs"""
  476. reftest_list = self._get_reftest_list(test_name)
  477. if not reftest_list:
  478. reftest_list = []
  479. for expectation, prefix in (('==', ''), ('!=', '-mismatch')):
  480. for extention in Port._supported_reference_extensions:
  481. path = self.expected_filename(test_name, prefix + extention)
  482. if self._filesystem.exists(path):
  483. reftest_list.append((expectation, path))
  484. return reftest_list
  485. return reftest_list.get(self._filesystem.join(self.layout_tests_dir(), test_name), []) # pylint: disable=E1103
  486. def tests(self, paths):
  487. """Return the list of tests found. Both generic and platform-specific tests matching paths should be returned."""
  488. expanded_paths = self._expanded_paths(paths)
  489. tests = self._real_tests(expanded_paths)
  490. tests.extend(self._virtual_tests(expanded_paths, self.populated_virtual_test_suites()))
  491. return tests
  492. def _expanded_paths(self, paths):
  493. expanded_paths = []
  494. fs = self._filesystem
  495. all_platform_dirs = [path for path in fs.glob(fs.join(self.layout_tests_dir(), 'platform', '*')) if fs.isdir(path)]
  496. for path in paths:
  497. expanded_paths.append(path)
  498. if self.test_isdir(path) and not path.startswith('platform'):
  499. for platform_dir in all_platform_dirs:
  500. if fs.isdir(fs.join(platform_dir, path)) and platform_dir in self.baseline_search_path():
  501. expanded_paths.append(self.relative_test_filename(fs.join(platform_dir, path)))
  502. return expanded_paths
  503. def _real_tests(self, paths):
  504. # When collecting test cases, skip these directories
  505. skipped_directories = set(['.svn', '_svn', 'resources', 'script-tests', 'reference', 'reftest'])
  506. files = find_files.find(self._filesystem, self.layout_tests_dir(), paths, skipped_directories, Port._is_test_file, self.test_key)
  507. return [self.relative_test_filename(f) for f in files]
  508. # When collecting test cases, we include any file with these extensions.
  509. _supported_test_extensions = set(['.html', '.shtml', '.xml', '.xhtml', '.pl', '.htm', '.php', '.svg', '.mht'])
  510. _supported_reference_extensions = set(['.html', '.xml', '.xhtml', '.htm', '.svg'])
  511. @staticmethod
  512. # If any changes are made here be sure to update the isUsedInReftest method in old-run-webkit-tests as well.
  513. def is_reference_html_file(filesystem, dirname, filename):
  514. if filename.startswith('ref-') or filename.startswith('notref-'):
  515. return True
  516. filename_wihout_ext, ext = filesystem.splitext(filename)
  517. if ext not in Port._supported_reference_extensions:
  518. return False
  519. for suffix in ['-expected', '-expected-mismatch', '-ref', '-notref']:
  520. if filename_wihout_ext.endswith(suffix):
  521. return True
  522. return False
  523. @staticmethod
  524. def _has_supported_extension(filesystem, filename):
  525. """Return true if filename is one of the file extensions we want to run a test on."""
  526. extension = filesystem.splitext(filename)[1]
  527. return extension in Port._supported_test_extensions
  528. @staticmethod
  529. def _is_test_file(filesystem, dirname, filename):
  530. return Port._has_supported_extension(filesystem, filename) and not Port.is_reference_html_file(filesystem, dirname, filename)
  531. def test_key(self, test_name):
  532. """Turns a test name into a list with two sublists, the natural key of the
  533. dirname, and the natural key of the basename.
  534. This can be used when sorting paths so that files in a directory.
  535. directory are kept together rather than being mixed in with files in
  536. subdirectories."""
  537. dirname, basename = self.split_test(test_name)
  538. return (self._natural_sort_key(dirname + self.TEST_PATH_SEPARATOR), self._natural_sort_key(basename))
  539. def _natural_sort_key(self, string_to_split):
  540. """ Turns a string into a list of string and number chunks, i.e. "z23a" -> ["z", 23, "a"]
  541. This can be used to implement "natural sort" order. See:
  542. http://www.codinghorror.com/blog/2007/12/sorting-for-humans-natural-sort-order.html
  543. http://nedbatchelder.com/blog/200712.html#e20071211T054956
  544. """
  545. def tryint(val):
  546. try:
  547. return int(val)
  548. except ValueError:
  549. return val
  550. return [tryint(chunk) for chunk in re.split('(\d+)', string_to_split)]
  551. def test_dirs(self):
  552. """Returns the list of top-level test directories."""
  553. layout_tests_dir = self.layout_tests_dir()
  554. return filter(lambda x: self._filesystem.isdir(self._filesystem.join(layout_tests_dir, x)),
  555. self._filesystem.listdir(layout_tests_dir))
  556. @memoized
  557. def test_isfile(self, test_name):
  558. """Return True if the test name refers to a directory of tests."""
  559. # Used by test_expectations.py to apply rules to whole directories.
  560. if self._filesystem.isfile(self.abspath_for_test(test_name)):
  561. return True
  562. base = self.lookup_virtual_test_base(test_name)
  563. return base and self._filesystem.isfile(self.abspath_for_test(base))
  564. @memoized
  565. def test_isdir(self, test_name):
  566. """Return True if the test name refers to a directory of tests."""
  567. # Used by test_expectations.py to apply rules to whole directories.
  568. if self._filesystem.isdir(self.abspath_for_test(test_name)):
  569. return True
  570. base = self.lookup_virtual_test_base(test_name)
  571. return base and self._filesystem.isdir(self.abspath_for_test(base))
  572. @memoized
  573. def test_exists(self, test_name):
  574. """Return True if the test name refers to an existing test or baseline."""
  575. # Used by test_expectations.py to determine if an entry refers to a
  576. # valid test and by printing.py to determine if baselines exist.
  577. return self.test_isfile(test_name) or self.test_isdir(test_name)
  578. def split_test(self, test_name):
  579. """Splits a test name into the 'directory' part and the 'basename' part."""
  580. index = test_name.rfind(self.TEST_PATH_SEPARATOR)
  581. if index < 1:
  582. return ('', test_name)
  583. return (test_name[0:index], test_name[index:])
  584. def normalize_test_name(self, test_name):
  585. """Returns a normalized version of the test name or test directory."""
  586. if test_name.endswith('/'):
  587. return test_name
  588. if self.test_isdir(test_name):
  589. return test_name + '/'
  590. return test_name
  591. def driver_cmd_line(self):
  592. """Prints the DRT command line that will be used."""
  593. driver = self.create_driver(0)
  594. return driver.cmd_line(self.get_option('pixel_tests'), [])
  595. def update_baseline(self, baseline_path, data):
  596. """Updates the baseline for a test.
  597. Args:
  598. baseline_path: the actual path to use for baseline, not the path to
  599. the test. This function is used to update either generic or
  600. platform-specific baselines, but we can't infer which here.
  601. data: contents of the baseline.
  602. """
  603. self._filesystem.write_binary_file(baseline_path, data)
  604. # FIXME: update callers to create a finder and call it instead of these next five routines (which should be protected).
  605. def webkit_base(self):
  606. return self._webkit_finder.webkit_base()
  607. def path_from_webkit_base(self, *comps):
  608. return self._webkit_finder.path_from_webkit_base(*comps)
  609. def path_to_script(self, script_name):
  610. return self._webkit_finder.path_to_script(script_name)
  611. def layout_tests_dir(self):
  612. return self._webkit_finder.layout_tests_dir()
  613. def perf_tests_dir(self):
  614. return self._webkit_finder.perf_tests_dir()
  615. def skipped_layout_tests(self, test_list):
  616. """Returns tests skipped outside of the TestExpectations files."""
  617. return set(self._tests_for_other_platforms()).union(self._skipped_tests_for_unsupported_features(test_list))
  618. def _tests_from_skipped_file_contents(self, skipped_file_contents):
  619. tests_to_skip = []
  620. for line in skipped_file_contents.split('\n'):
  621. line = line.strip()
  622. line = line.rstrip('/') # Best to normalize directory names to not include the trailing slash.
  623. if line.startswith('#') or not len(line):
  624. continue
  625. tests_to_skip.append(line)
  626. return tests_to_skip
  627. def _expectations_from_skipped_files(self, skipped_file_paths):
  628. tests_to_skip = []
  629. for search_path in skipped_file_paths:
  630. filename = self._filesystem.join(self._webkit_baseline_path(search_path), "Skipped")
  631. if not self._filesystem.exists(filename):
  632. _log.debug("Skipped does not exist: %s" % filename)
  633. continue
  634. _log.debug("Using Skipped file: %s" % filename)
  635. skipped_file_contents = self._filesystem.read_text_file(filename)
  636. tests_to_skip.extend(self._tests_from_skipped_file_contents(skipped_file_contents))
  637. return tests_to_skip
  638. @memoized
  639. def skipped_perf_tests(self):
  640. return self._expectations_from_skipped_files([self.perf_tests_dir()])
  641. def skips_perf_test(self, test_name):
  642. for test_or_category in self.skipped_perf_tests():
  643. if test_or_category == test_name:
  644. return True
  645. category = self._filesystem.join(self.perf_tests_dir(), test_or_category)
  646. if self._filesystem.isdir(category) and test_name.startswith(test_or_category):
  647. return True
  648. return False
  649. def name(self):
  650. """Returns a name that uniquely identifies this particular type of port
  651. (e.g., "mac-snowleopard" or "chromium-linux-x86_x64" and can be passed
  652. to factory.get() to instantiate the port."""
  653. return self._name
  654. def operating_system(self):
  655. # Subclasses should override this default implementation.
  656. return 'mac'
  657. def version(self):
  658. """Returns a string indicating the version of a given platform, e.g.
  659. 'leopard' or 'xp'.
  660. This is used to help identify the exact port when parsing test
  661. expectations, determining search paths, and logging information."""
  662. return self._version
  663. def architecture(self):
  664. return self._architecture
  665. def get_option(self, name, default_value=None):
  666. return getattr(self._options, name, default_value)
  667. def set_option_default(self, name, default_value):
  668. return self._options.ensure_value(name, default_value)
  669. @memoized
  670. def path_to_generic_test_expectations_file(self):
  671. return self._filesystem.join(self.layout_tests_dir(), 'TestExpectations')
  672. @memoized
  673. def path_to_test_expectations_file(self):
  674. """Update the test expectations to the passed-in string.
  675. This is used by the rebaselining tool. Raises NotImplementedError
  676. if the port does not use expectations files."""
  677. # FIXME: We need to remove this when we make rebaselining work with multiple files and just generalize expectations_files().
  678. # test_expectations are always in mac/ not mac-leopard/ by convention, hence we use port_name instead of name().
  679. return self._filesystem.join(self._webkit_baseline_path(self.port_name), 'TestExpectations')
  680. def relative_test_filename(self, filename):
  681. """Returns a test_name a relative unix-style path for a filename under the LayoutTests
  682. directory. Ports may legitimately return abspaths here if no relpath makes sense."""
  683. # Ports that run on windows need to override this method to deal with
  684. # filenames with backslashes in them.
  685. if filename.startswith(self.layout_tests_dir()):
  686. return self.host.filesystem.relpath(filename, self.layout_tests_dir())
  687. else:
  688. return self.host.filesystem.abspath(filename)
  689. @memoized
  690. def abspath_for_test(self, test_name):
  691. """Returns the full path to the file for a given test name. This is the
  692. inverse of relative_test_filename()."""
  693. return self._filesystem.join(self.layout_tests_dir(), test_name)
  694. def results_directory(self):
  695. """Absolute path to the place to store the test results (uses --results-directory)."""
  696. if not self._results_directory:
  697. option_val = self.get_option('results_directory') or self.default_results_directory()
  698. self._results_directory = self._filesystem.abspath(option_val)
  699. return self._results_directory
  700. def perf_results_directory(self):
  701. return self._build_path()
  702. def default_results_directory(self):
  703. """Absolute path to the default place to store the test results."""
  704. # Results are store relative to the built products to make it easy
  705. # to have multiple copies of webkit checked out and built.
  706. return self._build_path('layout-test-results')
  707. def setup_test_run(self):
  708. """Perform port-specific work at the beginning of a test run."""
  709. pass
  710. def clean_up_test_run(self):
  711. """Perform port-specific work at the end of a test run."""
  712. if self._image_differ:
  713. self._image_differ.stop()
  714. self._image_differ = None
  715. # FIXME: os.environ access should be moved to onto a common/system class to be more easily mockable.
  716. def _value_or_default_from_environ(self, name, default=None):
  717. if name in os.environ:
  718. return os.environ[name]
  719. return default
  720. def _copy_value_from_environ_if_set(self, clean_env, name):
  721. if name in os.environ:
  722. clean_env[name] = os.environ[name]
  723. def setup_environ_for_server(self, server_name=None):
  724. # We intentionally copy only a subset of os.environ when
  725. # launching subprocesses to ensure consistent test results.
  726. clean_env = {}
  727. variables_to_copy = [
  728. # For Linux:
  729. 'XAUTHORITY',
  730. 'HOME',
  731. 'LANG',
  732. 'LD_LIBRARY_PATH',
  733. 'DBUS_SESSION_BUS_ADDRESS',
  734. 'XDG_DATA_DIRS',
  735. # Darwin:
  736. 'DYLD_LIBRARY_PATH',
  737. 'HOME',
  738. # CYGWIN:
  739. 'HOMEDRIVE',
  740. 'HOMEPATH',
  741. '_NT_SYMBOL_PATH',
  742. # Windows:
  743. 'PATH',
  744. # Most ports (?):
  745. 'WEBKIT_TESTFONTS',
  746. 'WEBKITOUTPUTDIR',
  747. # Chromium:
  748. 'CHROME_DEVEL_SANDBOX',
  749. ]
  750. for variable in variables_to_copy:
  751. self._copy_value_from_environ_if_set(clean_env, variable)
  752. # For Linux:
  753. clean_env['DISPLAY'] = self._value_or_default_from_environ('DISPLAY', ':1')
  754. for string_variable in self.get_option('additional_env_var', []):
  755. [name, value] = string_variable.split('=', 1)
  756. clean_env[name] = value
  757. return clean_env
  758. def show_results_html_file(self, results_filename):
  759. """This routine should display the HTML file pointed at by
  760. results_filename in a users' browser."""
  761. return self.host.user.open_url(path.abspath_to_uri(self.host.platform, results_filename))
  762. def create_driver(self, worker_number, no_timeout=False):
  763. """Return a newly created Driver subclass for starting/stopping the test driver."""
  764. return driver.DriverProxy(self, worker_number, self._driver_class(), pixel_tests=self.get_option('pixel_tests'), no_timeout=no_timeout)
  765. def start_helper(self):
  766. """If a port needs to reconfigure graphics settings or do other
  767. things to ensure a known test configuration, it should override this
  768. method."""
  769. pass
  770. def start_http_server(self, additional_dirs=None, number_of_servers=None):
  771. """Start a web server. Raise an error if it can't start or is already running.
  772. Ports can stub this out if they don't need a web server to be running."""
  773. assert not self._http_server, 'Already running an http server.'
  774. if self._uses_apache():
  775. server = apache_http_server.LayoutTestApacheHttpd(self, self.results_directory(), additional_dirs=additional_dirs, number_of_servers=number_of_servers)
  776. else:
  777. server = http_server.Lighttpd(self, self.results_directory(), additional_dirs=additional_dirs, number_of_servers=number_of_servers)
  778. server.start()
  779. self._http_server = server
  780. def start_websocket_server(self):
  781. """Start a web server. Raise an error if it can't start or is already running.
  782. Ports can stub this out if they don't need a websocket server to be running."""
  783. assert not self._websocket_server, 'Already running a websocket server.'
  784. server = websocket_server.PyWebSocket(self, self.results_directory())
  785. server.start()
  786. self._websocket_server = server
  787. def http_server_supports_ipv6(self):
  788. # Cygwin is the only platform to still use Apache 1.3, which only supports IPV4.
  789. # Once it moves to Apache 2, we can drop this method altogether.
  790. if self.host.platform.is_cygwin():
  791. return False
  792. return True
  793. def acquire_http_lock(self):
  794. self._http_lock = http_lock.HttpLock(None, filesystem=self._filesystem, executive=self._executive)
  795. self._http_lock.wait_for_httpd_lock()
  796. def stop_helper(self):
  797. """Shut down the test helper if it is running. Do nothing if
  798. it isn't, or it isn't available. If a port overrides start_helper()
  799. it must override this routine as well."""
  800. pass
  801. def stop_http_server(self):
  802. """Shut down the http server if it is running. Do nothing if it isn't."""
  803. if self._http_server:
  804. self._http_server.stop()
  805. self._http_server = None
  806. def stop_websocket_server(self):
  807. """Shut down the websocket server if it is running. Do nothing if it isn't."""
  808. if self._websocket_server:
  809. self._websocket_server.stop()
  810. self._websocket_server = None
  811. def release_http_lock(self):
  812. if self._http_lock:
  813. self._http_lock.cleanup_http_lock()
  814. def exit_code_from_summarized_results(self, unexpected_results):
  815. """Given summarized results, compute the exit code to be returned by new-run-webkit-tests.
  816. Bots turn red when this function returns a non-zero value. By default, return the number of regressions
  817. to avoid turning bots red by flaky failures, unexpected passes, and missing results"""
  818. # Don't turn bots red for flaky failures, unexpected passes, and missing results.
  819. return unexpected_results['num_regressions']
  820. #
  821. # TEST EXPECTATION-RELATED METHODS
  822. #
  823. def test_configuration(self):
  824. """Returns the current TestConfiguration for the port."""
  825. if not self._test_configuration:
  826. self._test_configuration = TestConfiguration(self._version, self._architecture, self._options.configuration.lower())
  827. return self._test_configuration
  828. # FIXME: Belongs on a Platform object.
  829. @memoized
  830. def all_test_configurations(self):
  831. """Returns a list of TestConfiguration instances, representing all available
  832. test configurations for this port."""
  833. return self._generate_all_test_configurations()
  834. # FIXME: Belongs on a Platform object.
  835. def configuration_specifier_macros(self):
  836. """Ports may provide a way to abbreviate configuration specifiers to conveniently
  837. refer to them as one term or alias specific values to more generic ones. For example:
  838. (xp, vista, win7) -> win # Abbreviate all Windows versions into one namesake.
  839. (lucid) -> linux # Change specific name of the Linux distro to a more generic term.
  840. Returns a dictionary, each key representing a macro term ('win', for example),
  841. and value being a list of valid configuration specifiers (such as ['xp', 'vista', 'win7'])."""
  842. return {}
  843. def all_baseline_variants(self):
  844. """Returns a list of platform names sufficient to cover all the baselines.
  845. The list should be sorted so that a later platform will reuse
  846. an earlier platform's baselines if they are the same (e.g.,
  847. 'snowleopard' should precede 'leopard')."""
  848. raise NotImplementedError
  849. def uses_test_expectations_file(self):
  850. # This is different from checking test_expectations() is None, because
  851. # some ports have Skipped files which are returned as part of test_expectations().
  852. return self._filesystem.exists(self.path_to_test_expectations_file())
  853. def warn_if_bug_missing_in_test_expectations(self):
  854. return False
  855. def expectations_dict(self):
  856. """Returns an OrderedDict of name -> expectations strings.
  857. The names are expected to be (but not required to be) paths in the filesystem.
  858. If the name is a path, the file can be considered updatable for things like rebaselining,
  859. so don't use names that are paths if they're not paths.
  860. Generally speaking the ordering should be files in the filesystem in cascade order
  861. (TestExpectations followed by Skipped, if the port honors both formats),
  862. then any built-in expectations (e.g., from compile-time exclusions), then --additional-expectations options."""
  863. # FIXME: rename this to test_expectations() once all the callers are updated to know about the ordered dict.
  864. expectations = OrderedDict()
  865. for path in self.expectations_files():
  866. if self._filesystem.exists(path):
  867. expectations[path] = self._filesystem.read_text_file(path)
  868. for path in self.get_option('additional_expectations', []):
  869. expanded_path = self._filesystem.expanduser(path)
  870. if self._filesystem.exists(expanded_path):
  871. _log.debug("reading additional_expectations from path '%s'" % path)
  872. expectations[path] = self._filesystem.read_text_file(expanded_path)
  873. else:
  874. _log.warning("additional_expectations path '%s' does not exist" % path)
  875. return expectations
  876. def _port_specific_expectations_files(self):
  877. # Unlike baseline_search_path, we only want to search [WK2-PORT, PORT-VERSION, PORT] and any directories
  878. # included via --additional-platform-directory, not the full casade.
  879. search_paths = [self.port_name]
  880. non_wk2_name = self.name().replace('-wk2', '')
  881. if non_wk2_name != self.port_name:
  882. search_paths.append(non_wk2_name)
  883. if self.get_option('webkit_test_runner'):
  884. # Because nearly all of the skipped tests for WebKit 2 are due to cross-platform
  885. # issues, all wk2 ports share a skipped list under platform/wk2.
  886. search_paths.extend(["wk2", self._wk2_port_name()])
  887. search_paths.extend(self.get_option("additional_platform_directory", []))
  888. return [self._filesystem.join(self._webkit_baseline_path(d), 'TestExpectations') for d in search_paths]
  889. def expectations_files(self):
  890. return [self.path_to_generic_test_expectations_file()] + self._port_specific_expectations_files()
  891. def repository_paths(self):
  892. """Returns a list of (repository_name, repository_path) tuples of its depending code base.
  893. By default it returns a list that only contains a ('WebKit', <webkitRepositoryPath>) tuple."""
  894. # We use LayoutTest directory here because webkit_base isn't a part of WebKit repository in Chromium port
  895. # where turnk isn't checked out as a whole.
  896. return [('WebKit', self.layout_tests_dir())]
  897. _WDIFF_DEL = '##WDIFF_DEL##'
  898. _WDIFF_ADD = '##WDIFF_ADD##'
  899. _WDIFF_END = '##WDIFF_END##'
  900. def _format_wdiff_output_as_html(self, wdiff):
  901. wdiff = cgi.escape(wdiff)
  902. wdiff = wdiff.replace(self._WDIFF_DEL, "<span class=del>")
  903. wdiff = wdiff.replace(self._WDIFF_ADD, "<span class=add>")
  904. wdiff = wdiff.replace(self._WDIFF_END, "</span>")
  905. html = "<head><style>.del { background: #faa; } "
  906. html += ".add { background: #afa; }</style></head>"
  907. html += "<pre>%s</pre>" % wdiff
  908. return html
  909. def _wdiff_command(self, actual_filename, expected_filename):
  910. executable = self._path_to_wdiff()
  911. return [executable,
  912. "--start-delete=%s" % self._WDIFF_DEL,
  913. "--end-delete=%s" % self._WDIFF_END,
  914. "--start-insert=%s" % self._WDIFF_ADD,
  915. "--end-insert=%s" % self._WDIFF_END,
  916. actual_filename,
  917. expected_filename]
  918. @staticmethod
  919. def _handle_wdiff_error(script_error):
  920. # Exit 1 means the files differed, any other exit code is an error.
  921. if script_error.exit_code != 1:
  922. return
  923. def _run_wdiff(self, actual_filename, expected_filename):
  924. """Runs wdiff and may throw exceptions.
  925. This is mostly a hook for unit testing."""
  926. # Diffs are treated as binary as they may include multiple files
  927. # with conflicting encodings. Thus we do not decode the output.
  928. command = self._wdiff_command(actual_filename, expected_filename)
  929. wdiff = self._executive.run_command(command, decode_output=False,
  930. error_handler=self._handle_wdiff_error)
  931. return self._format_wdiff_output_as_html(wdiff)
  932. def wdiff_text(self, actual_filename, expected_filename):
  933. """Returns a string of HTML indicating the word-level diff of the
  934. contents of the two filenames. Returns an empty string if word-level
  935. diffing isn't available."""
  936. if not self.wdiff_available():
  937. return ""
  938. try:
  939. # It's possible to raise a ScriptError we pass wdiff invalid paths.
  940. return self._run_wdiff(actual_filename, expected_filename)
  941. except OSError, e:
  942. if e.errno in [errno.ENOENT, errno.EACCES, errno.ECHILD]:
  943. # Silently ignore cases where wdiff is missing.
  944. self._wdiff_available = False
  945. return ""
  946. raise
  947. # This is a class variable so we can test error output easily.
  948. _pretty_patch_error_html = "Failed to run PrettyPatch, see error log."
  949. def pretty_patch_text(self, diff_path):
  950. if self._pretty_patch_available is None:
  951. self._pretty_patch_available = self.check_pretty_patch(logging=False)
  952. if not self._pretty_patch_available:
  953. return self._pretty_patch_error_html
  954. command = ("ruby", "-I", self._filesystem.dirname(self._pretty_patch_path),
  955. self._pretty_patch_path, diff_path)
  956. try:
  957. # Diffs are treated as binary (we pass decode_output=False) as they
  958. # may contain multiple files of conflicting encodings.
  959. return self._executive.run_command(command, decode_output=False)
  960. except OSError, e:
  961. # If the system is missing ruby log the error and stop trying.
  962. self._pretty_patch_available = False
  963. _log.error("Failed to run PrettyPatch (%s): %s" % (command, e))
  964. return self._pretty_patch_error_html
  965. except ScriptError, e:
  966. # If ruby failed to run for some reason, log the command
  967. # output and stop trying.
  968. self._pretty_patch_available = False
  969. _log.error("Failed to run PrettyPatch (%s):\n%s" % (command, e.message_with_output()))
  970. return self._pretty_patch_error_html
  971. def default_configuration(self):
  972. return self._config.default_configuration()
  973. #
  974. # PROTECTED ROUTINES
  975. #
  976. # The routines below should only be called by routines in this class
  977. # or any of its subclasses.
  978. #
  979. def _uses_apache(self):
  980. return True
  981. # FIXME: This does not belong on the port object.
  982. @memoized
  983. def _path_to_apache(self):
  984. """Returns the full path to the apache binary.
  985. This is needed only by ports that use the apache_http_server module."""
  986. # The Apache binary path can vary depending on OS and distribution
  987. # See http://wiki.apache.org/httpd/DistrosDefaultLayout
  988. for path in ["/usr/sbin/httpd", "/usr/sbin/apache2"]:
  989. if self._filesystem.exists(path):
  990. return path
  991. _log.error("Could not find apache. Not installed or unknown path.")
  992. return None
  993. # FIXME: This belongs on some platform abstraction instead of Port.
  994. def _is_redhat_based(self):
  995. return self._filesystem.exists('/etc/redhat-release')
  996. def _is_debian_based(self):
  997. return self._filesystem.exists('/etc/debian_version')
  998. def _is_arch_based(self):
  999. return self._filesystem.exists('/etc/arch-release')
  1000. def _apache_version(self):
  1001. config = self._executive.run_command([self._path_to_apache(), '-v'])
  1002. return re.sub(r'(?:.|\n)*Server version: Apache/(\d+\.\d+)(?:.|\n)*', r'\1', config)
  1003. # We pass sys_platform into this method to make it easy to unit test.
  1004. def _apache_config_file_name_for_platform(self, sys_platform):
  1005. if sys_platform == 'cygwin':
  1006. return 'cygwin-httpd.conf' # CYGWIN is the only platform to still use Apache 1.3.
  1007. if sys_platform.startswith('linux'):
  1008. if self._is_redhat_based():
  1009. return 'fedora-httpd-' + self._apache_version() + '.conf'
  1010. if self._is_debian_based():
  1011. return 'apache2-debian-httpd.conf'
  1012. if self._is_arch_based():
  1013. return 'archlinux-httpd.conf'
  1014. # All platforms use apache2 except for CYGWIN (and Mac OS X Tiger and prior, which we no longer support).
  1015. return "apache2-httpd.conf"
  1016. def _path_to_apache_config_file(self):
  1017. """Returns the full path to the apache configuration file.
  1018. If the WEBKIT_HTTP_SERVER_CONF_PATH environment variable is set, its
  1019. contents will be used instead.
  1020. This is needed only by ports that use the apache_http_server module."""
  1021. config_file_from_env = os.environ.get('WEBKIT_HTTP_SERVER_CONF_PATH')
  1022. if config_file_from_env:
  1023. if not self._filesystem.exists(config_file_from_env):
  1024. raise IOError('%s was not found on the system' % config_file_from_env)
  1025. return config_file_from_env
  1026. config_file_name = self._apache_config_file_name_for_platform(sys.platform)
  1027. return self._filesystem.join(self.layout_tests_dir(), 'http', 'conf', config_file_name)
  1028. def _build_path(self, *comps):
  1029. root_directory = self.get_option('root')
  1030. if not root_directory:
  1031. build_directory = self.get_option('build_directory')
  1032. if build_directory:
  1033. root_directory = self._filesystem.join(build_directory, self.get_option('configuration'))
  1034. else:
  1035. root_directory = self._config.build_directory(self.get_option('configuration'))
  1036. # Set --root so that we can pass this to subprocesses and avoid making the
  1037. # slow call to config.build_directory() N times in each worker.
  1038. # FIXME: This is like @memoized, but more annoying and fragile; there should be another
  1039. # way to propagate values without mutating the options list.
  1040. self.set_option_default('root', root_directory)
  1041. return self._filesystem.join(self._filesystem.abspath(root_directory), *comps)
  1042. def _path_to_driver(self, configuration=None):
  1043. """Returns the full path to the test driver (DumpRenderTree)."""
  1044. return self._build_path(self.driver_name())
  1045. def _driver_tempdir(self):
  1046. return self._filesystem.mkdtemp(prefix='%s-' % self.driver_name())
  1047. def _driver_tempdir_for_environment(self):
  1048. return self._driver_tempdir()
  1049. def _path_to_webcore_library(self):
  1050. """Returns the full path to a built copy of WebCore."""
  1051. return None
  1052. def _path_to_helper(self):
  1053. """Returns the full path to the layout_test_helper binary, which
  1054. is used to help configure the system for the test run, or None
  1055. if no helper is needed.
  1056. This is likely only used by start/stop_helper()."""
  1057. return None
  1058. def _path_to_image_diff(self):
  1059. """Returns the full path to the image_diff binary, or None if it is not available.
  1060. This is likely used only by diff_image()"""
  1061. return self._build_path('ImageDiff')
  1062. def _path_to_lighttpd(self):
  1063. """Returns the path to the LigHTTPd binary.
  1064. This is needed only by ports that use the http_server.py module."""
  1065. raise NotImplementedError('Port._path_to_lighttpd')
  1066. def _path_to_lighttpd_modules(self):
  1067. """Returns the path to the LigHTTPd modules directory.
  1068. This is needed only by ports that use the http_server.py module."""
  1069. raise NotImplementedError('Port._path_to_lighttpd_modules')
  1070. def _path_to_lighttpd_php(self):
  1071. """Returns the path to the LigHTTPd PHP executable.
  1072. This is needed only by ports that use the http_server.py module."""
  1073. raise NotImplementedError('Port._path_to_lighttpd_php')
  1074. @memoized
  1075. def _path_to_wdiff(self):
  1076. """Returns the full path to the wdiff binary, or None if it is not available.
  1077. This is likely used only by wdiff_text()"""
  1078. for path in ("/usr/bin/wdiff", "/usr/bin/dwdiff"):
  1079. if self._filesystem.exists(path):
  1080. return path
  1081. return None
  1082. def _webkit_baseline_path(self, platform):
  1083. """Return the full path to the top of the baseline tree for a
  1084. given platform."""
  1085. return self._filesystem.join(self.layout_tests_dir(), 'platform', platform)
  1086. # FIXME: Belongs on a Platform object.
  1087. def _generate_all_test_configurations(self):
  1088. """Generates a list of TestConfiguration instances, representing configurations
  1089. for a platform across all OSes, architectures, build and graphics types."""
  1090. raise NotImplementedError('Port._generate_test_configurations')
  1091. def _driver_class(self):
  1092. """Returns the port's driver implementation."""
  1093. return driver.Driver
  1094. def _get_crash_log(self, name, pid, stdout, stderr, newer_than):
  1095. name_str = name or '<unknown process name>'
  1096. pid_str = str(pid or '<unknown>')
  1097. stdout_lines = (stdout or '<empty>').decode('utf8', 'replace').splitlines()
  1098. stderr_lines = (stderr or '<empty>').decode('utf8', 'replace').splitlines()
  1099. return (stderr, 'crash log for %s (pid %s):\n%s\n%s\n' % (name_str, pid_str,
  1100. '\n'.join(('STDOUT: ' + l) for l in stdout_lines),
  1101. '\n'.join(('STDERR: ' + l) for l in stderr_lines)))
  1102. def look_for_new_crash_logs(self, crashed_processes, start_time):
  1103. pass
  1104. def look_for_new_samples(self, unresponsive_processes, start_time):
  1105. pass
  1106. def sample_process(self, name, pid):
  1107. pass
  1108. def virtual_test_suites(self):
  1109. return []
  1110. def find_system_pid(self, name, pid):
  1111. # This is only overridden on Windows
  1112. return pid
  1113. @memoized
  1114. def populated_virtual_test_suites(self):
  1115. suites = self.virtual_test_suites()
  1116. # Sanity-check the suites to make sure they don't point to other suites.
  1117. suite_dirs = [suite.name for suite in suites]
  1118. for suite in suites:
  1119. assert suite.base not in suite_dirs
  1120. for suite in suites:
  1121. base_tests = self._real_tests([suite.base])
  1122. suite.tests = {}
  1123. for test in base_tests:
  1124. suite.tests[test.replace(suite.base, suite.name, 1)] = test
  1125. return suites
  1126. def _virtual_tests(self, paths, suites):
  1127. virtual_tests = list()
  1128. for suite in suites:
  1129. if paths:
  1130. for test in suite.tests:
  1131. if any(test.startswith(p) for p in paths):
  1132. virtual_tests.append(test)
  1133. else:
  1134. virtual_tests.extend(suite.tests.keys())
  1135. return virtual_tests
  1136. def lookup_virtual_test_base(self, test_name):
  1137. for suite in self.populated_virtual_test_suites():
  1138. if test_name.startswith(suite.name):
  1139. return test_name.replace(suite.name, suite.base, 1)
  1140. return None
  1141. def lookup_virtual_test_args(self, test_name):
  1142. for suite in self.populated_virtual_test_suites():
  1143. if test_name.startswith(suite.name):
  1144. return suite.args
  1145. return []
  1146. def should_run_as_pixel_test(self, test_input):
  1147. if not self._options.pixel_tests:
  1148. return False
  1149. if self._options.pixel_test_directories:
  1150. return any(test_input.test_name.startswith(directory) for directory in self._options.pixel_test_directories)
  1151. return self._should_run_as_pixel_test(test_input)
  1152. def _should_run_as_pixel_test(self, test_input):
  1153. # Default behavior is to allow all test to run as pixel tests if --pixel-tests is on and
  1154. # --pixel-test-directory is not specified.
  1155. return True
  1156. # FIXME: Eventually we should standarize port naming, and make this method smart enough
  1157. # to use for all port configurations (including architectures, graphics types, etc).
  1158. def _port_flag_for_scripts(self):
  1159. # This is overrriden by ports which need a flag passed to scripts to distinguish the use of that port.
  1160. # For example --qt on linux, since a user might have both Gtk and Qt libraries installed.
  1161. return None
  1162. # This is modeled after webkitdirs.pm argumentsForConfiguration() from old-run-webkit-tests
  1163. def _arguments_for_configuration(self):
  1164. config_args = []
  1165. config_args.append(self._config.flag_for_configuration(self.get_option('configuration')))
  1166. # FIXME: We may need to add support for passing --32-bit like old-run-webkit-tests had.
  1167. port_flag = self._port_flag_for_scripts()
  1168. if port_flag:
  1169. config_args.append(port_flag)
  1170. return config_args
  1171. def _run_script(self, script_name, args=None, include_configuration_arguments=True, decode_output=True, env=None):
  1172. run_script_command = [self.path_to_script(script_name)]
  1173. if include_configuration_arguments:
  1174. run_script_command.extend(self._arguments_for_configuration())
  1175. if args:
  1176. run_script_command.extend(args)
  1177. output = self._executive.run_command(run_script_command, cwd=self.webkit_base(), decode_output=decode_output, env=env)
  1178. _log.debug('Output of %s:\n%s' % (run_script_command, output))
  1179. return output
  1180. def _build_driver(self):
  1181. environment = self.host.copy_current_environment()
  1182. environment.disable_gcc_smartquotes()
  1183. env = environment.to_dictionary()
  1184. # FIXME: We build both DumpRenderTree and WebKitTestRunner for
  1185. # WebKitTestRunner runs because DumpRenderTree still includes
  1186. # the DumpRenderTreeSupport module and the TestNetscapePlugin.
  1187. # These two projects should be factored out into their own
  1188. # projects.
  1189. try:
  1190. self._run_script("build-dumprendertree", args=self._build_driver_flags(), env=env)
  1191. if self.get_option('webkit_test_runner'):
  1192. self._run_script("build-webkittestrunner", args=self._build_driver_flags(), env=env)
  1193. except ScriptError, e:
  1194. _log.error(e.message_with_output(output_limit=None))
  1195. return False
  1196. return True
  1197. def _build_driver_flags(self):
  1198. return []
  1199. def test_search_path(self):
  1200. return self.baseline_search_path()
  1201. def _tests_for_other_platforms(self):
  1202. # By default we will skip any directory under LayoutTests/platform
  1203. # that isn't in our baseline search path (this mirrors what
  1204. # old-run-webkit-tests does in findTestsToRun()).
  1205. # Note this returns LayoutTests/platform/*, not platform/*/*.
  1206. entries = self._filesystem.glob(self._webkit_baseline_path('*'))
  1207. dirs_to_skip = []
  1208. for entry in entries:
  1209. if self._filesystem.isdir(entry) and entry not in self.test_search_path():
  1210. basename = self._filesystem.basename(entry)
  1211. dirs_to_skip.append('platform/%s' % basename)
  1212. return dirs_to_skip
  1213. def _runtime_feature_list(self):
  1214. """If a port makes certain features available only through runtime flags, it can override this routine to indicate which ones are available."""
  1215. return None
  1216. def nm_command(self):
  1217. return 'nm'
  1218. def _modules_to_search_for_symbols(self):
  1219. path = self._path_to_webcore_library()
  1220. if path:
  1221. return [path]
  1222. return []
  1223. def _symbols_string(self):
  1224. symbols = ''
  1225. for path_to_module in self._modules_to_search_for_symbols():
  1226. try:
  1227. symbols += self._executive.run_command([self.nm_command(), path_to_module], error_handler=self._executive.ignore_error)
  1228. except OSError, e:
  1229. _log.warn("Failed to run nm: %s. Can't determine supported features correctly." % e)
  1230. return symbols
  1231. # Ports which use run-time feature detection should define this method and return
  1232. # a dictionary mapping from Feature Names to skipped directoires. NRWT will
  1233. # run DumpRenderTree --print-supported-features and parse the output.
  1234. # If the Feature Names are not found in the output, the corresponding directories
  1235. # will be skipped.
  1236. def _missing_feature_to_skipped_tests(self):
  1237. """Return the supported feature dictionary. Keys are feature names and values
  1238. are the lists of directories to skip if the feature name is not matched."""
  1239. # FIXME: This list matches WebKitWin and should be moved onto the Win port.
  1240. return {
  1241. "Accelerated Compositing": ["compositing"],
  1242. "3D Rendering": ["animations/3d", "transforms/3d"],
  1243. }
  1244. def _has_test_in_directories(self, directory_lists, test_list):
  1245. if not test_list:
  1246. return False
  1247. directories = itertools.chain.from_iterable(directory_lists)
  1248. for directory, test in itertools.product(directories, test_list):
  1249. if test.startswith(directory):
  1250. return True
  1251. return False
  1252. def _skipped_tests_for_unsupported_features(self, test_list):
  1253. # Only check the runtime feature list of there are tests in the test_list that might get skipped.
  1254. # This is a performance optimization to avoid the subprocess call to DRT.
  1255. # If the port supports runtime feature detection, disable any tests
  1256. # for features missing from the runtime feature list.
  1257. # If _runtime_feature_list returns a non-None value, then prefer
  1258. # runtime feature detection over static feature detection.
  1259. if self._has_test_in_directories(self._missing_feature_to_skipped_tests().values(), test_list):
  1260. supported_feature_list = self._runtime_feature_list()
  1261. if supported_feature_list is not None:
  1262. return reduce(operator.add, [directories for feature, directories in self._missing_feature_to_skipped_tests().items() if feature not in supported_feature_list])
  1263. return []
  1264. def _wk2_port_name(self):
  1265. # By current convention, the WebKit2 name is always mac-wk2, win-wk2, not mac-leopard-wk2, etc,
  1266. # except for Qt because WebKit2 is only supported by Qt 5.0 (therefore: qt-5.0-wk2).
  1267. return "%s-wk2" % self.port_name
  1268. class VirtualTestSuite(object):
  1269. def __init__(self, name, base, args, tests=None):
  1270. self.name = name
  1271. self.base = base
  1272. self.args = args
  1273. self.tests = tests or set()
  1274. def __repr__(self):
  1275. return "VirtualTestSuite('%s', '%s', %s)" % (self.name, self.base, self.args)