Skip to content

Commit

Permalink
Merge pull request #52 from plone/jo-51
Browse files Browse the repository at this point in the history
Enable picking a free port for ZServer layers automatically
  • Loading branch information
jensens authored Oct 3, 2018
2 parents bfc4be8 + 702f3f6 commit 4e6bd26
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 17 deletions.
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

1 comment on commit 4e6bd26

@jenkins-plone-org
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jensens Jenkins CI reporting about code analysis
See the full report here: https://jenkins.plone.org/job/package-plone.testing/111/violations

src/plone/testing/z2.py:98:1: C901 'uninstallProduct' is too complex (15)
src/plone/testing/z2.py:138:19: T000 Todo note found.
src/plone/testing/z2.py:414:5: C901 'Startup.setUpPatches' is too complex (11)
src/plone/testing/z2.py:829:11: T000 Todo note found.
src/plone/testing/z2.py:1137:9: E303 too many blank lines (2)

Follow these instructions to reproduce it locally.

Please sign in to comment.