Tests: Make $_path support dots in paths (#28917)

`$_path` is used by documentation tests to ignore a value from a
response, for example:

```
[source,js]
----
{
  "count": 1,
  "datafeeds": [
    {
      "datafeed_id": "datafeed-total-requests",
      "state": "started",
      "node": {
        ...
        "attributes": {
          "ml.machine_memory": "17179869184",
          "ml.max_open_jobs": "20",
          "ml.enabled": "true"
        }
      },
      "assignment_explanation": ""
    }
  ]
}
----
// TESTRESPONSE[s/"17179869184"/$body.$_path/]
```

That example shows `17179869184` in the compiled docs but when it runs
the tests generated by that doc it ignores `17179869184` and asserts
instead that there is a value in that field. This is required because we
can't predict things like "how many milliseconds will this take?" and
"how much memory will this take?".

Before this change it was impossible to use `$_path` when any component
of the path contained a `.`. This fixes the `$_path` evaluator to
properly escape `.`.

Closes #28770
This commit is contained in:
Nik Everett 2018-03-19 14:17:09 -04:00 committed by GitHub
parent 5135484a27
commit a813492fe3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 13 deletions

View File

@ -184,10 +184,10 @@ public class Stash implements ToXContentFragment {
StringBuilder pathBuilder = new StringBuilder(); StringBuilder pathBuilder = new StringBuilder();
Iterator<Object> element = path.iterator(); Iterator<Object> element = path.iterator();
if (element.hasNext()) { if (element.hasNext()) {
pathBuilder.append(element.next()); pathBuilder.append(element.next().toString().replace(".", "\\."));
while (element.hasNext()) { while (element.hasNext()) {
pathBuilder.append('.'); pathBuilder.append('.');
pathBuilder.append(element.next()); pathBuilder.append(element.next().toString().replace(".", "\\."));
} }
} }
String builtPath = Matcher.quoteReplacement(pathBuilder.toString()); String builtPath = Matcher.quoteReplacement(pathBuilder.toString());

View File

@ -119,16 +119,22 @@ public class StashTests extends ESTestCase {
public void testPathInList() throws IOException { public void testPathInList() throws IOException {
Stash stash = new Stash(); Stash stash = new Stash();
stash.stashValue("body", singletonMap("foo", Arrays.asList("a", "b"))); String topLevelKey;
if (randomBoolean()) {
topLevelKey = randomAlphaOfLength(2) + "." + randomAlphaOfLength(2);
} else {
topLevelKey = randomAlphaOfLength(5);
}
stash.stashValue("body", singletonMap(topLevelKey, Arrays.asList("a", "b")));
Map<String, Object> expected; Map<String, Object> expected;
Map<String, Object> map; Map<String, Object> map;
if (randomBoolean()) { if (randomBoolean()) {
expected = singletonMap("foo", Arrays.asList("test", "boooooh!")); expected = singletonMap(topLevelKey, Arrays.asList("test", "boooooh!"));
map = singletonMap("foo", Arrays.asList("test", "${body.$_path}oooooh!")); map = singletonMap(topLevelKey, Arrays.asList("test", "${body.$_path}oooooh!"));
} else { } else {
expected = singletonMap("foo", Arrays.asList("test", "b")); expected = singletonMap(topLevelKey, Arrays.asList("test", "b"));
map = singletonMap("foo", Arrays.asList("test", "$body.$_path")); map = singletonMap(topLevelKey, Arrays.asList("test", "$body.$_path"));
} }
Map<String, Object> actual = stash.replaceStashedValues(map); Map<String, Object> actual = stash.replaceStashedValues(map);
@ -138,21 +144,26 @@ public class StashTests extends ESTestCase {
public void testPathInMapValue() throws IOException { public void testPathInMapValue() throws IOException {
Stash stash = new Stash(); Stash stash = new Stash();
stash.stashValue("body", singletonMap("foo", singletonMap("a", "b"))); String topLevelKey;
if (randomBoolean()) {
topLevelKey = randomAlphaOfLength(2) + "." + randomAlphaOfLength(2);
} else {
topLevelKey = randomAlphaOfLength(5);
}
stash.stashValue("body", singletonMap(topLevelKey, singletonMap("a", "b")));
Map<String, Object> expected; Map<String, Object> expected;
Map<String, Object> map; Map<String, Object> map;
if (randomBoolean()) { if (randomBoolean()) {
expected = singletonMap("foo", singletonMap("a", "boooooh!")); expected = singletonMap(topLevelKey, singletonMap("a", "boooooh!"));
map = singletonMap("foo", singletonMap("a", "${body.$_path}oooooh!")); map = singletonMap(topLevelKey, singletonMap("a", "${body.$_path}oooooh!"));
} else { } else {
expected = singletonMap("foo", singletonMap("a", "b")); expected = singletonMap(topLevelKey, singletonMap("a", "b"));
map = singletonMap("foo", singletonMap("a", "$body.$_path")); map = singletonMap(topLevelKey, singletonMap("a", "$body.$_path"));
} }
Map<String, Object> actual = stash.replaceStashedValues(map); Map<String, Object> actual = stash.replaceStashedValues(map);
assertEquals(expected, actual); assertEquals(expected, actual);
assertThat(actual, not(sameInstance(map))); assertThat(actual, not(sameInstance(map)));
} }
} }