Skip to content

Commit 423f955

Browse files
authored
Add Contract Tests for SNS (#296)
*Description of changes:* Set up SNS contract tests cases for `AWS::SNS::Topic` resource. Note: Was not able to get the `error` path to work so added a `TODO` comment for now. It seems boto3 handles the response code for this resource in a special way compared to the other resources. Will need to investigate further to figure out how we can add this case. *Test Plan:* ![Screenshot 2024-11-27 at 10 52 37 AM](https://github.com/user-attachments/assets/121a5829-e960-4292-b3c5-8ceb9c6f9d6c) By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
1 parent 642427e commit 423f955

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed

contract-tests/images/applications/botocore/botocore_server.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ def do_GET(self):
5252
self._handle_secretsmanager_request()
5353
if self.in_path("stepfunctions"):
5454
self._handle_stepfunctions_request()
55+
if self.in_path("sns"):
56+
self._handle_sns_request()
5557

5658
self._end_request(self.main_status)
5759

@@ -369,6 +371,27 @@ def _handle_stepfunctions_request(self) -> None:
369371
else:
370372
set_main_status(404)
371373

374+
def _handle_sns_request(self) -> None:
375+
sns_client = boto3.client("sns", endpoint_url=_AWS_SDK_ENDPOINT, region_name=_AWS_REGION)
376+
if self.in_path(_FAULT):
377+
set_main_status(500)
378+
try:
379+
fault_client = boto3.client("sns", endpoint_url=_FAULT_ENDPOINT, region_name=_AWS_REGION)
380+
fault_client.meta.events.register(
381+
"before-call.sns.GetTopicAttributes",
382+
lambda **kwargs: inject_500_error("GetTopicAttributes", **kwargs),
383+
)
384+
fault_client.get_topic_attributes(TopicArn="arn:aws:sns:us-west-2:000000000000:invalid-topic")
385+
except Exception as exception:
386+
print("Expected exception occurred", exception)
387+
elif self.in_path("gettopicattributes/test-topic"):
388+
set_main_status(200)
389+
sns_client.get_topic_attributes(
390+
TopicArn="arn:aws:sns:us-west-2:000000000000:test-topic",
391+
)
392+
else:
393+
set_main_status(404)
394+
372395
def _end_request(self, status_code: int):
373396
self.send_response_only(status_code)
374397
self.end_headers()
@@ -557,6 +580,11 @@ def prepare_aws_server() -> None:
557580
Name="testSecret", SecretString="secretValue", Description="This is a test secret"
558581
)
559582

583+
# Set up SNS so tests can access a topic.
584+
sns_client: BaseClient = boto3.client("sns", endpoint_url=_AWS_SDK_ENDPOINT, region_name=_AWS_REGION)
585+
create_topic_response = sns_client.create_topic(Name="test-topic")
586+
print("Created topic successfully:", create_topic_response)
587+
560588
# Set up Step Functions so tests can access a state machine and activity.
561589
sfn_client: BaseClient = boto3.client("stepfunctions", endpoint_url=_AWS_SDK_ENDPOINT, region_name=_AWS_REGION)
562590
sfn_response = sfn_client.list_state_machines()

contract-tests/tests/test/amazon/botocore/botocore_test.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
_AWS_SECRET_ARN: str = "aws.secretsmanager.secret.arn"
4848
_AWS_STATE_MACHINE_ARN: str = "aws.stepfunctions.state_machine.arn"
4949
_AWS_ACTIVITY_ARN: str = "aws.stepfunctions.activity.arn"
50+
_AWS_SNS_TOPIC_ARN: str = "aws.sns.topic.arn"
5051

5152

5253
# pylint: disable=too-many-public-methods,too-many-lines
@@ -86,7 +87,7 @@ def set_up_dependency_container(cls):
8687
cls._local_stack: LocalStackContainer = (
8788
LocalStackContainer(image="localstack/localstack:3.5.0")
8889
.with_name("localstack")
89-
.with_services("s3", "sqs", "dynamodb", "kinesis", "secretsmanager", "iam", "stepfunctions")
90+
.with_services("s3", "sqs", "dynamodb", "kinesis", "secretsmanager", "iam", "stepfunctions", "sns")
9091
.with_env("DEFAULT_REGION", "us-west-2")
9192
.with_kwargs(network=NETWORK_NAME, networking_config=local_stack_networking_config)
9293
)
@@ -752,6 +753,45 @@ def test_secretsmanager_fault(self):
752753
span_name="Secrets Manager.GetSecretValue",
753754
)
754755

756+
def test_sns_get_topic_attributes(self):
757+
self.do_test_requests(
758+
"sns/gettopicattributes/test-topic",
759+
"GET",
760+
200,
761+
0,
762+
0,
763+
rpc_service="SNS",
764+
remote_service="AWS::SNS",
765+
remote_operation="GetTopicAttributes",
766+
remote_resource_type="AWS::SNS::Topic",
767+
remote_resource_identifier="test-topic",
768+
cloudformation_primary_identifier="arn:aws:sns:us-west-2:000000000000:test-topic",
769+
request_specific_attributes={_AWS_SNS_TOPIC_ARN: "arn:aws:sns:us-west-2:000000000000:test-topic"},
770+
span_name="SNS.GetTopicAttributes",
771+
)
772+
773+
# TODO: Add error case for sns - our test setup is not setting the http status code properly
774+
# for this resource
775+
776+
def test_sns_fault(self):
777+
self.do_test_requests(
778+
"sns/fault",
779+
"GET",
780+
500,
781+
0,
782+
1,
783+
rpc_service="SNS",
784+
remote_service="AWS::SNS",
785+
remote_operation="GetTopicAttributes",
786+
remote_resource_type="AWS::SNS::Topic",
787+
remote_resource_identifier="invalid-topic",
788+
cloudformation_primary_identifier="arn:aws:sns:us-west-2:000000000000:invalid-topic",
789+
request_specific_attributes={
790+
_AWS_SNS_TOPIC_ARN: "arn:aws:sns:us-west-2:000000000000:invalid-topic",
791+
},
792+
span_name="SNS.GetTopicAttributes",
793+
)
794+
755795
def test_stepfunctions_describe_state_machine(self):
756796
self.do_test_requests(
757797
"stepfunctions/describestatemachine/my-state-machine",

0 commit comments

Comments
 (0)