Skip to content

Commit ae8017a

Browse files
authored
Fix SnapshotLifecycleRestIT.testFullPolicySnapshot (elastic#51769)
* Fix SnapshotLifecycleRestIT.testFullPolicySnapshot This previously was missing some key information in the output of the failure. This captures that information and adds logging at each step so we can determine the cause *if* it fails again. Resolves elastic#50358
1 parent c3767b1 commit ae8017a

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

x-pack/plugin/ilm/qa/multi-node/src/test/java/org/elasticsearch/xpack/slm/SnapshotLifecycleRestIT.java

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,10 @@
4141
import java.util.HashMap;
4242
import java.util.List;
4343
import java.util.Map;
44+
import java.util.Optional;
4445
import java.util.concurrent.TimeUnit;
4546
import java.util.function.Function;
47+
import java.util.function.Predicate;
4648
import java.util.stream.Collectors;
4749

4850
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
@@ -104,6 +106,11 @@ public void testFullPolicySnapshot() throws Exception {
104106

105107
createSnapshotPolicy(policyName, "snap", "*/1 * * * * ?", repoId, indexName, true);
106108

109+
// A test for whether the repository's snapshots have any snapshots starting with "snap-"
110+
Predicate<Map<String, Object>> repoHasSnapshot = snapMap -> Optional.ofNullable((String) snapMap.get("snapshot"))
111+
.map(snapName -> snapName.startsWith("snap-"))
112+
.orElse(false);
113+
107114
// Check that the snapshot was actually taken
108115
assertBusy(() -> {
109116
Response response = client().performRequest(new Request("GET", "/_snapshot/" + repoId + "/_all"));
@@ -112,17 +119,16 @@ public void testFullPolicySnapshot() throws Exception {
112119
snapshotResponseMap = XContentHelper.convertToMap(XContentType.JSON.xContent(), is, true);
113120
}
114121
assertThat(snapshotResponseMap.size(), greaterThan(0));
115-
final Map<String, Object> snapResponse;
116-
try {
117-
Map<String, Object> responseMap = ((List<Map<String, Object>>) snapshotResponseMap.get("responses")).get(0);
118-
List<Map<String, Object>> snapshots = (List<Map<String, Object>>) responseMap.get("snapshots");
119-
assertTrue(snapshots.stream().anyMatch(s -> s.containsKey("snapshot") && s.get("snapshot").toString().startsWith("snap-")));
120-
snapResponse = snapshots.get(0);
121-
} catch (Exception e) {
122-
throw new AssertionError("failed to find snapshot response in " + snapshotResponseMap, e);
123-
}
124-
assertThat(snapResponse.get("indices"), equalTo(Collections.singletonList(indexName)));
125-
Map<String, Object> metadata = (Map<String, Object>) snapResponse.get("metadata");
122+
List<Map<String, Object>> snapResponse = ((List<Map<String, Object>>) snapshotResponseMap.get("responses")).stream()
123+
.peek(m -> logger.info("--> responses: {}", m))
124+
.map(m -> (List<Map<String, Object>>) m.get("snapshots"))
125+
.peek(allReposSnapshots -> logger.info("--> all repository's snapshots: {}", allReposSnapshots))
126+
.filter(allReposSnapshots -> allReposSnapshots.stream().anyMatch(repoHasSnapshot))
127+
.peek(allRepos -> logger.info("--> snapshots with 'snap-' snapshot: {}", allRepos))
128+
.findFirst()
129+
.orElseThrow(() -> new AssertionError("failed to find snapshot response in " + snapshotResponseMap));
130+
assertThat(snapResponse.get(0).get("indices"), equalTo(Collections.singletonList(indexName)));
131+
Map<String, Object> metadata = (Map<String, Object>) snapResponse.get(0).get("metadata");
126132
assertNotNull(metadata);
127133
assertThat(metadata.get("policy"), equalTo(policyName));
128134
});

0 commit comments

Comments
 (0)