-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
gh-109015: Add test.support.socket_helper.tcp_blackhole() #109016
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
gh-109015: Add test.support.socket_helper.tcp_blackhole()
Skip test_asyncio, test_imaplib and test_socket tests if FreeBSD TCP blackhole is enabled (net.inet.tcp.blackhole=2).
- Loading branch information
commit 43499b75307cd1e1335b524102108ad06d33a5be
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
import os.path | ||
import socket | ||
import sys | ||
import subprocess | ||
import tempfile | ||
import unittest | ||
|
||
|
@@ -277,3 +278,62 @@ def create_unix_domain_name(): | |
""" | ||
return tempfile.mktemp(prefix="test_python_", suffix='.sock', | ||
dir=os.path.curdir) | ||
|
||
|
||
# consider that sysctl values should not change while tests are running | ||
_sysctl_cache = {} | ||
|
||
def _get_sysctl(name): | ||
"""Get a sysctl value as an integer.""" | ||
try: | ||
return _sysctl_cache[name] | ||
except KeyError: | ||
pass | ||
|
||
cmd = ['sysctl', name] | ||
proc = subprocess.run(cmd, | ||
stdout=subprocess.PIPE, | ||
stderr=subprocess.STDOUT, | ||
text=True) | ||
if proc.returncode: | ||
support.print_warning(f'{' '.join(cmd)!r} command failed with ' | ||
f'exit code {proc.returncode}') | ||
# cache the error to only log the warning once | ||
_sysctl_cache[name] = None | ||
return None | ||
output = proc.stdout | ||
|
||
# Parse 'net.inet.tcp.blackhole: 0\n' to get '0' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this comment is leftover |
||
try: | ||
value = output.rstrip().split(':', 1)[-1].strip() | ||
value = int(value) | ||
except Exception as exc: | ||
support.print_warning(f'Failed to parse {' '.join(cmd)!r} ' | ||
f'command output {output!r}: {exc!r}') | ||
# cache the error to only log the warning once | ||
_sysctl_cache[name] = None | ||
return None | ||
|
||
_sysctl_cache[name] = value | ||
return value | ||
|
||
|
||
def tcp_blackhole(): | ||
if not sys.platform.startswith('freebsd'): | ||
return False | ||
|
||
# gh-109015: test if FreeBSD TCP blackhole is enabled | ||
value = _get_sysctl('net.inet.tcp.blackhole') | ||
if value is None: | ||
# don't skip if we fail to get the sysctl value | ||
return False | ||
return (value != 0) | ||
|
||
|
||
def skip_if_tcp_blackhole(test): | ||
"""Decorator skipping test if TCP blackhole is enabled.""" | ||
skip_if = unittest.skipIf( | ||
tcp_blackhole(), | ||
"TCP blackhole is enabled (sysctl net.inet.tcp.blackhole)" | ||
) | ||
return skip_if(test) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
Misc/NEWS.d/next/Tests/2023-09-06-18-27-53.gh-issue-109015.1dS1AQ.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
Fix test_asyncio, test_imaplib and test_socket tests on FreeBSD if the TCP | ||
blackhole is enabled (``sysctl net.inet.tcp.blackhole``). Skip the few tests | ||
which failed with ``ETIMEDOUT`` which such non standard configuration. | ||
Currently, the `FreeBSD GCP image enables TCP and UDP blackhole | ||
<https://reviews.freebsd.org/D41751>`_ (``sysctl net.inet.tcp.blackhole=2`` | ||
and ``sysctl net.inet.udp.blackhole=1``). Patch by Victor Stinner. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sysctl provides a
-n
arg to suppress the name:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting, I didn't know it. It's also supported by Linux (even if right now, this function is only called on FreeBSD ;-)). I updated my PR.