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