xvfbdriver_unittest.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. # Copyright (C) 2012 Zan Dobersek <zandobersek@gmail.com>
  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 name of Google Inc. 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 unittest2 as unittest
  30. from webkitpy.common.system.filesystem_mock import MockFileSystem
  31. from webkitpy.common.system.executive_mock import MockExecutive2
  32. from webkitpy.common.system.outputcapture import OutputCapture
  33. from webkitpy.common.system.systemhost_mock import MockSystemHost
  34. from webkitpy.port import Port
  35. from webkitpy.port.server_process_mock import MockServerProcess
  36. from webkitpy.port.xvfbdriver import XvfbDriver
  37. from webkitpy.tool.mocktool import MockOptions
  38. _log = logging.getLogger(__name__)
  39. class XvfbDriverTest(unittest.TestCase):
  40. def make_driver(self, worker_number=0, xorg_running=False, executive=None):
  41. port = Port(MockSystemHost(log_executive=True, executive=executive), 'xvfbdrivertestport', options=MockOptions(configuration='Release'))
  42. port._config.build_directory = lambda configuration: "/mock-build"
  43. port._server_process_constructor = MockServerProcess
  44. if xorg_running:
  45. port._executive._running_pids['Xorg'] = 108
  46. driver = XvfbDriver(port, worker_number=worker_number, pixel_tests=True)
  47. driver._startup_delay_secs = 0
  48. return driver
  49. def cleanup_driver(self, driver):
  50. # Setting _xvfb_process member to None is necessary as the Driver object is stopped on deletion,
  51. # killing the Xvfb process if present. Thus, this method should only be called from tests that do not
  52. # intend to test the behavior of XvfbDriver.stop.
  53. driver._xvfb_process = None
  54. def assertDriverStartSuccessful(self, driver, expected_logs, expected_display, pixel_tests=False):
  55. OutputCapture().assert_outputs(self, driver.start, [pixel_tests, []], expected_logs=expected_logs)
  56. self.assertTrue(driver._server_process.started)
  57. self.assertEqual(driver._server_process.env["DISPLAY"], expected_display)
  58. def test_start_no_pixel_tests(self):
  59. driver = self.make_driver()
  60. expected_logs = "MOCK run_command: ['ps', '-eo', 'comm,command'], cwd=None\nMOCK popen: ['Xvfb', ':0', '-screen', '0', '800x600x24', '-nolisten', 'tcp']\n"
  61. self.assertDriverStartSuccessful(driver, expected_logs=expected_logs, expected_display=":0")
  62. self.cleanup_driver(driver)
  63. def test_start_pixel_tests(self):
  64. driver = self.make_driver()
  65. expected_logs = "MOCK run_command: ['ps', '-eo', 'comm,command'], cwd=None\nMOCK popen: ['Xvfb', ':0', '-screen', '0', '800x600x24', '-nolisten', 'tcp']\n"
  66. self.assertDriverStartSuccessful(driver, expected_logs=expected_logs, expected_display=":0", pixel_tests=True)
  67. self.cleanup_driver(driver)
  68. def test_start_arbitrary_worker_number(self):
  69. driver = self.make_driver(worker_number=17)
  70. expected_logs = "MOCK run_command: ['ps', '-eo', 'comm,command'], cwd=None\nMOCK popen: ['Xvfb', ':0', '-screen', '0', '800x600x24', '-nolisten', 'tcp']\n"
  71. self.assertDriverStartSuccessful(driver, expected_logs=expected_logs, expected_display=":0", pixel_tests=True)
  72. self.cleanup_driver(driver)
  73. def test_next_free_display(self):
  74. output = "Xorg /usr/bin/X :0 -auth /var/run/lightdm/root/:0 -nolisten tcp vt7 -novtswitch -background none\nXvfb Xvfb :1 -screen 0 800x600x24 -nolisten tcp"
  75. executive = MockExecutive2(output)
  76. driver = self.make_driver(executive=executive)
  77. self.assertEqual(driver._next_free_display(), 2)
  78. self.cleanup_driver(driver)
  79. output = "X /usr/bin/X :0 vt7 -nolisten tcp -auth /var/run/xauth/A:0-8p7Ybb"
  80. executive = MockExecutive2(output)
  81. driver = self.make_driver(executive=executive)
  82. self.assertEqual(driver._next_free_display(), 1)
  83. self.cleanup_driver(driver)
  84. output = "Xvfb Xvfb :0 -screen 0 800x600x24 -nolisten tcp"
  85. executive = MockExecutive2(output)
  86. driver = self.make_driver(executive=executive)
  87. self.assertEqual(driver._next_free_display(), 1)
  88. self.cleanup_driver(driver)
  89. output = "Xvfb Xvfb :1 -screen 0 800x600x24 -nolisten tcp\nXvfb Xvfb :0 -screen 0 800x600x24 -nolisten tcp\nXvfb Xvfb :3 -screen 0 800x600x24 -nolisten tcp"
  90. executive = MockExecutive2(output)
  91. driver = self.make_driver(executive=executive)
  92. self.assertEqual(driver._next_free_display(), 2)
  93. self.cleanup_driver(driver)
  94. def test_start_next_worker(self):
  95. driver = self.make_driver()
  96. driver._next_free_display = lambda: 0
  97. expected_logs = "MOCK popen: ['Xvfb', ':0', '-screen', '0', '800x600x24', '-nolisten', 'tcp']\n"
  98. self.assertDriverStartSuccessful(driver, expected_logs=expected_logs, expected_display=":0", pixel_tests=True)
  99. self.cleanup_driver(driver)
  100. driver = self.make_driver()
  101. driver._next_free_display = lambda: 3
  102. expected_logs = "MOCK popen: ['Xvfb', ':3', '-screen', '0', '800x600x24', '-nolisten', 'tcp']\n"
  103. self.assertDriverStartSuccessful(driver, expected_logs=expected_logs, expected_display=":3", pixel_tests=True)
  104. self.cleanup_driver(driver)
  105. def test_stop(self):
  106. filesystem = MockFileSystem(files={'/tmp/.X42-lock': '1234\n'})
  107. port = Port(MockSystemHost(log_executive=True, filesystem=filesystem), 'xvfbdrivertestport', options=MockOptions(configuration='Release'))
  108. port._executive.kill_process = lambda x: _log.info("MOCK kill_process pid: " + str(x))
  109. driver = XvfbDriver(port, worker_number=0, pixel_tests=True)
  110. class FakeXvfbProcess(object):
  111. pid = 1234
  112. driver._xvfb_process = FakeXvfbProcess()
  113. driver._lock_file = '/tmp/.X42-lock'
  114. expected_logs = "MOCK kill_process pid: 1234\n"
  115. OutputCapture().assert_outputs(self, driver.stop, [], expected_logs=expected_logs)
  116. self.assertIsNone(driver._xvfb_process)
  117. self.assertFalse(port._filesystem.exists(driver._lock_file))