moto: @mock_s3 permanently breaks requests library
At first look, I thought this was a duplicate of https://github.com/spulec/moto/issues/303.
However, on further inspection, the problem is only present with the mock_s3 decorator, not with the mock_s3_deprecated decorator. Therefore, if my inspection of the code base is right, this looks like a problem related to the responses library or the use of it in moto, whereas https://github.com/spulec/moto/issues/303 concerned the HTTPretty library.
Anyway, the meat of the issue:
After using @mock_s3 in django tests, other test classes that use LiveserverTestCase and the requests library stop working, and fail with the error: ConnectionError: Connection refused.
This is not an inherent problem with responses itself, as the following test demonstrates:
python -c "from moto.packages.responses.responses import mock; import requests; mock.start(); mock.stop(); print requests.get('http://google.com')"
<Response [200]>
However, if mock.stop() is omitted, you see the same error as in the django test:
python -c "from moto.packages.responses.responses import mock; import requests; mock.start(); print requests.get('http://google.com')"
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/home/REDACTED/env/local/lib/python2.7/site-packages/requests/api.py", line 72, in get
return request('get', url, params=params, **kwargs)
File "/home/REDACTED/env/local/lib/python2.7/site-packages/requests/api.py", line 58, in request
return session.request(method=method, url=url, **kwargs)
File "/home/REDACTED/env/local/lib/python2.7/site-packages/requests/sessions.py", line 502, in request
resp = self.send(prep, **send_kwargs)
File "/home/REDACTED/env/local/lib/python2.7/site-packages/requests/sessions.py", line 612, in send
r = adapter.send(request, **kwargs)
File "/home/REDACTED/env/local/lib/python2.7/site-packages/moto/packages/responses/responses.py", line 302, in unbound_on_send
return self._on_request(adapter, request, *a, **kwargs)
File "/home/REDACTED/env/local/lib/python2.7/site-packages/moto/packages/responses/responses.py", line 244, in _on_request
raise response
requests.exceptions.ConnectionError: Connection refused: GET http://google.com/
This suggests to me that, one way or another, moto is failing to call mock.stop().
moto version 1.0.1 Django version 1.11.3
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 18
- Comments: 31 (6 by maintainers)
Commits related to this issue
- Add other S3 notification handlers. Tests don't work yet due to https://github.com/spulec/moto/issues/1026 — committed to dacut/cfn-toolkit by dacut 7 years ago
- According to what I said on spulec/moto#1026 Add a unittest and attempt a fix. — committed to hoangelos/moto by hoangelos 6 years ago
@yhvh A workaround for this for now is:
As of moto 1.3.5 this works:
Closed with #1553
@terrycain I’m kinda okay with non-supported services passing through to AWS. It seems reasonable to me that if you
@mock_ec2it goes to Moto and if you don’t you get AWS, always (If I understand the decision we’re trying to make correctly). @spulec WDYT?Installed from master, this still fails. We have calls to different services interleaved with calls to aws, can you confirm this minimal use case is supported?
@g-io I have worked around the problem with the following monkeypatch:
Note that as of the new version of botocore, it no longer uses responses, so likely the solution above will fail too, whenever moto is rewritten to follow the change in botocore.
Explicitly calling the following upon the end of the mocked portion also appears to fix the issue.
This issue is biting us as well - we’re unable to use AWS services hosted in LocalStack docker containers if moto mocks are used anywhere in our test suite. I tried @amagee 's workaround successfully and got past my initial problem which was preventing me from talking to docker at all. However now in botocore’s urllib connection pool, I’m running into an odd error with an isinstance check on a Timeout class of all things - is it possible that this is a mocked resource that isn’t being cleaned up either? Haven’t been able to piece it together yet.
I’m having this issue also. I’m pretty sure the problem occurs when you use more than one mock from moto, for example decorating a test with mock_s3 and mock_lambda. My guess is something is going on with disabling patching. This will fail:
Both tests will pass when removing the @mock_apigateway decorator.