1 # Copyright (C) 2011 Google Inc. All rights reserved.
3 # Redistribution and use in source and binary forms, with or without
4 # modification, are permitted provided that the following conditions are
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
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.
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.
29 """Integration tests for the new-run-webkit-httpd and new-run-webkit-websocketserver scripts"""
31 # FIXME: Rename this file to something more descriptive.
41 from webkitpy.layout_tests.port import port_testcase
44 class BaseTest(unittest.TestCase):
45 """Basic framework for script tests."""
48 # Override in actual test classes.
52 def assert_servers_are_down(self, ports=None):
53 ports = ports or self.PORTS
56 test_socket = socket.socket()
57 test_socket.connect((self.HOST, port))
60 self.assertTrue(e.errno in (errno.ECONNREFUSED, errno.ECONNRESET))
64 def assert_servers_are_up(self, ports=None):
65 ports = ports or self.PORTS
68 test_socket = socket.socket()
69 test_socket.connect((self.HOST, port))
71 self.fail('failed to connect to %s:%d' % (self.HOST, port))
75 def run_script(self, args):
76 script_dir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
77 script_path = os.path.join(script_dir, self.SCRIPT_NAME)
78 return subprocess.call([sys.executable, script_path] + args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
80 def integration_test_server__normal(self):
81 if not self.SCRIPT_NAME:
84 self.assert_servers_are_down()
85 self.assertEquals(self.run_script(['--server', 'start']), 0)
86 self.assert_servers_are_up()
87 self.assertEquals(self.run_script(['--server', 'stop']), 0)
88 self.assert_servers_are_down()
90 def integration_test_server__fails(self):
91 if not self.SCRIPT_NAME:
94 # Test that if a port isn't available, the call fails.
95 for port_number in self.PORTS:
96 test_socket = socket.socket()
99 test_socket.bind((self.HOST, port_number))
100 except socket.error, e:
101 if e.errno in (errno.EADDRINUSE, errno.EALREADY):
102 self.fail('could not bind to port %d: %s' % (port_number, str(e)))
104 self.assertEquals(self.run_script(['--server', 'start']), 1)
106 self.run_script(['--server', 'stop'])
109 # Test that calling stop() twice is harmless.
110 self.assertEquals(self.run_script(['--server', 'stop']), 0)
112 def maybe_make_dir(self, *comps):
114 os.makedirs(os.path.join(*comps))
116 if e.errno != errno.EEXIST:
119 def integration_test_port_and_root(self):
120 if not self.SCRIPT_NAME:
123 tmpdir = tempfile.mkdtemp(prefix='webkitpytest')
124 self.maybe_make_dir(tmpdir, 'http', 'tests', 'websocket')
125 self.maybe_make_dir(tmpdir, 'fast', 'js', 'resources')
126 self.maybe_make_dir(tmpdir, 'media')
128 self.assert_servers_are_down([18000])
129 self.assertEquals(self.run_script(['--server', 'start', '--port=18000', '--root', tmpdir]), 0)
130 self.assert_servers_are_up([18000])
131 self.assertEquals(self.run_script(['--server', 'stop']), 0)
132 self.assert_servers_are_down([18000])
135 class HTTPServerTest(BaseTest):
136 """Tests that new-run-webkit-http must pass."""
138 PORTS = (8000, 8080, 8443)
139 SCRIPT_NAME = 'new-run-webkit-httpd'
142 class WebsocketserverTest(BaseTest):
143 """Tests that new-run-webkit-websocketserver must pass."""
145 # FIXME: test TLS at some point?
147 SCRIPT_NAME = 'new-run-webkit-websocketserver'
150 if __name__ == '__main__':