Skip to content

Commit 9aea851

Browse files
committed
Improve SnapshotInfo serialization tests (#71677)
Today we have no tests that directly verify that `SnapshotInfo` correctly round-trips through its in-repository `XContent`-based representation. We do have tests that verify that it correctly round-trips through the wire format, but these tests always use an empty collection for `featureStates`. This commit strengthens the wire format round-trip tests to use nontrivial `featureStates`, and generalizes these tests to also verify that the `XContent` representation is faithful.
1 parent 519e235 commit 9aea851

File tree

11 files changed

+358
-172
lines changed

11 files changed

+358
-172
lines changed

server/src/internalClusterTest/java/org/elasticsearch/cluster/ClusterStateDiffIT.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
import org.elasticsearch.index.shard.ShardId;
4646
import org.elasticsearch.snapshots.Snapshot;
4747
import org.elasticsearch.snapshots.SnapshotId;
48-
import org.elasticsearch.snapshots.SnapshotInfoTests;
48+
import org.elasticsearch.snapshots.SnapshotInfoTestUtils;
4949
import org.elasticsearch.snapshots.SnapshotsInProgressSerializationTests;
5050
import org.elasticsearch.test.ESIntegTestCase;
5151

@@ -713,7 +713,7 @@ public ClusterState.Custom randomCreate(String name) {
713713
Math.abs(randomLong()), randomIntBetween(0, 1000),
714714
ImmutableOpenMap.of(),
715715
null,
716-
SnapshotInfoTests.randomUserMetadata(),
716+
SnapshotInfoTestUtils.randomUserMetadata(),
717717
randomVersion(random()))));
718718
case 1:
719719
return new RestoreInProgress.Builder().add(

server/src/test/java/org/elasticsearch/action/admin/cluster/snapshots/create/CreateSnapshotRequestTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
import java.util.List;
3333
import java.util.Map;
3434

35-
import static org.elasticsearch.snapshots.SnapshotInfoTests.randomUserMetadata;
35+
import static org.elasticsearch.snapshots.SnapshotInfoTestUtils.randomUserMetadata;
3636
import static org.hamcrest.Matchers.equalTo;
3737
import static org.hamcrest.Matchers.hasSize;
3838

server/src/test/java/org/elasticsearch/action/admin/cluster/snapshots/create/CreateSnapshotResponseTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import org.elasticsearch.snapshots.SnapshotFeatureInfoTests;
1515
import org.elasticsearch.snapshots.SnapshotId;
1616
import org.elasticsearch.snapshots.SnapshotInfo;
17-
import org.elasticsearch.snapshots.SnapshotInfoTests;
17+
import org.elasticsearch.snapshots.SnapshotInfoTestUtils;
1818
import org.elasticsearch.snapshots.SnapshotShardFailure;
1919
import org.elasticsearch.test.AbstractXContentTestCase;
2020

@@ -65,7 +65,7 @@ protected CreateSnapshotResponse createTestInstance() {
6565

6666
return new CreateSnapshotResponse(
6767
new SnapshotInfo(snapshotId, indices, dataStreams, featureStates, reason, endTime, totalShards, shardFailures,
68-
globalState, SnapshotInfoTests.randomUserMetadata(), startTime
68+
globalState, SnapshotInfoTestUtils.randomUserMetadata(), startTime
6969
));
7070
}
7171

server/src/test/java/org/elasticsearch/action/admin/cluster/snapshots/get/GetSnapshotsResponseTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import org.elasticsearch.snapshots.SnapshotFeatureInfo;
1616
import org.elasticsearch.snapshots.SnapshotId;
1717
import org.elasticsearch.snapshots.SnapshotInfo;
18-
import org.elasticsearch.snapshots.SnapshotInfoTests;
18+
import org.elasticsearch.snapshots.SnapshotInfoTestUtils;
1919
import org.elasticsearch.snapshots.SnapshotShardFailure;
2020
import org.elasticsearch.test.AbstractSerializingTestCase;
2121

@@ -47,7 +47,7 @@ protected GetSnapshotsResponse createTestInstance() {
4747
List<SnapshotFeatureInfo> featureInfos = randomList(0, () -> randomSnapshotFeatureInfo());
4848
snapshots.add(new SnapshotInfo(snapshotId, Arrays.asList("index1", "index2"), Collections.singletonList("ds"),
4949
featureInfos, reason, System.currentTimeMillis(), randomIntBetween(2, 3), shardFailures, randomBoolean(),
50-
SnapshotInfoTests.randomUserMetadata(), System.currentTimeMillis()
50+
SnapshotInfoTestUtils.randomUserMetadata(), System.currentTimeMillis()
5151
));
5252
}
5353
return new GetSnapshotsResponse(snapshots);

server/src/test/java/org/elasticsearch/cluster/metadata/MetadataDeleteIndexServiceTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import org.elasticsearch.snapshots.Snapshot;
2323
import org.elasticsearch.snapshots.SnapshotId;
2424
import org.elasticsearch.snapshots.SnapshotInProgressException;
25-
import org.elasticsearch.snapshots.SnapshotInfoTests;
25+
import org.elasticsearch.snapshots.SnapshotInfoTestUtils;
2626
import org.elasticsearch.test.ESTestCase;
2727
import org.elasticsearch.test.VersionUtils;
2828
import org.hamcrest.core.IsNull;
@@ -74,7 +74,7 @@ public void testDeleteSnapshotting() {
7474
org.elasticsearch.common.collect.List.of(new SnapshotsInProgress.Entry(snapshot, true, false,
7575
SnapshotsInProgress.State.INIT, singletonList(new IndexId(index, "doesn't matter")),
7676
Collections.emptyList(), Collections.emptyList(), System.currentTimeMillis(), (long) randomIntBetween(0, 1000),
77-
ImmutableOpenMap.of(), null, SnapshotInfoTests.randomUserMetadata(), VersionUtils.randomVersion(random()))));
77+
ImmutableOpenMap.of(), null, SnapshotInfoTestUtils.randomUserMetadata(), VersionUtils.randomVersion(random()))));
7878
ClusterState state = ClusterState.builder(clusterState(index))
7979
.putCustom(SnapshotsInProgress.TYPE, snaps)
8080
.build();

server/src/test/java/org/elasticsearch/cluster/metadata/MetadataIndexStateServiceTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
import org.elasticsearch.snapshots.Snapshot;
3737
import org.elasticsearch.snapshots.SnapshotId;
3838
import org.elasticsearch.snapshots.SnapshotInProgressException;
39-
import org.elasticsearch.snapshots.SnapshotInfoTests;
39+
import org.elasticsearch.snapshots.SnapshotInfoTestUtils;
4040
import org.elasticsearch.test.ESTestCase;
4141
import org.elasticsearch.test.VersionUtils;
4242

@@ -411,7 +411,7 @@ private static ClusterState addSnapshotIndex(final String index, final int numSh
411411
final SnapshotsInProgress.Entry entry =
412412
new SnapshotsInProgress.Entry(snapshot, randomBoolean(), false, SnapshotsInProgress.State.INIT,
413413
Collections.singletonList(new IndexId(index, index)), Collections.emptyList(), Collections.emptyList(),
414-
randomNonNegativeLong(), randomLong(), shardsBuilder.build(), null, SnapshotInfoTests.randomUserMetadata(),
414+
randomNonNegativeLong(), randomLong(), shardsBuilder.build(), null, SnapshotInfoTestUtils.randomUserMetadata(),
415415
VersionUtils.randomVersion(random())
416416
);
417417
return ClusterState.builder(newState).putCustom(SnapshotsInProgress.TYPE,
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0 and the Server Side Public License, v 1; you may not use this file except
5+
* in compliance with, at your election, the Elastic License 2.0 or the Server
6+
* Side Public License, v 1.
7+
*/
8+
9+
package org.elasticsearch.snapshots;
10+
11+
import org.elasticsearch.Version;
12+
import org.elasticsearch.action.ActionListener;
13+
import org.elasticsearch.action.support.PlainActionFuture;
14+
import org.elasticsearch.common.util.BigArrays;
15+
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
16+
import org.elasticsearch.repositories.blobstore.BlobStoreRepository;
17+
import org.elasticsearch.test.AbstractWireTestCase;
18+
19+
import java.io.IOException;
20+
21+
public class SnapshotInfoBlobSerializationTests extends AbstractWireTestCase<SnapshotInfo> {
22+
23+
@Override
24+
protected SnapshotInfo createTestInstance() {
25+
return SnapshotInfoTestUtils.createRandomSnapshotInfo();
26+
}
27+
28+
@Override
29+
protected SnapshotInfo mutateInstance(SnapshotInfo instance) throws IOException {
30+
return SnapshotInfoTestUtils.mutateSnapshotInfo(instance);
31+
}
32+
33+
@Override
34+
protected SnapshotInfo copyInstance(SnapshotInfo instance, Version version) throws IOException {
35+
final PlainActionFuture<SnapshotInfo> future = new PlainActionFuture<>();
36+
BlobStoreRepository.SNAPSHOT_FORMAT.serialize(instance, "test", randomBoolean(), BigArrays.NON_RECYCLING_INSTANCE,
37+
bytes -> ActionListener.completeWith(future,
38+
() -> BlobStoreRepository.SNAPSHOT_FORMAT.deserialize("test", NamedXContentRegistry.EMPTY, bytes)));
39+
return future.actionGet();
40+
}
41+
42+
}

0 commit comments

Comments
 (0)