GithubHelp home page GithubHelp logo

Comments (10)

sloria avatar sloria commented on June 14, 2024 4

adding the --disable-socket would be unnecessary.

True, but it's un-intuitive for --disable-socket to take precedence over --allow-hosts when both are passed. Either allow users to pass both and fix the behavior or disallow passing both (i.e. raise an error).

from pytest-socket.

joetsoi avatar joetsoi commented on June 14, 2024 1

https://github.com/urllib3/urllib3/blob/main/src/urllib3/util/connection.py#L63-L72

So I've bumped into this due to the way socket is used in urllib3, socket.socket is called before socket.socket.connect. Causing a SocketBlockedError() to be raised before the guarded connect is called.

Current behaviour disable_socket does not reset the guard, but it just blocks at an earlier point. (i.e SocketBlockedError is raised instead of SocketConnectBlockedError)

Why would I want to do this? Because I want to --disable-socket globally and then allow hosts for a specific test, so best way to allow that is to mark with both allowed_hosts and enable_socket

        @pytest.mark.allowed_hosts("localhost")
        @pytest.mark.enable_socket
        def test_something():
            ...

It sounds like erroring when both --allowed-hosts and --disable-socket are provided might be sensible, and maybe add something to the docs with the above example if you want to enable for specific tests?

from pytest-socket.

miketheman avatar miketheman commented on June 14, 2024 1

Hey gang! Thanks for your patience - I've worked out the precedence levels, and have pushed the latest code the the main branch. I'll aim to cut a release sometime within the next couple of weeks, keep an eye out for that!

from pytest-socket.

sloria avatar sloria commented on June 14, 2024

Alternatively, an error could be raised if both arguments are passed.

I don't have a strong preference for (1) allowing --allow-hosts to take precedence or (2) raise an error. Both are better than the status quo, IMO.

from pytest-socket.

miketheman avatar miketheman commented on June 14, 2024

I wonder if that has to do with the approach of closest marker, from here:

mark_restrictions = item.get_closest_marker('allow_hosts')

It also looks like there's not the test case you're specifying - but I feel like the --allow-hosts ought to disable anything other than the allowed hosts, so adding the --disable-socket would be unnecessary.

from pytest-socket.

SHxKM avatar SHxKM commented on June 14, 2024

@sloria - did you find an elegant way to deal with?

from pytest-socket.

atugushev avatar atugushev commented on June 14, 2024

I believe this is because disable_socket resets the guard set by socket_allow_hosts, see:

def disable_socket():
""" disable socket.socket to disable the Internet. useful in testing.
"""
def guarded(*args, **kwargs):
raise SocketBlockedError()
socket.socket = guarded

UPDATE: and also there must be socket.socket.connect = guarded

from pytest-socket.

miketheman avatar miketheman commented on June 14, 2024

Version 0.3.5 was just released with a slight modification to the ordering behavior of disable-socket and how it might impact the allow-hosts approach.

Can someone who's been impacted by this re-test, and if it's still not working, provide reproducible example? Some good guidance here: https://stackoverflow.com/help/minimal-reproducible-example

from pytest-socket.

ryanjdillon avatar ryanjdillon commented on June 14, 2024

I just ran into this, as I'm using motoserver to mock AWS services, so I want to allow connections to localhost, and block to AWS.

I also assumed that --disable-socket --allow-hosts=127.0.01,localhost would do what I wanted. I only discovered this behavior by looking through the issues here.

from pytest-socket.

miketheman avatar miketheman commented on June 14, 2024

Thanks for the input everyone - I think I have enough details to establish what's wrong.

TL,DR: there's a logical conflict between the config options, leading to failed outcomes. This is likely to change the
flag combinations, so I'm treading carefully.

from pytest-socket.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.