initial import
[vuplus_webkit] / Tools / Scripts / webkitpy / layout_tests / servers / http_server_integrationtest.py
1 # Copyright (C) 2011 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 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 """Integration tests for the new-run-webkit-httpd and new-run-webkit-websocketserver scripts"""
30
31 # FIXME: Rename this file to something more descriptive.
32
33 import errno
34 import os
35 import socket
36 import subprocess
37 import sys
38 import tempfile
39 import unittest
40
41 from webkitpy.layout_tests.port import port_testcase
42
43
44 class BaseTest(unittest.TestCase):
45     """Basic framework for script tests."""
46     HOST = 'localhost'
47
48     # Override in actual test classes.
49     PORTS = None
50     SCRIPT_NAME = None
51
52     def assert_servers_are_down(self, ports=None):
53         ports = ports or self.PORTS
54         for port in ports:
55             try:
56                 test_socket = socket.socket()
57                 test_socket.connect((self.HOST, port))
58                 self.fail()
59             except IOError, e:
60                 self.assertTrue(e.errno in (errno.ECONNREFUSED, errno.ECONNRESET))
61             finally:
62                 test_socket.close()
63
64     def assert_servers_are_up(self, ports=None):
65         ports = ports or self.PORTS
66         for port in ports:
67             try:
68                 test_socket = socket.socket()
69                 test_socket.connect((self.HOST, port))
70             except IOError, e:
71                 self.fail('failed to connect to %s:%d' % (self.HOST, port))
72             finally:
73                 test_socket.close()
74
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)
79
80     def integration_test_server__normal(self):
81         if not self.SCRIPT_NAME:
82             return
83
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()
89
90     def integration_test_server__fails(self):
91         if not self.SCRIPT_NAME:
92             return
93
94         # Test that if a port isn't available, the call fails.
95         for port_number in self.PORTS:
96             test_socket = socket.socket()
97             try:
98                 try:
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)))
103                     raise
104                 self.assertEquals(self.run_script(['--server', 'start']), 1)
105             finally:
106                 self.run_script(['--server', 'stop'])
107                 test_socket.close()
108
109         # Test that calling stop() twice is harmless.
110         self.assertEquals(self.run_script(['--server', 'stop']), 0)
111
112     def maybe_make_dir(self, *comps):
113         try:
114             os.makedirs(os.path.join(*comps))
115         except OSError, e:
116             if e.errno != errno.EEXIST:
117                 raise
118
119     def integration_test_port_and_root(self):
120         if not self.SCRIPT_NAME:
121             return
122
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')
127
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])
133
134
135 class HTTPServerTest(BaseTest):
136     """Tests that new-run-webkit-http must pass."""
137
138     PORTS = (8000, 8080, 8443)
139     SCRIPT_NAME = 'new-run-webkit-httpd'
140
141
142 class WebsocketserverTest(BaseTest):
143     """Tests that new-run-webkit-websocketserver must pass."""
144
145     # FIXME: test TLS at some point?
146     PORTS = (8880, )
147     SCRIPT_NAME = 'new-run-webkit-websocketserver'
148
149
150 if __name__ == '__main__':
151     port_testcase.main()