Skip to content

Commit

Permalink
Dry up getting cluster admin client in tests
Browse files Browse the repository at this point in the history
Drying this up further and adding the same short-cut for single node
tests. Dealing with most of the spots that I could grab via automatic
refactorings.
  • Loading branch information
original-brownbear committed Jun 20, 2023
1 parent 3f8ee82 commit 407d86e
Show file tree
Hide file tree
Showing 203 changed files with 730 additions and 1,246 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -587,14 +587,14 @@ public void testResolvabilityOfDataStreamsInAPIs() throws Exception {
false
);
verifyResolvability(dataStreamName, indicesAdmin().prepareGetSettings(dataStreamName), false);
verifyResolvability(dataStreamName, client().admin().cluster().prepareHealth(dataStreamName), false);
verifyResolvability(dataStreamName, client().admin().cluster().prepareState().setIndices(dataStreamName), false);
verifyResolvability(dataStreamName, clusterAdmin().prepareHealth(dataStreamName), false);
verifyResolvability(dataStreamName, clusterAdmin().prepareState().setIndices(dataStreamName), false);
verifyResolvability(dataStreamName, client().prepareFieldCaps(dataStreamName).setFields("*"), false);
verifyResolvability(dataStreamName, indicesAdmin().prepareGetIndex().addIndices(dataStreamName), false);
verifyResolvability(dataStreamName, indicesAdmin().prepareOpen(dataStreamName), false);
verifyResolvability(dataStreamName, indicesAdmin().prepareClose(dataStreamName), true);
verifyResolvability(aliasToDataStream, indicesAdmin().prepareClose(aliasToDataStream), true);
verifyResolvability(dataStreamName, client().admin().cluster().prepareSearchShards(dataStreamName), false);
verifyResolvability(dataStreamName, clusterAdmin().prepareSearchShards(dataStreamName), false);
verifyResolvability(dataStreamName, indicesAdmin().prepareShardStores(dataStreamName), false);

request = new CreateDataStreamAction.Request("logs-barbaz");
Expand Down Expand Up @@ -633,13 +633,13 @@ public void testResolvabilityOfDataStreamsInAPIs() throws Exception {
indicesAdmin().prepareUpdateSettings(wildcardExpression).setSettings(Settings.builder().put("index.number_of_replicas", 0)),
false
);
verifyResolvability(wildcardExpression, client().admin().cluster().prepareHealth(wildcardExpression), false);
verifyResolvability(wildcardExpression, client().admin().cluster().prepareState().setIndices(wildcardExpression), false);
verifyResolvability(wildcardExpression, clusterAdmin().prepareHealth(wildcardExpression), false);
verifyResolvability(wildcardExpression, clusterAdmin().prepareState().setIndices(wildcardExpression), false);
verifyResolvability(wildcardExpression, client().prepareFieldCaps(wildcardExpression).setFields("*"), false);
verifyResolvability(wildcardExpression, indicesAdmin().prepareGetIndex().addIndices(wildcardExpression), false);
verifyResolvability(wildcardExpression, indicesAdmin().prepareOpen(wildcardExpression), false);
verifyResolvability(wildcardExpression, indicesAdmin().prepareClose(wildcardExpression), false);
verifyResolvability(wildcardExpression, client().admin().cluster().prepareSearchShards(wildcardExpression), false);
verifyResolvability(wildcardExpression, clusterAdmin().prepareSearchShards(wildcardExpression), false);
verifyResolvability(wildcardExpression, indicesAdmin().prepareShardStores(wildcardExpression), false);
}

Expand Down Expand Up @@ -1512,7 +1512,7 @@ public void testClusterStateIncludeDataStream() throws Exception {

// when querying a backing index then the data stream should be included as well.
ClusterStateRequest request = new ClusterStateRequest().indices(".ds-metrics-foo-*000001");
ClusterState state = client().admin().cluster().state(request).get().getState();
ClusterState state = clusterAdmin().state(request).get().getState();
assertThat(state.metadata().dataStreams().size(), equalTo(1));
assertThat(state.metadata().dataStreams().get("metrics-foo").getName(), equalTo("metrics-foo"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -823,15 +823,15 @@ public void testDataStreamNotIncludedInLimitedSnapshot() throws ExecutionExcepti
is(true)
);

final RestoreSnapshotResponse restoreSnapshotResponse = client().admin().cluster().prepareRestoreSnapshot(REPO, snapshotName).get();
final RestoreSnapshotResponse restoreSnapshotResponse = clusterAdmin().prepareRestoreSnapshot(REPO, snapshotName).get();
assertThat(restoreSnapshotResponse.getRestoreInfo().indices(), empty());
}

public void testDeleteDataStreamDuringSnapshot() throws Exception {
Client client1 = client();

// this test uses a MockRepository
assertAcked(client().admin().cluster().prepareDeleteRepository(REPO));
assertAcked(clusterAdmin().prepareDeleteRepository(REPO));

final String repositoryName = "test-repo";
createRepository(
Expand Down Expand Up @@ -910,11 +910,7 @@ public void testCloneSnapshotThatIncludesDataStream() throws Exception {
.execute()
);
assertAcked(
client().admin()
.cluster()
.prepareCloneSnapshot(REPO, sourceSnapshotName, "target-snapshot-1")
.setIndices(indexWithoutDataStream)
.get()
clusterAdmin().prepareCloneSnapshot(REPO, sourceSnapshotName, "target-snapshot-1").setIndices(indexWithoutDataStream).get()
);
}

Expand Down Expand Up @@ -944,9 +940,7 @@ public void testSnapshotDSDuringRollover() throws Exception {
final boolean partial = randomBoolean();
blockAllDataNodes(repoName);
final String snapshotName = "ds-snap";
final ActionFuture<CreateSnapshotResponse> snapshotFuture = client().admin()
.cluster()
.prepareCreateSnapshot(repoName, snapshotName)
final ActionFuture<CreateSnapshotResponse> snapshotFuture = clusterAdmin().prepareCreateSnapshot(repoName, snapshotName)
.setWaitForCompletion(true)
.setPartial(partial)
.setIncludeGlobalState(randomBoolean())
Expand All @@ -967,9 +961,7 @@ public void testSnapshotDSDuringRollover() throws Exception {
assertThat(snapshotInfo.dataStreams(), hasItems("ds"));
assertAcked(client().execute(DeleteDataStreamAction.INSTANCE, new DeleteDataStreamAction.Request(new String[] { "ds" })).get());

RestoreInfo restoreSnapshotResponse = client().admin()
.cluster()
.prepareRestoreSnapshot(repoName, snapshotName)
RestoreInfo restoreSnapshotResponse = clusterAdmin().prepareRestoreSnapshot(repoName, snapshotName)
.setWaitForCompletion(true)
.setIndices("ds")
.get()
Expand All @@ -986,9 +978,7 @@ public void testSnapshotDSDuringRolloverAndDeleteOldIndex() throws Exception {
createRepository(repoName, "mock");
blockAllDataNodes(repoName);
final String snapshotName = "ds-snap";
final ActionFuture<CreateSnapshotResponse> snapshotFuture = client().admin()
.cluster()
.prepareCreateSnapshot(repoName, snapshotName)
final ActionFuture<CreateSnapshotResponse> snapshotFuture = clusterAdmin().prepareCreateSnapshot(repoName, snapshotName)
.setWaitForCompletion(true)
.setPartial(true)
.setIncludeGlobalState(randomBoolean())
Expand All @@ -1013,9 +1003,7 @@ public void testSnapshotDSDuringRolloverAndDeleteOldIndex() throws Exception {
client().execute(DeleteDataStreamAction.INSTANCE, new DeleteDataStreamAction.Request(new String[] { "other-ds" })).get()
);

RestoreInfo restoreSnapshotResponse = client().admin()
.cluster()
.prepareRestoreSnapshot(repoName, snapshotName)
RestoreInfo restoreSnapshotResponse = clusterAdmin().prepareRestoreSnapshot(repoName, snapshotName)
.setWaitForCompletion(true)
.setIndices("other-ds")
.get()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,7 @@ public void testSystemDataStreamInGlobalState() throws Exception {
}

assertSuccessful(
client().admin()
.cluster()
.prepareCreateSnapshot(REPO, SNAPSHOT)
.setWaitForCompletion(true)
.setIncludeGlobalState(true)
.execute()
clusterAdmin().prepareCreateSnapshot(REPO, SNAPSHOT).setWaitForCompletion(true).setIncludeGlobalState(true).execute()
);

// We have to delete the data stream directly, as the feature reset API doesn't clean up system data streams yet
Expand All @@ -104,9 +99,7 @@ public void testSystemDataStreamInGlobalState() throws Exception {
// Make sure requesting the data stream by name throws.
// For some reason, expectThrows() isn't working for me here, hence the try/catch.
try {
client().admin()
.cluster()
.prepareRestoreSnapshot(REPO, SNAPSHOT)
clusterAdmin().prepareRestoreSnapshot(REPO, SNAPSHOT)
.setIndices(".test-data-stream")
.setWaitForCompletion(true)
.setRestoreGlobalState(randomBoolean()) // this shouldn't matter
Expand All @@ -125,9 +118,7 @@ public void testSystemDataStreamInGlobalState() throws Exception {
assertSystemDataStreamDoesNotExist();

// Now actually restore the data stream
RestoreSnapshotResponse restoreSnapshotResponse = client().admin()
.cluster()
.prepareRestoreSnapshot(REPO, SNAPSHOT)
RestoreSnapshotResponse restoreSnapshotResponse = clusterAdmin().prepareRestoreSnapshot(REPO, SNAPSHOT)
.setWaitForCompletion(true)
.setRestoreGlobalState(true)
.get();
Expand All @@ -142,7 +133,7 @@ public void testSystemDataStreamInGlobalState() throws Exception {

// Attempting to restore again without specifying indices or global/feature states should work, as the wildcard should not be
// resolved to system indices/data streams.
client().admin().cluster().prepareRestoreSnapshot(REPO, SNAPSHOT).setWaitForCompletion(true).setRestoreGlobalState(false).get();
clusterAdmin().prepareRestoreSnapshot(REPO, SNAPSHOT).setWaitForCompletion(true).setRestoreGlobalState(false).get();
assertEquals(restoreSnapshotResponse.getRestoreInfo().totalShards(), restoreSnapshotResponse.getRestoreInfo().successfulShards());
}

Expand Down Expand Up @@ -196,9 +187,7 @@ public void testSystemDataStreamInFeatureState() throws Exception {
}

SnapshotInfo snapshotInfo = assertSuccessful(
client().admin()
.cluster()
.prepareCreateSnapshot(REPO, SNAPSHOT)
clusterAdmin().prepareCreateSnapshot(REPO, SNAPSHOT)
.setIndices("my-index")
.setFeatureStates(SystemDataStreamTestPlugin.class.getSimpleName())
.setWaitForCompletion(true)
Expand All @@ -223,9 +212,7 @@ public void testSystemDataStreamInFeatureState() throws Exception {
assertThat(indicesRemaining.indices(), arrayWithSize(0));
}

RestoreSnapshotResponse restoreSnapshotResponse = client().admin()
.cluster()
.prepareRestoreSnapshot(REPO, SNAPSHOT)
RestoreSnapshotResponse restoreSnapshotResponse = clusterAdmin().prepareRestoreSnapshot(REPO, SNAPSHOT)
.setWaitForCompletion(true)
.setIndices("my-index")
.setFeatureStates(SystemDataStreamTestPlugin.class.getSimpleName())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,9 @@ public void testStatsClosedBackingIndexDataStream() throws Exception {
assertTrue(indicesAdmin().rolloverIndex(new RolloverRequest(dataStreamName, null)).get().isAcknowledged());
assertTrue(indicesAdmin().close(new CloseIndexRequest(".ds-" + dataStreamName + "-*-000001")).actionGet().isAcknowledged());

assertBusy(() -> {
assertNotEquals(ClusterHealthStatus.RED, client().admin().cluster().health(new ClusterHealthRequest()).actionGet().getStatus());
});
assertBusy(
() -> assertNotEquals(ClusterHealthStatus.RED, clusterAdmin().health(new ClusterHealthRequest()).actionGet().getStatus())
);

DataStreamsStatsAction.Response stats = getDataStreamsStats();
assertEquals(2, stats.getSuccessfulShards());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public void testLookAheadTimeSettingHigherThanTimeSeriesPollIntervalSetting() th
}

private void updateClusterSettings(Settings settings) {
client().admin().cluster().updateSettings(new ClusterUpdateSettingsRequest().persistentSettings(settings)).actionGet();
clusterAdmin().updateSettings(new ClusterUpdateSettingsRequest().persistentSettings(settings)).actionGet();
}

private void updateIndexSettings(Settings settings) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void testFailureInConditionalProcessor() {
internalCluster().ensureAtLeastNumDataNodes(1);
internalCluster().startMasterOnlyNode();
final String pipelineId = "foo";
client().admin().cluster().preparePutPipeline(pipelineId, new BytesArray(Strings.format("""
clusterAdmin().preparePutPipeline(pipelineId, new BytesArray(Strings.format("""
{
"processors": [
{
Expand Down Expand Up @@ -95,7 +95,7 @@ public void testFailureInConditionalProcessor() {
);
assertTrue(e.getMessage().contains("this script always fails"));

NodesStatsResponse r = client().admin().cluster().prepareNodesStats(internalCluster().getNodeNames()).setIngest(true).get();
NodesStatsResponse r = clusterAdmin().prepareNodesStats(internalCluster().getNodeNames()).setIngest(true).get();
int nodeCount = r.getNodes().size();
for (int k = 0; k < nodeCount; k++) {
List<IngestStats.ProcessorStat> stats = r.getNodes().get(k).getIngestStats().processorStats().get(pipelineId);
Expand All @@ -120,12 +120,12 @@ public void testScriptDisabled() throws Exception {
}""");

Consumer<String> checkPipelineExists = (id) -> assertThat(
client().admin().cluster().prepareGetPipeline(id).get().pipelines().get(0).getId(),
clusterAdmin().prepareGetPipeline(id).get().pipelines().get(0).getId(),
equalTo(id)
);

client().admin().cluster().preparePutPipeline(pipelineIdWithScript, pipelineWithScript, XContentType.JSON).get();
client().admin().cluster().preparePutPipeline(pipelineIdWithoutScript, pipelineWithoutScript, XContentType.JSON).get();
clusterAdmin().preparePutPipeline(pipelineIdWithScript, pipelineWithScript, XContentType.JSON).get();
clusterAdmin().preparePutPipeline(pipelineIdWithoutScript, pipelineWithoutScript, XContentType.JSON).get();

checkPipelineExists.accept(pipelineIdWithScript);
checkPipelineExists.accept(pipelineIdWithoutScript);
Expand Down Expand Up @@ -180,7 +180,7 @@ public Settings onNodeStopped(String nodeName) {
public void testPipelineWithScriptProcessorThatHasStoredScript() throws Exception {
internalCluster().startNode();

client().admin().cluster().preparePutStoredScript().setId("1").setContent(new BytesArray(Strings.format("""
clusterAdmin().preparePutStoredScript().setId("1").setContent(new BytesArray(Strings.format("""
{"script": {"lang": "%s", "source": "my_script"} }
""", MockScriptEngine.NAME)), XContentType.JSON).get();
BytesReference pipeline = new BytesArray("""
Expand All @@ -190,7 +190,7 @@ public void testPipelineWithScriptProcessorThatHasStoredScript() throws Exceptio
{"script" : {"id": "1"}}
]
}""");
client().admin().cluster().preparePutPipeline("_id", pipeline, XContentType.JSON).get();
clusterAdmin().preparePutPipeline("_id", pipeline, XContentType.JSON).get();

client().prepareIndex("index")
.setId("1")
Expand Down Expand Up @@ -234,7 +234,7 @@ public void testWithDedicatedIngestNode() throws Exception {
{"set" : {"field": "y", "value": 0}}
]
}""");
client().admin().cluster().preparePutPipeline("_id", pipeline, XContentType.JSON).get();
clusterAdmin().preparePutPipeline("_id", pipeline, XContentType.JSON).get();

client().prepareIndex("index")
.setId("1")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ private void putGeoIpPipeline(String pipelineId, boolean downloadDatabaseOnPipel
builder.endObject();
bytes = BytesReference.bytes(builder);
}
assertAcked(client().admin().cluster().preparePutPipeline(pipelineId, bytes, XContentType.JSON).get());
assertAcked(clusterAdmin().preparePutPipeline(pipelineId, bytes, XContentType.JSON).get());
}

/**
Expand Down Expand Up @@ -592,7 +592,7 @@ private void putNonGeoipPipeline(String pipelineId) throws IOException {
builder.endObject();
bytes = BytesReference.bytes(builder);
}
assertAcked(client().admin().cluster().preparePutPipeline(pipelineId, bytes, XContentType.JSON).get());
assertAcked(clusterAdmin().preparePutPipeline(pipelineId, bytes, XContentType.JSON).get());
}

private List<Path> getGeoIpTmpDirs() throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ private void putPipeline() throws IOException {
builder.endObject();
bytes = BytesReference.bytes(builder);
}
assertAcked(client().admin().cluster().preparePutPipeline("_id", bytes, XContentType.JSON).get());
assertAcked(clusterAdmin().preparePutPipeline("_id", bytes, XContentType.JSON).get());
}

public static Map<String, Object> convertToMap(ToXContent part) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,12 @@ public void testTaskRemovedAfterCancellation() throws Exception {
assertTrue(task.isAssigned());
});
assertBusy(() -> {
ListTasksResponse tasks = client().admin()
.cluster()
.listTasks(new ListTasksRequest().setActions("geoip-downloader[c]"))
.actionGet();
ListTasksResponse tasks = clusterAdmin().listTasks(new ListTasksRequest().setActions("geoip-downloader[c]")).actionGet();
assertEquals(1, tasks.getTasks().size());
});
updateClusterSettings(Settings.builder().put(GeoIpDownloaderTaskExecutor.ENABLED_SETTING.getKey(), false));
assertBusy(() -> {
ListTasksResponse tasks2 = client().admin()
.cluster()
.listTasks(new ListTasksRequest().setActions("geoip-downloader[c]"))
.actionGet();
ListTasksResponse tasks2 = clusterAdmin().listTasks(new ListTasksRequest().setActions("geoip-downloader[c]")).actionGet();
assertEquals(0, tasks2.getTasks().size());
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void testLazyLoading() throws IOException {
builder.endObject();
bytes = BytesReference.bytes(builder);
}
assertAcked(client().admin().cluster().putPipeline(new PutPipelineRequest("geoip", bytes, XContentType.JSON)).actionGet());
assertAcked(clusterAdmin().putPipeline(new PutPipelineRequest("geoip", bytes, XContentType.JSON)).actionGet());
// the geo-IP databases should not be loaded on any nodes as they are all non-ingest nodes
Arrays.stream(internalCluster().getNodeNames()).forEach(node -> assertDatabaseLoadStatus(node, false));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ protected Collection<Class<? extends Plugin>> nodePlugins() {
}

public void testAllOpsDisabledIndexedScripts() throws IOException {
client().admin().cluster().preparePutStoredScript().setId("script1").setContent(new BytesArray("""
clusterAdmin().preparePutStoredScript().setId("script1").setContent(new BytesArray("""
{"script": {"lang": "expression", "source": "2"} }"""), XContentType.JSON).get();
client().prepareIndex("test").setId("1").setSource("{\"theField\":\"foo\"}", XContentType.JSON).get();
try {
Expand Down
Loading

0 comments on commit 407d86e

Please sign in to comment.