-
-
Notifications
You must be signed in to change notification settings - Fork 43
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
Host Whitelisting #6
Comments
Hi @kevinkjt2000 ! Thanks for the awesome report. Right now, the granularity of the logic is very coarse - it's set at the test case level - so it's not set to distinguish a difference between types of sockets within a test case. So we can selectively enable socket use for the entire test case using fixtures, but then run the risk of the items within the async test case making network calls. I also was curious about your use of import pytest
import pytest_socket
@pytest.mark.asyncio
async def test_something(socket_enabled):
pass Again, this doesn't have the granularity of detecting what happens during the test case, as socket is now enabled. I was thinking about how that might be implemented, and haven't come up with a decent design - as it would require evaluating within the test case any time |
Well I could enable socket and add self to get the asynctest to pass too xD (I edited my original comment to include self) As for how to design this, I would suggest some kind of lookup list in the fake disabled socket? If it is allowed, let it use real socket; otherwise, send it to fake socket? I am ignorant as to how blocking all sockets is implemented currently. |
I'll have a go if no one else has started... Before i go too far, @miketheman do you have any views on the following:
Cheers |
Hey @sadams! Awesome that you're going for it. For the questions asked, some thoughts.
|
Hi @miketheman , import socket
def foo():
print('overridden')
socket.socket = foo
print(socket.gethostbyname('google.com'))
# 172.217.23.46 So that is 'technically' a bug in the original, but I think it's probably pretty rare that people are using this to avoid DNS side effects. |
On the other points, i ended up doing the code, so i will submit a PR and we can discuss there (basically i added an optional argument to disable_sockets that was a whitelist of hosts). |
I may have closed this prematurely - the solution in #9 appears to work for connections with designated IP addresses, but the repro case here for unix sockets remains unsolved. |
got same problem... allowing AF_UNIX sockets while disabling AF_INET could be nice to have |
* Remove unnecessary comments. * Remove `pytest-socket` as it does not work with aiohttp. See miketheman/pytest-socket#6 for reference.
* Remove unnecessary comments. * Remove `pytest-socket` as it does not work with aiohttp. See miketheman/pytest-socket#6 for reference.
* Remove unnecessary comments. * Remove `pytest-socket` as it does not work with aiohttp. See miketheman/pytest-socket#6 for reference. * Enable `mergify` for this repository.
* Remove unnecessary comments. * Remove `pytest-socket` as it does not work with aiohttp. See miketheman/pytest-socket#6 for reference. * Enable `mergify` for this repository.
I have taken a look at this because I needed to be able to whitelist hostnames (i.e. I've created a fork with my changes which boils down to the addition of this function: https://github.com/benhowes/pytest-socket/blob/master/pytest_socket.py#L129-L157 It's not in a great shape for a PR at the moment since I opted to ditch python 2.7 and 3.3 at the same time because I wanted to use the Before I do any work on making it more PR ready, is there an appetite for this @miketheman ? |
I have similar problem in tests for Django project (it appeared after upgrade to django 3.* because it's using async underneath) |
Also getting this problem when running pytest with Tests (that access the database, possibly related) fail with:
and then start failing with:
When running without |
I'm also getting
That's strange because as I understand --allow-hosts should never cause SocketBlockedError. In tests without async stuff if I do something like
It gives: [ |
@jamesbeith I've run this on our CI with the pr above and it seems to be passing, will need to check that all the failures are real http requests, but looks promising so far 👋 @alex-verve , long time no see, do you mind running it on your codebase and giving it a go if it's still useful that is! |
Thanks to @joetsoi 's efforts, the patch is merged and I'll be cutting a release in the next few days. Closing this issue! |
While the basics of asyncio ought to be covered by enabling unix sockets in #54, I thought it might be nice to add some explicit asyncio tests to ensure that we don't hit any framework-specific regressions. I had written these locally when testing the changes anyhow. Refs: #6 Refs: #47 Signed-off-by: Mike Fiedler <miketheman@gmail.com>
While the basics of asyncio ought to be covered by enabling unix sockets in #54, I thought it might be nice to add some explicit asyncio tests to ensure that we don't hit any framework-specific regressions. I had written these locally when testing the changes anyhow. Refs: #6 Refs: #47 Signed-off-by: Mike Fiedler <miketheman@gmail.com>
While the basics of asyncio ought to be covered by enabling unix sockets in #54, I thought it might be nice to add some explicit asyncio tests to ensure that we don't hit any framework-specific regressions. I had written these locally when testing the changes anyhow. Refs: #6 Refs: #47 Signed-off-by: Mike Fiedler <miketheman@gmail.com>
I've just run into the same issue trying to set up pytest with redis in a Github workflow. Essentially when running tests in a container the redis service is accessible via the Eventually I ended up resolving the issue with this: - name: Get Redis IP
id: redis
run: |
redis_ip=$(echo "import socket; print(socket.gethostbyname('redis'))" | python)
echo ::set-output name=ip::"$redis_ip"
- run: pytest ... --allow-hosts=127.0.0.1,${{ steps.redis.outputs.ip }}
env:
REDIS_HOST: redis
REDIS_PORT: ${{ job.services.redis.ports[6379] }} But ideally, I would want to be able to just do this - run: pytest ... --allow-hosts=127.0.0.1,redis
env:
REDIS_HOST: redis
REDIS_PORT: ${{ job.services.redis.ports[6379] }} Are you still planning on submitting a PR for this feature @benhowes, or has an equivalent fix already been added? |
@sondrelg I'm no longer working on a project which uses my fork and I've really lost track of where this library. Sorry! |
It seems that asynctest requires at least localhost sockets when I ran my test. Would it be possible to selectively enable certain sockets and not others? For example, here I would probably get past this if I could allow localhost sockets.
To reproduce:
pytest.ini
test.py
commands to enter:
The text was updated successfully, but these errors were encountered: