Skip to content

Commit f19e2f9

Browse files
authored
Initial set of test utilities (#11)
1 parent ac81a9e commit f19e2f9

File tree

12 files changed

+114
-59
lines changed

12 files changed

+114
-59
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from localstack.sdk.testing.decorators import cloudpods
2+
3+
__all__ = ["cloudpods"]
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import logging
2+
from functools import wraps
3+
4+
from localstack.sdk.pods import PodsClient
5+
from localstack.sdk.state import StateClient
6+
7+
LOG = logging.getLogger(__name__)
8+
9+
10+
def cloudpods(*args, **kwargs):
11+
"""This is a decorator that loads a cloud pod before a test and resets the state afterward."""
12+
13+
def decorator(func):
14+
@wraps(func)
15+
def wrapper(*test_args, **test_kwargs):
16+
if not (pod_name := kwargs.get("name")):
17+
raise Exception("Specify a Cloud Pod name in the `name` arg")
18+
pods_client = PodsClient()
19+
LOG.debug("Loading %s", pod_name)
20+
pods_client.load_pod(pod_name=pod_name)
21+
try:
22+
result = func(*test_args, **test_kwargs)
23+
finally:
24+
LOG.debug("Reset state of the container")
25+
state_client = StateClient()
26+
state_client.reset_state()
27+
return result
28+
29+
return wrapper
30+
31+
return decorator

localstack-sdk-python/localstack/sdk/testing/pytest/__init__.py

Whitespace-only changes.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import pytest
2+
3+
from localstack.sdk.state import StateClient
4+
5+
6+
@pytest.fixture
7+
def reset_state():
8+
"""This fixture is used to completely reset the state of LocalStack after a test runs."""
9+
yield
10+
state_client = StateClient()
11+
state_client.reset_state()

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ where = ["localstack-sdk-python/"]
4747
include = ["localstack*"]
4848
exclude = ["tests*"]
4949

50+
[project.entry-points.pytest11]
51+
localstack = "localstack.sdk.testing.pytest.plugins"
52+
5053
[tool.ruff]
5154
# Always generate Python 3.8-compatible code.
5255
target-version = "py38"

tests/conftest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pytest_plugins = ["pytester"]

tests/integration/test_aws.py

Lines changed: 10 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import json
22
import random
33

4-
import boto3
5-
64
import localstack.sdk.aws
7-
from tests.utils import retry, short_uid
5+
from tests.utils import boto_client, retry, short_uid
86

97
SAMPLE_SIMPLE_EMAIL = {
108
"Subject": {
@@ -25,13 +23,7 @@ class TestLocalStackAWS:
2523
client = localstack.sdk.aws.AWSClient()
2624

2725
def test_list_sqs_messages(self):
28-
sqs_client = boto3.client(
29-
"sqs",
30-
endpoint_url=self.client.configuration.host,
31-
region_name="us-east-1",
32-
aws_access_key_id="test",
33-
aws_secret_access_key="test",
34-
)
26+
sqs_client = boto_client("sqs")
3527
queue_name = f"queue-{short_uid()}"
3628
sqs_client.create_queue(QueueName=queue_name)
3729
queue_url = sqs_client.get_queue_url(QueueName=queue_name)["QueueUrl"]
@@ -49,18 +41,12 @@ def test_list_sqs_messages(self):
4941
assert len(messages) == 5
5042

5143
def test_list_sqs_messages_from_account_region(self):
52-
sqs_client_us = boto3.client(
53-
"sqs",
54-
endpoint_url=self.client.configuration.host,
55-
region_name="us-east-1",
56-
aws_access_key_id="test",
57-
aws_secret_access_key="test",
58-
)
44+
sqs_client = boto_client("sqs")
5945
queue_name = f"queue-{short_uid()}"
60-
sqs_client_us.create_queue(QueueName=queue_name)
61-
queue_url = sqs_client_us.get_queue_url(QueueName=queue_name)["QueueUrl"]
46+
sqs_client.create_queue(QueueName=queue_name)
47+
queue_url = sqs_client.get_queue_url(QueueName=queue_name)["QueueUrl"]
6248

63-
send_result = sqs_client_us.send_message(
49+
send_result = sqs_client.send_message(
6450
QueueUrl=queue_url,
6551
MessageBody=json.dumps({"event": "random-event", "message": "random-message"}),
6652
)
@@ -72,13 +58,7 @@ def test_list_sqs_messages_from_account_region(self):
7258
assert messages[0].message_id == send_result["MessageId"]
7359

7460
def test_empty_queue(self):
75-
sqs_client = boto3.client(
76-
"sqs",
77-
endpoint_url=self.client.configuration.host,
78-
region_name="us-east-1",
79-
aws_access_key_id="test",
80-
aws_secret_access_key="test",
81-
)
61+
sqs_client = boto_client("sqs")
8262
queue_name = f"queue-{short_uid()}"
8363
sqs_client.create_queue(QueueName=queue_name)
8464
messages = self.client.list_sqs_messages(
@@ -87,14 +67,7 @@ def test_empty_queue(self):
8767
assert messages == []
8868

8969
def test_get_and_discard_ses_messages(self):
90-
aws_client = boto3.client(
91-
"ses",
92-
endpoint_url=self.client.configuration.host,
93-
region_name="us-east-1",
94-
aws_access_key_id="test",
95-
aws_secret_access_key="test",
96-
)
97-
70+
aws_client = boto_client("ses")
9871
email = f"user-{short_uid()}@example.com"
9972
aws_client.verify_email_address(EmailAddress=email)
10073

@@ -143,14 +116,7 @@ def test_get_and_discard_ses_messages(self):
143116
assert not self.client.get_ses_messages()
144117

145118
def test_sns_platform_endpoint_messages(self):
146-
client = boto3.client(
147-
"sns",
148-
endpoint_url=self.client.configuration.host,
149-
region_name="us-east-1",
150-
aws_access_key_id="test",
151-
aws_secret_access_key="test",
152-
)
153-
119+
client = boto_client("sns")
154120
# create a topic
155121
topic_name = f"topic-{short_uid()}"
156122
topic_arn = client.create_topic(Name=topic_name)["TopicArn"]
@@ -219,14 +185,7 @@ def test_sns_platform_endpoint_messages(self):
219185
], "platform messages not cleared"
220186

221187
def test_sns_messages(self):
222-
client = boto3.client(
223-
"sns",
224-
endpoint_url=self.client.configuration.host,
225-
region_name="us-east-1",
226-
aws_access_key_id="test",
227-
aws_secret_access_key="test",
228-
)
229-
188+
client = boto_client("sns")
230189
numbers = [
231190
f"+{random.randint(100000000, 9999999999)}",
232191
f"+{random.randint(100000000, 9999999999)}",

tests/integration/test_state.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
1-
import boto3
21
import pytest
32

43
from localstack.sdk.state import StateClient
4+
from tests.utils import boto_client
55

66

77
class TestStateClient:
88
client = StateClient()
99

1010
def test_reset_state(self):
11-
sqs_client = boto3.client(
12-
"sqs",
13-
endpoint_url=self.client.configuration.host,
14-
region_name="us-east-1",
15-
aws_access_key_id="test",
16-
aws_secret_access_key="test",
17-
)
11+
sqs_client = boto_client("sqs")
1812
sqs_client.create_queue(QueueName="test-queue")
1913
url = sqs_client.get_queue_url(QueueName="test-queue")["QueueUrl"]
2014
assert url

tests/testing/__init__.py

Whitespace-only changes.

tests/testing/test_decorators.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import pytest
2+
3+
from localstack.sdk.pods import PodsClient
4+
from localstack.sdk.state import StateClient
5+
from localstack.sdk.testing import cloudpods
6+
from tests.utils import boto_client
7+
8+
DECORATOR_POD_NAME = "ls-sdk-pod-decorator"
9+
QUEUE_NAME = "ls-decorator-queue"
10+
11+
12+
@pytest.fixture(scope="class", autouse=True)
13+
def create_state_and_pod():
14+
pods_client = PodsClient()
15+
sqs_client = boto_client("sqs")
16+
queue_url = sqs_client.create_queue(QueueName=QUEUE_NAME)["QueueUrl"]
17+
pods_client.save_pod(DECORATOR_POD_NAME)
18+
sqs_client.delete_queue(QueueUrl=queue_url)
19+
yield
20+
state_client = StateClient()
21+
state_client.reset_state()
22+
pods_client.delete_pod(DECORATOR_POD_NAME)
23+
24+
25+
class TestPodsDecorators:
26+
@cloudpods(name=DECORATOR_POD_NAME)
27+
def test_pod_load_decorator(self):
28+
sqs_client = boto_client("sqs")
29+
assert sqs_client.get_queue_url(QueueName=QUEUE_NAME), "state from pod not restored"

0 commit comments

Comments
 (0)