-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
114 additions
and
59 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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from localstack.sdk.testing.decorators import cloudpods | ||
|
||
__all__ = ["cloudpods"] |
31 changes: 31 additions & 0 deletions
31
localstack-sdk-python/localstack/sdk/testing/decorators.py
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,31 @@ | ||
import logging | ||
from functools import wraps | ||
|
||
from localstack.sdk.pods import PodsClient | ||
from localstack.sdk.state import StateClient | ||
|
||
LOG = logging.getLogger(__name__) | ||
|
||
|
||
def cloudpods(*args, **kwargs): | ||
"""This is a decorator that loads a cloud pod before a test and resets the state afterward.""" | ||
|
||
def decorator(func): | ||
@wraps(func) | ||
def wrapper(*test_args, **test_kwargs): | ||
if not (pod_name := kwargs.get("name")): | ||
raise Exception("Specify a Cloud Pod name in the `name` arg") | ||
pods_client = PodsClient() | ||
LOG.debug("Loading %s", pod_name) | ||
pods_client.load_pod(pod_name=pod_name) | ||
try: | ||
result = func(*test_args, **test_kwargs) | ||
finally: | ||
LOG.debug("Reset state of the container") | ||
state_client = StateClient() | ||
state_client.reset_state() | ||
return result | ||
|
||
return wrapper | ||
|
||
return decorator |
Empty file.
11 changes: 11 additions & 0 deletions
11
localstack-sdk-python/localstack/sdk/testing/pytest/plugins.py
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,11 @@ | ||
import pytest | ||
|
||
from localstack.sdk.state import StateClient | ||
|
||
|
||
@pytest.fixture | ||
def reset_state(): | ||
"""This fixture is used to completely reset the state of LocalStack after a test runs.""" | ||
yield | ||
state_client = StateClient() | ||
state_client.reset_state() |
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
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 @@ | ||
pytest_plugins = ["pytester"] |
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
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
Empty file.
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,29 @@ | ||
import pytest | ||
|
||
from localstack.sdk.pods import PodsClient | ||
from localstack.sdk.state import StateClient | ||
from localstack.sdk.testing import cloudpods | ||
from tests.utils import boto_client | ||
|
||
DECORATOR_POD_NAME = "ls-sdk-pod-decorator" | ||
QUEUE_NAME = "ls-decorator-queue" | ||
|
||
|
||
@pytest.fixture(scope="class", autouse=True) | ||
def create_state_and_pod(): | ||
pods_client = PodsClient() | ||
sqs_client = boto_client("sqs") | ||
queue_url = sqs_client.create_queue(QueueName=QUEUE_NAME)["QueueUrl"] | ||
pods_client.save_pod(DECORATOR_POD_NAME) | ||
sqs_client.delete_queue(QueueUrl=queue_url) | ||
yield | ||
state_client = StateClient() | ||
state_client.reset_state() | ||
pods_client.delete_pod(DECORATOR_POD_NAME) | ||
|
||
|
||
class TestPodsDecorators: | ||
@cloudpods(name=DECORATOR_POD_NAME) | ||
def test_pod_load_decorator(self): | ||
sqs_client = boto_client("sqs") | ||
assert sqs_client.get_queue_url(QueueName=QUEUE_NAME), "state from pod not restored" |
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,12 @@ | ||
def test_reset_state(pytester): | ||
"""Smoke test for the reset_state fixture""" | ||
pytester.makeconftest("") | ||
pytester.makepyfile( | ||
""" | ||
def test_hello_default(reset_state): | ||
pass | ||
""" | ||
) | ||
|
||
result = pytester.runpytest() | ||
result.assert_outcomes(passed=1) |
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