mintel / pytest-localstack

Pytest plugin for local AWS integration tests
MIT License
78 stars 17 forks source link

ResourceWarning during _check_services #44

Open Andrew-Wichmann opened 3 years ago

Andrew-Wichmann commented 3 years ago

I'm getting a ResourceWarning from pytest-localstack during fixture setup.

awichmann@CHI-LX-L-018 '16:30:29' (master) '~/Code/everest_code/libraries/mintel-event-bus-connector-sqs'
> $ make test
.venv/bin/pytest
================================================================================================ test session starts =================================================================================================
platform linux -- Python 3.8.1, pytest-6.2.3, py-1.10.0, pluggy-0.13.1
rootdir: /home/awichmann/Code/everest_code/libraries/mintel-event-bus-connector-sqs, configfile: pytest.ini
plugins: env-0.6.2, localstack-0.4.1
collected 4 items                                                                                                                                                                                                    

tests/test_service.py E...                                                                                                                                                                                     [100%]

======================================================================================================= ERRORS =======================================================================================================
_________________________________________________________________________________________ ERROR at setup of test_no_handlers _________________________________________________________________________________________

cls = <class '_pytest.runner.CallInfo'>, func = <function call_runtest_hook.<locals>.<lambda> at 0x7ff72ef99670>, when = 'setup', reraise = (<class '_pytest.outcomes.Exit'>, <class 'KeyboardInterrupt'>)

    @classmethod
    def from_call(
        cls,
        func: "Callable[[], TResult]",
        when: "Literal['collect', 'setup', 'call', 'teardown']",
        reraise: Optional[
            Union[Type[BaseException], Tuple[Type[BaseException], ...]]
        ] = None,
    ) -> "CallInfo[TResult]":
        excinfo = None
        start = timing.time()
        precise_start = timing.perf_counter()
        try:
>           result: Optional[TResult] = func()

cls        = <class '_pytest.runner.CallInfo'>
duration   = 12.87210832799974
excinfo    = <ExceptionInfo PytestUnraisableExceptionWarning('Exception ignored in: <socket.socket fd=-1, family=AddressFamily.AF_I...AF_INET, type=SocketKind.SOCK_STREAM, proto=6, laddr=(\'127.0.0.1\', 51724), raddr=(\'127.0.0.1\', 49246)>\n') tblen=7>
func       = <function call_runtest_hook.<locals>.<lambda> at 0x7ff72ef99670>
precise_start = 21921.919507388
precise_stop = 21934.791615716
reraise    = (<class '_pytest.outcomes.Exit'>, <class 'KeyboardInterrupt'>)
result     = None
start      = 1617831042.9316583
stop       = 1617831055.8037689
when       = 'setup'

.venv/lib/python3.8/site-packages/_pytest/runner.py:311: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
.venv/lib/python3.8/site-packages/_pytest/runner.py:255: in <lambda>
    lambda: ihook(item=item, **kwds), when=when, reraise=reraise
        ihook      = <_HookCaller 'pytest_runtest_setup'>
        item       = <Function test_no_handlers>
        kwds       = {}
.venv/lib/python3.8/site-packages/pluggy/hooks.py:286: in __call__
    return self._hookexec(self, self.get_hookimpls(), kwargs)
        args       = ()
        kwargs     = {'item': <Function test_no_handlers>}
        notincall  = set()
        self       = <_HookCaller 'pytest_runtest_setup'>
.venv/lib/python3.8/site-packages/pluggy/manager.py:93: in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
        hook       = <_HookCaller 'pytest_runtest_setup'>
        kwargs     = {'item': <Function test_no_handlers>}
        methods    = [<HookImpl plugin_name='nose', plugin=<module '_pytest.nose' from '/home/awichmann/Code/everest_code/libraries/mintel-...=None>>, <HookImpl plugin_name='logging-plugin', plugin=<_pytest.logging.LoggingPlugin object at 0x7ff72ef6c4c0>>, ...]
        self       = <_pytest.config.PytestPluginManager object at 0x7ff7362a67c0>
.venv/lib/python3.8/site-packages/pluggy/manager.py:84: in <lambda>
    self._inner_hookexec = lambda hook, methods, kwargs: hook.multicall(
        hook       = <_HookCaller 'pytest_runtest_setup'>
        kwargs     = {'item': <Function test_no_handlers>}
        methods    = [<HookImpl plugin_name='nose', plugin=<module '_pytest.nose' from '/home/awichmann/Code/everest_code/libraries/mintel-...=None>>, <HookImpl plugin_name='logging-plugin', plugin=<_pytest.logging.LoggingPlugin object at 0x7ff72ef6c4c0>>, ...]
.venv/lib/python3.8/site-packages/_pytest/unraisableexception.py:83: in pytest_runtest_setup
    yield from unraisable_exception_runtest_hook()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

    def unraisable_exception_runtest_hook() -> Generator[None, None, None]:
        with catch_unraisable_exception() as cm:
            yield
            if cm.unraisable:
                if cm.unraisable.err_msg is not None:
                    err_msg = cm.unraisable.err_msg
                else:
                    err_msg = "Exception ignored in"
                msg = f"{err_msg}: {cm.unraisable.object!r}\n\n"
                msg += "".join(
                    traceback.format_exception(
                        cm.unraisable.exc_type,
                        cm.unraisable.exc_value,
                        cm.unraisable.exc_traceback,
                    )
                )
>               warnings.warn(pytest.PytestUnraisableExceptionWarning(msg))
E               pytest.PytestUnraisableExceptionWarning: Exception ignored in: <socket.socket fd=-1, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6>
E               
E               Traceback (most recent call last):
E                 File "/home/awichmann/Code/everest_code/libraries/mintel-event-bus-connector-sqs/.venv/lib/python3.8/site-packages/pytest_localstack/session.py", line 108, in _check_services
E                   service_checks.SERVICE_CHECKS[service_name](self)
E               ResourceWarning: unclosed <socket.socket fd=24, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6, laddr=('127.0.0.1', 51724), raddr=('127.0.0.1', 49246)>

cm         = <_pytest.unraisableexception.catch_unraisable_exception object at 0x7ff72d9c3af0>
err_msg    = 'Exception ignored in'
msg        = "Exception ignored in: <socket.socket fd=-1, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6>\n\nTr...AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6, laddr=('127.0.0.1', 51724), raddr=('127.0.0.1', 49246)>\n"

.venv/lib/python3.8/site-packages/_pytest/unraisableexception.py:78: PytestUnraisableExceptionWarning
localstack = pytest_localstack.session_fixture(
    scope="module", services=["sqs", "sns"], localstack_version="0.10.9"
)

@pytest.fixture(autouse=True)
def env(localstack, queue_name):
    original_env = deepcopy(os.environ)
    os.environ["SQS_INPUT_QUEUE"] = queue_name
    os.environ["AWS_ENDPOINTS"] = f"sqs={localstack.endpoint_url('sqs')}"
    os.environ["AWS_ACCESS_KEY_ID"] = "foo"
    os.environ["AWS_SECRET_ACCESS_KEY"] = "bar"
    yield
    os.environ = original_env

@pytest.fixture
def queue_name():
    return "input-queue"

@pytest.fixture
def queue(localstack, queue_name):
    sqs = boto3.resource(
        "sqs",
        endpoint_url=localstack.endpoint_url("sqs"),
        aws_access_key_id="foobar",
        aws_secret_access_key="barbaz",
    )
    queue = sqs.create_queue(QueueName=queue_name)
    yield queue
    queue.delete()

def test_no_handlers(queue):
    from mintel_event_bus_connector_sqs import loop

    queue.send_message(
        MessageBody=AssetCreated({"id": "foobar"}, source="testing").as_string()
    )
    assert loop.step() == ProcessOutcomeEnum.SUCCESS
p1c2u commented 3 years ago

Hi @Andrew-Wichmann

I ran into the same issue with sns client. I had to clear session to get rid of the error


@pytest.fixture
def client():
    client = boto3("sns")
    yield client
    client._endpoint.http_session._manager.clear()

The issue is related to boto3 library itself https://github.com/boto/boto3/issues/454