http_server_integrationtest.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. """Integration tests for the new-run-webkit-httpd and new-run-webkit-websocketserver scripts"""
  29. # FIXME: Rename this file to something more descriptive.
  30. import errno
  31. import os
  32. import socket
  33. import subprocess
  34. import sys
  35. import tempfile
  36. import unittest2 as unittest
  37. class BaseTest(unittest.TestCase):
  38. """Basic framework for script tests."""
  39. HOST = 'localhost'
  40. # Override in actual test classes.
  41. PORTS = None
  42. SCRIPT_NAME = None
  43. def assert_servers_are_down(self, ports=None):
  44. ports = ports or self.PORTS
  45. for port in ports:
  46. try:
  47. test_socket = socket.socket()
  48. test_socket.connect((self.HOST, port))
  49. self.fail()
  50. except IOError, e:
  51. self.assertTrue(e.errno in (errno.ECONNREFUSED, errno.ECONNRESET))
  52. finally:
  53. test_socket.close()
  54. def assert_servers_are_up(self, ports=None):
  55. ports = ports or self.PORTS
  56. for port in ports:
  57. try:
  58. test_socket = socket.socket()
  59. test_socket.connect((self.HOST, port))
  60. except IOError, e:
  61. self.fail('failed to connect to %s:%d' % (self.HOST, port))
  62. finally:
  63. test_socket.close()
  64. def run_script(self, args):
  65. script_dir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
  66. script_path = os.path.join(script_dir, self.SCRIPT_NAME)
  67. return subprocess.call([sys.executable, script_path] + args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  68. def integration_test_server__normal(self):
  69. if not self.SCRIPT_NAME:
  70. return
  71. self.assert_servers_are_down()
  72. self.assertEqual(self.run_script(['--server', 'start']), 0)
  73. self.assert_servers_are_up()
  74. self.assertEqual(self.run_script(['--server', 'stop']), 0)
  75. self.assert_servers_are_down()
  76. def integration_test_server__fails(self):
  77. if not self.SCRIPT_NAME:
  78. return
  79. # Test that if a port isn't available, the call fails.
  80. for port_number in self.PORTS:
  81. test_socket = socket.socket()
  82. try:
  83. try:
  84. test_socket.bind((self.HOST, port_number))
  85. except socket.error, e:
  86. if e.errno in (errno.EADDRINUSE, errno.EALREADY):
  87. self.fail('could not bind to port %d: %s' % (port_number, str(e)))
  88. raise
  89. self.assertEqual(self.run_script(['--server', 'start']), 1)
  90. finally:
  91. self.run_script(['--server', 'stop'])
  92. test_socket.close()
  93. # Test that calling stop() twice is harmless.
  94. self.assertEqual(self.run_script(['--server', 'stop']), 0)
  95. def maybe_make_dir(self, *comps):
  96. try:
  97. os.makedirs(os.path.join(*comps))
  98. except OSError, e:
  99. if e.errno != errno.EEXIST:
  100. raise
  101. def integration_test_port_and_root(self):
  102. if not self.SCRIPT_NAME:
  103. return
  104. tmpdir = tempfile.mkdtemp(prefix='webkitpytest')
  105. self.maybe_make_dir(tmpdir, 'http', 'tests', 'websocket')
  106. self.maybe_make_dir(tmpdir, 'fast', 'js', 'resources')
  107. self.maybe_make_dir(tmpdir, 'media')
  108. self.assert_servers_are_down([18000])
  109. self.assertEqual(self.run_script(['--server', 'start', '--port=18000', '--root', tmpdir]), 0)
  110. self.assert_servers_are_up([18000])
  111. self.assertEqual(self.run_script(['--server', 'stop']), 0)
  112. self.assert_servers_are_down([18000])
  113. class HTTPServerTest(BaseTest):
  114. """Tests that new-run-webkit-http must pass."""
  115. PORTS = (8000, 8080, 8443)
  116. SCRIPT_NAME = 'new-run-webkit-httpd'
  117. class WebsocketserverTest(BaseTest):
  118. """Tests that new-run-webkit-websocketserver must pass."""
  119. # FIXME: test TLS at some point?
  120. PORTS = (8880, )
  121. SCRIPT_NAME = 'new-run-webkit-websocketserver'