From 8b88a07cd46bfa006bc34b629df1623d4a68e057 Mon Sep 17 00:00:00 2001 From: Steve Kowalik Date: Wed, 5 Feb 2025 13:55:51 +1100 Subject: [PATCH] pytest-server-fixtures: Support large pids in xvfb Since a large number of 64 bit distributions are now using larger PIDs than 65535 by default, and the xvfb fixtures makes the assumption that PIDs are going to be less than that, check what the maximum is, falling back to 65535 if we can't. --- .../pytest_server_fixtures/xvfb.py | 5 ++++- .../tests/integration/test_xvfb_server.py | 22 ++++++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/pytest-server-fixtures/pytest_server_fixtures/xvfb.py b/pytest-server-fixtures/pytest_server_fixtures/xvfb.py index 01d858be..58554c6c 100644 --- a/pytest-server-fixtures/pytest_server_fixtures/xvfb.py +++ b/pytest-server-fixtures/pytest_server_fixtures/xvfb.py @@ -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: + 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) diff --git a/pytest-server-fixtures/tests/integration/test_xvfb_server.py b/pytest-server-fixtures/tests/integration/test_xvfb_server.py index 96d6315f..8ca64247 100644 --- a/pytest-server-fixtures/tests/integration/test_xvfb_server.py +++ b/pytest-server-fixtures/tests/integration/test_xvfb_server.py @@ -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 @@ -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)