-
-
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.
Allow DOCKER_HOST and related settings to be set in `~/.testcontainer…
…s.properties` (#4118)
- Loading branch information
Showing
5 changed files
with
171 additions
and
10 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
91 changes: 91 additions & 0 deletions
91
...g/testcontainers/dockerclient/EnvironmentAndSystemPropertyClientProviderStrategyTest.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,91 @@ | ||
package org.testcontainers.dockerclient; | ||
|
||
import com.github.dockerjava.core.DefaultDockerClientConfig; | ||
import com.github.dockerjava.core.LocalDirectorySSLConfig; | ||
import com.github.dockerjava.transport.SSLConfig; | ||
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.io.IOException; | ||
import java.net.URI; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.ArgumentMatchers.isNull; | ||
|
||
/** | ||
* Test that we can use Testcontainers configuration file to override settings. We assume that docker-java has test | ||
* coverage for detection of environment variables (e.g. DOCKER_HOST) and its own properties config file. | ||
*/ | ||
@RunWith(MockitoJUnitRunner.class) | ||
public class EnvironmentAndSystemPropertyClientProviderStrategyTest { | ||
|
||
@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(); | ||
defaultDockerHost = defaultConfig.getDockerHost(); | ||
defaultSSLConfig = defaultConfig.getSSLConfig(); | ||
} | ||
|
||
@Test | ||
public void testWhenConfigAbsent() { | ||
Mockito.doReturn(null).when(TestcontainersConfiguration.getInstance()).getEnvVarOrUserProperty(eq("docker.host"), isNull()); | ||
Mockito.doReturn(null).when(TestcontainersConfiguration.getInstance()).getEnvVarOrUserProperty(eq("docker.tls.verify"), isNull()); | ||
Mockito.doReturn(null).when(TestcontainersConfiguration.getInstance()).getEnvVarOrUserProperty(eq("docker.cert.path"), isNull()); | ||
|
||
EnvironmentAndSystemPropertyClientProviderStrategy strategy = new EnvironmentAndSystemPropertyClientProviderStrategy(); | ||
|
||
TransportConfig transportConfig = strategy.getTransportConfig(); | ||
assertEquals(defaultDockerHost, transportConfig.getDockerHost()); | ||
assertEquals(defaultSSLConfig, transportConfig.getSslConfig()); | ||
} | ||
|
||
@Test | ||
public void testWhenDockerHostPresent() { | ||
Mockito.doReturn("tcp://1.2.3.4:2375").when(TestcontainersConfiguration.getInstance()).getEnvVarOrUserProperty(eq("docker.host"), isNull()); | ||
Mockito.doReturn(null).when(TestcontainersConfiguration.getInstance()).getEnvVarOrUserProperty(eq("docker.tls.verify"), isNull()); | ||
Mockito.doReturn(null).when(TestcontainersConfiguration.getInstance()).getEnvVarOrUserProperty(eq("docker.cert.path"), isNull()); | ||
|
||
EnvironmentAndSystemPropertyClientProviderStrategy strategy = new EnvironmentAndSystemPropertyClientProviderStrategy(); | ||
|
||
TransportConfig transportConfig = strategy.getTransportConfig(); | ||
assertEquals("tcp://1.2.3.4:2375", transportConfig.getDockerHost().toString()); | ||
assertEquals(defaultSSLConfig, transportConfig.getSslConfig()); | ||
} | ||
|
||
@Test | ||
public void testWhenDockerHostAndSSLConfigPresent() throws IOException { | ||
Path tempDir = Files.createTempDirectory("testcontainers-test"); | ||
String tempDirPath = tempDir.toAbsolutePath().toString(); | ||
|
||
Mockito.doReturn("tcp://1.2.3.4:2375").when(TestcontainersConfiguration.getInstance()).getEnvVarOrUserProperty(eq("docker.host"), isNull()); | ||
Mockito.doReturn("1").when(TestcontainersConfiguration.getInstance()).getEnvVarOrUserProperty(eq("docker.tls.verify"), isNull()); | ||
Mockito.doReturn(tempDirPath).when(TestcontainersConfiguration.getInstance()).getEnvVarOrUserProperty(eq("docker.cert.path"), isNull()); | ||
|
||
EnvironmentAndSystemPropertyClientProviderStrategy strategy = new EnvironmentAndSystemPropertyClientProviderStrategy(); | ||
|
||
TransportConfig transportConfig = strategy.getTransportConfig(); | ||
assertEquals("tcp://1.2.3.4:2375", transportConfig.getDockerHost().toString()); | ||
|
||
SSLConfig sslConfig = transportConfig.getSslConfig(); | ||
assertNotNull(sslConfig); | ||
assertTrue(sslConfig instanceof LocalDirectorySSLConfig); | ||
assertEquals(tempDirPath, ((LocalDirectorySSLConfig) sslConfig).getDockerCertPath()); | ||
} | ||
} |
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