Skip to content

Commit 77ab672

Browse files
committed
Wrong exception thrown when snapshot doesn't exist
When a Snapshot does not exist, we should raise a `SnapshotMissingException`. Add also tests for GET/DELETE on non existing repo Closes #86.
1 parent 392ed73 commit 77ab672

File tree

2 files changed

+62
-3
lines changed

2 files changed

+62
-3
lines changed

src/main/java/org/elasticsearch/cloud/aws/blobstore/AbstractS3BlobContainer.java

+8
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.elasticsearch.common.blobstore.support.PlainBlobMetaData;
3333
import org.elasticsearch.common.collect.ImmutableMap;
3434

35+
import java.io.FileNotFoundException;
3536
import java.io.IOException;
3637
import java.io.InputStream;
3738

@@ -81,6 +82,13 @@ public void run() {
8182
try {
8283
S3Object object = blobStore.client().getObject(blobStore.bucket(), buildKey(blobName));
8384
is = object.getObjectContent();
85+
} catch (AmazonS3Exception e) {
86+
if (e.getStatusCode() == 404) {
87+
listener.onFailure(new FileNotFoundException(e.getMessage()));
88+
} else {
89+
listener.onFailure(e);
90+
}
91+
return;
8492
} catch (Throwable e) {
8593
listener.onFailure(e);
8694
return;

src/test/java/org/elasticsearch/repositories/s3/S3SnapshotRestoreTest.java

+54-3
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@
2323
import com.amazonaws.services.s3.model.DeleteObjectsRequest;
2424
import com.amazonaws.services.s3.model.ObjectListing;
2525
import com.amazonaws.services.s3.model.S3ObjectSummary;
26-
2726
import org.elasticsearch.action.admin.cluster.repositories.put.PutRepositoryResponse;
2827
import org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse;
2928
import org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse;
3029
import org.elasticsearch.client.Client;
30+
import org.elasticsearch.client.ClusterAdminClient;
3131
import org.elasticsearch.cloud.aws.AbstractAwsTest;
3232
import org.elasticsearch.cloud.aws.AbstractAwsTest.AwsTest;
3333
import org.elasticsearch.cloud.aws.AwsS3Service;
@@ -37,6 +37,7 @@
3737
import org.elasticsearch.common.util.concurrent.UncategorizedExecutionException;
3838
import org.elasticsearch.plugins.PluginsService;
3939
import org.elasticsearch.repositories.RepositoryMissingException;
40+
import org.elasticsearch.snapshots.SnapshotMissingException;
4041
import org.elasticsearch.snapshots.SnapshotState;
4142
import org.elasticsearch.test.ElasticsearchIntegrationTest.ClusterScope;
4243
import org.elasticsearch.test.ElasticsearchIntegrationTest.Scope;
@@ -45,8 +46,8 @@
4546
import org.junit.Before;
4647
import org.junit.Test;
4748

48-
import java.util.List;
4949
import java.util.ArrayList;
50+
import java.util.List;
5051

5152
import static org.hamcrest.Matchers.*;
5253

@@ -317,7 +318,57 @@ public void testRepositoryInRemoteRegion() {
317318
assertRepositoryIsOperational(client, "test-repo");
318319
}
319320

320-
private void assertRepositoryIsOperational(Client client, String repository) {
321+
/**
322+
* Test case for issue #86: https://github.com/elasticsearch/elasticsearch-cloud-aws/issues/86
323+
*/
324+
@Test
325+
public void testNonExistingRepo_86() {
326+
Client client = client();
327+
logger.info("--> creating s3 repository with bucket[{}] and path [{}]", internalCluster().getInstance(Settings.class).get("repositories.s3.bucket"), basePath);
328+
PutRepositoryResponse putRepositoryResponse = client.admin().cluster().preparePutRepository("test-repo")
329+
.setType("s3").setSettings(ImmutableSettings.settingsBuilder()
330+
.put("base_path", basePath)
331+
).get();
332+
assertThat(putRepositoryResponse.isAcknowledged(), equalTo(true));
333+
334+
logger.info("--> restore non existing snapshot");
335+
try {
336+
client.admin().cluster().prepareRestoreSnapshot("test-repo", "no-existing-snapshot").setWaitForCompletion(true).execute().actionGet();
337+
fail("Shouldn't be here");
338+
} catch (SnapshotMissingException ex) {
339+
// Expected
340+
}
341+
}
342+
343+
/**
344+
* For issue #86: https://github.com/elasticsearch/elasticsearch-cloud-aws/issues/86
345+
*/
346+
@Test
347+
public void testGetDeleteNonExistingSnapshot_86() {
348+
ClusterAdminClient client = client().admin().cluster();
349+
logger.info("--> creating azure repository without any path");
350+
PutRepositoryResponse putRepositoryResponse = client.preparePutRepository("test-repo").setType("azure")
351+
.setType("s3").setSettings(ImmutableSettings.settingsBuilder()
352+
.put("base_path", basePath)
353+
).get();
354+
assertThat(putRepositoryResponse.isAcknowledged(), equalTo(true));
355+
356+
try {
357+
client.prepareGetSnapshots("test-repo").addSnapshots("no-existing-snapshot").get();
358+
fail("Shouldn't be here");
359+
} catch (SnapshotMissingException ex) {
360+
// Expected
361+
}
362+
363+
try {
364+
client.prepareDeleteSnapshot("test-repo", "no-existing-snapshot").get();
365+
fail("Shouldn't be here");
366+
} catch (SnapshotMissingException ex) {
367+
// Expected
368+
}
369+
}
370+
371+
private void assertRepositoryIsOperational(Client client, String repository) {
321372
createIndex("test-idx-1");
322373
ensureGreen();
323374

0 commit comments

Comments
 (0)