Skip to main content

cloud-storage-mocker

Mocker library of Google Cloud Storage Python client with local filesystem mounting.

Install

For package users:

pip install cloud-storage-mocker

For package developers:

git clone git@github.com:CroudTech/cloud-storage-mocker
cd cloud-storage-mocker
python -m venv venv
source venv/bin/activate
pip install -e '.[dev]'

How the package works

Basic usage

This library provides patch context manager, which replaces following classes on the google-cloud-storage package:

  • Client
  • Bucket
  • Blob

patch takes a list of Mount objects, which represent mappings between bucket names and directories on the local filesystem. Each Mount has Boolean configs readable and writable to control read/write permissions to the mounted buckets.

A canonical use-case of this package is writing unit tests to check the behavior of the code that works with Google Cloud Storage:

import pathlib

import google.cloud.storage  # type: ignore[import]

from ct_cloud_storage_mocker import BlobMetadata, Mount
from ct_cloud_storage_mocker import patch as gcs_patch


def test_something(tmp_path: pathlib.Path) -> None:
    # Creates two temporary directories for readable/writable buckets.
    src_dir = tmp_path / "src"
    dest_dir = tmp_path / "dest"
    src_dir.mkdir()
    dest_dir.mkdir()

    # A sample file on the readable bucket.
    (src_dir / "hello.txt").write_text("Hello.")
    # Optionally, object metadata can also be specified by the file beside the
    # content, suffixed by ".__metadata__".
    (src_dir / "hello.txt.__metadata__").write_text(
        BlobMetadata(content_type="text/plain").dump_json()
    )

    # Mounts directories. Empty list is allowed if no actual access is required.
    with gcs_patch(
        [
            Mount("readable", src_dir, readable=True),
            Mount("writable", dest_dir, writable=True),
        ],
    ):
        client = google.cloud.storage.Client()

        # Reads a blob.
        blob = client.bucket("readable").blob("hello.txt")
        assert blob.download_as_text() == "Hello."
        # Metadata is available after downloading the content.
        assert blob.content_type == "text/plain"

        # Writes a blob.
        blob = client.bucket("writable").blob("world.txt")
        blob.upload_from_string("World.")

    # Checks if the file is written appropriately.
    assert (dest_dir / "world.txt").read_text() == "World."

Patched methods/properties

Methods listed below have specialized behavior to mount the local filesystem.

Other methods are mapped to MagicMock.

Client()

Client.bucket()

Bucket()

Bucket.blob()

Blob()

# Blob properties (download only)
Blob.cache_control
Blob.content_disposition
Blob.content_encoding
Blob.content_language
Blob.content_type

Blob.download_to_file()
Blob.download_to_filename()
Blob.download_as_bytes()
Blob.download_as_string()
Blob.download_as_text()
Blob.upload_from_file()
Blob.upload_from_filename()
Blob.upload_from_string()

Caution

This library is basically provided for writing unit tests. DO NOT use this library on any code on the production.

Metadata

Release files for ct-cloud-storage-mocker 0.1.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ct-cloud-storage-mocker 0.1.5
File Size Uploaded Provenance Actions
ct_cloud_storage_mocker-0.1.5.tar.gz 8.3 kB Without provenance Details for ct_cloud_storage_mocker-0.1.5.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for ct-cloud-storage-mocker 0.1.5
File Interpreter ABI Platform Actions
ct_cloud_storage_mocker-0.1.5-py3-none-any.whl Python 3 none any Details for ct_cloud_storage_mocker-0.1.5-py3-none-any.whl

Total release size: 17.8 kB

Release files / ct_cloud_storage_mocker-0.1.5.tar.gz

Download URL ct_cloud_storage_mocker-0.1.5.tar.gz
Size 8.3 kB
Tags Source
SHA-256 checksum
How to use checksums
50a822b624b378901e9c28d7a0b15de0a76abcd8163db4aab322781b828a2cfb
BLAKE2b-256 checksum
How to use checksums
adde3a4db994a8694c39b29a9677bfa1ac2692c253e1731ad3487906c8c765dc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.4 CPython/3.13.7 Linux/6.11.0-1018-azure

Release files / ct_cloud_storage_mocker-0.1.5-py3-none-any.whl

Download URL ct_cloud_storage_mocker-0.1.5-py3-none-any.whl
Size 9.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8f0e517740b56aa3cc8835ff3c0c103c2f2a91849c232ce1ec2f8d940dc01531
BLAKE2b-256 checksum
How to use checksums
f76dc3e384966cdd677d37414c177e6a3096862c019ff6a32d48f87276cc0bee
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.4 CPython/3.13.7 Linux/6.11.0-1018-azure

Security for ct-cloud-storage-mocker 0.1.5

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page