11package com .github .dockerjava .core ;
22
3- import com .github .dockerjava .api .exception .DockerClientException ;
43import com .github .dockerjava .api .model .AuthConfig ;
54import com .github .dockerjava .api .model .AuthConfigurations ;
65import com .google .common .io .Resources ;
6+ import java .io .IOException ;
77import org .apache .commons .lang3 .SerializationUtils ;
88import org .junit .Test ;
99
2727public class DefaultDockerClientConfigTest {
2828
2929 public static final DefaultDockerClientConfig EXAMPLE_CONFIG = newExampleConfig ();
30+ public static final DefaultDockerClientConfig EXAMPLE_CONFIG_FULLY_LOADED = newExampleConfigFullyLoaded ();
3031
3132 private static DefaultDockerClientConfig newExampleConfig () {
32-
3333 String dockerCertPath = dockerCertPath ();
34+ return new DefaultDockerClientConfig (URI .create ("tcp://foo" ), null , "dockerConfig" , "apiVersion" , "registryUrl" ,
35+ "registryUsername" , "registryPassword" , "registryEmail" ,
36+ new LocalDirectorySSLConfig (dockerCertPath ));
37+ }
3438
35- return new DefaultDockerClientConfig (URI .create ("tcp://foo" ), "dockerConfig" , "apiVersion" , "registryUrl" , "registryUsername" , "registryPassword" , "registryEmail" ,
39+ private static DefaultDockerClientConfig newExampleConfigFullyLoaded () {
40+ try {
41+ String dockerCertPath = dockerCertPath ();
42+ String dockerConfig = "dockerConfig" ;
43+ DockerConfigFile loadedConfigFile = DockerConfigFile .loadConfig (DockerClientConfig .getDefaultObjectMapper (), dockerConfig );
44+ return new DefaultDockerClientConfig (URI .create ("tcp://foo" ), loadedConfigFile , dockerConfig , "apiVersion" , "registryUrl" ,
45+ "registryUsername" , "registryPassword" , "registryEmail" ,
3646 new LocalDirectorySSLConfig (dockerCertPath ));
47+ } catch (IOException exception ) {
48+ throw new RuntimeException (exception );
49+ }
3750 }
3851
3952 private static String homeDir () {
@@ -69,6 +82,37 @@ public void environmentDockerHost() throws Exception {
6982 assertEquals (config .getDockerHost (), URI .create ("tcp://baz:8768" ));
7083 }
7184
85+ @ Test
86+ public void dockerContextFromConfig () throws Exception {
87+ // given home directory with docker contexts configured
88+ Properties systemProperties = new Properties ();
89+ systemProperties .setProperty ("user.home" , "target/test-classes/dockerContextHomeDir" );
90+
91+ // and an empty environment
92+ Map <String , String > env = new HashMap <>();
93+
94+ // when you build a config
95+ DefaultDockerClientConfig config = buildConfig (env , systemProperties );
96+
97+ assertEquals (URI .create ("unix:///configcontext.sock" ), config .getDockerHost ());
98+ }
99+
100+ @ Test
101+ public void dockerContextFromEnvironmentVariable () throws Exception {
102+ // given home directory with docker contexts
103+ Properties systemProperties = new Properties ();
104+ systemProperties .setProperty ("user.home" , "target/test-classes/dockerContextHomeDir" );
105+
106+ // and an environment variable that overrides docker context
107+ Map <String , String > env = new HashMap <>();
108+ env .put (DefaultDockerClientConfig .DOCKER_CONTEXT , "envvarcontext" );
109+
110+ // when you build a config
111+ DefaultDockerClientConfig config = buildConfig (env , systemProperties );
112+
113+ assertEquals (URI .create ("unix:///envvarcontext.sock" ), config .getDockerHost ());
114+ }
115+
72116 @ Test
73117 public void environment () throws Exception {
74118
@@ -88,7 +132,7 @@ public void environment() throws Exception {
88132 DefaultDockerClientConfig config = buildConfig (env , new Properties ());
89133
90134 // then we get the example object
91- assertEquals (config , EXAMPLE_CONFIG );
135+ assertEquals (EXAMPLE_CONFIG_FULLY_LOADED , config );
92136 }
93137
94138 @ Test
@@ -147,7 +191,7 @@ public void systemProperties() throws Exception {
147191 DefaultDockerClientConfig config = buildConfig (Collections .<String , String > emptyMap (), systemProperties );
148192
149193 // then it is the same as the example
150- assertEquals (config , EXAMPLE_CONFIG );
194+ assertEquals (EXAMPLE_CONFIG_FULLY_LOADED , config );
151195
152196 }
153197
@@ -161,22 +205,22 @@ public void serializableTest() {
161205
162206 @ Test ()
163207 public void testSslContextEmpty () throws Exception {
164- new DefaultDockerClientConfig (URI .create ("tcp://foo" ), "dockerConfig" , "apiVersion" , "registryUrl" , "registryUsername" , "registryPassword" , "registryEmail" ,
208+ new DefaultDockerClientConfig (URI .create ("tcp://foo" ), new DockerConfigFile (), "dockerConfig" , "apiVersion" , "registryUrl" , "registryUsername" , "registryPassword" , "registryEmail" ,
165209 null );
166210 }
167211
168212
169213
170214 @ Test ()
171215 public void testTlsVerifyAndCertPath () throws Exception {
172- new DefaultDockerClientConfig (URI .create ("tcp://foo" ), "dockerConfig" , "apiVersion" , "registryUrl" , "registryUsername" , "registryPassword" , "registryEmail" ,
216+ new DefaultDockerClientConfig (URI .create ("tcp://foo" ), new DockerConfigFile (), "dockerConfig" , "apiVersion" , "registryUrl" , "registryUsername" , "registryPassword" , "registryEmail" ,
173217 new LocalDirectorySSLConfig (dockerCertPath ()));
174218 }
175219
176220 @ Test ()
177221 public void testAnyHostScheme () throws Exception {
178222 URI dockerHost = URI .create ("a" + UUID .randomUUID ().toString ().replace ("-" , "" ) + "://foo" );
179- new DefaultDockerClientConfig (dockerHost , "dockerConfig" , "apiVersion" , "registryUrl" , "registryUsername" , "registryPassword" , "registryEmail" ,
223+ new DefaultDockerClientConfig (dockerHost , new DockerConfigFile (), "dockerConfig" , "apiVersion" , "registryUrl" , "registryUsername" , "registryPassword" , "registryEmail" ,
180224 null );
181225 }
182226
@@ -249,10 +293,12 @@ public void dockerHostSetExplicitlyIfSetToDefaultByUser() {
249293
250294
251295 @ Test
252- public void testGetAuthConfigurationsFromDockerCfg () throws URISyntaxException {
296+ public void testGetAuthConfigurationsFromDockerCfg () throws URISyntaxException , IOException {
253297 File cfgFile = new File (Resources .getResource ("com.github.dockerjava.core/registry.v1" ).toURI ());
298+ DockerConfigFile dockerConfigFile =
299+ DockerConfigFile .loadConfig (DockerClientConfig .getDefaultObjectMapper (), cfgFile .getAbsolutePath ());
254300 DefaultDockerClientConfig clientConfig = new DefaultDockerClientConfig (URI .create (
255- "unix://foo" ), cfgFile .getAbsolutePath (), "apiVersion" , "registryUrl" , "registryUsername" , "registryPassword" ,
301+ "unix://foo" ), dockerConfigFile , cfgFile .getAbsolutePath (), "apiVersion" , "registryUrl" , "registryUsername" , "registryPassword" ,
256302 "registryEmail" , null );
257303
258304 AuthConfigurations authConfigurations = clientConfig .getAuthConfigurations ();
@@ -265,10 +311,12 @@ public void testGetAuthConfigurationsFromDockerCfg() throws URISyntaxException {
265311 }
266312
267313 @ Test
268- public void testGetAuthConfigurationsFromConfigJson () throws URISyntaxException {
314+ public void testGetAuthConfigurationsFromConfigJson () throws URISyntaxException , IOException {
269315 File cfgFile = new File (Resources .getResource ("com.github.dockerjava.core/registry.v2" ).toURI ());
316+ DockerConfigFile dockerConfigFile =
317+ DockerConfigFile .loadConfig (DockerClientConfig .getDefaultObjectMapper (), cfgFile .getAbsolutePath ());
270318 DefaultDockerClientConfig clientConfig = new DefaultDockerClientConfig (URI .create (
271- "unix://foo" ), cfgFile .getAbsolutePath (), "apiVersion" , "registryUrl" , "registryUsername" , "registryPassword" ,
319+ "unix://foo" ), dockerConfigFile , cfgFile .getAbsolutePath (), "apiVersion" , "registryUrl" , "registryUsername" , "registryPassword" ,
272320 "registryEmail" , null );
273321
274322 AuthConfigurations authConfigurations = clientConfig .getAuthConfigurations ();
0 commit comments