Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,9 @@ public void handle(final HttpExchange exchange) throws IOException {
exchange.sendResponseHeaders(RestStatus.OK.getStatus(), -1);

} else if (request.isListObjectsRequest()) {
if (request.queryParameters().containsKey("list-type")) {
throw new AssertionError("Test must be adapted for GET Bucket (List Objects) Version 2");
final var listType = request.queryParameters().getOrDefault("list-type", List.of("1")).get(0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you clarify with a comment that V2 requests newly have this extra version (?) field, so we're adding the check in case the version unexpectedly changes in future? That's my understanding, anyway, from looking around on the internet.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can replace it something like

final var listType = request.getOptionalQueryParam("list-type");
assert listType.isEmpty() || "2".equals(listType.get());

Seems more explicit with the intention and also ensures the parameter has a single value?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All fair points. Previously I was doing something with the listType value so it was useful to distinguish "1" from "2", but we don't need this right now so we can just drop this check entirely -> 5974795

Copy link
Contributor

@DiannaHohensee DiannaHohensee Apr 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, injecting "1" is a little misleading: it made me look around for what "1" could represent and it appears to be meaningless (since V1 doesn't have the field)?

Might it be better to add a not-null check to the middle of the if-statement (to avoid the mystery default number confusion)?

if (request.queryParameters().containsKey("list-type") && listType.equals("2") == false) {
throw new AssertionError("Unsupported list-type, must be absent or `2` but got " + listType);
}

final StringBuilder list = new StringBuilder();
Expand All @@ -223,6 +224,9 @@ public void handle(final HttpExchange exchange) throws IOException {
if (delimiter != null) {
list.append("<Delimiter>").append(delimiter).append("</Delimiter>");
}
// Would be good to test pagination here (the only real difference between ListObjects and ListObjectsV2) but for now
// we return all the results at once.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this explanation be partly phrased as a TODO?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hold no hope that we'll actually do it so I'm going to just leave it as a comment.

list.append("<IsTruncated>false</IsTruncated>");
for (Map.Entry<String, BytesReference> blob : blobs.entrySet()) {
if (prefix != null && blob.getKey().startsWith("/" + bucket + "/" + prefix) == false) {
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpPrincipal;

import org.elasticsearch.common.Randomness;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.bytes.BytesReference;
Expand All @@ -28,6 +29,7 @@
import java.net.InetSocketAddress;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

Expand All @@ -45,63 +47,80 @@ public void testRejectsBadUri() {
);
}

private static void assertListObjectsResponse(
S3HttpHandler handler,
@Nullable String prefix,
@Nullable String delimiter,
String expectedResponse
) {
final var queryParts = new ArrayList<String>(3);
if (prefix != null) {
queryParts.add("prefix=" + prefix);
}
if (delimiter != null) {
queryParts.add("delimiter=" + delimiter);
}
if (randomBoolean()) {
queryParts.add("list-type=2");
}
Randomness.shuffle(queryParts);

final var requestUri = "/bucket" + randomFrom("", "/") + (queryParts.isEmpty() ? "" : "?" + String.join("&", queryParts));
assertEquals("GET " + requestUri, new TestHttpResponse(RestStatus.OK, expectedResponse), handleRequest(handler, "GET", requestUri));
}

public void testSimpleObjectOperations() {
final var handler = new S3HttpHandler("bucket", "path");

assertEquals(RestStatus.NOT_FOUND, handleRequest(handler, "GET", "/bucket/path/blob").status());

assertEquals(
new TestHttpResponse(RestStatus.OK, """
<?xml version="1.0" encoding="UTF-8"?><ListBucketResult><Prefix></Prefix></ListBucketResult>"""),
handleRequest(handler, "GET", "/bucket/?prefix=")
);
assertListObjectsResponse(handler, "", null, """
<?xml version="1.0" encoding="UTF-8"?><ListBucketResult><Prefix></Prefix><IsTruncated>false</IsTruncated>\
</ListBucketResult>""");

final var body = randomAlphaOfLength(50);
assertEquals(RestStatus.OK, handleRequest(handler, "PUT", "/bucket/path/blob", body).status());
assertEquals(new TestHttpResponse(RestStatus.OK, body), handleRequest(handler, "GET", "/bucket/path/blob"));

assertEquals(new TestHttpResponse(RestStatus.OK, """
<?xml version="1.0" encoding="UTF-8"?><ListBucketResult><Prefix></Prefix>\
assertListObjectsResponse(handler, "", null, """
<?xml version="1.0" encoding="UTF-8"?><ListBucketResult><Prefix></Prefix><IsTruncated>false</IsTruncated>\
<Contents><Key>path/blob</Key><Size>50</Size></Contents>\
</ListBucketResult>"""), handleRequest(handler, "GET", "/bucket/?prefix="));
</ListBucketResult>""");

assertEquals(new TestHttpResponse(RestStatus.OK, """
<?xml version="1.0" encoding="UTF-8"?><ListBucketResult><Prefix>path/</Prefix>\
assertListObjectsResponse(handler, "path/", null, """
<?xml version="1.0" encoding="UTF-8"?><ListBucketResult><Prefix>path/</Prefix><IsTruncated>false</IsTruncated>\
<Contents><Key>path/blob</Key><Size>50</Size></Contents>\
</ListBucketResult>"""), handleRequest(handler, "GET", "/bucket/?prefix=path/"));
</ListBucketResult>""");

assertEquals(
new TestHttpResponse(RestStatus.OK, """
<?xml version="1.0" encoding="UTF-8"?><ListBucketResult><Prefix>path/other</Prefix></ListBucketResult>"""),
handleRequest(handler, "GET", "/bucket/?prefix=path/other")
);
assertListObjectsResponse(handler, "path/other", null, """
<?xml version="1.0" encoding="UTF-8"?><ListBucketResult><Prefix>path/other</Prefix><IsTruncated>false</IsTruncated>\
</ListBucketResult>""");

assertEquals(RestStatus.OK, handleRequest(handler, "PUT", "/bucket/path/subpath1/blob", randomAlphaOfLength(50)).status());
assertEquals(RestStatus.OK, handleRequest(handler, "PUT", "/bucket/path/subpath2/blob", randomAlphaOfLength(50)).status());
assertEquals(new TestHttpResponse(RestStatus.OK, """
<?xml version="1.0" encoding="UTF-8"?><ListBucketResult><Prefix>path/</Prefix>\
<Delimiter>/</Delimiter><Contents><Key>path/blob</Key><Size>50</Size></Contents>\
assertListObjectsResponse(handler, "path/", "/", """
<?xml version="1.0" encoding="UTF-8"?><ListBucketResult>\
<Prefix>path/</Prefix><Delimiter>/</Delimiter><IsTruncated>false</IsTruncated>\
<Contents><Key>path/blob</Key><Size>50</Size></Contents>\
<CommonPrefixes><Prefix>path/subpath1/</Prefix></CommonPrefixes>\
<CommonPrefixes><Prefix>path/subpath2/</Prefix></CommonPrefixes>\
</ListBucketResult>"""), handleRequest(handler, "GET", "/bucket/?delimiter=/&prefix=path/"));
</ListBucketResult>""");

assertEquals(RestStatus.OK, handleRequest(handler, "DELETE", "/bucket/path/blob").status());
assertEquals(RestStatus.NO_CONTENT, handleRequest(handler, "DELETE", "/bucket/path/blob").status());

assertEquals(new TestHttpResponse(RestStatus.OK, """
<?xml version="1.0" encoding="UTF-8"?><ListBucketResult><Prefix></Prefix>\
assertListObjectsResponse(handler, "", null, """
<?xml version="1.0" encoding="UTF-8"?><ListBucketResult><Prefix></Prefix><IsTruncated>false</IsTruncated>\
<Contents><Key>path/subpath1/blob</Key><Size>50</Size></Contents>\
<Contents><Key>path/subpath2/blob</Key><Size>50</Size></Contents>\
</ListBucketResult>"""), handleRequest(handler, "GET", "/bucket/?prefix="));
</ListBucketResult>""");

assertEquals(RestStatus.OK, handleRequest(handler, "DELETE", "/bucket/path/subpath1/blob").status());
assertEquals(RestStatus.OK, handleRequest(handler, "DELETE", "/bucket/path/subpath2/blob").status());

assertEquals(
new TestHttpResponse(RestStatus.OK, """
<?xml version="1.0" encoding="UTF-8"?><ListBucketResult><Prefix></Prefix></ListBucketResult>"""),
handleRequest(handler, "GET", "/bucket/?prefix=")
);
assertListObjectsResponse(handler, "", null, """
<?xml version="1.0" encoding="UTF-8"?><ListBucketResult><Prefix></Prefix><IsTruncated>false</IsTruncated>\
</ListBucketResult>""");
}

public void testGetWithBytesRange() {
Expand Down Expand Up @@ -216,10 +235,10 @@ public void testSingleMultipartUpload() {
</CompleteMultipartUpload>""", part1Etag, part2Etag))
);

assertEquals(new TestHttpResponse(RestStatus.OK, """
<?xml version="1.0" encoding="UTF-8"?><ListBucketResult><Prefix></Prefix>\
assertListObjectsResponse(handler, "", null, """
<?xml version="1.0" encoding="UTF-8"?><ListBucketResult><Prefix></Prefix><IsTruncated>false</IsTruncated>\
<Contents><Key>path/blob</Key><Size>100</Size></Contents>\
</ListBucketResult>"""), handleRequest(handler, "GET", "/bucket/?prefix="));
</ListBucketResult>""");

assertEquals(new TestHttpResponse(RestStatus.OK, part1 + part2), handleRequest(handler, "GET", "/bucket/path/blob"));

Expand Down