Skip to content

pytest-server-fixtures: Support large pids in xvfb #250

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
5 changes: 4 additions & 1 deletion pytest-server-fixtures/pytest_server_fixtures/xvfb.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ class XvfbServer(object):

def __init__(self):
tmpdir = mkdtemp(prefix='XvfbServer.', dir=Workspace.get_base_tempdir())
for servernum in range(os.getpid(), 65536):
pid_max = 65536
with open('/proc/sys/kernel/pid_max') as pid_max_file:
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this guaranteed to exist on all Linux distributions? We should probably safely fall back to 65536 if the file doesn't exist.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We do fall back, it's set before we open the /proc file. That /proc file was added sometime in 2.5, so it should exist, but it may throw an exception if /proc isn't mounted -- but we probably have bigger problems in that case.

pid_max = int(pid_max_file.read())
for servernum in range(os.getpid(), pid_max):
if os.path.exists('/tmp/.X{0}-lock'.format(servernum)):
continue
self.display = ':' + str(servernum)
Expand Down
22 changes: 21 additions & 1 deletion pytest-server-fixtures/tests/integration/test_xvfb_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from itertools import chain, repeat

try:
from unittest.mock import patch
from unittest.mock import mock_open, patch
except ImportError:
# python 2
from mock import patch
Expand Down Expand Up @@ -69,3 +69,23 @@ def test_handles_unexpected_failure_to_start():
with raises(RuntimeError) as ex:
XvfbServer()
assert 'Failed to start Xvfb' in str(ex)


def test_handles_64_bit_pids():
with patch('os.getpid') as mock_getpid:
# This just has to be larger than 65535
mock_getpid.return_value = 65537
with XvfbServer() as server:
assert server.display


def test_errors_64_bit_pids():
with patch('os.getpid') as mock_getpid:
target = 'pytest_server_fixtures.xvfb.open'
with patch(target, mock_open(read_data='4')) as m:
# This just has to be larger than the returned max PID
mock_getpid.return_value = 7
with raises(RuntimeError) as ex:
XvfbServer()
m.assert_called_once_with('/proc/sys/kernel/pid_max')
assert 'Unable to find a free server number to start Xvfb' in str(ex)