Merge pull request #1006 from normanjaeckel/CheckPort

Check used port, fix #985.
This commit is contained in:
Oskar Hahn 2013-11-06 13:03:40 -08:00
commit 929587971a
2 changed files with 34 additions and 14 deletions

View File

@ -23,6 +23,10 @@ class PortableDirNotWritable(Exception):
pass
class PortIsBlockedError(Exception):
pass
class DatabaseInSettingsError(Exception):
pass
@ -273,22 +277,27 @@ def get_portable_paths(name):
def get_port(address, port):
"""
Returns the port for the server. If port 80 is given, checks if it is
available. If not returns port 8000.
Checks if the port for the server is available and returns it the port. If
it is port 80, try also port 8000.
The argument 'address' should be an IP address. The argument 'port' should
be an integer.
"""
if port == 80:
# test if we can use port 80
s = socket.socket()
try:
s.bind((address, port))
s.listen(-1)
except socket.error:
port = 8000
finally:
s.close()
s = socket.socket()
try:
s.bind((address, port))
s.listen(-1)
except socket.error:
error = True
else:
error = False
finally:
s.close()
if error:
if port == 80:
port = get_port(address, 8000)
else:
raise PortIsBlockedError('Port %d is not available. Try another port using the --port option.' % port)
return port

View File

@ -20,6 +20,7 @@ from openslides.utils.main import (
get_port,
get_portable_paths,
get_user_data_path_values,
PortIsBlockedError,
setup_django_settings_module,
start_browser,
UNIX_VERSION,
@ -71,11 +72,19 @@ class TestFunctions(TestCase):
def test_get_port(self):
class MyException(Exception):
pass
def block_some_ports(mock):
if not 8000 == mock_socket.socket().bind.call_args[0][0][1]:
raise MyException
# Test open port
self.assertEqual(get_port('localhost', 8234), 8234)
# Test blocked ports
with patch('openslides.utils.main.socket') as mock_socket:
mock_socket.error = MyException
mock_socket.socket().listen = MagicMock(side_effect=MyException)
mock_socket.socket().listen = MagicMock(side_effect=block_some_ports)
self.assertEqual(get_port('localhost', 80), 8000)
self.assertRaises(PortIsBlockedError, get_port, 'localhost', 81)
@patch('openslides.utils.main.threading.Thread')
@patch('openslides.utils.main.time')
@ -121,9 +130,11 @@ class TestOtherFunctions(TestCase):
mock_syncdb.assert_called()
mock_runserver.assert_called_with(None, mock_args)
@patch('openslides.__main__.get_port')
@patch('openslides.__main__.run_tornado')
@patch('openslides.__main__.start_browser')
def test_runserver(self, mock_start_browser, mock_run_tornado):
def test_runserver(self, mock_start_browser, mock_run_tornado, mock_get_port):
mock_get_port.return_value = 8000
mock_args = MagicMock()
runserver(settings=None, args=mock_args)
mock_run_tornado.assert_called()