|
| 1 | +# |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 3 | +# not use this file except in compliance with the License. You may obtain |
| 4 | +# a copy of the License at |
| 5 | +# |
| 6 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +# |
| 8 | +# Unless required by applicable law or agreed to in writing, software |
| 9 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 10 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 11 | +# License for the specific language governing permissions and limitations |
| 12 | +# under the License. |
| 13 | +import os |
| 14 | +from typing import Optional |
| 15 | +from unittest.mock import patch |
| 16 | + |
| 17 | +from google.cloud import storage |
| 18 | +from testcontainers.core.container import DockerContainer |
| 19 | + |
| 20 | + |
| 21 | +class GoogleCloudStorageContainer(DockerContainer): |
| 22 | + """ |
| 23 | + GoogleCloudStorage container for testing managed object storage buckets. |
| 24 | +
|
| 25 | + Example: |
| 26 | +
|
| 27 | + The example will spin up a Google Cloud Storage Fake Server that you can use for integration |
| 28 | + tests. The :code:`storage` instance provides convenience methods :code:`get_storage_client` to interact with |
| 29 | + the fake GCS server without having to set the environment variable :code:`STORAGE_EMULATOR_HOST`. |
| 30 | +
|
| 31 | + .. doctest:: |
| 32 | +
|
| 33 | + >>> from testcontainers.google import GoogleCloudStorageContainer |
| 34 | +
|
| 35 | + >>> config = GoogleCloudStorageContainer() |
| 36 | + >>> with config as gcs: |
| 37 | + ... client = gcs.get_storage_client() |
| 38 | + ... bucket = client.create_bucket("test-bucket") |
| 39 | + ... bucket = client.create_bucket("test-bucket") |
| 40 | + """ |
| 41 | + |
| 42 | + def __init__( |
| 43 | + self, image: str = "fsouza/fake-gcs-server", |
| 44 | + location: str = "US-CENTRAL1", |
| 45 | + scheme: str = "http", |
| 46 | + port_http: int = 8000, |
| 47 | + data: Optional[str] = None, |
| 48 | + **kwargs |
| 49 | + ) -> None: |
| 50 | + super().__init__(image=image, **kwargs) |
| 51 | + self.location = location |
| 52 | + self.scheme = scheme |
| 53 | + self.port_http = port_http |
| 54 | + self.data = data |
| 55 | + self.with_exposed_ports(self.port_http) |
| 56 | + command = f"-location {location} -scheme={scheme} -port={port_http}" |
| 57 | + if self.data: |
| 58 | + self.with_volume_mapping(self.data, "/data") |
| 59 | + command += " -data /data" |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | + self.with_command(command) |
| 64 | + |
| 65 | + def get_gcs_emulator_host(self) -> str: |
| 66 | + return f"{self.scheme}://{self.get_container_host_ip()}:{self.get_exposed_port(self.port_http)}" |
| 67 | + |
| 68 | + def _get_client(self, cls: type, **kwargs) -> dict: |
| 69 | + with patch.dict(os.environ, STORAGE_EMULATOR_HOST=self.get_gcs_emulator_host()): |
| 70 | + return cls(**kwargs) |
| 71 | + |
| 72 | + def get_storage_client(self, **kwargs) -> storage.Client: |
| 73 | + from google.auth import credentials |
| 74 | + |
| 75 | + kwargs["credentials"] = credentials.AnonymousCredentials() |
| 76 | + return self._get_client(storage.Client, **kwargs) |
0 commit comments