1
1
package com .github .dockerjava .core ;
2
2
3
- import com .github .dockerjava .api .exception .DockerClientException ;
4
3
import com .github .dockerjava .api .model .AuthConfig ;
5
4
import com .github .dockerjava .api .model .AuthConfigurations ;
6
5
import com .google .common .io .Resources ;
6
+ import java .io .IOException ;
7
7
import org .apache .commons .lang3 .SerializationUtils ;
8
8
import org .junit .Test ;
9
9
27
27
public class DefaultDockerClientConfigTest {
28
28
29
29
public static final DefaultDockerClientConfig EXAMPLE_CONFIG = newExampleConfig ();
30
+ public static final DefaultDockerClientConfig EXAMPLE_CONFIG_FULLY_LOADED = newExampleConfigFullyLoaded ();
30
31
31
32
private static DefaultDockerClientConfig newExampleConfig () {
32
-
33
33
String dockerCertPath = dockerCertPath ();
34
+ return new DefaultDockerClientConfig (URI .create ("tcp://foo" ), null , "dockerConfig" , "apiVersion" , "registryUrl" ,
35
+ "registryUsername" , "registryPassword" , "registryEmail" ,
36
+ new LocalDirectorySSLConfig (dockerCertPath ));
37
+ }
34
38
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" ,
36
46
new LocalDirectorySSLConfig (dockerCertPath ));
47
+ } catch (IOException exception ) {
48
+ throw new RuntimeException (exception );
49
+ }
37
50
}
38
51
39
52
private static String homeDir () {
@@ -69,6 +82,37 @@ public void environmentDockerHost() throws Exception {
69
82
assertEquals (config .getDockerHost (), URI .create ("tcp://baz:8768" ));
70
83
}
71
84
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
+
72
116
@ Test
73
117
public void environment () throws Exception {
74
118
@@ -88,7 +132,7 @@ public void environment() throws Exception {
88
132
DefaultDockerClientConfig config = buildConfig (env , new Properties ());
89
133
90
134
// then we get the example object
91
- assertEquals (config , EXAMPLE_CONFIG );
135
+ assertEquals (EXAMPLE_CONFIG_FULLY_LOADED , config );
92
136
}
93
137
94
138
@ Test
@@ -147,7 +191,7 @@ public void systemProperties() throws Exception {
147
191
DefaultDockerClientConfig config = buildConfig (Collections .<String , String > emptyMap (), systemProperties );
148
192
149
193
// then it is the same as the example
150
- assertEquals (config , EXAMPLE_CONFIG );
194
+ assertEquals (EXAMPLE_CONFIG_FULLY_LOADED , config );
151
195
152
196
}
153
197
@@ -161,22 +205,22 @@ public void serializableTest() {
161
205
162
206
@ Test ()
163
207
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" ,
165
209
null );
166
210
}
167
211
168
212
169
213
170
214
@ Test ()
171
215
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" ,
173
217
new LocalDirectorySSLConfig (dockerCertPath ()));
174
218
}
175
219
176
220
@ Test ()
177
221
public void testAnyHostScheme () throws Exception {
178
222
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" ,
180
224
null );
181
225
}
182
226
@@ -249,10 +293,12 @@ public void dockerHostSetExplicitlyIfSetToDefaultByUser() {
249
293
250
294
251
295
@ Test
252
- public void testGetAuthConfigurationsFromDockerCfg () throws URISyntaxException {
296
+ public void testGetAuthConfigurationsFromDockerCfg () throws URISyntaxException , IOException {
253
297
File cfgFile = new File (Resources .getResource ("com.github.dockerjava.core/registry.v1" ).toURI ());
298
+ DockerConfigFile dockerConfigFile =
299
+ DockerConfigFile .loadConfig (DockerClientConfig .getDefaultObjectMapper (), cfgFile .getAbsolutePath ());
254
300
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" ,
256
302
"registryEmail" , null );
257
303
258
304
AuthConfigurations authConfigurations = clientConfig .getAuthConfigurations ();
@@ -265,10 +311,12 @@ public void testGetAuthConfigurationsFromDockerCfg() throws URISyntaxException {
265
311
}
266
312
267
313
@ Test
268
- public void testGetAuthConfigurationsFromConfigJson () throws URISyntaxException {
314
+ public void testGetAuthConfigurationsFromConfigJson () throws URISyntaxException , IOException {
269
315
File cfgFile = new File (Resources .getResource ("com.github.dockerjava.core/registry.v2" ).toURI ());
316
+ DockerConfigFile dockerConfigFile =
317
+ DockerConfigFile .loadConfig (DockerClientConfig .getDefaultObjectMapper (), cfgFile .getAbsolutePath ());
270
318
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" ,
272
320
"registryEmail" , null );
273
321
274
322
AuthConfigurations authConfigurations = clientConfig .getAuthConfigurations ();
0 commit comments