Skip to content

Commit c899aa6

Browse files
committed
QA: Node name selector for yml tests
In 6.x we need to be able to select which node receives a request by node name. This implements that. Relates to elastic#30523
1 parent a365435 commit c899aa6

File tree

3 files changed

+90
-10
lines changed

3 files changed

+90
-10
lines changed

rest-api-spec/src/main/resources/rest-api-spec/test/README.asciidoc

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,7 @@ header. The warnings must match exactly. Using it looks like this:
198198
....
199199
200200
If the arguments to `do` include `node_selector` then the request is only
201-
sent to nodes that match the `node_selector`. Currently only the `version`
202-
selector is supported and it has the same logic as the `version` field in
203-
`skip`. It looks like this:
201+
sent to nodes that match the `node_selector`. It looks like this:
204202
205203
....
206204
"test id":
@@ -216,6 +214,14 @@ selector is supported and it has the same logic as the `version` field in
216214
body: { foo: bar }
217215
....
218216
217+
If you list multiple selectors then the request will only go to nodes that
218+
match all of those selectors. The following selectors are supported:
219+
* `version`: Only nodes who's version is within the range will receive the
220+
request. The syntax for the pattern is the same as when `version` is within
221+
`skip`.
222+
* `name`: Only nodes who's name matches the name exactly will receive the
223+
request.
224+
219225
=== `set`
220226
221227
For some tests, it is necessary to extract a value from the previous `response`, in

test/framework/src/main/java/org/elasticsearch/test/rest/yaml/section/DoSection.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,27 @@ private String formatStatusCodeMessage(ClientYamlTestResponse restTestResponse,
370370

371371
private static NodeSelector buildNodeSelector(XContentLocation location, String name, String value) {
372372
switch (name) {
373+
case "name":
374+
return new NodeSelector() {
375+
@Override
376+
public void select(Iterable<Node> nodes) {
377+
for (Iterator<Node> itr = nodes.iterator(); itr.hasNext();) {
378+
Node node = itr.next();
379+
if (node.getName() == null) {
380+
throw new IllegalStateException("expected [name] metadata to be set but got "
381+
+ node);
382+
}
383+
if (false == value.equals(node.getName())) {
384+
itr.remove();
385+
}
386+
}
387+
}
388+
389+
@Override
390+
public String toString() {
391+
return "name is [" + value + "]";
392+
}
393+
};
373394
case "version":
374395
Version[] range = SkipSection.parseVersionRange(value);
375396
return new NodeSelector() {

test/framework/src/test/java/org/elasticsearch/test/rest/yaml/section/DoSectionTests.java

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ public void testParseDoSectionExpectedWarnings() throws Exception {
511511
"just one entry this time")));
512512
}
513513

514-
public void testNodeSelector() throws IOException {
514+
public void testNodeSelectorByVersion() throws IOException {
515515
parser = createParser(YamlXContent.yamlXContent,
516516
"node_selector:\n" +
517517
" version: 5.2.0-6.0.0\n" +
@@ -521,10 +521,10 @@ public void testNodeSelector() throws IOException {
521521

522522
DoSection doSection = DoSection.parse(parser);
523523
assertNotSame(NodeSelector.ANY, doSection.getApiCallSection().getNodeSelector());
524-
Node v170 = nodeWithVersion("1.7.0");
525-
Node v521 = nodeWithVersion("5.2.1");
526-
Node v550 = nodeWithVersion("5.5.0");
527-
Node v612 = nodeWithVersion("6.1.2");
524+
Node v170 = new Node(new HttpHost("dummy"), null, null, "1.7.0", null);
525+
Node v521 = new Node(new HttpHost("dummy"), null, null, "5.2.1", null);
526+
Node v550 = new Node(new HttpHost("dummy"), null, null, "5.5.0", null);
527+
Node v612 = new Node(new HttpHost("dummy"), null, null, "6.1.2", null);
528528
List<Node> nodes = new ArrayList<>();
529529
nodes.add(v170);
530530
nodes.add(v521);
@@ -541,8 +541,61 @@ public void testNodeSelector() throws IOException {
541541
emptyList(), emptyMap(), doSection.getApiCallSection().getNodeSelector());
542542
}
543543

544-
private Node nodeWithVersion(String version) {
545-
return new Node(new HttpHost("dummy"), null, null, version, null);
544+
public void testNodeSelectorByName() throws IOException {
545+
parser = createParser(YamlXContent.yamlXContent,
546+
"node_selector:\n" +
547+
" name: cat\n" +
548+
"indices.get_field_mapping:\n" +
549+
" index: test_index"
550+
);
551+
552+
DoSection doSection = DoSection.parse(parser);
553+
assertNotSame(NodeSelector.ANY, doSection.getApiCallSection().getNodeSelector());
554+
Node cat = new Node(new HttpHost("dummy"), null, "cat", null, null);
555+
Node dog = new Node(new HttpHost("dummy"), null, "dog", null, null);
556+
Node pig = new Node(new HttpHost("dummy"), null, "pig", null, null);
557+
List<Node> nodes = new ArrayList<>();
558+
nodes.add(cat);
559+
nodes.add(dog);
560+
nodes.add(pig);
561+
doSection.getApiCallSection().getNodeSelector().select(nodes);
562+
assertEquals(Arrays.asList(cat), nodes);
563+
ClientYamlTestExecutionContext context = mock(ClientYamlTestExecutionContext.class);
564+
ClientYamlTestResponse mockResponse = mock(ClientYamlTestResponse.class);
565+
when(context.callApi("indices.get_field_mapping", singletonMap("index", "test_index"),
566+
emptyList(), emptyMap(), doSection.getApiCallSection().getNodeSelector())).thenReturn(mockResponse);
567+
doSection.execute(context);
568+
verify(context).callApi("indices.get_field_mapping", singletonMap("index", "test_index"),
569+
emptyList(), emptyMap(), doSection.getApiCallSection().getNodeSelector());
570+
}
571+
572+
public void testNodeSelectorByTwoThings() throws IOException {
573+
parser = createParser(YamlXContent.yamlXContent,
574+
"node_selector:\n" +
575+
" name: cat\n" +
576+
" version: 5.2.0-6.0.0\n" +
577+
"indices.get_field_mapping:\n" +
578+
" index: test_index"
579+
);
580+
581+
DoSection doSection = DoSection.parse(parser);
582+
assertNotSame(NodeSelector.ANY, doSection.getApiCallSection().getNodeSelector());
583+
Node cat = new Node(new HttpHost("dummy"), null, "cat", "5.2.1", null);
584+
Node badName = new Node(new HttpHost("dummy"), null, "dog", "5.2.1", null);
585+
Node badVersion = new Node(new HttpHost("dummy"), null, "cat", "6.1.2", null);
586+
List<Node> nodes = new ArrayList<>();
587+
nodes.add(cat);
588+
nodes.add(badName);
589+
nodes.add(badVersion);
590+
doSection.getApiCallSection().getNodeSelector().select(nodes);
591+
assertEquals(Arrays.asList(cat), nodes);
592+
ClientYamlTestExecutionContext context = mock(ClientYamlTestExecutionContext.class);
593+
ClientYamlTestResponse mockResponse = mock(ClientYamlTestResponse.class);
594+
when(context.callApi("indices.get_field_mapping", singletonMap("index", "test_index"),
595+
emptyList(), emptyMap(), doSection.getApiCallSection().getNodeSelector())).thenReturn(mockResponse);
596+
doSection.execute(context);
597+
verify(context).callApi("indices.get_field_mapping", singletonMap("index", "test_index"),
598+
emptyList(), emptyMap(), doSection.getApiCallSection().getNodeSelector());
546599
}
547600

548601
private void assertJsonEquals(Map<String, Object> actual, String expected) throws IOException {

0 commit comments

Comments
 (0)