Skip to content

Commit 81d7375

Browse files
committed
add sns contract tests
1 parent 642427e commit 81d7375

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed

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

Lines changed: 32 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,29 @@ 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(
385+
TopicArn="arn:aws:sns:us-west-2:000000000000:invalid-topic"
386+
)
387+
except Exception as exception:
388+
print("Expected exception occurred", exception)
389+
elif self.in_path("gettopicattributes/test-topic"):
390+
set_main_status(200)
391+
sns_client.get_topic_attributes(
392+
TopicArn="arn:aws:sns:us-west-2:000000000000:test-topic",
393+
)
394+
else:
395+
set_main_status(404)
396+
372397
def _end_request(self, status_code: int):
373398
self.send_response_only(status_code)
374399
self.end_headers()
@@ -557,6 +582,13 @@ def prepare_aws_server() -> None:
557582
Name="testSecret", SecretString="secretValue", Description="This is a test secret"
558583
)
559584

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

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

Lines changed: 43 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,47 @@ 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={
770+
_AWS_SNS_TOPIC_ARN: "arn:aws:sns:us-west-2:000000000000:test-topic"
771+
},
772+
span_name="SNS.GetTopicAttributes"
773+
)
774+
775+
# TODO: Add error case for sns - our test setup is not setting the http status code properly
776+
# for this resource
777+
778+
def test_sns_fault(self):
779+
self.do_test_requests(
780+
"sns/fault",
781+
"GET",
782+
500,
783+
0,
784+
1,
785+
rpc_service="SNS",
786+
remote_service="AWS::SNS",
787+
remote_operation="GetTopicAttributes",
788+
remote_resource_type="AWS::SNS::Topic",
789+
remote_resource_identifier="invalid-topic",
790+
cloudformation_primary_identifier="arn:aws:sns:us-west-2:000000000000:invalid-topic",
791+
request_specific_attributes={
792+
_AWS_SNS_TOPIC_ARN: "arn:aws:sns:us-west-2:000000000000:invalid-topic",
793+
},
794+
span_name="SNS.GetTopicAttributes",
795+
)
796+
755797
def test_stepfunctions_describe_state_machine(self):
756798
self.do_test_requests(
757799
"stepfunctions/describestatemachine/my-state-machine",

0 commit comments

Comments
 (0)