-
Notifications
You must be signed in to change notification settings - Fork 301
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
fix(google): add support for Datastore emulator (#508)
Expands the google module with a DatastoreContainer using the beta Datastore emulator using the same image as the PubSubContainer. Im already using a local copy of this in production. It would be nice to not have to support copy paste solutions and instead see it added to the google module. This is my first PR so please let me know what I need to do to get this over the line. Thanks. Looks like @tillahoffmann wrote the original PubSub emulator container --------- Co-authored-by: Matt Oates <matt.oates@biorelate.com> Co-authored-by: David Ankin <daveankin@gmail.com>
- testcontainers-v4.9.1
- testcontainers-v4.9.0
- testcontainers-v4.8.2
- testcontainers-v4.8.1
- testcontainers-v4.8.0
- testcontainers-v4.7.2
- testcontainers-v4.7.1
- testcontainers-v4.7.0
- testcontainers-v4.6.0
- testcontainers-v4.5.1
- testcontainers-v4.5.0
- testcontainers-v4.4.1
- testcontainers-v4.4.0
- testcontainers-v4.3.3
- testcontainers-v4.3.2
- testcontainers-v4.3.1
- testcontainers-v4.3.0
1 parent
8addc11
commit 3d891a5
Showing
6 changed files
with
165 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
.. autoclass:: testcontainers.google.DatastoreContainer | ||
.. title:: testcontainers.google.DatastoreContainer | ||
.. autoclass:: testcontainers.google.PubSubContainer | ||
.. title:: testcontainers.google.PubSubContainer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
from .datastore import DatastoreContainer # noqa: F401 | ||
from .pubsub import PubSubContainer # noqa: F401 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
# not use this file except in compliance with the License. You may obtain | ||
# a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
# License for the specific language governing permissions and limitations | ||
# under the License. | ||
import os | ||
from unittest.mock import patch | ||
|
||
from google.cloud import datastore | ||
from testcontainers.core.container import DockerContainer | ||
from testcontainers.core.waiting_utils import wait_for_logs | ||
|
||
|
||
class DatastoreContainer(DockerContainer): | ||
""" | ||
Datastore container for testing managed message queues. | ||
Example: | ||
The example will spin up a Google Cloud Datastore emulator that you can use for integration | ||
tests. The :code:`datastore` instance provides convenience methods :code:`get_datastore_client` to | ||
connect to the emulator without having to set the environment variable :code:`DATASTORE_EMULATOR_HOST`. | ||
.. doctest:: | ||
>>> from testcontainers.google import DatastoreContainer | ||
>>> config = DatastoreContainer() | ||
>>> with config as datastore: | ||
... datastore_client = datastore.get_datastore_client() | ||
""" | ||
|
||
def __init__( | ||
self, | ||
image: str = "google/cloud-sdk:emulators", | ||
project: str = "test-project", | ||
port: int = 8081, | ||
**kwargs, | ||
) -> None: | ||
super().__init__(image=image, **kwargs) | ||
self.project = project | ||
self.port = port | ||
self.with_exposed_ports(self.port) | ||
self.with_command( | ||
f"gcloud beta emulators datastore start --no-store-on-disk --project={project} --host-port=0.0.0.0:{port}" | ||
) | ||
|
||
def get_datastore_emulator_host(self) -> str: | ||
return f"{self.get_container_host_ip()}:{self.get_exposed_port(self.port)}" | ||
|
||
def get_datastore_client(self, **kwargs) -> datastore.Client: | ||
wait_for_logs(self, "Dev App Server is now running.", timeout=30.0) | ||
env_vars = { | ||
"DATASTORE_DATASET": self.project, | ||
"DATASTORE_EMULATOR_HOST": self.get_datastore_emulator_host(), | ||
"DATASTORE_EMULATOR_HOST_PATH": f"{self.get_datastore_emulator_host()}/datastore", | ||
"DATASTORE_HOST": f"http://{self.get_datastore_emulator_host()}", | ||
"DATASTORE_PROJECT_ID": self.project, | ||
} | ||
with patch.dict(os.environ, env_vars): | ||
return datastore.Client(**kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters