Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mock the result of accessing public GCS bucket

I have the following code:

bucket = get_bucket('bucket-name')
blob = bucket.blob(os.path.join(*pieces))
blob.upload_from_string('test')
blob.make_public()
result = blob.public_url
# result is `<Mock name='mock().get_bucket().blob().public_url`

And I would do like to mock the result of public_url, my unit test code is something like this

with ExitStack() as st:
    from google.cloud import storage
    blob_mock = mock.Mock(spec=storage.Blob)
    blob_mock.public_url.return_value = 'http://'

    bucket_mock = mock.Mock(spec=storage.Bucket)
    bucket_mock.blob.return_value = blob_mock

    storage_client_mock = mock.Mock(spec=storage.Client)
    storage_client_mock.get_bucket.return_value = bucket_mock

    st.enter_context(
        mock.patch('google.cloud.storage.Client', storage_client_mock))
    my_function()

Is there something like FakeRedis or moto for Google Storage, so I can mock google.cloud.storage.Blob.public_url?

like image 747
Rodrigo Avatar asked Aug 17 '18 16:08

Rodrigo


1 Answers

I found this fake gcs server written in Go which can be run within a Docker container and consumed by the Python library. See Python examples.

like image 117
arauter Avatar answered Sep 24 '22 09:09

arauter