Skip to content

Commit 918d80b

Browse files
committed
Add copyBlob to ESBlobStoreRepositoryIntegTestCase
1 parent ec271f5 commit 918d80b

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

test/fixtures/s3-fixture/src/main/java/fixture/s3/S3HttpHandler.java

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -206,13 +206,25 @@ public void handle(final HttpExchange exchange) throws IOException {
206206
RestUtils.decodeQueryString(request, request.indexOf('?') + 1, params);
207207
final var upload = uploads.remove(params.get("uploadId"));
208208
exchange.sendResponseHeaders((upload == null ? RestStatus.NOT_FOUND : RestStatus.NO_CONTENT).getStatus(), -1);
209-
210209
} else if (Regex.simpleMatch("PUT /" + path + "/*", request)) {
211-
final Tuple<String, BytesReference> blob = parseRequestBody(exchange);
212-
blobs.put(requestComponents.uri(), blob.v2());
213-
exchange.getResponseHeaders().add("ETag", blob.v1());
214-
exchange.sendResponseHeaders(RestStatus.OK.getStatus(), -1);
215-
210+
// copy request
211+
final var sourceBlobName = exchange.getRequestHeaders().get("X-amz-copy-source");
212+
if (sourceBlobName != null) {
213+
var sourceBlob = blobs.get(sourceBlobName.getFirst());
214+
blobs.put(requestComponents.uri(), sourceBlob);
215+
216+
byte[] response = ("""
217+
<?xml version="1.0" encoding="UTF-8"?>
218+
<CopyObjectResult></CopyObjectResult>""").getBytes(StandardCharsets.UTF_8);
219+
exchange.getResponseHeaders().add("Content-Type", "application/xml");
220+
exchange.sendResponseHeaders(RestStatus.OK.getStatus(), response.length);
221+
exchange.getResponseBody().write(response);
222+
} else {
223+
final Tuple<String, BytesReference> blob = parseRequestBody(exchange);
224+
blobs.put(requestComponents.uri(), blob.v2());
225+
exchange.getResponseHeaders().add("ETag", blob.v1());
226+
exchange.sendResponseHeaders(RestStatus.OK.getStatus(), -1);
227+
}
216228
} else if (isListObjectsRequest(request)) {
217229
final Map<String, String> params = new HashMap<>();
218230
RestUtils.decodeQueryString(request, request.indexOf('?') + 1, params);

test/framework/src/main/java/org/elasticsearch/repositories/blobstore/ESBlobStoreRepositoryIntegTestCase.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,17 +137,27 @@ public void testReadNonExistingPath() throws IOException {
137137
}
138138
}
139139

140-
public void testWriteRead() throws IOException {
140+
public void testWriteMaybeCopyRead() throws IOException {
141141
try (BlobStore store = newBlobStore()) {
142142
final BlobContainer container = store.blobContainer(BlobPath.EMPTY);
143143
byte[] data = randomBytes(randomIntBetween(10, scaledRandomIntBetween(1024, 1 << 16)));
144-
writeBlob(container, "foobar", new BytesArray(data), randomBoolean());
144+
final String blobName = randomAlphaOfLengthBetween(8, 12);
145+
String readBlobName = blobName;
146+
writeBlob(container, blobName, new BytesArray(data), randomBoolean());
145147
if (randomBoolean()) {
146148
// override file, to check if we get latest contents
147149
data = randomBytes(randomIntBetween(10, scaledRandomIntBetween(1024, 1 << 16)));
148-
writeBlob(container, "foobar", new BytesArray(data), false);
150+
writeBlob(container, blobName, new BytesArray(data), false);
149151
}
150-
try (InputStream stream = container.readBlob(randomPurpose(), "foobar")) {
152+
if (randomBoolean()) {
153+
// server-side copy if supported
154+
try {
155+
final var destinationBlobName = blobName + "_copy";
156+
container.copyBlob(randomPurpose(), blobName, container, destinationBlobName);
157+
readBlobName = destinationBlobName;
158+
} catch (UnsupportedOperationException ignored) {}
159+
}
160+
try (InputStream stream = container.readBlob(randomPurpose(), readBlobName)) {
151161
BytesRefBuilder target = new BytesRefBuilder();
152162
while (target.length() < data.length) {
153163
byte[] buffer = new byte[scaledRandomIntBetween(1, data.length - target.length())];

0 commit comments

Comments
 (0)