Replace Socket, ServerSocket, and HttpServer usages in tests with mocksocket versions (#22287)
This integrates the mocksocket jar with elasticsearch tests. Mocksocket wraps actions requiring SocketPermissions in doPrivilege blocks. This will eventually allow SocketPermissions to be assigned to the mocksocket jar opposed to the entire elasticsearch codebase.
This commit is contained in:
parent
2f510b38c3
commit
be22a250b6
|
@ -20,5 +20,6 @@ commonslogging = 1.1.3
|
|||
commonscodec = 1.10
|
||||
hamcrest = 1.3
|
||||
securemock = 1.2
|
||||
mocksocket = 1.1
|
||||
# benchmark dependencies
|
||||
jmh = 1.17.3
|
||||
|
|
|
@ -43,6 +43,7 @@ dependencies {
|
|||
testCompile "junit:junit:${versions.junit}"
|
||||
testCompile "org.hamcrest:hamcrest-all:${versions.hamcrest}"
|
||||
testCompile "org.elasticsearch:securemock:${versions.securemock}"
|
||||
testCompile "org.elasticsearch:mocksocket:${versions.mocksocket}"
|
||||
testCompile "org.codehaus.mojo:animal-sniffer-annotations:1.15"
|
||||
signature "org.codehaus.mojo.signature:java17:1.0@signature"
|
||||
}
|
||||
|
|
|
@ -24,6 +24,7 @@ import com.sun.net.httpserver.HttpHandler;
|
|||
import com.sun.net.httpserver.HttpServer;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement;
|
||||
import org.elasticsearch.mocksocket.MockHttpServer;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
|
@ -80,7 +81,7 @@ public class RestClientMultipleHostsIntegTests extends RestClientTestCase {
|
|||
}
|
||||
|
||||
private static HttpServer createHttpServer() throws Exception {
|
||||
HttpServer httpServer = HttpServer.create(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 0);
|
||||
HttpServer httpServer = MockHttpServer.createHttp(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 0);
|
||||
httpServer.start();
|
||||
//returns a different status code depending on the path
|
||||
for (int statusCode : getAllStatusCodes()) {
|
||||
|
|
|
@ -29,6 +29,7 @@ import org.apache.http.HttpHost;
|
|||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement;
|
||||
import org.elasticsearch.mocksocket.MockHttpServer;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
|
||||
|
@ -87,7 +88,7 @@ public class RestClientSingleHostIntegTests extends RestClientTestCase {
|
|||
}
|
||||
|
||||
private static HttpServer createHttpServer() throws Exception {
|
||||
HttpServer httpServer = HttpServer.create(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 0);
|
||||
HttpServer httpServer = MockHttpServer.createHttp(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 0);
|
||||
httpServer.start();
|
||||
//returns a different status code depending on the path
|
||||
for (int statusCode : getAllStatusCodes()) {
|
||||
|
|
|
@ -43,6 +43,7 @@ dependencies {
|
|||
testCompile "junit:junit:${versions.junit}"
|
||||
testCompile "org.hamcrest:hamcrest-all:${versions.hamcrest}"
|
||||
testCompile "org.elasticsearch:securemock:${versions.securemock}"
|
||||
testCompile "org.elasticsearch:mocksocket:${versions.mocksocket}"
|
||||
testCompile "org.codehaus.mojo:animal-sniffer-annotations:1.15"
|
||||
signature "org.codehaus.mojo.signature:java17:1.0@signature"
|
||||
}
|
||||
|
|
|
@ -35,6 +35,7 @@ import org.elasticsearch.client.Response;
|
|||
import org.elasticsearch.client.ResponseException;
|
||||
import org.elasticsearch.client.RestClient;
|
||||
import org.elasticsearch.client.RestClientTestCase;
|
||||
import org.elasticsearch.mocksocket.MockHttpServer;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
|
||||
|
@ -141,7 +142,7 @@ public class ElasticsearchHostsSnifferTests extends RestClientTestCase {
|
|||
}
|
||||
|
||||
private static HttpServer createHttpServer(final SniffResponse sniffResponse, final int sniffTimeoutMillis) throws IOException {
|
||||
HttpServer httpServer = HttpServer.create(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 0);
|
||||
HttpServer httpServer = MockHttpServer.createHttp(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 0);
|
||||
httpServer.createContext("/_nodes/http", new ResponseHandler(sniffTimeoutMillis, sniffResponse));
|
||||
return httpServer;
|
||||
}
|
||||
|
|
|
@ -26,6 +26,7 @@ import org.elasticsearch.common.transport.TransportAddress;
|
|||
import org.elasticsearch.common.util.BigArrays;
|
||||
import org.elasticsearch.common.util.MockBigArrays;
|
||||
import org.elasticsearch.indices.breaker.NoneCircuitBreakerService;
|
||||
import org.elasticsearch.mocksocket.MockSocket;
|
||||
import org.elasticsearch.test.ESTestCase;
|
||||
import org.elasticsearch.threadpool.ThreadPool;
|
||||
import org.elasticsearch.transport.TransportSettings;
|
||||
|
@ -84,7 +85,7 @@ public class Netty4SizeHeaderFrameDecoderTests extends ESTestCase {
|
|||
String randomMethod = randomFrom("GET", "POST", "PUT", "DELETE", "HEAD", "OPTIONS", "PATCH");
|
||||
String data = randomMethod + " / HTTP/1.1";
|
||||
|
||||
try (Socket socket = new Socket(host, port)) {
|
||||
try (Socket socket = new MockSocket(host, port)) {
|
||||
socket.getOutputStream().write(data.getBytes(StandardCharsets.UTF_8));
|
||||
socket.getOutputStream().flush();
|
||||
|
||||
|
@ -95,7 +96,7 @@ public class Netty4SizeHeaderFrameDecoderTests extends ESTestCase {
|
|||
}
|
||||
|
||||
public void testThatNothingIsReturnedForOtherInvalidPackets() throws Exception {
|
||||
try (Socket socket = new Socket(host, port)) {
|
||||
try (Socket socket = new MockSocket(host, port)) {
|
||||
socket.getOutputStream().write("FOOBAR".getBytes(StandardCharsets.UTF_8));
|
||||
socket.getOutputStream().flush();
|
||||
|
||||
|
|
|
@ -32,6 +32,7 @@ import org.elasticsearch.common.settings.Setting;
|
|||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.discovery.DiscoveryModule;
|
||||
import org.elasticsearch.env.Environment;
|
||||
import org.elasticsearch.mocksocket.MockHttpServer;
|
||||
import org.elasticsearch.node.Node;
|
||||
import org.elasticsearch.plugin.discovery.azure.classic.AzureDiscoveryPlugin;
|
||||
import org.elasticsearch.plugins.Plugin;
|
||||
|
@ -131,7 +132,7 @@ public class AzureDiscoveryClusterFormationTests extends ESIntegTestCase {
|
|||
public static void startHttpd() throws Exception {
|
||||
logDir = createTempDir();
|
||||
SSLContext sslContext = getSSLContext();
|
||||
httpsServer = HttpsServer.create(new InetSocketAddress(InetAddress.getLoopbackAddress().getHostAddress(), 0), 0);
|
||||
httpsServer = MockHttpServer.createHttps(new InetSocketAddress(InetAddress.getLoopbackAddress().getHostAddress(), 0), 0);
|
||||
httpsServer.setHttpsConfigurator(new HttpsConfigurator(sslContext));
|
||||
httpsServer.createContext("/subscription/services/hostedservices/myservice", (s) -> {
|
||||
Headers headers = s.getResponseHeaders();
|
||||
|
|
|
@ -29,6 +29,7 @@ import org.elasticsearch.common.SuppressForbidden;
|
|||
import org.elasticsearch.common.io.FileSystemUtils;
|
||||
import org.elasticsearch.common.logging.Loggers;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.mocksocket.MockHttpServer;
|
||||
import org.elasticsearch.plugin.discovery.ec2.Ec2DiscoveryPlugin;
|
||||
import org.elasticsearch.plugins.Plugin;
|
||||
import org.elasticsearch.test.ESIntegTestCase;
|
||||
|
@ -96,7 +97,7 @@ public class Ec2DiscoveryClusterFormationTests extends ESIntegTestCase {
|
|||
@BeforeClass
|
||||
public static void startHttpd() throws Exception {
|
||||
logDir = createTempDir();
|
||||
httpServer = HttpServer.create(new InetSocketAddress(InetAddress.getLoopbackAddress().getHostAddress(), 0), 0);
|
||||
httpServer = MockHttpServer.createHttp(new InetSocketAddress(InetAddress.getLoopbackAddress().getHostAddress(), 0), 0);
|
||||
|
||||
httpServer.createContext("/", (s) -> {
|
||||
Headers headers = s.getResponseHeaders();
|
||||
|
|
|
@ -31,6 +31,7 @@ import org.elasticsearch.common.io.FileSystemUtils;
|
|||
import org.elasticsearch.common.logging.Loggers;
|
||||
import org.elasticsearch.common.settings.Setting;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.mocksocket.MockHttpServer;
|
||||
import org.elasticsearch.plugin.discovery.gce.GceDiscoveryPlugin;
|
||||
import org.elasticsearch.plugins.Plugin;
|
||||
import org.elasticsearch.test.ESIntegTestCase;
|
||||
|
@ -108,8 +109,8 @@ public class GceDiscoverTests extends ESIntegTestCase {
|
|||
public static void startHttpd() throws Exception {
|
||||
logDir = createTempDir();
|
||||
SSLContext sslContext = getSSLContext();
|
||||
httpsServer = HttpsServer.create(new InetSocketAddress(InetAddress.getLoopbackAddress().getHostAddress(), 0), 0);
|
||||
httpServer = HttpServer.create(new InetSocketAddress(InetAddress.getLoopbackAddress().getHostAddress(), 0), 0);
|
||||
httpsServer = MockHttpServer.createHttps(new InetSocketAddress(InetAddress.getLoopbackAddress().getHostAddress(), 0), 0);
|
||||
httpServer = MockHttpServer.createHttp(new InetSocketAddress(InetAddress.getLoopbackAddress().getHostAddress(), 0), 0);
|
||||
httpsServer.setHttpsConfigurator(new HttpsConfigurator(sslContext));
|
||||
httpServer.createContext("/computeMetadata/v1/instance/service-accounts/default/token", (s) -> {
|
||||
String response = GceMockUtils.readGoogleInternalJsonResponse(
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
|
||||
package org.elasticsearch.plugin.example;
|
||||
|
||||
import org.elasticsearch.mocksocket.MockSocket;
|
||||
import org.elasticsearch.test.ESTestCase;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
|
@ -34,7 +35,7 @@ public class ExampleExternalIT extends ESTestCase {
|
|||
String stringAddress = Objects.requireNonNull(System.getProperty("external.address"));
|
||||
URL url = new URL("http://" + stringAddress);
|
||||
InetAddress address = InetAddress.getByName(url.getHost());
|
||||
try (Socket socket = new Socket(address, url.getPort());
|
||||
try (Socket socket = new MockSocket(address, url.getPort());
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8))) {
|
||||
assertEquals("TEST", reader.readLine());
|
||||
}
|
||||
|
|
|
@ -32,6 +32,7 @@ dependencies {
|
|||
compile "commons-logging:commons-logging:${versions.commonslogging}"
|
||||
compile "commons-codec:commons-codec:${versions.commonscodec}"
|
||||
compile "org.elasticsearch:securemock:${versions.securemock}"
|
||||
compile "org.elasticsearch:mocksocket:${versions.mocksocket}"
|
||||
}
|
||||
|
||||
compileJava.options.compilerArgs << '-Xlint:-cast,-rawtypes,-try,-unchecked'
|
||||
|
|
|
@ -26,6 +26,7 @@ import org.elasticsearch.common.network.NetworkUtils;
|
|||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.common.util.CollectionUtils;
|
||||
import org.elasticsearch.env.NodeEnvironment;
|
||||
import org.elasticsearch.mocksocket.MockServerSocket;
|
||||
import org.elasticsearch.test.InternalTestCluster;
|
||||
import org.elasticsearch.test.NodeConfigurationSource;
|
||||
import org.elasticsearch.transport.TransportSettings;
|
||||
|
@ -136,7 +137,7 @@ public class ClusterDiscoveryConfiguration extends NodeConfigurationSource {
|
|||
for (int i = 0; i < unicastHostPorts.length; i++) {
|
||||
boolean foundPortInRange = false;
|
||||
while (tries < InternalTestCluster.PORTS_PER_JVM && !foundPortInRange) {
|
||||
try (ServerSocket serverSocket = new ServerSocket()) {
|
||||
try (ServerSocket serverSocket = new MockServerSocket()) {
|
||||
// Set SO_REUSEADDR as we may bind here and not be able to reuse the address immediately without it.
|
||||
serverSocket.setReuseAddress(NetworkUtils.defaultReuseAddress());
|
||||
serverSocket.bind(new InetSocketAddress(IP_ADDR, nextPort));
|
||||
|
|
|
@ -40,6 +40,7 @@ import org.elasticsearch.common.util.BigArrays;
|
|||
import org.elasticsearch.common.util.concurrent.AbstractRunnable;
|
||||
import org.elasticsearch.common.util.concurrent.ConcurrentCollections;
|
||||
import org.elasticsearch.indices.breaker.NoneCircuitBreakerService;
|
||||
import org.elasticsearch.mocksocket.MockServerSocket;
|
||||
import org.elasticsearch.node.Node;
|
||||
import org.elasticsearch.test.ESTestCase;
|
||||
import org.elasticsearch.test.transport.MockTransportService;
|
||||
|
@ -1366,7 +1367,7 @@ public abstract class AbstractSimpleTransportTestCase extends ESTestCase {
|
|||
// all is well
|
||||
}
|
||||
|
||||
try (Transport.Connection connection = serviceB.openConnection(nodeA, MockTcpTransport.LIGHT_PROFILE)){
|
||||
try (Transport.Connection connection = serviceB.openConnection(nodeA, MockTcpTransport.LIGHT_PROFILE)) {
|
||||
serviceB.handshake(connection, 100);
|
||||
fail("exception should be thrown");
|
||||
} catch (IllegalStateException e) {
|
||||
|
@ -1424,7 +1425,7 @@ public abstract class AbstractSimpleTransportTestCase extends ESTestCase {
|
|||
// all is well
|
||||
}
|
||||
|
||||
try (Transport.Connection connection = serviceB.openConnection(nodeA, MockTcpTransport.LIGHT_PROFILE)){
|
||||
try (Transport.Connection connection = serviceB.openConnection(nodeA, MockTcpTransport.LIGHT_PROFILE)) {
|
||||
serviceB.handshake(connection, 100);
|
||||
fail("exception should be thrown");
|
||||
} catch (IllegalStateException e) {
|
||||
|
@ -1778,7 +1779,7 @@ public abstract class AbstractSimpleTransportTestCase extends ESTestCase {
|
|||
public void testTimeoutPerConnection() throws IOException {
|
||||
assumeTrue("Works only on BSD network stacks and apparently windows",
|
||||
Constants.MAC_OS_X || Constants.FREE_BSD || Constants.WINDOWS);
|
||||
try (ServerSocket socket = new ServerSocket()) {
|
||||
try (ServerSocket socket = new MockServerSocket()) {
|
||||
// note - this test uses backlog=1 which is implementation specific ie. it might not work on some TCP/IP stacks
|
||||
// on linux (at least newer ones) the listen(addr, backlog=1) should just ignore new connections if the queue is full which
|
||||
// means that once we received an ACK from the client we just drop the packet on the floor (which is what we want) and we run
|
||||
|
@ -1823,7 +1824,7 @@ public abstract class AbstractSimpleTransportTestCase extends ESTestCase {
|
|||
NamedWriteableRegistry namedWriteableRegistry = new NamedWriteableRegistry(Collections.emptyList());
|
||||
|
||||
try (MockTcpTransport transport = new MockTcpTransport(Settings.EMPTY, threadPool, BigArrays.NON_RECYCLING_INSTANCE,
|
||||
new NoneCircuitBreakerService(), namedWriteableRegistry, new NetworkService(Settings.EMPTY, Collections.emptyList())){
|
||||
new NoneCircuitBreakerService(), namedWriteableRegistry, new NetworkService(Settings.EMPTY, Collections.emptyList())) {
|
||||
@Override
|
||||
protected String handleRequest(MockChannel mockChannel, String profileName, StreamInput stream, long requestId,
|
||||
int messageLengthBytes, Version version, InetSocketAddress remoteAddress, byte status)
|
||||
|
@ -1854,7 +1855,7 @@ public abstract class AbstractSimpleTransportTestCase extends ESTestCase {
|
|||
}
|
||||
|
||||
public void testTcpHandshakeTimeout() throws IOException {
|
||||
try (ServerSocket socket = new ServerSocket()) {
|
||||
try (ServerSocket socket = new MockServerSocket()) {
|
||||
socket.bind(new InetSocketAddress(InetAddress.getLocalHost(), 0), 1);
|
||||
socket.setReuseAddress(true);
|
||||
DiscoveryNode dummy = new DiscoveryNode("TEST", new TransportAddress(socket.getInetAddress(),
|
||||
|
@ -1870,12 +1871,12 @@ public abstract class AbstractSimpleTransportTestCase extends ESTestCase {
|
|||
builder.setHandshakeTimeout(TimeValue.timeValueMillis(1));
|
||||
ConnectTransportException ex = expectThrows(ConnectTransportException.class,
|
||||
() -> serviceA.connectToNode(dummy, builder.build()));
|
||||
assertEquals("[][" + dummy.getAddress() +"] handshake_timeout[1ms]", ex.getMessage());
|
||||
assertEquals("[][" + dummy.getAddress() + "] handshake_timeout[1ms]", ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void testTcpHandshakeConnectionReset() throws IOException, InterruptedException {
|
||||
try (ServerSocket socket = new ServerSocket()) {
|
||||
try (ServerSocket socket = new MockServerSocket()) {
|
||||
socket.bind(new InetSocketAddress(InetAddress.getLocalHost(), 0), 1);
|
||||
socket.setReuseAddress(true);
|
||||
DiscoveryNode dummy = new DiscoveryNode("TEST", new TransportAddress(socket.getInetAddress(),
|
||||
|
@ -1904,7 +1905,7 @@ public abstract class AbstractSimpleTransportTestCase extends ESTestCase {
|
|||
builder.setHandshakeTimeout(TimeValue.timeValueHours(1));
|
||||
ConnectTransportException ex = expectThrows(ConnectTransportException.class,
|
||||
() -> serviceA.connectToNode(dummy, builder.build()));
|
||||
assertEquals(ex.getMessage(), "[][" + dummy.getAddress() +"] general node connection failure");
|
||||
assertEquals(ex.getMessage(), "[][" + dummy.getAddress() + "] general node connection failure");
|
||||
assertThat(ex.getCause().getMessage(), startsWith("handshake failed"));
|
||||
t.join();
|
||||
}
|
||||
|
|
|
@ -35,6 +35,8 @@ import org.elasticsearch.common.util.CancellableThreads;
|
|||
import org.elasticsearch.common.util.concurrent.AbstractRunnable;
|
||||
import org.elasticsearch.common.util.concurrent.EsExecutors;
|
||||
import org.elasticsearch.indices.breaker.CircuitBreakerService;
|
||||
import org.elasticsearch.mocksocket.MockServerSocket;
|
||||
import org.elasticsearch.mocksocket.MockSocket;
|
||||
import org.elasticsearch.threadpool.ThreadPool;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
|
@ -110,7 +112,7 @@ public class MockTcpTransport extends TcpTransport<MockTcpTransport.MockChannel>
|
|||
|
||||
@Override
|
||||
protected MockChannel bind(final String name, InetSocketAddress address) throws IOException {
|
||||
ServerSocket socket = new ServerSocket();
|
||||
MockServerSocket socket = new MockServerSocket();
|
||||
socket.bind(address);
|
||||
socket.setReuseAddress(TCP_REUSE_ADDRESS.get(settings));
|
||||
ByteSizeValue tcpReceiveBufferSize = TCP_RECEIVE_BUFFER_SIZE.get(settings);
|
||||
|
@ -178,7 +180,7 @@ public class MockTcpTransport extends TcpTransport<MockTcpTransport.MockChannel>
|
|||
final MockChannel[] mockChannels = new MockChannel[1];
|
||||
final NodeChannels nodeChannels = new NodeChannels(node, mockChannels, LIGHT_PROFILE); // we always use light here
|
||||
boolean success = false;
|
||||
final Socket socket = new Socket();
|
||||
final MockSocket socket = new MockSocket();
|
||||
try {
|
||||
Consumer<MockChannel> onClose = (channel) -> {
|
||||
final NodeChannels connected = connectedNodes.get(node);
|
||||
|
|
Loading…
Reference in New Issue