Skip to content

Commit

Permalink
WIP: add lang version to ITs
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-spies committed Apr 4, 2024
1 parent d6582cf commit 3f13c4a
Show file tree
Hide file tree
Showing 15 changed files with 123 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.elasticsearch.xpack.core.esql.action.EsqlQueryRequest;
import org.elasticsearch.xpack.core.esql.action.EsqlQueryRequestBuilder;
import org.elasticsearch.xpack.core.esql.action.EsqlQueryResponse;
import org.elasticsearch.xpack.esql.version.EsqlVersion;
import org.junit.Before;

import java.util.ArrayList;
Expand Down Expand Up @@ -127,7 +128,12 @@ public void testAccessAfterClose() {
}
}

protected EsqlQueryResponse run(EsqlQueryRequestBuilder<? extends EsqlQueryRequest, ? extends EsqlQueryResponse> request) {
protected EsqlQueryResponse run(
EsqlQueryRequestBuilder<? extends EsqlQueryRequest, ? extends EsqlQueryResponse> requestWithoutVersion
) {
EsqlQueryRequestBuilder<? extends EsqlQueryRequest, ? extends EsqlQueryResponse> request = requestWithoutVersion.esqlVersion(
EsqlVersion.SNAPSHOT.toString()
);
try {
// The variants here ensure API usage patterns
if (randomBoolean()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ private Response runAsync(String user, String command) throws IOException {
}
XContentBuilder json = JsonXContent.contentBuilder();
json.startObject();
json.field("version", "snapshot");
json.field("query", command);
addRandomPragmas(json);
json.field("wait_for_completion_timeout", timeValueNanos(randomIntBetween(1, 1000)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ protected Response runESQLCommand(String user, String command) throws IOExceptio
}
XContentBuilder json = JsonXContent.contentBuilder();
json.startObject();
json.field("version", "snapshot");
json.field("query", command);
addRandomPragmas(json);
json.endObject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.elasticsearch.test.rest.ESRestTestCase;
import org.elasticsearch.test.rest.TestFeatureService;
import org.elasticsearch.xpack.esql.qa.rest.EsqlSpecTestCase;
import org.elasticsearch.xpack.esql.qa.rest.RestEsqlTestCase;
import org.elasticsearch.xpack.ql.CsvSpecReader;
import org.elasticsearch.xpack.ql.CsvSpecReader.CsvTestCase;
import org.elasticsearch.xpack.ql.SpecReader;
Expand All @@ -37,6 +38,7 @@
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -231,4 +233,10 @@ static boolean hasIndexMetadata(String query) {
}
return false;
}

@Override
protected Map<String, Object> runEsql(RestEsqlTestCase.RequestObjectBuilder requestObject, List<String> expectedWarnings)
throws IOException {
return super.runEsql(requestObject.esqlVersion("snapshot"), expectedWarnings);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ void indexDocs(RestClient client, String index, List<Doc> docs) throws IOExcepti
}

private Map<String, Object> run(String query) throws IOException {
Map<String, Object> resp = runEsql(new RestEsqlTestCase.RequestObjectBuilder().query(query).build());
Map<String, Object> resp = runEsql(new RestEsqlTestCase.RequestObjectBuilder().esqlVersion("snapshot").query(query).build());
logger.info("--> query {} response {}", query, resp);
return resp;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@

import org.elasticsearch.test.cluster.ElasticsearchCluster;
import org.elasticsearch.xpack.esql.qa.rest.EsqlSpecTestCase;
import org.elasticsearch.xpack.esql.qa.rest.RestEsqlTestCase;
import org.elasticsearch.xpack.ql.CsvSpecReader.CsvTestCase;
import org.junit.ClassRule;

import java.io.IOException;
import java.util.List;
import java.util.Map;

public class EsqlSpecIT extends EsqlSpecTestCase {
@ClassRule
public static ElasticsearchCluster cluster = Clusters.testCluster();
Expand All @@ -24,4 +29,10 @@ protected String getTestRestCluster() {
public EsqlSpecIT(String fileName, String groupName, String testName, Integer lineNumber, CsvTestCase testCase, Mode mode) {
super(fileName, groupName, testName, lineNumber, testCase, mode);
}

@Override
protected Map<String, Object> runEsql(RestEsqlTestCase.RequestObjectBuilder requestObject, List<String> expectedWarnings)
throws IOException {
return super.runEsql(requestObject.esqlVersion("snapshot"), expectedWarnings);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@
import org.elasticsearch.test.TestClustersThreadFilter;
import org.elasticsearch.test.cluster.ElasticsearchCluster;
import org.elasticsearch.xpack.esql.qa.rest.FieldExtractorTestCase;
import org.elasticsearch.xpack.esql.qa.rest.RestEsqlTestCase;
import org.junit.ClassRule;

import java.io.IOException;
import java.util.Map;

@ThreadLeakFilters(filters = TestClustersThreadFilter.class)
public class FieldExtractorIT extends FieldExtractorTestCase {
@ClassRule
Expand All @@ -23,4 +27,9 @@ public class FieldExtractorIT extends FieldExtractorTestCase {
protected String getTestRestCluster() {
return cluster.getHttpAddresses();
}

@Override
protected Map<String, Object> runEsqlSync(RestEsqlTestCase.RequestObjectBuilder requestObject) throws IOException {
return super.runEsqlSync(requestObject.esqlVersion("snapshot"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,14 @@
import org.elasticsearch.test.TestClustersThreadFilter;
import org.elasticsearch.test.cluster.ElasticsearchCluster;
import org.elasticsearch.xpack.esql.qa.rest.EsqlSpecTestCase;
import org.elasticsearch.xpack.esql.qa.rest.RestEsqlTestCase;
import org.elasticsearch.xpack.ql.CsvSpecReader.CsvTestCase;
import org.junit.ClassRule;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@ThreadLeakFilters(filters = TestClustersThreadFilter.class)
public class EsqlSpecIT extends EsqlSpecTestCase {
@ClassRule
Expand All @@ -28,4 +33,10 @@ protected String getTestRestCluster() {
public EsqlSpecIT(String fileName, String groupName, String testName, Integer lineNumber, CsvTestCase testCase, Mode mode) {
super(fileName, groupName, testName, lineNumber, testCase, mode);
}

@Override
protected Map<String, Object> runEsql(RestEsqlTestCase.RequestObjectBuilder requestObject, List<String> expectedWarnings)
throws IOException {
return super.runEsql(requestObject.esqlVersion("snapshot"), expectedWarnings);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@
import org.elasticsearch.test.TestClustersThreadFilter;
import org.elasticsearch.test.cluster.ElasticsearchCluster;
import org.elasticsearch.xpack.esql.qa.rest.FieldExtractorTestCase;
import org.elasticsearch.xpack.esql.qa.rest.RestEsqlTestCase;
import org.junit.ClassRule;

import java.io.IOException;
import java.util.Map;

@ThreadLeakFilters(filters = TestClustersThreadFilter.class)
public class FieldExtractorIT extends FieldExtractorTestCase {
@ClassRule
Expand All @@ -23,4 +27,9 @@ public class FieldExtractorIT extends FieldExtractorTestCase {
protected String getTestRestCluster() {
return cluster.getHttpAddresses();
}

@Override
protected Map<String, Object> runEsqlSync(RestEsqlTestCase.RequestObjectBuilder requestObject) throws IOException {
return super.runEsqlSync(requestObject.esqlVersion("snapshot"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public void testInvalidPragma() throws IOException {
}
RequestObjectBuilder builder = new RequestObjectBuilder().query("from test-index | limit 1 | keep f");
builder.pragmas(Settings.builder().put("data_partitioning", "invalid-option").build());
ResponseException re = expectThrows(ResponseException.class, () -> runEsqlSync(builder));
ResponseException re = expectThrows(ResponseException.class, () -> runEsql(builder, List.of(), Mode.SYNC));
assertThat(EntityUtils.toString(re.getResponse().getEntity()), containsString("No enum constant"));

assertThat(deleteIndex("test-index").isAcknowledged(), is(true)); // clean up
Expand All @@ -101,7 +101,7 @@ public void testPragmaNotAllowed() throws IOException {
assumeFalse("pragma only disabled on release builds", Build.current().isSnapshot());
RequestObjectBuilder builder = new RequestObjectBuilder().query("row a = 1, b = 2");
builder.pragmas(Settings.builder().put("data_partitioning", "shard").build());
ResponseException re = expectThrows(ResponseException.class, () -> runEsqlSync(builder));
ResponseException re = expectThrows(ResponseException.class, () -> runEsql(builder, List.of(), Mode.SYNC));
assertThat(EntityUtils.toString(re.getResponse().getEntity()), containsString("[pragma] only allowed in snapshot builds"));
}

Expand Down Expand Up @@ -197,10 +197,26 @@ public void testIncompatibleMappingsErrors() throws IOException {
}

private void assertException(String query, String... errorMessages) throws IOException {
ResponseException re = expectThrows(ResponseException.class, () -> runEsqlSync(new RequestObjectBuilder().query(query)));
ResponseException re = expectThrows(
ResponseException.class,
() -> runEsql(new RequestObjectBuilder().query(query), List.of(), Mode.SYNC)
);
assertThat(re.getResponse().getStatusLine().getStatusCode(), equalTo(400));
for (var error : errorMessages) {
assertThat(re.getMessage(), containsString(error));
}
}

@Override
protected Map<String, Object> runEsql(RequestObjectBuilder requestObject, List<String> expectedWarnings, Mode mode) throws IOException {
if (requestObject.esqlVersion() == null) {
requestObject.esqlVersion("snapshot");
}
return super.runEsql(requestObject, expectedWarnings, mode);
}

@Override
protected RequestObjectBuilder builder() throws IOException {
return super.builder().esqlVersion("snapshot");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void testTimeSeriesQuerying() throws IOException {

RestEsqlTestCase.RequestObjectBuilder builder = new RestEsqlTestCase.RequestObjectBuilder().query(
"FROM k8s | KEEP k8s.pod.name, @timestamp"
);
).esqlVersion("snapshot");
builder.pragmas(Settings.builder().put("time_series", true).build());
Map<String, Object> result = runEsqlSync(builder);
@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ protected final void doTest() throws Throwable {
assertResults(expectedColumnsWithValues, actualColumns, actualValues, testCase.ignoreOrder, logger);
}

private Map<String, Object> runEsql(RequestObjectBuilder requestObject, List<String> expectedWarnings) throws IOException {
protected Map<String, Object> runEsql(RequestObjectBuilder requestObject, List<String> expectedWarnings) throws IOException {
if (mode == Mode.ASYNC) {
assert supportsAsync();
return RestEsqlTestCase.runEsqlAsync(requestObject, expectedWarnings);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
import static org.elasticsearch.test.MapMatcher.assertMap;
import static org.elasticsearch.test.MapMatcher.matchesMap;
import static org.elasticsearch.xpack.esql.qa.rest.RestEsqlTestCase.entityToMap;
import static org.elasticsearch.xpack.esql.qa.rest.RestEsqlTestCase.runEsqlSync;
import static org.hamcrest.Matchers.closeTo;
import static org.hamcrest.Matchers.containsString;

Expand Down Expand Up @@ -1189,7 +1188,7 @@ private static String randomPrerelease() {

private record StoreAndDocValues(Boolean store, Boolean docValues) {}

private static class Test {
private class Test {
private final String type;
private final Map<String, Test> subFields = new TreeMap<>();

Expand Down Expand Up @@ -1417,6 +1416,10 @@ private Map<String, Object> fetchAll() throws IOException {
}
}

protected Map<String, Object> runEsqlSync(RestEsqlTestCase.RequestObjectBuilder requestObject) throws IOException {
return RestEsqlTestCase.runEsqlSync(requestObject);
}

private static Map<String, Object> columnInfo(String name, String type) {
return Map.of("name", name, "type", type);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public void wipeTestData() throws IOException {
public void testNonExistentEnrichPolicy() throws IOException {
ResponseException re = expectThrows(
ResponseException.class,
() -> RestEsqlTestCase.runEsqlSync(new RestEsqlTestCase.RequestObjectBuilder().query("from test | enrich countris"), List.of())
() -> runEsql(new RestEsqlTestCase.RequestObjectBuilder().query("from test | enrich countris"), Mode.SYNC)
);
assertThat(
EntityUtils.toString(re.getResponse().getEntity()),
Expand All @@ -155,9 +155,7 @@ public void testNonExistentEnrichPolicy() throws IOException {
public void testNonExistentEnrichPolicy_KeepField() throws IOException {
ResponseException re = expectThrows(
ResponseException.class,
() -> RestEsqlTestCase.runEsqlSync(
new RestEsqlTestCase.RequestObjectBuilder().query("from test | enrich countris | keep number")
)
() -> runEsql(new RestEsqlTestCase.RequestObjectBuilder().query("from test | enrich countris | keep number"), Mode.SYNC)
);
assertThat(
EntityUtils.toString(re.getResponse().getEntity()),
Expand All @@ -178,7 +176,6 @@ public void testMatchField_ImplicitFieldsList() throws IOException {
public void testMatchField_ImplicitFieldsList_WithStats() throws IOException {
Map<String, Object> result = runEsql(
new RestEsqlTestCase.RequestObjectBuilder().query("from test | enrich countries | stats s = sum(number) by country_name")

);
var columns = List.of(Map.of("name", "s", "type", "long"), Map.of("name", "country_name", "type", "keyword"));
var values = List.of(List.of(2000, "United States of America"), List.of(5000, "China"));
Expand All @@ -187,10 +184,23 @@ public void testMatchField_ImplicitFieldsList_WithStats() throws IOException {
}

private Map<String, Object> runEsql(RestEsqlTestCase.RequestObjectBuilder requestObject) throws IOException {
return runEsql(requestObject, this.mode, NO_WARNINGS);
}

private static Map<String, Object> runEsql(RestEsqlTestCase.RequestObjectBuilder requestObject, Mode mode) throws IOException {
return runEsql(requestObject, mode, NO_WARNINGS);
}

private static Map<String, Object> runEsql(
RestEsqlTestCase.RequestObjectBuilder requestObject,
Mode mode,
List<String> expectedWarnings
) throws IOException {
requestObject = requestObject.esqlVersion("snapshot");
if (mode == Mode.ASYNC) {
return RestEsqlTestCase.runEsqlAsync(requestObject, NO_WARNINGS);
return RestEsqlTestCase.runEsqlAsync(requestObject, expectedWarnings);
} else {
return RestEsqlTestCase.runEsqlSync(requestObject, NO_WARNINGS);
return RestEsqlTestCase.runEsqlSync(requestObject, expectedWarnings);
}
}

Expand Down
Loading

0 comments on commit 3f13c4a

Please sign in to comment.