Skip to content

Commit 0ae3187

Browse files
committed
[TEST] Enforce skip headers when needed (#34735)
The java yaml test runner supports sending request headers, yet not all clients support headers. This commit makes sure that we enforce adding a skip section with feature "headers" whenever headers are used in a do section as part of a test. That decreases the chance for new tests to break client builds due to the missing skip section. Closes #34650
1 parent bcbb3df commit 0ae3187

File tree

45 files changed

+156
-58
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+156
-58
lines changed

qa/rolling-upgrade/src/test/resources/rest-api-spec/test/upgraded_cluster/10_basic.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@
107107

108108
---
109109
"Find a task result record from the old cluster":
110+
- skip:
111+
features: headers
112+
110113
- do:
111114
search:
112115
index: .tasks

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ public static ClientYamlTestSuite parse(String api, String suiteName, XContentPa
8484

8585
SetupSection setupSection = SetupSection.parseIfNext(parser);
8686
TeardownSection teardownSection = TeardownSection.parseIfNext(parser);
87-
8887
Set<ClientYamlTestSection> testSections = new TreeSet<>();
8988
while(true) {
9089
//the "---" section separator is not understood by the yaml parser. null is returned, same as when the parser is closed
@@ -177,6 +176,14 @@ private static Stream<String> validateExecutableSections(List<ExecutableSection>
177176
"without a corresponding [\"skip\": \"features\": \"contains\"] so runners that do not support the " +
178177
"[contains] assertion can skip the test at line [" + section.getLocation().lineNumber + "]"));
179178

179+
errors = Stream.concat(errors, sections.stream().filter(section -> section instanceof DoSection)
180+
.map(section -> (DoSection) section)
181+
.filter(section -> false == section.getApiCallSection().getHeaders().isEmpty())
182+
.filter(section -> false == hasSkipFeature("headers", testSection, setupSection, teardownSection))
183+
.map(section -> "attempted to add a [do] with a [headers] section without a corresponding "
184+
+ "[\"skip\": \"features\": \"headers\"] so runners that do not support the [headers] section can skip the test at " +
185+
"line [" + section.getLocation().lineNumber + "]"));
186+
180187
return errors;
181188
}
182189

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

Lines changed: 39 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.util.Map;
3232

3333
import static java.util.Collections.singletonList;
34+
import static java.util.Collections.singletonMap;
3435
import static org.hamcrest.Matchers.containsString;
3536
import static org.hamcrest.Matchers.equalTo;
3637
import static org.hamcrest.Matchers.instanceOf;
@@ -403,11 +404,24 @@ public void testAddingDoWithWarningWithoutSkipWarnings() {
403404
DoSection doSection = new DoSection(new XContentLocation(lineNumber, 0));
404405
doSection.setExpectedWarningHeaders(singletonList("foo"));
405406
doSection.setApiCallSection(new ApiCallSection("test"));
406-
ClientYamlTestSuite testSuite = createTestSuite(doSection);
407+
ClientYamlTestSuite testSuite = createTestSuite(SkipSection.EMPTY, doSection);
407408
Exception e = expectThrows(IllegalArgumentException.class, testSuite::validate);
408-
assertEquals("api/name:\nattempted to add a [do] with a [warnings] section without a corresponding " +
409+
assertThat(e.getMessage(), containsString("api/name:\nattempted to add a [do] with a [warnings] section without a corresponding " +
409410
"[\"skip\": \"features\": \"warnings\"] so runners that do not support the [warnings] section can skip the test " +
410-
"at line [" + lineNumber + "]", e.getMessage());
411+
"at line [" + lineNumber + "]"));
412+
}
413+
414+
public void testAddingDoWithHeaderWithoutSkipHeaders() {
415+
int lineNumber = between(1, 10000);
416+
DoSection doSection = new DoSection(new XContentLocation(lineNumber, 0));
417+
ApiCallSection apiCallSection = new ApiCallSection("test");
418+
apiCallSection.addHeaders(Collections.singletonMap("header", "value"));
419+
doSection.setApiCallSection(apiCallSection);
420+
ClientYamlTestSuite testSuite = createTestSuite(SkipSection.EMPTY, doSection);
421+
Exception e = expectThrows(IllegalArgumentException.class, testSuite::validate);
422+
assertThat(e.getMessage(), containsString("api/name:\nattempted to add a [do] with a [headers] section without a corresponding " +
423+
"[\"skip\": \"features\": \"headers\"] so runners that do not support the [headers] section can skip the test at line ["
424+
+ lineNumber + "]"));
411425
}
412426

413427
public void testAddingDoWithNodeSelectorWithoutSkipNodeSelector() {
@@ -416,11 +430,11 @@ public void testAddingDoWithNodeSelectorWithoutSkipNodeSelector() {
416430
ApiCallSection apiCall = new ApiCallSection("test");
417431
apiCall.setNodeSelector(NodeSelector.SKIP_DEDICATED_MASTERS);
418432
doSection.setApiCallSection(apiCall);
419-
ClientYamlTestSuite testSuite = createTestSuite(doSection);
433+
ClientYamlTestSuite testSuite = createTestSuite(SkipSection.EMPTY, doSection);
420434
Exception e = expectThrows(IllegalArgumentException.class, testSuite::validate);
421-
assertEquals("api/name:\nattempted to add a [do] with a [node_selector] section without a corresponding"
422-
+ " [\"skip\": \"features\": \"node_selector\"] so runners that do not support the [node_selector] section can skip the test at"
423-
+ " line [" + lineNumber + "]", e.getMessage());
435+
assertThat(e.getMessage(), containsString("api/name:\nattempted to add a [do] with a [node_selector] section without a " +
436+
"corresponding [\"skip\": \"features\": \"node_selector\"] so runners that do not support the [node_selector] section can " +
437+
"skip the test at line [" + lineNumber + "]"));
424438
}
425439

426440
public void testAddingContainsWithoutSkipContains() {
@@ -429,11 +443,11 @@ public void testAddingContainsWithoutSkipContains() {
429443
new XContentLocation(lineNumber, 0),
430444
randomAlphaOfLength(randomIntBetween(3, 30)),
431445
randomDouble());
432-
ClientYamlTestSuite testSuite = createTestSuite(containsAssertion);
446+
ClientYamlTestSuite testSuite = createTestSuite(SkipSection.EMPTY, containsAssertion);
433447
Exception e = expectThrows(IllegalArgumentException.class, testSuite::validate);
434-
assertEquals("api/name:\nattempted to add a [contains] assertion without a corresponding " +
448+
assertThat(e.getMessage(), containsString("api/name:\nattempted to add a [contains] assertion without a corresponding " +
435449
"[\"skip\": \"features\": \"contains\"] so runners that do not support the [contains] assertion " +
436-
"can skip the test at line [" + lineNumber + "]", e.getMessage());
450+
"can skip the test at line [" + lineNumber + "]"));
437451
}
438452

439453
public void testMultipleValidationErrors() {
@@ -477,44 +491,13 @@ public void testMultipleValidationErrors() {
477491
e.getMessage());
478492
}
479493

480-
private static ClientYamlTestSuite createTestSuite(ExecutableSection executableSection) {
481-
final SetupSection setupSection;
482-
final TeardownSection teardownSection;
483-
final ClientYamlTestSection clientYamlTestSection;
484-
switch(randomIntBetween(0, 2)) {
485-
case 0:
486-
setupSection = new SetupSection(SkipSection.EMPTY, Collections.singletonList(executableSection));
487-
teardownSection = TeardownSection.EMPTY;
488-
clientYamlTestSection = new ClientYamlTestSection(new XContentLocation(0, 0), "test",
489-
SkipSection.EMPTY, Collections.emptyList());
490-
break;
491-
case 1:
492-
setupSection = SetupSection.EMPTY;
493-
teardownSection = new TeardownSection(SkipSection.EMPTY, Collections.singletonList(executableSection));
494-
clientYamlTestSection = new ClientYamlTestSection(new XContentLocation(0, 0), "test",
495-
SkipSection.EMPTY, Collections.emptyList());
496-
break;
497-
case 2:
498-
setupSection = SetupSection.EMPTY;
499-
teardownSection = TeardownSection.EMPTY;
500-
clientYamlTestSection = new ClientYamlTestSection(new XContentLocation(0, 0), "test",
501-
SkipSection.EMPTY, Collections.singletonList(executableSection));
502-
break;
503-
default:
504-
throw new UnsupportedOperationException();
505-
}
506-
507-
return new ClientYamlTestSuite("api", "name", setupSection, teardownSection,
508-
Collections.singletonList(clientYamlTestSection));
509-
}
510-
511494
public void testAddingDoWithWarningWithSkip() {
512495
int lineNumber = between(1, 10000);
513496
DoSection doSection = new DoSection(new XContentLocation(lineNumber, 0));
514497
doSection.setExpectedWarningHeaders(singletonList("foo"));
515498
doSection.setApiCallSection(new ApiCallSection("test"));
516499
SkipSection skipSection = new SkipSection(null, singletonList("warnings"), null);
517-
createTestSuiteAndValidate(skipSection, doSection);
500+
createTestSuite(skipSection, doSection).validate();
518501
}
519502

520503
public void testAddingDoWithNodeSelectorWithSkip() {
@@ -524,7 +507,17 @@ public void testAddingDoWithNodeSelectorWithSkip() {
524507
ApiCallSection apiCall = new ApiCallSection("test");
525508
apiCall.setNodeSelector(NodeSelector.SKIP_DEDICATED_MASTERS);
526509
doSection.setApiCallSection(apiCall);
527-
createTestSuiteAndValidate(skipSection, doSection);
510+
createTestSuite(skipSection, doSection).validate();
511+
}
512+
513+
public void testAddingDoWithHeadersWithSkip() {
514+
int lineNumber = between(1, 10000);
515+
SkipSection skipSection = new SkipSection(null, singletonList("headers"), null);
516+
DoSection doSection = new DoSection(new XContentLocation(lineNumber, 0));
517+
ApiCallSection apiCallSection = new ApiCallSection("test");
518+
apiCallSection.addHeaders(singletonMap("foo", "bar"));
519+
doSection.setApiCallSection(apiCallSection);
520+
createTestSuite(skipSection, doSection).validate();
528521
}
529522

530523
public void testAddingContainsWithSkip() {
@@ -534,10 +527,10 @@ public void testAddingContainsWithSkip() {
534527
new XContentLocation(lineNumber, 0),
535528
randomAlphaOfLength(randomIntBetween(3, 30)),
536529
randomDouble());
537-
createTestSuiteAndValidate(skipSection, containsAssertion);
530+
createTestSuite(skipSection, containsAssertion).validate();
538531
}
539532

540-
private static void createTestSuiteAndValidate(SkipSection skipSection, ExecutableSection executableSection) {
533+
private static ClientYamlTestSuite createTestSuite(SkipSection skipSection, ExecutableSection executableSection) {
541534
final SetupSection setupSection;
542535
final TeardownSection teardownSection;
543536
final ClientYamlTestSection clientYamlTestSection;
@@ -571,13 +564,11 @@ private static void createTestSuiteAndValidate(SkipSection skipSection, Executab
571564
teardownSection = new TeardownSection(skipSection, Collections.singletonList(executableSection));
572565
clientYamlTestSection = new ClientYamlTestSection(new XContentLocation(0, 0), "test",
573566
SkipSection.EMPTY, randomBoolean() ? Collections.emptyList() : Collections.singletonList(executableSection));
574-
575567
break;
576568
default:
577569
throw new UnsupportedOperationException();
578570
}
579-
ClientYamlTestSuite clientYamlTestSuite = new ClientYamlTestSuite("api", "name", setupSection, teardownSection,
571+
return new ClientYamlTestSuite("api", "name", setupSection, teardownSection,
580572
Collections.singletonList(clientYamlTestSection));
581-
clientYamlTestSuite.validate();
582573
}
583574
}

x-pack/plugin/src/test/resources/rest-api-spec/test/ml/custom_all_field.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
setup:
2+
- skip:
3+
features: headers
24
- do:
35
headers:
46
Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser

x-pack/plugin/src/test/resources/rest-api-spec/test/ml/datafeeds_crud.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
setup:
2+
- skip:
3+
features: headers
24
- do:
35
headers:
46
Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser

x-pack/plugin/src/test/resources/rest-api-spec/test/ml/delete_forecast.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
setup:
2+
- skip:
3+
features: headers
24
- do:
35
headers:
46
Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser

x-pack/plugin/src/test/resources/rest-api-spec/test/ml/delete_job_force.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
setup:
2+
- skip:
3+
features: headers
24
- do:
35
headers:
46
Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser

x-pack/plugin/src/test/resources/rest-api-spec/test/ml/delete_model_snapshot.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
setup:
2+
- skip:
3+
features: headers
24
- do:
35
headers:
46
Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser

x-pack/plugin/src/test/resources/rest-api-spec/test/ml/filter_crud.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---
22
setup:
3-
3+
- skip:
4+
features: headers
45
- do:
56
headers:
67
Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser

x-pack/plugin/src/test/resources/rest-api-spec/test/ml/find_file_structure.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
setup:
2+
- skip:
3+
features: headers
14
---
25
"Test NDJSON file structure analysis without overrides":
36
- do:

0 commit comments

Comments
 (0)