Replace securemock with mock-maker (test support), update Mockito to 3.12.4 (#1332)
Signed-off-by: Andriy Redko <andriy.redko@aiven.io>
This commit is contained in:
parent
3665daf5d0
commit
e9635d6bfe
|
@ -36,9 +36,9 @@ httpasyncclient = 4.1.4
|
||||||
commonslogging = 1.1.3
|
commonslogging = 1.1.3
|
||||||
commonscodec = 1.13
|
commonscodec = 1.13
|
||||||
hamcrest = 2.1
|
hamcrest = 2.1
|
||||||
securemock = 1.2
|
mockito = 3.12.4
|
||||||
mockito = 1.9.5
|
objenesis = 3.2
|
||||||
objenesis = 1.0
|
bytebuddy = 1.11.13
|
||||||
|
|
||||||
# benchmark dependencies
|
# benchmark dependencies
|
||||||
jmh = 1.19
|
jmh = 1.19
|
||||||
|
|
|
@ -53,6 +53,7 @@ dependencies {
|
||||||
testImplementation "org.hamcrest:hamcrest:${versions.hamcrest}"
|
testImplementation "org.hamcrest:hamcrest:${versions.hamcrest}"
|
||||||
testImplementation "org.mockito:mockito-core:${versions.mockito}"
|
testImplementation "org.mockito:mockito-core:${versions.mockito}"
|
||||||
testImplementation "org.objenesis:objenesis:${versions.objenesis}"
|
testImplementation "org.objenesis:objenesis:${versions.objenesis}"
|
||||||
|
testImplementation "net.bytebuddy:byte-buddy:${versions.bytebuddy}"
|
||||||
}
|
}
|
||||||
|
|
||||||
tasks.withType(CheckForbiddenApis).configureEach {
|
tasks.withType(CheckForbiddenApis).configureEach {
|
||||||
|
|
|
@ -101,6 +101,7 @@ import static org.junit.Assert.assertThat;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
import static org.junit.Assert.fail;
|
import static org.junit.Assert.fail;
|
||||||
import static org.mockito.Matchers.any;
|
import static org.mockito.Matchers.any;
|
||||||
|
import static org.mockito.Matchers.nullable;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
import static org.mockito.Mockito.times;
|
import static org.mockito.Mockito.times;
|
||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
|
@ -137,7 +138,7 @@ public class RestClientSingleHostTests extends RestClientTestCase {
|
||||||
static CloseableHttpAsyncClient mockHttpClient(final ExecutorService exec) {
|
static CloseableHttpAsyncClient mockHttpClient(final ExecutorService exec) {
|
||||||
CloseableHttpAsyncClient httpClient = mock(CloseableHttpAsyncClient.class);
|
CloseableHttpAsyncClient httpClient = mock(CloseableHttpAsyncClient.class);
|
||||||
when(httpClient.<HttpResponse>execute(any(HttpAsyncRequestProducer.class), any(HttpAsyncResponseConsumer.class),
|
when(httpClient.<HttpResponse>execute(any(HttpAsyncRequestProducer.class), any(HttpAsyncResponseConsumer.class),
|
||||||
any(HttpClientContext.class), any(FutureCallback.class))).thenAnswer((Answer<Future<HttpResponse>>) invocationOnMock -> {
|
any(HttpClientContext.class), nullable(FutureCallback.class))).thenAnswer((Answer<Future<HttpResponse>>) invocationOnMock -> {
|
||||||
final HttpAsyncRequestProducer requestProducer = (HttpAsyncRequestProducer) invocationOnMock.getArguments()[0];
|
final HttpAsyncRequestProducer requestProducer = (HttpAsyncRequestProducer) invocationOnMock.getArguments()[0];
|
||||||
final FutureCallback<HttpResponse> futureCallback =
|
final FutureCallback<HttpResponse> futureCallback =
|
||||||
(FutureCallback<HttpResponse>) invocationOnMock.getArguments()[3];
|
(FutureCallback<HttpResponse>) invocationOnMock.getArguments()[3];
|
||||||
|
@ -215,7 +216,7 @@ public class RestClientSingleHostTests extends RestClientTestCase {
|
||||||
for (String httpMethod : getHttpMethods()) {
|
for (String httpMethod : getHttpMethods()) {
|
||||||
HttpUriRequest expectedRequest = performRandomRequest(httpMethod);
|
HttpUriRequest expectedRequest = performRandomRequest(httpMethod);
|
||||||
verify(httpClient, times(++times)).<HttpResponse>execute(requestArgumentCaptor.capture(),
|
verify(httpClient, times(++times)).<HttpResponse>execute(requestArgumentCaptor.capture(),
|
||||||
any(HttpAsyncResponseConsumer.class), any(HttpClientContext.class), any(FutureCallback.class));
|
any(HttpAsyncResponseConsumer.class), any(HttpClientContext.class), nullable(FutureCallback.class));
|
||||||
HttpUriRequest actualRequest = (HttpUriRequest)requestArgumentCaptor.getValue().generateRequest();
|
HttpUriRequest actualRequest = (HttpUriRequest)requestArgumentCaptor.getValue().generateRequest();
|
||||||
assertEquals(expectedRequest.getURI(), actualRequest.getURI());
|
assertEquals(expectedRequest.getURI(), actualRequest.getURI());
|
||||||
assertEquals(expectedRequest.getClass(), actualRequest.getClass());
|
assertEquals(expectedRequest.getClass(), actualRequest.getClass());
|
||||||
|
|
|
@ -49,6 +49,7 @@ dependencies {
|
||||||
testImplementation "junit:junit:${versions.junit}"
|
testImplementation "junit:junit:${versions.junit}"
|
||||||
testImplementation "org.mockito:mockito-core:${versions.mockito}"
|
testImplementation "org.mockito:mockito-core:${versions.mockito}"
|
||||||
testImplementation "org.objenesis:objenesis:${versions.objenesis}"
|
testImplementation "org.objenesis:objenesis:${versions.objenesis}"
|
||||||
|
testImplementation "net.bytebuddy:byte-buddy:${versions.bytebuddy}"
|
||||||
}
|
}
|
||||||
|
|
||||||
tasks.named('forbiddenApisMain').configure {
|
tasks.named('forbiddenApisMain').configure {
|
||||||
|
|
|
@ -12,8 +12,8 @@ import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.elasticsearch.mock.orig.Mockito;
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
|
import org.mockito.Mockito;
|
||||||
import org.opensearch.cli.MockTerminal;
|
import org.opensearch.cli.MockTerminal;
|
||||||
import org.opensearch.common.collect.Tuple;
|
import org.opensearch.common.collect.Tuple;
|
||||||
import org.opensearch.common.settings.Settings;
|
import org.opensearch.common.settings.Settings;
|
||||||
|
|
|
@ -52,7 +52,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
import java.util.function.BiConsumer;
|
import java.util.function.BiConsumer;
|
||||||
|
|
||||||
import static org.mockito.Matchers.any;
|
import static org.mockito.Matchers.any;
|
||||||
import static org.mockito.Matchers.anyInt;
|
import static org.mockito.Matchers.anyLong;
|
||||||
import static org.mockito.Matchers.isNull;
|
import static org.mockito.Matchers.isNull;
|
||||||
import static org.mockito.Matchers.same;
|
import static org.mockito.Matchers.same;
|
||||||
import static org.mockito.Mockito.doAnswer;
|
import static org.mockito.Mockito.doAnswer;
|
||||||
|
@ -192,7 +192,7 @@ public class NioSelectorTests extends OpenSearchTestCase {
|
||||||
|
|
||||||
public void testSelectorClosedExceptionIsNotCaughtWhileRunning() throws IOException {
|
public void testSelectorClosedExceptionIsNotCaughtWhileRunning() throws IOException {
|
||||||
boolean closedSelectorExceptionCaught = false;
|
boolean closedSelectorExceptionCaught = false;
|
||||||
when(rawSelector.select(anyInt())).thenThrow(new ClosedSelectorException());
|
when(rawSelector.select(anyLong())).thenThrow(new ClosedSelectorException());
|
||||||
try {
|
try {
|
||||||
this.selector.singleLoop();
|
this.selector.singleLoop();
|
||||||
} catch (ClosedSelectorException e) {
|
} catch (ClosedSelectorException e) {
|
||||||
|
@ -205,7 +205,7 @@ public class NioSelectorTests extends OpenSearchTestCase {
|
||||||
public void testIOExceptionWhileSelect() throws IOException {
|
public void testIOExceptionWhileSelect() throws IOException {
|
||||||
IOException ioException = new IOException();
|
IOException ioException = new IOException();
|
||||||
|
|
||||||
when(rawSelector.select(anyInt())).thenThrow(ioException);
|
when(rawSelector.select(anyLong())).thenThrow(ioException);
|
||||||
|
|
||||||
this.selector.singleLoop();
|
this.selector.singleLoop();
|
||||||
|
|
||||||
|
|
|
@ -41,6 +41,7 @@ import org.apache.lucene.index.NoMergePolicy;
|
||||||
import org.apache.lucene.search.Query;
|
import org.apache.lucene.search.Query;
|
||||||
import org.apache.lucene.search.TermQuery;
|
import org.apache.lucene.search.TermQuery;
|
||||||
import org.apache.lucene.store.Directory;
|
import org.apache.lucene.store.Directory;
|
||||||
|
import org.mockito.Mockito;
|
||||||
import org.opensearch.LegacyESVersion;
|
import org.opensearch.LegacyESVersion;
|
||||||
import org.opensearch.Version;
|
import org.opensearch.Version;
|
||||||
import org.opensearch.cluster.metadata.IndexMetadata;
|
import org.opensearch.cluster.metadata.IndexMetadata;
|
||||||
|
@ -56,7 +57,6 @@ import org.opensearch.index.mapper.Mapper;
|
||||||
import org.opensearch.index.mapper.ParseContext;
|
import org.opensearch.index.mapper.ParseContext;
|
||||||
import org.opensearch.index.query.QueryShardContext;
|
import org.opensearch.index.query.QueryShardContext;
|
||||||
import org.opensearch.index.query.TermQueryBuilder;
|
import org.opensearch.index.query.TermQueryBuilder;
|
||||||
import org.elasticsearch.mock.orig.Mockito;
|
|
||||||
import org.opensearch.search.SearchModule;
|
import org.opensearch.search.SearchModule;
|
||||||
import org.opensearch.search.aggregations.support.CoreValuesSourceType;
|
import org.opensearch.search.aggregations.support.CoreValuesSourceType;
|
||||||
import org.opensearch.test.OpenSearchTestCase;
|
import org.opensearch.test.OpenSearchTestCase;
|
||||||
|
|
|
@ -34,7 +34,7 @@
|
||||||
//// These are mock objects and test management that we allow test framework libs
|
//// These are mock objects and test management that we allow test framework libs
|
||||||
//// to provide on our behalf. But tests themselves cannot do this stuff!
|
//// to provide on our behalf. But tests themselves cannot do this stuff!
|
||||||
|
|
||||||
grant codeBase "${codebase.securemock}" {
|
grant codeBase "${codebase.mockito-core}" {
|
||||||
// needed to access ReflectionFactory (see below)
|
// needed to access ReflectionFactory (see below)
|
||||||
permission java.lang.RuntimePermission "accessClassInPackage.sun.reflect";
|
permission java.lang.RuntimePermission "accessClassInPackage.sun.reflect";
|
||||||
// needed for reflection in ibm jdk
|
// needed for reflection in ibm jdk
|
||||||
|
@ -44,6 +44,27 @@ grant codeBase "${codebase.securemock}" {
|
||||||
// needed for spy interception, etc
|
// needed for spy interception, etc
|
||||||
permission java.lang.RuntimePermission "accessDeclaredMembers";
|
permission java.lang.RuntimePermission "accessDeclaredMembers";
|
||||||
permission java.lang.reflect.ReflectPermission "suppressAccessChecks";
|
permission java.lang.reflect.ReflectPermission "suppressAccessChecks";
|
||||||
|
permission java.lang.RuntimePermission "getClassLoader";
|
||||||
|
};
|
||||||
|
|
||||||
|
grant codeBase "${codebase.objenesis}" {
|
||||||
|
permission java.lang.RuntimePermission "reflectionFactoryAccess";
|
||||||
|
permission java.lang.RuntimePermission "accessClassInPackage.sun.reflect";
|
||||||
|
permission java.lang.RuntimePermission "accessDeclaredMembers";
|
||||||
|
permission java.lang.reflect.ReflectPermission "suppressAccessChecks";
|
||||||
|
};
|
||||||
|
|
||||||
|
grant codeBase "${codebase.byte-buddy}" {
|
||||||
|
permission java.lang.RuntimePermission "getClassLoader";
|
||||||
|
permission java.lang.RuntimePermission "createClassLoader";
|
||||||
|
permission java.lang.RuntimePermission "accessDeclaredMembers";
|
||||||
|
permission java.lang.RuntimePermission "net.bytebuddy.createJavaDispatcher";
|
||||||
|
permission java.lang.RuntimePermission "accessClassInPackage.sun.misc";
|
||||||
|
permission java.lang.reflect.ReflectPermission "suppressAccessChecks";
|
||||||
|
permission java.lang.reflect.ReflectPermission "newProxyInPackage.net.bytebuddy.utility";
|
||||||
|
permission java.lang.reflect.ReflectPermission "newProxyInPackage.net.bytebuddy.dynamic.loading";
|
||||||
|
permission java.lang.reflect.ReflectPermission "newProxyInPackage.net.bytebuddy.description.type";
|
||||||
|
permission java.lang.reflect.ReflectPermission "newProxyInPackage.net.bytebuddy.description.method";
|
||||||
};
|
};
|
||||||
|
|
||||||
grant codeBase "${codebase.lucene-test-framework}" {
|
grant codeBase "${codebase.lucene-test-framework}" {
|
||||||
|
|
|
@ -79,8 +79,6 @@ import org.opensearch.transport.TransportResponseHandler;
|
||||||
import org.opensearch.transport.TransportService;
|
import org.opensearch.transport.TransportService;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.mockito.ArgumentCaptor;
|
import org.mockito.ArgumentCaptor;
|
||||||
import org.mockito.Captor;
|
|
||||||
import org.mockito.MockitoAnnotations;
|
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
@ -125,13 +123,9 @@ public class TransportBulkActionIngestTests extends OpenSearchTestCase {
|
||||||
ThreadPool threadPool;
|
ThreadPool threadPool;
|
||||||
|
|
||||||
/** Arguments to callbacks we want to capture, but which require generics, so we must use @Captor */
|
/** Arguments to callbacks we want to capture, but which require generics, so we must use @Captor */
|
||||||
@Captor
|
|
||||||
ArgumentCaptor<BiConsumer<Integer, Exception>> failureHandler;
|
ArgumentCaptor<BiConsumer<Integer, Exception>> failureHandler;
|
||||||
@Captor
|
|
||||||
ArgumentCaptor<BiConsumer<Thread, Exception>> completionHandler;
|
ArgumentCaptor<BiConsumer<Thread, Exception>> completionHandler;
|
||||||
@Captor
|
|
||||||
ArgumentCaptor<TransportResponseHandler<BulkResponse>> remoteResponseHandler;
|
ArgumentCaptor<TransportResponseHandler<BulkResponse>> remoteResponseHandler;
|
||||||
@Captor
|
|
||||||
ArgumentCaptor<Iterable<DocWriteRequest<?>>> bulkDocsItr;
|
ArgumentCaptor<Iterable<DocWriteRequest<?>>> bulkDocsItr;
|
||||||
|
|
||||||
/** The actual action we want to test, with real indexing mocked */
|
/** The actual action we want to test, with real indexing mocked */
|
||||||
|
@ -199,7 +193,12 @@ public class TransportBulkActionIngestTests extends OpenSearchTestCase {
|
||||||
threadPool = mock(ThreadPool.class);
|
threadPool = mock(ThreadPool.class);
|
||||||
final ExecutorService direct = OpenSearchExecutors.newDirectExecutorService();
|
final ExecutorService direct = OpenSearchExecutors.newDirectExecutorService();
|
||||||
when(threadPool.executor(anyString())).thenReturn(direct);
|
when(threadPool.executor(anyString())).thenReturn(direct);
|
||||||
MockitoAnnotations.initMocks(this);
|
|
||||||
|
bulkDocsItr = ArgumentCaptor.forClass(Iterable.class);
|
||||||
|
failureHandler = ArgumentCaptor.forClass(BiConsumer.class);
|
||||||
|
completionHandler = ArgumentCaptor.forClass(BiConsumer.class);
|
||||||
|
remoteResponseHandler = ArgumentCaptor.forClass(TransportResponseHandler.class);
|
||||||
|
|
||||||
// setup services that will be called by action
|
// setup services that will be called by action
|
||||||
transportService = mock(TransportService.class);
|
transportService = mock(TransportService.class);
|
||||||
clusterService = mock(ClusterService.class);
|
clusterService = mock(ClusterService.class);
|
||||||
|
@ -672,6 +671,7 @@ public class TransportBulkActionIngestTests extends OpenSearchTestCase {
|
||||||
}));
|
}));
|
||||||
|
|
||||||
assertEquals("pipeline2", indexRequest.getPipeline());
|
assertEquals("pipeline2", indexRequest.getPipeline());
|
||||||
|
|
||||||
verify(ingestService).executeBulkRequest(eq(1), bulkDocsItr.capture(), failureHandler.capture(),
|
verify(ingestService).executeBulkRequest(eq(1), bulkDocsItr.capture(), failureHandler.capture(),
|
||||||
completionHandler.capture(), any(), eq(Names.WRITE));
|
completionHandler.capture(), any(), eq(Names.WRITE));
|
||||||
}
|
}
|
||||||
|
|
|
@ -77,6 +77,7 @@ import static java.util.Collections.emptySet;
|
||||||
import static org.opensearch.common.UUIDs.randomBase64UUID;
|
import static org.opensearch.common.UUIDs.randomBase64UUID;
|
||||||
import static org.hamcrest.Matchers.equalTo;
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
import static org.hamcrest.Matchers.instanceOf;
|
import static org.hamcrest.Matchers.instanceOf;
|
||||||
|
import static org.mockito.ArgumentMatchers.nullable;
|
||||||
import static org.mockito.Matchers.anyString;
|
import static org.mockito.Matchers.anyString;
|
||||||
import static org.mockito.Matchers.eq;
|
import static org.mockito.Matchers.eq;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
|
@ -144,13 +145,13 @@ public class TransportMultiGetActionTests extends OpenSearchTestCase {
|
||||||
when(index2ShardIterator.shardId()).thenReturn(new ShardId(index2, randomInt()));
|
when(index2ShardIterator.shardId()).thenReturn(new ShardId(index2, randomInt()));
|
||||||
|
|
||||||
final OperationRouting operationRouting = mock(OperationRouting.class);
|
final OperationRouting operationRouting = mock(OperationRouting.class);
|
||||||
when(operationRouting.getShards(eq(clusterState), eq(index1.getName()), anyString(), anyString(), anyString()))
|
when(operationRouting.getShards(eq(clusterState), eq(index1.getName()), anyString(), nullable(String.class),
|
||||||
.thenReturn(index1ShardIterator);
|
nullable(String.class))).thenReturn(index1ShardIterator);
|
||||||
when(operationRouting.shardId(eq(clusterState), eq(index1.getName()), anyString(), anyString()))
|
when(operationRouting.shardId(eq(clusterState), eq(index1.getName()), nullable(String.class), nullable(String.class)))
|
||||||
.thenReturn(new ShardId(index1, randomInt()));
|
.thenReturn(new ShardId(index1, randomInt()));
|
||||||
when(operationRouting.getShards(eq(clusterState), eq(index2.getName()), anyString(), anyString(), anyString()))
|
when(operationRouting.getShards(eq(clusterState), eq(index2.getName()), anyString(), nullable(String.class),
|
||||||
.thenReturn(index2ShardIterator);
|
nullable(String.class))).thenReturn(index2ShardIterator);
|
||||||
when(operationRouting.shardId(eq(clusterState), eq(index2.getName()), anyString(), anyString()))
|
when(operationRouting.shardId(eq(clusterState), eq(index2.getName()), nullable(String.class), nullable(String.class)))
|
||||||
.thenReturn(new ShardId(index2, randomInt()));
|
.thenReturn(new ShardId(index2, randomInt()));
|
||||||
|
|
||||||
clusterService = mock(ClusterService.class);
|
clusterService = mock(ClusterService.class);
|
||||||
|
|
|
@ -80,6 +80,7 @@ import static org.hamcrest.Matchers.equalTo;
|
||||||
import static org.hamcrest.Matchers.instanceOf;
|
import static org.hamcrest.Matchers.instanceOf;
|
||||||
import static org.mockito.Matchers.anyString;
|
import static org.mockito.Matchers.anyString;
|
||||||
import static org.mockito.Matchers.eq;
|
import static org.mockito.Matchers.eq;
|
||||||
|
import static org.mockito.Matchers.nullable;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
@ -145,13 +146,13 @@ public class TransportMultiTermVectorsActionTests extends OpenSearchTestCase {
|
||||||
when(index2ShardIterator.shardId()).thenReturn(new ShardId(index2, randomInt()));
|
when(index2ShardIterator.shardId()).thenReturn(new ShardId(index2, randomInt()));
|
||||||
|
|
||||||
final OperationRouting operationRouting = mock(OperationRouting.class);
|
final OperationRouting operationRouting = mock(OperationRouting.class);
|
||||||
when(operationRouting.getShards(eq(clusterState), eq(index1.getName()), anyString(), anyString(), anyString()))
|
when(operationRouting.getShards(eq(clusterState), eq(index1.getName()), anyString(), nullable(String.class),
|
||||||
.thenReturn(index1ShardIterator);
|
nullable(String.class))).thenReturn(index1ShardIterator);
|
||||||
when(operationRouting.shardId(eq(clusterState), eq(index1.getName()), anyString(), anyString()))
|
when(operationRouting.shardId(eq(clusterState), eq(index1.getName()), nullable(String.class), nullable(String.class)))
|
||||||
.thenReturn(new ShardId(index1, randomInt()));
|
.thenReturn(new ShardId(index1, randomInt()));
|
||||||
when(operationRouting.getShards(eq(clusterState), eq(index2.getName()), anyString(), anyString(), anyString()))
|
when(operationRouting.getShards(eq(clusterState), eq(index2.getName()), anyString(), nullable(String.class),
|
||||||
.thenReturn(index2ShardIterator);
|
nullable(String.class))).thenReturn(index2ShardIterator);
|
||||||
when(operationRouting.shardId(eq(clusterState), eq(index2.getName()), anyString(), anyString()))
|
when(operationRouting.shardId(eq(clusterState), eq(index2.getName()), nullable(String.class), nullable(String.class)))
|
||||||
.thenReturn(new ShardId(index2, randomInt()));
|
.thenReturn(new ShardId(index2, randomInt()));
|
||||||
|
|
||||||
clusterService = mock(ClusterService.class);
|
clusterService = mock(ClusterService.class);
|
||||||
|
|
|
@ -35,9 +35,9 @@ package org.opensearch.common.settings;
|
||||||
import org.opensearch.cluster.ClusterState;
|
import org.opensearch.cluster.ClusterState;
|
||||||
import org.opensearch.cluster.ClusterStateUpdateTask;
|
import org.opensearch.cluster.ClusterStateUpdateTask;
|
||||||
import org.opensearch.cluster.service.ClusterService;
|
import org.opensearch.cluster.service.ClusterService;
|
||||||
import org.elasticsearch.mock.orig.Mockito;
|
|
||||||
import org.opensearch.test.OpenSearchTestCase;
|
import org.opensearch.test.OpenSearchTestCase;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
|
import org.mockito.Mockito;
|
||||||
import org.mockito.stubbing.Answer;
|
import org.mockito.stubbing.Answer;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
|
@ -70,7 +70,6 @@ import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.Stack;
|
import java.util.Stack;
|
||||||
import java.util.concurrent.CountDownLatch;
|
import java.util.concurrent.CountDownLatch;
|
||||||
import java.util.concurrent.ExecutionException;
|
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.ThreadFactory;
|
import java.util.concurrent.ThreadFactory;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
@ -420,6 +419,6 @@ public class SeedHostsResolverTests extends OpenSearchTestCase {
|
||||||
assertThat(transportAddresses, hasSize(1)); // only one of the two is valid and will be used
|
assertThat(transportAddresses, hasSize(1)); // only one of the two is valid and will be used
|
||||||
assertThat(transportAddresses.get(0).getAddress(), equalTo("127.0.0.1"));
|
assertThat(transportAddresses.get(0).getAddress(), equalTo("127.0.0.1"));
|
||||||
assertThat(transportAddresses.get(0).getPort(), equalTo(9301));
|
assertThat(transportAddresses.get(0).getPort(), equalTo(9301));
|
||||||
verify(logger).warn(eq("failed to resolve host [127.0.0.1:9300:9300]"), Matchers.any(ExecutionException.class));
|
verify(logger).warn(eq("failed to resolve host [127.0.0.1:9300:9300]"), Matchers.any(IllegalArgumentException.class));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -52,11 +52,11 @@ import org.opensearch.transport.TransportService;
|
||||||
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
|
||||||
import static org.elasticsearch.mock.orig.Mockito.never;
|
|
||||||
import static org.elasticsearch.mock.orig.Mockito.when;
|
|
||||||
import static org.opensearch.test.ClusterServiceUtils.createClusterService;
|
import static org.opensearch.test.ClusterServiceUtils.createClusterService;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.never;
|
||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
public class GlobalCheckpointSyncActionTests extends OpenSearchTestCase {
|
public class GlobalCheckpointSyncActionTests extends OpenSearchTestCase {
|
||||||
|
|
||||||
|
|
|
@ -58,11 +58,11 @@ import java.util.Collections;
|
||||||
import java.util.concurrent.CountDownLatch;
|
import java.util.concurrent.CountDownLatch;
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
|
||||||
import static org.elasticsearch.mock.orig.Mockito.when;
|
|
||||||
import static org.opensearch.test.ClusterServiceUtils.createClusterService;
|
import static org.opensearch.test.ClusterServiceUtils.createClusterService;
|
||||||
import static org.hamcrest.Matchers.sameInstance;
|
import static org.hamcrest.Matchers.sameInstance;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
public class RetentionLeaseBackgroundSyncActionTests extends OpenSearchTestCase {
|
public class RetentionLeaseBackgroundSyncActionTests extends OpenSearchTestCase {
|
||||||
|
|
||||||
|
|
|
@ -58,11 +58,11 @@ import java.util.Collections;
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
|
||||||
import static java.util.Collections.emptyMap;
|
import static java.util.Collections.emptyMap;
|
||||||
import static org.elasticsearch.mock.orig.Mockito.when;
|
|
||||||
import static org.opensearch.test.ClusterServiceUtils.createClusterService;
|
import static org.opensearch.test.ClusterServiceUtils.createClusterService;
|
||||||
import static org.hamcrest.Matchers.sameInstance;
|
import static org.hamcrest.Matchers.sameInstance;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
public class RetentionLeaseSyncActionTests extends OpenSearchTestCase {
|
public class RetentionLeaseSyncActionTests extends OpenSearchTestCase {
|
||||||
|
|
||||||
|
|
|
@ -66,12 +66,11 @@ import java.util.concurrent.atomic.AtomicLong;
|
||||||
|
|
||||||
import static org.opensearch.index.seqno.SequenceNumbers.NO_OPS_PERFORMED;
|
import static org.opensearch.index.seqno.SequenceNumbers.NO_OPS_PERFORMED;
|
||||||
import static org.opensearch.index.seqno.SequenceNumbers.UNASSIGNED_SEQ_NO;
|
import static org.opensearch.index.seqno.SequenceNumbers.UNASSIGNED_SEQ_NO;
|
||||||
import static org.hamcrest.Matchers.any;
|
|
||||||
import static org.hamcrest.Matchers.containsString;
|
import static org.hamcrest.Matchers.containsString;
|
||||||
import static org.hamcrest.Matchers.equalTo;
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
import static org.hamcrest.Matchers.hasToString;
|
import static org.hamcrest.Matchers.hasToString;
|
||||||
import static org.hamcrest.Matchers.instanceOf;
|
import static org.hamcrest.Matchers.instanceOf;
|
||||||
import static org.mockito.Matchers.argThat;
|
import static org.mockito.Matchers.any;
|
||||||
import static org.mockito.Mockito.doAnswer;
|
import static org.mockito.Mockito.doAnswer;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
import static org.mockito.Mockito.reset;
|
import static org.mockito.Mockito.reset;
|
||||||
|
@ -646,7 +645,7 @@ public class GlobalCheckpointListenersTests extends OpenSearchTestCase {
|
||||||
doAnswer(invocationOnMock -> {
|
doAnswer(invocationOnMock -> {
|
||||||
latch.countDown();
|
latch.countDown();
|
||||||
return null;
|
return null;
|
||||||
}).when(mockLogger).warn(argThat(any(String.class)), argThat(any(RuntimeException.class)));
|
}).when(mockLogger).warn(any(String.class), any(RuntimeException.class));
|
||||||
final GlobalCheckpointListeners globalCheckpointListeners =
|
final GlobalCheckpointListeners globalCheckpointListeners =
|
||||||
new GlobalCheckpointListeners(shardId, scheduler, mockLogger);
|
new GlobalCheckpointListeners(shardId, scheduler, mockLogger);
|
||||||
final TimeValue timeout = TimeValue.timeValueMillis(randomIntBetween(1, 50));
|
final TimeValue timeout = TimeValue.timeValueMillis(randomIntBetween(1, 50));
|
||||||
|
|
|
@ -160,7 +160,7 @@ import static org.hamcrest.Matchers.lessThanOrEqualTo;
|
||||||
import static org.hamcrest.Matchers.not;
|
import static org.hamcrest.Matchers.not;
|
||||||
import static org.hamcrest.Matchers.nullValue;
|
import static org.hamcrest.Matchers.nullValue;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
import static org.mockito.Mockito.stub;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
@LuceneTestCase.SuppressFileSystems("ExtrasFS")
|
@LuceneTestCase.SuppressFileSystems("ExtrasFS")
|
||||||
public class TranslogTests extends OpenSearchTestCase {
|
public class TranslogTests extends OpenSearchTestCase {
|
||||||
|
@ -411,7 +411,7 @@ public class TranslogTests extends OpenSearchTestCase {
|
||||||
long period = randomLongBetween(10000, 1000000);
|
long period = randomLongBetween(10000, 1000000);
|
||||||
periods[numberOfReaders] = period;
|
periods[numberOfReaders] = period;
|
||||||
TranslogWriter w = mock(TranslogWriter.class);
|
TranslogWriter w = mock(TranslogWriter.class);
|
||||||
stub(w.getLastModifiedTime()).toReturn(fixedTime - period);
|
when(w.getLastModifiedTime()).thenReturn(fixedTime - period);
|
||||||
assertThat(Translog.findEarliestLastModifiedAge(fixedTime, new ArrayList<>(), w), equalTo(period));
|
assertThat(Translog.findEarliestLastModifiedAge(fixedTime, new ArrayList<>(), w), equalTo(period));
|
||||||
|
|
||||||
for (int i = 0; i < numberOfReaders; i++) {
|
for (int i = 0; i < numberOfReaders; i++) {
|
||||||
|
@ -420,7 +420,7 @@ public class TranslogTests extends OpenSearchTestCase {
|
||||||
List<TranslogReader> readers = new ArrayList<>();
|
List<TranslogReader> readers = new ArrayList<>();
|
||||||
for (long l : periods) {
|
for (long l : periods) {
|
||||||
TranslogReader r = mock(TranslogReader.class);
|
TranslogReader r = mock(TranslogReader.class);
|
||||||
stub(r.getLastModifiedTime()).toReturn(fixedTime - l);
|
when(r.getLastModifiedTime()).thenReturn(fixedTime - l);
|
||||||
readers.add(r);
|
readers.add(r);
|
||||||
}
|
}
|
||||||
assertThat(Translog.findEarliestLastModifiedAge(fixedTime, readers, w), equalTo
|
assertThat(Translog.findEarliestLastModifiedAge(fixedTime, readers, w), equalTo
|
||||||
|
|
|
@ -77,7 +77,6 @@ import org.opensearch.test.OpenSearchTestCase;
|
||||||
import org.opensearch.test.MockLogAppender;
|
import org.opensearch.test.MockLogAppender;
|
||||||
import org.opensearch.threadpool.ThreadPool;
|
import org.opensearch.threadpool.ThreadPool;
|
||||||
import org.opensearch.threadpool.ThreadPool.Names;
|
import org.opensearch.threadpool.ThreadPool.Names;
|
||||||
import org.hamcrest.CustomTypeSafeMatcher;
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.mockito.ArgumentMatcher;
|
import org.mockito.ArgumentMatcher;
|
||||||
import org.mockito.invocation.InvocationOnMock;
|
import org.mockito.invocation.InvocationOnMock;
|
||||||
|
@ -724,19 +723,8 @@ public class IngestServiceTests extends OpenSearchTestCase {
|
||||||
ingestService.executeBulkRequest(bulkRequest.numberOfActions(), bulkRequest.requests(), failureHandler,
|
ingestService.executeBulkRequest(bulkRequest.numberOfActions(), bulkRequest.requests(), failureHandler,
|
||||||
completionHandler, indexReq -> {}, Names.WRITE);
|
completionHandler, indexReq -> {}, Names.WRITE);
|
||||||
verify(failureHandler, times(1)).accept(
|
verify(failureHandler, times(1)).accept(
|
||||||
argThat(new CustomTypeSafeMatcher<Integer>("failure handler was not called with the expected arguments") {
|
argThat((Integer item) -> item == 2),
|
||||||
@Override
|
argThat((IllegalArgumentException iae) -> "pipeline with id [does_not_exist] does not exist".equals(iae.getMessage()))
|
||||||
protected boolean matchesSafely(Integer item) {
|
|
||||||
return item == 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
}),
|
|
||||||
argThat(new CustomTypeSafeMatcher<IllegalArgumentException>("failure handler was not called with the expected arguments") {
|
|
||||||
@Override
|
|
||||||
protected boolean matchesSafely(IllegalArgumentException iae) {
|
|
||||||
return "pipeline with id [does_not_exist] does not exist".equals(iae.getMessage());
|
|
||||||
}
|
|
||||||
})
|
|
||||||
);
|
);
|
||||||
verify(completionHandler, times(1)).accept(Thread.currentThread(), null);
|
verify(completionHandler, times(1)).accept(Thread.currentThread(), null);
|
||||||
}
|
}
|
||||||
|
@ -995,12 +983,9 @@ public class IngestServiceTests extends OpenSearchTestCase {
|
||||||
ingestService.executeBulkRequest(numRequest, bulkRequest.requests(), requestItemErrorHandler, completionHandler, indexReq -> {},
|
ingestService.executeBulkRequest(numRequest, bulkRequest.requests(), requestItemErrorHandler, completionHandler, indexReq -> {},
|
||||||
Names.WRITE);
|
Names.WRITE);
|
||||||
|
|
||||||
verify(requestItemErrorHandler, times(numIndexRequests)).accept(anyInt(), argThat(new ArgumentMatcher<Exception>() {
|
verify(requestItemErrorHandler, times(numIndexRequests)).accept(anyInt(),
|
||||||
@Override
|
argThat(o -> o.getCause().equals(error)));
|
||||||
public boolean matches(final Object o) {
|
|
||||||
return ((Exception)o).getCause().equals(error);
|
|
||||||
}
|
|
||||||
}));
|
|
||||||
verify(completionHandler, times(1)).accept(Thread.currentThread(), null);
|
verify(completionHandler, times(1)).accept(Thread.currentThread(), null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1499,7 +1484,7 @@ public class IngestServiceTests extends OpenSearchTestCase {
|
||||||
return processor;
|
return processor;
|
||||||
}
|
}
|
||||||
|
|
||||||
private class IngestDocumentMatcher extends ArgumentMatcher<IngestDocument> {
|
private class IngestDocumentMatcher implements ArgumentMatcher<IngestDocument> {
|
||||||
|
|
||||||
private final IngestDocument ingestDocument;
|
private final IngestDocument ingestDocument;
|
||||||
|
|
||||||
|
@ -1512,13 +1497,8 @@ public class IngestServiceTests extends OpenSearchTestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean matches(Object o) {
|
public boolean matches(IngestDocument o) {
|
||||||
if (o.getClass() == IngestDocument.class) {
|
return Objects.equals(ingestDocument.getSourceAndMetadata(), o.getSourceAndMetadata());
|
||||||
IngestDocument otherIngestDocument = (IngestDocument) o;
|
|
||||||
//ingest metadata will not be the same (timestamp differs every time)
|
|
||||||
return Objects.equals(ingestDocument.getSourceAndMetadata(), otherIngestDocument.getSourceAndMetadata());
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -55,9 +55,9 @@ import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import static org.elasticsearch.mock.orig.Mockito.when;
|
|
||||||
import static org.hamcrest.CoreMatchers.equalTo;
|
import static org.hamcrest.CoreMatchers.equalTo;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
public class RestRecoveryActionTests extends OpenSearchTestCase {
|
public class RestRecoveryActionTests extends OpenSearchTestCase {
|
||||||
|
|
||||||
|
|
|
@ -85,6 +85,7 @@ import static org.hamcrest.Matchers.is;
|
||||||
import static org.mockito.Matchers.anyObject;
|
import static org.mockito.Matchers.anyObject;
|
||||||
import static org.mockito.Matchers.anyString;
|
import static org.mockito.Matchers.anyString;
|
||||||
import static org.mockito.Matchers.eq;
|
import static org.mockito.Matchers.eq;
|
||||||
|
import static org.mockito.Matchers.nullable;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
@ -122,7 +123,8 @@ public class DefaultSearchContextTests extends OpenSearchTestCase {
|
||||||
when(indexCache.query()).thenReturn(queryCache);
|
when(indexCache.query()).thenReturn(queryCache);
|
||||||
when(indexService.cache()).thenReturn(indexCache);
|
when(indexService.cache()).thenReturn(indexCache);
|
||||||
QueryShardContext queryShardContext = mock(QueryShardContext.class);
|
QueryShardContext queryShardContext = mock(QueryShardContext.class);
|
||||||
when(indexService.newQueryShardContext(eq(shardId.id()), anyObject(), anyObject(), anyString())).thenReturn(queryShardContext);
|
when(indexService.newQueryShardContext(eq(shardId.id()), anyObject(), anyObject(),
|
||||||
|
nullable(String.class))).thenReturn(queryShardContext);
|
||||||
MapperService mapperService = mock(MapperService.class);
|
MapperService mapperService = mock(MapperService.class);
|
||||||
when(mapperService.hasNested()).thenReturn(randomBoolean());
|
when(mapperService.hasNested()).thenReturn(randomBoolean());
|
||||||
when(indexService.mapperService()).thenReturn(mapperService);
|
when(indexService.mapperService()).thenReturn(mapperService);
|
||||||
|
@ -267,7 +269,8 @@ public class DefaultSearchContextTests extends OpenSearchTestCase {
|
||||||
|
|
||||||
IndexService indexService = mock(IndexService.class);
|
IndexService indexService = mock(IndexService.class);
|
||||||
QueryShardContext queryShardContext = mock(QueryShardContext.class);
|
QueryShardContext queryShardContext = mock(QueryShardContext.class);
|
||||||
when(indexService.newQueryShardContext(eq(shardId.id()), anyObject(), anyObject(), anyString())).thenReturn(queryShardContext);
|
when(indexService.newQueryShardContext(eq(shardId.id()), anyObject(), anyObject(),
|
||||||
|
nullable(String.class))).thenReturn(queryShardContext);
|
||||||
|
|
||||||
BigArrays bigArrays = new MockBigArrays(new MockPageCacheRecycler(Settings.EMPTY), new NoneCircuitBreakerService());
|
BigArrays bigArrays = new MockBigArrays(new MockPageCacheRecycler(Settings.EMPTY), new NoneCircuitBreakerService());
|
||||||
|
|
||||||
|
|
|
@ -45,7 +45,9 @@ dependencies {
|
||||||
api "org.apache.lucene:lucene-codecs:${versions.lucene}"
|
api "org.apache.lucene:lucene-codecs:${versions.lucene}"
|
||||||
api "commons-logging:commons-logging:${versions.commonslogging}"
|
api "commons-logging:commons-logging:${versions.commonslogging}"
|
||||||
api "commons-codec:commons-codec:${versions.commonscodec}"
|
api "commons-codec:commons-codec:${versions.commonscodec}"
|
||||||
api "org.elasticsearch:securemock:${versions.securemock}"
|
api "org.mockito:mockito-core:${versions.mockito}"
|
||||||
|
api "net.bytebuddy:byte-buddy:${versions.bytebuddy}"
|
||||||
|
api "org.objenesis:objenesis:${versions.objenesis}"
|
||||||
}
|
}
|
||||||
|
|
||||||
compileJava.options.compilerArgs -= '-Xlint:cast'
|
compileJava.options.compilerArgs -= '-Xlint:cast'
|
||||||
|
@ -73,15 +75,14 @@ thirdPartyAudit.ignoreMissingClasses(
|
||||||
'org.apache.log4j.Level',
|
'org.apache.log4j.Level',
|
||||||
'org.apache.log4j.Logger',
|
'org.apache.log4j.Logger',
|
||||||
'org.apache.log4j.Priority',
|
'org.apache.log4j.Priority',
|
||||||
// TODO - OpenSearch remove this missing classes. Issue: https://github.com/opensearch-project/OpenSearch/issues/420
|
'org.mockito.internal.creation.bytebuddy.inject.MockMethodDispatcher',
|
||||||
'org.apache.tools.ant.BuildException',
|
'org.opentest4j.AssertionFailedError',
|
||||||
'org.apache.tools.ant.DirectoryScanner',
|
'net.bytebuddy.agent.ByteBuddyAgent'
|
||||||
'org.apache.tools.ant.Task',
|
|
||||||
'org.apache.tools.ant.types.FileSet',
|
|
||||||
)
|
)
|
||||||
// TODO - OpenSearch remove this violation. Issue: https://github.com/opensearch-project/OpenSearch/issues/420
|
// TODO - OpenSearch remove this violation. Issue: https://github.com/opensearch-project/OpenSearch/issues/420
|
||||||
thirdPartyAudit.ignoreViolations(
|
thirdPartyAudit.ignoreViolations(
|
||||||
'org.objenesis.instantiator.sun.UnsafeFactoryInstantiator'
|
'org.objenesis.instantiator.sun.UnsafeFactoryInstantiator',
|
||||||
|
'org.objenesis.instantiator.util.UnsafeUtils'
|
||||||
)
|
)
|
||||||
|
|
||||||
test {
|
test {
|
||||||
|
|
|
@ -44,6 +44,7 @@ import org.opensearch.common.io.PathUtils;
|
||||||
import org.opensearch.common.network.IfConfig;
|
import org.opensearch.common.network.IfConfig;
|
||||||
import org.opensearch.common.network.NetworkAddress;
|
import org.opensearch.common.network.NetworkAddress;
|
||||||
import org.opensearch.common.settings.Settings;
|
import org.opensearch.common.settings.Settings;
|
||||||
|
import org.opensearch.mockito.plugin.PriviledgedMockMaker;
|
||||||
import org.opensearch.plugins.PluginInfo;
|
import org.opensearch.plugins.PluginInfo;
|
||||||
import org.opensearch.secure_sm.SecureSM;
|
import org.opensearch.secure_sm.SecureSM;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
|
@ -151,8 +152,8 @@ public class BootstrapForTesting {
|
||||||
addClassCodebase(codebases,"opensearch", "org.opensearch.plugins.PluginsService");
|
addClassCodebase(codebases,"opensearch", "org.opensearch.plugins.PluginsService");
|
||||||
if (System.getProperty("tests.gradle") == null) {
|
if (System.getProperty("tests.gradle") == null) {
|
||||||
// intellij and eclipse don't package our internal libs, so we need to set the codebases for them manually
|
// intellij and eclipse don't package our internal libs, so we need to set the codebases for them manually
|
||||||
addClassCodebase(codebases,"plugin-classloader", "org.opensearch.plugins.ExtendedPluginsClassLoader");
|
addClassCodebase(codebases, "plugin-classloader", "org.opensearch.plugins.ExtendedPluginsClassLoader");
|
||||||
addClassCodebase(codebases,"opensearch-nio", "org.opensearch.nio.ChannelFactory");
|
addClassCodebase(codebases, "opensearch-nio", "org.opensearch.nio.ChannelFactory");
|
||||||
addClassCodebase(codebases, "opensearch-secure-sm", "org.opensearch.secure_sm.SecureSM");
|
addClassCodebase(codebases, "opensearch-secure-sm", "org.opensearch.secure_sm.SecureSM");
|
||||||
addClassCodebase(codebases, "opensearch-rest-client", "org.opensearch.client.RestClient");
|
addClassCodebase(codebases, "opensearch-rest-client", "org.opensearch.client.RestClient");
|
||||||
}
|
}
|
||||||
|
@ -165,6 +166,8 @@ public class BootstrapForTesting {
|
||||||
return opensearchPolicy.implies(domain, permission) || testFramework.implies(domain, permission);
|
return opensearchPolicy.implies(domain, permission) || testFramework.implies(domain, permission);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
// Create access control context for mocking
|
||||||
|
PriviledgedMockMaker.createAccessControlContext();
|
||||||
System.setSecurityManager(SecureSM.createTestSecureSM(getTrustedHosts()));
|
System.setSecurityManager(SecureSM.createTestSecureSM(getTrustedHosts()));
|
||||||
Security.selfTest();
|
Security.selfTest();
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,145 @@
|
||||||
|
/*
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*
|
||||||
|
* The OpenSearch Contributors require contributions made to
|
||||||
|
* this file be licensed under the Apache-2.0 license or a
|
||||||
|
* compatible open source license.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.opensearch.mockito.plugin;
|
||||||
|
|
||||||
|
import org.mockito.Incubating;
|
||||||
|
import org.mockito.MockedConstruction;
|
||||||
|
import org.mockito.internal.creation.bytebuddy.ByteBuddyMockMaker;
|
||||||
|
import org.mockito.internal.util.reflection.LenientCopyTool;
|
||||||
|
import org.mockito.invocation.MockHandler;
|
||||||
|
import org.mockito.mock.MockCreationSettings;
|
||||||
|
import org.mockito.plugins.MockMaker;
|
||||||
|
import org.opensearch.common.SuppressForbidden;
|
||||||
|
|
||||||
|
import java.security.AccessControlContext;
|
||||||
|
import java.security.AccessController;
|
||||||
|
import java.security.DomainCombiner;
|
||||||
|
import java.security.PrivilegedAction;
|
||||||
|
import java.security.ProtectionDomain;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mockito plugin which wraps the Mockito calls into priviledged execution blocks and respects
|
||||||
|
* SecurityManager presence.
|
||||||
|
*/
|
||||||
|
@SuppressForbidden(reason = "allow URL#getFile() to be used in tests")
|
||||||
|
public class PriviledgedMockMaker implements MockMaker {
|
||||||
|
private static AccessControlContext context;
|
||||||
|
private final ByteBuddyMockMaker delegate;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create dedicated AccessControlContext to use the Mockito protection domain (test only)
|
||||||
|
* so to relax the security constraints for the test cases which rely on mocks. This plugin
|
||||||
|
* wraps the mock/spy creation into priviledged action using the custom access control context
|
||||||
|
* since Mockito does not support SecurityManager out of the box. The method has to be called by
|
||||||
|
* test framework before the SecurityManager is being set, otherwise additional permissions have
|
||||||
|
* to be granted to the caller:
|
||||||
|
*
|
||||||
|
* permission java.security.Permission "createAccessControlContext"
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static void createAccessControlContext() {
|
||||||
|
// This combiner, if bound to an access control context, will unconditionally
|
||||||
|
// substitute the call chain protection domains with the 'mockito-core' one if it
|
||||||
|
// is present. The security checks are relaxed intentionally to trust mocking
|
||||||
|
// implementation if it is part of the call chain.
|
||||||
|
final DomainCombiner combiner = (current, assigned) -> Arrays
|
||||||
|
.stream(current)
|
||||||
|
.filter(pd ->
|
||||||
|
pd
|
||||||
|
.getCodeSource()
|
||||||
|
.getLocation()
|
||||||
|
.getFile()
|
||||||
|
.contains("mockito-core") /* check mockito-core only */)
|
||||||
|
.findAny()
|
||||||
|
.map(pd -> new ProtectionDomain[] { pd })
|
||||||
|
.orElse(current);
|
||||||
|
|
||||||
|
// Bind combiner to an access control context (the combiner stateless and shareable)
|
||||||
|
final AccessControlContext wrapper = new AccessControlContext(AccessController.getContext(), combiner);
|
||||||
|
|
||||||
|
// Create new access control context with dedicated combiner
|
||||||
|
context = AccessController.doPrivileged(
|
||||||
|
(PrivilegedAction<AccessControlContext>) AccessController::getContext,
|
||||||
|
wrapper);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construct an instance of the priviledged mock maker using ByteBuddyMockMaker under the hood.
|
||||||
|
*/
|
||||||
|
public PriviledgedMockMaker() {
|
||||||
|
delegate = AccessController.doPrivileged(
|
||||||
|
(PrivilegedAction<ByteBuddyMockMaker>) () -> new ByteBuddyMockMaker(),
|
||||||
|
context);
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("rawtypes")
|
||||||
|
@Override
|
||||||
|
public <T> T createMock(MockCreationSettings<T> settings, MockHandler handler) {
|
||||||
|
return AccessController.doPrivileged(
|
||||||
|
(PrivilegedAction<T>) () -> delegate.createMock(settings, handler),
|
||||||
|
context);
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("rawtypes")
|
||||||
|
@Override
|
||||||
|
public <T> Optional<T> createSpy(MockCreationSettings<T> settings, MockHandler handler, T object) {
|
||||||
|
// The ByteBuddyMockMaker does not implement createSpy and relies on Mockito's fallback
|
||||||
|
return AccessController.doPrivileged(
|
||||||
|
(PrivilegedAction<Optional<T> >) () -> {
|
||||||
|
T instance = delegate.createMock(settings, handler);
|
||||||
|
new LenientCopyTool().copyToMock(object, instance);
|
||||||
|
return Optional.of(instance);
|
||||||
|
},
|
||||||
|
context);
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("rawtypes")
|
||||||
|
@Override
|
||||||
|
public MockHandler getHandler(Object mock) {
|
||||||
|
return delegate.getHandler(mock);
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("rawtypes")
|
||||||
|
@Override
|
||||||
|
public void resetMock(Object mock, MockHandler newHandler, MockCreationSettings settings) {
|
||||||
|
AccessController.doPrivileged(
|
||||||
|
(PrivilegedAction<Void>) () -> {
|
||||||
|
delegate.resetMock(mock, newHandler, settings);
|
||||||
|
return null;
|
||||||
|
}, context);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Incubating
|
||||||
|
public TypeMockability isTypeMockable(Class<?> type) {
|
||||||
|
return delegate.isTypeMockable(type);
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("rawtypes")
|
||||||
|
@Override
|
||||||
|
public <T> StaticMockControl<T> createStaticMock(Class<T> type, MockCreationSettings<T> settings, MockHandler handler) {
|
||||||
|
return delegate.createStaticMock(type, settings, handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T> ConstructionMockControl<T> createConstructionMock(Class<T> type,
|
||||||
|
Function<MockedConstruction.Context, MockCreationSettings<T>> settingsFactory,
|
||||||
|
Function<MockedConstruction.Context, MockHandler<T>> handlerFactory,
|
||||||
|
MockedConstruction.MockInitializer<T> mockInitializer) {
|
||||||
|
return delegate.createConstructionMock(type, settingsFactory, handlerFactory, mockInitializer);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void clearAllCaches() {
|
||||||
|
delegate.clearAllCaches();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1 @@
|
||||||
|
org.opensearch.mockito.plugin.PriviledgedMockMaker
|
Loading…
Reference in New Issue