Skip to content

enabling sockets on 0.4.6 #96

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

Closed
drc38 opened this issue Nov 6, 2021 · 9 comments
Closed

enabling sockets on 0.4.6 #96

drc38 opened this issue Nov 6, 2021 · 9 comments

Comments

@drc38
Copy link

drc38 commented Nov 6, 2021

How do you enable sockets for testing in the latest version? I have tried adding the following to conftest.py:

from pytest_socket import enable_socket

def pytest_runtest_setup():
    """Enable socket for tests - default is to disable."""
    enable_socket()

and the following in the test function:

async def test_cms_responses(hass, socket_enabled):

but still get pytest_socket.SocketConnectBlockedError: raised when the test runs.

@MatthewFlamm
Copy link
Owner

I'm not very familiar but have you tried the built in fixture?

@drc38
Copy link
Author

drc38 commented Nov 6, 2021

I'm not very familiar but have you tried the built in fixture?

How do you use the fixture in a test function or conftest.py, I tried passing socket_enabled to the function as per the example above but that didn't work.

@MatthewFlamm
Copy link
Owner

How do you use the fixture in a test function or conftest.py, I tried passing socket_enabled to the function as per the example above but that didn't work.

Like you did in your original comment. I'm not sure how the socket functionality works, but if you figure it out, we can put in a pointer here.

@MatthewFlamm
Copy link
Owner

@drc38
Copy link
Author

drc38 commented Nov 8, 2021

I have not had any luck with resolving, this suggests it may be related to the fixture resolution order:
miketheman/pytest-socket#45

and a similar issue reported here:
miketheman/pytest-socket#57

@drc38
Copy link
Author

drc38 commented Nov 8, 2021

Does having pytest_runtest_setup in the plugin override or prevent enable_socket being set by the local test function setup?

@MatthewFlamm
Copy link
Owner

I believe that the default behavior in the plugins here is to allow only host 127.0.0.1. Have you verified that it isn't failing because of trying to reach another host?

@drc38
Copy link
Author

drc38 commented Nov 9, 2021

Thanks, I managed to get it working by changing all localhost references explicitly to 127.0.0.1 and using the socket_enabled marker. In my view the plug-in code should also explicitly allow ::1 which is equivalent to 127.0.0.1 and was referenced in the error message as a blocked address.

@MatthewFlamm
Copy link
Owner

In my view the plug-in code should also explicitly allow ::1 which is equivalent to 127.0.0.1 and was referenced in the error message as a blocked address.

I'm going to close this, since it seems you have an issue with either pytest-socket or the implementation thereof in home-assistant/core. The plugins here are taken directly from home-assistant/core with only minimal changes needed for custom components. These minimal changes are mainly due to file path changes when testing directly in core vs. as a custom component.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants