-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add optional OkHttp transport (instead of Netty) (#710)
* add OkHttp transport * implement chunked response handling based on Netty implementation * handle response errors * do not block caller's thread * split files, support more protocols, fix blocking call * mark OkHttpInvocationBuilder & OkHttpWebTarget as internal classes * re-throw ExecutionException's cause * always close the response * reuse OkHttpClient instance * shade okhttp / okio, disable pooling of unix-socket based transport * do not block caller's thread (well, again :D) * retry on connection failure * do not use mutable "." directory in DirectoryTarResourceTest * replace docker-filesocket with plain AFUNIXSocket factory * fix shaded dependencies, rename handleStreamedResponse -> executeAndStream * fix shading * remove isOpen check * try jnr-unixsocket * remove cglib * try org.scala-sbt.ipcsocket:ipcsocket * experimental npipe support * fix pipe name * fix double escaping * shade ipcsocket * disable read timeout * make OkHttp opt-in-able * fix properties file name * fix typo * fix Codacy warnings * workaround docker-java (does not support npipe) * support npipe in DockerClientConfigUtils
- Loading branch information
Showing
15 changed files
with
894 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
core/src/main/java/org/testcontainers/dockerclient/NpipeSocketClientProviderStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package org.testcontainers.dockerclient; | ||
|
||
import com.github.dockerjava.core.DefaultDockerClientConfig; | ||
import com.github.dockerjava.core.DockerClientConfig; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.experimental.Delegate; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.commons.lang.SystemUtils; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.net.URI; | ||
|
||
@Slf4j | ||
public class NpipeSocketClientProviderStrategy extends DockerClientProviderStrategy { | ||
|
||
protected static final String DOCKER_SOCK_PATH = "//./pipe/docker_engine"; | ||
private static final String SOCKET_LOCATION = "npipe://" + DOCKER_SOCK_PATH; | ||
|
||
private static final String PING_TIMEOUT_DEFAULT = "10"; | ||
private static final String PING_TIMEOUT_PROPERTY_NAME = "testcontainers.npipesocketprovider.timeout"; | ||
|
||
public static final int PRIORITY = EnvironmentAndSystemPropertyClientProviderStrategy.PRIORITY - 20; | ||
|
||
@Override | ||
protected boolean isApplicable() { | ||
return SystemUtils.IS_OS_WINDOWS; | ||
} | ||
|
||
@Override | ||
public void test() throws InvalidConfigurationException { | ||
try { | ||
config = tryConfiguration(); | ||
log.info("Accessing docker with {}", getDescription()); | ||
} catch (Exception | UnsatisfiedLinkError e) { | ||
throw new InvalidConfigurationException("ping failed", e); | ||
} | ||
} | ||
|
||
@NotNull | ||
private DockerClientConfig tryConfiguration() { | ||
URI dockerHost = URI.create(SOCKET_LOCATION); | ||
|
||
config = new DelegatingDockerClientConfig( | ||
DefaultDockerClientConfig.createDefaultConfigBuilder() | ||
.withDockerHost("tcp://localhost:0") | ||
.withDockerTlsVerify(false) | ||
.build() | ||
) { | ||
@Override | ||
public URI getDockerHost() { | ||
return dockerHost; | ||
} | ||
}; | ||
client = getClientForConfig(config); | ||
|
||
final int timeout = Integer.parseInt(System.getProperty(PING_TIMEOUT_PROPERTY_NAME, PING_TIMEOUT_DEFAULT)); | ||
ping(client, timeout); | ||
|
||
return config; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "local Npipe socket (" + SOCKET_LOCATION + ")"; | ||
} | ||
|
||
@Override | ||
protected int getPriority() { | ||
return PRIORITY; | ||
} | ||
|
||
@RequiredArgsConstructor | ||
private static class DelegatingDockerClientConfig implements DockerClientConfig { | ||
|
||
@Delegate | ||
final DockerClientConfig dockerClientConfig; | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
...rc/main/java/org/testcontainers/dockerclient/transport/okhttp/NamedPipeSocketFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package org.testcontainers.dockerclient.transport.okhttp; | ||
|
||
import lombok.SneakyThrows; | ||
import lombok.Value; | ||
import org.scalasbt.ipcsocket.Win32NamedPipeSocket; | ||
|
||
import javax.net.SocketFactory; | ||
import java.io.FilterInputStream; | ||
import java.io.FilterOutputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.net.InetAddress; | ||
import java.net.Socket; | ||
import java.net.SocketAddress; | ||
|
||
@Value | ||
public class NamedPipeSocketFactory extends SocketFactory { | ||
|
||
String socketPath; | ||
|
||
@Override | ||
@SneakyThrows | ||
public Socket createSocket() { | ||
return new Win32NamedPipeSocket(socketPath.replace("/", "\\")) { | ||
|
||
@Override | ||
public void connect(SocketAddress endpoint, int timeout) throws IOException { | ||
// Do nothing since it's not "connectable" | ||
} | ||
|
||
@Override | ||
public InputStream getInputStream() { | ||
return new FilterInputStream(super.getInputStream()) { | ||
@Override | ||
public void close() throws IOException { | ||
shutdownInput(); | ||
} | ||
}; | ||
} | ||
|
||
@Override | ||
public OutputStream getOutputStream() { | ||
return new FilterOutputStream(super.getOutputStream()) { | ||
|
||
@Override | ||
public void write(byte[] b, int off, int len) throws IOException { | ||
out.write(b, off, len); | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
shutdownOutput(); | ||
} | ||
}; | ||
} | ||
}; | ||
} | ||
|
||
@Override | ||
public Socket createSocket(String s, int i) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public Socket createSocket(String s, int i, InetAddress inetAddress, int i1) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public Socket createSocket(InetAddress inetAddress, int i) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public Socket createSocket(InetAddress inetAddress, int i, InetAddress inetAddress1, int i1) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
} |
Oops, something went wrong.