Skip to content

Commit fbec19c

Browse files
authored
Centralize mocks initialization in ILM steps tests (#51384) (#51453)
* Centralize mocks initialization in ILM steps tests This change centralizes initialization of `Client`, `AdminClient` and `IndicesAdminClient` for all classes extending `AbstractStepTestCase`. This removes a lot of code duplication and make it easier to write tests. This also removes need for `AsyncActionStep#setClient` * Unused imports removed * Added missed tests * Fix OpenFollowerIndexStepTests
1 parent 8560847 commit fbec19c

20 files changed

+168
-524
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/AsyncActionStep.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,6 @@ protected Client getClient() {
2929
return client;
3030
}
3131

32-
//visible for testing
33-
void setClient(Client client){
34-
this.client = client;
35-
}
36-
3732
public static TimeValue getMasterTimeout(ClusterState clusterState){
3833
Objects.requireNonNull(clusterState, "cannot determine master timeout when cluster state is null");
3934
return LifecycleSettings.LIFECYCLE_STEP_MASTER_TIMEOUT_SETTING.get(clusterState.metaData().settings());

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/AbstractStepMasterTimeoutTestCase.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import org.junit.After;
2323
import org.junit.Before;
2424

25+
import java.util.concurrent.atomic.AtomicBoolean;
26+
2527
import static org.elasticsearch.xpack.core.ilm.LifecycleSettings.LIFECYCLE_STEP_MASTER_TIMEOUT;
2628
import static org.hamcrest.Matchers.equalTo;
2729

@@ -51,18 +53,19 @@ public void testMasterTimeout() {
5153
}
5254

5355
private void checkMasterTimeout(TimeValue timeValue, ClusterState currentClusterState) {
54-
T instance = createRandomInstance();
55-
instance.setClient(new NoOpClient(pool) {
56+
AtomicBoolean timeoutChecked = new AtomicBoolean();
57+
client = new NoOpClient(pool) {
5658
@Override
5759
protected <Request extends ActionRequest, Response extends ActionResponse> void doExecute(ActionType<Response> action,
5860
Request request,
5961
ActionListener<Response> listener) {
6062
if (request instanceof MasterNodeRequest) {
6163
assertThat(((MasterNodeRequest<?>) request).masterNodeTimeout(), equalTo(timeValue));
64+
timeoutChecked.set(true);
6265
}
6366
}
64-
});
65-
instance.performAction(getIndexMetaData(), currentClusterState, null, new AsyncActionStep.Listener() {
67+
};
68+
createRandomInstance().performAction(getIndexMetaData(), currentClusterState, null, new AsyncActionStep.Listener() {
6669
@Override
6770
public void onResponse(boolean complete) {
6871

@@ -73,6 +76,7 @@ public void onFailure(Exception e) {
7376

7477
}
7578
});
79+
assertTrue(timeoutChecked.get());
7680
}
7781

7882
protected abstract IndexMetaData getIndexMetaData();

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/AbstractStepTestCase.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,32 @@
55
*/
66
package org.elasticsearch.xpack.core.ilm;
77

8+
import org.elasticsearch.client.AdminClient;
9+
import org.elasticsearch.client.Client;
10+
import org.elasticsearch.client.IndicesAdminClient;
811
import org.elasticsearch.common.unit.TimeValue;
912
import org.elasticsearch.test.ESTestCase;
1013
import org.elasticsearch.test.EqualsHashCodeTestUtils;
1114
import org.elasticsearch.xpack.core.ilm.Step.StepKey;
15+
import org.junit.Before;
16+
import org.mockito.Mockito;
1217

1318
public abstract class AbstractStepTestCase<T extends Step> extends ESTestCase {
1419

20+
protected Client client;
21+
protected AdminClient adminClient;
22+
protected IndicesAdminClient indicesClient;
23+
24+
@Before
25+
public void setupClient() {
26+
client = Mockito.mock(Client.class);
27+
adminClient = Mockito.mock(AdminClient.class);
28+
indicesClient = Mockito.mock(IndicesAdminClient.class);
29+
30+
Mockito.when(client.admin()).thenReturn(adminClient);
31+
Mockito.when(adminClient.indices()).thenReturn(indicesClient);
32+
}
33+
1534
protected static final int NUMBER_OF_TEST_RUNS = 20;
1635
protected static final TimeValue MASTER_TIMEOUT = TimeValue.timeValueSeconds(30);
1736

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/AbstractUnfollowIndexStepTestCase.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
package org.elasticsearch.xpack.core.ilm;
77

88
import org.elasticsearch.Version;
9-
import org.elasticsearch.client.Client;
109
import org.elasticsearch.cluster.metadata.IndexMetaData;
1110
import org.mockito.Mockito;
1211

@@ -19,7 +18,7 @@ public abstract class AbstractUnfollowIndexStepTestCase<T extends AbstractUnfoll
1918
protected final T createRandomInstance() {
2019
Step.StepKey stepKey = randomStepKey();
2120
Step.StepKey nextStepKey = randomStepKey();
22-
return newInstance(stepKey, nextStepKey, Mockito.mock(Client.class));
21+
return newInstance(stepKey, nextStepKey);
2322
}
2423

2524
@Override
@@ -33,12 +32,12 @@ protected final T mutateInstance(T instance) {
3332
nextKey = new Step.StepKey(key.getPhase(), key.getAction(), key.getName() + randomAlphaOfLength(5));
3433
}
3534

36-
return newInstance(key, nextKey, instance.getClient());
35+
return newInstance(key, nextKey);
3736
}
3837

3938
@Override
4039
protected final T copyInstance(T instance) {
41-
return newInstance(instance.getKey(), instance.getNextStepKey(), instance.getClient());
40+
return newInstance(instance.getKey(), instance.getNextStepKey());
4241
}
4342

4443
public final void testNotAFollowerIndex() {
@@ -48,8 +47,7 @@ public final void testNotAFollowerIndex() {
4847
.numberOfReplicas(0)
4948
.build();
5049

51-
Client client = Mockito.mock(Client.class);
52-
T step = newInstance(randomStepKey(), randomStepKey(), client);
50+
T step = newInstance(randomStepKey(), randomStepKey());
5351

5452
Boolean[] completed = new Boolean[1];
5553
Exception[] failure = new Exception[1];
@@ -69,5 +67,5 @@ public void onFailure(Exception e) {
6967
Mockito.verifyZeroInteractions(client);
7068
}
7169

72-
protected abstract T newInstance(Step.StepKey key, Step.StepKey nextKey, Client client);
70+
protected abstract T newInstance(Step.StepKey key, Step.StepKey nextKey);
7371
}

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/CloseFollowerIndexStepTests.java

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@
99
import org.elasticsearch.action.ActionListener;
1010
import org.elasticsearch.action.admin.indices.close.CloseIndexRequest;
1111
import org.elasticsearch.action.admin.indices.close.CloseIndexResponse;
12-
import org.elasticsearch.client.AdminClient;
13-
import org.elasticsearch.client.Client;
14-
import org.elasticsearch.client.IndicesAdminClient;
1512
import org.elasticsearch.cluster.metadata.IndexMetaData;
1613
import org.mockito.Mockito;
1714

@@ -38,12 +35,6 @@ protected IndexMetaData getIndexMetaData() {
3835
public void testCloseFollowingIndex() {
3936
IndexMetaData indexMetadata = getIndexMetaData();
4037

41-
Client client = Mockito.mock(Client.class);
42-
AdminClient adminClient = Mockito.mock(AdminClient.class);
43-
Mockito.when(client.admin()).thenReturn(adminClient);
44-
IndicesAdminClient indicesClient = Mockito.mock(IndicesAdminClient.class);
45-
Mockito.when(adminClient.indices()).thenReturn(indicesClient);
46-
4738
Mockito.doAnswer(invocation -> {
4839
CloseIndexRequest closeIndexRequest = (CloseIndexRequest) invocation.getArguments()[0];
4940
assertThat(closeIndexRequest.indices()[0], equalTo("follower-index"));
@@ -75,12 +66,6 @@ public void testCloseFollowingIndexFailed() {
7566
IndexMetaData indexMetadata = getIndexMetaData();
7667

7768
// Mock pause follow api call:
78-
Client client = Mockito.mock(Client.class);
79-
AdminClient adminClient = Mockito.mock(AdminClient.class);
80-
Mockito.when(client.admin()).thenReturn(adminClient);
81-
IndicesAdminClient indicesClient = Mockito.mock(IndicesAdminClient.class);
82-
Mockito.when(adminClient.indices()).thenReturn(indicesClient);
83-
8469
Exception error = new RuntimeException();
8570
Mockito.doAnswer(invocation -> {
8671
CloseIndexRequest closeIndexRequest = (CloseIndexRequest) invocation.getArguments()[0];
@@ -118,7 +103,6 @@ public void testCloseFollowerIndexIsNoopForAlreadyClosedIndex() {
118103
.numberOfShards(1)
119104
.numberOfReplicas(0)
120105
.build();
121-
Client client = Mockito.mock(Client.class);
122106
CloseFollowerIndexStep step = new CloseFollowerIndexStep(randomStepKey(), randomStepKey(), client);
123107
step.performAction(indexMetadata, null, null, new AsyncActionStep.Listener() {
124108
@Override
@@ -138,7 +122,7 @@ public void onFailure(Exception e) {
138122
protected CloseFollowerIndexStep createRandomInstance() {
139123
Step.StepKey stepKey = randomStepKey();
140124
Step.StepKey nextStepKey = randomStepKey();
141-
return new CloseFollowerIndexStep(stepKey, nextStepKey, Mockito.mock(Client.class));
125+
return new CloseFollowerIndexStep(stepKey, nextStepKey, client);
142126
}
143127

144128
@Override

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/DeleteStepTests.java

Lines changed: 9 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,14 @@
1111
import org.elasticsearch.action.ActionListener;
1212
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
1313
import org.elasticsearch.action.support.master.AcknowledgedResponse;
14-
import org.elasticsearch.client.AdminClient;
15-
import org.elasticsearch.client.Client;
16-
import org.elasticsearch.client.IndicesAdminClient;
1714
import org.elasticsearch.cluster.metadata.IndexMetaData;
1815
import org.elasticsearch.xpack.core.ilm.Step.StepKey;
19-
import org.junit.Before;
2016
import org.mockito.Mockito;
21-
import org.mockito.invocation.InvocationOnMock;
22-
import org.mockito.stubbing.Answer;
2317

2418
import static org.hamcrest.Matchers.equalTo;
2519

2620
public class DeleteStepTests extends AbstractStepMasterTimeoutTestCase<DeleteStep> {
2721

28-
private Client client;
29-
30-
@Before
31-
public void setup() {
32-
client = Mockito.mock(Client.class);
33-
}
34-
3522
@Override
3623
public DeleteStep createRandomInstance() {
3724
StepKey stepKey = randomStepKey();
@@ -77,11 +64,6 @@ public void testIndexSurvives() {
7764
public void testDeleted() {
7865
IndexMetaData indexMetaData = getIndexMetaData();
7966

80-
AdminClient adminClient = Mockito.mock(AdminClient.class);
81-
IndicesAdminClient indicesClient = Mockito.mock(IndicesAdminClient.class);
82-
83-
Mockito.when(client.admin()).thenReturn(adminClient);
84-
Mockito.when(adminClient.indices()).thenReturn(indicesClient);
8567
Mockito.doAnswer(invocation -> {
8668
DeleteIndexRequest request = (DeleteIndexRequest) invocation.getArguments()[0];
8769
@SuppressWarnings("unchecked")
@@ -119,25 +101,15 @@ public void testExceptionThrown() {
119101
IndexMetaData indexMetaData = getIndexMetaData();
120102
Exception exception = new RuntimeException();
121103

122-
AdminClient adminClient = Mockito.mock(AdminClient.class);
123-
IndicesAdminClient indicesClient = Mockito.mock(IndicesAdminClient.class);
124-
125-
Mockito.when(client.admin()).thenReturn(adminClient);
126-
Mockito.when(adminClient.indices()).thenReturn(indicesClient);
127-
Mockito.doAnswer(new Answer<Void>() {
128-
129-
@Override
130-
public Void answer(InvocationOnMock invocation) throws Throwable {
131-
DeleteIndexRequest request = (DeleteIndexRequest) invocation.getArguments()[0];
132-
@SuppressWarnings("unchecked")
133-
ActionListener<AcknowledgedResponse> listener = (ActionListener<AcknowledgedResponse>) invocation.getArguments()[1];
134-
assertNotNull(request);
135-
assertEquals(1, request.indices().length);
136-
assertEquals(indexMetaData.getIndex().getName(), request.indices()[0]);
137-
listener.onFailure(exception);
138-
return null;
139-
}
140-
104+
Mockito.doAnswer(invocation -> {
105+
DeleteIndexRequest request = (DeleteIndexRequest) invocation.getArguments()[0];
106+
@SuppressWarnings("unchecked")
107+
ActionListener<AcknowledgedResponse> listener = (ActionListener<AcknowledgedResponse>) invocation.getArguments()[1];
108+
assertNotNull(request);
109+
assertEquals(1, request.indices().length);
110+
assertEquals(indexMetaData.getIndex().getName(), request.indices()[0]);
111+
listener.onFailure(exception);
112+
return null;
141113
}).when(indicesClient).delete(Mockito.any(), Mockito.any());
142114

143115
SetOnce<Boolean> exceptionThrown = new SetOnce<>();

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/ForceMergeStepTests.java

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,13 @@
1111
import org.elasticsearch.action.ActionListener;
1212
import org.elasticsearch.action.admin.indices.forcemerge.ForceMergeRequest;
1313
import org.elasticsearch.action.admin.indices.forcemerge.ForceMergeResponse;
14-
import org.elasticsearch.client.AdminClient;
15-
import org.elasticsearch.client.Client;
16-
import org.elasticsearch.client.IndicesAdminClient;
1714
import org.elasticsearch.cluster.metadata.IndexMetaData;
1815
import org.elasticsearch.rest.RestStatus;
1916
import org.elasticsearch.xpack.core.ilm.Step.StepKey;
2017
import org.mockito.Mockito;
2118

2219
import static org.hamcrest.Matchers.equalTo;
2320
import static org.mockito.Matchers.any;
24-
import static org.mockito.Mockito.mock;
25-
import static org.mockito.Mockito.when;
2621

2722
public class ForceMergeStepTests extends AbstractStepTestCase<ForceMergeStep> {
2823

@@ -70,11 +65,6 @@ public void testPerformActionComplete() {
7065
Step.StepKey stepKey = randomStepKey();
7166
StepKey nextStepKey = randomStepKey();
7267
int maxNumSegments = randomIntBetween(1, 10);
73-
Client client = mock(Client.class);
74-
AdminClient adminClient = mock(AdminClient.class);
75-
IndicesAdminClient indicesClient = mock(IndicesAdminClient.class);
76-
when(client.admin()).thenReturn(adminClient);
77-
when(adminClient.indices()).thenReturn(indicesClient);
7868
ForceMergeResponse forceMergeResponse = Mockito.mock(ForceMergeResponse.class);
7969
Mockito.when(forceMergeResponse.getStatus()).thenReturn(RestStatus.OK);
8070
Mockito.doAnswer(invocationOnMock -> {
@@ -109,11 +99,6 @@ public void testPerformActionThrowsException() {
10999
Step.StepKey stepKey = randomStepKey();
110100
StepKey nextStepKey = randomStepKey();
111101
int maxNumSegments = randomIntBetween(1, 10);
112-
Client client = mock(Client.class);
113-
AdminClient adminClient = mock(AdminClient.class);
114-
IndicesAdminClient indicesClient = mock(IndicesAdminClient.class);
115-
when(client.admin()).thenReturn(adminClient);
116-
when(adminClient.indices()).thenReturn(indicesClient);
117102
ForceMergeResponse forceMergeResponse = Mockito.mock(ForceMergeResponse.class);
118103
Mockito.when(forceMergeResponse.getStatus()).thenReturn(RestStatus.OK);
119104
Mockito.doAnswer(invocationOnMock -> {

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ilm/FreezeStepTests.java

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,16 @@
1010
import org.elasticsearch.Version;
1111
import org.elasticsearch.action.ActionListener;
1212
import org.elasticsearch.action.support.master.AcknowledgedResponse;
13-
import org.elasticsearch.client.AdminClient;
14-
import org.elasticsearch.client.Client;
15-
import org.elasticsearch.client.IndicesAdminClient;
1613
import org.elasticsearch.cluster.metadata.IndexMetaData;
1714
import org.elasticsearch.protocol.xpack.frozen.FreezeRequest;
1815
import org.elasticsearch.xpack.core.frozen.action.FreezeIndexAction;
1916
import org.elasticsearch.xpack.core.ilm.Step.StepKey;
20-
import org.junit.Before;
2117
import org.mockito.Mockito;
22-
import org.mockito.stubbing.Answer;
2318

2419
import static org.hamcrest.Matchers.equalTo;
2520

2621
public class FreezeStepTests extends AbstractStepMasterTimeoutTestCase<FreezeStep> {
2722

28-
private Client client;
29-
30-
@Before
31-
public void setup() {
32-
client = Mockito.mock(Client.class);
33-
}
34-
3523
@Override
3624
public FreezeStep createRandomInstance() {
3725
StepKey stepKey = randomStepKey();
@@ -77,11 +65,6 @@ public void testIndexSurvives() {
7765
public void testFreeze() {
7866
IndexMetaData indexMetaData = getIndexMetaData();
7967

80-
AdminClient adminClient = Mockito.mock(AdminClient.class);
81-
IndicesAdminClient indicesClient = Mockito.mock(IndicesAdminClient.class);
82-
83-
Mockito.when(client.admin()).thenReturn(adminClient);
84-
Mockito.when(adminClient.indices()).thenReturn(indicesClient);
8568
Mockito.doAnswer(invocation -> {
8669
assertSame(invocation.getArguments()[0], FreezeIndexAction.INSTANCE);
8770
FreezeRequest request = (FreezeRequest) invocation.getArguments()[1];
@@ -120,12 +103,7 @@ public void testExceptionThrown() {
120103
IndexMetaData indexMetaData = getIndexMetaData();
121104
Exception exception = new RuntimeException();
122105

123-
AdminClient adminClient = Mockito.mock(AdminClient.class);
124-
IndicesAdminClient indicesClient = Mockito.mock(IndicesAdminClient.class);
125-
126-
Mockito.when(client.admin()).thenReturn(adminClient);
127-
Mockito.when(adminClient.indices()).thenReturn(indicesClient);
128-
Mockito.doAnswer((Answer<Void>) invocation -> {
106+
Mockito.doAnswer(invocation -> {
129107
@SuppressWarnings("unchecked")
130108
ActionListener<AcknowledgedResponse> listener = (ActionListener<AcknowledgedResponse>) invocation.getArguments()[2];
131109
listener.onFailure(exception);

0 commit comments

Comments
 (0)