Skip to content
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

[Rest Api Compatibility] Add conditions to InjectHeaders transformation #75001

Merged
Merged
Show file tree
Hide file tree
Changes from 21 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 @@ -250,7 +250,12 @@ class YamlRestCompatTestPluginFuncTest extends AbstractRestResourcesFuncTest {
- is_false: "value_to_replace"
- is_true: "value_not_to_replace"
- is_false: "value_not_to_replace"
---
"use cat with no header":
- do:
cat.indices:
{}
- match: {}
""".stripIndent()
when:
def result = gradleRunner("yamlRestCompatTest").build()
Expand Down Expand Up @@ -337,6 +342,16 @@ class YamlRestCompatTestPluginFuncTest extends AbstractRestResourcesFuncTest {
- is_false: "replaced_value"
- is_true: "value_not_to_replace"
- is_false: "value_not_to_replace"
---
"use cat with no header":
- do:
cat.indices:
{}
allowed_warnings:
- "added allowed warning"
allowed_warnings_regex:
- "added allowed warning regex .* [0-9]"
- match: {}
""".stripIndent()).readAll()

expectedAll.eachWithIndex{ ObjectNode expected, int i ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
Expand Down Expand Up @@ -94,7 +95,18 @@ public RestCompatTestTransformTask(
// always inject compat headers
headers.put("Content-Type", "application/vnd.elasticsearch+json;compatible-with=" + compatibleVersion);
headers.put("Accept", "application/vnd.elasticsearch+json;compatible-with=" + compatibleVersion);
transformations.add(new InjectHeaders(headers));
transformations.add(new InjectHeaders(headers, Set.of(RestCompatTestTransformTask::doesNotHaveCatOperation)));
}

private static boolean doesNotHaveCatOperation(ObjectNode doNodeValue) {
final Iterator<String> fieldNamesIterator = doNodeValue.fieldNames();
while (fieldNamesIterator.hasNext()) {
final String fieldName = fieldNamesIterator.next();
if (fieldName.startsWith("cat.")) {
return false;
}
}
return true;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
import org.gradle.api.tasks.Internal;

import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.Function;

/**
* A {@link RestTestTransform} that injects HTTP headers into a REST test. This includes adding the necessary values to the "do" section
Expand All @@ -28,25 +31,36 @@ public class InjectHeaders extends FeatureInjector implements RestTestTransformB
private static JsonNodeFactory jsonNodeFactory = JsonNodeFactory.withExactBigDecimals(false);

private final Map<String, String> headers;
private final Set<Function<ObjectNode, Boolean>> applyConditions;

/**
* @param headers The headers to inject
* @param applyConditions a set of conditions that has to be satisfied in order to apply headers
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add "If the Set is empty then headers are always applied"

*/
public InjectHeaders(Map<String, String> headers) {
public InjectHeaders(Map<String, String> headers, Set<Function<ObjectNode, Boolean>> applyConditions) {
this.headers = headers;
this.applyConditions = applyConditions;
}

@Override
public void transformTest(ObjectNode doNodeParent) {
ObjectNode doNodeValue = (ObjectNode) doNodeParent.get(getKeyToFind());
ObjectNode headersNode = (ObjectNode) doNodeValue.get("headers");
if (headersNode == null) {
headersNode = new ObjectNode(jsonNodeFactory);
}
for (Map.Entry<String, String> entry : headers.entrySet()) {
headersNode.set(entry.getKey(), TextNode.valueOf(entry.getValue()));

if (shouldApplyHeaders(doNodeValue)) {
ObjectNode headersNode = (ObjectNode) doNodeValue.get("headers");
if (headersNode == null) {
headersNode = new ObjectNode(jsonNodeFactory);
}

for (Map.Entry<String, String> entry : headers.entrySet()) {
headersNode.set(entry.getKey(), TextNode.valueOf(entry.getValue()));
}
doNodeValue.set("headers", headersNode);
}
doNodeValue.set("headers", headersNode);
}

private boolean shouldApplyHeaders(ObjectNode doNodeValue) {
return applyConditions.stream().allMatch(f -> f.apply(doNodeValue));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.LongAdder;
import java.util.function.Consumer;
import java.util.stream.Collectors;

public abstract class TransformTests extends GradleUnitTestCase {
Expand Down Expand Up @@ -125,8 +126,8 @@ protected List<String> getKnownFeatures() {

protected List<RestTestTransform<?>> getTransformations() {
List<RestTestTransform<?>> transformations = new ArrayList<>();
transformations.add(new InjectHeaders(headers1));
transformations.add(new InjectHeaders(headers2));
transformations.add(new InjectHeaders(headers1, Collections.emptySet()));
transformations.add(new InjectHeaders(headers2, Collections.emptySet()));
return transformations;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@
import org.elasticsearch.gradle.internal.test.rest.transform.headers.InjectHeaders;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class InjectHeaderTests extends InjectFeatureTests {

Expand Down Expand Up @@ -57,14 +60,42 @@ public void testInjectHeadersWithPreExisting() throws Exception {
validateBodyHasHeaders(transformedTests, headers);
}


@Test
public void testNotInjectingHeaders() throws Exception {
String testName = "/rest/transform/header/with_operation_to_skip_adding_headers.yml";
List<ObjectNode> tests = getTests(testName);
validateSetupExist(tests);
validateBodyHasHeaders(tests, Map.of("foo", "bar"));

List<RestTestTransform<?>> transformations =
Collections.singletonList(new InjectHeaders(headers, Set.of(InjectHeaderTests::applyCondition)));
List<ObjectNode> transformedTests = transformTests(tests, transformations);
printTest(testName, transformedTests);
validateSetupAndTearDown(transformedTests);
validateBodyHasHeaders(tests, Map.of("foo", "bar"));
validateBodyHasHeaders(transformedTests, Map.of("foo", "bar"));
}

private static boolean applyCondition(ObjectNode doNodeValue) {
final Iterator<String> fieldNamesIterator = doNodeValue.fieldNames();
while (fieldNamesIterator.hasNext()) {
final String fieldName = fieldNamesIterator.next();
if (fieldName.startsWith("something_to_skip")) {
return false;
}
}
return true;
}

@Override
protected List<String> getKnownFeatures() {
return Collections.singletonList("headers");
}

@Override
protected List<RestTestTransform<?>> getTransformations() {
return Collections.singletonList(new InjectHeaders(headers));
return Collections.singletonList(new InjectHeaders(headers, Collections.emptySet()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
setup:
- skip:
features: headers
---
"Test without a setup":
- do:
headers:
foo: "bar"
something_to_skip:
id: "something"
- match: { acknowledged: true }