Skip to content

Commit 62d7562

Browse files
committed
Add get_client to LocalStackContainer and fix doctests.
1 parent 908b840 commit 62d7562

File tree

9 files changed

+43
-26
lines changed

9 files changed

+43
-26
lines changed

localstack/setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
long_description_content_type="text/x-rst",
1212
url="https://github.com/testcontainers/testcontainers-python",
1313
install_requires=[
14+
"boto3",
1415
"testcontainers-core",
1516
],
1617
python_requires=">=3.7",

localstack/testcontainers/localstack/__init__.py

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@
1010
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
1111
# License for the specific language governing permissions and limitations
1212
# under the License.
13+
import boto3
14+
import functools as ft
15+
import os
1316
from testcontainers.core.waiting_utils import wait_for_logs
1417
from testcontainers.core.container import DockerContainer
18+
from typing import Any, Optional
1519

1620

1721
class LocalStackContainer(DockerContainer):
@@ -24,28 +28,21 @@ class LocalStackContainer(DockerContainer):
2428
2529
>>> from testcontainers.localstack import LocalStackContainer
2630
27-
>>> with LocalStackContainer(image="localstack/localstack:0.11.4") as localstack:
28-
... dynamo_endpoint = localstack.get_url()
29-
<testcontainers.localstack.LocalStackContainer object at 0x...>
30-
31-
The endpoint can be used to create a client with the boto3 library:
32-
33-
.. doctest::
34-
35-
>>> import boto3
36-
>>> from testcontainers.localstack import LocalStackContainer
37-
38-
>>> with LocalStackContainer(image="localstack/localstack:0.11.4") as localstack:
39-
... dynamo_endpoint = localstack.get_url()
40-
... dynamo_client = boto3.client("dynamodb", endpoint_url=dynamo_endpoint,
41-
... region_name="us-west-1")
42-
... scan_result = dynamo_client.scan(TableName='foo')
31+
>>> with LocalStackContainer(image="localstack/localstack:2.0.1") as localstack:
32+
... dynamo_client = localstack.get_client("dynamodb")
33+
... tables = dynamo_client.list_tables()
34+
>>> tables
35+
{'TableNames': [], ...}
4336
"""
44-
def __init__(self, image: str = 'localstack/localstack:0.11.4', edge_port: int = 4566,
45-
**kwargs) -> None:
37+
def __init__(self, image: str = 'localstack/localstack:2.0.1', edge_port: int = 4566,
38+
region_name: Optional[str] = None, **kwargs) -> None:
4639
super(LocalStackContainer, self).__init__(image, **kwargs)
4740
self.edge_port = edge_port
41+
self.region_name = region_name or os.environ.get("AWS_DEFAULT_REGION", "us-west-1")
4842
self.with_exposed_ports(self.edge_port)
43+
self.with_env("AWS_DEFAULT_REGION", self.region_name)
44+
self.with_env("AWS_ACCESS_KEY_ID", "testcontainers-localstack")
45+
self.with_env("AWS_SECRET_ACCESS_KEY", "testcontainers-localstack")
4946

5047
def with_services(self, *services) -> "LocalStackContainer":
5148
"""
@@ -69,6 +66,17 @@ def get_url(self) -> str:
6966
port = self.get_exposed_port(self.edge_port)
7067
return f'http://{host}:{port}'
7168

69+
@ft.wraps(boto3.client)
70+
def get_client(self, name, **kwargs) -> Any:
71+
kwargs_ = {
72+
"endpoint_url": self.get_url(),
73+
"region_name": self.region_name,
74+
"aws_access_key_id": "testcontainers-localstack",
75+
"aws_secret_access_key": "testcontainers-localstack",
76+
}
77+
kwargs_.update(kwargs)
78+
return boto3.client(name, **kwargs_)
79+
7280
def start(self, timeout: float = 60) -> "LocalStackContainer":
7381
super().start()
7482
wait_for_logs(self, r'Ready\.\n', timeout=timeout)

localstack/tests/test_localstack.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ def test_docker_run_localstack():
1010
services = json.loads(resp.read().decode())['services']
1111

1212
# Check that all services are running
13-
assert all(value == 'running' for value in services.values())
13+
assert all(value == 'available' for value in services.values())
1414
# Check that some of the services keys
15-
assert all(test_service in services.keys() for test_service in ['dynamodb', 'sns', 'sqs'])
15+
assert all(test_service in services for test_service in ['dynamodb', 'sns', 'sqs'])
16+
17+
18+
def test_localstack_boto3():
19+
from testcontainers.localstack import LocalStackContainer
20+
21+
with LocalStackContainer(image="localstack/localstack:2.0.1") as localstack:
22+
dynamo_client = localstack.get_client("dynamodb")
23+
tables = dynamo_client.list_tables()
24+
assert tables["TableNames"] == []

requirements.in

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
-e file:rabbitmq
2222
-e file:redis
2323
-e file:selenium
24-
boto3 # Required for localstack doctest.
2524
cryptography<37
2625
flake8<3.8.0 # 3.8.0 adds a dependency on importlib-metadata which conflicts with other packages.
2726
pg8000

requirements/3.10.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ bcrypt==4.0.1
9797
bleach==6.0.0
9898
# via readme-renderer
9999
boto3==1.26.112
100-
# via -r requirements.in
100+
# via testcontainers-localstack
101101
botocore==1.29.112
102102
# via
103103
# boto3

requirements/3.11.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ bcrypt==4.0.1
9797
bleach==6.0.0
9898
# via readme-renderer
9999
boto3==1.26.112
100-
# via -r requirements.in
100+
# via testcontainers-localstack
101101
botocore==1.29.112
102102
# via
103103
# boto3

requirements/3.7.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ bcrypt==4.0.1
101101
bleach==6.0.0
102102
# via readme-renderer
103103
boto3==1.26.112
104-
# via -r requirements.in
104+
# via testcontainers-localstack
105105
botocore==1.29.112
106106
# via
107107
# boto3

requirements/3.8.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ bcrypt==4.0.1
101101
bleach==6.0.0
102102
# via readme-renderer
103103
boto3==1.26.112
104-
# via -r requirements.in
104+
# via testcontainers-localstack
105105
botocore==1.29.112
106106
# via
107107
# boto3

requirements/3.9.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ bcrypt==4.0.1
9797
bleach==6.0.0
9898
# via readme-renderer
9999
boto3==1.26.112
100-
# via -r requirements.in
100+
# via testcontainers-localstack
101101
botocore==1.29.112
102102
# via
103103
# boto3

0 commit comments

Comments
 (0)