|
19 | 19 |
|
20 | 20 | package org.elasticsearch.repositories; |
21 | 21 |
|
| 22 | +import org.elasticsearch.ElasticsearchParseException; |
22 | 23 | import org.elasticsearch.common.UUIDs; |
23 | 24 | import org.elasticsearch.common.bytes.BytesReference; |
24 | 25 | import org.elasticsearch.common.xcontent.ToXContent; |
| 26 | +import org.elasticsearch.common.xcontent.XContent; |
25 | 27 | import org.elasticsearch.common.xcontent.XContentBuilder; |
26 | 28 | import org.elasticsearch.common.xcontent.XContentParser; |
| 29 | +import org.elasticsearch.common.xcontent.XContentType; |
27 | 30 | import org.elasticsearch.common.xcontent.json.JsonXContent; |
28 | 31 | import org.elasticsearch.snapshots.SnapshotId; |
29 | 32 | import org.elasticsearch.snapshots.SnapshotState; |
|
40 | 43 | import java.util.Set; |
41 | 44 |
|
42 | 45 | import static org.elasticsearch.repositories.RepositoryData.EMPTY_REPO_GEN; |
| 46 | +import static org.hamcrest.Matchers.equalTo; |
43 | 47 | import static org.hamcrest.Matchers.greaterThan; |
44 | 48 |
|
45 | 49 | /** |
@@ -101,15 +105,18 @@ public void testAddSnapshots() { |
101 | 105 | public void testInitIndices() { |
102 | 106 | final int numSnapshots = randomIntBetween(1, 30); |
103 | 107 | final Map<String, SnapshotId> snapshotIds = new HashMap<>(numSnapshots); |
| 108 | + final Map<String, SnapshotState> snapshotStates = new HashMap<>(numSnapshots); |
104 | 109 | for (int i = 0; i < numSnapshots; i++) { |
105 | 110 | final SnapshotId snapshotId = new SnapshotId(randomAlphaOfLength(8), UUIDs.randomBase64UUID()); |
106 | 111 | snapshotIds.put(snapshotId.getUUID(), snapshotId); |
| 112 | + snapshotStates.put(snapshotId.getUUID(), randomFrom(SnapshotState.values())); |
107 | 113 | } |
108 | 114 | RepositoryData repositoryData = new RepositoryData(EMPTY_REPO_GEN, snapshotIds, |
109 | 115 | Collections.emptyMap(), Collections.emptyMap(), Collections.emptyList()); |
110 | 116 | // test that initializing indices works |
111 | 117 | Map<IndexId, Set<SnapshotId>> indices = randomIndices(snapshotIds); |
112 | | - RepositoryData newRepoData = repositoryData.initIndices(indices); |
| 118 | + RepositoryData newRepoData = new RepositoryData(repositoryData.getGenId(), snapshotIds, snapshotStates, indices, |
| 119 | + new ArrayList<>(repositoryData.getIncompatibleSnapshotIds())); |
113 | 120 | List<SnapshotId> expected = new ArrayList<>(repositoryData.getSnapshotIds()); |
114 | 121 | Collections.sort(expected); |
115 | 122 | List<SnapshotId> actual = new ArrayList<>(newRepoData.getSnapshotIds()); |
@@ -153,6 +160,46 @@ public void testGetSnapshotState() { |
153 | 160 | assertNull(repositoryData.getSnapshotState(new SnapshotId(randomAlphaOfLength(8), UUIDs.randomBase64UUID()))); |
154 | 161 | } |
155 | 162 |
|
| 163 | + public void testUnknownSnapshot() throws IOException { |
| 164 | + final XContent xContent = randomFrom(XContentType.values()).xContent(); |
| 165 | + final RepositoryData repositoryData = generateRandomRepoData(); |
| 166 | + |
| 167 | + XContentBuilder builder = XContentBuilder.builder(xContent); |
| 168 | + repositoryData.snapshotsToXContent(builder, ToXContent.EMPTY_PARAMS); |
| 169 | + RepositoryData parsedRepositoryData = RepositoryData.snapshotsFromXContent(createParser(builder), repositoryData.getGenId()); |
| 170 | + assertEquals(repositoryData, parsedRepositoryData); |
| 171 | + |
| 172 | + Map<String, SnapshotId> snapshotIds = new HashMap<>(); |
| 173 | + Map<String, SnapshotState> snapshotStates = new HashMap<>(); |
| 174 | + for (SnapshotId snapshotId : parsedRepositoryData.getSnapshotIds()) { |
| 175 | + snapshotIds.put(snapshotId.getUUID(), snapshotId); |
| 176 | + snapshotStates.put(snapshotId.getUUID(), parsedRepositoryData.getSnapshotState(snapshotId)); |
| 177 | + } |
| 178 | + |
| 179 | + final IndexId corruptedIndexId = randomFrom(parsedRepositoryData.getIndices().values()); |
| 180 | + |
| 181 | + Map<IndexId, Set<SnapshotId>> indexSnapshots = new HashMap<>(); |
| 182 | + for (Map.Entry<String, IndexId> snapshottedIndex : parsedRepositoryData.getIndices().entrySet()) { |
| 183 | + IndexId indexId = snapshottedIndex.getValue(); |
| 184 | + Set<SnapshotId> snapshotsIds = new LinkedHashSet<>(parsedRepositoryData.getSnapshots(indexId)); |
| 185 | + if (corruptedIndexId.equals(indexId)) { |
| 186 | + snapshotsIds.add(new SnapshotId("_uuid", "_does_not_exist")); |
| 187 | + } |
| 188 | + indexSnapshots.put(indexId, snapshotsIds); |
| 189 | + } |
| 190 | + assertNotNull(corruptedIndexId); |
| 191 | + |
| 192 | + RepositoryData corruptedRepositoryData = new RepositoryData(parsedRepositoryData.getGenId(), snapshotIds, snapshotStates, |
| 193 | + indexSnapshots, new ArrayList<>(parsedRepositoryData.getIncompatibleSnapshotIds())); |
| 194 | + |
| 195 | + final XContentBuilder corruptedBuilder = XContentBuilder.builder(xContent); |
| 196 | + corruptedRepositoryData.snapshotsToXContent(corruptedBuilder, ToXContent.EMPTY_PARAMS); |
| 197 | + |
| 198 | + ElasticsearchParseException e = expectThrows(ElasticsearchParseException.class, () -> |
| 199 | + RepositoryData.snapshotsFromXContent(createParser(corruptedBuilder), corruptedRepositoryData.getGenId())); |
| 200 | + assertThat(e.getMessage(), equalTo("Unknown snapshot uuid [_does_not_exist] for index " + corruptedIndexId)); |
| 201 | + } |
| 202 | + |
156 | 203 | public static RepositoryData generateRandomRepoData() { |
157 | 204 | final int numIndices = randomIntBetween(1, 30); |
158 | 205 | final List<IndexId> indices = new ArrayList<>(numIndices); |
|
0 commit comments