Skip to content

Commit

Permalink
chore: adding comments to test_sns_helper.py
Browse files Browse the repository at this point in the history
  • Loading branch information
drduhe committed Oct 25, 2024
1 parent 5f669be commit 519144f
Showing 1 changed file with 42 additions and 2 deletions.
44 changes: 42 additions & 2 deletions test/aws/osml/model_runner/status/test_sns_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,21 @@
from botocore.exceptions import ClientError
from moto import mock_aws

# Mock to simulate a ClientError when trying to publish to SNS
TEST_MOCK_PUBLISH_EXCEPTION = Mock(side_effect=ClientError({"Error": {"Code": 500, "Message": "ClientError"}}, "publish"))


@mock_aws
class TestSnsHelper(TestCase):
def setUp(self):
"""
Set up the mock AWS environment. This includes creating an SNS topic and an SQS queue,
subscribing the queue to the SNS topic, and initializing the SNSHelper instance.
"""
from aws.osml.model_runner.app_config import BotoConfig
from aws.osml.model_runner.status.sns_helper import SNSHelper

# Initialize mock SNS and SQS clients
self.sns = boto3.client("sns", config=BotoConfig.default)
sns_response = self.sns.create_topic(Name=os.environ["IMAGE_STATUS_TOPIC"])
self.mock_topic_arn = sns_response.get("TopicArn")
Expand All @@ -29,25 +35,38 @@ def setUp(self):
queue_attributes = self.sqs.get_queue_attributes(QueueUrl=self.mock_queue_url, AttributeNames=["QueueArn"])
queue_arn = queue_attributes.get("Attributes").get("QueueArn")

# Subscribe the mock SQS queue to the SNS topic
self.sns.subscribe(TopicArn=self.mock_topic_arn, Protocol="sqs", Endpoint=queue_arn)

# Initialize SNSHelper with the mock topic ARN
self.image_status_sns = SNSHelper(self.mock_topic_arn)

def tearDown(self):
"""
Clean up by setting all instance variables to None.
"""
self.sns = None
self.mock_topic_arn = None
self.sqs = None
self.mock_queue_url = None
self.image_status_sns = None

def test_publish_message_success(self):
"""
Test that a valid message with string and binary attributes is successfully published to SNS
and received by the subscribed SQS queue. Verify that message attributes are correctly transformed.
"""
mock_message = "test message 1"
mock_attributes = {"key1": "string data", "bin1": b"binary data"}
expected_attributes = {
"key1": {"Type": "String", "Value": "string data"},
"bin1": {"Type": "Binary", "Value": "YmluYXJ5IGRhdGE="},
"bin1": {"Type": "Binary", "Value": "YmluYXJ5IGRhdGE="}, # Base64 encoded binary data
}

# Publish the message
self.image_status_sns.publish_message(mock_message, mock_attributes)

# Retrieve and verify the message from the mock SQS queue
messages = self.sqs.receive_message(QueueUrl=self.mock_queue_url, MessageAttributeNames=["key1", "bin1"]).get(
"Messages"
)
Expand All @@ -57,13 +76,21 @@ def test_publish_message_success(self):
assert message_body.get("MessageAttributes") == expected_attributes

def test_publish_message_success_drop_invalid_types(self):
"""
Test that a message with invalid attribute types (e.g., integer) drops the invalid attributes
and only publishes valid string and binary attributes.
"""
mock_message = "test invalid data gets removed"
mock_attributes = {"key1": "string data", "bin1": b"binary data", "invalid_int_data": 1}
expected_attributes = {
"key1": {"Type": "String", "Value": "string data"},
"bin1": {"Type": "Binary", "Value": "YmluYXJ5IGRhdGE="},
"bin1": {"Type": "Binary", "Value": "YmluYXJ5IGRhdGE="}, # Base64 encoded binary data
}

# Publish the message
self.image_status_sns.publish_message(mock_message, mock_attributes)

# Retrieve and verify the message from the mock SQS queue
messages = self.sqs.receive_message(QueueUrl=self.mock_queue_url, MessageAttributeNames=["key1", "bin1"]).get(
"Messages"
)
Expand All @@ -73,20 +100,33 @@ def test_publish_message_success_drop_invalid_types(self):
assert message_body.get("MessageAttributes") == expected_attributes

def test_publish_message_failure(self):
"""
Test that when the SNS publish operation fails, an SNSPublishException is raised.
"""
from aws.osml.model_runner.status.exceptions import SNSPublishException

# Mock the publish method to simulate a failure
self.image_status_sns.sns_client.publish = TEST_MOCK_PUBLISH_EXCEPTION
mock_message = "test message 1"
mock_attributes = {"key1": "string data", "bin1": b"binary data"}

# Expecting SNSPublishException due to the mock exception
with self.assertRaises(SNSPublishException):
self.image_status_sns.publish_message(mock_message, mock_attributes)

def test_publish_message_no_topic(self):
"""
Test that if no SNS topic is configured, the publish_message method should gracefully return None
and not attempt to send a message.
"""
from aws.osml.model_runner.status.sns_helper import SNSHelper

# Initialize SNSHelper with no topic
image_status_sns = SNSHelper(None)
mock_message = "test message 1"
mock_attributes = {"key1": "string data", "bin1": b"binary data"}

# Verify that publishing returns None when no topic is configured
response = image_status_sns.publish_message(mock_message, mock_attributes)
assert response is None

Expand Down

0 comments on commit 519144f

Please sign in to comment.