Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Spotless] Applying Google Code Format for integ-test directory (pt 4/4) #18 #1979

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ spotless {
'prometheus/**/*.java',
'sql/**/*.java',
'common/**/*.java',
'ppl/**/*.java'
'ppl/**/*.java',
'integ-test/**/*java'
exclude '**/build/**', '**/build-*/**'
}
importOrder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,177 +35,189 @@

public class SQLBackwardsCompatibilityIT extends SQLIntegTestCase {

private static final ClusterType CLUSTER_TYPE = ClusterType.parse(System.getProperty("tests.rest.bwcsuite"));
private static final String CLUSTER_NAME = System.getProperty("tests.clustername");

@Override
protected final boolean preserveIndicesUponCompletion() {
return true;
private static final ClusterType CLUSTER_TYPE =
ClusterType.parse(System.getProperty("tests.rest.bwcsuite"));
private static final String CLUSTER_NAME = System.getProperty("tests.clustername");

@Override
protected final boolean preserveIndicesUponCompletion() {
return true;
}

@Override
protected final boolean preserveReposUponCompletion() {
return true;
}

@Override
protected boolean preserveTemplatesUponCompletion() {
return true;
}

@Override
protected final Settings restClientSettings() {
return Settings.builder()
.put(super.restClientSettings())
// increase the timeout here to 90 seconds to handle long waits for a green
// cluster health. the waits for green need to be longer than a minute to
// account for delayed shards
.put(OpenSearchRestTestCase.CLIENT_SOCKET_TIMEOUT, "90s")
.build();
}

private enum ClusterType {
OLD,
MIXED,
UPGRADED;

public static ClusterType parse(String value) {
switch (value) {
case "old_cluster":
return OLD;
case "mixed_cluster":
return MIXED;
case "upgraded_cluster":
return UPGRADED;
default:
throw new AssertionError("unknown cluster type: " + value);
}
}

@Override
protected final boolean preserveReposUponCompletion() {
return true;
}

@SuppressWarnings("unchecked")
public void testBackwardsCompatibility() throws Exception {
String uri = getUri();
Map<String, Map<String, Object>> responseMap =
(Map<String, Map<String, Object>>) getAsMap(uri).get("nodes");
for (Map<String, Object> response : responseMap.values()) {
List<Map<String, Object>> plugins = (List<Map<String, Object>>) response.get("plugins");
Set<Object> pluginNames =
plugins.stream().map(map -> map.get("name")).collect(Collectors.toSet());
switch (CLUSTER_TYPE) {
case OLD:
Assert.assertTrue(pluginNames.contains("opensearch-sql"));
updateLegacySQLSettings();
loadIndex(Index.ACCOUNT);
verifySQLQueries(LEGACY_QUERY_API_ENDPOINT);
break;
case MIXED:
Assert.assertTrue(pluginNames.contains("opensearch-sql"));
verifySQLSettings();
verifySQLQueries(LEGACY_QUERY_API_ENDPOINT);
break;
case UPGRADED:
Assert.assertTrue(pluginNames.contains("opensearch-sql"));
verifySQLSettings();
verifySQLQueries(QUERY_API_ENDPOINT);
break;
}
break;
}

@Override
protected boolean preserveTemplatesUponCompletion() {
return true;
}

@Override
protected final Settings restClientSettings() {
return Settings
.builder()
.put(super.restClientSettings())
// increase the timeout here to 90 seconds to handle long waits for a green
// cluster health. the waits for green need to be longer than a minute to
// account for delayed shards
.put(OpenSearchRestTestCase.CLIENT_SOCKET_TIMEOUT, "90s")
.build();
}

private enum ClusterType {
OLD,
MIXED,
UPGRADED;

public static ClusterType parse(String value) {
switch (value) {
case "old_cluster":
return OLD;
case "mixed_cluster":
return MIXED;
case "upgraded_cluster":
return UPGRADED;
default:
throw new AssertionError("unknown cluster type: " + value);
}
}

private String getUri() {
switch (CLUSTER_TYPE) {
case OLD:
return "_nodes/" + CLUSTER_NAME + "-0/plugins";
case MIXED:
String round = System.getProperty("tests.rest.bwcsuite_round");
if (round.equals("second")) {
return "_nodes/" + CLUSTER_NAME + "-1/plugins";
} else if (round.equals("third")) {
return "_nodes/" + CLUSTER_NAME + "-2/plugins";
} else {
return "_nodes/" + CLUSTER_NAME + "-0/plugins";
}
case UPGRADED:
return "_nodes/plugins";
default:
throw new AssertionError("unknown cluster type: " + CLUSTER_TYPE);
}

@SuppressWarnings("unchecked")
public void testBackwardsCompatibility() throws Exception {
String uri = getUri();
Map<String, Map<String, Object>> responseMap = (Map<String, Map<String, Object>>) getAsMap(uri).get("nodes");
for (Map<String, Object> response : responseMap.values()) {
List<Map<String, Object>> plugins = (List<Map<String, Object>>) response.get("plugins");
Set<Object> pluginNames = plugins.stream().map(map -> map.get("name")).collect(Collectors.toSet());
switch (CLUSTER_TYPE) {
case OLD:
Assert.assertTrue(pluginNames.contains("opensearch-sql"));
updateLegacySQLSettings();
loadIndex(Index.ACCOUNT);
verifySQLQueries(LEGACY_QUERY_API_ENDPOINT);
break;
case MIXED:
Assert.assertTrue(pluginNames.contains("opensearch-sql"));
verifySQLSettings();
verifySQLQueries(LEGACY_QUERY_API_ENDPOINT);
break;
case UPGRADED:
Assert.assertTrue(pluginNames.contains("opensearch-sql"));
verifySQLSettings();
verifySQLQueries(QUERY_API_ENDPOINT);
break;
}
break;
}
}

private void updateLegacySQLSettings() throws IOException {
Request request = new Request("PUT", LEGACY_SQL_SETTINGS_API_ENDPOINT);
request.setJsonEntity(
String.format(
Locale.ROOT,
"{\n" + " \"persistent\" : {\n" + " \"%s\" : \"%s\"\n" + " }\n" + "}",
Yury-Fridlyand marked this conversation as resolved.
Show resolved Hide resolved
"opendistro.sql.cursor.keep_alive",
"7m"));

RequestOptions.Builder restOptionsBuilder = RequestOptions.DEFAULT.toBuilder();
restOptionsBuilder.addHeader("Content-Type", "application/json");
request.setOptions(restOptionsBuilder);

Response response = client().performRequest(request);
JSONObject jsonObject = new JSONObject(getResponseBody(response));
Assert.assertTrue((boolean) jsonObject.get("acknowledged"));
}

private void verifySQLSettings() throws IOException {
Request request = new Request("GET", "_cluster/settings?flat_settings");

RequestOptions.Builder restOptionsBuilder = RequestOptions.DEFAULT.toBuilder();
restOptionsBuilder.addHeader("Content-Type", "application/json");
request.setOptions(restOptionsBuilder);

Response response = client().performRequest(request);
JSONObject jsonObject = new JSONObject(getResponseBody(response));
Assert.assertEquals(
"{\"transient\":{},\"persistent\":{\"opendistro.sql.cursor.keep_alive\":\"7m\"}}",
jsonObject.toString());
}

private void verifySQLQueries(String endpoint) throws IOException {
JSONObject filterResponse =
executeSQLQuery(
endpoint,
"SELECT COUNT(*) FILTER(WHERE age > 35) FROM " + TestsConstants.TEST_INDEX_ACCOUNT);
verifySchema(filterResponse, schema("COUNT(*) FILTER(WHERE age > 35)", null, "integer"));
verifyDataRows(filterResponse, rows(238));

JSONObject aggResponse =
executeSQLQuery(
endpoint, "SELECT COUNT(DISTINCT age) FROM " + TestsConstants.TEST_INDEX_ACCOUNT);
verifySchema(aggResponse, schema("COUNT(DISTINCT age)", null, "integer"));
verifyDataRows(aggResponse, rows(21));

JSONObject groupByResponse =
executeSQLQuery(
endpoint,
"select a.gender from "
+ TestsConstants.TEST_INDEX_ACCOUNT
+ " a group by a.gender having count(*) > 0");
verifySchema(groupByResponse, schema("gender", null, "text"));
Assert.assertEquals("[[\"F\"],[\"M\"]]", groupByResponse.getJSONArray("datarows").toString());
}

private JSONObject executeSQLQuery(String endpoint, String query) throws IOException {
Request request = new Request("POST", endpoint);
request.setJsonEntity(String.format(Locale.ROOT, "{" + " \"query\" : \"%s\"" + "}", query));
Yury-Fridlyand marked this conversation as resolved.
Show resolved Hide resolved

RequestOptions.Builder restOptionsBuilder = RequestOptions.DEFAULT.toBuilder();
restOptionsBuilder.addHeader("Content-Type", "application/json");
request.setOptions(restOptionsBuilder);

Response response = client().performRequest(request);
return new JSONObject(getResponseBody(response));
}

@Override
public boolean shouldResetQuerySizeLimit() {
return false;
}

@Override
protected synchronized void loadIndex(Index index) throws IOException {
String indexName = index.getName();
String mapping = index.getMapping();
// current directory becomes 'integ-test/build/testrun/sqlBwcCluster#<task>' during bwc
String dataSet = "../../../" + index.getDataSet();

if (!isIndexExist(client(), indexName)) {
createIndexByRestClient(client(), indexName, mapping);
loadDataByRestClient(client(), indexName, dataSet);
}

private String getUri() {
switch (CLUSTER_TYPE) {
case OLD:
return "_nodes/" + CLUSTER_NAME + "-0/plugins";
case MIXED:
String round = System.getProperty("tests.rest.bwcsuite_round");
if (round.equals("second")) {
return "_nodes/" + CLUSTER_NAME + "-1/plugins";
} else if (round.equals("third")) {
return "_nodes/" + CLUSTER_NAME + "-2/plugins";
} else {
return "_nodes/" + CLUSTER_NAME + "-0/plugins";
}
case UPGRADED:
return "_nodes/plugins";
default:
throw new AssertionError("unknown cluster type: " + CLUSTER_TYPE);
}
}

private void updateLegacySQLSettings() throws IOException {
Request request = new Request("PUT", LEGACY_SQL_SETTINGS_API_ENDPOINT);
request.setJsonEntity(String.format(Locale.ROOT, "{\n" +
" \"persistent\" : {\n" +
" \"%s\" : \"%s\"\n" +
" }\n" +
"}", "opendistro.sql.cursor.keep_alive", "7m"));

RequestOptions.Builder restOptionsBuilder = RequestOptions.DEFAULT.toBuilder();
restOptionsBuilder.addHeader("Content-Type", "application/json");
request.setOptions(restOptionsBuilder);

Response response = client().performRequest(request);
JSONObject jsonObject = new JSONObject(getResponseBody(response));
Assert.assertTrue((boolean) jsonObject.get("acknowledged"));
}

private void verifySQLSettings() throws IOException {
Request request = new Request("GET", "_cluster/settings?flat_settings");

RequestOptions.Builder restOptionsBuilder = RequestOptions.DEFAULT.toBuilder();
restOptionsBuilder.addHeader("Content-Type", "application/json");
request.setOptions(restOptionsBuilder);

Response response = client().performRequest(request);
JSONObject jsonObject = new JSONObject(getResponseBody(response));
Assert.assertEquals("{\"transient\":{},\"persistent\":{\"opendistro.sql.cursor.keep_alive\":\"7m\"}}", jsonObject.toString());
}

private void verifySQLQueries(String endpoint) throws IOException {
JSONObject filterResponse = executeSQLQuery(endpoint, "SELECT COUNT(*) FILTER(WHERE age > 35) FROM " + TestsConstants.TEST_INDEX_ACCOUNT);
verifySchema(filterResponse, schema("COUNT(*) FILTER(WHERE age > 35)", null, "integer"));
verifyDataRows(filterResponse, rows(238));

JSONObject aggResponse = executeSQLQuery(endpoint, "SELECT COUNT(DISTINCT age) FROM " + TestsConstants.TEST_INDEX_ACCOUNT);
verifySchema(aggResponse, schema("COUNT(DISTINCT age)", null, "integer"));
verifyDataRows(aggResponse, rows(21));

JSONObject groupByResponse = executeSQLQuery(endpoint, "select a.gender from " + TestsConstants.TEST_INDEX_ACCOUNT + " a group by a.gender having count(*) > 0");
verifySchema(groupByResponse, schema("gender", null, "text"));
Assert.assertEquals("[[\"F\"],[\"M\"]]", groupByResponse.getJSONArray("datarows").toString());
}

private JSONObject executeSQLQuery(String endpoint, String query) throws IOException {
Request request = new Request("POST", endpoint);
request.setJsonEntity(String.format(Locale.ROOT, "{" +
" \"query\" : \"%s\"" +
"}", query));

RequestOptions.Builder restOptionsBuilder = RequestOptions.DEFAULT.toBuilder();
restOptionsBuilder.addHeader("Content-Type", "application/json");
request.setOptions(restOptionsBuilder);

Response response = client().performRequest(request);
return new JSONObject(getResponseBody(response));
}

@Override
public boolean shouldResetQuerySizeLimit() {
return false;
}

@Override
protected synchronized void loadIndex(Index index) throws IOException {
String indexName = index.getName();
String mapping = index.getMapping();
// current directory becomes 'integ-test/build/testrun/sqlBwcCluster#<task>' during bwc
String dataSet = "../../../" + index.getDataSet();

if (!isIndexExist(client(), indexName)) {
createIndexByRestClient(client(), indexName, mapping);
loadDataByRestClient(client(), indexName, dataSet);
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import org.opensearch.sql.legacy.utils.StringUtils;

/**
*
*
* <pre>
* Test configuration parse the following information from command line arguments:
* 1) Test schema and data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ public void testInsertNullData() throws IOException {
assertEquals("POST", actual.getMethod());
assertEquals("/test/_bulk?refresh=true", actual.getEndpoint());
assertEquals(
"{\"index\":{}}\n{\"age\":30}\n{\"index\":{}}\n{\"name\":\"Hank\"}\n",
getBody(actual));
"{\"index\":{}}\n{\"age\":30}\n{\"index\":{}}\n{\"name\":\"Hank\"}\n", getBody(actual));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ private Object convertStringToObject(String type, String str) {
@Override
public String toString() {
int total = dataRows.size();
return String.format("Test data set:\n Table name: %s\n Schema: %s\n Data rows (first 5 in %d):", tableName, schema, total)
return String.format(
"Test data set:\n Table name: %s\n Schema: %s\n Data rows (first 5 in %d):",
tableName, schema, total)
+ dataRows.stream().limit(5).map(Arrays::toString).collect(joining("\n ", "\n ", "\n"));
}
}
Loading
Loading