Fix GCS Mock Behavior for Missing Bucket (#57283) (#57310)

* Fix GCS Mock Behavior for Missing Bucket

We were throwing a 500 instead of a 404 for a missing bucket.
This would make yaml tests needlessly wait for multiple seconds, retrying
the 500 response with backoff, in the test checking behavior for missing buckets.
This commit is contained in:
Armin Braun 2020-05-29 10:01:20 +02:00 committed by GitHub
parent 75868ea915
commit be6fa72432
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 5 deletions

View File

@ -38,6 +38,7 @@ import org.elasticsearch.common.SuppressForbidden;
import org.elasticsearch.common.blobstore.BlobContainer;
import org.elasticsearch.common.blobstore.BlobPath;
import org.elasticsearch.common.blobstore.BlobStore;
import org.elasticsearch.common.blobstore.BlobStoreException;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.io.Streams;
import org.elasticsearch.common.regex.Regex;
@ -50,6 +51,7 @@ import org.elasticsearch.env.Environment;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.repositories.RepositoriesService;
import org.elasticsearch.repositories.Repository;
import org.elasticsearch.repositories.RepositoryException;
import org.elasticsearch.repositories.blobstore.BlobStoreRepository;
import org.elasticsearch.repositories.blobstore.ESMockAPIBasedRepositoryIntegTestCase;
import org.junit.BeforeClass;
@ -71,6 +73,8 @@ import static org.elasticsearch.repositories.gcs.GoogleCloudStorageClientSetting
import static org.elasticsearch.repositories.gcs.GoogleCloudStorageClientSettings.TOKEN_URI_SETTING;
import static org.elasticsearch.repositories.gcs.GoogleCloudStorageRepository.BUCKET;
import static org.elasticsearch.repositories.gcs.GoogleCloudStorageRepository.CLIENT_NAME;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
@SuppressForbidden(reason = "this test uses a HttpServer to emulate a Google Cloud Storage endpoint")
public class GoogleCloudStorageBlobStoreRepositoryTests extends ESMockAPIBasedRepositoryIntegTestCase {
@ -210,6 +214,16 @@ public class GoogleCloudStorageBlobStoreRepositoryTests extends ESMockAPIBasedRe
}
}
public void testBucketDoesNotExist() {
RepositoryException ex = expectThrows(RepositoryException.class, () ->
client().admin().cluster().preparePutRepository("invalid")
.setType(repositoryType())
.setVerify(true)
.setSettings(Settings.builder().put(repositorySettings()).put("bucket", "missing")).get());
assertThat(ex.getCause(), instanceOf(BlobStoreException.class));
assertThat(ex.getCause().getMessage(), is("Bucket [missing] does not exist"));
}
public static class TestGoogleCloudStoragePlugin extends GoogleCloudStoragePlugin {
public TestGoogleCloudStoragePlugin(Settings settings) {
@ -249,7 +263,8 @@ public class GoogleCloudStorageBlobStoreRepositoryTests extends ESMockAPIBasedRe
metadata -> new GoogleCloudStorageRepository(metadata, registry, this.storageService, clusterService) {
@Override
protected GoogleCloudStorageBlobStore createBlobStore() {
return new GoogleCloudStorageBlobStore("bucket", "test", metadata.name(), storageService) {
return new GoogleCloudStorageBlobStore(
metadata.settings().get("bucket"), "test", metadata.name(), storageService) {
@Override
long getLargeBlobThresholdInBytes() {
return ByteSizeUnit.MB.toBytes(1);

View File

@ -167,13 +167,13 @@ public class GoogleCloudStorageHttpHandler implements HttpHandler {
final StringBuilder batch = new StringBuilder();
for (String line : Streams.readAllLines(requestBody.streamInput())) {
if (line.length() == 0 || line.startsWith("--") || line.toLowerCase(Locale.ROOT).startsWith("content")) {
batch.append(line).append('\n');
batch.append(line).append("\r\n");
} else if (line.startsWith("DELETE")) {
final String name = line.substring(line.indexOf(uri) + uri.length(), line.lastIndexOf(" HTTP"));
if (Strings.hasText(name)) {
blobs.remove(URLDecoder.decode(name, UTF_8.name()));
batch.append("HTTP/1.1 204 NO_CONTENT").append('\n');
batch.append('\n');
batch.append("HTTP/1.1 204 NO_CONTENT").append("\r\n");
batch.append("\r\n");
}
}
}
@ -243,7 +243,7 @@ public class GoogleCloudStorageHttpHandler implements HttpHandler {
exchange.sendResponseHeaders(RestStatus.OK.getStatus(), -1);
}
} else {
exchange.sendResponseHeaders(RestStatus.INTERNAL_SERVER_ERROR.getStatus(), -1);
exchange.sendResponseHeaders(RestStatus.NOT_FOUND.getStatus(), -1);
}
} finally {
int read = exchange.getRequestBody().read();