Skip to content

Commit 256e463

Browse files
author
Hendrik Muhs
committed
add test cases for _usage
1 parent d493e3d commit 256e463

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

x-pack/plugin/ml/qa/native-multi-node-tests/src/test/java/org/elasticsearch/xpack/ml/integration/MlJobIT.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.elasticsearch.common.util.concurrent.ConcurrentCollections;
1414
import org.elasticsearch.common.util.concurrent.ConcurrentMapLong;
1515
import org.elasticsearch.common.util.concurrent.ThreadContext;
16+
import org.elasticsearch.common.xcontent.support.XContentMapValues;
1617
import org.elasticsearch.test.SecuritySettingsSourceField;
1718
import org.elasticsearch.test.rest.ESRestTestCase;
1819
import org.elasticsearch.xpack.core.ml.integration.MlRestTestStateCleaner;
@@ -22,7 +23,9 @@
2223
import org.junit.After;
2324

2425
import java.io.IOException;
26+
import java.util.Collections;
2527
import java.util.Locale;
28+
import java.util.Map;
2629
import java.util.concurrent.atomic.AtomicInteger;
2730
import java.util.concurrent.atomic.AtomicReference;
2831
import java.util.regex.Matcher;
@@ -111,6 +114,21 @@ public void testGetJobs_GivenMultipleJobs() throws Exception {
111114
assertThat(implicitAll, containsString("\"job_id\":\"given-multiple-jobs-job-3\""));
112115
}
113116

117+
// tests the _xpack/usage endpoint
118+
public void testUsage() throws IOException {
119+
createFarequoteJob("job-1");
120+
createFarequoteJob("job-2");
121+
Map<String, Object> usage = entityAsMap(client().performRequest(new Request("GET", "_xpack/usage")));
122+
assertEquals(2, XContentMapValues.extractValue("ml.jobs._all.count", usage));
123+
assertEquals(2, XContentMapValues.extractValue("ml.jobs.closed.count", usage));
124+
Response openResponse = client().performRequest(new Request("POST", MachineLearning.BASE_PATH + "anomaly_detectors/job-1/_open"));
125+
assertEquals(Collections.singletonMap("opened", true), entityAsMap(openResponse));
126+
usage = entityAsMap(client().performRequest(new Request("GET", "_xpack/usage")));
127+
assertEquals(2, XContentMapValues.extractValue("ml.jobs._all.count", usage));
128+
assertEquals(1, XContentMapValues.extractValue("ml.jobs.closed.count", usage));
129+
assertEquals(1, XContentMapValues.extractValue("ml.jobs.opened.count", usage));
130+
}
131+
114132
private Response createFarequoteJob(String jobId) throws IOException {
115133
Request request = new Request("PUT", MachineLearning.BASE_PATH + "anomaly_detectors/" + jobId);
116134
request.setJsonEntity(

x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/MachineLearningFeatureSetTests.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,29 @@ public void testUsage() throws Exception {
240240
}
241241
}
242242

243+
public void testUsageDisabledML() throws Exception {
244+
when(licenseState.isMachineLearningAllowed()).thenReturn(true);
245+
Settings.Builder settings = Settings.builder().put(commonSettings);
246+
settings.put("xpack.ml.enabled", false);
247+
248+
JobManagerHolder emptyJobManagerHolder = new JobManagerHolder();
249+
MachineLearningFeatureSet featureSet = new MachineLearningFeatureSet(TestEnvironment.newEnvironment(settings.build()),
250+
clusterService, client, licenseState, emptyJobManagerHolder);
251+
PlainActionFuture<Usage> future = new PlainActionFuture<>();
252+
featureSet.usage(future);
253+
XPackFeatureSet.Usage mlUsage = future.get();
254+
BytesStreamOutput out = new BytesStreamOutput();
255+
mlUsage.writeTo(out);
256+
XPackFeatureSet.Usage serializedUsage = new MachineLearningFeatureSetUsage(out.bytes().streamInput());
257+
258+
for (XPackFeatureSet.Usage usage : Arrays.asList(mlUsage, serializedUsage)) {
259+
assertThat(usage, is(notNullValue()));
260+
assertThat(usage.name(), is(XPackField.MACHINE_LEARNING));
261+
assertThat(usage.enabled(), is(false));
262+
assertThat(usage.available(), is(false));
263+
}
264+
}
265+
243266
public void testNodeCount() throws Exception {
244267
when(licenseState.isMachineLearningAllowed()).thenReturn(true);
245268
int nodeCount = randomIntBetween(1, 3);

0 commit comments

Comments
 (0)