diff --git a/x-pack/plugin/sql/build.gradle b/x-pack/plugin/sql/build.gradle index f5dc3175d41..cd2f82ee7b3 100644 --- a/x-pack/plugin/sql/build.gradle +++ b/x-pack/plugin/sql/build.gradle @@ -21,10 +21,13 @@ archivesBaseName = 'x-pack-sql' integTest.enabled = false task internalClusterTest(type: RandomizedTestingTask, - group: JavaBasePlugin.VERIFICATION_GROUP -) { + group: JavaBasePlugin.VERIFICATION_GROUP, + dependsOn: unitTest.dependsOn) { include '**/*IT.class' + systemProperty 'es.set.netty.runtime.available.processors', 'false' } +check.dependsOn internalClusterTest +internalClusterTest.mustRunAfter test dependencies { // "org.elasticsearch.plugin:x-pack-core:${version}" doesn't work with idea because the testArtifacts are also here diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/SqlPlugin.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/SqlPlugin.java index 8c759a7d87f..9f569206438 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/SqlPlugin.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/SqlPlugin.java @@ -50,40 +50,38 @@ import static java.util.Collections.emptyList; public class SqlPlugin extends Plugin implements ActionPlugin { private final boolean enabled; - private final SqlLicenseChecker sqlLicenseChecker; - - SqlPlugin(boolean enabled, SqlLicenseChecker sqlLicenseChecker) { - this.enabled = enabled; - this.sqlLicenseChecker = sqlLicenseChecker; - } + private final SqlLicenseChecker sqlLicenseChecker = new SqlLicenseChecker( + (mode) -> { + XPackLicenseState licenseState = getLicenseState(); + switch (mode) { + case JDBC: + if (licenseState.isJdbcAllowed() == false) { + throw LicenseUtils.newComplianceException("jdbc"); + } + break; + case ODBC: + if (licenseState.isOdbcAllowed() == false) { + throw LicenseUtils.newComplianceException("odbc"); + } + break; + case PLAIN: + if (licenseState.isSqlAllowed() == false) { + throw LicenseUtils.newComplianceException(XPackField.SQL); + } + break; + default: + throw new IllegalArgumentException("Unknown SQL mode " + mode); + } + } + ); public SqlPlugin(Settings settings) { - this(XPackSettings.SQL_ENABLED.get(settings), new SqlLicenseChecker( - (mode) -> { - XPackLicenseState licenseState = XPackPlugin.getSharedLicenseState(); - switch (mode) { - case JDBC: - if (licenseState.isJdbcAllowed() == false) { - throw LicenseUtils.newComplianceException("jdbc"); - } - break; - case ODBC: - if (licenseState.isOdbcAllowed() == false) { - throw LicenseUtils.newComplianceException("odbc"); - } - break; - case PLAIN: - if (licenseState.isSqlAllowed() == false) { - throw LicenseUtils.newComplianceException(XPackField.SQL); - } - break; - default: - throw new IllegalArgumentException("Unknown SQL mode " + mode); - } - } - )); + this.enabled = XPackSettings.SQL_ENABLED.get(settings); } + // overridable by tests + protected XPackLicenseState getLicenseState() { return XPackPlugin.getSharedLicenseState(); } + @Override public Collection createComponents(Client client, ClusterService clusterService, ThreadPool threadPool, ResourceWatcherService resourceWatcherService, ScriptService scriptService, diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/action/AbstractSqlIntegTestCase.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/action/AbstractSqlIntegTestCase.java index 0e7d2888f9e..c741667ba9e 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/action/AbstractSqlIntegTestCase.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/action/AbstractSqlIntegTestCase.java @@ -5,25 +5,20 @@ */ package org.elasticsearch.xpack.sql.action; -import org.elasticsearch.analysis.common.CommonAnalysisPlugin; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.index.reindex.ReindexPlugin; +import org.elasticsearch.license.LicenseService; import org.elasticsearch.plugins.Plugin; import org.elasticsearch.test.ESIntegTestCase; -import org.elasticsearch.test.discovery.TestZenDiscovery; -import org.elasticsearch.xpack.core.XPackPlugin; import org.elasticsearch.xpack.core.XPackSettings; -import java.util.Arrays; import java.util.Collection; +import java.util.Collections; +import static org.elasticsearch.test.ESIntegTestCase.Scope.SUITE; + +@ESIntegTestCase.ClusterScope(scope = SUITE, numDataNodes = 0, numClientNodes = 0, maxNumDataNodes = 0, transportClientRatio = 0) public abstract class AbstractSqlIntegTestCase extends ESIntegTestCase { - @Override - protected boolean ignoreExternalCluster() { - return true; - } - @Override protected Settings nodeSettings(int nodeOrdinal) { Settings.Builder settings = Settings.builder().put(super.nodeSettings(nodeOrdinal)); @@ -32,29 +27,18 @@ public abstract class AbstractSqlIntegTestCase extends ESIntegTestCase { settings.put(XPackSettings.WATCHER_ENABLED.getKey(), false); settings.put(XPackSettings.GRAPH_ENABLED.getKey(), false); settings.put(XPackSettings.MACHINE_LEARNING_ENABLED.getKey(), false); - settings.put("xpack.ml.autodetect_process", false); + settings.put(LicenseService.SELF_GENERATED_LICENSE_TYPE.getKey(), "trial"); return settings.build(); } @Override protected Collection> nodePlugins() { - return Arrays.asList(XPackPlugin.class, CommonAnalysisPlugin.class, ReindexPlugin.class); + return Collections.singletonList(LocalStateSQLXPackPlugin.class); } @Override protected Collection> transportClientPlugins() { return nodePlugins(); } - - @Override - protected Settings transportClientSettings() { - // Plugin should be loaded on the transport client as well - return nodeSettings(0); - } - - @Override - protected Collection> getMockPlugins() { - return Arrays.asList(TestZenDiscovery.TestPlugin.class, TestSeedPlugin.class); - } } diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/action/LocalStateSQLXPackPlugin.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/action/LocalStateSQLXPackPlugin.java new file mode 100644 index 00000000000..1203bfae677 --- /dev/null +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/action/LocalStateSQLXPackPlugin.java @@ -0,0 +1,27 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ +package org.elasticsearch.xpack.sql.action; + +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.license.XPackLicenseState; +import org.elasticsearch.xpack.core.LocalStateCompositeXPackPlugin; +import org.elasticsearch.xpack.sql.plugin.SqlPlugin; + +import java.nio.file.Path; + +public class LocalStateSQLXPackPlugin extends LocalStateCompositeXPackPlugin { + + public LocalStateSQLXPackPlugin(final Settings settings, final Path configPath) throws Exception { + super(settings, configPath); + LocalStateSQLXPackPlugin thisVar = this; + plugins.add(new SqlPlugin(settings) { + @Override + protected XPackLicenseState getLicenseState() { + return thisVar.getLicenseState(); + } + }); + } +} diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/action/SqlActionIT.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/action/SqlActionIT.java index c71d7c27494..43ea7fe92eb 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/action/SqlActionIT.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/action/SqlActionIT.java @@ -18,12 +18,11 @@ import static org.hamcrest.Matchers.hasSize; public class SqlActionIT extends AbstractSqlIntegTestCase { - @AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/37191") - public void testSqlAction() throws Exception { + public void testSqlAction() { assertAcked(client().admin().indices().prepareCreate("test").get()); client().prepareBulk() - .add(new IndexRequest("test", "doc", "1").source("data", "bar", "count", 42)) - .add(new IndexRequest("test", "doc", "2").source("data", "baz", "count", 43)) + .add(new IndexRequest("test").id("1").source("data", "bar", "count", 42)) + .add(new IndexRequest("test").id("2").source("data", "baz", "count", 43)) .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) .get(); ensureYellow("test"); diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/action/SqlClearCursorActionIT.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/action/SqlClearCursorActionIT.java index 952104b49ee..d509c1325fe 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/action/SqlClearCursorActionIT.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/action/SqlClearCursorActionIT.java @@ -5,7 +5,6 @@ */ package org.elasticsearch.xpack.sql.action; -import org.apache.lucene.util.LuceneTestCase.AwaitsFix; import org.elasticsearch.action.bulk.BulkRequestBuilder; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.support.WriteRequest; @@ -17,16 +16,15 @@ import static org.hamcrest.Matchers.greaterThan; import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.notNullValue; -@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/37191") public class SqlClearCursorActionIT extends AbstractSqlIntegTestCase { - public void testSqlClearCursorAction() throws Exception { + public void testSqlClearCursorAction() { assertAcked(client().admin().indices().prepareCreate("test").get()); BulkRequestBuilder bulkRequestBuilder = client().prepareBulk(); int indexSize = randomIntBetween(100, 300); logger.info("Indexing {} records", indexSize); for (int i = 0; i < indexSize; i++) { - bulkRequestBuilder.add(new IndexRequest("test", "doc", "id" + i).source("data", "bar", "count", i)); + bulkRequestBuilder.add(new IndexRequest("test").id("id" + i).source("data", "bar", "count", i)); } bulkRequestBuilder.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE).get(); ensureYellow("test"); @@ -50,13 +48,13 @@ public class SqlClearCursorActionIT extends AbstractSqlIntegTestCase { assertEquals(0, getNumberOfSearchContexts()); } - public void testAutoCursorCleanup() throws Exception { + public void testAutoCursorCleanup() { assertAcked(client().admin().indices().prepareCreate("test").get()); BulkRequestBuilder bulkRequestBuilder = client().prepareBulk(); int indexSize = randomIntBetween(100, 300); logger.info("Indexing {} records", indexSize); for (int i = 0; i < indexSize; i++) { - bulkRequestBuilder.add(new IndexRequest("test", "doc", "id" + i).source("data", "bar", "count", i)); + bulkRequestBuilder.add(new IndexRequest("test").id("id" + i).source("data", "bar", "count", i)); } bulkRequestBuilder.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE).get(); ensureYellow("test"); @@ -77,7 +75,7 @@ public class SqlClearCursorActionIT extends AbstractSqlIntegTestCase { do { sqlQueryResponse = new SqlQueryRequestBuilder(client(), SqlQueryAction.INSTANCE).cursor(sqlQueryResponse.cursor()).get(); fetched += sqlQueryResponse.size(); - } while (sqlQueryResponse.cursor().equals("") == false); + } while (sqlQueryResponse.cursor().isEmpty() == false); assertEquals(indexSize, fetched); SqlClearCursorResponse cleanCursorResponse = new SqlClearCursorRequestBuilder(client(), SqlClearCursorAction.INSTANCE) diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/action/SqlDisabledIT.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/action/SqlDisabledIT.java index 0a56e804a00..51be1470051 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/action/SqlDisabledIT.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/action/SqlDisabledIT.java @@ -6,13 +6,23 @@ package org.elasticsearch.xpack.sql.action; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.plugins.Plugin; +import org.elasticsearch.xpack.core.XPackPlugin; import org.elasticsearch.xpack.core.XPackSettings; +import java.util.Collection; +import java.util.Collections; + import static org.hamcrest.CoreMatchers.either; import static org.hamcrest.CoreMatchers.startsWith; public class SqlDisabledIT extends AbstractSqlIntegTestCase { + @Override + protected Collection> nodePlugins() { + return Collections.singletonList(XPackPlugin.class); + } + @Override protected Settings nodeSettings(int nodeOrdinal) { return Settings.builder() @@ -29,8 +39,7 @@ public class SqlDisabledIT extends AbstractSqlIntegTestCase { .build(); } - @AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/37191") - public void testSqlAction() throws Exception { + public void testSqlAction() { Throwable throwable = expectThrows(Throwable.class, () -> new SqlQueryRequestBuilder(client(), SqlQueryAction.INSTANCE).query("SHOW tables").get()); assertThat(throwable.getMessage(), diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/action/SqlLicenseIT.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/action/SqlLicenseIT.java index 62f7b42c699..50dda656ab4 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/action/SqlLicenseIT.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/action/SqlLicenseIT.java @@ -35,7 +35,7 @@ import static org.elasticsearch.license.XPackLicenseStateTests.randomTrialBasicS import static org.elasticsearch.license.XPackLicenseStateTests.randomTrialOrPlatinumMode; import static org.hamcrest.Matchers.equalTo; -@LuceneTestCase.AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/37191") +@LuceneTestCase.AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/37320") public class SqlLicenseIT extends AbstractLicensesIntegrationTestCase { @Override protected boolean ignoreExternalCluster() { @@ -164,8 +164,8 @@ public class SqlLicenseIT extends AbstractLicensesIntegrationTestCase { private void setupTestIndex() { ElasticsearchAssertions.assertAcked(client().admin().indices().prepareCreate("test").get()); client().prepareBulk() - .add(new IndexRequest("test", "doc", "1").source("data", "bar", "count", 42)) - .add(new IndexRequest("test", "doc", "2").source("data", "baz", "count", 43)) + .add(new IndexRequest("test").id("1").source("data", "bar", "count", 42)) + .add(new IndexRequest("test").id("2").source("data", "baz", "count", 43)) .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) .get(); } diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/action/SqlTranslateActionIT.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/action/SqlTranslateActionIT.java index d86245dcbfa..3dc41ad9dd3 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/action/SqlTranslateActionIT.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/action/SqlTranslateActionIT.java @@ -17,12 +17,11 @@ import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcke public class SqlTranslateActionIT extends AbstractSqlIntegTestCase { - @AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/37191") - public void testSqlTranslateAction() throws Exception { + public void testSqlTranslateAction() { assertAcked(client().admin().indices().prepareCreate("test").get()); client().prepareBulk() - .add(new IndexRequest("test", "doc", "1").source("data", "bar", "count", 42)) - .add(new IndexRequest("test", "doc", "2").source("data", "baz", "count", 43)) + .add(new IndexRequest("test").id("1").source("data", "bar", "count", 42)) + .add(new IndexRequest("test").id("2").source("data", "baz", "count", 43)) .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) .get(); ensureYellow("test"); @@ -33,11 +32,11 @@ public class SqlTranslateActionIT extends AbstractSqlIntegTestCase { .query("SELECT " + columns + " FROM test ORDER BY count").get(); SearchSourceBuilder source = response.source(); FetchSourceContext fetch = source.fetchSource(); - assertEquals(true, fetch.fetchSource()); + assertTrue(fetch.fetchSource()); assertArrayEquals(new String[] { "data" }, fetch.includes()); assertEquals( singletonList(new DocValueFieldsContext.FieldAndFormat("count", DocValueFieldsContext.USE_DEFAULT_FORMAT)), source.docValueFields()); - assertEquals(singletonList(SortBuilders.fieldSort("count")), source.sorts()); + assertEquals(singletonList(SortBuilders.fieldSort("count").missing("_last").unmappedType("long")), source.sorts()); } } diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/plugin/SqlPluginTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/plugin/SqlPluginTests.java index 363254f414c..be8ac7b9968 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/plugin/SqlPluginTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/plugin/SqlPluginTests.java @@ -25,14 +25,14 @@ import static org.mockito.Mockito.mock; public class SqlPluginTests extends ESTestCase { public void testSqlDisabled() { - SqlPlugin plugin = new SqlPlugin(false, new SqlLicenseChecker((mode) -> {})); + Settings settings = Settings.builder().put("xpack.sql.enabled", false).build(); + SqlPlugin plugin = new SqlPlugin(settings); assertThat(plugin.createComponents(mock(Client.class), "cluster", new NamedWriteableRegistry(Cursors.getNamedWriteables())), - empty()); + empty()); assertThat(plugin.getActions(), empty()); assertThat(plugin.getRestHandlers(Settings.EMPTY, mock(RestController.class), - new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS), - IndexScopedSettings.DEFAULT_SCOPED_SETTINGS, new SettingsFilter(Collections.emptyList()), - mock(IndexNameExpressionResolver.class), () -> mock(DiscoveryNodes.class)), empty()); + new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS), + IndexScopedSettings.DEFAULT_SCOPED_SETTINGS, new SettingsFilter(Collections.emptyList()), + mock(IndexNameExpressionResolver.class), () -> mock(DiscoveryNodes.class)), empty()); } - }