-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
@mock_s3 permanently breaks requests library #1026
Comments
In addition, the tracebacks from the failing django unit tests themselves confirms that the requests are going through the responses library. For example:
This is from a test method in a test class using |
I can confirm similar behavior, but using |
Can confirm similar behavior, and I've tried both the decorator and raw usage (explicitly calling In my case, it is Lambda which is not properly restored: # Python 3.6
import boto3, moto
mocks = [moto.mock_lambda(), moto.mock_s3()]
for mock in mocks:
mock.start()
for mock in mocks:
mock.stop()
boto3.client('lambda').get_function(FunctionName='...')
|
A workaround for this is to manually restore the import botocore
real_http_adapter = botocore.vendored.requests.adapters.HTTPAdapter.send
# run tests using moto
botocore.vendored.requests.adapters.HTTPAdapter.send = real_http_adapter
# boto3 operations work correctly now |
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: def _process_lamda(pfunc):
zip_output = io.BytesIO()
zip_file = zipfile.ZipFile(zip_output, 'w', zipfile.ZIP_DEFLATED)
zip_file.writestr('lambda_function.zip', pfunc)
zip_file.close()
zip_output.seek(0)
return zip_output.read()
def get_test_zip_file():
pfunc = """
def lambda_handler(event, context):
return event
"""
return _process_lamda(pounce)
@mock_lambda
@mock_apigateway
def test_lambda_invoke(self):
conn = boto3.client('lambda', 'us-west-2')
conn.create_function(
FunctionName='testMotoFunction',
Runtime='python2.7',
Role='test-iam-role',
Handler='lambda_function.handler',
Code={
'ZipFile': get_test_zip_file(),
},
Description='test lambda function',
Timeout=3,
MemorySize=128,
Publish=True,
)
def test_request(self):
import requests
r = requests.get('https://www.google.com')
self.assertEqual(r.status_code, 200) Traceback (most recent call last):
File "/Users/chris.keogh/Documents/Workspace/report-card-app/flask/app/tests/common_tests/test_aws_lambda.py", line 36, in test_request
r = requests.get('https://www.google.com')
File "/Users/chris.keogh/Documents/Workspace/report-card-app/flask/lib/python3.5/site-packages/requests/api.py", line 72, in get
return request('get', url, params=params, **kwargs)
File "/Users/chris.keogh/Documents/Workspace/report-card-app/flask/lib/python3.5/site-packages/requests/api.py", line 58, in request
return session.request(method=method, url=url, **kwargs)
File "/Users/chris.keogh/Documents/Workspace/report-card-app/flask/lib/python3.5/site-packages/requests/sessions.py", line 508, in request
resp = self.send(prep, **send_kwargs)
File "/Users/chris.keogh/Documents/Workspace/report-card-app/flask/lib/python3.5/site-packages/requests/sessions.py", line 618, in send
r = adapter.send(request, **kwargs)
File "/Users/chris.keogh/Documents/Workspace/report-card-app/flask/lib/python3.5/site-packages/moto/packages/responses/responses.py", line 308, in unbound_on_send
return self._on_request(adapter, request, *a, **kwargs)
File "/Users/chris.keogh/Documents/Workspace/report-card-app/flask/lib/python3.5/site-packages/moto/packages/responses/responses.py", line 250, in _on_request
raise response
requests.exceptions.ConnectionError: Connection refused: GET https://www.google.com/ Both tests will pass when removing the @mock_apigateway decorator. |
I can confirm using |
Tests don't work yet due to getmoto/moto#1026
I'll confirm this issue is still live in 1.1.24. For me the issue occurs when trying to use the responses library separately in addition with |
Ok so this line Hmm, I have a feeling if i set pass_through to True, things will break. Lets see. |
Ok well it seems I can change pass_through to True, without breaking any tests, results. @JackDanger any thoughts on this, according to the tests it wont break anything. Only downside is it will allow non supported services to go to AWS but people should really know that some things are not supported as they would have had to Other option would be to do something like |
Is it so that moto can't now be used in conjuction with the pure I'm not sure what double patching the same target does but it probably kills the other patching meaning it can't be stopped, thus the target remains patched after. What is the reason moto carries it's own responses and doesn't use the |
@terrycain I'm kinda okay with non-supported services passing through to AWS. It seems reasonable to me that if you |
Any news on this? I'm experiencing the same for requests to unrelated domains with the requests library while using @mock_... |
the
|
@g-io I have worked around the problem with the following monkeypatch:
|
So, maybe I'm missing this. However, how is this even a solution? Doesn't this just instead of fixing the monkey patching make the mock able to connect to other URLs? We are getting this error too. But we only get it when you use two together. I still don't see a solution that seems to fix this without adding an ability to make the mocked request connect, which doesn't seem like a solution. Also, the issue is worse. We have other requests failing in other testing files altogether because requests is being permanently set to Moto's request. The other test doesn't use moto at all. It does use httpretty. All is fixed by removing the additional mock. Not using two just one. |
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. |
@killthrush This is the same issue. Two things are patched. requests and botocore. |
seeing this too... |
Sadly I haven't been able to come back to this, but the interesting problem is the depreciated methods, which use httpretty work. It's the "new" ones that are broke. :-D |
Same issue here when using multiple annotations on class with unittest. My tests include some tests with moto and some without. Using moto breaks the other tests |
There are some workarounds suggested here to modify the moto patches. Is there a simple way to absolutely ensure that moto's patching is simply removed, so that, thereafter, requests works as before. I have tried reloading some requests submodules, but to no avail. |
I've had many problems with shared use of the global responses Replacing the
|
I've created a PR that should address this: #1553 If anyone can test and confirm it works, it would be appreciated. |
Closed with #1553 |
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? import moto, requests
@moto.mock_s3
def test_foo():
print requests.get('https://www.google.co.uk')
|
@yhvh A workaround for this for now is: import requests
import responses
import moto
import unittest
class TestRequests(unittest.TestCase):
@moto.mock_s3
def test_requests(self):
responses.add_passthru('https://')
response = requests.get('https://www.google.com')
self.assertEqual(response.status_code, 200)
unittest.main() |
Explicitly calling the following upon the end of the mocked portion also appears to fix the issue.
|
same problem on mock_emr |
Follow along on #1567 for more discussion of changing requests during tests. |
As of moto 1.3.5 this works:
|
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. |
At first look, I thought this was a duplicate of #303.
However, on further inspection, the problem is only present with the
mock_s3
decorator, not with themock_s3_deprecated
decorator. Therefore, if my inspection of the code base is right, this looks like a problem related to theresponses
library or the use of it inmoto
, whereas #303 concerned the HTTPretty library.Anyway, the meat of the issue:
After using
@mock_s3
in django tests, other test classes that useLiveserverTestCase
and therequests
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:However, if
mock.stop()
is omitted, you see the same error as in the django test: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
The text was updated successfully, but these errors were encountered: