refined-addition-53644
11/15/2022, 2:30 PMgenerate-lockfiles
was taking forever (>=10 mins) and won’t even finish. I figured it out that it’s because of adding s3fs which depends on aiobotocore. It’s the aiobotocore which has some very restrictive dependencies.
How long is pants/pex supposed to keep trying? I tried it with pip and pip throws an error about this.ripe-cpu-85141
11/15/2022, 2:45 PMripe-cpu-85141
11/15/2022, 2:47 PMrefined-addition-53644
11/15/2022, 3:01 PMhappy-kitchen-89482
11/15/2022, 4:01 PMgentle-painting-24549
11/15/2022, 4:07 PMs3fs
aiobotocore[boto3]
refined-addition-53644
11/15/2022, 4:08 PMrefined-addition-53644
11/15/2022, 4:09 PMrefined-addition-53644
11/15/2022, 4:10 PMs3fs
for testing?gentle-painting-24549
11/15/2022, 4:15 PMgentle-painting-24549
11/15/2022, 4:17 PMgentle-painting-24549
11/15/2022, 4:24 PMhappy-kitchen-89482
11/15/2022, 8:06 PMhappy-kitchen-89482
11/16/2022, 7:41 PMhappy-kitchen-89482
11/16/2022, 7:41 PMrefined-addition-53644
11/16/2022, 8:21 PMgentle-painting-24549
02/09/2023, 10:40 PMaiohttp==3.8.3
and moto[s3]==4.1.0
in my dev dependencies to get this to work - and using s3fs==2023.1.0
`conftest.py`:
from unittest.mock import MagicMock
import aiobotocore.awsrequest
import aiobotocore.endpoint
import aiohttp
import aiohttp.client_reqrep
import aiohttp.typedefs
import botocore.awsrequest
import botocore.model
import pytest
class MockAWSResponse(aiobotocore.awsrequest.AioAWSResponse):
"""
Mocked AWS Response.
<https://github.com/aio-libs/aiobotocore/issues/755>
<https://gist.github.com/giles-betteromics/12e68b88e261402fbe31c2e918ea4168>
"""
def __init__(self, response: botocore.awsrequest.AWSResponse):
self._moto_response = response
self.status_code = response.status_code
self.raw = MockHttpClientResponse(response)
# adapt async methods to use moto's response
async def _content_prop(self) -> bytes:
return self._moto_response.content
async def _text_prop(self) -> str:
return self._moto_response.text
class MockHttpClientResponse(aiohttp.client_reqrep.ClientResponse):
"""
Mocked HTP Response.
See <MockAWSResponse> Notes
"""
def __init__(self, response: botocore.awsrequest.AWSResponse):
"""
Mocked Response Init.
"""
async def read(self: MockHttpClientResponse, n: int = -1) -> bytes:
return response.content
self.content = MagicMock(aiohttp.StreamReader)
self.content.read = read
self.response = response
@property
def raw_headers(self) -> Any:
"""
Return the headers encoded the way that aiobotocore expects them.
"""
return {
k.encode("utf-8"): str(v).encode("utf-8")
for k, v in self.response.headers.items()
}.items()
@pytest.fixture(scope="session", autouse=True)
def patch_aiobotocore() -> None:
"""
Pytest Fixture Supporting S3FS Mocks.
See <MockAWSResponse> Notes
"""
def factory(original: Callable[[Any, Any], Any]) -> Callable[[Any, Any], Any]:
"""
Response Conversion Factory.
"""
def patched_convert_to_response_dict(
http_response: botocore.awsrequest.AWSResponse,
operation_model: botocore.model.OperationModel,
) -> Any:
return original(MockAWSResponse(http_response), operation_model)
return patched_convert_to_response_dict
aiobotocore.endpoint.convert_to_response_dict = factory(
aiobotocore.endpoint.convert_to_response_dict
)
`test_example.py`:
import boto3
from moto import mock_s3
@mock_s3
def test_mocked_s3() -> None:
"""
Test S3 Mocking with S3FS
"""
# Create the bucket in our Mocked S3 Env First
conn = boto3.resource("s3", region_name="us-east-1")
conn.create_bucket(Bucket="example-bucket")
# Create the files using whatever you're doing with S3FS
upload_three_files_using_s3fs(bucket="example-bucket") # pseudo code
# Make some assertions about what's supposed to be in S3
bucket = conn.Bucket("example-bucket")
matching_file_objects = list(bucket.objects.all())
assert len(matching_file_objects) == 3
refined-addition-53644
02/09/2023, 10:45 PM