Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable picking a free port for ZServer layers automatically #52

Merged
merged 1 commit into from
Oct 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ Changelog

Breaking changes:

- *add item here*
- Default to picking a dynamical port for ZServer layers instead of a static
default port.
[Rotonen]

New features:

Expand Down
37 changes: 33 additions & 4 deletions src/plone/testing/z2.py
Original file line number Diff line number Diff line change
Expand Up @@ -973,8 +973,10 @@ class ZServer(Layer):

defaultBases = (STARTUP,)

host = os.environ.get('ZSERVER_HOST', 'localhost')
port = int(os.environ.get('ZSERVER_PORT', 55001))
# Default to 'bindall' (marked by an empty string) from os.socket
host = os.environ.get('ZSERVER_HOST', '')
# Default to letting the OS allocate us a free port (marked by 0)
port = int(os.environ.get('ZSERVER_PORT', 0))
timeout = 5.0
log = None

Expand Down Expand Up @@ -1044,6 +1046,17 @@ def setUpServer(self):

self.zserver = server

# If we dynamically set the host/port, we want to reset it to localhost
# Otherwise this will depend on, for example, the local network setup
if self.host in ('', '0.0.0.0', '127.0.0.1', ):
self.zserver.server_name = 'localhost'

# Refresh the hostname and port in case we dynamically picked them
self.host = self.zserver.server_name
self['host'] = self.host
self.port = self.zserver.server_port
self['port'] = self.port

def tearDownServer(self):
"""Close the ZServer socket
"""
Expand Down Expand Up @@ -1092,8 +1105,10 @@ class FTPServer(ZServer):

defaultBases = (STARTUP,)

host = os.environ.get('FTPSERVER_HOST', 'localhost')
port = int(os.environ.get('FTPSERVER_PORT', 55002))
# Default to 'bindall' (marked by an empty string) from os.socket
host = os.environ.get('FTPSERVER_HOST', '')
# Default to letting the OS allocate us a free port (marked by 0)
port = int(os.environ.get('FTPSERVER_PORT', 0))
threads = 1
timeout = 5.0
log = None
Expand All @@ -1118,6 +1133,20 @@ def setUpServer(self):
port=self.port,
logger_object=zopeLog)


# Refresh the hostname and port in case we dynamically picked them
self.host, self.port = self.ftpServer.socket.getsockname()

# If we dynamically set the host/port, we want to reset it to localhost
# Otherwise this will depend on, for example, the local network setup
if self.host in ('', '0.0.0.0', '127.0.0.1', ):
self.host = 'localhost'
self.ftpServer.hostname = 'localhost'
self.ftpServer.ip = '127.0.0.1'

self['host'] = self.host
self['port'] = self.port

def tearDownServer(self):
"""Close the FTPServer socket
"""
Expand Down
12 changes: 0 additions & 12 deletions src/plone/testing/z2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -467,13 +467,7 @@ The ``ZSERVER`` layer provides a ``FunctionalTesting`` layer that has ``ZSERVER_
After layer setup, the resources ``host`` and ``port`` are available, and indicate where Zope is running.::

>>> host = z2.ZSERVER['host']
>>> host
'localhost'

>>> port = z2.ZSERVER['port']
>>> import os
>>> port == int(os.environ.get('ZSERVER_PORT', 55001))
True

Let's now simulate a test.
Test setup does nothing beyond what the base layers do.::
Expand Down Expand Up @@ -573,13 +567,7 @@ The ``FTP_SERVER`` layer is based on ``FTP_SERVER_FIXTURE``, using the ``Functio
After layer setup, the resources ``host`` and ``port`` are available, and indicate where Zope is running.::

>>> host = z2.FTP_SERVER['host']
>>> host
'localhost'

>>> port = z2.FTP_SERVER['port']
>>> import os
>>> port == int(os.environ.get('FTPSERVER_PORT', 55002))
True

Let's now simulate a test.
Test setup does nothing beyond what the base layers do.::
Expand Down