Skip to content

Commit

Permalink
Fix CreateSnapshotRequestTests Failure (#31630)
Browse files Browse the repository at this point in the history
Original test failure found here in issue (#31625). Had to rework the tests to only include options available externally for create snapshot requests.
  • Loading branch information
jdconrad committed Jun 28, 2018
1 parent bb1b7d0 commit df95ae2
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,9 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
builder.endArray();
builder.field("ignore_unavailable", ignoreUnavailable());
builder.field("allow_no_indices", allowNoIndices());
builder.field("forbid_aliases_to_multiple_indices", allowAliasesToMultipleIndices() == false);
builder.field("forbid_closed_indices", forbidClosedIndices());
builder.field("ignore_aliases", ignoreAliases());
return builder;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@
package org.elasticsearch.action.admin.cluster.snapshots.create;

import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.action.support.IndicesOptions.Option;
import org.elasticsearch.action.support.IndicesOptions.WildcardStates;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.ToXContent.MapParams;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser;
Expand All @@ -30,6 +33,10 @@

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -43,7 +50,7 @@ public void testToXContent() throws IOException {

CreateSnapshotRequest original = new CreateSnapshotRequest(repo, snap);

if (randomBoolean()) { // replace
if (randomBoolean()) {
List<String> indices = new ArrayList<>();
int count = randomInt(3) + 1;

Expand All @@ -54,44 +61,43 @@ public void testToXContent() throws IOException {
original.indices(indices);
}

if (randomBoolean()) { // replace
if (randomBoolean()) {
original.partial(randomBoolean());
}

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

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

original.settings(settings);
}

if (randomBoolean()) { // replace
if (randomBoolean()) {
original.includeGlobalState(randomBoolean());
}

if (randomBoolean()) { // replace
IndicesOptions[] indicesOptions = new IndicesOptions[] {
IndicesOptions.STRICT_EXPAND_OPEN,
IndicesOptions.STRICT_EXPAND_OPEN_CLOSED,
IndicesOptions.LENIENT_EXPAND_OPEN,
IndicesOptions.STRICT_EXPAND_OPEN_FORBID_CLOSED,
IndicesOptions.STRICT_SINGLE_INDEX_NO_EXPAND_FORBID_CLOSED};
if (randomBoolean()) {
Collection<WildcardStates> wildcardStates = randomSubsetOf(Arrays.asList(WildcardStates.values()));
Collection<Option> options = randomSubsetOf(Arrays.asList(Option.ALLOW_NO_INDICES, Option.IGNORE_UNAVAILABLE));

original.indicesOptions(randomFrom(indicesOptions));
original.indicesOptions(new IndicesOptions(
options.isEmpty() ? Option.NONE : EnumSet.copyOf(options),
wildcardStates.isEmpty() ? WildcardStates.NONE : EnumSet.copyOf(wildcardStates)));
}

if (randomBoolean()) { // replace
if (randomBoolean()) {
original.waitForCompletion(randomBoolean());
}

if (randomBoolean()) { // replace
if (randomBoolean()) {
original.masterNodeTimeout("60s");
}

XContentBuilder builder = original.toXContent(XContentFactory.jsonBuilder(), null);
XContentBuilder builder = original.toXContent(XContentFactory.jsonBuilder(), new MapParams(Collections.emptyMap()));
XContentParser parser = XContentType.JSON.xContent().createParser(
NamedXContentRegistry.EMPTY, null, BytesReference.bytes(builder).streamInput());
Map<String, Object> map = parser.mapOrdered();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,27 @@
package org.elasticsearch.action.support;

import org.elasticsearch.Version;
import org.elasticsearch.action.support.IndicesOptions.Option;
import org.elasticsearch.action.support.IndicesOptions.WildcardStates;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.ToXContent.MapParams;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.test.EqualsHashCodeTestUtils;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.elasticsearch.test.VersionUtils.randomVersionBetween;
Expand Down Expand Up @@ -246,4 +261,71 @@ public void testEqualityAndHashCode() {
allowAliasesToMulti, forbidClosed, ignoreAliases);
});
}

public void testFromMap() {
IndicesOptions defaults = IndicesOptions.strictExpandOpen();
Collection<String> wildcardStates = randomBoolean() ?
null : randomSubsetOf(Arrays.asList("open", "closed"));
Boolean ignoreUnavailable = randomBoolean() ? null : randomBoolean();
Boolean allowNoIndices = randomBoolean() ? null : randomBoolean();

Map<String, Object> settings = new HashMap<>();

if (wildcardStates != null) {
settings.put("expand_wildcards", wildcardStates);
}

if (ignoreUnavailable != null) {
settings.put("ignore_unavailable", ignoreUnavailable);
}

if (allowNoIndices != null) {
settings.put("allow_no_indices", allowNoIndices);
}

IndicesOptions fromMap = IndicesOptions.fromMap(settings, defaults);

boolean open = wildcardStates != null ? wildcardStates.contains("open") : defaults.expandWildcardsOpen();
assertEquals(fromMap.expandWildcardsOpen(), open);
boolean closed = wildcardStates != null ? wildcardStates.contains("closed") : defaults.expandWildcardsClosed();
assertEquals(fromMap.expandWildcardsClosed(), closed);

assertEquals(fromMap.ignoreUnavailable(), ignoreUnavailable == null ? defaults.ignoreUnavailable() : ignoreUnavailable);
assertEquals(fromMap.allowNoIndices(), allowNoIndices == null ? defaults.allowNoIndices() : allowNoIndices);
}

public void testToXContent() throws IOException {
Collection<WildcardStates> wildcardStates = randomSubsetOf(Arrays.asList(WildcardStates.values()));
Collection<Option> options = randomSubsetOf(Arrays.asList(Option.values()));

IndicesOptions indicesOptions = new IndicesOptions(
options.isEmpty() ? Option.NONE : EnumSet.copyOf(options),
wildcardStates.isEmpty() ? WildcardStates.NONE : EnumSet.copyOf(wildcardStates));

XContentBuilder builder = XContentFactory.jsonBuilder();
builder.startObject();
indicesOptions.toXContent(builder, new MapParams(Collections.emptyMap()));
builder.endObject();
XContentParser parser = XContentType.JSON.xContent().createParser(
NamedXContentRegistry.EMPTY, null, BytesReference.bytes(builder).streamInput());
Map<String, Object> map = parser.mapOrdered();

boolean open = wildcardStates.contains(WildcardStates.OPEN);
if (open) {
assertTrue(((List)map.get("expand_wildcards")).contains("open"));
} else {
assertFalse(((List)map.get("expand_wildcards")).contains("open"));
}
boolean closed = wildcardStates.contains(WildcardStates.CLOSED);
if (closed) {
assertTrue(((List)map.get("expand_wildcards")).contains("closed"));
} else {
assertFalse(((List)map.get("expand_wildcards")).contains("closed"));
}
assertEquals(map.get("ignore_unavailable"), options.contains(Option.IGNORE_UNAVAILABLE));
assertEquals(map.get("allow_no_indices"), options.contains(Option.ALLOW_NO_INDICES));
assertEquals(map.get("forbid_aliases_to_multiple_indices"), options.contains(Option.FORBID_ALIASES_TO_MULTIPLE_INDICES));
assertEquals(map.get("forbid_closed_indices"), options.contains(Option.FORBID_CLOSED_INDICES));
assertEquals(map.get("ignore_aliases"), options.contains(Option.IGNORE_ALIASES));
}
}

0 comments on commit df95ae2

Please sign in to comment.