From d41d9128759f153f7f0ba33f227f84c3330573b5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Francisco=20Fern=C3=A1ndez=20Casta=C3=B1o?=
 <francisco.fernandez.castano@gmail.com>
Date: Wed, 11 Aug 2021 14:31:40 +0200
Subject: [PATCH] Fix IndexSnapshotsServiceIT and
 SnapshotsRecoveryPlannerServiceTests

Closes #76343
---
 .../repositories/IndexSnapshotsServiceIT.java             | 8 +++++---
 .../plan/SnapshotsRecoveryPlannerServiceTests.java        | 6 +++++-
 2 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/server/src/internalClusterTest/java/org/elasticsearch/repositories/IndexSnapshotsServiceIT.java b/server/src/internalClusterTest/java/org/elasticsearch/repositories/IndexSnapshotsServiceIT.java
index bfc73e629e6b9..d69d0b562abcd 100644
--- a/server/src/internalClusterTest/java/org/elasticsearch/repositories/IndexSnapshotsServiceIT.java
+++ b/server/src/internalClusterTest/java/org/elasticsearch/repositories/IndexSnapshotsServiceIT.java
@@ -207,10 +207,11 @@ public void testGetShardSnapshotFailureHandlingLetOtherRepositoriesRequestsMakeP
         createIndexWithContent(indexName);
 
         int snapshotIdx = 0;
-        createSnapshot(failingRepoName, "empty-snap-" + snapshotIdx++, Collections.singletonList(indexName));
+        createSnapshot(failingRepoName, String.format(Locale.ROOT, "snap-%03d", snapshotIdx++), Collections.singletonList(indexName));
         SnapshotInfo latestSnapshot = null;
         for (String workingRepoName : workingRepoNames) {
-            latestSnapshot = createSnapshot(workingRepoName, "empty-snap-" + snapshotIdx++, Collections.singletonList(indexName));
+            String snapshot = String.format(Locale.ROOT, "snap-%03d", snapshotIdx++);
+            latestSnapshot = createSnapshot(workingRepoName, snapshot, Collections.singletonList(indexName));
         }
 
         final MockRepository repository = getRepositoryOnMaster(failingRepoName);
@@ -264,7 +265,8 @@ public void testGetShardSnapshotInMultipleRepositoriesReturnsTheLatestSnapshot()
         int snapshotIdx = 0;
         SnapshotInfo expectedLatestSnapshot = null;
         for (String repository : repositories) {
-            expectedLatestSnapshot = createSnapshot(repository, "snap-" + snapshotIdx++, Collections.singletonList(indexName));
+            String snapshot = String.format(Locale.ROOT, "snap-%03d", snapshotIdx++);
+            expectedLatestSnapshot = createSnapshot(repository, snapshot, Collections.singletonList(indexName));
         }
 
         GetShardSnapshotResponse response = getLatestSnapshotForShardFuture(repositories, indexName, 0).actionGet();
diff --git a/server/src/test/java/org/elasticsearch/indices/recovery/plan/SnapshotsRecoveryPlannerServiceTests.java b/server/src/test/java/org/elasticsearch/indices/recovery/plan/SnapshotsRecoveryPlannerServiceTests.java
index b8156e4c2d0a0..6d00b3a931ef1 100644
--- a/server/src/test/java/org/elasticsearch/indices/recovery/plan/SnapshotsRecoveryPlannerServiceTests.java
+++ b/server/src/test/java/org/elasticsearch/indices/recovery/plan/SnapshotsRecoveryPlannerServiceTests.java
@@ -258,7 +258,11 @@ public void testPlannerTriesToUseMostFilesFromSnapshots() throws Exception {
                 new ShardSnapshotsService(null, null, null, null) {
                     @Override
                     public void fetchLatestSnapshotsForShard(ShardId shardId, ActionListener<Optional<ShardSnapshot>> listener) {
-                        listener.onResponse(Optional.of(availableSnapshots.get(availableSnapshots.size() - 1)));
+                        if (availableSnapshots.isEmpty()) {
+                            listener.onResponse(Optional.empty());
+                        } else {
+                            listener.onResponse(Optional.of(availableSnapshots.get(availableSnapshots.size() - 1)));
+                        }
                     }
                 },
                 true