Skip to content

Commit

Permalink
support regex warnings via REST test transform (#70359)
Browse files Browse the repository at this point in the history
This commit adds support to for injecting warnings_regex and
allowed_warnings_regex via test transformations used by
compatible testing.

related: #69501
related: #69010
  • Loading branch information
jakelandis authored Mar 16, 2021
1 parent 3417684 commit fec4ee5
Show file tree
Hide file tree
Showing 10 changed files with 234 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class YamlRestCompatTestPluginFuncTest extends AbstractRestResourcesFuncTest {
def YAML_FACTORY = new YAMLFactory()
def MAPPER = new ObjectMapper(YAML_FACTORY)
def READER = MAPPER.readerFor(ObjectNode.class)
def WRITER = MAPPER.writerFor(ObjectNode.class)

def "yamlRestCompatTest does nothing when there are no tests"() {
given:
Expand Down Expand Up @@ -211,7 +212,9 @@ class YamlRestCompatTestPluginFuncTest extends AbstractRestResourcesFuncTest {
task.removeMatch("_source.junk", "two")
task.addMatch("_source.added", [name: 'jake', likes: 'cheese'], "one")
task.addWarning("one", "warning1", "warning2")
task.addWarningRegex("two", "regex warning here .* [a-z]")
task.addAllowedWarning("added allowed warning")
task.addAllowedWarningRegex("added allowed warning regex .* [0-9]")
task.removeWarning("one", "warning to remove")
})
// can't actually spin up test cluster from this test
Expand Down Expand Up @@ -262,7 +265,9 @@ class YamlRestCompatTestPluginFuncTest extends AbstractRestResourcesFuncTest {
features:
- "headers"
- "warnings"
- "warnings_regex"
- "allowed_warnings"
- "allowed_warnings_regex"
---
one:
- do:
Expand All @@ -273,10 +278,12 @@ class YamlRestCompatTestPluginFuncTest extends AbstractRestResourcesFuncTest {
- "warning1"
- "warning2"
headers:
Accept: "application/vnd.elasticsearch+json;compatible-with=7"
Content-Type: "application/vnd.elasticsearch+json;compatible-with=7"
Accept: "application/vnd.elasticsearch+json;compatible-with=7"
allowed_warnings:
- "added allowed warning"
allowed_warnings_regex:
- "added allowed warning regex .* [0-9]"
- match:
_source.values:
- "z"
Expand All @@ -298,10 +305,14 @@ class YamlRestCompatTestPluginFuncTest extends AbstractRestResourcesFuncTest {
index: "test"
id: 1
headers:
Accept: "application/vnd.elasticsearch+json;compatible-with=7"
Content-Type: "application/vnd.elasticsearch+json;compatible-with=7"
Accept: "application/vnd.elasticsearch+json;compatible-with=7"
warnings_regex:
- "regex warning here .* [a-z]"
allowed_warnings:
- "added allowed warning"
allowed_warnings_regex:
- "added allowed warning regex .* [0-9]"
- match:
_source.values:
- "foo"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,15 @@ public void addWarning(String testName, String... warnings) {
transformations.add(new InjectWarnings(Arrays.asList(warnings), testName));
}

/**
* Adds one or more regex warnings to the given test
* @param testName the test name to add the regex warning
* @param warningsRegex the regex warning(s) to add
*/
public void addWarningRegex(String testName, String... warningsRegex) {
transformations.add(new InjectWarnings(true, Arrays.asList(warningsRegex), testName));
}

/**
* Removes one or more warnings
* @param warnings the warning(s) to remove
Expand All @@ -164,6 +173,14 @@ public void addAllowedWarning(String... allowedWarnings) {
transformations.add(new InjectAllowedWarnings(Arrays.asList(allowedWarnings)));
}

/**
* Adds one or more allowed regular expression warnings
* @param allowedWarningsRegex the regex warning(s) to add
*/
public void addAllowedWarningRegex(String... allowedWarningsRegex) {
transformations.add(new InjectAllowedWarnings(true, Arrays.asList(allowedWarningsRegex)));
}

@OutputDirectory
public DirectoryProperty getOutputDirectory() {
return outputDirectory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,31 @@ public class InjectAllowedWarnings extends FeatureInjector implements RestTestTr
private static JsonNodeFactory jsonNodeFactory = JsonNodeFactory.withExactBigDecimals(false);

private final List<String> allowedWarnings;
private final boolean isRegex;

/**
* @param allowedWarnings The allowed warnings to inject
*/
public InjectAllowedWarnings(List<String> allowedWarnings) {
this(false, allowedWarnings);
}

/**
* @param isRegex true if should inject the regex variant of allowed warnings
* @param allowedWarnings The allowed warnings to inject
*/
public InjectAllowedWarnings(boolean isRegex, List<String> allowedWarnings) {
this.isRegex = isRegex;
this.allowedWarnings = allowedWarnings;
}

@Override
public void transformTest(ObjectNode doNodeParent) {
ObjectNode doNodeValue = (ObjectNode) doNodeParent.get(getKeyToFind());
ArrayNode arrayWarnings = (ArrayNode) doNodeValue.get("allowed_warnings");
ArrayNode arrayWarnings = (ArrayNode) doNodeValue.get(getSkipFeatureName());
if (arrayWarnings == null) {
arrayWarnings = new ArrayNode(jsonNodeFactory);
doNodeValue.set("allowed_warnings", arrayWarnings);
doNodeValue.set(getSkipFeatureName(), arrayWarnings);
}
allowedWarnings.forEach(arrayWarnings::add);
}
Expand All @@ -52,9 +62,9 @@ public String getKeyToFind() {
}

@Override
@Internal
@Input
public String getSkipFeatureName() {
return "allowed_warnings";
return isRegex ? "allowed_warnings_regex" : "allowed_warnings";
}

@Input
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,34 @@ public class InjectWarnings extends FeatureInjector implements RestTestTransform

private final List<String> warnings;
private final String testName;
private final boolean isRegex;

/**
* @param warnings The warnings to inject
* @param testName The testName to inject
*/
public InjectWarnings(List<String> warnings, String testName) {
this(false, warnings, testName);
}

/**
* @param isRegex true is should inject the regex variant of warning
* @param warnings The warnings to inject
* @param testName The testName to inject
*/
public InjectWarnings(boolean isRegex, List<String> warnings, String testName) {
this.isRegex = isRegex;
this.warnings = warnings;
this.testName = Objects.requireNonNull(testName, "inject warnings is only supported for named tests");
}

@Override
public void transformTest(ObjectNode doNodeParent) {
ObjectNode doNodeValue = (ObjectNode) doNodeParent.get(getKeyToFind());
ArrayNode arrayWarnings = (ArrayNode) doNodeValue.get("warnings");
ArrayNode arrayWarnings = (ArrayNode) doNodeValue.get(getSkipFeatureName());
if (arrayWarnings == null) {
arrayWarnings = new ArrayNode(jsonNodeFactory);
doNodeValue.set("warnings", arrayWarnings);
doNodeValue.set(getSkipFeatureName(), arrayWarnings);
}
warnings.forEach(arrayWarnings::add);
}
Expand All @@ -57,9 +68,9 @@ public String getKeyToFind() {
}

@Override
@Internal
@Input
public String getSkipFeatureName() {
return "warnings";
return isRegex ? "warnings_regex" : "warnings";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

package org.elasticsearch.gradle.test.rest.transform.warnings;

import com.fasterxml.jackson.databind.node.ObjectNode;
import org.elasticsearch.gradle.test.rest.transform.RestTestTransform;
import org.elasticsearch.gradle.test.rest.transform.feature.InjectFeatureTests;
import org.junit.Test;

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

public class InjectAllowedWarningsRegexTests extends InjectFeatureTests {

Set<String> addWarnings = Set.of("added warning");
private static final String ALLOWED_WARNINGS_REGEX = "allowed_warnings_regex";

/**
* test file does not any allowed warnings defined
*/
@Test
public void testInjectAllowedWarningsNoPreExisting() throws Exception {
String testName = "/rest/transform/warnings/without_existing_warnings.yml";
List<ObjectNode> tests = getTests(testName);
validateSetupDoesNotExist(tests);
List<ObjectNode> transformedTests = transformTests(tests);
printTest(testName, transformedTests);
validateSetupAndTearDown(transformedTests);
validateBodyHasWarnings(ALLOWED_WARNINGS_REGEX, transformedTests, addWarnings);
}

/**
* test file has preexisting allowed warnings
*/
@Test
public void testInjectAllowedWarningsWithPreExisting() throws Exception {
String testName = "/rest/transform/warnings/with_existing_allowed_warnings.yml";
List<ObjectNode> tests = getTests(testName);
validateSetupExist(tests);
validateBodyHasWarnings(ALLOWED_WARNINGS_REGEX, tests, Set.of("c", "d"));
List<ObjectNode> transformedTests = transformTests(tests);
printTest(testName, transformedTests);
validateSetupAndTearDown(transformedTests);
validateBodyHasWarnings(ALLOWED_WARNINGS_REGEX, tests, Set.of("c", "d"));
validateBodyHasWarnings(ALLOWED_WARNINGS_REGEX, tests, addWarnings);
}

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

@Override
protected List<RestTestTransform<?>> getTransformations() {
return Collections.singletonList(new InjectAllowedWarnings(true, new ArrayList<>(addWarnings)));
}

@Override
protected boolean getHumanDebug() {
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

package org.elasticsearch.gradle.test.rest.transform.warnings;

import com.fasterxml.jackson.databind.node.ObjectNode;
import org.elasticsearch.gradle.test.rest.transform.RestTestTransform;
import org.elasticsearch.gradle.test.rest.transform.feature.InjectFeatureTests;
import org.junit.Test;

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

public class InjectWarningsRegexTests extends InjectFeatureTests {

private static final String WARNINGS_REGEX = "warnings_regex";
Set<String> addWarnings = Set.of("added warning");

/**
* inject warning requires a test name to insert
*/
@Test
public void testInjectWarningsRequiresTestName() throws Exception {
String testName = "/rest/transform/warnings/without_existing_warnings.yml";
List<ObjectNode> tests = getTests(testName);
validateSetupDoesNotExist(tests);
assertEquals(
"inject warnings is only supported for named tests",
expectThrows(
NullPointerException.class,
() -> transformTests(tests, Collections.singletonList(new InjectWarnings(new ArrayList<>(addWarnings), null)))
).getMessage()
);
}

/**
* test file does not any warnings defined
*/
@Test
public void testInjectWarningsNoPreExisting() throws Exception {
String testName = "/rest/transform/warnings/without_existing_warnings.yml";
List<ObjectNode> tests = getTests(testName);
validateSetupDoesNotExist(tests);
List<ObjectNode> transformedTests = transformTests(tests);
printTest(testName, transformedTests);
validateSetupAndTearDown(transformedTests);
validateBodyHasWarnings(WARNINGS_REGEX, "Test warnings", transformedTests, addWarnings);
validateBodyHasNoWarnings(WARNINGS_REGEX, "Test another", transformedTests);
}

/**
* test file has preexisting warnings
*/
@Test
public void testInjectWarningsWithPreExisting() throws Exception {
String testName = "/rest/transform/warnings/with_existing_warnings.yml";
List<ObjectNode> tests = getTests(testName);
validateSetupExist(tests);
validateBodyHasWarnings(WARNINGS_REGEX, tests, Set.of("c", "d"));
List<ObjectNode> transformedTests = transformTests(tests);
printTest(testName, transformedTests);
validateSetupAndTearDown(transformedTests);
validateBodyHasWarnings(WARNINGS_REGEX, tests, Set.of("c", "d"));
validateBodyHasWarnings(WARNINGS_REGEX, "Test warnings", tests, addWarnings);
}

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

@Override
protected List<RestTestTransform<?>> getTransformations() {
return Collections.singletonList(new InjectWarnings(true, new ArrayList<>(addWarnings), "Test warnings"));
}

@Override
protected boolean getHumanDebug() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,6 @@ protected List<RestTestTransform<?>> getTransformations() {

@Override
protected boolean getHumanDebug() {
return true;
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,6 @@ protected List<RestTestTransform<?>> getTransformations() {

@Override
protected boolean getHumanDebug() {
return true;
return false;
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
---
setup:
- skip:
features: allowed_warnings
features:
- allowed_warnings
- allowed_warnings_regex
---
"Test with existing allowed warnings":
- do:
allowed_warnings:
- "a"
- "b"
allowed_warnings_regex:
- "c"
- "d"
something:
id: "something"
- match: { acknowledged: true }
Expand Down
Loading

0 comments on commit fec4ee5

Please sign in to comment.