xvfbdriver.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. import logging
  29. import os
  30. import re
  31. import time
  32. from webkitpy.port.server_process import ServerProcess
  33. from webkitpy.port.driver import Driver
  34. from webkitpy.common.system.file_lock import FileLock
  35. _log = logging.getLogger(__name__)
  36. class XvfbDriver(Driver):
  37. @staticmethod
  38. def check_xvfb(port):
  39. xvfb_found = port.host.executive.run_command(['which', 'Xvfb'], return_exit_code=True) is 0
  40. if not xvfb_found:
  41. _log.error("No Xvfb found. Cannot run layout tests.")
  42. return xvfb_found
  43. def __init__(self, *args, **kwargs):
  44. Driver.__init__(self, *args, **kwargs)
  45. self._guard_lock = None
  46. self._startup_delay_secs = 1.0
  47. def _next_free_display(self):
  48. running_pids = self._port.host.executive.run_command(['ps', '-eo', 'comm,command'])
  49. reserved_screens = set()
  50. for pid in running_pids.split('\n'):
  51. match = re.match('(X|Xvfb|Xorg)\s+.*\s:(?P<screen_number>\d+)', pid)
  52. if match:
  53. reserved_screens.add(int(match.group('screen_number')))
  54. for i in range(99):
  55. if i not in reserved_screens:
  56. _guard_lock_file = self._port.host.filesystem.join('/tmp', 'WebKitXvfb.lock.%i' % i)
  57. self._guard_lock = self._port.host.make_file_lock(_guard_lock_file)
  58. if self._guard_lock.acquire_lock():
  59. return i
  60. def _start(self, pixel_tests, per_test_args):
  61. self.stop()
  62. # Use even displays for pixel tests and odd ones otherwise. When pixel tests are disabled,
  63. # DriverProxy creates two drivers, one for normal and the other for ref tests. Both have
  64. # the same worker number, so this prevents them from using the same Xvfb instance.
  65. display_id = self._next_free_display()
  66. self._lock_file = "/tmp/.X%d-lock" % display_id
  67. run_xvfb = ["Xvfb", ":%d" % display_id, "-screen", "0", "800x600x24", "-nolisten", "tcp"]
  68. with open(os.devnull, 'w') as devnull:
  69. self._xvfb_process = self._port.host.executive.popen(run_xvfb, stderr=devnull)
  70. # Crashes intend to occur occasionally in the first few tests that are run through each
  71. # worker because the Xvfb display isn't ready yet. Halting execution a bit should avoid that.
  72. time.sleep(self._startup_delay_secs)
  73. server_name = self._port.driver_name()
  74. environment = self._port.setup_environ_for_server(server_name)
  75. # We must do this here because the DISPLAY number depends on _worker_number
  76. environment['DISPLAY'] = ":%d" % display_id
  77. self._driver_tempdir = self._port._filesystem.mkdtemp(prefix='%s-' % self._port.driver_name())
  78. environment['DUMPRENDERTREE_TEMP'] = str(self._driver_tempdir)
  79. environment['LOCAL_RESOURCE_ROOT'] = self._port.layout_tests_dir()
  80. # Currently on WebKit2, there is no API for setting the application
  81. # cache directory. Each worker should have it's own and it should be
  82. # cleaned afterwards, so we set it to inside the temporary folder by
  83. # prepending XDG_CACHE_HOME with DUMPRENDERTREE_TEMP.
  84. environment['XDG_CACHE_HOME'] = self._port.host.filesystem.join(str(self._driver_tempdir), 'appcache')
  85. self._crashed_process_name = None
  86. self._crashed_pid = None
  87. self._server_process = self._port._server_process_constructor(self._port, server_name, self.cmd_line(pixel_tests, per_test_args), environment)
  88. self._server_process.start()
  89. def stop(self):
  90. super(XvfbDriver, self).stop()
  91. if self._guard_lock:
  92. self._guard_lock.release_lock()
  93. self._guard_lock = None
  94. if getattr(self, '_xvfb_process', None):
  95. self._port.host.executive.kill_process(self._xvfb_process.pid)
  96. self._xvfb_process = None
  97. if self._port.host.filesystem.exists(self._lock_file):
  98. self._port.host.filesystem.remove(self._lock_file)