Skip to content

Commit 08e6639

Browse files
S3RetryingInputStreamTests compiles but runs into a NPE
1 parent e2f8bc5 commit 08e6639

File tree

1 file changed

+37
-19
lines changed

1 file changed

+37
-19
lines changed
Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,20 @@
99

1010
package org.elasticsearch.repositories.s3;
1111

12-
import com.amazonaws.services.s3.AmazonS3;
13-
import com.amazonaws.services.s3.model.AmazonS3Exception;
14-
import com.amazonaws.services.s3.model.GetObjectRequest;
15-
import com.amazonaws.services.s3.model.S3Object;
16-
import com.amazonaws.services.s3.model.S3ObjectInputStream;
12+
import software.amazon.awssdk.core.ResponseInputStream;
13+
import software.amazon.awssdk.http.AbortableInputStream;
14+
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
1715

18-
import org.apache.http.client.methods.HttpGet;
1916
import org.elasticsearch.common.io.Streams;
2017
import org.elasticsearch.core.Nullable;
2118
import org.elasticsearch.repositories.blobstore.RequestedRangeNotSatisfiedException;
2219
import org.elasticsearch.rest.RestStatus;
2320
import org.elasticsearch.test.ESTestCase;
2421

22+
import software.amazon.awssdk.services.s3.S3Client;
23+
import software.amazon.awssdk.services.s3.model.GetObjectResponse;
24+
import software.amazon.awssdk.services.s3.model.S3Exception;
25+
2526
import java.io.ByteArrayInputStream;
2627
import java.io.IOException;
2728
import java.util.Arrays;
@@ -115,34 +116,51 @@ public void testReadAfterBlobLengthThrowsRequestedRangeNotSatisfiedException() t
115116
}
116117
}
117118

119+
/**
120+
* Creates a mock BlobStore that returns a mock S3Client, configured to supply a #getObject response. The blob store is then wrapped in
121+
* a {@link S3RetryingInputStream}.
122+
*
123+
* @param data The data to stream.
124+
* @param position The position at which to start reading from the stream.
125+
* @param length How much to read from the data stream starting at {@code position}
126+
* @return A {@link S3RetryingInputStream} that reads from the data stream.
127+
* @throws IOException
128+
*/
118129
private S3RetryingInputStream createInputStream(final byte[] data, @Nullable final Integer position, @Nullable final Integer length)
119130
throws IOException {
120-
final AmazonS3 client = mock(AmazonS3.class);
131+
final S3Client client = mock(S3Client.class);
121132
final AmazonS3Reference clientReference = mock(AmazonS3Reference.class);
122133
when(clientReference.client()).thenReturn(client);
123134
final S3BlobStore blobStore = mock(S3BlobStore.class);
124135
when(blobStore.clientReference()).thenReturn(clientReference);
125136

126137
if (position != null && length != null) {
127138
if (data.length <= position) {
128-
var amazonS3Exception = new AmazonS3Exception("test");
129-
amazonS3Exception.setStatusCode(RestStatus.REQUESTED_RANGE_NOT_SATISFIED.getStatus());
130-
when(client.getObject(any(GetObjectRequest.class))).thenThrow(amazonS3Exception);
139+
var s3Exception = S3Exception.builder().message("test");
140+
s3Exception.statusCode(RestStatus.REQUESTED_RANGE_NOT_SATISFIED.getStatus());
141+
when(client.getObject(any(GetObjectRequest.class))).thenThrow(s3Exception.build());
131142
return new S3RetryingInputStream(randomPurpose(), blobStore, "_blob", position, Math.addExact(position, length - 1));
132143
}
133144

134-
final S3Object s3Object = new S3Object();
135-
s3Object.getObjectMetadata().setContentLength(length);
136-
s3Object.setObjectContent(new S3ObjectInputStream(new ByteArrayInputStream(data, position, length), new HttpGet()));
137-
when(client.getObject(any(GetObjectRequest.class))).thenReturn(s3Object);
145+
// NOMERGE: I think blobStore.getMetricPublisher(operation, purpose) needs to be defined, to fix the NPE. I didn't get that far.
146+
// TODO NOMERGE: revisit AbortableInputStream, I just threw it on to see if that fixed the NPE.
147+
ResponseInputStream<GetObjectResponse> objectResponse =
148+
new ResponseInputStream<>(
149+
GetObjectResponse.builder().build(),//.contentLength(length.longValue()).build(),
150+
AbortableInputStream.create(new ByteArrayInputStream(data, position, length))
151+
);
152+
when(client.getObject(any(GetObjectRequest.class))).thenReturn(objectResponse);
138153
return new S3RetryingInputStream(randomPurpose(), blobStore, "_blob", position, Math.addExact(position, length - 1));
139154
}
140155

141-
final S3Object s3Object = new S3Object();
142-
s3Object.getObjectMetadata().setContentLength(data.length);
143-
s3Object.setObjectContent(new S3ObjectInputStream(new ByteArrayInputStream(data), new HttpGet()));
144-
when(client.getObject(any(GetObjectRequest.class))).thenReturn(s3Object);
156+
// NOMERGE: I think blobStore.getMetricPublisher(operation, purpose) needs to be defined, to fix the NPE. I didn't get that far.
157+
// TODO NOMERGE: revisit AbortableInputStream, I just threw it on to see if that fixed the NPE.
158+
ResponseInputStream<GetObjectResponse> objectResponse =
159+
new ResponseInputStream<>(
160+
GetObjectResponse.builder().build(),//.contentLength(Long.valueOf(data.length)).build(),
161+
AbortableInputStream.create(new ByteArrayInputStream(data))
162+
);
163+
when(client.getObject(any(GetObjectRequest.class))).thenReturn(objectResponse);
145164
return new S3RetryingInputStream(randomPurpose(), blobStore, "_blob");
146165
}
147166
}
148-
// TODO NOMERGE bring these tests back

0 commit comments

Comments
 (0)