Skip to content

Fix CreateSnapshotRequestTests Failure #31630

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,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,21 +33,24 @@

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;

public class CreateSnapshotRequestTests extends ESTestCase {

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

CreateSnapshotRequest original = new CreateSnapshotRequest(repo, snap);

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

Expand All @@ -55,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 @@ -242,4 +257,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));
}
}