[ML] Adds a test for ml transport client (elastic/x-pack-elasticsearch#851)

This change also adds synchronous methods to `MachineLearningClient`.

relates elastic/x-pack-elasticsearch#567

Original commit: elastic/x-pack-elasticsearch@b3a4b38a51
This commit is contained in:
Colin Goodheart-Smithe 2017-03-28 09:05:55 +01:00 committed by GitHub
parent eac00c6a9d
commit 99db6013ea
6 changed files with 496 additions and 30 deletions

View File

@ -8,14 +8,15 @@ package org.elasticsearch.xpack;
import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.ActionListener;
import org.elasticsearch.client.Client; import org.elasticsearch.client.Client;
import org.elasticsearch.license.LicensingClient; import org.elasticsearch.license.LicensingClient;
import org.elasticsearch.license.XPackInfoResponse;
import org.elasticsearch.xpack.action.XPackInfoAction;
import org.elasticsearch.xpack.action.XPackInfoRequest;
import org.elasticsearch.xpack.action.XPackInfoRequestBuilder;
import org.elasticsearch.xpack.ml.client.MachineLearningClient;
import org.elasticsearch.xpack.monitoring.client.MonitoringClient; import org.elasticsearch.xpack.monitoring.client.MonitoringClient;
import org.elasticsearch.xpack.security.authc.support.SecuredString; import org.elasticsearch.xpack.security.authc.support.SecuredString;
import org.elasticsearch.xpack.security.client.SecurityClient; import org.elasticsearch.xpack.security.client.SecurityClient;
import org.elasticsearch.xpack.watcher.client.WatcherClient; import org.elasticsearch.xpack.watcher.client.WatcherClient;
import org.elasticsearch.xpack.action.XPackInfoAction;
import org.elasticsearch.xpack.action.XPackInfoRequest;
import org.elasticsearch.xpack.action.XPackInfoRequestBuilder;
import org.elasticsearch.license.XPackInfoResponse;
import java.util.Collections; import java.util.Collections;
import java.util.Map; import java.util.Map;
@ -31,6 +32,7 @@ public class XPackClient {
private final MonitoringClient monitoringClient; private final MonitoringClient monitoringClient;
private final SecurityClient securityClient; private final SecurityClient securityClient;
private final WatcherClient watcherClient; private final WatcherClient watcherClient;
private final MachineLearningClient machineLearning;
public XPackClient(Client client) { public XPackClient(Client client) {
this.client = client; this.client = client;
@ -38,6 +40,7 @@ public class XPackClient {
this.monitoringClient = new MonitoringClient(client); this.monitoringClient = new MonitoringClient(client);
this.securityClient = new SecurityClient(client); this.securityClient = new SecurityClient(client);
this.watcherClient = new WatcherClient(client); this.watcherClient = new WatcherClient(client);
this.machineLearning = new MachineLearningClient(client);
} }
public Client es() { public Client es() {
@ -60,6 +63,10 @@ public class XPackClient {
return watcherClient; return watcherClient;
} }
public MachineLearningClient machineLearning() {
return machineLearning;
}
public XPackClient withHeaders(Map<String, String> headers) { public XPackClient withHeaders(Map<String, String> headers) {
return new XPackClient(client.filterWithHeader(headers)); return new XPackClient(client.filterWithHeader(headers));
} }

View File

@ -5,7 +5,9 @@
*/ */
package org.elasticsearch.xpack.ml.client; package org.elasticsearch.xpack.ml.client;
import org.elasticsearch.action.ActionFuture;
import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.support.PlainListenableActionFuture;
import org.elasticsearch.client.ElasticsearchClient; import org.elasticsearch.client.ElasticsearchClient;
import org.elasticsearch.xpack.ml.action.CloseJobAction; import org.elasticsearch.xpack.ml.action.CloseJobAction;
import org.elasticsearch.xpack.ml.action.DeleteDatafeedAction; import org.elasticsearch.xpack.ml.action.DeleteDatafeedAction;
@ -43,114 +45,341 @@ public class MachineLearningClient {
this.client = client; this.client = client;
} }
public void closeJob(CloseJobAction.Request request, ActionListener<CloseJobAction.Response> listener) { public void closeJob(CloseJobAction.Request request,
ActionListener<CloseJobAction.Response> listener) {
client.execute(CloseJobAction.INSTANCE, request, listener); client.execute(CloseJobAction.INSTANCE, request, listener);
} }
public void deleteDatafeed(DeleteDatafeedAction.Request request, ActionListener<DeleteDatafeedAction.Response> listener) { public ActionFuture<CloseJobAction.Response> closeJob(CloseJobAction.Request request) {
PlainListenableActionFuture<CloseJobAction.Response> listener =
new PlainListenableActionFuture<>(client.threadPool());
client.execute(CloseJobAction.INSTANCE, request, listener);
return listener;
}
public void deleteDatafeed(DeleteDatafeedAction.Request request,
ActionListener<DeleteDatafeedAction.Response> listener) {
client.execute(DeleteDatafeedAction.INSTANCE, request, listener); client.execute(DeleteDatafeedAction.INSTANCE, request, listener);
} }
public void deleteFilter(DeleteFilterAction.Request request, ActionListener<DeleteFilterAction.Response> listener) { public ActionFuture<DeleteDatafeedAction.Response> deleteDatafeed(
DeleteDatafeedAction.Request request) {
PlainListenableActionFuture<DeleteDatafeedAction.Response> listener =
new PlainListenableActionFuture<>(client.threadPool());
client.execute(DeleteDatafeedAction.INSTANCE, request, listener);
return listener;
}
public void deleteFilter(DeleteFilterAction.Request request,
ActionListener<DeleteFilterAction.Response> listener) {
client.execute(DeleteFilterAction.INSTANCE, request, listener); client.execute(DeleteFilterAction.INSTANCE, request, listener);
} }
public void deleteJob(DeleteJobAction.Request request, ActionListener<DeleteJobAction.Response> listener) { public ActionFuture<DeleteFilterAction.Response> deleteFilter(
DeleteFilterAction.Request request) {
PlainListenableActionFuture<DeleteFilterAction.Response> listener =
new PlainListenableActionFuture<>(client.threadPool());
client.execute(DeleteFilterAction.INSTANCE, request, listener);
return listener;
}
public void deleteJob(DeleteJobAction.Request request,
ActionListener<DeleteJobAction.Response> listener) {
client.execute(DeleteJobAction.INSTANCE, request, listener); client.execute(DeleteJobAction.INSTANCE, request, listener);
} }
public ActionFuture<DeleteJobAction.Response> deleteJob(DeleteJobAction.Request request) {
PlainListenableActionFuture<DeleteJobAction.Response> listener =
new PlainListenableActionFuture<>(client.threadPool());
client.execute(DeleteJobAction.INSTANCE, request, listener);
return listener;
}
public void deleteModelSnapshot(DeleteModelSnapshotAction.Request request, public void deleteModelSnapshot(DeleteModelSnapshotAction.Request request,
ActionListener<DeleteModelSnapshotAction.Response> listener) { ActionListener<DeleteModelSnapshotAction.Response> listener) {
client.execute(DeleteModelSnapshotAction.INSTANCE, request, listener); client.execute(DeleteModelSnapshotAction.INSTANCE, request, listener);
} }
public void flushJob(FlushJobAction.Request request, ActionListener<FlushJobAction.Response> listener) { public ActionFuture<DeleteModelSnapshotAction.Response> deleteModelSnapshot(
DeleteModelSnapshotAction.Request request) {
PlainListenableActionFuture<DeleteModelSnapshotAction.Response> listener =
new PlainListenableActionFuture<>(client.threadPool());
client.execute(DeleteModelSnapshotAction.INSTANCE, request, listener);
return listener;
}
public void flushJob(FlushJobAction.Request request,
ActionListener<FlushJobAction.Response> listener) {
client.execute(FlushJobAction.INSTANCE, request, listener); client.execute(FlushJobAction.INSTANCE, request, listener);
} }
public void getBuckets(GetBucketsAction.Request request, ActionListener<GetBucketsAction.Response> listener) { public ActionFuture<FlushJobAction.Response> flushJob(FlushJobAction.Request request) {
PlainListenableActionFuture<FlushJobAction.Response> listener =
new PlainListenableActionFuture<>(client.threadPool());
client.execute(FlushJobAction.INSTANCE, request, listener);
return listener;
}
public void getBuckets(GetBucketsAction.Request request,
ActionListener<GetBucketsAction.Response> listener) {
client.execute(GetBucketsAction.INSTANCE, request, listener); client.execute(GetBucketsAction.INSTANCE, request, listener);
} }
public void getCategories(GetCategoriesAction.Request request, ActionListener<GetCategoriesAction.Response> listener) { public ActionFuture<GetBucketsAction.Response> getBuckets(GetBucketsAction.Request request) {
PlainListenableActionFuture<GetBucketsAction.Response> listener =
new PlainListenableActionFuture<>(client.threadPool());
client.execute(GetBucketsAction.INSTANCE, request, listener);
return listener;
}
public void getCategories(GetCategoriesAction.Request request,
ActionListener<GetCategoriesAction.Response> listener) {
client.execute(GetCategoriesAction.INSTANCE, request, listener); client.execute(GetCategoriesAction.INSTANCE, request, listener);
} }
public void getDatafeeds(GetDatafeedsAction.Request request, ActionListener<GetDatafeedsAction.Response> listener) { public ActionFuture<GetCategoriesAction.Response> getCategories(
GetCategoriesAction.Request request) {
PlainListenableActionFuture<GetCategoriesAction.Response> listener =
new PlainListenableActionFuture<>(client.threadPool());
client.execute(GetCategoriesAction.INSTANCE, request, listener);
return listener;
}
public void getDatafeeds(GetDatafeedsAction.Request request,
ActionListener<GetDatafeedsAction.Response> listener) {
client.execute(GetDatafeedsAction.INSTANCE, request, listener); client.execute(GetDatafeedsAction.INSTANCE, request, listener);
} }
public void getDatafeedsStats(GetDatafeedsStatsAction.Request request, ActionListener<GetDatafeedsStatsAction.Response> listener) { public ActionFuture<GetDatafeedsAction.Response> getDatafeeds(
GetDatafeedsAction.Request request) {
PlainListenableActionFuture<GetDatafeedsAction.Response> listener =
new PlainListenableActionFuture<>(client.threadPool());
client.execute(GetDatafeedsAction.INSTANCE, request, listener);
return listener;
}
public void getDatafeedsStats(GetDatafeedsStatsAction.Request request,
ActionListener<GetDatafeedsStatsAction.Response> listener) {
client.execute(GetDatafeedsStatsAction.INSTANCE, request, listener); client.execute(GetDatafeedsStatsAction.INSTANCE, request, listener);
} }
public void getFilters(GetFiltersAction.Request request, ActionListener<GetFiltersAction.Response> listener) { public ActionFuture<GetDatafeedsStatsAction.Response> getDatafeedsStats(
GetDatafeedsStatsAction.Request request) {
PlainListenableActionFuture<GetDatafeedsStatsAction.Response> listener =
new PlainListenableActionFuture<>(client.threadPool());
client.execute(GetDatafeedsStatsAction.INSTANCE, request, listener);
return listener;
}
public void getFilters(GetFiltersAction.Request request,
ActionListener<GetFiltersAction.Response> listener) {
client.execute(GetFiltersAction.INSTANCE, request, listener); client.execute(GetFiltersAction.INSTANCE, request, listener);
} }
public void getInfluencers(GetInfluencersAction.Request request, ActionListener<GetInfluencersAction.Response> listener) { public ActionFuture<GetFiltersAction.Response> getFilters(GetFiltersAction.Request request) {
PlainListenableActionFuture<GetFiltersAction.Response> listener =
new PlainListenableActionFuture<>(client.threadPool());
client.execute(GetFiltersAction.INSTANCE, request, listener);
return listener;
}
public void getInfluencers(GetInfluencersAction.Request request,
ActionListener<GetInfluencersAction.Response> listener) {
client.execute(GetInfluencersAction.INSTANCE, request, listener); client.execute(GetInfluencersAction.INSTANCE, request, listener);
} }
public void getJobs(GetJobsAction.Request request, ActionListener<GetJobsAction.Response> listener) { public ActionFuture<GetInfluencersAction.Response> getInfluencers(
GetInfluencersAction.Request request) {
PlainListenableActionFuture<GetInfluencersAction.Response> listener =
new PlainListenableActionFuture<>(client.threadPool());
client.execute(GetInfluencersAction.INSTANCE, request, listener);
return listener;
}
public void getJobs(GetJobsAction.Request request,
ActionListener<GetJobsAction.Response> listener) {
client.execute(GetJobsAction.INSTANCE, request, listener); client.execute(GetJobsAction.INSTANCE, request, listener);
} }
public void getJobsStats(GetJobsStatsAction.Request request, ActionListener<GetJobsStatsAction.Response> listener) { public ActionFuture<GetJobsAction.Response> getJobs(GetJobsAction.Request request) {
PlainListenableActionFuture<GetJobsAction.Response> listener =
new PlainListenableActionFuture<>(client.threadPool());
client.execute(GetJobsAction.INSTANCE, request, listener);
return listener;
}
public void getJobsStats(GetJobsStatsAction.Request request,
ActionListener<GetJobsStatsAction.Response> listener) {
client.execute(GetJobsStatsAction.INSTANCE, request, listener); client.execute(GetJobsStatsAction.INSTANCE, request, listener);
} }
public void getModelSnapshots(GetModelSnapshotsAction.Request request, ActionListener<GetModelSnapshotsAction.Response> listener) { public ActionFuture<GetJobsStatsAction.Response> getJobsStats(
GetJobsStatsAction.Request request) {
PlainListenableActionFuture<GetJobsStatsAction.Response> listener =
new PlainListenableActionFuture<>(client.threadPool());
client.execute(GetJobsStatsAction.INSTANCE, request, listener);
return listener;
}
public void getModelSnapshots(GetModelSnapshotsAction.Request request,
ActionListener<GetModelSnapshotsAction.Response> listener) {
client.execute(GetModelSnapshotsAction.INSTANCE, request, listener); client.execute(GetModelSnapshotsAction.INSTANCE, request, listener);
} }
public void getRecords(GetRecordsAction.Request request, ActionListener<GetRecordsAction.Response> listener) { public ActionFuture<GetModelSnapshotsAction.Response> getModelSnapshots(
GetModelSnapshotsAction.Request request) {
PlainListenableActionFuture<GetModelSnapshotsAction.Response> listener =
new PlainListenableActionFuture<>(client.threadPool());
client.execute(GetModelSnapshotsAction.INSTANCE, request, listener);
return listener;
}
public void getRecords(GetRecordsAction.Request request,
ActionListener<GetRecordsAction.Response> listener) {
client.execute(GetRecordsAction.INSTANCE, request, listener); client.execute(GetRecordsAction.INSTANCE, request, listener);
} }
public void openJob(OpenJobAction.Request request, ActionListener<OpenJobAction.Response> listener) { public ActionFuture<GetRecordsAction.Response> getRecords(GetRecordsAction.Request request) {
PlainListenableActionFuture<GetRecordsAction.Response> listener =
new PlainListenableActionFuture<>(client.threadPool());
client.execute(GetRecordsAction.INSTANCE, request, listener);
return listener;
}
public void openJob(OpenJobAction.Request request,
ActionListener<OpenJobAction.Response> listener) {
client.execute(OpenJobAction.INSTANCE, request, listener); client.execute(OpenJobAction.INSTANCE, request, listener);
} }
public void postData(PostDataAction.Request request, ActionListener<PostDataAction.Response> listener) { public ActionFuture<OpenJobAction.Response> openJob(OpenJobAction.Request request) {
PlainListenableActionFuture<OpenJobAction.Response> listener =
new PlainListenableActionFuture<>(client.threadPool());
client.execute(OpenJobAction.INSTANCE, request, listener);
return listener;
}
public void postData(PostDataAction.Request request,
ActionListener<PostDataAction.Response> listener) {
client.execute(PostDataAction.INSTANCE, request, listener); client.execute(PostDataAction.INSTANCE, request, listener);
} }
public void putDatafeed(PutDatafeedAction.Request request, ActionListener<PutDatafeedAction.Response> listener) { public ActionFuture<PostDataAction.Response> postData(PostDataAction.Request request) {
PlainListenableActionFuture<PostDataAction.Response> listener =
new PlainListenableActionFuture<>(client.threadPool());
client.execute(PostDataAction.INSTANCE, request, listener);
return listener;
}
public void putDatafeed(PutDatafeedAction.Request request,
ActionListener<PutDatafeedAction.Response> listener) {
client.execute(PutDatafeedAction.INSTANCE, request, listener); client.execute(PutDatafeedAction.INSTANCE, request, listener);
} }
public void putFilter(PutFilterAction.Request request, ActionListener<PutFilterAction.Response> listener) { public ActionFuture<PutDatafeedAction.Response> putDatafeed(PutDatafeedAction.Request request) {
PlainListenableActionFuture<PutDatafeedAction.Response> listener =
new PlainListenableActionFuture<>(client.threadPool());
client.execute(PutDatafeedAction.INSTANCE, request, listener);
return listener;
}
public void putFilter(PutFilterAction.Request request,
ActionListener<PutFilterAction.Response> listener) {
client.execute(PutFilterAction.INSTANCE, request, listener); client.execute(PutFilterAction.INSTANCE, request, listener);
} }
public void putJob(PutJobAction.Request request, ActionListener<PutJobAction.Response> listener) { public ActionFuture<PutFilterAction.Response> putFilter(PutFilterAction.Request request) {
PlainListenableActionFuture<PutFilterAction.Response> listener =
new PlainListenableActionFuture<>(client.threadPool());
client.execute(PutFilterAction.INSTANCE, request, listener);
return listener;
}
public void putJob(PutJobAction.Request request,
ActionListener<PutJobAction.Response> listener) {
client.execute(PutJobAction.INSTANCE, request, listener); client.execute(PutJobAction.INSTANCE, request, listener);
} }
public ActionFuture<PutJobAction.Response> putJob(PutJobAction.Request request) {
PlainListenableActionFuture<PutJobAction.Response> listener =
new PlainListenableActionFuture<>(client.threadPool());
client.execute(PutJobAction.INSTANCE, request, listener);
return listener;
}
public void revertModelSnapshot(RevertModelSnapshotAction.Request request, public void revertModelSnapshot(RevertModelSnapshotAction.Request request,
ActionListener<RevertModelSnapshotAction.Response> listener) { ActionListener<RevertModelSnapshotAction.Response> listener) {
client.execute(RevertModelSnapshotAction.INSTANCE, request, listener); client.execute(RevertModelSnapshotAction.INSTANCE, request, listener);
} }
public void startDatafeed(StartDatafeedAction.Request request, ActionListener<StartDatafeedAction.Response> listener) { public ActionFuture<RevertModelSnapshotAction.Response> revertModelSnapshot(
RevertModelSnapshotAction.Request request) {
PlainListenableActionFuture<RevertModelSnapshotAction.Response> listener =
new PlainListenableActionFuture<>(client.threadPool());
client.execute(RevertModelSnapshotAction.INSTANCE, request, listener);
return listener;
}
public void startDatafeed(StartDatafeedAction.Request request,
ActionListener<StartDatafeedAction.Response> listener) {
client.execute(StartDatafeedAction.INSTANCE, request, listener); client.execute(StartDatafeedAction.INSTANCE, request, listener);
} }
public void stopDatafeed(StopDatafeedAction.Request request, ActionListener<StopDatafeedAction.Response> listener) { public ActionFuture<StartDatafeedAction.Response> startDatafeed(
StartDatafeedAction.Request request) {
PlainListenableActionFuture<StartDatafeedAction.Response> listener =
new PlainListenableActionFuture<>(client.threadPool());
client.execute(StartDatafeedAction.INSTANCE, request, listener);
return listener;
}
public void stopDatafeed(StopDatafeedAction.Request request,
ActionListener<StopDatafeedAction.Response> listener) {
client.execute(StopDatafeedAction.INSTANCE, request, listener); client.execute(StopDatafeedAction.INSTANCE, request, listener);
} }
public void updateDatafeed(UpdateDatafeedAction.Request request, ActionListener<PutDatafeedAction.Response> listener) { public ActionFuture<StopDatafeedAction.Response> stopDatafeed(
StopDatafeedAction.Request request) {
PlainListenableActionFuture<StopDatafeedAction.Response> listener =
new PlainListenableActionFuture<>(client.threadPool());
client.execute(StopDatafeedAction.INSTANCE, request, listener);
return listener;
}
public void updateDatafeed(UpdateDatafeedAction.Request request,
ActionListener<PutDatafeedAction.Response> listener) {
client.execute(UpdateDatafeedAction.INSTANCE, request, listener); client.execute(UpdateDatafeedAction.INSTANCE, request, listener);
} }
public void updateJob(UpdateJobAction.Request request, ActionListener<PutJobAction.Response> listener) { public ActionFuture<PutDatafeedAction.Response> updateDatafeed(
UpdateDatafeedAction.Request request) {
PlainListenableActionFuture<PutDatafeedAction.Response> listener =
new PlainListenableActionFuture<>(client.threadPool());
client.execute(UpdateDatafeedAction.INSTANCE, request, listener);
return listener;
}
public void updateJob(UpdateJobAction.Request request,
ActionListener<PutJobAction.Response> listener) {
client.execute(UpdateJobAction.INSTANCE, request, listener); client.execute(UpdateJobAction.INSTANCE, request, listener);
} }
public ActionFuture<PutJobAction.Response> updateJob(UpdateJobAction.Request request) {
PlainListenableActionFuture<PutJobAction.Response> listener =
new PlainListenableActionFuture<>(client.threadPool());
client.execute(UpdateJobAction.INSTANCE, request, listener);
return listener;
}
public void updateModelSnapshot(UpdateModelSnapshotAction.Request request, public void updateModelSnapshot(UpdateModelSnapshotAction.Request request,
ActionListener<UpdateModelSnapshotAction.Response> listener) { ActionListener<UpdateModelSnapshotAction.Response> listener) {
client.execute(UpdateModelSnapshotAction.INSTANCE, request, listener); client.execute(UpdateModelSnapshotAction.INSTANCE, request, listener);
} }
public ActionFuture<UpdateModelSnapshotAction.Response> updateModelSnapshot(
UpdateModelSnapshotAction.Request request) {
PlainListenableActionFuture<UpdateModelSnapshotAction.Response> listener =
new PlainListenableActionFuture<>(client.threadPool());
client.execute(UpdateModelSnapshotAction.INSTANCE, request, listener);
return listener;
}
} }

View File

@ -7,8 +7,6 @@ dependencies {
integTestCluster { integTestCluster {
setting 'xpack.security.enabled', 'false' setting 'xpack.security.enabled', 'false'
setting 'xpack.ml.enabled', 'true'
setting 'script.inline', 'true' setting 'script.inline', 'true'
distribution = 'zip'
plugin ':x-pack-elasticsearch:plugin' plugin ':x-pack-elasticsearch:plugin'
} }

View File

@ -0,0 +1,13 @@
apply plugin: 'elasticsearch.standalone-rest-test'
apply plugin: 'elasticsearch.rest-test'
dependencies {
testCompile project(path: ':x-pack-elasticsearch:plugin', configuration: 'runtime')
testCompile project(path: ':x-pack-elasticsearch:transport-client', configuration: 'runtime')
}
integTestCluster {
setting 'xpack.security.enabled', 'false'
setting 'script.inline', 'true'
plugin ':x-pack-elasticsearch:plugin'
}

View File

@ -0,0 +1,154 @@
/*
* 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.ml.client;
import org.apache.logging.log4j.Logger;
import org.apache.lucene.util.LuceneTestCase;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.logging.ESLoggerFactory;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.env.Environment;
import org.elasticsearch.xpack.client.PreBuiltXPackTransportClient;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.URL;
import java.nio.file.Path;
import java.util.Locale;
import java.util.concurrent.atomic.AtomicInteger;
import static com.carrotsearch.randomizedtesting.RandomizedTest.randomAsciiOfLength;
import static org.hamcrest.Matchers.notNullValue;
/**
* An abstract base class to run integration tests against an Elasticsearch
* cluster running outside of the test process.
* <p>
* You can define a list of transport addresses from where you can reach your
* cluster by setting "tests.cluster" system property. It defaults to
* "localhost:9300". If you run this from `gradle integTest` then it will start
* the clsuter for you and set up the property.
* <p>
* If you want to debug this module from your IDE, then start an external
* cluster by yourself, maybe with `gradle run`, then run JUnit. If you changed
* the default port, set "-Dtests.cluster=localhost:PORT" when running your
* test.
*/
@LuceneTestCase.SuppressSysoutChecks(bugUrl = "we log a lot on purpose")
public abstract class ESXPackSmokeClientTestCase extends LuceneTestCase {
/**
* Key used to eventually switch to using an external cluster and provide
* its transport addresses
*/
public static final String TESTS_CLUSTER = "tests.cluster";
protected static final Logger logger = ESLoggerFactory
.getLogger(ESXPackSmokeClientTestCase.class.getName());
private static final AtomicInteger counter = new AtomicInteger();
private static Client client;
private static String clusterAddresses;
protected String index;
private static Client startClient(Path tempDir, TransportAddress... transportAddresses) {
Settings.Builder builder = Settings.builder()
.put("node.name", "qa_xpack_smoke_client_" + counter.getAndIncrement())
.put("client.transport.ignore_cluster_name", true)
.put("xpack.security.enabled", false)
.put(Environment.PATH_HOME_SETTING.getKey(), tempDir);
TransportClient client = new PreBuiltXPackTransportClient(builder.build())
.addTransportAddresses(transportAddresses);
logger.info("--> Elasticsearch Java TransportClient started");
Exception clientException = null;
try {
ClusterHealthResponse health = client.admin().cluster().prepareHealth().get();
logger.info("--> connected to [{}] cluster which is running [{}] node(s).",
health.getClusterName(), health.getNumberOfNodes());
} catch (Exception e) {
logger.error("Error getting cluster health", e);
clientException = e;
}
assumeNoException("Sounds like your cluster is not running at " + clusterAddresses,
clientException);
return client;
}
private static Client startClient() throws IOException {
String[] stringAddresses = clusterAddresses.split(",");
TransportAddress[] transportAddresses = new TransportAddress[stringAddresses.length];
int i = 0;
for (String stringAddress : stringAddresses) {
URL url = new URL("http://" + stringAddress);
InetAddress inetAddress = InetAddress.getByName(url.getHost());
transportAddresses[i++] = new TransportAddress(
new InetSocketAddress(inetAddress, url.getPort()));
}
return startClient(createTempDir(), transportAddresses);
}
public static Client getClient() {
if (client == null) {
try {
client = startClient();
} catch (IOException e) {
logger.error("can not start the client", e);
}
assertThat(client, notNullValue());
}
return client;
}
@BeforeClass
public static void initializeSettings() {
clusterAddresses = System.getProperty(TESTS_CLUSTER);
if (clusterAddresses == null || clusterAddresses.isEmpty()) {
fail("Must specify " + TESTS_CLUSTER + " for smoke client test");
}
}
@AfterClass
public static void stopTransportClient() {
if (client != null) {
client.close();
client = null;
}
}
@Before
public void defineIndexName() {
doClean();
index = "qa-xpack-smoke-test-client-"
+ randomAsciiOfLength(10).toLowerCase(Locale.getDefault());
}
@After
public void cleanIndex() {
doClean();
}
private void doClean() {
if (client != null) {
try {
client.admin().indices().prepareDelete(index).get();
} catch (Exception e) {
// We ignore this cleanup exception
}
}
}
}

View File

@ -0,0 +1,65 @@
/*
* 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.ml.client;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.xpack.XPackClient;
import org.elasticsearch.xpack.ml.action.DeleteJobAction;
import org.elasticsearch.xpack.ml.action.GetJobsAction;
import org.elasticsearch.xpack.ml.action.PutJobAction;
import org.elasticsearch.xpack.ml.job.config.AnalysisConfig;
import org.elasticsearch.xpack.ml.job.config.Detector;
import org.elasticsearch.xpack.ml.job.config.Job;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.notNullValue;
public class MLTransportClientIT extends ESXPackSmokeClientTestCase {
public void testMLTransportClient() {
Client client = getClient();
XPackClient xPackClient = new XPackClient(client);
MachineLearningClient mlClient = xPackClient.machineLearning();
Job.Builder job = new Job.Builder();
job.setId("test");
job.setCreateTime(new Date());
List<Detector> detectors = new ArrayList<>();
Detector.Builder detector = new Detector.Builder();
detector.setFunction("count");
detectors.add(detector.build());
AnalysisConfig.Builder analysisConfig = new AnalysisConfig.Builder(detectors);
analysisConfig.setBatchSpan(TimeValue.timeValueMinutes(5));
job.setAnalysisConfig(analysisConfig);
PutJobAction.Response putJobResponse = mlClient
.putJob(new PutJobAction.Request(job.build()))
.actionGet();
assertThat(putJobResponse, notNullValue());
assertThat(putJobResponse.isAcknowledged(), equalTo(true));
GetJobsAction.Response getJobResponse = mlClient.getJobs(new GetJobsAction.Request("test"))
.actionGet();
assertThat(getJobResponse, notNullValue());
assertThat(getJobResponse.getResponse(), notNullValue());
assertThat(getJobResponse.getResponse().count(), equalTo(1L));
DeleteJobAction.Response deleteJobResponse = mlClient
.deleteJob(new DeleteJobAction.Request("test"))
.actionGet();
assertThat(deleteJobResponse, notNullValue());
assertThat(deleteJobResponse.isAcknowledged(), equalTo(true));
}
}