Skip to content

Commit

Permalink
Use delegate ProxySelector if can't find route (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
SerialVelocity authored Jul 5, 2017
1 parent 2c431e0 commit a47db2d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
5 changes: 4 additions & 1 deletion src/main/java/com/palantir/docker/proxy/DockerProxyRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ public void before() throws IOException, InterruptedException {
originalProxySelector = ProxySelector.getDefault();
dockerComposeRule.before();
getNameServices().add(0, new DockerNameService(dockerContainerInfo));
ProxySelector.setDefault(new DockerProxySelector(dockerComposeRule.containers(), dockerContainerInfo));
ProxySelector.setDefault(new DockerProxySelector(
dockerComposeRule.containers(),
dockerContainerInfo,
originalProxySelector));
} catch (DockerExecutionException e) {
if (e.getMessage().contains("declared as external")) {
throw new IllegalStateException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ public class DockerProxySelector extends ProxySelector {

private final InetSocketAddress proxyAddress;
private final DockerContainerInfo containerInfo;
private final ProxySelector delegate;

public DockerProxySelector(Cluster containers, DockerContainerInfo containerInfo) {
public DockerProxySelector(Cluster containers, DockerContainerInfo containerInfo, ProxySelector delegate) {
this.proxyAddress = InetSocketAddress.createUnresolved(
containers.ip(),
containers.container(PROXY_CONTAINER_NAME).port(PROXY_CONTAINER_PORT).getExternalPort());
this.containerInfo = containerInfo;
this.delegate = delegate;
}

@Override
Expand All @@ -37,12 +39,13 @@ public List<Proxy> select(URI uri) {
if (containerIpForUriHost.isPresent() || containerHostForUriHost.isPresent()) {
return ImmutableList.of(new Proxy(Proxy.Type.SOCKS, proxyAddress));
} else {
return ImmutableList.of(Proxy.NO_PROXY);
return delegate.select(uri);
}
}

@Override
public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
Preconditions.checkArgument(uri != null && sa != null && ioe != null, "Invalid connectFailed call");
delegate.connectFailed(uri, sa, ioe);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@
package com.palantir.docker.proxy;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableList;
import com.palantir.docker.compose.connection.Cluster;
import com.palantir.docker.compose.connection.Container;
import com.palantir.docker.compose.connection.ContainerCache;
Expand All @@ -16,10 +20,12 @@
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Optional;
import org.junit.Before;
import org.junit.Test;

public class DockerProxySelectorTest {
Expand All @@ -34,17 +40,27 @@ public class DockerProxySelectorTest {
private static final URI TEST_HOSTNAME_URI = createUriUnsafe("http://some-address");

private final DockerContainerInfo containerInfo = mock(DockerContainerInfo.class);
private final DockerProxySelector dockerProxySelector = new DockerProxySelector(setupProxyContainer(),
containerInfo);
private final ProxySelector originalProxySelector = mock(ProxySelector.class);
private final ProxySelector dockerProxySelector = new DockerProxySelector(
setupProxyContainer(),
containerInfo,
originalProxySelector);

@Before
public void originalProxySelectorIsNoProxy() {
when(originalProxySelector.select(any())).thenReturn(ImmutableList.of(Proxy.NO_PROXY));
}

@Test
public void nonDockerAddressesShouldNotGoThroughAProxy() {
public void nonDockerAddressesShouldDelegateToPassedInSelector() {
when(containerInfo.getIpForHost(TEST_HOSTNAME)).thenReturn(Optional.empty());
when(containerInfo.getHostForIp(TEST_HOSTNAME)).thenReturn(Optional.empty());

List<Proxy> selectedProxy = dockerProxySelector.select(TEST_HOSTNAME_URI);

assertThat(selectedProxy).containsExactly(Proxy.NO_PROXY);

verify(originalProxySelector, times(1)).select(TEST_HOSTNAME_URI);
}

@Test
Expand Down Expand Up @@ -97,6 +113,17 @@ public void connectionFailedShouldNotThrowOnValidArguments() {
new IOException());
}

@Test
public void connectionFailedShouldDelegateToPassedInSelector() {
IOException exception = new IOException();
dockerProxySelector.connectFailed(
TEST_HOSTNAME_URI,
PROXY_ADDRESS,
exception);

verify(originalProxySelector, times(1)).connectFailed(TEST_HOSTNAME_URI, PROXY_ADDRESS, exception);
}

private static Cluster setupProxyContainer() {
Container proxyContainer = mock(Container.class);
when(proxyContainer.port(DockerProxySelector.PROXY_CONTAINER_PORT))
Expand Down

0 comments on commit a47db2d

Please sign in to comment.