From d0be96df7b3d48ce1432f5557ad1ede3397a51f9 Mon Sep 17 00:00:00 2001 From: Nik Everett Date: Wed, 14 Sep 2016 17:04:51 -0400 Subject: [PATCH] Clean up snapshots after each REST test The only repository we can be sure is safe to clean is `fs` so we clean any snapshots in those repositories after each test. Other repositories like url and azure tend to throw exceptions rather than let us fetch their contents during the REST test. So we clean what we can.... Closes #18159 --- .../test/snapshot.get/10_basic.yaml | 11 ++----- .../test/snapshot.status/10_basic.yaml | 11 ++----- .../test/rest/ESRestTestCase.java | 32 +++++++++++++++++-- 3 files changed, 33 insertions(+), 21 deletions(-) diff --git a/rest-api-spec/src/main/resources/rest-api-spec/test/snapshot.get/10_basic.yaml b/rest-api-spec/src/main/resources/rest-api-spec/test/snapshot.get/10_basic.yaml index bd609e3e3bf..24a7ac6adc6 100644 --- a/rest-api-spec/src/main/resources/rest-api-spec/test/snapshot.get/10_basic.yaml +++ b/rest-api-spec/src/main/resources/rest-api-spec/test/snapshot.get/10_basic.yaml @@ -9,13 +9,6 @@ setup: settings: location: "test_repo_get_1_loc" ---- -teardown: - - - do: - snapshot.delete_repository: - repository: test_repo_get_1 - --- "Get snapshot info": @@ -39,7 +32,7 @@ teardown: snapshot: test_snapshot - is_true: snapshots - + --- "Get missing snapshot info throws an exception": @@ -48,7 +41,7 @@ teardown: snapshot.get: repository: test_repo_get_1 snapshot: test_nonexistent_snapshot - + --- "Get missing snapshot info succeeds when ignoreUnavailable is true": diff --git a/rest-api-spec/src/main/resources/rest-api-spec/test/snapshot.status/10_basic.yaml b/rest-api-spec/src/main/resources/rest-api-spec/test/snapshot.status/10_basic.yaml index d4548553e25..838c1264974 100644 --- a/rest-api-spec/src/main/resources/rest-api-spec/test/snapshot.status/10_basic.yaml +++ b/rest-api-spec/src/main/resources/rest-api-spec/test/snapshot.status/10_basic.yaml @@ -9,13 +9,6 @@ setup: settings: location: "test_repo_status_1_loc" ---- -teardown: - - - do: - snapshot.delete_repository: - repository: test_repo_status_1 - --- "Get snapshot status": @@ -39,7 +32,7 @@ teardown: snapshot: test_snapshot - is_true: snapshots - + --- "Get missing snapshot status throws an exception": @@ -48,7 +41,7 @@ teardown: snapshot.status: repository: test_repo_status_1 snapshot: test_nonexistent_snapshot - + --- "Get missing snapshot status succeeds when ignoreUnavailable is true": diff --git a/test/framework/src/main/java/org/elasticsearch/test/rest/ESRestTestCase.java b/test/framework/src/main/java/org/elasticsearch/test/rest/ESRestTestCase.java index 573c301105a..fdce0248ed3 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/rest/ESRestTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/test/rest/ESRestTestCase.java @@ -55,6 +55,7 @@ import java.util.Set; import javax.net.ssl.SSLContext; +import static java.util.Collections.singletonMap; import static java.util.Collections.sort; import static java.util.Collections.unmodifiableList; @@ -151,9 +152,34 @@ public class ESRestTestCase extends ESTestCase { // wipe index templates adminClient().performRequest("DELETE", "_template/*"); - // wipe snapshots - // Technically this deletes all repositories and leave the snapshots in the repository. OK. - adminClient().performRequest("DELETE", "_snapshot/*"); + wipeSnapshots(); + } + + /** + * Wipe fs snapshots we created one by one and all repositories so that the next test can create the repositories fresh and they'll + * start empty. There isn't an API to delete all snapshots. There is an API to delete all snapshot repositories but that leaves all of + * the snapshots intact in the repository. + */ + private void wipeSnapshots() throws IOException { + for (Map.Entry repo : entityAsMap(adminClient.performRequest("GET", "_snapshot/_all")).entrySet()) { + String repoName = repo.getKey(); + Map repoSpec = (Map) repo.getValue(); + String repoType = (String) repoSpec.get("type"); + if (repoType.equals("fs")) { + // All other repo types we really don't have a chance of being able to iterate properly, sadly. + String url = "_snapshot/" + repoName + "/_all"; + Map params = singletonMap("ignore_unavailable", "true"); + List snapshots = (List) entityAsMap(adminClient.performRequest("GET", url, params)).get("snapshots"); + for (Object snapshot : snapshots) { + Map snapshotInfo = (Map) snapshot; + String name = (String) snapshotInfo.get("snapshot"); + logger.debug("wiping snapshot [{}/{}]", repoName, name); + adminClient().performRequest("DELETE", "_snapshot/" + repoName + "/" + name); + } + } + logger.debug("wiping snapshot repository [{}]", repoName); + adminClient().performRequest("DELETE", "_snapshot/" + repoName); + } } /**