|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package org.opensearch.dataprepper.plugins.source.s3; |
| 7 | + |
| 8 | +import io.micrometer.core.instrument.Counter; |
| 9 | +import io.micrometer.core.instrument.DistributionSummary; |
| 10 | +import io.micrometer.core.instrument.Meter; |
| 11 | +import io.micrometer.core.instrument.Tags; |
| 12 | +import io.micrometer.core.instrument.Timer; |
| 13 | +import io.micrometer.core.instrument.noop.NoopTimer; |
| 14 | +import org.junit.jupiter.api.BeforeEach; |
| 15 | +import org.junit.jupiter.api.Test; |
| 16 | +import org.opensearch.dataprepper.model.buffer.Buffer; |
| 17 | +import org.opensearch.dataprepper.model.event.Event; |
| 18 | +import org.opensearch.dataprepper.model.record.Record; |
| 19 | +import org.opensearch.dataprepper.plugins.codec.CompressionOption; |
| 20 | +import org.opensearch.dataprepper.plugins.source.s3.configuration.S3DataSelection; |
| 21 | +import org.opensearch.dataprepper.plugins.source.s3.ownership.BucketOwnerProvider; |
| 22 | +import software.amazon.awssdk.regions.Region; |
| 23 | +import software.amazon.awssdk.services.s3.S3Client; |
| 24 | + |
| 25 | +import java.io.IOException; |
| 26 | +import java.time.Duration; |
| 27 | +import java.time.Instant; |
| 28 | +import java.util.Collection; |
| 29 | +import java.util.Optional; |
| 30 | +import java.util.UUID; |
| 31 | +import java.util.function.Consumer; |
| 32 | + |
| 33 | +import static org.hamcrest.CoreMatchers.equalTo; |
| 34 | +import static org.hamcrest.CoreMatchers.notNullValue; |
| 35 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 36 | +import static org.hamcrest.Matchers.greaterThanOrEqualTo; |
| 37 | +import static org.mockito.ArgumentMatchers.anyCollection; |
| 38 | +import static org.mockito.ArgumentMatchers.anyInt; |
| 39 | +import static org.mockito.Mockito.doAnswer; |
| 40 | +import static org.mockito.Mockito.mock; |
| 41 | +import static org.mockito.Mockito.verify; |
| 42 | +import static org.mockito.Mockito.when; |
| 43 | + |
| 44 | +class S3DataSelectionIT { |
| 45 | + private static final int TIMEOUT_IN_MILLIS = 200; |
| 46 | + |
| 47 | + private S3Client s3Client; |
| 48 | + private S3ObjectGenerator s3ObjectGenerator; |
| 49 | + private String bucket; |
| 50 | + private Buffer<Record<Event>> buffer; |
| 51 | + private S3ObjectPluginMetrics s3ObjectPluginMetrics; |
| 52 | + private BucketOwnerProvider bucketOwnerProvider; |
| 53 | + private EventMetadataModifier eventMetadataModifier; |
| 54 | + |
| 55 | + @BeforeEach |
| 56 | + void setUp() { |
| 57 | + s3Client = S3Client.builder() |
| 58 | + .region(Region.of(System.getProperty("tests.s3source.region"))) |
| 59 | + .build(); |
| 60 | + bucket = System.getProperty("tests.s3source.bucket"); |
| 61 | + s3ObjectGenerator = new S3ObjectGenerator(s3Client, bucket); |
| 62 | + buffer = mock(Buffer.class); |
| 63 | + |
| 64 | + eventMetadataModifier = new EventMetadataModifier(S3SourceConfig.DEFAULT_METADATA_ROOT_KEY, false); |
| 65 | + bucketOwnerProvider = b -> Optional.empty(); |
| 66 | + |
| 67 | + s3ObjectPluginMetrics = mock(S3ObjectPluginMetrics.class); |
| 68 | + final Counter counter = mock(Counter.class); |
| 69 | + final DistributionSummary distributionSummary = mock(DistributionSummary.class); |
| 70 | + final Timer timer = new NoopTimer(new Meter.Id("test", Tags.empty(), null, null, Meter.Type.TIMER)); |
| 71 | + when(s3ObjectPluginMetrics.getS3ObjectsFailedCounter()).thenReturn(counter); |
| 72 | + when(s3ObjectPluginMetrics.getS3ObjectsSucceededCounter()).thenReturn(counter); |
| 73 | + when(s3ObjectPluginMetrics.getS3ObjectsFailedAccessDeniedCounter()).thenReturn(counter); |
| 74 | + when(s3ObjectPluginMetrics.getS3ObjectsFailedNotFoundCounter()).thenReturn(counter); |
| 75 | + when(s3ObjectPluginMetrics.getS3ObjectSizeSummary()).thenReturn(distributionSummary); |
| 76 | + when(s3ObjectPluginMetrics.getS3ObjectEventsSummary()).thenReturn(distributionSummary); |
| 77 | + when(s3ObjectPluginMetrics.getS3ObjectSizeProcessedSummary()).thenReturn(distributionSummary); |
| 78 | + when(s3ObjectPluginMetrics.getS3ObjectReadTimer()).thenReturn(timer); |
| 79 | + when(s3ObjectPluginMetrics.getS3ObjectNoRecordsFound()).thenReturn(counter); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + void data_and_metadata_selection_includes_both_data_and_metadata() throws Exception { |
| 84 | + testS3DataSelection(S3DataSelection.DATA_AND_METADATA, event -> { |
| 85 | + assertThat(event.toMap().containsKey("message"), equalTo(true)); // data content |
| 86 | + assertThat(event.get("s3/bucket", String.class), equalTo(bucket)); // metadata present |
| 87 | + }); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + void data_only_selection_includes_only_data() throws Exception { |
| 92 | + testS3DataSelection(S3DataSelection.DATA_ONLY, event -> { |
| 93 | + assertThat(event.toMap().containsKey("message"), equalTo(true)); // data content |
| 94 | + assertThat(event.get("s3/bucket", String.class) == null, equalTo(true)); // no metadata |
| 95 | + assertThat(event.get("s3/key", String.class) == null, equalTo(true)); // no metadata |
| 96 | + }); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + void metadata_only_selection_includes_only_metadata() throws Exception { |
| 101 | + testS3DataSelection(S3DataSelection.METADATA_ONLY, event -> { |
| 102 | + assertThat(event.toMap().containsKey("message"), equalTo(false)); // no data content |
| 103 | + assertThat(event.get("bucket", String.class), equalTo(bucket)); // metadata present as top-level key |
| 104 | + }); |
| 105 | + } |
| 106 | + |
| 107 | + private void testS3DataSelection(S3DataSelection dataSelection, Consumer<Event> eventAssertions) throws Exception { |
| 108 | + String key = writeTestObject(); |
| 109 | + S3ObjectReference s3ObjectReference = S3ObjectReference.bucketAndKey(bucket, key).build(); |
| 110 | + |
| 111 | + stubBufferWriter(eventAssertions); |
| 112 | + |
| 113 | + S3ObjectWorker objectWorker = createObjectUnderTest(); |
| 114 | + objectWorker.processS3Object(s3ObjectReference, dataSelection, null, null, null); |
| 115 | + |
| 116 | + verify(buffer).writeAll(anyCollection(), anyInt()); |
| 117 | + } |
| 118 | + |
| 119 | + private void stubBufferWriter(final Consumer<Event> eventAssertions) throws Exception { |
| 120 | + doAnswer(a -> { |
| 121 | + final Collection<Record<Event>> recordsCollection = a.getArgument(0); |
| 122 | + assertThat(recordsCollection.size(), greaterThanOrEqualTo(1)); |
| 123 | + for (Record<Event> eventRecord : recordsCollection) { |
| 124 | + assertThat(eventRecord, notNullValue()); |
| 125 | + assertThat(eventRecord.getData(), notNullValue()); |
| 126 | + eventAssertions.accept(eventRecord.getData()); |
| 127 | + } |
| 128 | + return null; |
| 129 | + }).when(buffer).writeAll(anyCollection(), anyInt()); |
| 130 | + } |
| 131 | + |
| 132 | + private S3ObjectWorker createObjectUnderTest() { |
| 133 | + final S3ObjectRequest request = new S3ObjectRequest.Builder(buffer, 100, |
| 134 | + Duration.ofMillis(TIMEOUT_IN_MILLIS), s3ObjectPluginMetrics) |
| 135 | + .bucketOwnerProvider(bucketOwnerProvider) |
| 136 | + .eventConsumer(eventMetadataModifier) |
| 137 | + .codec(new NewlineDelimitedRecordsGenerator().getCodec()) |
| 138 | + .s3Client(s3Client) |
| 139 | + .compressionOption(CompressionOption.NONE) |
| 140 | + .build(); |
| 141 | + return new S3ObjectWorker(request); |
| 142 | + } |
| 143 | + |
| 144 | + private String writeTestObject() throws IOException { |
| 145 | + final NewlineDelimitedRecordsGenerator generator = new NewlineDelimitedRecordsGenerator(); |
| 146 | + final String key = "s3 source/data-selection/" + UUID.randomUUID() + "_" + Instant.now().toString() + generator.getFileExtension(); |
| 147 | + s3ObjectGenerator.write(5, key, generator, false); |
| 148 | + return key; |
| 149 | + } |
| 150 | +} |
0 commit comments