Skip to content

Commit 02c01cb

Browse files
authored
Fix CreateSnapshotRequestTests Failure (#31630)
Original test failure found here in issue (#31625). Had to rework the tests to only include options available externally for create snapshot requests.
1 parent db6b339 commit 02c01cb

File tree

3 files changed

+106
-16
lines changed

3 files changed

+106
-16
lines changed

server/src/main/java/org/elasticsearch/action/support/IndicesOptions.java

+3
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,9 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
325325
builder.endArray();
326326
builder.field("ignore_unavailable", ignoreUnavailable());
327327
builder.field("allow_no_indices", allowNoIndices());
328+
builder.field("forbid_aliases_to_multiple_indices", allowAliasesToMultipleIndices() == false);
329+
builder.field("forbid_closed_indices", forbidClosedIndices());
330+
builder.field("ignore_aliases", ignoreAliases());
328331
return builder;
329332
}
330333

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

+21-16
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@
2020
package org.elasticsearch.action.admin.cluster.snapshots.create;
2121

2222
import org.elasticsearch.action.support.IndicesOptions;
23+
import org.elasticsearch.action.support.IndicesOptions.Option;
24+
import org.elasticsearch.action.support.IndicesOptions.WildcardStates;
2325
import org.elasticsearch.common.bytes.BytesReference;
2426
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
27+
import org.elasticsearch.common.xcontent.ToXContent.MapParams;
2528
import org.elasticsearch.common.xcontent.XContentBuilder;
2629
import org.elasticsearch.common.xcontent.XContentFactory;
2730
import org.elasticsearch.common.xcontent.XContentParser;
@@ -30,21 +33,24 @@
3033

3134
import java.io.IOException;
3235
import java.util.ArrayList;
36+
import java.util.Arrays;
37+
import java.util.Collection;
38+
import java.util.Collections;
39+
import java.util.EnumSet;
3340
import java.util.HashMap;
3441
import java.util.List;
3542
import java.util.Map;
3643

3744
public class CreateSnapshotRequestTests extends ESTestCase {
3845

3946
// tests creating XContent and parsing with source(Map) equivalency
40-
@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/31625")
4147
public void testToXContent() throws IOException {
4248
String repo = randomAlphaOfLength(5);
4349
String snap = randomAlphaOfLength(10);
4450

4551
CreateSnapshotRequest original = new CreateSnapshotRequest(repo, snap);
4652

47-
if (randomBoolean()) { // replace
53+
if (randomBoolean()) {
4854
List<String> indices = new ArrayList<>();
4955
int count = randomInt(3) + 1;
5056

@@ -55,44 +61,43 @@ public void testToXContent() throws IOException {
5561
original.indices(indices);
5662
}
5763

58-
if (randomBoolean()) { // replace
64+
if (randomBoolean()) {
5965
original.partial(randomBoolean());
6066
}
6167

62-
if (randomBoolean()) { // replace
68+
if (randomBoolean()) {
6369
Map<String, Object> settings = new HashMap<>();
6470
int count = randomInt(3) + 1;
6571

6672
for (int i = 0; i < count; ++i) {
6773
settings.put(randomAlphaOfLength(randomInt(3) + 2), randomAlphaOfLength(randomInt(3) + 2));
6874
}
6975

76+
original.settings(settings);
7077
}
7178

72-
if (randomBoolean()) { // replace
79+
if (randomBoolean()) {
7380
original.includeGlobalState(randomBoolean());
7481
}
7582

76-
if (randomBoolean()) { // replace
77-
IndicesOptions[] indicesOptions = new IndicesOptions[] {
78-
IndicesOptions.STRICT_EXPAND_OPEN,
79-
IndicesOptions.STRICT_EXPAND_OPEN_CLOSED,
80-
IndicesOptions.LENIENT_EXPAND_OPEN,
81-
IndicesOptions.STRICT_EXPAND_OPEN_FORBID_CLOSED,
82-
IndicesOptions.STRICT_SINGLE_INDEX_NO_EXPAND_FORBID_CLOSED};
83+
if (randomBoolean()) {
84+
Collection<WildcardStates> wildcardStates = randomSubsetOf(Arrays.asList(WildcardStates.values()));
85+
Collection<Option> options = randomSubsetOf(Arrays.asList(Option.ALLOW_NO_INDICES, Option.IGNORE_UNAVAILABLE));
8386

84-
original.indicesOptions(randomFrom(indicesOptions));
87+
original.indicesOptions(new IndicesOptions(
88+
options.isEmpty() ? Option.NONE : EnumSet.copyOf(options),
89+
wildcardStates.isEmpty() ? WildcardStates.NONE : EnumSet.copyOf(wildcardStates)));
8590
}
8691

87-
if (randomBoolean()) { // replace
92+
if (randomBoolean()) {
8893
original.waitForCompletion(randomBoolean());
8994
}
9095

91-
if (randomBoolean()) { // replace
96+
if (randomBoolean()) {
9297
original.masterNodeTimeout("60s");
9398
}
9499

95-
XContentBuilder builder = original.toXContent(XContentFactory.jsonBuilder(), null);
100+
XContentBuilder builder = original.toXContent(XContentFactory.jsonBuilder(), new MapParams(Collections.emptyMap()));
96101
XContentParser parser = XContentType.JSON.xContent().createParser(
97102
NamedXContentRegistry.EMPTY, null, BytesReference.bytes(builder).streamInput());
98103
Map<String, Object> map = parser.mapOrdered();

server/src/test/java/org/elasticsearch/action/support/IndicesOptionsTests.java

+82
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,27 @@
2020
package org.elasticsearch.action.support;
2121

2222
import org.elasticsearch.Version;
23+
import org.elasticsearch.action.support.IndicesOptions.Option;
24+
import org.elasticsearch.action.support.IndicesOptions.WildcardStates;
25+
import org.elasticsearch.common.bytes.BytesReference;
2326
import org.elasticsearch.common.io.stream.BytesStreamOutput;
2427
import org.elasticsearch.common.io.stream.StreamInput;
28+
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
29+
import org.elasticsearch.common.xcontent.ToXContent.MapParams;
30+
import org.elasticsearch.common.xcontent.XContentBuilder;
31+
import org.elasticsearch.common.xcontent.XContentFactory;
32+
import org.elasticsearch.common.xcontent.XContentParser;
33+
import org.elasticsearch.common.xcontent.XContentType;
2534
import org.elasticsearch.test.ESTestCase;
2635
import org.elasticsearch.test.EqualsHashCodeTestUtils;
2736

37+
import java.io.IOException;
38+
import java.util.Arrays;
39+
import java.util.Collection;
40+
import java.util.Collections;
41+
import java.util.EnumSet;
2842
import java.util.HashMap;
43+
import java.util.List;
2944
import java.util.Map;
3045

3146
import static org.elasticsearch.test.VersionUtils.randomVersionBetween;
@@ -242,4 +257,71 @@ public void testEqualityAndHashCode() {
242257
allowAliasesToMulti, forbidClosed, ignoreAliases);
243258
});
244259
}
260+
261+
public void testFromMap() {
262+
IndicesOptions defaults = IndicesOptions.strictExpandOpen();
263+
Collection<String> wildcardStates = randomBoolean() ?
264+
null : randomSubsetOf(Arrays.asList("open", "closed"));
265+
Boolean ignoreUnavailable = randomBoolean() ? null : randomBoolean();
266+
Boolean allowNoIndices = randomBoolean() ? null : randomBoolean();
267+
268+
Map<String, Object> settings = new HashMap<>();
269+
270+
if (wildcardStates != null) {
271+
settings.put("expand_wildcards", wildcardStates);
272+
}
273+
274+
if (ignoreUnavailable != null) {
275+
settings.put("ignore_unavailable", ignoreUnavailable);
276+
}
277+
278+
if (allowNoIndices != null) {
279+
settings.put("allow_no_indices", allowNoIndices);
280+
}
281+
282+
IndicesOptions fromMap = IndicesOptions.fromMap(settings, defaults);
283+
284+
boolean open = wildcardStates != null ? wildcardStates.contains("open") : defaults.expandWildcardsOpen();
285+
assertEquals(fromMap.expandWildcardsOpen(), open);
286+
boolean closed = wildcardStates != null ? wildcardStates.contains("closed") : defaults.expandWildcardsClosed();
287+
assertEquals(fromMap.expandWildcardsClosed(), closed);
288+
289+
assertEquals(fromMap.ignoreUnavailable(), ignoreUnavailable == null ? defaults.ignoreUnavailable() : ignoreUnavailable);
290+
assertEquals(fromMap.allowNoIndices(), allowNoIndices == null ? defaults.allowNoIndices() : allowNoIndices);
291+
}
292+
293+
public void testToXContent() throws IOException {
294+
Collection<WildcardStates> wildcardStates = randomSubsetOf(Arrays.asList(WildcardStates.values()));
295+
Collection<Option> options = randomSubsetOf(Arrays.asList(Option.values()));
296+
297+
IndicesOptions indicesOptions = new IndicesOptions(
298+
options.isEmpty() ? Option.NONE : EnumSet.copyOf(options),
299+
wildcardStates.isEmpty() ? WildcardStates.NONE : EnumSet.copyOf(wildcardStates));
300+
301+
XContentBuilder builder = XContentFactory.jsonBuilder();
302+
builder.startObject();
303+
indicesOptions.toXContent(builder, new MapParams(Collections.emptyMap()));
304+
builder.endObject();
305+
XContentParser parser = XContentType.JSON.xContent().createParser(
306+
NamedXContentRegistry.EMPTY, null, BytesReference.bytes(builder).streamInput());
307+
Map<String, Object> map = parser.mapOrdered();
308+
309+
boolean open = wildcardStates.contains(WildcardStates.OPEN);
310+
if (open) {
311+
assertTrue(((List)map.get("expand_wildcards")).contains("open"));
312+
} else {
313+
assertFalse(((List)map.get("expand_wildcards")).contains("open"));
314+
}
315+
boolean closed = wildcardStates.contains(WildcardStates.CLOSED);
316+
if (closed) {
317+
assertTrue(((List)map.get("expand_wildcards")).contains("closed"));
318+
} else {
319+
assertFalse(((List)map.get("expand_wildcards")).contains("closed"));
320+
}
321+
assertEquals(map.get("ignore_unavailable"), options.contains(Option.IGNORE_UNAVAILABLE));
322+
assertEquals(map.get("allow_no_indices"), options.contains(Option.ALLOW_NO_INDICES));
323+
assertEquals(map.get("forbid_aliases_to_multiple_indices"), options.contains(Option.FORBID_ALIASES_TO_MULTIPLE_INDICES));
324+
assertEquals(map.get("forbid_closed_indices"), options.contains(Option.FORBID_CLOSED_INDICES));
325+
assertEquals(map.get("ignore_aliases"), options.contains(Option.IGNORE_ALIASES));
326+
}
245327
}

0 commit comments

Comments
 (0)