Skip to content

Commit 9074c08

Browse files
authored
Add tests for SnapshotLifecyclePolicyItem (#40912)
Adds serialization tests for SnapshotLifecyclePolicyItem.
1 parent 68b4387 commit 9074c08

File tree

5 files changed

+224
-110
lines changed

5 files changed

+224
-110
lines changed
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
package org.elasticsearch.xpack.core.snapshotlifecycle;
8+
9+
import org.elasticsearch.common.Nullable;
10+
import org.elasticsearch.common.Strings;
11+
import org.elasticsearch.common.io.stream.StreamInput;
12+
import org.elasticsearch.common.io.stream.StreamOutput;
13+
import org.elasticsearch.common.io.stream.Writeable;
14+
import org.elasticsearch.common.xcontent.ToXContentFragment;
15+
import org.elasticsearch.common.xcontent.XContentBuilder;
16+
17+
import java.io.IOException;
18+
import java.util.Objects;
19+
20+
/**
21+
* The {@code SnapshotLifecyclePolicyItem} class is a special wrapper almost exactly like the
22+
* {@link SnapshotLifecyclePolicyMetadata}, however, it elides the headers to ensure that they
23+
* are not leaked to the user since they may contain sensitive information.
24+
*/
25+
public class SnapshotLifecyclePolicyItem implements ToXContentFragment, Writeable {
26+
27+
private final SnapshotLifecyclePolicy policy;
28+
private final long version;
29+
private final long modifiedDate;
30+
31+
@Nullable
32+
private final SnapshotInvocationRecord lastSuccess;
33+
34+
@Nullable
35+
private final SnapshotInvocationRecord lastFailure;
36+
public SnapshotLifecyclePolicyItem(SnapshotLifecyclePolicyMetadata policyMetadata) {
37+
this.policy = policyMetadata.getPolicy();
38+
this.version = policyMetadata.getVersion();
39+
this.modifiedDate = policyMetadata.getModifiedDate();
40+
this.lastSuccess = policyMetadata.getLastSuccess();
41+
this.lastFailure = policyMetadata.getLastFailure();
42+
}
43+
44+
public SnapshotLifecyclePolicyItem(StreamInput in) throws IOException {
45+
this.policy = new SnapshotLifecyclePolicy(in);
46+
this.version = in.readVLong();
47+
this.modifiedDate = in.readVLong();
48+
this.lastSuccess = in.readOptionalWriteable(SnapshotInvocationRecord::new);
49+
this.lastFailure = in.readOptionalWriteable(SnapshotInvocationRecord::new);
50+
}
51+
52+
// For testing
53+
54+
SnapshotLifecyclePolicyItem(SnapshotLifecyclePolicy policy, long version, long modifiedDate,
55+
SnapshotInvocationRecord lastSuccess, SnapshotInvocationRecord lastFailure) {
56+
this.policy = policy;
57+
this.version = version;
58+
this.modifiedDate = modifiedDate;
59+
this.lastSuccess = lastSuccess;
60+
this.lastFailure = lastFailure;
61+
}
62+
public SnapshotLifecyclePolicy getPolicy() {
63+
return policy;
64+
}
65+
66+
public long getVersion() {
67+
return version;
68+
}
69+
70+
public long getModifiedDate() {
71+
return modifiedDate;
72+
}
73+
74+
public SnapshotInvocationRecord getLastSuccess() {
75+
return lastSuccess;
76+
}
77+
78+
public SnapshotInvocationRecord getLastFailure() {
79+
return lastFailure;
80+
}
81+
82+
@Override
83+
public void writeTo(StreamOutput out) throws IOException {
84+
policy.writeTo(out);
85+
out.writeVLong(version);
86+
out.writeVLong(modifiedDate);
87+
out.writeOptionalWriteable(lastSuccess);
88+
out.writeOptionalWriteable(lastFailure);
89+
}
90+
91+
@Override
92+
public int hashCode() {
93+
return Objects.hash(policy, version, modifiedDate, lastSuccess, lastFailure);
94+
}
95+
96+
@Override
97+
public boolean equals(Object obj) {
98+
if (obj == null) {
99+
return false;
100+
}
101+
if (obj.getClass() != getClass()) {
102+
return false;
103+
}
104+
SnapshotLifecyclePolicyItem other = (SnapshotLifecyclePolicyItem) obj;
105+
return policy.equals(other.policy) &&
106+
version == other.version &&
107+
modifiedDate == other.modifiedDate &&
108+
Objects.equals(lastSuccess, other.lastSuccess) &&
109+
Objects.equals(lastFailure, other.lastFailure);
110+
}
111+
112+
@Override
113+
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
114+
builder.startObject(policy.getId());
115+
builder.field("version", version);
116+
builder.field("modified_date", modifiedDate);
117+
builder.field("policy", policy);
118+
if (lastSuccess != null) {
119+
builder.field("last_success", lastSuccess);
120+
}
121+
if (lastFailure != null) {
122+
builder.field("last_failure", lastFailure);
123+
}
124+
builder.endObject();
125+
return builder;
126+
}
127+
128+
@Override
129+
public String toString() {
130+
return Strings.toString(this);
131+
}
132+
}

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/snapshotlifecycle/action/GetSnapshotLifecycleAction.java

Lines changed: 1 addition & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,13 @@
1010
import org.elasticsearch.action.ActionRequestValidationException;
1111
import org.elasticsearch.action.ActionResponse;
1212
import org.elasticsearch.action.support.master.AcknowledgedRequest;
13-
import org.elasticsearch.common.Nullable;
1413
import org.elasticsearch.common.Strings;
1514
import org.elasticsearch.common.io.stream.StreamInput;
1615
import org.elasticsearch.common.io.stream.StreamOutput;
1716
import org.elasticsearch.common.io.stream.Writeable;
18-
import org.elasticsearch.common.xcontent.ToXContentFragment;
1917
import org.elasticsearch.common.xcontent.ToXContentObject;
2018
import org.elasticsearch.common.xcontent.XContentBuilder;
21-
import org.elasticsearch.xpack.core.snapshotlifecycle.SnapshotInvocationRecord;
22-
import org.elasticsearch.xpack.core.snapshotlifecycle.SnapshotLifecyclePolicy;
23-
import org.elasticsearch.xpack.core.snapshotlifecycle.SnapshotLifecyclePolicyMetadata;
19+
import org.elasticsearch.xpack.core.snapshotlifecycle.SnapshotLifecyclePolicyItem;
2420

2521
import java.io.IOException;
2622
import java.util.Arrays;
@@ -148,91 +144,4 @@ public boolean equals(Object obj) {
148144
}
149145
}
150146

151-
/**
152-
* The {@code SnapshotLifecyclePolicyItem} class is a special wrapper almost exactly like the
153-
* {@link SnapshotLifecyclePolicyMetadata}, however, it elides the headers to ensure that they
154-
* are not leaked to the user since they may contain sensitive information.
155-
*/
156-
public static class SnapshotLifecyclePolicyItem implements ToXContentFragment, Writeable {
157-
158-
private final SnapshotLifecyclePolicy policy;
159-
private final long version;
160-
private final long modifiedDate;
161-
@Nullable
162-
private final SnapshotInvocationRecord lastSuccess;
163-
@Nullable
164-
private final SnapshotInvocationRecord lastFailure;
165-
166-
public SnapshotLifecyclePolicyItem(SnapshotLifecyclePolicyMetadata policyMetadata) {
167-
this.policy = policyMetadata.getPolicy();
168-
this.version = policyMetadata.getVersion();
169-
this.modifiedDate = policyMetadata.getModifiedDate();
170-
this.lastSuccess = policyMetadata.getLastSuccess();
171-
this.lastFailure = policyMetadata.getLastFailure();
172-
}
173-
174-
public SnapshotLifecyclePolicyItem(StreamInput in) throws IOException {
175-
this.policy = new SnapshotLifecyclePolicy(in);
176-
this.version = in.readVLong();
177-
this.modifiedDate = in.readVLong();
178-
this.lastSuccess = in.readOptionalWriteable(SnapshotInvocationRecord::new);
179-
this.lastFailure = in.readOptionalWriteable(SnapshotInvocationRecord::new);
180-
}
181-
182-
public SnapshotLifecyclePolicy getPolicy() {
183-
return policy;
184-
}
185-
186-
public long getVersion() {
187-
return version;
188-
}
189-
190-
public long getModifiedDate() {
191-
return modifiedDate;
192-
}
193-
194-
@Override
195-
public void writeTo(StreamOutput out) throws IOException {
196-
policy.writeTo(out);
197-
out.writeVLong(version);
198-
out.writeVLong(modifiedDate);
199-
out.writeOptionalWriteable(lastSuccess);
200-
out.writeOptionalWriteable(lastFailure);
201-
}
202-
203-
@Override
204-
public int hashCode() {
205-
return Objects.hash(policy, version, modifiedDate);
206-
}
207-
208-
@Override
209-
public boolean equals(Object obj) {
210-
if (obj == null) {
211-
return false;
212-
}
213-
if (obj.getClass() != getClass()) {
214-
return false;
215-
}
216-
SnapshotLifecyclePolicyItem other = (SnapshotLifecyclePolicyItem) obj;
217-
return policy.equals(other.policy) &&
218-
version == other.version &&
219-
modifiedDate == other.modifiedDate;
220-
}
221-
222-
@Override
223-
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
224-
builder.startObject(policy.getId());
225-
builder.field("version", version);
226-
builder.field("modified_date", modifiedDate);
227-
builder.field("policy", policy);
228-
if (lastSuccess != null) {
229-
builder.field("last_success", lastSuccess);
230-
}
231-
if (lastFailure != null) {
232-
builder.field("last_failure", lastFailure);
233-
}
234-
builder.endObject();
235-
return builder;
236-
}
237-
}
238147
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
package org.elasticsearch.xpack.core.snapshotlifecycle;
8+
9+
import org.elasticsearch.common.io.stream.Writeable;
10+
import org.elasticsearch.test.AbstractWireSerializingTestCase;
11+
import org.elasticsearch.test.ESTestCase;
12+
13+
import static org.elasticsearch.xpack.core.snapshotlifecycle.SnapshotLifecyclePolicyMetadataTests.createRandomPolicy;
14+
import static org.elasticsearch.xpack.core.snapshotlifecycle.SnapshotLifecyclePolicyMetadataTests.createRandomPolicyMetadata;
15+
16+
public class SnapshotLifecyclePolicyItemTests extends AbstractWireSerializingTestCase<SnapshotLifecyclePolicyItem> {
17+
18+
@Override
19+
protected SnapshotLifecyclePolicyItem createTestInstance() {
20+
return new SnapshotLifecyclePolicyItem(createRandomPolicyMetadata(randomAlphaOfLengthBetween(5, 10)));
21+
}
22+
23+
@Override
24+
protected SnapshotLifecyclePolicyItem mutateInstance(SnapshotLifecyclePolicyItem instance) {
25+
switch (between(0, 4)) {
26+
case 0:
27+
String newPolicyId = randomValueOtherThan(instance.getPolicy().getId(), () -> randomAlphaOfLengthBetween(5, 10));
28+
return new SnapshotLifecyclePolicyItem(createRandomPolicy(newPolicyId),
29+
instance.getVersion(),
30+
instance.getModifiedDate(),
31+
instance.getLastSuccess(),
32+
instance.getLastFailure());
33+
case 1:
34+
return new SnapshotLifecyclePolicyItem(instance.getPolicy(),
35+
randomValueOtherThan(instance.getVersion(), ESTestCase::randomNonNegativeLong),
36+
instance.getModifiedDate(),
37+
instance.getLastSuccess(),
38+
instance.getLastFailure());
39+
case 2:
40+
return new SnapshotLifecyclePolicyItem(instance.getPolicy(),
41+
instance.getVersion(),
42+
randomValueOtherThan(instance.getModifiedDate(), ESTestCase::randomNonNegativeLong),
43+
instance.getLastSuccess(),
44+
instance.getLastFailure());
45+
case 3:
46+
return new SnapshotLifecyclePolicyItem(instance.getPolicy(),
47+
instance.getVersion(),
48+
instance.getModifiedDate(),
49+
randomValueOtherThan(instance.getLastSuccess(),
50+
SnapshotInvocationRecordTests::randomSnapshotInvocationRecord),
51+
instance.getLastFailure());
52+
case 4:
53+
return new SnapshotLifecyclePolicyItem(instance.getPolicy(),
54+
instance.getVersion(),
55+
instance.getModifiedDate(),
56+
instance.getLastSuccess(),
57+
randomValueOtherThan(instance.getLastFailure(),
58+
SnapshotInvocationRecordTests::randomSnapshotInvocationRecord));
59+
default:
60+
throw new AssertionError("failure, got illegal switch case");
61+
}
62+
}
63+
64+
@Override
65+
protected Writeable.Reader<SnapshotLifecyclePolicyItem> instanceReader() {
66+
return SnapshotLifecyclePolicyItem::new;
67+
}
68+
}

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/snapshotlifecycle/SnapshotLifecyclePolicyMetadataTests.java

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,10 @@ protected SnapshotLifecyclePolicyMetadata doParseInstance(XContentParser parser)
2828
@Override
2929
protected SnapshotLifecyclePolicyMetadata createTestInstance() {
3030
policyId = randomAlphaOfLength(5);
31-
SnapshotLifecyclePolicyMetadata.Builder builder = SnapshotLifecyclePolicyMetadata.builder()
32-
.setPolicy(createRandomPolicy(policyId))
33-
.setVersion(randomNonNegativeLong())
34-
.setModifiedDate(randomNonNegativeLong());
35-
if (randomBoolean()) {
36-
builder.setHeaders(randomHeaders());
37-
}
38-
if (randomBoolean()) {
39-
builder.setLastSuccess(randomSnapshotInvocationRecord());
40-
}
41-
if (randomBoolean()) {
42-
builder.setLastFailure(randomSnapshotInvocationRecord());
43-
}
44-
return builder.build();
31+
return createRandomPolicyMetadata(policyId);
4532
}
4633

47-
private Map<String, String> randomHeaders() {
34+
private static Map<String, String> randomHeaders() {
4835
Map<String, String> headers = new HashMap<>();
4936
int headerCount = randomIntBetween(1,10);
5037
for (int i = 0; i < headerCount; i++) {
@@ -71,7 +58,7 @@ protected SnapshotLifecyclePolicyMetadata mutateInstance(SnapshotLifecyclePolicy
7158
.build();
7259
case 2:
7360
return SnapshotLifecyclePolicyMetadata.builder(instance)
74-
.setHeaders(randomValueOtherThan(instance.getHeaders(), this::randomHeaders))
61+
.setHeaders(randomValueOtherThan(instance.getHeaders(), SnapshotLifecyclePolicyMetadataTests::randomHeaders))
7562
.build();
7663
case 3:
7764
return SnapshotLifecyclePolicyMetadata.builder(instance)
@@ -88,6 +75,23 @@ protected SnapshotLifecyclePolicyMetadata mutateInstance(SnapshotLifecyclePolicy
8875
}
8976
}
9077

78+
public static SnapshotLifecyclePolicyMetadata createRandomPolicyMetadata(String policyId) {
79+
SnapshotLifecyclePolicyMetadata.Builder builder = SnapshotLifecyclePolicyMetadata.builder()
80+
.setPolicy(createRandomPolicy(policyId))
81+
.setVersion(randomNonNegativeLong())
82+
.setModifiedDate(randomNonNegativeLong());
83+
if (randomBoolean()) {
84+
builder.setHeaders(randomHeaders());
85+
}
86+
if (randomBoolean()) {
87+
builder.setLastSuccess(randomSnapshotInvocationRecord());
88+
}
89+
if (randomBoolean()) {
90+
builder.setLastFailure(randomSnapshotInvocationRecord());
91+
}
92+
return builder.build();
93+
}
94+
9195
public static SnapshotLifecyclePolicy createRandomPolicy(String policyId) {
9296
Map<String, Object> config = new HashMap<>();
9397
for (int i = 0; i < randomIntBetween(2, 5); i++) {

x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/snapshotlifecycle/action/TransportGetSnapshotLifecycleAction.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.elasticsearch.threadpool.ThreadPool;
2222
import org.elasticsearch.transport.TransportService;
2323
import org.elasticsearch.xpack.core.snapshotlifecycle.SnapshotLifecycleMetadata;
24+
import org.elasticsearch.xpack.core.snapshotlifecycle.SnapshotLifecyclePolicyItem;
2425
import org.elasticsearch.xpack.core.snapshotlifecycle.action.GetSnapshotLifecycleAction;
2526

2627
import java.io.IOException;
@@ -66,7 +67,7 @@ protected void masterOperation(final GetSnapshotLifecycleAction.Request request,
6667
listener.onResponse(new GetSnapshotLifecycleAction.Response(Collections.emptyList()));
6768
} else {
6869
final Set<String> ids = new HashSet<>(Arrays.asList(request.getLifecycleIds()));
69-
List<GetSnapshotLifecycleAction.SnapshotLifecyclePolicyItem> lifecycles = snapMeta.getSnapshotConfigurations()
70+
List<SnapshotLifecyclePolicyItem> lifecycles = snapMeta.getSnapshotConfigurations()
7071
.values()
7172
.stream()
7273
.filter(meta -> {
@@ -76,7 +77,7 @@ protected void masterOperation(final GetSnapshotLifecycleAction.Request request,
7677
return ids.contains(meta.getPolicy().getId());
7778
}
7879
})
79-
.map(GetSnapshotLifecycleAction.SnapshotLifecyclePolicyItem::new)
80+
.map(SnapshotLifecyclePolicyItem::new)
8081
.collect(Collectors.toList());
8182
listener.onResponse(new GetSnapshotLifecycleAction.Response(lifecycles));
8283
}

0 commit comments

Comments
 (0)