Skip to content

Commit 4ba9f65

Browse files
committed
[TEST] prevent yaml tests from using raw requests (#26044)
Raw requests are supported only by the java yaml test runner and were introduced to test docs snippets. Some yaml tests ended up using them (see #23497) which causes failures for other language clients. This commit migrates those yaml tests to Java tests that send requests through the Java low-level REST client, and also moves the ability to send raw requests to a special client that's only available when testing docs snippets. Closes #25694
1 parent cf44b82 commit 4ba9f65

File tree

22 files changed

+262
-194
lines changed

22 files changed

+262
-194
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.test.rest;
21+
22+
import org.elasticsearch.client.ResponseException;
23+
24+
import java.io.IOException;
25+
26+
import static org.hamcrest.CoreMatchers.containsString;
27+
28+
public class RequestsWithoutContentIT extends ESRestTestCase {
29+
30+
public void testIndexMissingBody() throws IOException {
31+
ResponseException responseException = expectThrows(ResponseException.class, () -> client().performRequest(
32+
randomBoolean() ? "POST" : "PUT", "/idx/type/123"));
33+
assertResponseException(responseException, "request body is required");
34+
}
35+
36+
public void testBulkMissingBody() throws IOException {
37+
ResponseException responseException = expectThrows(ResponseException.class, () -> client().performRequest(
38+
randomBoolean() ? "POST" : "PUT", "/_bulk"));
39+
assertResponseException(responseException, "request body is required");
40+
}
41+
42+
public void testPutSettingsMissingBody() throws IOException {
43+
ResponseException responseException = expectThrows(ResponseException.class, () -> client().performRequest(
44+
"PUT", "/_settings"));
45+
assertResponseException(responseException, "request body is required");
46+
}
47+
48+
public void testPutMappingsMissingBody() throws IOException {
49+
ResponseException responseException = expectThrows(ResponseException.class, () -> client().performRequest(
50+
randomBoolean() ? "POST" : "PUT", "/test_index/test_type/_mapping"));
51+
assertResponseException(responseException, "request body is required");
52+
}
53+
54+
public void testPutIndexTemplateMissingBody() throws IOException {
55+
ResponseException responseException = expectThrows(ResponseException.class, () -> client().performRequest(
56+
randomBoolean() ? "PUT" : "POST", "/_template/my_template"));
57+
assertResponseException(responseException, "request body is required");
58+
}
59+
60+
public void testMultiSearchMissingBody() throws IOException {
61+
ResponseException responseException = expectThrows(ResponseException.class, () -> client().performRequest(
62+
randomBoolean() ? "POST" : "GET", "/_msearch"));
63+
assertResponseException(responseException, "request body or source parameter is required");
64+
}
65+
66+
public void testPutPipelineMissingBody() throws IOException {
67+
ResponseException responseException = expectThrows(ResponseException.class, () -> client().performRequest(
68+
"PUT", "/_ingest/pipeline/my_pipeline"));
69+
assertResponseException(responseException, "request body or source parameter is required");
70+
}
71+
72+
public void testSimulatePipelineMissingBody() throws IOException {
73+
ResponseException responseException = expectThrows(ResponseException.class, () -> client().performRequest(
74+
randomBoolean() ? "POST" : "GET", "/_ingest/pipeline/my_pipeline/_simulate"));
75+
assertResponseException(responseException, "request body or source parameter is required");
76+
}
77+
78+
public void testPutScriptMissingBody() throws IOException {
79+
ResponseException responseException = expectThrows(ResponseException.class, () -> client().performRequest(
80+
randomBoolean() ? "POST" : "PUT", "/_scripts/lang"));
81+
assertResponseException(responseException, "request body is required");
82+
}
83+
84+
private static void assertResponseException(ResponseException responseException, String message) {
85+
assertEquals(400, responseException.getResponse().getStatusLine().getStatusCode());
86+
assertThat(responseException.getMessage(), containsString(message));
87+
}
88+
}

docs/src/test/java/org/elasticsearch/smoketest/DocsClientYamlTestSuiteIT.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,16 @@
2121

2222
import com.carrotsearch.randomizedtesting.annotations.Name;
2323
import com.carrotsearch.randomizedtesting.annotations.ParametersFactory;
24-
24+
import org.elasticsearch.Version;
25+
import org.elasticsearch.client.RestClient;
26+
import org.elasticsearch.client.http.HttpHost;
27+
import org.elasticsearch.test.rest.yaml.ClientYamlDocsTestClient;
2528
import org.elasticsearch.test.rest.yaml.ClientYamlTestCandidate;
29+
import org.elasticsearch.test.rest.yaml.ClientYamlTestClient;
2630
import org.elasticsearch.test.rest.yaml.ESClientYamlSuiteTestCase;
31+
import org.elasticsearch.test.rest.yaml.restspec.ClientYamlSuiteRestSpec;
2732

33+
import java.io.IOException;
2834
import java.util.List;
2935

3036
public class DocsClientYamlTestSuiteIT extends ESClientYamlSuiteTestCase {
@@ -52,5 +58,11 @@ protected void afterIfFailed(List<Throwable> errors) {
5258
protected boolean randomizeContentType() {
5359
return false;
5460
}
61+
62+
@Override
63+
protected ClientYamlTestClient initClientYamlTestClient(ClientYamlSuiteRestSpec restSpec, RestClient restClient,
64+
List<HttpHost> hosts, Version esVersion) throws IOException {
65+
return new ClientYamlDocsTestClient(restSpec, restClient, hosts, esVersion);
66+
}
5567
}
5668

modules/ingest-common/src/test/resources/rest-api-spec/test/ingest/20_crud.yml

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -203,16 +203,3 @@ teardown:
203203
catch: missing
204204
ingest.get_pipeline:
205205
id: "my_pipeline"
206-
207-
---
208-
"missing body":
209-
210-
- skip:
211-
version: " - 5.99.99"
212-
reason: NPE caused by missing body fixed in 6.0.0
213-
214-
- do:
215-
catch: /request body or source parameter is required/
216-
raw:
217-
method: PUT
218-
path: _ingest/pipeline/my_pipeline

modules/ingest-common/src/test/resources/rest-api-spec/test/ingest/90_simulate.yml

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -605,16 +605,3 @@ teardown:
605605
- length: { docs.0.processor_results.1: 2 }
606606
- match: { docs.0.processor_results.1.tag: "rename-1" }
607607
- match: { docs.0.processor_results.1.doc._source.new_status: 200 }
608-
609-
---
610-
"missing body":
611-
612-
- skip:
613-
version: " - 5.99.99"
614-
reason: NPE caused by missing body fixed in 6.0.0
615-
616-
- do:
617-
catch: /request body or source parameter is required/
618-
raw:
619-
method: POST
620-
path: _ingest/pipeline/my_pipeline/_simulate
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.script.mustache;
21+
22+
import org.elasticsearch.client.ResponseException;
23+
import org.elasticsearch.test.rest.ESRestTestCase;
24+
25+
import java.io.IOException;
26+
27+
import static org.hamcrest.CoreMatchers.containsString;
28+
29+
public class SearchTemplateWithoutContentIT extends ESRestTestCase {
30+
31+
public void testSearchTemplateMissingBody() throws IOException {
32+
ResponseException responseException = expectThrows(ResponseException.class, () -> client().performRequest(
33+
randomBoolean() ? "POST" : "GET", "/_search/template"));
34+
assertEquals(400, responseException.getResponse().getStatusLine().getStatusCode());
35+
assertThat(responseException.getMessage(), containsString("request body or source parameter is required"));
36+
}
37+
38+
public void testMultiSearchTemplateMissingBody() throws IOException {
39+
ResponseException responseException = expectThrows(ResponseException.class, () -> client().performRequest(
40+
randomBoolean() ? "POST" : "GET", "/_msearch/template"));
41+
assertEquals(400, responseException.getResponse().getStatusLine().getStatusCode());
42+
assertThat(responseException.getMessage(), containsString("request body or source parameter is required"));
43+
}
44+
}

modules/lang-mustache/src/test/resources/rest-api-spec/test/lang_mustache/10_basic.yml

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,3 @@
1111
nodes.info: {}
1212

1313
- match: { nodes.$master.modules.0.name: lang-mustache }
14-
15-
---
16-
"missing body":
17-
18-
- skip:
19-
version: " - 5.99.99"
20-
reason: NPE caused by missing body fixed in 6.0.0
21-
22-
- do:
23-
catch: /request body is required/
24-
raw:
25-
method: POST
26-
path: _search/template/1

modules/lang-mustache/src/test/resources/rest-api-spec/test/lang_mustache/30_search_template.yml

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -122,16 +122,3 @@
122122
- match: { hits.total: 1 }
123123
- length: { hits.hits: 1 }
124124
- length: { profile: 1 }
125-
126-
---
127-
"missing body":
128-
129-
- skip:
130-
version: " - 5.99.99"
131-
reason: NPE caused by missing body fixed in 6.0.0
132-
133-
- do:
134-
catch: /request body or source parameter is required/
135-
raw:
136-
method: POST
137-
path: _search/template

modules/lang-mustache/src/test/resources/rest-api-spec/test/lang_mustache/50_multi_search_template.yml

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -156,16 +156,3 @@ setup:
156156
- match: { responses.0.hits.total: 2 }
157157
- match: { responses.1.hits.total: 1 }
158158
- match: { responses.2.hits.total: 1 }
159-
160-
---
161-
"missing body":
162-
163-
- skip:
164-
version: " - 5.99.99"
165-
reason: NPE caused by missing body fixed in 6.0.0
166-
167-
- do:
168-
catch: /request body or source parameter is required/
169-
raw:
170-
method: POST
171-
path: _msearch/template
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.index.reindex;
21+
22+
import org.elasticsearch.client.ResponseException;
23+
import org.elasticsearch.test.rest.ESRestTestCase;
24+
25+
import java.io.IOException;
26+
27+
import static org.hamcrest.CoreMatchers.containsString;
28+
29+
public class ReindexWithoutContentIT extends ESRestTestCase {
30+
31+
public void testReindexMissingBody() throws IOException {
32+
ResponseException responseException = expectThrows(ResponseException.class, () -> client().performRequest(
33+
"POST", "/_reindex"));
34+
assertEquals(400, responseException.getResponse().getStatusLine().getStatusCode());
35+
assertThat(responseException.getMessage(), containsString("request body is required"));
36+
}
37+
}

modules/reindex/src/test/resources/rest-api-spec/test/reindex/10_basic.yml

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -299,16 +299,3 @@
299299
index: source
300300
metric: search
301301
- match: {indices.source.total.search.open_contexts: 0}
302-
303-
---
304-
"missing body":
305-
306-
- skip:
307-
version: " - 5.99.99"
308-
reason: NPE caused by missing body fixed in 6.0.0
309-
310-
- do:
311-
catch: /request body is required/
312-
raw:
313-
method: POST
314-
path: _reindex

0 commit comments

Comments
 (0)