Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ protected Client getClient() {
return client;
}

//visible for testing
void setClient(Client client){
this.client = client;
}

public static TimeValue getMasterTimeout(ClusterState clusterState){
Objects.requireNonNull(clusterState, "cannot determine master timeout when cluster state is null");
return LifecycleSettings.LIFECYCLE_STEP_MASTER_TIMEOUT_SETTING.get(clusterState.metaData().settings());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import org.junit.After;
import org.junit.Before;

import java.util.concurrent.atomic.AtomicBoolean;

import static org.elasticsearch.xpack.core.ilm.LifecycleSettings.LIFECYCLE_STEP_MASTER_TIMEOUT;
import static org.hamcrest.Matchers.equalTo;

Expand Down Expand Up @@ -51,18 +53,19 @@ public void testMasterTimeout() {
}

private void checkMasterTimeout(TimeValue timeValue, ClusterState currentClusterState) {
T instance = createRandomInstance();
instance.setClient(new NoOpClient(pool) {
AtomicBoolean timeoutChecked = new AtomicBoolean();
client = new NoOpClient(pool) {
@Override
protected <Request extends ActionRequest, Response extends ActionResponse> void doExecute(ActionType<Response> action,
Request request,
ActionListener<Response> listener) {
if (request instanceof MasterNodeRequest) {
assertThat(((MasterNodeRequest<?>) request).masterNodeTimeout(), equalTo(timeValue));
timeoutChecked.set(true);
}
}
});
instance.performAction(getIndexMetaData(), currentClusterState, null, new AsyncActionStep.Listener() {
};
createRandomInstance().performAction(getIndexMetaData(), currentClusterState, null, new AsyncActionStep.Listener() {
@Override
public void onResponse(boolean complete) {

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

}
});
assertTrue(timeoutChecked.get());
}

protected abstract IndexMetaData getIndexMetaData();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,32 @@
*/
package org.elasticsearch.xpack.core.ilm;

import org.elasticsearch.client.AdminClient;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.IndicesAdminClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.test.EqualsHashCodeTestUtils;
import org.elasticsearch.xpack.core.ilm.Step.StepKey;
import org.junit.Before;
import org.mockito.Mockito;

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

protected Client client;
protected AdminClient adminClient;
protected IndicesAdminClient indicesClient;

@Before
public void setupClient() {
client = Mockito.mock(Client.class);
adminClient = Mockito.mock(AdminClient.class);
indicesClient = Mockito.mock(IndicesAdminClient.class);

Mockito.when(client.admin()).thenReturn(adminClient);
Mockito.when(adminClient.indices()).thenReturn(indicesClient);
}

protected static final int NUMBER_OF_TEST_RUNS = 20;
protected static final TimeValue MASTER_TIMEOUT = TimeValue.timeValueSeconds(30);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package org.elasticsearch.xpack.core.ilm;

import org.elasticsearch.Version;
import org.elasticsearch.client.Client;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.mockito.Mockito;

Expand All @@ -19,7 +18,7 @@ public abstract class AbstractUnfollowIndexStepTestCase<T extends AbstractUnfoll
protected final T createRandomInstance() {
Step.StepKey stepKey = randomStepKey();
Step.StepKey nextStepKey = randomStepKey();
return newInstance(stepKey, nextStepKey, Mockito.mock(Client.class));
return newInstance(stepKey, nextStepKey);
}

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

return newInstance(key, nextKey, instance.getClient());
return newInstance(key, nextKey);
}

@Override
protected final T copyInstance(T instance) {
return newInstance(instance.getKey(), instance.getNextStepKey(), instance.getClient());
return newInstance(instance.getKey(), instance.getNextStepKey());
}

public final void testNotAFollowerIndex() {
Expand All @@ -48,8 +47,7 @@ public final void testNotAFollowerIndex() {
.numberOfReplicas(0)
.build();

Client client = Mockito.mock(Client.class);
T step = newInstance(randomStepKey(), randomStepKey(), client);
T step = newInstance(randomStepKey(), randomStepKey());

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

protected abstract T newInstance(Step.StepKey key, Step.StepKey nextKey, Client client);
protected abstract T newInstance(Step.StepKey key, Step.StepKey nextKey);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.admin.indices.close.CloseIndexRequest;
import org.elasticsearch.action.admin.indices.close.CloseIndexResponse;
import org.elasticsearch.client.AdminClient;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.IndicesAdminClient;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.mockito.Mockito;

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

Client client = Mockito.mock(Client.class);
AdminClient adminClient = Mockito.mock(AdminClient.class);
Mockito.when(client.admin()).thenReturn(adminClient);
IndicesAdminClient indicesClient = Mockito.mock(IndicesAdminClient.class);
Mockito.when(adminClient.indices()).thenReturn(indicesClient);

Mockito.doAnswer(invocation -> {
CloseIndexRequest closeIndexRequest = (CloseIndexRequest) invocation.getArguments()[0];
assertThat(closeIndexRequest.indices()[0], equalTo("follower-index"));
Expand Down Expand Up @@ -75,12 +66,6 @@ public void testCloseFollowingIndexFailed() {
IndexMetaData indexMetadata = getIndexMetaData();

// Mock pause follow api call:
Client client = Mockito.mock(Client.class);
AdminClient adminClient = Mockito.mock(AdminClient.class);
Mockito.when(client.admin()).thenReturn(adminClient);
IndicesAdminClient indicesClient = Mockito.mock(IndicesAdminClient.class);
Mockito.when(adminClient.indices()).thenReturn(indicesClient);

Exception error = new RuntimeException();
Mockito.doAnswer(invocation -> {
CloseIndexRequest closeIndexRequest = (CloseIndexRequest) invocation.getArguments()[0];
Expand Down Expand Up @@ -118,7 +103,6 @@ public void testCloseFollowerIndexIsNoopForAlreadyClosedIndex() {
.numberOfShards(1)
.numberOfReplicas(0)
.build();
Client client = Mockito.mock(Client.class);
CloseFollowerIndexStep step = new CloseFollowerIndexStep(randomStepKey(), randomStepKey(), client);
step.performAction(indexMetadata, null, null, new AsyncActionStep.Listener() {
@Override
Expand All @@ -138,7 +122,7 @@ public void onFailure(Exception e) {
protected CloseFollowerIndexStep createRandomInstance() {
Step.StepKey stepKey = randomStepKey();
Step.StepKey nextStepKey = randomStepKey();
return new CloseFollowerIndexStep(stepKey, nextStepKey, Mockito.mock(Client.class));
return new CloseFollowerIndexStep(stepKey, nextStepKey, client);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,14 @@
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.AdminClient;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.IndicesAdminClient;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.xpack.core.ilm.Step.StepKey;
import org.junit.Before;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

import static org.hamcrest.Matchers.equalTo;

public class DeleteStepTests extends AbstractStepMasterTimeoutTestCase<DeleteStep> {

private Client client;

@Before
public void setup() {
client = Mockito.mock(Client.class);
}

@Override
public DeleteStep createRandomInstance() {
StepKey stepKey = randomStepKey();
Expand Down Expand Up @@ -77,11 +64,6 @@ public void testIndexSurvives() {
public void testDeleted() {
IndexMetaData indexMetaData = getIndexMetaData();

AdminClient adminClient = Mockito.mock(AdminClient.class);
IndicesAdminClient indicesClient = Mockito.mock(IndicesAdminClient.class);

Mockito.when(client.admin()).thenReturn(adminClient);
Mockito.when(adminClient.indices()).thenReturn(indicesClient);
Mockito.doAnswer(invocation -> {
DeleteIndexRequest request = (DeleteIndexRequest) invocation.getArguments()[0];
@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -119,25 +101,15 @@ public void testExceptionThrown() {
IndexMetaData indexMetaData = getIndexMetaData();
Exception exception = new RuntimeException();

AdminClient adminClient = Mockito.mock(AdminClient.class);
IndicesAdminClient indicesClient = Mockito.mock(IndicesAdminClient.class);

Mockito.when(client.admin()).thenReturn(adminClient);
Mockito.when(adminClient.indices()).thenReturn(indicesClient);
Mockito.doAnswer(new Answer<Void>() {

@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
DeleteIndexRequest request = (DeleteIndexRequest) invocation.getArguments()[0];
@SuppressWarnings("unchecked")
ActionListener<AcknowledgedResponse> listener = (ActionListener<AcknowledgedResponse>) invocation.getArguments()[1];
assertNotNull(request);
assertEquals(1, request.indices().length);
assertEquals(indexMetaData.getIndex().getName(), request.indices()[0]);
listener.onFailure(exception);
return null;
}

Mockito.doAnswer(invocation -> {
DeleteIndexRequest request = (DeleteIndexRequest) invocation.getArguments()[0];
@SuppressWarnings("unchecked")
ActionListener<AcknowledgedResponse> listener = (ActionListener<AcknowledgedResponse>) invocation.getArguments()[1];
assertNotNull(request);
assertEquals(1, request.indices().length);
assertEquals(indexMetaData.getIndex().getName(), request.indices()[0]);
listener.onFailure(exception);
return null;
}).when(indicesClient).delete(Mockito.any(), Mockito.any());

SetOnce<Boolean> exceptionThrown = new SetOnce<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,13 @@
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.admin.indices.forcemerge.ForceMergeRequest;
import org.elasticsearch.action.admin.indices.forcemerge.ForceMergeResponse;
import org.elasticsearch.client.AdminClient;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.IndicesAdminClient;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.xpack.core.ilm.Step.StepKey;
import org.mockito.Mockito;

import static org.hamcrest.Matchers.equalTo;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class ForceMergeStepTests extends AbstractStepTestCase<ForceMergeStep> {

Expand Down Expand Up @@ -70,11 +65,6 @@ public void testPerformActionComplete() {
Step.StepKey stepKey = randomStepKey();
StepKey nextStepKey = randomStepKey();
int maxNumSegments = randomIntBetween(1, 10);
Client client = mock(Client.class);
AdminClient adminClient = mock(AdminClient.class);
IndicesAdminClient indicesClient = mock(IndicesAdminClient.class);
when(client.admin()).thenReturn(adminClient);
when(adminClient.indices()).thenReturn(indicesClient);
ForceMergeResponse forceMergeResponse = Mockito.mock(ForceMergeResponse.class);
Mockito.when(forceMergeResponse.getStatus()).thenReturn(RestStatus.OK);
Mockito.doAnswer(invocationOnMock -> {
Expand Down Expand Up @@ -109,11 +99,6 @@ public void testPerformActionThrowsException() {
Step.StepKey stepKey = randomStepKey();
StepKey nextStepKey = randomStepKey();
int maxNumSegments = randomIntBetween(1, 10);
Client client = mock(Client.class);
AdminClient adminClient = mock(AdminClient.class);
IndicesAdminClient indicesClient = mock(IndicesAdminClient.class);
when(client.admin()).thenReturn(adminClient);
when(adminClient.indices()).thenReturn(indicesClient);
ForceMergeResponse forceMergeResponse = Mockito.mock(ForceMergeResponse.class);
Mockito.when(forceMergeResponse.getStatus()).thenReturn(RestStatus.OK);
Mockito.doAnswer(invocationOnMock -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,16 @@
import org.elasticsearch.Version;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.AdminClient;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.IndicesAdminClient;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.protocol.xpack.frozen.FreezeRequest;
import org.elasticsearch.xpack.core.frozen.action.FreezeIndexAction;
import org.elasticsearch.xpack.core.ilm.Step.StepKey;
import org.junit.Before;
import org.mockito.Mockito;
import org.mockito.stubbing.Answer;

import static org.hamcrest.Matchers.equalTo;

public class FreezeStepTests extends AbstractStepMasterTimeoutTestCase<FreezeStep> {

private Client client;

@Before
public void setup() {
client = Mockito.mock(Client.class);
}

@Override
public FreezeStep createRandomInstance() {
StepKey stepKey = randomStepKey();
Expand Down Expand Up @@ -77,11 +65,6 @@ public void testIndexSurvives() {
public void testFreeze() {
IndexMetaData indexMetaData = getIndexMetaData();

AdminClient adminClient = Mockito.mock(AdminClient.class);
IndicesAdminClient indicesClient = Mockito.mock(IndicesAdminClient.class);

Mockito.when(client.admin()).thenReturn(adminClient);
Mockito.when(adminClient.indices()).thenReturn(indicesClient);
Mockito.doAnswer(invocation -> {
assertSame(invocation.getArguments()[0], FreezeIndexAction.INSTANCE);
FreezeRequest request = (FreezeRequest) invocation.getArguments()[1];
Expand Down Expand Up @@ -120,12 +103,7 @@ public void testExceptionThrown() {
IndexMetaData indexMetaData = getIndexMetaData();
Exception exception = new RuntimeException();

AdminClient adminClient = Mockito.mock(AdminClient.class);
IndicesAdminClient indicesClient = Mockito.mock(IndicesAdminClient.class);

Mockito.when(client.admin()).thenReturn(adminClient);
Mockito.when(adminClient.indices()).thenReturn(indicesClient);
Mockito.doAnswer((Answer<Void>) invocation -> {
Mockito.doAnswer(invocation -> {
@SuppressWarnings("unchecked")
ActionListener<AcknowledgedResponse> listener = (ActionListener<AcknowledgedResponse>) invocation.getArguments()[2];
listener.onFailure(exception);
Expand Down
Loading