Update pytest-socket setup so allow_hosts works without enable_socket #104076
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.
Proposed change
EDIT: After creating this draft PR and seeing the CI tests failing I realised a lot of tests are accessing 127.0.0.1 and not reporting errors. This PR then causes them to timeout and fail when they don't immediately get a socket exception. I managed to work around the vscode-pytest issue by using a different test adapter so closing this PR.
I'm currently unable to use the vscode-pytest plug-in to debug tests as it communicates with it's test runner via a socket to localhost, and the way pytest-socket has been configured in HA prevents the connection.
This PR proposes to remove the setup of pytest-socket in core/tests/conftest.py:pytest_runtest_setup() and set it up via pytest.ini instead. The result is that the behaviour becomes the behaviour of pytest_socket:pytest_runtest_setup() which is that --allow-hosts takes precedence over --disable-socket rather than the other way round.
The original PR that enabled pytest-socket is #55516 and included logic that was not yet in the released version so that may have influenced the decision of how to configure it.
I've searched around and no-one else seems to be complaining about this issue on HA so apologies if I've missed something obvious. There is an open issue on vscode-python where it's suggested to use allow_hosts to solve it. Whilst I appreciate this could be viewed as an issue in the vscode-python extension it does seem like the preferred behaviour in HA is to allow 127.0.0.1 anyway as the enable_socket fixture is added to quite a few tests.
N.B. The error I get from the extension is below. In a previous version of the extension it would just hang indefinitely due to a bug in it's error handling:
vscode_pytest.VSCodePytestError: Error attempting to connect to extension communication socket[vscode-pytest]: A test tried to use socket.socket.
Type of change
Additional information
Checklist
black --fast homeassistant tests
)If user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
Updated and included derived files by running:
python3 -m script.hassfest
.requirements_all.txt
.Updated by running
python3 -m script.gen_requirements_all
..coveragerc
.To help with the load of incoming pull requests: