-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add TestcontainersHostPropertyClientStrategy (#7053)
New strategy to use `tc.host` in `~/.testcontainers.properties` to try and connect to docker. This strategy ensure that user-defined variables such as `DOCKER_HOST`, `TESTCONTAINERS_HOST_OVERRIDE` and `TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE` do not have an effect. This strategy takes precedence over other strategies.
- Loading branch information
1 parent
fc770df
commit eb5eb8a
Showing
7 changed files
with
168 additions
and
19 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
63 changes: 63 additions & 0 deletions
63
...ava/org/testcontainers/dockerclient/TestcontainersHostPropertyClientProviderStrategy.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,63 @@ | ||
package org.testcontainers.dockerclient; | ||
|
||
import com.github.dockerjava.core.DefaultDockerClientConfig; | ||
import com.github.dockerjava.core.DockerClientConfig; | ||
import org.testcontainers.utility.TestcontainersConfiguration; | ||
|
||
import java.util.Optional; | ||
|
||
/** | ||
* Use <code>tc.host</code> in <code>~/.testcontainers.properties</code> | ||
* to try and locate a docker environment. | ||
* | ||
* @deprecated this class is used by the SPI and should not be used directly | ||
*/ | ||
@Deprecated | ||
public final class TestcontainersHostPropertyClientProviderStrategy extends DockerClientProviderStrategy { | ||
|
||
public static final int PRIORITY = EnvironmentAndSystemPropertyClientProviderStrategy.PRIORITY - 10; | ||
|
||
private final DockerClientConfig dockerClientConfig; | ||
|
||
public TestcontainersHostPropertyClientProviderStrategy() { | ||
this(DefaultDockerClientConfig.createDefaultConfigBuilder()); | ||
} | ||
|
||
public TestcontainersHostPropertyClientProviderStrategy(DefaultDockerClientConfig.Builder configBuilder) { | ||
Optional<String> tcHost = Optional.ofNullable( | ||
TestcontainersConfiguration.getInstance().getUserProperty("tc.host", null) | ||
); | ||
|
||
tcHost.ifPresent(configBuilder::withDockerHost); | ||
this.dockerClientConfig = configBuilder.build(); | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "Testcontainers Host with tc.host=" + this.dockerClientConfig.getDockerHost(); | ||
} | ||
|
||
@Override | ||
public TransportConfig getTransportConfig() throws InvalidConfigurationException { | ||
return TransportConfig | ||
.builder() | ||
.dockerHost(dockerClientConfig.getDockerHost()) | ||
.sslConfig(dockerClientConfig.getSSLConfig()) | ||
.build(); | ||
} | ||
|
||
@Override | ||
protected int getPriority() { | ||
return PRIORITY; | ||
} | ||
|
||
@Override | ||
protected boolean isPersistable() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public boolean allowUserOverrides() { | ||
return false; | ||
} | ||
} |
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
1 change: 1 addition & 0 deletions
1
.../resources/META-INF/services/org.testcontainers.dockerclient.DockerClientProviderStrategy
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
61 changes: 61 additions & 0 deletions
61
...org/testcontainers/dockerclient/TestcontainersHostPropertyClientProviderStrategyTest.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,61 @@ | ||
package org.testcontainers.dockerclient; | ||
|
||
import com.github.dockerjava.core.DefaultDockerClientConfig; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mockito; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
import org.testcontainers.utility.MockTestcontainersConfigurationRule; | ||
import org.testcontainers.utility.TestcontainersConfiguration; | ||
|
||
import java.net.URI; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.ArgumentMatchers.isNull; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class TestcontainersHostPropertyClientProviderStrategyTest { | ||
|
||
@Rule | ||
public MockTestcontainersConfigurationRule mockConfig = new MockTestcontainersConfigurationRule(); | ||
|
||
private URI defaultDockerHost; | ||
|
||
private com.github.dockerjava.core.SSLConfig defaultSSLConfig; | ||
|
||
@Before | ||
public void checkEnvironmentClear() { | ||
// If docker-java picks up non-default settings from the environment, our test needs to know to expect those | ||
DefaultDockerClientConfig defaultConfig = DefaultDockerClientConfig.createDefaultConfigBuilder().build(); | ||
this.defaultDockerHost = defaultConfig.getDockerHost(); | ||
this.defaultSSLConfig = defaultConfig.getSSLConfig(); | ||
} | ||
|
||
@Test | ||
public void tcHostPropertyIsProvided() { | ||
Mockito | ||
.doReturn("tcp://127.0.0.1:9000") | ||
.when(TestcontainersConfiguration.getInstance()) | ||
.getUserProperty(eq("tc.host"), isNull()); | ||
|
||
TestcontainersHostPropertyClientProviderStrategy strategy = new TestcontainersHostPropertyClientProviderStrategy(); | ||
|
||
TransportConfig transportConfig = strategy.getTransportConfig(); | ||
assertThat(transportConfig.getDockerHost().toString()).isEqualTo("tcp://127.0.0.1:9000"); | ||
assertThat(transportConfig.getSslConfig()).isEqualTo(this.defaultSSLConfig); | ||
} | ||
|
||
@Test | ||
public void tcHostPropertyIsNotProvided() { | ||
Mockito.doReturn(null).when(TestcontainersConfiguration.getInstance()).getUserProperty(eq("tc.host"), isNull()); | ||
|
||
TestcontainersHostPropertyClientProviderStrategy strategy = new TestcontainersHostPropertyClientProviderStrategy(); | ||
|
||
TransportConfig transportConfig = strategy.getTransportConfig(); | ||
assertThat(transportConfig.getDockerHost()).isEqualTo(this.defaultDockerHost); | ||
assertThat(transportConfig.getSslConfig()).isEqualTo(this.defaultSSLConfig); | ||
} | ||
} |