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

SQL: Use new testing framework #101774

Merged
merged 19 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ public void checkSearchContent() throws IOException {
/**
* Read an address for Elasticsearch suitable for the JDBC driver from the system properties.
*/
public static String elasticsearchAddress() {
String cluster = System.getProperty("tests.rest.cluster");
public String elasticsearchAddress() {
String cluster = getTestRestCluster();
// JDBC only supports a single node at a time so we just give it one.
return cluster.split(",")[0];
/* This doesn't include "jdbc:es://" because we want the example in
Expand Down
131 changes: 69 additions & 62 deletions x-pack/plugin/sql/qa/server/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,73 +38,80 @@ subprojects {
apply plugin: 'elasticsearch.java'
}


if (project.name != 'security') {
// The security project just configures its subprojects
apply plugin: 'elasticsearch.legacy-java-rest-test'

testClusters.matching { it.name == "javaRestTest" }.configureEach {
testDistribution = 'DEFAULT'
setting 'xpack.ml.enabled', 'false'
setting 'xpack.watcher.enabled', 'false'
}


dependencies {
configurations.javaRestTestRuntimeClasspath {
resolutionStrategy.force "org.slf4j:slf4j-api:1.7.25"
}
configurations.javaRestTestRuntimeOnly {
// This is also required to make resolveAllDependencies work
resolutionStrategy.force "org.slf4j:slf4j-api:1.7.25"
}

/* Since we're a standalone rest test we actually get transitive
* dependencies but we don't really want them because they cause
* all kinds of trouble with the jar hell checks. So we suppress
* them explicitly for non-es projects. */
javaRestTestImplementation(project(':x-pack:plugin:sql:qa:server')) {
transitive = false
}
javaRestTestImplementation project(":test:framework")
javaRestTestImplementation project(xpackModule('ql:test-fixtures'))

// JDBC testing dependencies
javaRestTestRuntimeOnly "net.sourceforge.csvjdbc:csvjdbc:${csvjdbcVersion}"
javaRestTestRuntimeOnly "com.h2database:h2:${h2Version}"

// H2GIS testing dependencies
javaRestTestRuntimeOnly("org.orbisgis:h2gis:${h2gisVersion}")
javaRestTestRuntimeOnly("org.orbisgis:h2gis-api:${h2gisVersion}")
javaRestTestRuntimeOnly("org.orbisgis:h2gis-utilities:${h2gisVersion}")
javaRestTestRuntimeOnly("org.orbisgis:cts:1.5.2")


javaRestTestRuntimeOnly project(path: xpackModule('sql:jdbc'))
javaRestTestRuntimeOnly project(':x-pack:plugin:sql:sql-client')

// CLI testing dependencies
javaRestTestRuntimeOnly project(path: xpackModule('sql:sql-cli'))
javaRestTestRuntimeOnly(project(':x-pack:plugin:sql:sql-action')) {
transitive = false
if (project.parent.name == 'security')
{
apply plugin: 'elasticsearch.legacy-java-rest-test'

testClusters.matching { it.name == "javaRestTest" }.configureEach {
testDistribution = 'DEFAULT'
setting 'xpack.ml.enabled', 'false'
setting 'xpack.watcher.enabled', 'false'
}
} else {
apply plugin: 'elasticsearch.internal-java-rest-test'
tasks.named('javaRestTest') {
usesDefaultDistribution()
}
}

javaRestTestRuntimeOnly("org.jline:jline-terminal-jna:${jlineVersion}") {
exclude group: "net.java.dev.jna"
dependencies {
configurations.javaRestTestRuntimeClasspath {
resolutionStrategy.force "org.slf4j:slf4j-api:1.7.25"
}
configurations.javaRestTestRuntimeOnly {
// This is also required to make resolveAllDependencies work
resolutionStrategy.force "org.slf4j:slf4j-api:1.7.25"
}

/* Since we're a standalone rest test we actually get transitive
* dependencies but we don't really want them because they cause
* all kinds of trouble with the jar hell checks. So we suppress
* them explicitly for non-es projects. */
javaRestTestImplementation(project(':x-pack:plugin:sql:qa:server')) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing below this line is new - only fixed indentation (was tripping me up, haha).

transitive = false
}
javaRestTestImplementation project(":test:framework")
javaRestTestImplementation project(xpackModule('ql:test-fixtures'))

// JDBC testing dependencies
javaRestTestRuntimeOnly "net.sourceforge.csvjdbc:csvjdbc:${csvjdbcVersion}"
javaRestTestRuntimeOnly "com.h2database:h2:${h2Version}"

// H2GIS testing dependencies
javaRestTestRuntimeOnly("org.orbisgis:h2gis:${h2gisVersion}")
javaRestTestRuntimeOnly("org.orbisgis:h2gis-api:${h2gisVersion}")
javaRestTestRuntimeOnly("org.orbisgis:h2gis-utilities:${h2gisVersion}")
javaRestTestRuntimeOnly("org.orbisgis:cts:1.5.2")


javaRestTestRuntimeOnly project(path: xpackModule('sql:jdbc'))
javaRestTestRuntimeOnly project(':x-pack:plugin:sql:sql-client')

// CLI testing dependencies
javaRestTestRuntimeOnly project(path: xpackModule('sql:sql-cli'))
javaRestTestRuntimeOnly(project(':x-pack:plugin:sql:sql-action')) {
transitive = false
}

javaRestTestRuntimeOnly("org.jline:jline-terminal-jna:${jlineVersion}") {
exclude group: "net.java.dev.jna"
}
javaRestTestRuntimeOnly "org.jline:jline-terminal:${jlineVersion}"
javaRestTestRuntimeOnly "org.jline:jline-reader:${jlineVersion}"
javaRestTestRuntimeOnly "org.jline:jline-style:${jlineVersion}"

javaRestTestRuntimeOnly "net.java.dev.jna:jna:${versions.jna}"

// spatial dependency
javaRestTestRuntimeOnly project(path: xpackModule('spatial'))
javaRestTestRuntimeOnly project(path: ':modules:legacy-geo')

javaRestTestRuntimeOnly project(path: ':modules:rest-root')

javaRestTestRuntimeOnly "org.slf4j:slf4j-api:1.7.25"
}
javaRestTestRuntimeOnly "org.jline:jline-terminal:${jlineVersion}"
javaRestTestRuntimeOnly "org.jline:jline-reader:${jlineVersion}"
javaRestTestRuntimeOnly "org.jline:jline-style:${jlineVersion}"

javaRestTestRuntimeOnly "net.java.dev.jna:jna:${versions.jna}"

// spatial dependency
javaRestTestRuntimeOnly project(path: xpackModule('spatial'))
javaRestTestRuntimeOnly project(path: ':modules:legacy-geo')

javaRestTestRuntimeOnly project(path: ':modules:rest-root')

javaRestTestRuntimeOnly "org.slf4j:slf4j-api:1.7.25"
}
}
}
Original file line number Diff line number Diff line change
@@ -1,56 +1,6 @@
import org.elasticsearch.gradle.testclusters.DefaultTestClustersTask
import static org.elasticsearch.gradle.PropertyNormalization.IGNORE_VALUE

apply plugin: 'elasticsearch.legacy-java-rest-test'

dependencies {
javaRestTestImplementation project(path: xpackModule('ql:test-fixtures'))
clusterPlugins project(':x-pack:qa:freeze-plugin')
}

def remoteClusterReg = testClusters.register('remote-cluster') {
testDistribution = 'DEFAULT'
setting 'node.roles', '[data,ingest,master]'
setting 'xpack.ml.enabled', 'false'
setting 'xpack.watcher.enabled', 'false'
setting 'xpack.security.enabled', 'true'
setting 'xpack.license.self_generated.type', 'trial'
setting 'xpack.security.autoconfiguration.enabled', 'false'

user username: "test_user", password: "x-pack-test-password"
plugin ':x-pack:qa:freeze-plugin'
}

def javaRestTestClusterReg = testClusters.register('javaRestTest') {
testDistribution = 'DEFAULT'
setting 'xpack.ml.enabled', 'false'
setting 'xpack.watcher.enabled', 'false'
setting 'cluster.remote.my_remote_cluster.seeds', {
remoteClusterReg.get().getAllTransportPortURI().collect { "\"$it\"" }.toString()
}, IGNORE_VALUE
setting 'cluster.remote.connections_per_cluster', "1"
setting 'xpack.security.enabled', 'true'
setting 'xpack.license.self_generated.type', 'trial'
setting 'xpack.security.autoconfiguration.enabled', 'false'

user username: "test_user", password: "x-pack-test-password"
plugin ':x-pack:qa:freeze-plugin'
}

tasks.register("startRemoteCluster", DefaultTestClustersTask.class) {
useCluster remoteClusterReg
doLast {
"Starting remote cluster before integ tests and integ test cluster is started"
}
}

tasks.named("javaRestTest").configure {
dependsOn 'startRemoteCluster'
useCluster remoteClusterReg
doFirst {
nonInputProperties.systemProperty 'tests.rest.cluster.remote.host', remoteClusterReg.map(c->c.getAllHttpSocketURI().get(0))
// credentials for both local and remote clusters
nonInputProperties.systemProperty 'tests.rest.cluster.multi.user', "test_user"
nonInputProperties.systemProperty 'tests.rest.cluster.multi.password', "x-pack-test-password"
}
}
tasks.named("check").configure {dependsOn("javaRestTest") } // run these tests as part of the "check" task
Original file line number Diff line number Diff line change
Expand Up @@ -8,44 +8,82 @@
package org.elasticsearch.xpack.sql.qa.multi_cluster_with_security;

import org.elasticsearch.client.Request;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.xpack.sql.qa.jdbc.JdbcIntegrationTestCase;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.rules.RuleChain;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Properties;

import static org.elasticsearch.transport.RemoteClusterAware.buildRemoteIndexName;
import static org.elasticsearch.xpack.sql.qa.multi_cluster_with_security.SqlTestClusterWithRemote.LOCAL_CLUSTER_NAME;
import static org.elasticsearch.xpack.sql.qa.multi_cluster_with_security.SqlTestClusterWithRemote.PASSWORD;
import static org.elasticsearch.xpack.sql.qa.multi_cluster_with_security.SqlTestClusterWithRemote.REMOTE_CLUSTER_ALIAS;
import static org.elasticsearch.xpack.sql.qa.multi_cluster_with_security.SqlTestClusterWithRemote.USER_NAME;

public class JdbcCatalogIT extends JdbcIntegrationTestCase {
public static SqlTestClusterWithRemote clusterAndRemote = new SqlTestClusterWithRemote();
public static TestRule setupIndex = new TestRule() {
@Override
public Statement apply(Statement base, Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
try {
index(INDEX_NAME, body -> body.field("zero", 0), clusterAndRemote.getRemoteClient());
base.evaluate();
} finally {
clusterAndRemote.getRemoteClient().performRequest(new Request("DELETE", "/" + INDEX_NAME));
}
}
};
}
};

// gradle defines
public static final String LOCAL_CLUSTER_NAME = "javaRestTest";
public static final String REMOTE_CLUSTER_NAME = "my_remote_cluster";
@ClassRule
public static RuleChain testSetup = RuleChain.outerRule(clusterAndRemote).around(setupIndex);

private static final String INDEX_NAME = "test";
@Override
protected String getTestRestCluster() {
return clusterAndRemote.getCluster().getHttpAddresses();
}

@BeforeClass
static void setupIndex() throws IOException {
index(INDEX_NAME, body -> body.field("zero", 0));
@Override
protected Settings restClientSettings() {
return clusterAndRemote.clusterAuthSettings();
}

@AfterClass
static void cleanupIndex() throws IOException {
provisioningClient().performRequest(new Request("DELETE", "/" + INDEX_NAME));
@Override
protected RestClient provisioningClient() {
return clusterAndRemote.getRemoteClient();
}

@Override
protected Properties connectionProperties() {
Properties connectionProperties = super.connectionProperties();
connectionProperties.put("user", USER_NAME);
connectionProperties.put("password", PASSWORD);
return connectionProperties;
}

private static final String INDEX_NAME = "test";

public void testJdbcSetCatalog() throws Exception {
try (Connection es = esJdbc()) {
PreparedStatement ps = es.prepareStatement("SELECT count(*) FROM " + INDEX_NAME);
SQLException ex = expectThrows(SQLException.class, ps::executeQuery);
assertTrue(ex.getMessage().contains("Unknown index [" + INDEX_NAME + "]"));

String catalog = REMOTE_CLUSTER_NAME.substring(0, randomIntBetween(0, REMOTE_CLUSTER_NAME.length())) + "*";
String catalog = REMOTE_CLUSTER_ALIAS.substring(0, randomIntBetween(0, REMOTE_CLUSTER_ALIAS.length())) + "*";
es.setCatalog(catalog);
assertEquals(catalog, es.getCatalog());

Expand All @@ -62,7 +100,7 @@ public void testJdbcSetCatalog() throws Exception {

public void testQueryCatalogPrecedence() throws Exception {
try (Connection es = esJdbc()) {
PreparedStatement ps = es.prepareStatement("SELECT count(*) FROM " + buildRemoteIndexName(REMOTE_CLUSTER_NAME, INDEX_NAME));
PreparedStatement ps = es.prepareStatement("SELECT count(*) FROM " + buildRemoteIndexName(REMOTE_CLUSTER_ALIAS, INDEX_NAME));
es.setCatalog(LOCAL_CLUSTER_NAME);
ResultSet rs = ps.executeQuery();
assertTrue(rs.next());
Expand All @@ -73,7 +111,7 @@ public void testQueryCatalogPrecedence() throws Exception {
public void testQueryWithQualifierAndSetCatalog() throws Exception {
try (Connection es = esJdbc()) {
PreparedStatement ps = es.prepareStatement("SELECT " + INDEX_NAME + ".zero FROM " + INDEX_NAME);
es.setCatalog(REMOTE_CLUSTER_NAME);
es.setCatalog(REMOTE_CLUSTER_ALIAS);
ResultSet rs = ps.executeQuery();
assertTrue(rs.next());
assertEquals(0, rs.getInt(1));
Expand All @@ -84,7 +122,7 @@ public void testQueryWithQualifierAndSetCatalog() throws Exception {
public void testQueryWithQualifiedFieldAndIndex() throws Exception {
try (Connection es = esJdbc()) {
PreparedStatement ps = es.prepareStatement(
"SELECT " + INDEX_NAME + ".zero FROM " + buildRemoteIndexName(REMOTE_CLUSTER_NAME, INDEX_NAME)
"SELECT " + INDEX_NAME + ".zero FROM " + buildRemoteIndexName(REMOTE_CLUSTER_ALIAS, INDEX_NAME)
);
es.setCatalog(LOCAL_CLUSTER_NAME); // set, but should be ignored
ResultSet rs = ps.executeQuery();
Expand All @@ -105,7 +143,7 @@ public void testCatalogDependentCommands() throws Exception {
ResultSet rs = ps.executeQuery();
assertFalse(rs.next());

es.setCatalog(REMOTE_CLUSTER_NAME);
es.setCatalog(REMOTE_CLUSTER_ALIAS);
rs = ps.executeQuery();
assertTrue(rs.next());
assertFalse(rs.next());
Expand Down
Loading