|
23 | 23 | import org.mockito.InOrder; |
24 | 24 | import org.mockito.Mock; |
25 | 25 | import org.mockito.junit.jupiter.MockitoExtension; |
| 26 | +import org.mockito.junit.jupiter.MockitoSettings; |
| 27 | +import org.mockito.quality.Strictness; |
26 | 28 | import org.opensearch.dataprepper.metrics.PluginMetrics; |
27 | 29 | import org.opensearch.dataprepper.model.acknowledgements.AcknowledgementSet; |
28 | 30 | import org.opensearch.dataprepper.model.acknowledgements.AcknowledgementSetManager; |
|
45 | 47 | import software.amazon.awssdk.services.sqs.model.ReceiveMessageRequest; |
46 | 48 | import software.amazon.awssdk.services.sqs.model.ReceiveMessageResponse; |
47 | 49 | import software.amazon.awssdk.services.sqs.model.SqsException; |
| 50 | +import software.amazon.awssdk.awscore.exception.AwsServiceException; |
| 51 | +import software.amazon.awssdk.services.sqs.model.KmsAccessDeniedException; |
| 52 | +import software.amazon.awssdk.services.sqs.model.KmsNotFoundException; |
| 53 | +import software.amazon.awssdk.services.sqs.model.KmsThrottledException; |
| 54 | +import software.amazon.awssdk.services.sqs.model.QueueDoesNotExistException; |
48 | 55 | import software.amazon.awssdk.services.sts.model.StsException; |
49 | 56 |
|
50 | 57 | import java.io.IOException; |
|
92 | 99 | import static org.opensearch.dataprepper.plugins.source.s3.SqsWorker.SQS_VISIBILITY_TIMEOUT_CHANGED_COUNT_METRIC_NAME; |
93 | 100 |
|
94 | 101 | @ExtendWith(MockitoExtension.class) |
| 102 | +@MockitoSettings(strictness = Strictness.LENIENT) |
95 | 103 | class SqsWorkerTest { |
96 | 104 | private SqsClient sqsClient; |
97 | 105 | private S3Service s3Service; |
@@ -866,6 +874,88 @@ private static String createEventBridgeNotification(final Instant startTime) { |
866 | 874 | "\"reason\":\"PutObject\"}}"; |
867 | 875 | } |
868 | 876 |
|
| 877 | + @Test |
| 878 | + void testRecordSqsException_KmsThrottledException() { |
| 879 | + testRecordSqsException(mock(KmsThrottledException.class), SqsWorker.SQS_MESSAGE_THROTTLED_METRIC_NAME); |
| 880 | + } |
| 881 | + |
| 882 | + @Test |
| 883 | + void testRecordSqsException_403Status() { |
| 884 | + final SqsException sqsException = mock(SqsException.class); |
| 885 | + when(sqsException.statusCode()).thenReturn(403); |
| 886 | + testRecordSqsException(sqsException, SqsWorker.SQS_MESSAGE_ACCESS_DENIED_METRIC_NAME); |
| 887 | + } |
| 888 | + |
| 889 | + @Test |
| 890 | + void testRecordSqsException_KmsAccessDeniedException() { |
| 891 | + testRecordSqsException(mock(KmsAccessDeniedException.class), SqsWorker.SQS_MESSAGE_ACCESS_DENIED_METRIC_NAME); |
| 892 | + } |
| 893 | + |
| 894 | + @Test |
| 895 | + void testRecordSqsException_404Status() { |
| 896 | + final SqsException sqsException = mock(SqsException.class); |
| 897 | + when(sqsException.statusCode()).thenReturn(404); |
| 898 | + testRecordSqsException(sqsException, SqsWorker.SQS_QUEUE_NOT_FOUND_METRIC_NAME); |
| 899 | + } |
| 900 | + |
| 901 | + @Test |
| 902 | + void testRecordSqsException_QueueDoesNotExistException() { |
| 903 | + testRecordSqsException(mock(QueueDoesNotExistException.class), SqsWorker.SQS_QUEUE_NOT_FOUND_METRIC_NAME); |
| 904 | + } |
| 905 | + |
| 906 | + @Test |
| 907 | + void testRecordSqsException_KmsNotFoundException() { |
| 908 | + testRecordSqsException(mock(KmsNotFoundException.class), SqsWorker.SQS_QUEUE_NOT_FOUND_METRIC_NAME); |
| 909 | + } |
| 910 | + |
| 911 | + @Test |
| 912 | + void testRecordSqsException_isThrottlingException() { |
| 913 | + final SqsException sqsException = mock(SqsException.class); |
| 914 | + when(sqsException.isThrottlingException()).thenReturn(true); |
| 915 | + testRecordSqsException(sqsException, SqsWorker.SQS_MESSAGE_THROTTLED_METRIC_NAME); |
| 916 | + } |
| 917 | + |
| 918 | + private void testRecordSqsException(AwsServiceException exception, String metricName) { |
| 919 | + final Counter expectedCounter = mock(Counter.class); |
| 920 | + final Counter sqsMessagesReceivedCounter = mock(Counter.class); |
| 921 | + final Counter sqsMessagesDeletedCounter = mock(Counter.class); |
| 922 | + final Counter sqsMessagesFailedCounter = mock(Counter.class); |
| 923 | + final Counter s3ObjectsEmptyCounter = mock(Counter.class); |
| 924 | + final Counter sqsMessagesDeleteFailedCounter = mock(Counter.class); |
| 925 | + final Counter acknowledgementSetCallbackCounter = mock(Counter.class); |
| 926 | + final Counter sqsVisibilityTimeoutChangedCount = mock(Counter.class); |
| 927 | + final Counter sqsVisibilityTimeoutChangeFailedCount = mock(Counter.class); |
| 928 | + final Counter sqsReceiveMessagesFailedCounter = mock(Counter.class); |
| 929 | + final Timer sqsMessageDelayTimer = mock(Timer.class); |
| 930 | + final Counter sqsMessageAccessDeniedCounter = mock(Counter.class); |
| 931 | + final Counter sqsMessageThrottledCounter = mock(Counter.class); |
| 932 | + final Counter sqsQueueNotFoundCounter = mock(Counter.class); |
| 933 | + |
| 934 | + when(pluginMetrics.counter(SqsWorker.SQS_MESSAGES_RECEIVED_METRIC_NAME)).thenReturn(sqsMessagesReceivedCounter); |
| 935 | + when(pluginMetrics.counter(SqsWorker.SQS_MESSAGES_DELETED_METRIC_NAME)).thenReturn(sqsMessagesDeletedCounter); |
| 936 | + when(pluginMetrics.counter(SqsWorker.SQS_MESSAGES_FAILED_METRIC_NAME)).thenReturn(sqsMessagesFailedCounter); |
| 937 | + when(pluginMetrics.counter(SqsWorker.S3_OBJECTS_EMPTY_METRIC_NAME)).thenReturn(s3ObjectsEmptyCounter); |
| 938 | + when(pluginMetrics.counter(SqsWorker.SQS_MESSAGES_DELETE_FAILED_METRIC_NAME)).thenReturn(sqsMessagesDeleteFailedCounter); |
| 939 | + when(pluginMetrics.counter(SqsWorker.ACKNOWLEDGEMENT_SET_CALLACK_METRIC_NAME)).thenReturn(acknowledgementSetCallbackCounter); |
| 940 | + when(pluginMetrics.counter(SqsWorker.SQS_VISIBILITY_TIMEOUT_CHANGED_COUNT_METRIC_NAME)).thenReturn(sqsVisibilityTimeoutChangedCount); |
| 941 | + when(pluginMetrics.counter(SqsWorker.SQS_VISIBILITY_TIMEOUT_CHANGE_FAILED_COUNT_METRIC_NAME)).thenReturn(sqsVisibilityTimeoutChangeFailedCount); |
| 942 | + when(pluginMetrics.counter(SqsWorker.SQS_RECEIVE_MESSAGES_FAILED_METRIC_NAME)).thenReturn(sqsReceiveMessagesFailedCounter); |
| 943 | + when(pluginMetrics.timer(SqsWorker.SQS_MESSAGE_DELAY_METRIC_NAME)).thenReturn(sqsMessageDelayTimer); |
| 944 | + when(pluginMetrics.counter(SqsWorker.SQS_MESSAGE_ACCESS_DENIED_METRIC_NAME)).thenReturn( |
| 945 | + metricName.equals(SqsWorker.SQS_MESSAGE_ACCESS_DENIED_METRIC_NAME) ? expectedCounter : sqsMessageAccessDeniedCounter); |
| 946 | + when(pluginMetrics.counter(SqsWorker.SQS_MESSAGE_THROTTLED_METRIC_NAME)).thenReturn( |
| 947 | + metricName.equals(SqsWorker.SQS_MESSAGE_THROTTLED_METRIC_NAME) ? expectedCounter : sqsMessageThrottledCounter); |
| 948 | + when(pluginMetrics.counter(SqsWorker.SQS_QUEUE_NOT_FOUND_METRIC_NAME)).thenReturn( |
| 949 | + metricName.equals(SqsWorker.SQS_QUEUE_NOT_FOUND_METRIC_NAME) ? expectedCounter : sqsQueueNotFoundCounter); |
| 950 | + |
| 951 | + final SqsWorker testWorker = createObjectUnderTest(); |
| 952 | + |
| 953 | + testWorker.recordSqsException(exception); |
| 954 | + |
| 955 | + verify(expectedCounter).increment(); |
| 956 | + verifyNoMoreInteractions(expectedCounter); |
| 957 | + } |
| 958 | + |
869 | 959 | private static String createSecurityLakeNotification(final Instant startTime) { |
870 | 960 | return "{\"source\":\"aws.s3\",\"time\":\"" + startTime + "\",\"account\":\"123456789012\",\"region\":\"ca-central-1\"," + |
871 | 961 | "\"resources\":[\"arn:aws:s3:::example-bucket\"],\"detail\":{\"bucket\":{\"name\":\"example-bucket\"}," + |
|
0 commit comments